MADARA  3.2.3
VariableDecrementNode.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)
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 
31 void
33 {
34  visitor.visit (*this);
35 }
36 
39 {
41 
42  if (var_)
43  value = var_->item ();
44  else if (array_)
45  value = array_->item ();
46 
47  return value;
48 }
49 
55 {
56  bool left_child_can_change = false;
57  bool right_child_can_change = false;
59 
60  if (this->var_ != 0 || this->array_ != 0)
61  left_child_can_change = true;
62  else
63  {
65  "madara::expression::VariableDecrementNode: "
66  "KARL COMPILE ERROR: Variable assignment has no variable\n");
67 
68  throw KarlException ("madara::expression::VariableDecrementNode: "
69  "KARL COMPILE ERROR: "
70  "Node has no variable left-hand side\n");
71  }
72 
73  if (this->rhs_)
74  {
75  right_value = this->rhs_->prune (right_child_can_change);
76  if (!right_child_can_change && dynamic_cast <LeafNode *> (rhs_) == 0)
77  {
78  delete this->rhs_;
79  this->rhs_ = new LeafNode (*(this->logger_), right_value);
80  }
81  }
82  else
83  {
85  "madara::expression::VariableDecrementNode: "
86  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
87 
88  throw KarlException ("madara::expression::VariableDecrementNode: "
89  "KARL COMPILE ERROR: "
90  "Node has no right expression\n");
91  }
92 
93  can_change = left_child_can_change || right_child_can_change;
94 
95  return right_value;
96 }
97 
103 {
105 
106  if (rhs_)
107  rhs = rhs_->evaluate (settings);
108  else
109  rhs = value_;
110 
111  if (var_)
112  {
114  "madara::expression::CompositeAssignmentNode::evaluate: "
115  "Attempting to set variable %s to %s.\n",
116  var_->expand_key ().c_str (),
117  rhs.to_string ().c_str ());
118 
119  knowledge::KnowledgeRecord result (var_->evaluate (settings) - rhs);
120  var_->set (result, settings);
121  return result;
122  }
123  else if (array_)
124  {
126  "madara::expression::CompositeAssignmentNode::evaluate: "
127  "Attempting to set index of var %s to %s.\n",
128  array_->expand_key ().c_str (),
129  rhs.to_string ().c_str ());
130 
131  knowledge::KnowledgeRecord result (array_->evaluate (settings) - rhs);
132  array_->set (result, settings);
133  return result;
134  }
135  else
136  {
138  "madara::expression::CompositeAssignmentNode::evaluate: "
139  "left hand side was neither a variable nor an array reference. "
140  "Check your expression for errors.\n",
141  array_->expand_key ().c_str (),
142  rhs.to_string ().c_str ());
143 
144  }
145 
146  // return the value
147  return rhs;
148 }
149 
150 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
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.
ComponentNode * rhs_
holds a right hand side argument if it is not value_
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
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.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
madara::knowledge::KnowledgeRecord value_
amount to increment by. Note that this can also do decrement.
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.
#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
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
CompositeArrayReference * array_
variable index holder
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.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
VariableDecrementNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.