MADARA  3.2.3
SystemCallPrintSystemCalls.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 
9 
10 // a map of all the system calls to their help information.
13 
16  const ComponentNodes & nodes)
17  : SystemCallNode (context, nodes)
18 {
19 
20 }
21 
22 // Dtor
24 {
25 }
26 
29 {
31 }
32 
38 {
39  // user can always change a function, and we have no control over
40  // what it does. Consequently, a function node cannot be pruned out
41  // under any situation
42  can_change = true;
43 
45 
46  if (nodes_.size () == 1)
47  {
48  bool arg_can_change = false;
49  result = nodes_[0]->prune (arg_can_change);
50 
51  if (!arg_can_change && dynamic_cast <LeafNode *> (nodes_[0]) == 0)
52  {
53  delete nodes_[0];
54  nodes_[0] = new LeafNode (*(this->logger_), result);
55  }
56  }
57  else if (nodes_.size () != 0)
58  {
60  "madara::expression::SystemCallPrintSystemCalls: "
61  "KARL COMPILE ERROR:"
62  "System call print_system_calls takes either 0"
63  " or 1 arguments\n");
64 
65  throw KarlException ("madara::expression::SystemCallPrintSystemCalls: "
66  "KARL COMPILE ERROR: "
67  "System call print_system_calls takes either 0"
68  " or 1 arguments\n");
69  }
70 
71  // if calls hasn't been initialized yet, fill the list of system calls
72  if (calls_.size () == 0)
73  {
74  calls_["#clear_variable"] =
75  "\n#clear_var (var) or #clear_variable (var):\n"
76  " Clears the variable var in the knowledge base. This is\n"
77  " the preferred way to delete variables. It masks the variable\n"
78  " from printouts and save_modifieds but leaves a phantom record\n"
79  " in the knowledge base to safely reference in VariableReference,\n"
80  " CompiledExpression, etc.\n";
81 
82  calls_["#cos"] =
83  "\n#cos (var):\n"
84  " Returns the cosine of a term (radians)\n";
85 
86  calls_["#delete_variable"] =
87  "\n#delete_var (var) or #delete_variable (var):\n"
88  " Deletes a variable named var from the knowledge base. \n"
89  " This is an extremely unsafe operation that must only\n"
90  " be used if the variable is never used in compiled\n"
91  " expressions or variable references (including container\n"
92  " classes such as Integer, Double, Vector, etc.)\n";
93 
94  calls_["#eval"] =
95  "\n#eval (expression) or #evaluate (expression):\n"
96  " Evaluates the KaRL expression and returns a result. Works similarly\n"
97  " to the KnowledgeBase::evaluate function except this function\n"
98  " inherits the KnowledgeUpdateSettings from the eval call.\n";
99 
100  calls_["#evaluate"] =
101  "\n#eval (expression) or #evaluate (expression):\n"
102  " Evaluates the KaRL expression and returns a result. Works similarly\n"
103  " to the KnowledgeBase::evaluate function except this function\n"
104  " inherits the KnowledgeUpdateSettings from the eval call.\n";
105 
106  calls_["#expand"] =
107  "\n#expand (statement) or #expand_statement (statement):\n"
108  " Expands a statement such as 'var{.i}' into 'var0', assuming .i=0\n";
109 
110  calls_["#expand_env"] =
111  "\n#expand_env (statement) or #expand_envs (statement):\n"
112  " Expands a statement such as 'env_var=$(VAR)' into\n"
113  " env_var=VAR CONTENTS, assuming VAR has been set in the OS.\n";
114 
115  calls_["#expand_envs"] =
116  "\n#expand_env (statement) or #expand_envs (statement):\n"
117  " Expands a statement such as 'env_var=$(VAR)' into\n"
118  " env_var=VAR CONTENTS, assuming VAR has been set in the OS.\n";
119 
120  calls_["#expand_statement"] =
121  "\n#expand (statement) or #expand_statement (statement):\n"
122  " Expands a statement such as 'var{.i}' into 'var0', assuming .i=0\n";
123 
124  calls_["#fragment"] =
125  "\n#fragment (arg, first, last):\n"
126  " Returns a fragment of the arg, startin at first byte and extending\n"
127  " to last byte.\n";
128 
129  calls_["#get_clock"] =
130  "\n#get_clock () or #get_clock (variable):\n"
131  " Returns either the system clock or the variable clock. Note that\n"
132  " this is a Lamport clock value and not a wall time value.\n";
133 
134  calls_["#get_time"] =
135  "\n#get_time () or #get_time_ns() or #get_time_nano():\n"
136  " Returns wall clock time on the local machine in nanoseconds.\n";
137 
138  calls_["#get_time_seconds"] =
139  "\n#get_time_seconds () or #get_time_s ():\n"
140  " Returns wall clock time on the local machine in seconds.\n";
141 
142  calls_["#isinf"] =
143  "\n#isinf (value):\n"
144  " Returns std::isinf of a value to check for infinity/-infinity.\n";
145 
146  calls_["#log_level"] =
147  "\n#log_level () or #log_level (value):\n"
148  " Returns the logging level or sets the logging level. The logging\n"
149  " level determines the granularity of MADARA log messages. The value\n"
150  " should be an integer from 0-6.\n"
151  " 0: Emergency or always messages. Default setting.\n"
152  " 1. Errors\n"
153  " 2. Warnings\n"
154  " 3. Major events\n"
155  " 4. Minor events\n"
156  " 5. Trace events\n"
157  " 6. Detailed logging\n";
158 
159  calls_["#pow"] =
160  "\n#pow (base, power):\n"
161  " Returns the base taken to a power (exponent)\n";
162 
163  calls_["#print"] =
164  "\n#print (statement):\n"
165  " Prints a statement that can contain MADARA KaRL variables.\n"
166  " Variables are expanding to include their values where applicable.\n"
167  " For instance, 'var{.id}' would result in printing 'var0' if .id\n"
168  " was set to 0. The same statement would print 'variable' if .id\n"
169  " was set to 'iable'.\n";
170 
171  calls_["#print_system_calls"] =
172  "\n#print_system_calls () or #print_system_call (system_call):\n"
173  " Prints help for all system calls or a specific system call.\n";
174 
175  calls_["#rand_double"] =
176  "\n#rand_double (floor = 0.0, ceiling = 1.0, update_srand = true):\n"
177  " Generates a random double from a uniform distribution.\n";
178 
179  calls_["#rand_int"] =
180  "\n#rand_int (floor = 0, ceiling = RAND_MAX, update_srand = true):\n"
181  " Generates a random integer from a uniform distribution.\n";
182 
183  calls_["#read_file"] =
184  "\n#read_file (filename) or #read_file (filename, type):\n"
185  " Reads a file and returns its contents. Default is to determine\n"
186  " file type by filname extension. Explicit type can be a string\n"
187  " or an integer of the following values:\n"
188  " 'xml' = 16\n"
189  " 'text' = 32\n"
190  " 'jpeg' = 256\n";
191 
192  calls_["#set_clock"] =
193  "\n#set_clock (value) or #set_clock (variable, value):\n"
194  " Sets the system clock or a variable clock. The value should be\n"
195  " a non-zero integer (Lamport clock) and is used to determine which\n"
196  " variable values are rejected at the transport layer. Returns the\n"
197  " new clock value.\n";
198 
199  calls_["#set_fixed"] =
200  "\n#set_fixed () or #fixed ():\n"
201  " Sets the output format for doubles to fixed format (std::fixed).\n";
202 
203  calls_["#set_precision"] =
204  "\n#set_precision (value) or #precision (value):\n"
205  " Sets the precision that is used to convert doubles to strings.\n"
206  " This is a fixed precision (e.g., 5 for pi would be 3.14159).\n"
207  " Negative value resets to default precision.\n";
208 
209  calls_["#set_scientific"] =
210  "\n#set_scientific () or #scientific ():\n"
211  " Sets the output format for doubles to scientific format\n"
212  " (std::scientific).\n";
213 
214  calls_["#sin"] =
215  "\n#sin (var):\n"
216  " Returns the sine of a term (radians)\n";
217 
218  calls_["#size"] =
219  "\n#size (value):\n"
220  " Returns the size of the variable or value.\n";
221 
222  calls_["#sleep"] =
223  "\n#sleep (double time_to_sleep in s):\n"
224  " Sleeps for at least the specified time and returns time\n"
225  " spent sleeping in seconds.\n";
226 
227  calls_["#sqrt"] =
228  "\n#sqrt (value):\n"
229  " Returns the square root of a value\n";
230 
231  calls_["#tan"] =
232  "\n#tan (value):\n"
233  " Returns the tangent of a term (radians)\n";
234 
235  calls_["#to_buffer"] =
236  "\n#to_buffer (value) or #buffer (value):\n"
237  " Converts the value to an unsigned char array.\n";
238 
239  calls_["#to_double"] =
240  "\n#to_double (value) or #double (value):\n"
241  " Converts the value to a double.\n";
242 
243  calls_["#to_doubles"] =
244  "\n#to_doubles (value) or #doubles (value):\n"
245  " Converts the value to an array of doubles.\n";
246 
247  calls_["#to_host_dirs"] =
248  "\n#to_host_dirs (directory):\n"
249  " Returns the host appropriate directory structure, e.g.,.\n"
250  " files/file.txt to files\file.txt on Windows.\n";
251 
252  calls_["#to_integer"] =
253  "\n#to_integer (value) or #integer (value):\n"
254  " Converts the value to an integer.\n";
255 
256  calls_["#to_integers"] =
257  "\n#to_integers (value) or #integers (value):\n"
258  " Converts the value to an array of integers.\n";
259 
260  calls_["#to_string"] =
261  "\n#to_string (value, delimiter) or #string (value, delimiter):\n"
262  " Converts the value to an ASCII string. Delimiter is an optional\n"
263  " string sequence that separates elements of an integer or double"
264  " array.\n";
265 
266  calls_["#type"] =
267  "\n#type (value):\n"
268  " Returns the integer type of the variable or value.\n"
269  " Supported types are:\n"
270  " UNINITIALIZED = 0\n"
271  " INTEGER = 1\n"
272  " STRING = 2\n"
273  " DOUBLE = 4\n"
274  " UNKNOWN_FILE_TYPE = 8\n"
275  " XML = 16\n"
276  " TEXT_FILE = 32\n"
277  " INTEGER_ARRAY = 64\n"
278  " DOUBLE_ARRAY = 128\n"
279  " IMAGE_JPEG = 256\n";
280 
281  calls_["#write_file"] =
282  "\n#write_file (filename, value):\n"
283  " Writes the value to a file. Supports all MADARA types.\n"
284  " @see #type.\n";
285 
286  }
287 
288  return result;
289 }
290 
296 {
297  knowledge::KnowledgeRecord return_value;
298 
299  if (nodes_.size () == 1)
300  {
302  "madara::expression::SystemCallPrintSystemCalls: "
303  "System call print_system_calls is printing help\n");
304 
305  context_.print (
306  calls_[nodes_[0]->evaluate (settings).to_string ()], 0);
307 
309  }
310  else if (nodes_.size () == 0)
311  {
313  "madara::expression::SystemCallPrintSystemCalls: "
314  "System call print_system_calls is printing help\n");
315 
316  for (SystemCallsHelp::const_iterator i = calls_.begin ();
317  i != calls_.end (); ++i)
318  {
319  if (i->first != "#delete_var" && i->first != "#eval" &&
320  i->first != "#expand" && i->first != "#expand_env")
321  context_.print (i->second, 0);
322  }
323  }
324  else
325  {
327  "madara::expression::SystemCallPrintSystemCalls: "
328  "KARL RUNTIME ERROR:"
329  "System call print_system_calls takes either 0"
330  " or 1 arguments\n");
331 
332  throw KarlException ("madara::expression::SystemCallPrintSystemCalls: "
333  "KARL RUNTIME ERROR: "
334  "System call print_system_calls takes either 0"
335  " or 1 arguments\n");
336  }
337 
338  return return_value;
339 }
340 
341 // accept a visitor
342 void
344  madara::expression::Visitor &visitor) const
345 {
346  visitor.visit (*this);
347 }
348 
349 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
std::map< std::string, std::string > SystemCallsHelp
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:97
This class stores variables and their values for use by any entity needing state information in a thr...
void print(unsigned int level) const
Atomically prints all variables and values in the context.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
madara::knowledge::ThreadSafeContext & context_
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
Interface for a MADARA system call.
Settings for applying knowledge updates.
SystemCallPrintSystemCalls(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.