MADARA  3.4.1
Variables.cpp
Go to the documentation of this file.
1 #ifndef _MADARA_EXTERNAL_FUNCTION_VARIABLES_CPP_
2 #define _MADARA_EXTERNAL_FUNCTION_VARIABLES_CPP_
3 
8 
10 {
11  if (this != &rhs || context_ != rhs.context_)
12  {
13  context_ = rhs.context_;
14  }
15 }
16 
18  void)
19 {
20  return context_;
21 }
22 
24  const std::string& key, const KnowledgeReferenceSettings& settings)
25 {
27 
28  if (context_)
29  {
30  VariableReference variable = context_->get_ref(key, settings);
31  result = context_->get(variable, settings);
32  }
33  else
34  {
36  "Variables::get. Context not set correctly\n");
37  }
38 
39  return result;
40 }
41 
43  const VariableReference& variable,
44  const KnowledgeReferenceSettings& settings)
45 {
47 
48  if (context_)
49  {
50  result = context_->get(variable, settings);
51  }
52  else
53  {
55  "Variables::get. Context not set correctly\n");
56  }
57 
58  return result;
59 }
60 
62  const std::string& key, const KnowledgeReferenceSettings& settings) const
63 {
64  bool result(false);
65 
66  if (context_)
67  {
68  result = context_->exists(key, settings);
69  }
70  else
71  {
73  "Variables::exists. Context not set correctly\n");
74  }
75 
76  return result;
77 }
78 
80  const KnowledgeReferenceSettings& settings) const
81 {
82  bool result(false);
83 
84  if (context_)
85  {
86  result = context_->exists(variable, settings);
87  }
88  else
89  {
91  "Variables::exists. Context not set correctly\n");
92  }
93 
94  return result;
95 }
96 
98  const KnowledgeUpdateSettings& /*settings*/)
99 {
100  if (context_)
101  {
103  }
104  else
105  {
107  "Variables::apply_modified. Context not set correctly\n");
108  }
109 }
110 
112  const std::string& key, size_t index,
113  const KnowledgeReferenceSettings& settings)
114 {
115  if (context_)
116  {
117  VariableReference variable = context_->get_ref(key, settings);
118  return context_->retrieve_index(variable, index, settings);
119  }
120  else
121  {
123  "Variables::retrieve_index. Context not set correctly\n");
124 
126  }
127 }
128 
130  const VariableReference& variable, size_t index,
131  const KnowledgeReferenceSettings& settings)
132 {
133  if (context_)
134  {
135  return context_->retrieve_index(variable, index, settings);
136  }
137  else
138  {
140  "Variables::retrieve_index. Context not set correctly\n");
141 
143  }
144 }
145 
147  const std::string& key, const KnowledgeReferenceSettings& settings)
148 {
149  if (context_)
150  {
151  return context_->get_ref(key, settings);
152  }
153  else
154  {
156  "Variables::get_ref. Context not set correctly\n");
157 
159  }
160 }
161 
164  const KnowledgeUpdateSettings& settings)
165 {
166  if (context_)
167  {
168  VariableReference variable = context_->get_ref(key, settings);
169  return context_->set(variable, value, settings);
170  }
171  else
172  {
174  "Variables::set. Context not set correctly\n");
175 
176  return -4;
177  }
178 }
179 
182  const KnowledgeUpdateSettings& settings)
183 {
184  if (context_)
185  {
186  return context_->set(variable, value, settings);
187  }
188  else
189  {
191  "Variables::set. Context not set correctly\n");
192 
193  return -4;
194  }
195 }
196 
198  const knowledge::KnowledgeRecord::Integer* value, uint32_t size,
199  const KnowledgeUpdateSettings& settings)
200 {
201  if (context_)
202  {
203  VariableReference variable = context_->get_ref(key, settings);
204  return context_->set(variable, value, size, settings);
205  }
206  else
207  {
209  "Variables::set. Context not set correctly\n");
210 
211  return -4;
212  }
213 }
214 
216  const knowledge::KnowledgeRecord::Integer* value, uint32_t size,
217  const KnowledgeUpdateSettings& settings)
218 {
219  if (context_)
220  {
221  return context_->set(variable, value, size, settings);
222  }
223  else
224  {
226  "Variables::set. Context not set correctly\n");
227 
228  return -4;
229  }
230 }
231 
233  const std::vector<KnowledgeRecord::Integer>& value,
234  const KnowledgeUpdateSettings& settings)
235 {
236  if (context_)
237  {
238  VariableReference variable = context_->get_ref(key, settings);
239  return context_->set(variable, value, settings);
240  }
241  else
242  {
244  "Variables::set. Context not set correctly\n");
245 
246  return -4;
247  }
248 }
249 
251  const std::vector<KnowledgeRecord::Integer>& value,
252  const KnowledgeUpdateSettings& settings)
253 {
254  if (context_)
255  {
256  return context_->set(variable, value, settings);
257  }
258  else
259  {
261  "Variables::set. Context not set correctly\n");
262 
263  return -4;
264  }
265 }
266 
269  const KnowledgeUpdateSettings& settings)
270 {
271  if (context_)
272  {
273  VariableReference variable = context_->get_ref(key, settings);
274  return context_->set(variable, value, settings);
275  }
276  else
277  {
279  "Variables::set. Context not set correctly\n");
280 
281  return -4;
282  }
283 }
284 
287  const KnowledgeUpdateSettings& settings)
288 {
289  if (context_)
290  {
291  return context_->set(variable, value, settings);
292  }
293  else
294  {
296  "Variables::set. Context not set correctly\n");
297 
298  return -4;
299  }
300 }
301 
302 int madara::knowledge::Variables::set(const std::string& key, double value,
303  const KnowledgeUpdateSettings& settings)
304 {
305  if (context_)
306  {
307  VariableReference variable = context_->get_ref(key, settings);
308  return context_->set(variable, value, settings);
309  }
310  else
311  {
313  "Variables::set. Context not set correctly\n");
314 
315  return -4;
316  }
317 }
318 
320  double value, const KnowledgeUpdateSettings& settings)
321 {
322  if (context_)
323  {
324  return context_->set(variable, value, settings);
325  }
326  else
327  {
329  "Variables::set. Context not set correctly\n");
330 
331  return -4;
332  }
333 }
334 
336  const double* value, uint32_t size, const KnowledgeUpdateSettings& settings)
337 {
338  if (context_)
339  {
340  VariableReference variable = context_->get_ref(key, settings);
341  return context_->set(variable, value, size, settings);
342  }
343  else
344  {
346  "Variables::set. Context not set correctly\n");
347 
348  return -4;
349  }
350 }
351 
353  const double* value, uint32_t size, const KnowledgeUpdateSettings& settings)
354 {
355  if (context_)
356  {
357  return context_->set(variable, value, size, settings);
358  }
359  else
360  {
362  "Variables::set. Context not set correctly\n");
363 
364  return -4;
365  }
366 }
367 
369  const std::vector<double>& value, const KnowledgeUpdateSettings& settings)
370 {
371  if (context_)
372  {
373  VariableReference variable = context_->get_ref(key, settings);
374  return context_->set(variable, value, settings);
375  }
376  else
377  {
379  "Variables::set. Context not set correctly\n");
380 
381  return -4;
382  }
383 }
384 
386  const std::vector<double>& value, const KnowledgeUpdateSettings& settings)
387 {
388  if (context_)
389  {
390  return context_->set(variable, value, settings);
391  }
392  else
393  {
395  "Variables::set. Context not set correctly\n");
396 
397  return -4;
398  }
399 }
400 
402  const std::string& value, const KnowledgeUpdateSettings& settings)
403 {
404  if (context_)
405  {
406  VariableReference variable = context_->get_ref(key, settings);
407  return context_->set(variable, value, settings);
408  }
409  else
410  {
412  "Variables::set. Context not set correctly\n");
413 
414  return -4;
415  }
416 }
417 
419  const std::string& value, const KnowledgeUpdateSettings& settings)
420 {
421  if (context_)
422  {
423  return context_->set(variable, value, settings);
424  }
425  else
426  {
428  "Variables::set. Context not set correctly\n");
429 
430  return -4;
431  }
432 }
433 
435  const std::string& key, const KnowledgeUpdateSettings& settings)
436 {
437  if (context_)
438  {
439  VariableReference variable = context_->get_ref(key, settings);
440  return context_->inc(variable, settings);
441  }
442  else
443  {
445  "Variables::inc. Context not set correctly\n");
446 
448  }
449 }
450 
452  const VariableReference& variable, const KnowledgeUpdateSettings& settings)
453 {
454  if (context_)
455  {
456  return context_->inc(variable, settings);
457  }
458  else
459  {
461  "Variables::inc. Context not set correctly\n");
462 
464  }
465 }
466 
468  const std::string& key, const KnowledgeUpdateSettings& settings)
469 {
470  if (context_)
471  {
472  VariableReference variable = context_->get_ref(key, settings);
473  return context_->dec(variable, settings);
474  }
475  else
476  {
478  "Variables::dec. Context not set correctly\n");
479 
481  }
482 }
483 
485  const VariableReference& variable, const KnowledgeUpdateSettings& settings)
486 {
487  if (context_)
488  {
489  return context_->dec(variable, settings);
490  }
491  else
492  {
494  "Variables::dec. Context not set correctly\n");
495 
497  }
498 }
499 
501  const std::string& array_delimiter, const std::string& record_delimiter,
502  const std::string& key_val_delimiter) const
503 {
504  if (context_)
505  {
507  target, array_delimiter, record_delimiter, key_val_delimiter);
508  }
509  else
510  {
512  "Variables::to_string. Context not set correctly\n");
513  }
514 }
515 
516 void madara::knowledge::Variables::print(unsigned int level) const
517 {
518  if (context_)
519  {
520  context_->print(level);
521  }
522  else
523  {
525  "Variables::print. Context not set correctly\n");
526  }
527 }
528 
530  const std::string& statement, unsigned int level) const
531 {
532  if (context_)
533  {
534  context_->print(statement, level);
535  }
536  else
537  {
539  "Variables::print. Context not set correctly\n");
540  }
541 }
542 
544  const std::string& statement) const
545 {
546  if (context_)
547  {
548  return context_->expand_statement(statement);
549  }
550  else
551  {
553  "Variables::expand_statement. Context not set correctly\n");
554 
555  return "";
556  }
557 }
558 
559 #ifndef _MADARA_NO_KARL_
560 
561 // Defines a function
564 {
565  if (context_)
566  {
567  return context_->define_function(name, func);
568  }
569  else
570  {
572  "Variables::define_function. Context not set correctly\n");
573  }
574 }
575 
576 #ifdef _MADARA_JAVA_
577 // Defines a function
579  const std::string& name, jobject func)
580 {
581  if (context_)
582  {
583  return context_->define_function(name, func);
584  }
585  else
586  {
588  "Variables::define_function. Context not set correctly\n");
589  }
590 }
591 #endif
592 
593 #ifdef _MADARA_PYTHON_CALLBACKS_
594 
595 // Defines a function
597  const std::string& name, boost::python::object callable)
598 {
599  if (context_)
600  {
601  return context_->define_function(name, callable);
602  }
603  else
604  {
606  "Variables::define_function. Context not set correctly\n");
607  }
608 }
609 
610 #endif
611 
618  const std::string& name, const std::string& expression)
619 {
620  if (context_)
621  {
622  return context_->define_function(name, expression);
623  }
624  else
625  {
627  "Variables::define_function. Context not set correctly\n");
628  }
629 }
630 
637  const std::string& name, const CompiledExpression& expression)
638 {
639  if (context_)
640  {
641  return context_->define_function(name, expression);
642  }
643  else
644  {
646  "Variables::define_function. Context not set correctly\n");
647  }
648 }
649 
651  const std::string& expression)
652 {
653  if (context_)
654  {
655  return context_->compile(expression);
656  }
657  else
658  {
660  "Variables::compile. Context not set correctly\n");
661 
662  return CompiledExpression();
663  }
664 }
665 
667  const std::string& expression, const KnowledgeUpdateSettings& settings)
668 {
669  if (context_)
670  {
671  CompiledExpression compiled = context_->compile(expression);
672  return compiled.expression.evaluate(settings);
673  }
674  else
675  {
677  "Variables::evaluate. Context not set correctly\n");
678 
680  }
681 }
682 
684  CompiledExpression& expression, const KnowledgeUpdateSettings& settings)
685 {
686  if (context_)
687  {
688  return expression.expression.evaluate(settings);
689  }
690  else
691  {
693  "Variables::evaluate. Context not set correctly\n");
694 
696  }
697 }
698 
699 #endif // _MADARA_NO_KARL_
700 
702  unsigned int start, unsigned int end, std::vector<KnowledgeRecord>& target)
703 {
704  size_t result(0);
705 
706  if (context_)
707  {
708  result = context_->to_vector(subject, start, end, target);
709  }
710  else
711  {
713  "Variables::to_vector. Context not set correctly\n");
714  }
715 
716  return result;
717 }
718 
720  const std::string& suffix, VariableReferences& matches)
721 {
722  if (context_)
723  {
724  context_->get_matches(prefix, suffix, matches);
725  }
726  else
727  {
729  "Variables::get_matches: Context not set correctly\n");
730  }
731 }
732 
734  std::map<std::string, knowledge::KnowledgeRecord>& target)
735 {
736  size_t result(0);
737 
738  if (context_)
739  {
740  result = context_->to_map(expression, target);
741  }
742  else
743  {
745  "Variables::to_map. Context not set correctly\n");
746  }
747 
748  return result;
749 }
750 
752 {
753  int64_t result(0);
754 
755  if (context_)
756  {
757  result = context_->save_context(filename);
758  }
759  else
760  {
762  "Variables::save_context. Context not set correctly\n");
763  }
764 
765  return result;
766 }
767 
769  const std::string& filename) const
770 {
771  int64_t result(0);
772 
773  if (context_)
774  {
775  result = context_->save_as_karl(filename);
776  }
777  else
778  {
780  "Variables::save_as_karl. Context not set correctly\n");
781  }
782 
783  return result;
784 }
785 
787  const std::string& filename, bool reset_modifieds)
788 {
789  int64_t result(0);
790 
791  if (context_)
792  {
793  result = context_->save_context(filename);
794 
795  if (reset_modifieds)
797  }
798  else
799  {
801  "Variables::save_checkpoint. Context not set correctly\n");
802  }
803 
804  return result;
805 }
806 
808  const std::string& filename, const KnowledgeUpdateSettings& settings)
809 {
810  int64_t result(0);
811 
812  if (context_)
813  {
814  std::string id;
815  result = context_->load_context(filename, id, settings);
816  }
817  else
818  {
820  "Variables::load_context. Context not set correctly\n");
821  }
822 
823  return result;
824 }
825 
827  const std::string& knowledge_key, const std::string& filename)
828 {
829  if (context_)
830  {
831  return context_->get_record(knowledge_key)->to_file(filename);
832  ;
833  }
834  else
835  {
837  "Variables::write_file. Context not set correctly\n");
838 
839  return 0;
840  }
841 }
842 
843 #endif
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
const ThreadSafeContext * context_
madara::knowledge::KnowledgeRecord KnowledgeRecord
madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Evaluates the expression tree.
Compiled, optimized KaRL logic.
madara::expression::ExpressionTree expression
the expression tree
This class encapsulates an entry in a KnowledgeBase.
ssize_t to_file(const std::string &filename) const
writes the value to a file
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.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
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 get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the current value of a variable.
void apply_modified(void)
Changes all global variables to modified at current clock.
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &), const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Defines an external function.
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int64_t save_context(const std::string &filename, const std::string &id="") const
Saves the context to a file.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
KnowledgeRecord * get_record(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge record from the key.
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.
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
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 print(unsigned int level) const
Atomically prints all variables and values in the context.
void reset_modified(void)
Reset all variables to be unmodified.
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
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).
Definition: Variables.cpp:666
knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Decrements the value of the variable.
Definition: Variables.cpp:467
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.
Definition: Variables.cpp:719
void operator=(Variables &rhs)
Assignment operator.
Definition: Variables.cpp:9
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...
Definition: Variables.cpp:500
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.
Definition: Variables.cpp:162
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...
Definition: Variables.cpp:701
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.
Definition: Variables.cpp:733
void define_function(const std::string &name, knowledge::KnowledgeRecord(*func)(FunctionArguments &, Variables &))
Defines a function.
Definition: Variables.cpp:562
void apply_modified(const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Applies modified to all global variables.
Definition: Variables.cpp:97
int64_t save_checkpoint(const std::string &filename, bool reset_modifieds=true)
Saves a checkpoint of a list of changes to a file.
Definition: Variables.cpp:786
ThreadSafeContext * context_
Variables context that is directly used by the KaRL engine.
Definition: Variables.h:633
void print(unsigned int level=0) const
Prints all variables and values in the context.
Definition: Variables.cpp:516
int64_t save_context(const std::string &filename)
Saves the context to a file.
Definition: Variables.cpp:751
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
Definition: Variables.cpp:543
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false)) const
Checks if a knowledge location exists in the context.
Definition: Variables.cpp:61
int64_t save_as_karl(const std::string &filename) const
Saves the context to a file as karl assignments, rather than binary.
Definition: Variables.cpp:768
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
Definition: Variables.cpp:146
int64_t load_context(const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(true, true, true, false))
Loads the context from a file.
Definition: Variables.cpp:807
CompiledExpression compile(const std::string &expression)
Compiles a KaRL expression into an expression tree.
Definition: Variables.cpp:650
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.
Definition: Variables.cpp:111
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
Definition: Variables.cpp:17
knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings(false, true, false, false))
Atomically increments the value of the variable.
Definition: Variables.cpp:434
knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
Definition: Variables.cpp:23
ssize_t write_file(const std::string &knowledge_key, const std::string &filename)
Write a file from the context to a specified location.
Definition: Variables.cpp:826
constexpr string_t string
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
std::vector< KnowledgeRecord > FunctionArguments
std::vector< VariableReference > VariableReferences
a vector of variable references
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger