MADARA  3.1.8
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), 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 
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  "KARL COMPILE ERROR: Variable assignment has no variable\n");
66 
67  exit (-1);
68  }
69 
70  if (this->rhs_)
71  {
72  right_value = this->rhs_->prune (right_child_can_change);
73  if (!right_child_can_change && dynamic_cast <LeafNode *> (rhs_) == 0)
74  {
75  delete this->rhs_;
76  this->rhs_ = new LeafNode (*(this->logger_), right_value);
77  }
78  }
79  else
80  {
82  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
83 
84  exit (-1);
85  }
86 
87  can_change = left_child_can_change || right_child_can_change;
88 
89  return right_value;
90 }
91 
97 {
99 
100  if (rhs_)
101  rhs = rhs_->evaluate (settings);
102  else
103  rhs = value_;
104 
105  if (var_)
106  {
108  "CompositeAssignmentNode::evaluate: "
109  "Attempting to set variable %s to %s.\n",
110  var_->expand_key ().c_str (),
111  rhs.to_string ().c_str ());
112 
113  knowledge::KnowledgeRecord result (var_->evaluate (settings) - rhs);
114  var_->set (result, settings);
115  return result;
116  }
117  else if (array_)
118  {
120  "CompositeAssignmentNode::evaluate: "
121  "Attempting to set index of var %s to %s.\n",
122  array_->expand_key ().c_str (),
123  rhs.to_string ().c_str ());
124 
125  knowledge::KnowledgeRecord result (array_->evaluate (settings) - rhs);
126  array_->set (result, settings);
127  return result;
128  }
129  else
130  {
132  "CompositeAssignmentNode::evaluate: "
133  "left hand side was neither a variable nor an array reference. "
134  "Check your expression for errors.\n",
135  array_->expand_key ().c_str (),
136  rhs.to_string ().c_str ());
137 
138  }
139 
140  // return the value
141  return rhs;
142 }
143 
144 #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:96
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:35
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:90
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
CompositeArrayReference * array_
variable index holder
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.