MADARA  3.2.3
VariableDivideNode.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
7 
8 #include <math.h>
9 #include <string>
10 #include <sstream>
11 
14  ComponentNode * rhs,
16 : ComponentNode (context.get_logger ()), var_ (0),
17  array_ (0), 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::VariableDivideNode: "
65  "KARL COMPILE ERROR: Variable assignment has no variable\n");
66 
67  throw KarlException ("madara::expression::VariableDivideNode: "
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::VariableDivideNode: "
85  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
86 
87  throw KarlException ("madara::expression::VariableDivideNode: "
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 (rhs.is_false ())
111  // {
112  // rhs = NAN;
113  // }
114 
115  if (var_)
116  {
118  "madara::expression::VariableDivideNode::evaluate: "
119  "Attempting to set variable %s to %s.\n",
120  var_->expand_key ().c_str (),
121  rhs.to_string ().c_str ());
122 
123  knowledge::KnowledgeRecord result (var_->evaluate (settings) / rhs);
124  var_->set (result, settings);
125  return result;
126  }
127  else if (array_)
128  {
130  "madara::expression::VariableDivideNode::evaluate: "
131  "Attempting to set index of var %s to %s.\n",
132  array_->expand_key ().c_str (),
133  rhs.to_string ().c_str ());
134 
135  knowledge::KnowledgeRecord result (array_->evaluate (settings) / rhs);
136  array_->set (result, settings);
137  return result;
138  }
139  else
140  {
142  "madara::expression::VariableDivideNode::evaluate: "
143  "left hand side was neither a variable nor an array reference. "
144  "Check your expression for errors.\n",
145  array_->expand_key ().c_str (),
146  rhs.to_string ().c_str ());
147  }
148 
149  // return the value
150  return rhs;
151 }
152 
153 #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.
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_
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
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.
VariableDivideNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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
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.
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.
Settings for applying knowledge updates.
VariableNode * var_
variable holder
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.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
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.