MADARA  3.1.8
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  "KARL COMPILE ERROR: System call print_system_calls takes either 0"
61  " or 1 arguments.\n");
62  }
63 
64  // if calls hasn't been initialized yet, fill the list of system calls
65  if (calls_.size () == 0)
66  {
67  calls_["#clear_variable"] =
68  "\n#clear_var (var) or #clear_variable (var):\n"
69  " Clears the variable var in the knowledge base. This is\n"
70  " the preferred way to delete variables. It masks the variable\n"
71  " from printouts and save_modifieds but leaves a phantom record\n"
72  " in the knowledge base to safely reference in VariableReference,\n"
73  " CompiledExpression, etc.\n";
74 
75  calls_["#cos"] =
76  "\n#cos (var):\n"
77  " Returns the cosine of a term (radians)\n";
78 
79  calls_["#delete_variable"] =
80  "\n#delete_var (var) or #delete_variable (var):\n"
81  " Deletes a variable named var from the knowledge base. \n"
82  " This is an extremely unsafe operation that must only\n"
83  " be used if the variable is never used in compiled\n"
84  " expressions or variable references (including container\n"
85  " classes such as Integer, Double, Vector, etc.)\n";
86 
87  calls_["#eval"] =
88  "\n#eval (expression) or #evaluate (expression):\n"
89  " Evaluates the KaRL expression and returns a result. Works similarly\n"
90  " to the KnowledgeBase::evaluate function except this function\n"
91  " inherits the KnowledgeUpdateSettings from the eval call.\n";
92 
93  calls_["#evaluate"] =
94  "\n#eval (expression) or #evaluate (expression):\n"
95  " Evaluates the KaRL expression and returns a result. Works similarly\n"
96  " to the KnowledgeBase::evaluate function except this function\n"
97  " inherits the KnowledgeUpdateSettings from the eval call.\n";
98 
99  calls_["#expand"] =
100  "\n#expand (statement) or #expand_statement (statement):\n"
101  " Expands a statement such as 'var{.i}' into 'var0', assuming .i=0\n";
102 
103  calls_["#expand_env"] =
104  "\n#expand_env (statement) or #expand_envs (statement):\n"
105  " Expands a statement such as 'env_var=$(VAR)' into\n"
106  " env_var=VAR CONTENTS, assuming VAR has been set in the OS.\n";
107 
108  calls_["#expand_envs"] =
109  "\n#expand_env (statement) or #expand_envs (statement):\n"
110  " Expands a statement such as 'env_var=$(VAR)' into\n"
111  " env_var=VAR CONTENTS, assuming VAR has been set in the OS.\n";
112 
113  calls_["#expand_statement"] =
114  "\n#expand (statement) or #expand_statement (statement):\n"
115  " Expands a statement such as 'var{.i}' into 'var0', assuming .i=0\n";
116 
117  calls_["#fragment"] =
118  "\n#fragment (arg, first, last):\n"
119  " Returns a fragment of the arg, startin at first byte and extending\n"
120  " to last byte.\n";
121 
122  calls_["#get_clock"] =
123  "\n#get_clock () or #get_clock (variable):\n"
124  " Returns either the system clock or the variable clock. Note that\n"
125  " this is a Lamport clock value and not a wall time value.\n";
126 
127  calls_["#get_time"] =
128  "\n#get_time () or #get_time_ns() or #get_time_nano():\n"
129  " Returns wall clock time on the local machine in nanoseconds.\n";
130 
131  calls_["#get_time_seconds"] =
132  "\n#get_time_seconds () or #get_time_s ():\n"
133  " Returns wall clock time on the local machine in seconds.\n";
134 
135  calls_["#log_level"] =
136  "\n#log_level () or #log_level (value):\n"
137  " Returns the logging level or sets the logging level. The logging\n"
138  " level determines the granularity of MADARA log messages. The value\n"
139  " should be an integer from 0-6.\n"
140  " 0: Emergency or always messages. Default setting.\n"
141  " 1. Errors\n"
142  " 2. Warnings\n"
143  " 3. Major events\n"
144  " 4. Minor events\n"
145  " 5. Trace events\n"
146  " 6. Detailed logging\n";
147 
148  calls_["#pow"] =
149  "\n#pow (base, power):\n"
150  " Returns the base taken to a power (exponent)\n";
151 
152  calls_["#print"] =
153  "\n#print (statement):\n"
154  " Prints a statement that can contain MADARA KaRL variables.\n"
155  " Variables are expanding to include their values where applicable.\n"
156  " For instance, 'var{.id}' would result in printing 'var0' if .id\n"
157  " was set to 0. The same statement would print 'variable' if .id\n"
158  " was set to 'iable'.\n";
159 
160  calls_["#print_system_calls"] =
161  "\n#print_system_calls () or #print_system_call (system_call):\n"
162  " Prints help for all system calls or a specific system call.\n";
163 
164  calls_["#rand_double"] =
165  "\n#rand_double (floor = 0.0, ceiling = 1.0, update_srand = true):\n"
166  " Generates a random double from a uniform distribution.\n";
167 
168  calls_["#rand_int"] =
169  "\n#rand_int (floor = 0, ceiling = RAND_MAX, update_srand = true):\n"
170  " Generates a random integer from a uniform distribution.\n";
171 
172  calls_["#read_file"] =
173  "\n#read_file (filename) or #read_file (filename, type):\n"
174  " Reads a file and returns its contents. Default is to determine\n"
175  " file type by filname extension. Explicit type can be a string\n"
176  " or an integer of the following values:\n"
177  " 'xml' = 16\n"
178  " 'text' = 32\n"
179  " 'jpeg' = 256\n";
180 
181  calls_["#set_clock"] =
182  "\n#set_clock (value) or #set_clock (variable, value):\n"
183  " Sets the system clock or a variable clock. The value should be\n"
184  " a non-zero integer (Lamport clock) and is used to determine which\n"
185  " variable values are rejected at the transport layer. Returns the\n"
186  " new clock value.\n";
187 
188  calls_["#set_fixed"] =
189  "\n#set_fixed () or #fixed ():\n"
190  " Sets the output format for doubles to fixed format (std::fixed).\n";
191 
192  calls_["#set_precision"] =
193  "\n#set_precision (value) or #precision (value):\n"
194  " Sets the precision that is used to convert doubles to strings.\n"
195  " This is a fixed precision (e.g., 5 for pi would be 3.14159).\n"
196  " Negative value resets to default precision.\n";
197 
198  calls_["#set_scientific"] =
199  "\n#set_scientific () or #scientific ():\n"
200  " Sets the output format for doubles to scientific format\n"
201  " (std::scientific).\n";
202 
203  calls_["#sin"] =
204  "\n#sin (var):\n"
205  " Returns the sine of a term (radians)\n";
206 
207  calls_["#size"] =
208  "\n#size (value):\n"
209  " Returns the size of the variable or value.\n";
210 
211  calls_["#sleep"] =
212  "\n#sleep (double time_to_sleep in s):\n"
213  " Sleeps for at least the specified time and returns time\n"
214  " spent sleeping in seconds.\n";
215 
216  calls_["#sqrt"] =
217  "\n#sqrt (value):\n"
218  " Returns the square root of a value\n";
219 
220  calls_["#tan"] =
221  "\n#tan (value):\n"
222  " Returns the tangent of a term (radians)\n";
223 
224  calls_["#to_buffer"] =
225  "\n#to_buffer (value) or #buffer (value):\n"
226  " Converts the value to an unsigned char array.\n";
227 
228  calls_["#to_double"] =
229  "\n#to_double (value) or #double (value):\n"
230  " Converts the value to a double.\n";
231 
232  calls_["#to_doubles"] =
233  "\n#to_doubles (value) or #doubles (value):\n"
234  " Converts the value to an array of doubles.\n";
235 
236  calls_["#to_host_dirs"] =
237  "\n#to_host_dirs (directory):\n"
238  " Returns the host appropriate directory structure, e.g.,.\n"
239  " files/file.txt to files\file.txt on Windows.\n";
240 
241  calls_["#to_integer"] =
242  "\n#to_integer (value) or #integer (value):\n"
243  " Converts the value to an integer.\n";
244 
245  calls_["#to_integers"] =
246  "\n#to_integers (value) or #integers (value):\n"
247  " Converts the value to an array of integers.\n";
248 
249  calls_["#to_string"] =
250  "\n#to_string (value, delimiter) or #string (value, delimiter):\n"
251  " Converts the value to an ASCII string. Delimiter is an optional\n"
252  " string sequence that separates elements of an integer or double"
253  " array.\n";
254 
255  calls_["#type"] =
256  "\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 
277  return result;
278 }
279 
285 {
286  knowledge::KnowledgeRecord return_value;
287 
288  if (nodes_.size () == 1)
289  {
291  "System call print_system_calls is printing help\n");
292 
293  context_.print (
294  calls_[nodes_[0]->evaluate (settings).to_string ()], 0);
295 
297  }
298  else if (nodes_.size () == 0)
299  {
301  "System call print_system_calls is printing help\n");
302 
303  for (SystemCallsHelp::const_iterator i = calls_.begin ();
304  i != calls_.end (); ++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  "KARL RUNTIME ERROR: System call print_system_calls takes either 0"
315  " or 1 arguments\n");
316  }
317 
318  return return_value;
319 }
320 
321 // accept a visitor
322 void
324  madara::expression::Visitor &visitor) const
325 {
326  visitor.visit (*this);
327 }
328 
329 #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:96
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:90
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
madara::knowledge::ThreadSafeContext & context_
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.