MADARA  3.2.3
KnowledgeBase.inl
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_BASE_INL_
2 #define _MADARA_KNOWLEDGE_BASE_INL_
3 
4 #include <fstream>
5 
6 namespace madara { namespace knowledge {
7 
16 : impl_ (new KnowledgeBaseImpl ()), context_ (0)
17 {
18 }
19 
21  const std::string & host, int transport)
22 : impl_ (new KnowledgeBaseImpl (host, transport)), context_ (0)
23 {
24 }
25 
27  const std::string & host, int transport,
28  const std::string & knowledge_domain)
29 : impl_ (new KnowledgeBaseImpl (host, transport, knowledge_domain)),
30  context_ (0)
31 {
32 }
33 
35  const std::string & host, const madara::transport::TransportSettings & settings)
36 : impl_ (new KnowledgeBaseImpl (host, settings)), context_ (0)
37 {
38 }
39 
40 template<typename T,
41  typename std::enable_if<std::is_integral<T>::value,
42  void*>::type>
43 inline int
45  const std::string & key,
46  T value,
47  const EvalSettings & settings)
48 {
49  int result = 0;
50 
51  if (impl_.get ())
52  {
53  result = impl_->set (key, (KnowledgeRecord::Integer)value, settings);
54  }
55  else if (context_)
56  {
57  result = context_->set (key, (KnowledgeRecord::Integer)value, settings);
58  }
59 
60  return result;
61 }
62 
63 template<typename T,
64  typename std::enable_if<std::is_integral<T>::value,
65  void*>::type>
66 inline int
68  const VariableReference & variable,
69  T value,
70  const EvalSettings & settings)
71 {
72  int result = 0;
73 
74  if (impl_.get ())
75  {
76  result = impl_->set (variable, (KnowledgeRecord::Integer)value, settings);
77  }
78  else if (context_)
79  {
80  result = context_->set (variable, (KnowledgeRecord::Integer)value, settings);
81  }
82 
83  return result;
84 }
85 
86 template<typename T,
87  typename std::enable_if<std::is_floating_point<T>::value,
88  void*>::type>
89 inline int
91  const std::string & key,
92  T value,
93  const EvalSettings & settings)
94 {
95  int result = 0;
96 
97  if (impl_.get ())
98  {
99  result = impl_->set (key, (double)value, settings);
100  }
101  else if (context_)
102  {
103  result = context_->set (key, (double)value, settings);
104  }
105 
106  return result;
107 }
108 
109 template<typename T,
110  typename std::enable_if<std::is_floating_point<T>::value,
111  void*>::type>
112 inline int
114  const VariableReference & variable,
115  T value,
116  const EvalSettings & settings)
117 {
118  int result = 0;
119 
120  if (impl_.get ())
121  {
122  result = impl_->set (variable, (double)value, settings);
123  }
124  else if (context_)
125  {
126  result = context_->set (variable, (double)value, settings);
127  }
128 
129  return result;
130 }
131 
132 inline void
134  ThreadSafeContext & original)
135 {
136  impl_ = 0;
137  context_ = &original;
138 }
139 
140 inline void
142 {
143  if (impl_.get ())
144  {
145  impl_->lock ();
146  }
147  else if (context_)
148  {
149  context_->lock ();
150  }
151 }
152 
153 inline void
155 {
156  if (impl_.get ())
157  {
158  impl_->unlock ();
159  }
160  else if (context_)
161  {
162  context_->unlock ();
163  }
164 }
165 
166 inline int
168 {
169  int result (0);
170 
171  if (impl_.get ())
172  {
173  result = impl_->get_log_level ();
174  }
175  else if (context_)
176  {
177  result = context_->get_log_level ();
178  }
179 
180  return result;
181 }
182 
183 inline madara::logger::Logger &
185 {
186  if (impl_.get ())
187  {
188  return impl_->get_logger ();
189  }
190  else
191  {
192  return context_->get_logger ();
193  }
194 }
195 
196 inline void
198  logger::Logger & logger) const
199 {
200  if (impl_.get ())
201  {
202  impl_->attach_logger (logger);
203  }
204  else
205  {
206  context_->attach_logger (logger);
207  }
208 }
209 
210 inline void
212 {
213  if (impl_.get ())
214  {
215  impl_->set_log_level (level);
216  }
217  else if (context_)
218  {
219  context_->set_log_level (level);
220  }
221 }
222 
223 
224 inline void
226  const KnowledgeBase & source,
227  const KnowledgeRequirements & reqs)
228 {
229  if (impl_.get () && source.impl_.get () != 0)
230  {
231  impl_->copy (*source.impl_.get (), reqs);
232  }
233  else if (context_ && source.impl_.get () != 0)
234  {
235  KnowledgeBaseImpl * source_impl =
236  (KnowledgeBaseImpl *)source.impl_.get ();
237  ThreadSafeContext * source_context = &(source_impl->get_context ());
238 
239  context_->copy (*source_context, reqs);
240  }
241 }
242 
243 inline void
245  const KnowledgeBase & source,
246  const CopySet & copy_set,
247  bool clean_copy)
248 {
249  if (impl_.get () && source.impl_.get () != 0)
250  {
251  impl_->copy (*source.impl_.get (), copy_set, clean_copy);
252  }
253  else if (context_ && source.impl_.get () != 0)
254  {
255  KnowledgeBaseImpl * source_impl =
256  (KnowledgeBaseImpl *)source.impl_.get ();
257  ThreadSafeContext * source_context = &(source_impl->get_context ());
258 
259  context_->copy (*source_context, copy_set, clean_copy);
260  }
261 }
262 
265 inline int
267  const EvalSettings & settings)
268 {
269  int result = 0;
270 
271  if (impl_.get ())
272  {
273  result = impl_->apply_modified (settings);
274  }
275  else if (context_)
276  {
278  }
279 
280  return result;
281 }
282 
283 inline void
285 {
286  if (impl_.get ())
287  {
288  impl_->close_transport ();
289  }
290 }
291 
292 inline KnowledgeRecord
294  const KnowledgeReferenceSettings & settings)
295 {
296  KnowledgeRecord result;
297 
298  if (impl_.get ())
299  {
300  result = impl_->get (key, settings);
301  }
302  else if (context_)
303  {
304  result = context_->get (key, settings);
305  }
306 
307  return result;
308 }
309 
310 inline VariableReference
312  const KnowledgeReferenceSettings & settings)
313 {
314  VariableReference var;
315 
316  if (impl_.get ())
317  {
318  var = impl_->get_ref (key, settings);
319  }
320  else if (context_)
321  {
322  var = context_->get_ref (key, settings);
323  }
324 
325  return var;
326 }
327 
328 inline KnowledgeRecord
330  const VariableReference & variable,
331  const KnowledgeReferenceSettings & settings)
332 {
333  KnowledgeRecord result;
334 
335  if (impl_.get ())
336  {
337  result = impl_->get (variable, settings);
338  }
339  else if (context_)
340  {
341  result = context_->get (variable, settings);
342  }
343 
344  return result;
345 }
346 
347 inline KnowledgeRecord
349  const std::string & key, size_t index,
350  const KnowledgeReferenceSettings & settings)
351 {
352  KnowledgeRecord result;
353 
354  if (impl_.get ())
355  {
356  result = impl_->retrieve_index (key, index, settings);
357  }
358  else if (context_)
359  {
360  result = context_->retrieve_index (key, index, settings);
361  }
362 
363  return result;
364 }
365 
366 inline KnowledgeRecord
368  const VariableReference & variable, size_t index,
369  const KnowledgeReferenceSettings & settings)
370 {
371  KnowledgeRecord result;
372 
373  if (impl_.get ())
374  {
375  result = impl_->retrieve_index (variable, index, settings);
376  }
377  else if (context_)
378  {
379  result = context_->retrieve_index (variable, index, settings);
380  }
381 
382  return result;
383 }
384 
385 
386 inline int
388  const std::string & knowledge_key,
389  const std::string & filename,
390  const EvalSettings & settings)
391 {
392  int result = 0;
393 
394  if (impl_.get ())
395  {
396  result = impl_->read_file (knowledge_key, filename, settings);
397  }
398  else if (context_)
399  {
400  result = context_->read_file (knowledge_key, filename, settings);
401  }
402 
403  return result;
404 }
405 
406 inline int
408  const VariableReference & variable,
409  const std::string & filename,
410  const EvalSettings & settings)
411 {
412  int result = 0;
413 
414  if (impl_.get ())
415  {
416  result = impl_->read_file (variable, filename, settings);
417  }
418  else if (context_)
419  {
420  result = context_->read_file (variable, filename, settings);
421  }
422 
423  return result;
424 }
425 
426 inline int
428  const std::string & key,
429  const unsigned char * value, size_t size,
430  const EvalSettings & settings)
431 {
432  int result = -1;
433 
434  if (key != "")
435  {
436  if (impl_.get ())
437  {
438  result = impl_->set_file (
439  impl_->get_ref (key, settings), value, size, settings);
440  }
441  else if (context_)
442  {
443  result = context_->set_file (
444  context_->get_ref (key, settings), value, size, settings);
445  }
446  }
447 
448  return result;
449 }
450 
451 inline int
453  const VariableReference & variable,
454  const unsigned char * value, size_t size,
455  const EvalSettings & settings)
456 {
457  int result = 0;
458 
459  if (impl_.get ())
460  {
461  result = impl_->set_file (variable, value, size, settings);
462  }
463  else if (context_)
464  {
465  result = context_->set_file (variable, value, size, settings);
466  }
467 
468  return result;
469 }
470 
471 inline int
473  const std::string & key,
474  const unsigned char * value, size_t size,
475  const EvalSettings & settings)
476 {
477  int result = -1;
478 
479  if (key != "")
480  {
481  if (impl_.get ())
482  {
483  result = impl_->set_jpeg (
484  impl_->get_ref (key, settings), value, size, settings);
485  }
486  else if (context_)
487  {
488  result = context_->set_jpeg (
489  impl_->get_ref (key, settings), value, size, settings);
490  }
491  }
492 
493  return result;
494 }
495 
496 inline int
498  const VariableReference & variable,
499  const unsigned char * value, size_t size,
500  const EvalSettings & settings)
501 {
502  int result = 0;
503 
504  if (impl_.get ())
505  {
506  result = impl_->set_jpeg (variable, value, size, settings);
507  }
508  else if (context_)
509  {
510  result = context_->set_jpeg (variable, value, size, settings);
511  }
512 
513  return result;
514 }
515 
516 inline ssize_t
517 KnowledgeBase::write_file (const std::string & knowledge_key,
518  const std::string & filename)
519 {
520  ssize_t result = 0;
521 
522  if (impl_.get ())
523  {
524  result = impl_->write_file (knowledge_key, filename);
525  }
526 
527  return result;
528 }
529 
530 
531 inline std::string
533  const std::string & statement)
534 {
535  std::string result;
536 
537  if (impl_.get ())
538  {
539  result = impl_->expand_statement (statement);
540  }
541  else if (context_)
542  {
543  result = context_->expand_statement (statement);
544  }
545 
546  return result;
547 }
548 
549 inline void
551  const VariableReference & variable,
552  const KnowledgeUpdateSettings & /*settings*/)
553 {
554  if (impl_.get ())
555  {
556  impl_->mark_modified (variable);
557  }
558  else if (context_)
559  {
560  context_->mark_modified (variable);
561  }
562 }
563 
564 inline void
566  const std::string & name,
567  const KnowledgeUpdateSettings & settings)
568 {
569  if (impl_.get ())
570  {
571  impl_->mark_modified (name, settings);
572  }
573  else if (context_)
574  {
575  context_->mark_modified (name, settings);
576  }
577 }
578 
579 inline int
581  const VariableReference & variable,
582  KnowledgeRecord && value,
583  const EvalSettings & settings)
584 {
585  int result = 0;
586 
587  if (impl_.get ())
588  {
589  result = impl_->set (variable, std::move(value), settings);
590  }
591  else if (context_)
592  {
593  result = context_->set (variable, std::move(value), settings);
594  }
595 
596  return result;
597 }
598 
599 inline int
601  const std::string & key,
602  const KnowledgeRecord & value,
603  const EvalSettings & settings)
604 {
605  int result = 0;
606 
607  if (impl_.get ())
608  {
609  result = impl_->set (key, value, settings);
610  }
611  else if (context_)
612  {
613  result = context_->set (key, value, settings);
614  }
615 
616  return result;
617 }
618 
619 inline int
621  const std::string & key,
622  KnowledgeRecord && value,
623  const EvalSettings & settings)
624 {
625  int result = 0;
626 
627  if (impl_.get ())
628  {
629  result = impl_->set (key, std::move(value), settings);
630  }
631  else if (context_)
632  {
633  result = context_->set (key, std::move(value), settings);
634  }
635 
636  return result;
637 }
638 
639 inline int
641  const VariableReference & variable,
642  const KnowledgeRecord & value,
643  const EvalSettings & settings)
644 {
645  int result = 0;
646 
647  if (impl_.get ())
648  {
649  result = impl_->set (variable, value, settings);
650  }
651  else if (context_)
652  {
653  result = context_->set (variable, value, settings);
654  }
655 
656  return result;
657 }
658 
659 template<typename T>
660 inline int
662  const std::string & key,
663  size_t index, T&& value,
664  const EvalSettings & settings)
665 {
666  int result = 0;
667 
668  if (impl_.get ())
669  {
670  result = impl_->set_index (key, index, std::forward<T>(value), settings);
671  }
672  else if (context_)
673  {
674  result = context_->set_index (key, index, std::forward<T>(value), settings);
675  }
676 
677  return result;
678 }
679 
680 template<typename T>
681 inline int
683  const VariableReference & variable,
684  size_t index, T&& value,
685  const EvalSettings & settings)
686 {
687  int result = 0;
688 
689  if (impl_.get ())
690  {
691  result = impl_->set_index (variable, index, std::forward<T>(value), settings);
692  }
693  else if (context_)
694  {
695  result = context_->set_index (variable, index, std::forward<T>(value), settings);
696  }
697 
698  return result;
699 }
700 
701 inline int
703  const std::string & key,
704  const KnowledgeRecord::Integer * value,
705  uint32_t size,
706  const EvalSettings & settings)
707 {
708  int result = 0;
709 
710  if (impl_.get ())
711  {
712  result = impl_->set (key, value, size, settings);
713  }
714  else if (context_)
715  {
716  result = context_->set (key, value, size, settings);
717  }
718 
719  return result;
720 }
721 
722 inline int
724  const VariableReference & variable,
725  const KnowledgeRecord::Integer * value,
726  uint32_t size,
727  const EvalSettings & settings)
728 {
729  int result = 0;
730 
731  if (impl_.get ())
732  {
733  result = impl_->set (variable, value, size, settings);
734  }
735  else if (context_)
736  {
737  result = context_->set (variable, value, size, settings);
738  }
739 
740  return result;
741 }
742 
743 inline int
745  const std::string & key,
746  const std::vector <KnowledgeRecord::Integer> & value,
747  const EvalSettings & settings)
748 {
749  int result = 0;
750 
751  if (impl_.get ())
752  {
753  result = impl_->set (key, value, settings);
754  }
755  else if (context_)
756  {
757  result = context_->set (key, value, settings);
758  }
759 
760  return result;
761 }
762 
763 inline int
765  const VariableReference & variable,
766  const std::vector <KnowledgeRecord::Integer> & value,
767  const EvalSettings & settings)
768 {
769  int result = 0;
770 
771  if (impl_.get ())
772  {
773  result = impl_->set (variable, value, settings);
774  }
775  else if (context_)
776  {
777  result = context_->set (variable, value, settings);
778  }
779 
780  return result;
781 }
782 
783 inline int
785  const std::string & key,
786  std::vector <KnowledgeRecord::Integer> && value,
787  const EvalSettings & settings)
788 {
789  int result = 0;
790 
791  if (impl_.get ())
792  {
793  result = impl_->set (key, std::move(value), settings);
794  }
795  else if (context_)
796  {
797  result = context_->set (key, std::move(value), settings);
798  }
799 
800  return result;
801 }
802 
803 inline int
805  const VariableReference & variable,
806  std::vector <KnowledgeRecord::Integer> && value,
807  const EvalSettings & settings)
808 {
809  int result = 0;
810 
811  if (impl_.get ())
812  {
813  result = impl_->set (variable, std::move(value), settings);
814  }
815  else if (context_)
816  {
817  result = context_->set (variable, std::move(value), settings);
818  }
819 
820  return result;
821 }
822 
823 inline int
825  const std::string & key,
826  const double * value,
827  uint32_t size,
828  const EvalSettings & settings)
829 {
830  int result = 0;
831 
832  if (impl_.get ())
833  {
834  result = impl_->set (key, value, size, settings);
835  }
836  else if (context_)
837  {
838  result = context_->set (key, value, size, settings);
839  }
840 
841  return result;
842 }
843 
844 inline int
846  const VariableReference & variable,
847  const double * value,
848  uint32_t size,
849  const EvalSettings & settings)
850 {
851  int result = 0;
852 
853  if (impl_.get ())
854  {
855  result = impl_->set (variable, value, size, settings);
856  }
857  else if (context_)
858  {
859  result = context_->set (variable, value, size, settings);
860  }
861 
862  return result;
863 }
864 
865 inline int
867  const std::string & key,
868  const std::vector <double> & value,
869  const EvalSettings & settings)
870 {
871  int result = 0;
872 
873  if (impl_.get ())
874  {
875  result = impl_->set (key, value, settings);
876  }
877  else if (context_)
878  {
879  result = context_->set (key, value, settings);
880  }
881 
882  return result;
883 }
884 
885 inline int
887  const std::string & key,
888  std::vector <double> && value,
889  const EvalSettings & settings)
890 {
891  int result = 0;
892 
893  if (impl_.get ())
894  {
895  result = impl_->set (key, std::move(value), settings);
896  }
897  else if (context_)
898  {
899  result = context_->set (key, std::move(value), settings);
900  }
901 
902  return result;
903 }
904 
905 inline int
907  const VariableReference & variable,
908  const std::vector <double> & value,
909  const EvalSettings & settings)
910 {
911  int result = 0;
912 
913  if (impl_.get ())
914  {
915  result = impl_->set (variable, value, settings);
916  }
917  else if (context_)
918  {
919  result = context_->set (variable, value, settings);
920  }
921 
922  return result;
923 }
924 
925 inline int
927  const VariableReference & variable,
928  std::vector <double> && value,
929  const EvalSettings & settings)
930 {
931  int result = 0;
932 
933  if (impl_.get ())
934  {
935  result = impl_->set (variable, std::move(value), settings);
936  }
937  else if (context_)
938  {
939  result = context_->set (variable, std::move(value), settings);
940  }
941 
942  return result;
943 }
944 
945 inline int
947  const std::string & value,
948  const EvalSettings & settings)
949 {
950  int result = 0;
951 
952  if (impl_.get ())
953  {
954  result = impl_->set (key, value, settings);
955  }
956  else if (context_)
957  {
958  result = context_->set (key, value, settings);
959  }
960 
961  return result;
962 }
963 
964 inline int
966  const VariableReference & variable,
967  const std::string & value,
968  const EvalSettings & settings)
969 {
970  int result = 0;
971 
972  if (impl_.get ())
973  {
974  result = impl_->set (variable, value, settings);
975  }
976  else if (context_)
977  {
978  result = context_->set (variable, value, settings);
979  }
980 
981  return result;
982 }
983 
984 inline int
986  std::string && value,
987  const EvalSettings & settings)
988 {
989  int result = 0;
990 
991  if (impl_.get ())
992  {
993  result = impl_->set (key, std::move(value), settings);
994  }
995  else if (context_)
996  {
997  result = context_->set (key, std::move(value), settings);
998  }
999 
1000  return result;
1001 }
1002 
1003 inline int
1005  const VariableReference & variable,
1006  std::string && value,
1007  const EvalSettings & settings)
1008 {
1009  int result = 0;
1010 
1011  if (impl_.get ())
1012  {
1013  result = impl_->set (variable, std::move(value), settings);
1014  }
1015  else if (context_)
1016  {
1017  result = context_->set (variable, std::move(value), settings);
1018  }
1019 
1020  return result;
1021 }
1022 
1023 
1025 inline void
1027  const std::string & key, uint32_t quality,
1028  const KnowledgeReferenceSettings & settings)
1029 {
1030  if (impl_.get ())
1031  {
1032  impl_->set_quality (key, quality, settings);
1033  }
1034  else if (context_)
1035  {
1036  context_->set_quality (key, quality, true, settings);
1037  }
1038 }
1039 
1040 
1041 inline bool
1043  const std::string & key,
1044  const KnowledgeReferenceSettings & settings) const
1045 {
1046  bool result = false;
1047 
1048  if (impl_.get ())
1049  {
1050  result = impl_->exists (key, settings);
1051  }
1052  else if (context_)
1053  {
1054  result = context_->exists (key, settings);
1055  }
1056 
1057  return result;
1058 }
1059 
1060 inline bool
1062  const VariableReference & variable,
1063  const KnowledgeReferenceSettings & settings) const
1064 {
1065  bool result = false;
1066 
1067  if (impl_.get ())
1068  {
1069  result = impl_->exists (variable, settings);
1070  }
1071  else if (context_)
1072  {
1073  result = context_->exists (variable, settings);
1074  }
1075 
1076  return result;
1077 }
1078 
1081 {
1082  return impl_->transport_settings ();
1083 }
1084 
1085 inline void
1087  unsigned int level) const
1088 {
1089  if (impl_.get ())
1090  {
1091  impl_->print (level);
1092  }
1093  else if (context_)
1094  {
1095  context_->print (level);
1096  }
1097 }
1098 
1099 inline void
1101  unsigned int level) const
1102 {
1103  if (impl_.get ())
1104  {
1105  impl_->print (level);
1106  }
1107  else if (context_)
1108  {
1109  context_->print (level);
1110  }
1111 }
1112 
1113 inline void
1115  std::string & target,
1116  const std::string & array_delimiter,
1117  const std::string & record_delimiter,
1118  const std::string & key_val_delimiter) const
1119 {
1120  if (impl_.get ())
1121  {
1122  impl_->to_string (target,
1123  array_delimiter, record_delimiter, key_val_delimiter);
1124  }
1125  else if (context_)
1126  {
1127  context_->to_string (target,
1128  array_delimiter, record_delimiter, key_val_delimiter);
1129  }
1130 }
1131 
1132 
1133 inline void
1135  const std::string & statement, unsigned int level) const
1136 {
1137  if (impl_.get ())
1138  {
1139  impl_->print (statement, level);
1140  }
1141  else if (context_)
1142  {
1143  context_->print (level);
1144  }
1145 }
1146 
1147 inline bool
1149  const KnowledgeReferenceSettings & settings)
1150 {
1151  bool result (false);
1152 
1153  if (impl_.get ())
1154  {
1155  result = impl_->clear (key, settings);
1156  }
1157  else if (context_)
1158  {
1159  result = context_->clear (key, settings);
1160  }
1161 
1162  return result;
1163 }
1164 
1165 inline void
1167 {
1168  if (impl_.get ())
1169  {
1170  impl_->clear (erase);
1171  }
1172  else if (context_)
1173  {
1174  context_->clear (erase);
1175  }
1176 }
1177 
1178 inline void
1180 {
1181  if (impl_.get ())
1182  {
1183  impl_->clear_map ();
1184  }
1185  else if (context_)
1186  {
1187  context_->clear ();
1188  }
1189 }
1190 
1191 inline void
1193 {
1194  if (impl_.get ())
1195  {
1196  impl_->acquire ();
1197  }
1198  else if (context_)
1199  {
1200  context_->lock ();
1201  }
1202 }
1203 
1204 inline void
1206 {
1207  if (impl_.get ())
1208  {
1209  impl_->release ();
1210  }
1211  else if (context_)
1212  {
1213  context_->unlock ();
1214  }
1215 }
1216 
1217 #ifndef _MADARA_NO_KARL_
1218 
1219 inline CompiledExpression
1221  const std::string & expression)
1222 {
1223  CompiledExpression result;
1224 
1225  if (impl_.get ())
1226  {
1227  result = impl_->compile (expression);
1228  }
1229 
1230  return result;
1231 }
1232 
1233 // evaluate a knowledge expression and choose to send any modifications
1234 inline KnowledgeRecord
1236  const std::string & expression,
1237  const EvalSettings & settings)
1238 {
1239  KnowledgeRecord result;
1240 
1241  if (impl_.get ())
1242  {
1243  result = impl_->evaluate (expression, settings);
1244  }
1245  else if (context_)
1246  {
1247  CompiledExpression ce = context_->compile (expression);
1248  result = context_->evaluate (ce, settings);
1249  }
1250 
1251  return result;
1252 }
1253 
1254 // evaluate a knowledge expression and choose to send any modifications
1255 inline KnowledgeRecord
1257  CompiledExpression & expression,
1258  const EvalSettings & settings)
1259 {
1260  KnowledgeRecord result;
1261 
1262  if (impl_.get ())
1263  {
1264  result = impl_->evaluate (expression, settings);
1265  }
1266  else if (context_)
1267  {
1268  result = context_->evaluate (expression, settings);
1269  }
1270 
1271  return result;
1272 }
1273 
1274 // evaluate a knowledge expression and choose to send any modifications
1275 inline KnowledgeRecord
1278  const EvalSettings & settings)
1279 {
1280  KnowledgeRecord result;
1281 
1282  if (impl_.get ())
1283  {
1284  result = impl_->evaluate (root, settings);
1285  }
1286  else if (context_)
1287  {
1288  result = context_->evaluate (root, settings);
1289  }
1290 
1291  return result;
1292 }
1293 
1294 
1295 // Defines a function
1296 inline void
1298  const std::string & name,
1299  KnowledgeRecord (*func) (const char *, FunctionArguments &, Variables &))
1300 {
1301  if (impl_.get ())
1302  {
1303  impl_->define_function (name, func);
1304  }
1305  else if (context_)
1306  {
1307  context_->define_function (name, func);
1308  }
1309 }
1310 
1311 // Defines a function
1312 inline void
1314  const std::string & name,
1316 {
1317  if (impl_.get ())
1318  {
1319  impl_->define_function (name, func);
1320  }
1321  else if (context_)
1322  {
1323  context_->define_function (name, func);
1324  }
1325 }
1326 
1327 #ifdef _MADARA_JAVA_
1328 // Defines a function
1329 inline void
1331  const std::string & name, jobject func)
1332 {
1333  if (impl_.get ())
1334  {
1335  impl_->define_function (name, func);
1336  }
1337  else if (context_)
1338  {
1339  context_->define_function (name, func);
1340  }
1341 }
1342 #endif
1343 
1344 #ifdef _MADARA_PYTHON_CALLBACKS_
1345 
1346 // Defines a function
1347 inline void
1349  const std::string & name, boost::python::object callable)
1350 {
1351  if (impl_.get ())
1352  {
1353  impl_->define_function (name, callable);
1354  }
1355  else if (context_)
1356  {
1357  context_->define_function (name, callable);
1358  }
1359 }
1360 
1361 #endif
1362 
1368 inline void
1370  const std::string & expression)
1371 {
1372  if (impl_.get ())
1373  {
1374  impl_->define_function (name, expression);
1375  }
1376  else if (context_)
1377  {
1378  context_->define_function (name, expression);
1379  }
1380 }
1381 
1387 inline void
1389  const CompiledExpression & expression)
1390 {
1391  if (impl_.get ())
1392  {
1393  impl_->define_function (name, expression);
1394  }
1395  else if (context_)
1396  {
1397  context_->define_function (name, expression);
1398  }
1399 }
1400 
1401 inline KnowledgeRecord
1403  const std::string & expression,
1404  const WaitSettings & settings)
1405 {
1406  KnowledgeRecord result;
1407 
1408  if (context_)
1409  {
1410  CompiledExpression ce = context_->compile (expression);
1411  result = this->wait (ce, settings);
1412  }
1413  else if (impl_.get ())
1414  {
1415  result = impl_->wait (expression, settings);
1416  }
1417 
1418  return result;
1419 }
1420 
1421 #endif // _MADARA_NO_KARL_
1422 inline void
1424 {
1425  if (impl_.get ())
1426  {
1427  impl_->activate_transport ();
1428  }
1429 }
1430 
1431 inline size_t
1433  madara::transport::Base * transport)
1434 {
1435  size_t result = 0;
1436 
1437  if (impl_.get ())
1438  {
1439  result = impl_->attach_transport (transport);
1440  }
1441 
1442  return result;
1443 }
1444 
1445 inline size_t
1447 {
1448  size_t result (0);
1449 
1450  if (impl_.get ())
1451  {
1452  result = impl_->get_num_transports ();
1453  }
1454 
1455  return result;
1456 }
1457 
1458 inline size_t
1460  transport::TransportSettings & settings)
1461 {
1462  size_t result = 0;
1463 
1464  if (impl_.get ())
1465  {
1466  result = impl_->attach_transport (id, settings);
1467  }
1468 
1469  return result;
1470 }
1471 
1472 inline size_t
1474  size_t index)
1475 {
1476  size_t result = 0;
1477 
1478  if (impl_.get ())
1479  {
1480  result = impl_->remove_transport (index);
1481  }
1482 
1483  return result;
1484 }
1485 
1486 inline ThreadSafeContext &
1488 {
1489  ThreadSafeContext * result = 0;
1490 
1491  if (context_)
1492  {
1493  result = context_;
1494  }
1495  else if (impl_.get ())
1496  {
1497  result = &(impl_->get_context ());
1498  }
1499 
1500  return *result;
1501 }
1502 
1503 inline void
1505 {
1506  if (context_)
1507  {
1509  }
1510  else if (impl_.get ())
1511  {
1512  impl_->clear_modifieds ();
1513  }
1514 }
1515 
1516 inline void
1518 {
1519  if (context_)
1520  {
1521  context_->add_modifieds (modifieds);
1522  }
1523  else if (impl_.get ())
1524  {
1525  impl_->add_modifieds (modifieds);
1526  }
1527 }
1528 
1529 inline VariableReferences
1531 {
1532  VariableReferences default_result;
1533 
1534  if (context_)
1535  {
1536  return context_->save_modifieds ();
1537  }
1538  else if (impl_.get ())
1539  {
1540  return impl_->save_modifieds ();
1541  }
1542 
1543  return default_result;
1544 }
1545 
1546 inline int
1548  const std::string & prefix,
1549  const EvalSettings & settings)
1550 {
1551  int result = 0;
1552 
1553  if (impl_.get ())
1554  {
1555  result = impl_->send_modifieds (prefix, settings);
1556  }
1557 
1558  return result;
1559 }
1560 
1561 inline std::string
1563 {
1564  std::string result = "";
1565 
1566  if (context_)
1567  {
1568  result = context_->debug_modifieds ();
1569  }
1570  else if (impl_.get ())
1571  {
1572  result = impl_->debug_modifieds ();
1573  }
1574 
1575  return result;
1576 }
1577 
1582 inline std::string
1584 {
1585  std::string result;
1586 
1587  if (impl_.get ())
1588  {
1589  result = impl_->get_id ();
1590  }
1591 
1592  return result;
1593 }
1594 
1609 inline size_t
1611  const std::string & subject,
1612  unsigned int start,
1613  unsigned int end,
1614  std::vector <KnowledgeRecord> & target)
1615 {
1616  size_t result = 0;
1617 
1618  if (context_)
1619  {
1620  result = context_->to_vector (subject, start, end, target);
1621  }
1622  else if (impl_.get ())
1623  {
1624  result = impl_->to_vector (subject, start, end, target);
1625  }
1626 
1627  return result;
1628 }
1629 
1630 inline void
1632  const std::string & suffix, VariableReferences & matches)
1633 {
1634  if (context_)
1635  {
1636  context_->get_matches (prefix, suffix, matches);
1637  }
1638  else if (impl_.get ())
1639  {
1640  impl_->get_matches (prefix, suffix, matches);
1641  }
1642 }
1643 
1645  ThreadSafeContext & context)
1646 {
1647  context_ = &context;
1648  impl_ = 0;
1649 }
1650 
1651 inline size_t
1653  const std::string & expression,
1654  std::map <std::string, KnowledgeRecord> & target)
1655 {
1656  size_t result = 0;
1657 
1658  if (context_)
1659  {
1660  result = context_->to_map (expression, target);
1661  }
1662  else if (impl_.get ())
1663  {
1664  result = impl_->to_map (expression, target);
1665  }
1666 
1667  return result;
1668 }
1669 
1670 inline size_t
1672  const std::string & prefix,
1673  const std::string & delimiter,
1674  const std::string & suffix,
1675  std::vector <std::string> & next_keys,
1676  std::map <std::string, KnowledgeRecord> & result,
1677  bool just_keys)
1678 {
1679  size_t result_size = 0;
1680 
1681  if (context_)
1682  {
1683  result_size = context_->to_map (
1684  prefix, delimiter, suffix, next_keys, result, just_keys);
1685  }
1686  else if (impl_.get ())
1687  {
1688  result_size = impl_->to_map (
1689  prefix, delimiter, suffix, next_keys, result, just_keys);
1690  }
1691 
1692  return result_size;
1693 }
1694 
1695 inline KnowledgeMap
1697  const std::string & prefix) const
1698 {
1699  if (context_)
1700  {
1701  return context_->to_map (prefix);
1702  }
1703  else if (impl_.get ())
1704  {
1705  return impl_->to_map (prefix);
1706  }
1707 
1708  return KnowledgeMap();
1709 }
1710 
1711 inline KnowledgeMap
1713  const std::string & prefix) const
1714 {
1715  if (context_)
1716  {
1717  return context_->to_map_stripped (prefix);
1718  }
1719  else if (impl_.get ())
1720  {
1721  return impl_->to_map_stripped (prefix);
1722  }
1723 
1724  return KnowledgeMap();
1725 }
1726 
1727 inline int64_t
1729  const std::string & filename) const
1730 {
1731  int64_t result = 0;
1732 
1733  if (context_)
1734  {
1735  result = context_->save_context (filename);
1736  }
1737  else if (impl_.get ())
1738  {
1739  result = impl_->save_context (filename);
1740  }
1741 
1742  return result;
1743 }
1744 
1745 inline int64_t
1747  CheckpointSettings & settings) const
1748 {
1749  int64_t result = 0;
1750 
1751  if (context_)
1752  {
1753  result = context_->save_context (settings);
1754  }
1755  else if (impl_.get ())
1756  {
1757  result = impl_->save_context (settings);
1758  }
1759 
1760  return result;
1761 }
1762 
1763 
1764 inline int64_t
1766  const std::string & filename) const
1767 {
1768  int64_t result = 0;
1769 
1770  if (context_)
1771  {
1772  result = context_->save_as_json (filename);
1773  }
1774  else if (impl_.get ())
1775  {
1776  result = impl_->save_as_json (filename);
1777  }
1778 
1779  return result;
1780 }
1781 
1782 inline int64_t
1784  const CheckpointSettings & settings) const
1785 {
1786  int64_t result = 0;
1787 
1788  if (context_)
1789  {
1790  result = context_->save_as_json (settings);
1791  }
1792  else if (impl_.get ())
1793  {
1794  result = impl_->save_as_json (settings);
1795  }
1796 
1797  return result;
1798 }
1799 
1800 inline int64_t
1802  const std::string & filename) const
1803 {
1804  int64_t result = 0;
1805 
1806  if (context_)
1807  {
1808  result = context_->save_as_karl (filename);
1809  }
1810  else if (impl_.get ())
1811  {
1812  result = impl_->save_as_karl (filename);
1813  }
1814 
1815  return result;
1816 }
1817 
1818 inline int64_t
1820  const CheckpointSettings & settings) const
1821 {
1822  int64_t result = 0;
1823 
1824  if (context_)
1825  {
1826  result = context_->save_as_karl (settings);
1827  }
1828  else if (impl_.get ())
1829  {
1830  result = impl_->save_as_karl (settings);
1831  }
1832 
1833  return result;
1834 }
1835 
1836 
1837 inline int64_t
1839  const std::string & filename,
1840  bool reset_modifieds)
1841 {
1842  int64_t result = 0;
1843 
1844  if (context_)
1845  {
1846  result = context_->save_checkpoint (filename);
1847  }
1848  else if (impl_.get ())
1849  {
1850  result = impl_->save_checkpoint (filename, reset_modifieds);
1851  }
1852 
1853  return result;
1854 }
1855 
1856 inline int64_t
1858  CheckpointSettings & settings) const
1859 {
1860  int64_t result = 0;
1861 
1862  if (context_)
1863  {
1864  result = context_->save_checkpoint (settings);
1865  }
1866  else if (impl_.get ())
1867  {
1868  result = impl_->save_checkpoint (settings);
1869  }
1870 
1871  return result;
1872 }
1873 
1874 inline int64_t
1876  const std::string & filename,
1877  bool use_id,
1878  const KnowledgeUpdateSettings & settings)
1879 {
1880  int64_t result = 0;
1881 
1882  if (context_)
1883  {
1884  std::string id;
1885  result = context_->load_context (filename, id, settings);
1886  }
1887  else if (impl_.get ())
1888  {
1889  result = impl_->load_context (filename, use_id, settings);
1890  }
1891 
1892  return result;
1893 }
1894 
1895 inline int64_t
1897 const std::string & filename,
1898 FileHeader & meta,
1899 bool use_id,
1900 const KnowledgeUpdateSettings & settings)
1901 {
1902  int64_t result = 0;
1903 
1904  if (context_)
1905  {
1906  result = context_->load_context (filename, meta, settings);
1907  }
1908  else if (impl_.get ())
1909  {
1910  result = impl_->load_context (filename, meta, use_id, settings);
1911  }
1912 
1913  return result;
1914 }
1915 
1916 inline int64_t
1918 CheckpointSettings & checkpoint_settings,
1919 const KnowledgeUpdateSettings & update_settings)
1920 {
1921  int64_t result = 0;
1922 
1923  if (context_)
1924  {
1925  result = context_->load_context (checkpoint_settings, update_settings);
1926  }
1927  else if (impl_.get ())
1928  {
1929  result = impl_->load_context (checkpoint_settings, update_settings);
1930  }
1931 
1932  return result;
1933 }
1934 
1935 inline void
1937 {
1938  if (context_)
1939  {
1941  }
1942  else if (impl_.get ())
1943  {
1944  impl_->wait_for_change ();
1945  }
1946 }
1947 
1948 inline std::string
1950  const std::string & host)
1951 {
1952  std::string result;
1953 
1954  if (impl_.get ())
1955  {
1956  result = impl_->setup_unique_hostport (host);
1957  }
1958 
1959  return result;
1960 }
1961 
1962 inline int
1964 {
1965  return apply_modified (settings);
1966 }
1967 
1968 } }
1969 
1970 #endif // _MADARA_KNOWLEDGE_BASE_INL_
This class encapsulates an entry in a KnowledgeBase.
std::map< std::string, bool > CopySet
Typedef for set of copyable keys.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
This class provides a distributed knowledge base implementation.
int read_file(const std::string &knowledge_key, const std::string &filename, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Read a file into the knowledge base.
int apply_modified(const EvalSettings &settings=EvalSettings())
Applies current time and modified to all global variables and tries to send them. ...
Defines a file header which is the default for KaRL checkpointing.
Definition: FileHeader.h:36
size_t remove_transport(size_t index)
Removes a transport.
int64_t save_context(const std::string &filename) const
Saves the context to a file.
void get_matches(const std::string &prefix, const std::string &suffix, VariableReferences &matches)
Creates an iteration of VariableReferences to all keys matching the prefix and suffix.
helper type for specifying template type parameters using a function argument instead of inside expli...
Definition: KnowledgeCast.h:72
size_t to_vector(const std::string &subject, unsigned int start, unsigned int end, std::vector< KnowledgeRecord > &target)
Fills a vector with Knowledge Records that begin with a common subject and have a finite range of int...
int64_t save_context(const std::string &filename, const std::string &id="") const
Saves the context to a file.
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
void unlock(void)
Unlocks the context to allow updates over the network (is only necessary if the context has been expl...
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
size_t get_num_transports(void)
Gets the number of transports.
void copy(const ThreadSafeContext &source, const KnowledgeRequirements &settings)
Copies variables and values from source to this context.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
int set_index(const VariableReference &variable, size_t index, T &&value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of an array index to a value.
void lock(void) const
Locks the mutex on this context.
int send_modifieds(const std::string &prefix="KnowledgeBase::send_modifieds", const EvalSettings &settings=EvalSettings())
Sends all modified variables through the attached transports.
This class stores variables and their values for use by any entity needing state information in a thr...
int64_t load_context(const std::string &filename, std::string &id, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
std::string get_id(void)
Returns the unique host and ephemeral binding for this Knowlede Base.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
int get_log_level(void)
Gets the log level.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
Holds settings for checkpoints to load or save.
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
std::string setup_unique_hostport(const std::string &host="")
Binds to an ephemeral port for unique tie breakers in global ordering.
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
Holds basic transport settings.
int get_log_level(void)
Gets the log level.
Optimized reference to a variable within the knowledge base.
Compiled, optimized KaRL logic.
std::vector< KnowledgeRecord > FunctionArguments
void lock(void)
Locks the context to prevent updates over the network.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &), const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Defines an external function.
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the knowledge base to a specified location.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a knowledge value.
std::shared_ptr< KnowledgeBaseImpl > impl_
Pointer to actual implementation, i.e., the "bridge", which is reference counted to automate memory m...
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
madara::transport::TransportSettings & transport_settings(void)
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
void apply_modified(void)
Changes all global variables to modified at current clock.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
void print_knowledge(unsigned int level=0) const
Deprecated alias for.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
knowledge::KnowledgeRecord evaluate(CompiledExpression expression, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Evaluate a compiled expression.
int64_t save_checkpoint(const std::string &filename, const std::string &id="") const
Saves a checkpoint of a list of changes to a file.
static struct madara::knowledge::tags::string_t string
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
ThreadSafeContext * context_
A knowledge base can also be a facade for another knowledge base.
void to_string(std::string &target, const std::string &array_delimiter=",", const std::string &record_delimiter=";\n", const std::string &key_val_delimiter="=") const
Saves all keys and values into a string, using the underlying knowledge::KnowledgeRecord::to_string f...
madara::knowledge::KnowledgeRecord wait(const std::string &expression, const WaitSettings &settings=WaitSettings())
Waits for an expression to be non-zero.
void facade_for(ThreadSafeContext &target)
Change the knowledge base to become a facade for another context.
void clear_modifieds(void)
Clear all modifications to the knowledge base.
Holds settings requirements for knowledge, usually in copying.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
void unlock(void) const
Unlocks the mutex on this context.
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
::std::map< std::string, KnowledgeRecord > KnowledgeMap
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
An abstract base class defines a simple abstract implementation of an expression tree node...
Definition: ComponentNode.h:36
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
std::string expand_statement(const std::string &statement)
Expands a statement using variable expansion.
int modify(const EvalSettings &settings=EvalSettings())
Alias for apply_modified.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a JPEG image.
void get_matches(const std::string &prefix, const std::string &suffix, VariableReferences &matches)
Creates an iteration of VariableReferences to all keys matching the prefix and suffix.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int set_file(const std::string &key, const unsigned char *value, size_t size, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to an arbitrary string.
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
size_t to_vector(const std::string &subject, unsigned int start, unsigned int end, std::vector< KnowledgeRecord > &target)
Fills a vector with Knowledge Records that begin with a common subject and have a finite range of int...
void copy(const KnowledgeBase &source, const KnowledgeRequirements &settings)
Copies variables and values from source to this context.
std::vector< VariableReference > VariableReferences
a vector of variable references
void clear_map(void)
Clears the knowledge base.
void close_transport(void)
Closes the transport mechanism so no dissemination is possible.
void set_quality(const std::string &key, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Encapsulates settings for an evaluation statement.
Definition: EvalSettings.h:27
int set(const VariableReference &variable, const std::string &value, const EvalSettings &settings=EvalSettings(false, false, true, false, false))
Atomically sets the value of a variable to a string.
Provides functions and classes for the distributed knowledge base.
madara::knowledge::KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Retrieves a value at a specified index within a knowledge array.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
Checks if a knowledge location exists in the context.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression, const EvalSettings &settings=EvalSettings())
Evaluates an expression.
knowledge::KnowledgeMap to_map_stripped(const std::string &prefix) const
Creates a map with Knowledge Records that begin with the given prefix.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
Settings for applying knowledge updates.
void to_string(std::string &target, const std::string &array_delimiter=",", const std::string &record_delimiter=";\n", const std::string &key_val_delimiter="=") const
Saves all keys and values into a string, using the underlying knowledge::KnowledgeRecord::to_string f...
int64_t load_context(const std::string &filename, bool use_id=true, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
Copyright (c) 2015 Carnegie Mellon University.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a value at a specified index within a knowledge array.
void activate_transport(void)
Starts the transport mechanism for dissemination if it is closed.
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
size_t attach_transport(madara::transport::Base *transport)
Attaches a transport to the Knowledge Engine.
Settings for applying knowledge updates.
Base class from which all transports must be derived.
Definition: Transport.h:45
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
Provides an interface for external functions into the MADARA KaRL variable settings.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
void acquire(void)
Acquires the recursive lock on the knowledge base.
void set_log_level(int level)
Sets the log level.
int64_t save_as_json(const std::string &filename) const
Saves the context to a file as JSON.
void print(unsigned int level=0) const
Prints all knowledge variables and values in the context.
void set_log_level(int level)
Sets the log level.
void use(ThreadSafeContext &original)
Refer to and use another knowledge base&#39;s context.
void wait_for_change(void)
Wait for a change to happen to the context (e.g., from transports)
void release(void)
Releases a recursive lock on the knowledge base.
void reset_modified(void)
Reset all variables to be unmodified.