MADARA  3.1.8
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  "KARL COMPILE ERROR: Variable assignment has no variable\n");
49  exit (-1);
50  }
51 
52  if (this->right_)
53  {
54  right_value = this->right_->prune (right_child_can_change);
55  if (!right_child_can_change && dynamic_cast <LeafNode *> (right_) == 0)
56  {
57  delete this->right_;
58  this->right_ = new LeafNode (*(this->logger_), right_value);
59  }
60  }
61  else
62  {
64  "KARL COMPILE ERROR: Assignment has no right expression\n");
65 
66  exit (-1);
67  }
68 
69  can_change = left_child_can_change || right_child_can_change;
70 
71  return right_value;
72 }
73 
79 {
81 
82  // get the value from the right side and set the variable's value with it
83  //madara::knowledge::KnowledgeRecord value = right_->evaluate ();
84  if (var_)
85  {
87  "CompositeAssignmentNode::evaluate: "
88  "Attempting to set variable %s to %s.\n",
89  var_->expand_key ().c_str (),
90  rhs.to_string ().c_str ());
91 
92  var_->set (rhs, settings);
93  }
94  else if (array_)
95  {
97  "CompositeAssignmentNode::evaluate: "
98  "Attempting to set index of var %s to %s.\n",
99  array_->expand_key ().c_str (),
100  rhs.to_string ().c_str ());
101 
102  array_->set (rhs, settings);
103  }
104  else
105  {
107  "left hand side was neither a variable nor an array reference. "
108  "Check your expression for errors.\n");
109  }
110 
111  // return the value
112  return rhs;
113 }
114 
115 
116 // accept a visitor
117 void
119 {
120  visitor.visit (*this);
121 }
122 
123 #endif // _MADARA_NO_KARL_
124 
125 #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:96
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:35
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:90
VariableNode * var_
Left should always be a variable node.
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.