MADARA  3.1.8
VariableMultiplyNode.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),
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 
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 Multiply Node has no variable");
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 multiply assignment "
83  "has no right expression");
84 
85  exit (-1);
86  }
87 
88  can_change = left_child_can_change || right_child_can_change;
89 
90  return right_value;
91 }
92 
98 {
100 
101  if (rhs_)
102  rhs = rhs_->evaluate (settings);
103  else
104  rhs = value_;
105 
106  if (var_)
107  {
109  "VariableMultiplyNode::evaluate: "
110  "Attempting to set variable %s to %s.\n",
111  var_->expand_key ().c_str (),
112  rhs.to_string ().c_str ());
113 
114  knowledge::KnowledgeRecord result (var_->evaluate (settings) * rhs);
115  var_->set (result, settings);
116  return result;
117  }
118  else if (array_)
119  {
121  "VariableMultiplyNode::evaluate: "
122  "Attempting to set index of var %s to %s.\n",
123  array_->expand_key ().c_str (),
124  rhs.to_string ().c_str ());
125 
126  knowledge::KnowledgeRecord result (array_->evaluate (settings) * rhs);
127  array_->set (result, settings);
128  return result;
129  }
130  else
131  {
133  "VariableMultiplyNode::evaluate: "
134  "left hand side was neither a variable nor an array reference. "
135  "Check your expression for errors.\n",
136  array_->expand_key ().c_str (),
137  rhs.to_string ().c_str ());
138 
139  }
140 
141  // return the value
142  return rhs;
143 }
144 
145 
146 #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.
madara::knowledge::KnowledgeRecord value_
amount to increment by. Note that this can also do decrement.
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.
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:96
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
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.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
ComponentNode * rhs_
holds a right hand side argument if it is not value_
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.
CompositeArrayReference * array_
variable index holder
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 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.
VariableMultiplyNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.
std::string expand_key(void) const
Expands the key (if necessary).
std::string expand_key(void) const
Expands the key (if necessary).
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
Defines a terminal node of that references the current value stored in a variable.