MADARA  3.2.3
ExternFunctionVariables.cpp
Go to the documentation of this file.
1 #ifndef _MADARA_EXTERNAL_FUNCTION_VARIABLES_CPP_
2 #define _MADARA_EXTERNAL_FUNCTION_VARIABLES_CPP_
3 
8 
9 void
11 {
12  if (this != &rhs || context_ != rhs.context_)
13  {
14  context_ = rhs.context_;
15  }
16 }
17 
20 {
21  return context_;
22 }
23 
26  const KnowledgeReferenceSettings & settings)
27 {
29 
30  if (context_)
31  {
32  VariableReference variable = context_->get_ref (key, settings);
33  result = context_->get (variable, settings);
34  }
35  else
36  {
38  "Variables::get. Context not set correctly\n");
39  }
40 
41  return result;
42 }
43 
46  const KnowledgeReferenceSettings & settings)
47 {
49 
50  if (context_)
51  {
52  result = context_->get (variable, settings);
53  }
54  else
55  {
57  "Variables::get. Context not set correctly\n");
58  }
59 
60  return result;
61 }
62 
63 bool
65  const KnowledgeReferenceSettings & settings) const
66 {
67  bool result (false);
68 
69  if (context_)
70  {
71  result = context_->exists (key, settings);
72  }
73  else
74  {
76  "Variables::exists. Context not set correctly\n");
77  }
78 
79  return result;
80 }
81 
82 bool
84  const KnowledgeReferenceSettings & settings) const
85 {
86  bool result (false);
87 
88  if (context_)
89  {
90  result = context_->exists (variable, settings);
91  }
92  else
93  {
95  "Variables::exists. Context not set correctly\n");
96  }
97 
98  return result;
99 }
100 
101 void
103  const KnowledgeUpdateSettings & /*settings*/)
104 {
105  if (context_)
106  {
108  }
109  else
110  {
112  "Variables::apply_modified. Context not set correctly\n");
113  }
114 }
115 
118  const std::string & key,
119  size_t index,
120  const KnowledgeReferenceSettings & settings)
121 {
122  if (context_)
123  {
124  VariableReference variable = context_->get_ref (key, settings);
125  return context_->retrieve_index (variable, index, settings);
126  }
127  else
128  {
130  "Variables::retrieve_index. Context not set correctly\n");
131 
133  }
134 }
135 
138  const VariableReference & variable,
139  size_t index,
140  const KnowledgeReferenceSettings & settings)
141 {
142  if (context_)
143  {
144  return context_->retrieve_index (variable, index, settings);
145  }
146  else
147  {
149  "Variables::retrieve_index. Context not set correctly\n");
150 
152  }
153 }
154 
157  const KnowledgeReferenceSettings & settings)
158 {
159  if (context_)
160  {
161  return context_->get_ref (key, settings);
162  }
163  else
164  {
166  "Variables::get_ref. Context not set correctly\n");
167 
169  }
170 }
171 
172 int
175  const KnowledgeUpdateSettings & settings)
176 {
177  if (context_)
178  {
179  VariableReference variable = context_->get_ref (key, settings);
180  return context_->set (variable, value, settings);
181  }
182  else
183  {
185  "Variables::set. Context not set correctly\n");
186 
187  return -4;
188  }
189 }
190 
191 int
194  const KnowledgeUpdateSettings & settings)
195 {
196  if (context_)
197  {
198  return context_->set (variable, value, settings);
199  }
200  else
201  {
203  "Variables::set. Context not set correctly\n");
204 
205  return -4;
206  }
207 }
208 
209 
210 int
213  uint32_t size,
214  const KnowledgeUpdateSettings & settings)
215 {
216  if (context_)
217  {
218  VariableReference variable = context_->get_ref (key, settings);
219  return context_->set (variable, value, size, settings);
220  }
221  else
222  {
224  "Variables::set. Context not set correctly\n");
225 
226  return -4;
227  }
228 }
229 
230 int
232  const VariableReference & variable,
234  uint32_t size,
235  const KnowledgeUpdateSettings & settings)
236 {
237  if (context_)
238  {
239  return context_->set (variable, value, size, settings);
240  }
241  else
242  {
244  "Variables::set. Context not set correctly\n");
245 
246  return -4;
247  }
248 }
249 
250 int
252  const std::string & key,
253  const std::vector <KnowledgeRecord::Integer> & value,
254  const KnowledgeUpdateSettings & settings)
255 {
256  if (context_)
257  {
258  VariableReference variable = context_->get_ref (key, settings);
259  return context_->set (variable, value, settings);
260  }
261  else
262  {
264  "Variables::set. Context not set correctly\n");
265 
266  return -4;
267  }
268 }
269 
270 int
272  const VariableReference & variable,
273  const std::vector <KnowledgeRecord::Integer> & value,
274  const KnowledgeUpdateSettings & settings)
275 {
276  if (context_)
277  {
278  return context_->set (variable, value, settings);
279  }
280  else
281  {
283  "Variables::set. Context not set correctly\n");
284 
285  return -4;
286  }
287 }
288 
289 int
291  const std::string & key,
293  const KnowledgeUpdateSettings & settings)
294 {
295  if (context_)
296  {
297  VariableReference variable = context_->get_ref (key, settings);
298  return context_->set (variable, value, settings);
299  }
300  else
301  {
303  "Variables::set. Context not set correctly\n");
304 
305  return -4;
306  }
307 }
308 
309 int
311  const VariableReference & variable,
313  const KnowledgeUpdateSettings & settings)
314 {
315  if (context_)
316  {
317  return context_->set (variable, value, settings);
318  }
319  else
320  {
322  "Variables::set. Context not set correctly\n");
323 
324  return -4;
325  }
326 }
327 
328 int
330  const std::string & key,
331  double value,
332  const KnowledgeUpdateSettings & settings)
333 {
334  if (context_)
335  {
336  VariableReference variable = context_->get_ref (key, settings);
337  return context_->set (variable, value, settings);
338  }
339  else
340  {
342  "Variables::set. Context not set correctly\n");
343 
344  return -4;
345  }
346 }
347 
348 int
350  const VariableReference & variable,
351  double value,
352  const KnowledgeUpdateSettings & settings)
353 {
354  if (context_)
355  {
356  return context_->set (variable, value, settings);
357  }
358  else
359  {
361  "Variables::set. Context not set correctly\n");
362 
363  return -4;
364  }
365 }
366 
367 int
369  const std::string & key,
370  const double * value,
371  uint32_t size,
372  const KnowledgeUpdateSettings & settings)
373 {
374  if (context_)
375  {
376  VariableReference variable = context_->get_ref (key, settings);
377  return context_->set (variable, value, size, settings);
378  }
379  else
380  {
382  "Variables::set. Context not set correctly\n");
383 
384  return -4;
385  }
386 }
387 
388 int
390  const VariableReference & variable,
391  const double * value,
392  uint32_t size,
393  const KnowledgeUpdateSettings & settings)
394 {
395  if (context_)
396  {
397  return context_->set (variable, value, size, settings);
398  }
399  else
400  {
402  "Variables::set. Context not set correctly\n");
403 
404  return -4;
405  }
406 }
407 
408 int
410  const std::string & key,
411  const std::vector <double> & value,
412  const KnowledgeUpdateSettings & settings)
413 {
414  if (context_)
415  {
416  VariableReference variable = context_->get_ref (key, settings);
417  return context_->set (variable, value, settings);
418  }
419  else
420  {
422  "Variables::set. Context not set correctly\n");
423 
424  return -4;
425  }
426 }
427 
428 int
430  const VariableReference & variable,
431  const std::vector <double> & value,
432  const KnowledgeUpdateSettings & settings)
433 {
434  if (context_)
435  {
436  return context_->set (variable, value, settings);
437  }
438  else
439  {
441  "Variables::set. Context not set correctly\n");
442 
443  return -4;
444  }
445 }
446 
447 int
449  const std::string & value,
450  const KnowledgeUpdateSettings & settings)
451 {
452  if (context_)
453  {
454  VariableReference variable = context_->get_ref (key, settings);
455  return context_->set (variable, value, settings);
456  }
457  else
458  {
460  "Variables::set. Context not set correctly\n");
461 
462  return -4;
463  }
464 }
465 
466 int
468  const VariableReference & variable,
469  const std::string & value,
470  const KnowledgeUpdateSettings & settings)
471 {
472  if (context_)
473  {
474  return context_->set (variable, value, settings);
475  }
476  else
477  {
479  "Variables::set. Context not set correctly\n");
480 
481  return -4;
482  }
483 }
484 
487  const KnowledgeUpdateSettings & settings)
488 {
489  if (context_)
490  {
491  VariableReference variable = context_->get_ref (key, settings);
492  return context_->inc (variable, settings);
493  }
494  else
495  {
497  "Variables::inc. Context not set correctly\n");
498 
500  }
501 }
502 
505  const VariableReference & variable,
506  const KnowledgeUpdateSettings & settings)
507 {
508  if (context_)
509  {
510  return context_->inc (variable, settings);
511  }
512  else
513  {
515  "Variables::inc. Context not set correctly\n");
516 
518  }
519 }
520 
523  const std::string & key,
524  const KnowledgeUpdateSettings & settings)
525 {
526  if (context_)
527  {
528  VariableReference variable = context_->get_ref (key, settings);
529  return context_->dec (variable, settings);
530  }
531  else
532  {
534  "Variables::dec. Context not set correctly\n");
535 
537  }
538 }
539 
542  const VariableReference & variable,
543  const KnowledgeUpdateSettings & settings)
544 {
545  if (context_)
546  {
547  return context_->dec (variable, settings);
548  }
549  else
550  {
552  "Variables::dec. Context not set correctly\n");
553 
555  }
556 }
557 
558 void
560  std::string & target,
561  const std::string & array_delimiter,
562  const std::string & record_delimiter,
563  const std::string & key_val_delimiter) const
564 {
565  if (context_)
566  {
567  context_->to_string (target,
568  array_delimiter, record_delimiter, key_val_delimiter);
569  }
570  else
571  {
573  "Variables::to_string. Context not set correctly\n");
574  }
575 }
576 
577 void
578 madara::knowledge::Variables::print (unsigned int level) const
579 {
580  if (context_)
581  {
582  context_->print (level);
583  }
584  else
585  {
587  "Variables::print. Context not set correctly\n");
588  }
589 }
590 
591 
592 void
594  unsigned int level) const
595 {
596  if (context_)
597  {
598  context_->print (statement, level);
599  }
600  else
601  {
603  "Variables::print. Context not set correctly\n");
604  }
605 }
606 
609  const std::string & statement) const
610 {
611  if (context_)
612  {
613  return context_->expand_statement (statement);
614  }
615  else
616  {
618  "Variables::expand_statement. Context not set correctly\n");
619 
620  return "";
621  }
622 }
623 
624 
625 #ifndef _MADARA_NO_KARL_
626 
627 // Defines a function
629  const std::string & name,
631 {
632  if (context_)
633  {
634  return context_->define_function (name, func);
635  }
636  else
637  {
639  "Variables::define_function. Context not set correctly\n");
640  }
641 }
642 
643 #ifdef _MADARA_JAVA_
644 // Defines a function
646  const std::string & name, jobject func)
647 {
648  if (context_)
649  {
650  return context_->define_function (name, func);
651  }
652  else
653  {
655  "Variables::define_function. Context not set correctly\n");
656  }
657 }
658 #endif
659 
660 #ifdef _MADARA_PYTHON_CALLBACKS_
661 
662 // Defines a function
664  const std::string & name, boost::python::object callable)
665 {
666  if (context_)
667  {
668  return context_->define_function (name, callable);
669  }
670  else
671  {
673  "Variables::define_function. Context not set correctly\n");
674  }
675 }
676 
677 #endif
678 
684 void
686  const std::string & expression)
687 {
688  if (context_)
689  {
690  return context_->define_function (name, expression);
691  }
692  else
693  {
695  "Variables::define_function. Context not set correctly\n");
696  }
697 }
698 
704 void
706  const CompiledExpression & expression)
707 {
708  if (context_)
709  {
710  return context_->define_function (name, expression);
711  }
712  else
713  {
715  "Variables::define_function. Context not set correctly\n");
716  }
717 }
718 
719 
722 {
723  if (context_)
724  {
725  return context_->compile (expression);
726  }
727  else
728  {
730  "Variables::compile. Context not set correctly\n");
731 
732  return CompiledExpression ();
733  }
734 }
735 
738  const std::string & expression,
739  const KnowledgeUpdateSettings & settings)
740 {
741  if (context_)
742  {
743  CompiledExpression compiled = context_->compile (expression);
744  return compiled.expression.evaluate (settings);
745  }
746  else
747  {
749  "Variables::evaluate. Context not set correctly\n");
750 
752  }
753 }
754 
757  CompiledExpression & expression,
758  const KnowledgeUpdateSettings & settings)
759 {
760  if (context_)
761  {
762  return expression.expression.evaluate (settings);
763  }
764  else
765  {
767  "Variables::evaluate. Context not set correctly\n");
768 
770  }
771 }
772 
773 #endif // _MADARA_NO_KARL_
774 
775 size_t
777  const std::string & subject,
778  unsigned int start,
779  unsigned int end,
780  std::vector <KnowledgeRecord> & target)
781 {
782  size_t result (0);
783 
784  if (context_)
785  {
786  result = context_->to_vector (subject, start, end, target);
787  }
788  else
789  {
791  "Variables::to_vector. Context not set correctly\n");
792  }
793 
794  return result;
795 }
796 
797 void
799  const std::string & prefix,
800  const std::string & suffix,
801  VariableReferences & matches)
802 {
803  if (context_)
804  {
805  context_->get_matches (prefix, suffix, matches);
806  }
807  else
808  {
810  "Variables::get_matches: Context not set correctly\n");
811  }
812 }
813 
814 size_t
816  const std::string & expression,
817  std::map <std::string, knowledge::KnowledgeRecord> & target)
818 {
819  size_t result (0);
820 
821  if (context_)
822  {
823  result = context_->to_map (expression, target);
824  }
825  else
826  {
828  "Variables::to_map. Context not set correctly\n");
829  }
830 
831  return result;
832 }
833 
834 
835 
836 int64_t
838  const std::string & filename)
839 {
840  int64_t result (0);
841 
842  if (context_)
843  {
844  result = context_->save_context (filename);
845  }
846  else
847  {
849  "Variables::save_context. Context not set correctly\n");
850  }
851 
852  return result;
853 }
854 
855 int64_t
857 const std::string & filename) const
858 {
859  int64_t result (0);
860 
861  if (context_)
862  {
863  result = context_->save_as_karl (filename);
864  }
865  else
866  {
868  "Variables::save_as_karl. Context not set correctly\n");
869  }
870 
871  return result;
872 }
873 
874 int64_t
876  const std::string & filename,
877  bool reset_modifieds)
878 {
879  int64_t result (0);
880 
881  if (context_)
882  {
883  result = context_->save_context (filename);
884 
885  if (reset_modifieds)
887  }
888  else
889  {
891  "Variables::save_checkpoint. Context not set correctly\n");
892  }
893 
894  return result;
895 }
896 
897 int64_t
899  const std::string & filename,
900  const KnowledgeUpdateSettings & settings)
901 {
902  int64_t result (0);
903 
904  if (context_)
905  {
906  std::string id;
907  result = context_->load_context (filename, id, settings);
908  }
909  else
910  {
912  "Variables::load_context. Context not set correctly\n");
913  }
914 
915  return result;
916 }
917 
918 ssize_t
920  const std::string & knowledge_key,
921  const std::string & filename)
922 {
923  if (context_)
924  {
925  return context_->get_record (knowledge_key)->to_file (filename);;
926  }
927  else
928  {
930  "Variables::write_file. Context not set correctly\n");
931 
932  return 0;
933  }
934 }
935 
936 
937 #endif
This class encapsulates an entry in a KnowledgeBase.
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes 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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
int64_t save_context(const std::string &filename)
Saves the context to 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...
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.
madara::knowledge::KnowledgeRecord KnowledgeRecord
int64_t load_context(const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
madara::knowledge::KnowledgeRecord evaluate(const std::string &expression, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Evaluates an expression (USE ONLY FOR PROTOTYPING; DO NOT USE IN PRODUCTION SYSTEMS).
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
ssize_t to_file(const std::string &filename) const
writes the value to a file
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.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Decrements the value of the variable.
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
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...
Optimized reference to a variable within the knowledge base.
Compiled, optimized KaRL logic.
std::vector< KnowledgeRecord > FunctionArguments
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &), const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Defines an external function.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
void operator=(Variables &rhs)
Assignment operator.
void apply_modified(void)
Changes all global variables to modified at current clock.
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.
madara::expression::ExpressionTree expression
the expression tree
static struct madara::knowledge::tags::string_t string
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the 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...
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
Checks if a knowledge location exists in the context.
ThreadSafeContext * context_
Variables context that is directly used by the KaRL engine.
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...
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
std::vector< VariableReference > VariableReferences
a vector of variable references
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the context to a specified location.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
int set(const std::string &key, madara::knowledge::KnowledgeRecord::Integer value, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Sets the value of a variable.
void apply_modified(const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Applies modified to all global variables.
madara::knowledge::KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves a value at a specified index within a knowledge array.
KnowledgeRecord * get_record(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge record from the key.
knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Atomically increments the value of the variable.
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.
Settings for applying knowledge updates.
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
Provides an interface for external functions into the MADARA KaRL variable settings.
void print(unsigned int level=0) const
Prints all variables and values in the context.
madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Evaluates the expression tree.
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.
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
void reset_modified(void)
Reset all variables to be unmodified.