MADARA  3.2.3
SystemCallFragment.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 
8 
9 
12  const ComponentNodes & nodes)
13  : SystemCallNode (context, nodes)
14 {
15 
16 }
17 
18 // Dtor
20 {
21 }
22 
25 {
27 }
28 
34 {
35  // user can always change a function, and we have no control over
36  // what it does. Consequently, a function node cannot be pruned out
37  // under any situation
38  can_change = true;
39 
41 
42  for (ComponentNodes::iterator i = nodes_.begin (); i != nodes_.end ();
43  ++i)
44  {
45  bool arg_can_change = false;
46  result = (*i)->prune (arg_can_change);
47 
48  if (!arg_can_change && dynamic_cast <LeafNode *> (*i) == 0)
49  {
50  delete *i;
51  *i = new LeafNode (*(this->logger_), result);
52  }
53  }
54 
55  if (nodes_.size () != 3)
56  {
58  "madara::expression::SystemCallFragment: "
59  "KARL COMPILE ERROR:"
60  "System call fragment"
61  " requires three arguments, e.g."
62  " #fragment ('hello world', 0, 4) will return 'hello'\n");
63 
64  throw KarlException ("madara::expression::SystemCallFragment: "
65  "KARL COMPILE ERROR: "
66  "System call fragment"
67  " requires three arguments, e.g."
68  " #fragment ('hello world', 0, 4) will return 'hello'\n");
69  }
70 
71  return result;
72 }
73 
79 {
80  knowledge::KnowledgeRecord return_value;
81 
82  if (nodes_.size () == 3)
83  {
84  // grab the first and last ranges
85  unsigned int first =
86  (unsigned int) nodes_[1]->evaluate (settings).to_integer ();
87  unsigned int last =
88  (unsigned int) nodes_[2]->evaluate (settings).to_integer ();
89 
91  "madara::expression::SystemCallFragment: "
92  "System call fragment is returning segment [%d-%d].\n",
93  first, last);
94 
95  return nodes_[0]->evaluate (settings).fragment (first, last);
96  }
97 
98  else
99  {
101  "madara::expression::SystemCallFragment: "
102  "KARL RUNTIME ERROR:"
103  "System call fragment"
104  " requires three arguments, e.g."
105  " #fragment ('hello world', 0, 4) will return 'hello'\n");
106 
107  throw KarlException ("madara::expression::SystemCallFragment: "
108  "KARL RUNTIME ERROR: "
109  "System call fragment"
110  " requires three arguments, e.g."
111  " #fragment ('hello world', 0, 4) will return 'hello'\n");
112  }
113 
114 
115  return return_value;
116 }
117 
118 // accept a visitor
119 void
121  madara::expression::Visitor &visitor) const
122 {
123  visitor.visit (*this);
124 }
125 
126 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
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...
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
SystemCallFragment(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
Integer to_integer(void) const
converts the value to an integer.
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
Interface for a MADARA system call.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual ~SystemCallFragment(void)
Destructor.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.