MADARA  3.4.1
SystemCallPrintSystemCalls.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
8 
9 // a map of all the system calls to their help information.
12 
15  : SystemCallNode(context, nodes)
16 {
17 }
18 
19 // Dtor
21  void)
22 {
23 }
24 
27 {
29 }
30 
36 {
37  // user can always change a function, and we have no control over
38  // what it does. Consequently, a function node cannot be pruned out
39  // under any situation
40  can_change = true;
41 
43 
44  if (nodes_.size() == 1)
45  {
46  bool arg_can_change = false;
47  result = nodes_[0]->prune(arg_can_change);
48 
49  if (!arg_can_change && dynamic_cast<LeafNode*>(nodes_[0]) == 0)
50  {
51  delete nodes_[0];
52  nodes_[0] = new LeafNode(*(this->logger_), result);
53  }
54  }
55  else if (nodes_.size() != 0)
56  {
58  "madara::expression::SystemCallPrintSystemCalls: "
59  "KARL COMPILE ERROR:"
60  "System call print_system_calls takes either 0"
61  " or 1 arguments\n");
62 
64  "madara::expression::SystemCallPrintSystemCalls: "
65  "KARL COMPILE ERROR: "
66  "System call print_system_calls takes either 0"
67  " or 1 arguments\n");
68  }
69 
70  // if calls hasn't been initialized yet, fill the list of system calls
71  if (calls_.size() == 0)
72  {
73  calls_["#clear_variable"] =
74  "\n#clear_var (var) or #clear_variable (var):\n"
75  " Clears the variable var in the knowledge base. This is\n"
76  " the preferred way to delete variables. It masks the variable\n"
77  " from printouts and save_modifieds but leaves a phantom record\n"
78  " in the knowledge base to safely reference in VariableReference,\n"
79  " CompiledExpression, etc.\n";
80 
81  calls_["#cos"] = "\n#cos (var):\n"
82  " Returns the cosine of a term (radians)\n";
83 
84  calls_["#delete_variable"] =
85  "\n#delete_var (var) or #delete_variable (var):\n"
86  " Deletes a variable named var from the knowledge base. \n"
87  " This is an extremely unsafe operation that must only\n"
88  " be used if the variable is never used in compiled\n"
89  " expressions or variable references (including container\n"
90  " classes such as Integer, Double, Vector, etc.)\n";
91 
92  calls_["#eval"] =
93  "\n#eval (expression) or #evaluate (expression):\n"
94  " Evaluates the KaRL expression and returns a result. Works "
95  "similarly\n"
96  " to the KnowledgeBase::evaluate function except this function\n"
97  " inherits the KnowledgeUpdateSettings from the eval call.\n";
98 
99  calls_["#evaluate"] =
100  "\n#eval (expression) or #evaluate (expression):\n"
101  " Evaluates the KaRL expression and returns a result. Works "
102  "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"] = "\n#pow (base, power):\n"
160  " Returns the base taken to a power (exponent)\n";
161 
162  calls_["#print"] =
163  "\n#print (statement):\n"
164  " Prints a statement that can contain MADARA KaRL variables.\n"
165  " Variables are expanding to include their values where applicable.\n"
166  " For instance, 'var{.id}' would result in printing 'var0' if .id\n"
167  " was set to 0. The same statement would print 'variable' if .id\n"
168  " was set to 'iable'.\n";
169 
170  calls_["#print_system_calls"] =
171  "\n#print_system_calls () or #print_system_call (system_call):\n"
172  " Prints help for all system calls or a specific system call.\n";
173 
174  calls_["#rand_double"] =
175  "\n#rand_double (floor = 0.0, ceiling = 1.0, update_srand = true):\n"
176  " Generates a random double from a uniform distribution.\n";
177 
178  calls_["#rand_int"] =
179  "\n#rand_int (floor = 0, ceiling = RAND_MAX, update_srand = true):\n"
180  " Generates a random integer from a uniform distribution.\n";
181 
182  calls_["#read_file"] =
183  "\n#read_file (filename) or #read_file (filename, type):\n"
184  " Reads a file and returns its contents. Default is to determine\n"
185  " file type by filname extension. Explicit type can be a string\n"
186  " or an integer of the following values:\n"
187  " 'xml' = 16\n"
188  " 'text' = 32\n"
189  " 'jpeg' = 256\n";
190 
191  calls_["#set_clock"] =
192  "\n#set_clock (value) or #set_clock (variable, value):\n"
193  " Sets the system clock or a variable clock. The value should be\n"
194  " a non-zero integer (Lamport clock) and is used to determine which\n"
195  " variable values are rejected at the transport layer. Returns the\n"
196  " new clock value.\n";
197 
198  calls_["#set_fixed"] =
199  "\n#set_fixed () or #fixed ():\n"
200  " Sets the output format for doubles to fixed format (std::fixed).\n";
201 
202  calls_["#set_precision"] =
203  "\n#set_precision (value) or #precision (value):\n"
204  " Sets the precision that is used to convert doubles to strings.\n"
205  " This is a fixed precision (e.g., 5 for pi would be 3.14159).\n"
206  " Negative value resets to default precision.\n";
207 
208  calls_["#set_scientific"] =
209  "\n#set_scientific () or #scientific ():\n"
210  " Sets the output format for doubles to scientific format\n"
211  " (std::scientific).\n";
212 
213  calls_["#sin"] = "\n#sin (var):\n"
214  " Returns the sine of a term (radians)\n";
215 
216  calls_["#size"] = "\n#size (value):\n"
217  " Returns the size of the variable or value.\n";
218 
219  calls_["#sleep"] =
220  "\n#sleep (double time_to_sleep in s):\n"
221  " Sleeps for at least the specified time and returns time\n"
222  " spent sleeping in seconds.\n";
223 
224  calls_["#sqrt"] = "\n#sqrt (value):\n"
225  " Returns the square root of a value\n";
226 
227  calls_["#tan"] = "\n#tan (value):\n"
228  " Returns the tangent of a term (radians)\n";
229 
230  calls_["#to_buffer"] = "\n#to_buffer (value) or #buffer (value):\n"
231  " Converts the value to an unsigned char array.\n";
232 
233  calls_["#to_double"] = "\n#to_double (value) or #double (value):\n"
234  " Converts the value to a double.\n";
235 
236  calls_["#to_doubles"] = "\n#to_doubles (value) or #doubles (value):\n"
237  " Converts the value to an array of doubles.\n";
238 
239  calls_["#to_host_dirs"] =
240  "\n#to_host_dirs (directory):\n"
241  " Returns the host appropriate directory structure, e.g.,.\n"
242  " files/file.txt to files\file.txt on Windows.\n";
243 
244  calls_["#to_integer"] = "\n#to_integer (value) or #integer (value):\n"
245  " Converts the value to an integer.\n";
246 
247  calls_["#to_integers"] = "\n#to_integers (value) or #integers (value):\n"
248  " Converts the value to an array of integers.\n";
249 
250  calls_["#to_string"] =
251  "\n#to_string (value, delimiter) or #string (value, delimiter):\n"
252  " Converts the value to an ASCII string. Delimiter is an optional\n"
253  " string sequence that separates elements of an integer or double"
254  " array.\n";
255 
256  calls_["#type"] = "\n#type (value):\n"
257  " Returns the integer type of the variable or value.\n"
258  " Supported types are:\n"
259  " UNINITIALIZED = 0\n"
260  " INTEGER = 1\n"
261  " STRING = 2\n"
262  " DOUBLE = 4\n"
263  " UNKNOWN_FILE_TYPE = 8\n"
264  " XML = 16\n"
265  " TEXT_FILE = 32\n"
266  " INTEGER_ARRAY = 64\n"
267  " DOUBLE_ARRAY = 128\n"
268  " IMAGE_JPEG = 256\n";
269 
270  calls_["#write_file"] =
271  "\n#write_file (filename, value):\n"
272  " Writes the value to a file. Supports all MADARA types.\n"
273  " @see #type.\n";
274  }
275 
276  return result;
277 }
278 
284 {
285  knowledge::KnowledgeRecord return_value;
286 
287  if (nodes_.size() == 1)
288  {
290  "madara::expression::SystemCallPrintSystemCalls: "
291  "System call print_system_calls is printing help\n");
292 
293  context_.print(calls_[nodes_[0]->evaluate(settings).to_string()], 0);
294 
296  }
297  else if (nodes_.size() == 0)
298  {
300  "madara::expression::SystemCallPrintSystemCalls: "
301  "System call print_system_calls is printing help\n");
302 
303  for (SystemCallsHelp::const_iterator i = calls_.begin(); i != calls_.end();
304  ++i)
305  {
306  if (i->first != "#delete_var" && i->first != "#eval" &&
307  i->first != "#expand" && i->first != "#expand_env")
308  context_.print(i->second, 0);
309  }
310  }
311  else
312  {
314  "madara::expression::SystemCallPrintSystemCalls: "
315  "KARL RUNTIME ERROR:"
316  "System call print_system_calls takes either 0"
317  " or 1 arguments\n");
318 
320  "madara::expression::SystemCallPrintSystemCalls: "
321  "KARL RUNTIME ERROR: "
322  "System call print_system_calls takes either 0"
323  " or 1 arguments\n");
324  }
325 
326  return return_value;
327 }
328 
329 // accept a visitor
331  madara::expression::Visitor& visitor) const
332 {
333  visitor.visit(*this);
334 }
335 
336 #endif // _MADARA_NO_KARL_
#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
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
Interface for a MADARA system call.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
SystemCallPrintSystemCalls(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:93
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
This class encapsulates an entry in a KnowledgeBase.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
std::map< std::string, std::string > SystemCallsHelp