MADARA  3.1.8
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  "KARL COMPILE ERROR: Variable assignment has no variable\\n");
67 
68  exit (-1);
69  }
70 
71  if (this->rhs_)
72  {
73  right_value = this->rhs_->prune (right_child_can_change);
74  if (!right_child_can_change && dynamic_cast <LeafNode *> (rhs_) == 0)
75  {
76  delete this->rhs_;
77  this->rhs_ = new LeafNode (*(this->logger_), right_value);
78  }
79  }
80  else
81  {
83  "KARL COMPILE ERROR: Assignment has no right expression\n");
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 {
99  KnowledgeRecord lhs;
100  KnowledgeRecord::Integer result (0);
101 
102  if (var_)
103  lhs = var_->evaluate (settings);
104  else if (array_)
105  lhs = array_->evaluate (settings);
106 
107  if (rhs_)
108  {
109  if (compare_type_ == LESS_THAN)
110  {
111  result = lhs < rhs_->evaluate (settings);
112  }
113  else if (compare_type_ == LESS_THAN_EQUAL)
114  {
115  result = lhs <= rhs_->evaluate (settings);
116  }
117  else if (compare_type_ == EQUAL)
118  {
119  result = lhs == rhs_->evaluate (settings);
120  }
121  else if (compare_type_ == GREATER_THAN_EQUAL)
122  {
123  result = lhs >= rhs_->evaluate (settings);
124  }
125  else
126  {
127  result = lhs > rhs_->evaluate (settings);
128  }
129  }
130  else
131  {
132  if (compare_type_ == LESS_THAN)
133  {
134  result = lhs < value_;
135  }
136  else if (compare_type_ == LESS_THAN_EQUAL)
137  {
138  result = lhs <= value_;
139  }
140  else if (compare_type_ == EQUAL)
141  {
142  result = lhs == value_;
143  }
144  else if (compare_type_ == GREATER_THAN_EQUAL)
145  {
146  result = lhs >= value_;
147  }
148  else
149  {
150  result = lhs > value_;
151  }
152  }
153 
154  return knowledge::KnowledgeRecord (result);
155 }
156 
157 #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:96
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: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.
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.