MADARA  3.4.1
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  madara::knowledge::KnowledgeRecord value, int compare_type,
16  : ComponentNode(context.get_logger()),
17  var_(0),
18  array_(0),
19  value_(value),
20  rhs_(rhs),
21  context_(context),
22  compare_type_(compare_type)
23 {
24  var_ = dynamic_cast<VariableNode*>(lhs);
25 
26  if (!var_)
27  array_ = dynamic_cast<CompositeArrayReference*>(lhs);
28 }
29 
31 {
32  // do not clean up record_. Let the context clean that up.
33 }
34 
36 {
37  visitor.visit(*this);
38 }
39 
41 {
43 
44  if (var_)
45  value = var_->item();
46  else if (array_)
47  value = array_->item();
48 
49  return value;
50 }
51 
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 exceptions::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 exceptions::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 
103 {
104  KnowledgeRecord lhs;
105  KnowledgeRecord::Integer result(0);
106 
107  if (var_)
108  lhs = var_->evaluate(settings);
109  else if (array_)
110  lhs = array_->evaluate(settings);
111 
112  if (rhs_)
113  {
114  if (compare_type_ == LESS_THAN)
115  {
116  result = lhs < rhs_->evaluate(settings);
117  }
118  else if (compare_type_ == LESS_THAN_EQUAL)
119  {
120  result = lhs <= rhs_->evaluate(settings);
121  }
122  else if (compare_type_ == EQUAL)
123  {
124  result = lhs == rhs_->evaluate(settings);
125  }
126  else if (compare_type_ == GREATER_THAN_EQUAL)
127  {
128  result = lhs >= rhs_->evaluate(settings);
129  }
130  else
131  {
132  result = lhs > rhs_->evaluate(settings);
133  }
134  }
135  else
136  {
137  if (compare_type_ == LESS_THAN)
138  {
139  result = lhs < value_;
140  }
141  else if (compare_type_ == LESS_THAN_EQUAL)
142  {
143  result = lhs <= value_;
144  }
145  else if (compare_type_ == EQUAL)
146  {
147  result = lhs == value_;
148  }
149  else if (compare_type_ == GREATER_THAN_EQUAL)
150  {
151  result = lhs >= value_;
152  }
153  else
154  {
155  result = lhs > value_;
156  }
157  }
158 
159  return knowledge::KnowledgeRecord(result);
160 }
161 
162 #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_
madara::knowledge::KnowledgeRecord KnowledgeRecord
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
CompositeArrayReference * array_
variable index holder
VariableNode * var_
variable holder
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.
VariableCompareNode(ComponentNode *lhs, madara::knowledge::KnowledgeRecord value, int type, ComponentNode *rhs, madara::knowledge::ThreadSafeContext &context)
Ctor.
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.
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.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...