MADARA  3.2.3
VariableIncrementNode.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
7 
8 
9 #include <string>
10 #include <sstream>
11 
14  ComponentNode * rhs,
16 : ComponentNode (context.get_logger ()), var_ (0), array_ (0),
17  value_ (value), rhs_ (rhs), context_ (context)
18 {
19  var_ = dynamic_cast <VariableNode *> (lhs);
20 
21  if (!var_)
22  array_ = dynamic_cast <CompositeArrayReference *> (lhs);
23 }
24 
26 {
27  // do not clean up record_. Let the context clean that up.
28 }
29 
30 void
32 {
33  visitor.visit (*this);
34 }
35 
38 {
40 
41  if (var_)
42  value = var_->item ();
43  else if (array_)
44  value = array_->item ();
45 
46  return value;
47 }
48 
54 {
55  bool left_child_can_change = false;
56  bool right_child_can_change = false;
58 
59  if (this->var_ != 0 || this->array_ != 0)
60  left_child_can_change = true;
61  else
62  {
64  "madara::expression::VariableIncrementNode: "
65  "KARL COMPILE ERROR: Variable assignment has no variable\n");
66 
67  throw KarlException ("madara::expression::VariableIncrementNode: "
68  "KARL COMPILE ERROR: "
69  "Node has no variable left-hand side\n");
70  }
71 
72  if (this->rhs_)
73  {
74  right_value = this->rhs_->prune (right_child_can_change);
75  if (!right_child_can_change && dynamic_cast <LeafNode *> (rhs_) == 0)
76  {
77  delete this->rhs_;
78  this->rhs_ = new LeafNode (*(this->logger_), right_value);
79  }
80  }
81  else
82  {
84  "madara::expression::VariableIncrementNode: "
85  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
86 
87  throw KarlException ("madara::expression::VariableIncrementNode: "
88  "KARL COMPILE ERROR: "
89  "Node has no right expression\n");
90  }
91 
92  can_change = left_child_can_change || right_child_can_change;
93 
94  return right_value;
95 }
96 
102 {
104 
105  if (rhs_)
106  rhs = rhs_->evaluate (settings);
107  else
108  rhs = value_;
109 
110  if (var_)
111  {
113  "madara::expression::CompositeAssignmentNode::evaluate: "
114  "Attempting to set variable %s to %s.\n",
115  var_->expand_key ().c_str (),
116  rhs.to_string ().c_str ());
117 
118  knowledge::KnowledgeRecord result (var_->evaluate (settings) + rhs);
119  var_->set (result, settings);
120  return result;
121  }
122  else if (array_)
123  {
125  "madara::expression::CompositeAssignmentNode::evaluate: "
126  "Attempting to set index of var %s to %s.\n",
127  array_->expand_key ().c_str (),
128  rhs.to_string ().c_str ());
129 
130  knowledge::KnowledgeRecord result (array_->evaluate (settings) + rhs);
131  array_->set (result, settings);
132  return result;
133  }
134  else
135  {
137  "madara::expression::CompositeAssignmentNode::evaluate: "
138  "left hand side was neither a variable nor an array reference. "
139  "Check your expression for errors.\n",
140  array_->expand_key ().c_str (),
141  rhs.to_string ().c_str ());
142  }
143 
144  // return the value
145  return rhs;
146 }
147 
148 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
CompositeArrayReference * array_
variable index holder
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)=0
Prunes the expression tree of unnecessary nodes.
Defines a terminal node of that references the current value stored in a variable.
Definition: VariableNode.h:28
int set(const madara::knowledge::KnowledgeRecord::Integer &value, const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Sets the value stored in the node.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
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...
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)=0
Evaluates the expression tree.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
madara::knowledge::KnowledgeRecord value_
amount to increment by. Note that this can also do decrement.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
int set(const madara::knowledge::KnowledgeRecord::Integer &value, const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Sets the value stored in the node.
An abstract base class defines a simple abstract implementation of an expression tree node...
Definition: ComponentNode.h:36
VariableIncrementNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
ComponentNode * rhs_
holds a right hand side argument if it is not value_
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
std::string expand_key(void) const
Expands the key (if necessary).
std::string expand_key(void) const
Expands the key (if necessary).
Defines a terminal node of that references the current value stored in a variable.