MADARA  3.2.3
VariableCompareNode.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
7 
9 
10 #include <string>
11 #include <sstream>
12 
14  ComponentNode * lhs, madara::knowledge::KnowledgeRecord value, int compare_type,
16 : ComponentNode (context.get_logger ()), var_ (0), array_ (0),
17  value_ (value), rhs_ (rhs),
18  context_ (context),
19  compare_type_ (compare_type)
20 {
21  var_ = dynamic_cast <VariableNode *> (lhs);
22 
23  if (!var_)
24  array_ = dynamic_cast <CompositeArrayReference *> (lhs);
25 }
26 
28 {
29  // do not clean up record_. Let the context clean that up.
30 }
31 
32 void
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::VariableCompareNode: "
67  "KARL COMPILE ERROR: Compare has no variable\\n");
68 
69  throw KarlException ("madara::expression::VariableCompareNode: "
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::VariableCompareNode: "
87  "KARL COMPILE ERROR: Compare has no right expression\n");
88 
89  throw KarlException ("madara::expression::VariableCompareNode: "
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 {
105  KnowledgeRecord lhs;
106  KnowledgeRecord::Integer result (0);
107 
108  if (var_)
109  lhs = var_->evaluate (settings);
110  else if (array_)
111  lhs = array_->evaluate (settings);
112 
113  if (rhs_)
114  {
115  if (compare_type_ == LESS_THAN)
116  {
117  result = lhs < rhs_->evaluate (settings);
118  }
119  else if (compare_type_ == LESS_THAN_EQUAL)
120  {
121  result = lhs <= rhs_->evaluate (settings);
122  }
123  else if (compare_type_ == EQUAL)
124  {
125  result = lhs == rhs_->evaluate (settings);
126  }
127  else if (compare_type_ == GREATER_THAN_EQUAL)
128  {
129  result = lhs >= rhs_->evaluate (settings);
130  }
131  else
132  {
133  result = lhs > rhs_->evaluate (settings);
134  }
135  }
136  else
137  {
138  if (compare_type_ == LESS_THAN)
139  {
140  result = lhs < value_;
141  }
142  else if (compare_type_ == LESS_THAN_EQUAL)
143  {
144  result = lhs <= value_;
145  }
146  else if (compare_type_ == EQUAL)
147  {
148  result = lhs == value_;
149  }
150  else if (compare_type_ == GREATER_THAN_EQUAL)
151  {
152  result = lhs >= value_;
153  }
154  else
155  {
156  result = lhs > value_;
157  }
158  }
159 
160  return knowledge::KnowledgeRecord (result);
161 }
162 
163 #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
madara::knowledge::KnowledgeRecord KnowledgeRecord
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the node and its children.
VariableNode * var_
variable holder
virtual void accept(Visitor &visitor) const
Define the accept() operation used for the Visitor pattern.
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.
VariableCompareNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, int type, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.
CompositeArrayReference * array_
variable index holder
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
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
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.
virtual madara::knowledge::KnowledgeRecord item(void) const
Return the item stored in the node.
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.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prune the tree of unnecessary nodes.
madara::knowledge::KnowledgeRecord value_
amount to increment by. Note that this can also do decrement.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
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.