MADARA  3.4.1
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 
15  : ComponentNode(context.get_logger()),
16  var_(0),
17  array_(0),
18  value_(value),
19  rhs_(rhs),
20  context_(context)
21 {
22  var_ = dynamic_cast<VariableNode*>(lhs);
23 
24  if (!var_)
25  array_ = dynamic_cast<CompositeArrayReference*>(lhs);
26 }
27 
29 {
30  // do not clean up record_. Let the context clean that up.
31 }
32 
34 {
35  visitor.visit(*this);
36 }
37 
40 {
42 
43  if (var_)
44  value = var_->item();
45  else if (array_)
46  value = array_->item();
47 
48  return value;
49 }
50 
56 {
57  bool left_child_can_change = false;
58  bool right_child_can_change = false;
60 
61  if (this->var_ != 0 || this->array_ != 0)
62  left_child_can_change = true;
63  else
64  {
66  "madara::expression::VariableDivideNode: "
67  "KARL COMPILE ERROR: Variable assignment has no variable\n");
68 
69  throw exceptions::KarlException("madara::expression::VariableDivideNode: "
70  "KARL COMPILE ERROR: "
71  "Node has no variable left-hand side\n");
72  }
73 
74  if (this->rhs_)
75  {
76  right_value = this->rhs_->prune(right_child_can_change);
77  if (!right_child_can_change && dynamic_cast<LeafNode*>(rhs_) == 0)
78  {
79  delete this->rhs_;
80  this->rhs_ = new LeafNode(*(this->logger_), right_value);
81  }
82  }
83  else
84  {
86  "madara::expression::VariableDivideNode: "
87  "KARL COMPILE ERROR: Variable assignment has no right expression\n");
88 
89  throw exceptions::KarlException("madara::expression::VariableDivideNode: "
90  "KARL COMPILE ERROR: "
91  "Node has no right expression\n");
92  }
93 
94  can_change = left_child_can_change || right_child_can_change;
95 
96  return right_value;
97 }
98 
104 {
106 
107  if (rhs_)
108  rhs = rhs_->evaluate(settings);
109  else
110  rhs = value_;
111 
112  // if (rhs.is_false ())
113  // {
114  // rhs = NAN;
115  // }
116 
117  if (var_)
118  {
120  "madara::expression::VariableDivideNode::evaluate: "
121  "Attempting to set variable %s to %s.\n",
122  var_->expand_key().c_str(), rhs.to_string().c_str());
123 
124  knowledge::KnowledgeRecord result(var_->evaluate(settings) / rhs);
125  var_->set(result, settings);
126  return result;
127  }
128  else if (array_)
129  {
131  "madara::expression::VariableDivideNode::evaluate: "
132  "Attempting to set index of var %s to %s.\n",
133  array_->expand_key().c_str(), 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(), rhs.to_string().c_str());
146  }
147 
148  // return the value
149  return rhs;
150 }
151 
152 #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
const ThreadSafeContext * context_
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.
CompositeArrayReference * array_
variable index holder
VariableNode * var_
variable holder
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
VariableDivideNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.
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...