MADARA  3.1.8
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  "KARL COMPILE ERROR: System call fragment"
59  " requires three arguments, e.g."
60  " #fragment ('hello world', 0, 4) will return 'hello'\n");
61  }
62 
63  return result;
64 }
65 
71 {
72  knowledge::KnowledgeRecord return_value;
73 
74  if (nodes_.size () == 3)
75  {
76  // grab the first and last ranges
77  unsigned int first =
78  (unsigned int) nodes_[1]->evaluate (settings).to_integer ();
79  unsigned int last =
80  (unsigned int) nodes_[2]->evaluate (settings).to_integer ();
81 
83  "System call fragment is returning segment [%d-%d].\n",
84  first, last);
85 
86  return nodes_[0]->evaluate (settings).fragment (first, last);
87  }
88 
89  else
90  {
92  "KARL RUNTIME ERROR: System call fragment"
93  " requires three arguments, e.g."
94  " #fragment ('hello world', 0, 4) will return 'hello'\n");
95  }
96 
97 
98  return return_value;
99 }
100 
101 // accept a visitor
102 void
104  madara::expression::Visitor &visitor) const
105 {
106  visitor.visit (*this);
107 }
108 
109 #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:96
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:90
SystemCallFragment(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
Integer to_integer(void) const
converts the value to an integer
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.