MADARA  3.2.3
CompositeAssignmentNode.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 #ifndef _ASSIGNMENT_NODE_CPP_
4 #define _ASSIGNMENT_NODE_CPP_
5 
6 #ifndef _MADARA_NO_KARL_
7 
8 #include <iostream>
9 
14 
15 
16 // Ctor
17 
20  ComponentNode *left, ComponentNode *right)
21 : CompositeUnaryNode (logger, right)
22 {
23  var_ = dynamic_cast <VariableNode *> (left);
24 
25  if (!var_)
26  array_ = dynamic_cast <CompositeArrayReference *> (left);
27 }
28 
31 {
32  return knowledge::KnowledgeRecord ("=");
33 }
34 
35 
38 {
39  bool left_child_can_change = false;
40  bool right_child_can_change = false;
42 
43  if (this->var_ != 0 || this->array_ != 0)
44  left_child_can_change = true;
45  else
46  {
48  "CompositeAssignmentNode: "
49  "KARL COMPILE ERROR (=): "
50  "Assignments must have a variable left hand side.\n");
51 
52  throw KarlException ("CompositeAssignmentNode: "
53  "KARL COMPILE ERROR (=): "
54  "Assignments must have a variable left hand side");
55  }
56 
57  if (this->right_)
58  {
59  right_value = this->right_->prune (right_child_can_change);
60  if (!right_child_can_change && dynamic_cast <LeafNode *> (right_) == 0)
61  {
62  delete this->right_;
63  this->right_ = new LeafNode (*(this->logger_), right_value);
64  }
65  }
66  else
67  {
68  // we should never be able to get here but whatever
70  "CompositeAssignmentNode: KARL COMPILE ERROR: "
71  "Assignment has no right expression\n");
72 
73  throw KarlException (
74  "CompositeAssignmentNode: KARL COMPILE ERROR: "
75  "Assignment has no right expression\n");
76  }
77 
78  can_change = left_child_can_change || right_child_can_change;
79 
80  return right_value;
81 }
82 
88 {
90 
91  // get the value from the right side and set the variable's value with it
92  //madara::knowledge::KnowledgeRecord value = right_->evaluate ();
93  if (var_)
94  {
96  "CompositeAssignmentNode::evaluate: "
97  "Attempting to set variable %s to %s.\n",
98  var_->expand_key ().c_str (),
99  rhs.to_string ().c_str ());
100 
101  var_->set (rhs, settings);
102  }
103  else if (array_)
104  {
106  "CompositeAssignmentNode::evaluate: "
107  "Attempting to set index of var %s to %s.\n",
108  array_->expand_key ().c_str (),
109  rhs.to_string ().c_str ());
110 
111  array_->set (rhs, settings);
112  }
113  else
114  {
116  "left hand side was neither a variable nor an array reference. "
117  "Check your expression for errors.\n");
118  }
119 
120  // return the value
121  return rhs;
122 }
123 
124 
125 // accept a visitor
126 void
128 {
129  visitor.visit (*this);
130 }
131 
132 #endif // _MADARA_NO_KARL_
133 
134 #endif /* _ASSIGNMENT_NODE_CPP_ */
This class encapsulates an entry in a KnowledgeBase.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)=0
Prunes the expression tree of unnecessary nodes.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
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.
ComponentNode * right_
Right expression.
madara::knowledge::KnowledgeRecord KnowledgeRecord
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
CompositeAssignmentNode(logger::Logger &logger, ComponentNode *left, ComponentNode *right)
Constructor.
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:97
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
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
#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.
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
VariableNode * var_
Left should always be a variable node.
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
virtual ComponentNode * left(void) const
Returns the left expression.
Settings for applying knowledge updates.
Encapsulates a single expression tree.
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 prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
Defines a terminal node of that references the current value stored in a variable.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.