MADARA  3.4.1
VariableMultiplyNode.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
7 
8 #include <string>
9 #include <sstream>
10 
14  : ComponentNode(context.get_logger()),
15  var_(0),
16  array_(0),
17  value_(value),
18  rhs_(rhs)
19 {
20  var_ = dynamic_cast<VariableNode*>(lhs);
21 
22  if (!var_)
23  array_ = dynamic_cast<CompositeArrayReference*>(lhs);
24 }
25 
27 {
28  // do not clean up record_. Let the context clean that up.
29 }
30 
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::VariableMultiplyNode: "
65  "KARL COMPILE ERROR: Variable assignment has no variable\n");
66 
67  throw exceptions::KarlException("madara::expression::VariableMultiplyNode: "
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::VariableMultiplyNode: "
85  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
86 
87  throw exceptions::KarlException("madara::expression::VariableMultiplyNode: "
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::VariableMultiplyNode::evaluate: "
114  "Attempting to set variable %s to %s.\n",
115  var_->expand_key().c_str(), rhs.to_string().c_str());
116 
117  knowledge::KnowledgeRecord result(var_->evaluate(settings) * rhs);
118  var_->set(result, settings);
119  return result;
120  }
121  else if (array_)
122  {
124  "madara::expression::VariableMultiplyNode::evaluate: "
125  "Attempting to set index of var %s to %s.\n",
126  array_->expand_key().c_str(), rhs.to_string().c_str());
127 
128  knowledge::KnowledgeRecord result(array_->evaluate(settings) * rhs);
129  array_->set(result, settings);
130  return result;
131  }
132  else
133  {
135  "madara::expression::VariableMultiplyNode::evaluate: "
136  "left hand side was neither a variable nor an array reference. "
137  "Check your expression for errors.\n",
138  array_->expand_key().c_str(), rhs.to_string().c_str());
139  }
140 
141  // return the value
142  return rhs;
143 }
144 
145 #endif // _MADARA_NO_KARL_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
An abstract base class defines a simple abstract implementation of an expression tree node.
Definition: ComponentNode.h:37
Defines a terminal node of that references the current value stored in a variable.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item 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.
VariableMultiplyNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
CompositeArrayReference * array_
variable index holder
Defines a terminal node of that references the current value stored in a variable.
Definition: VariableNode.h:29
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:93
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
This class encapsulates an entry in a KnowledgeBase.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...