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