MADARA  3.1.8
CompositeModulusNode.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _MODULUS_NODE_CPP_
3 #define _MODULUS_NODE_CPP_
4 
5 #ifndef _MADARA_NO_KARL_
6 
7 #include <iostream>
8 
14 
17  : CompositeBinaryNode (logger, left, right)
18 {
19 }
20 
21 // Dtor
23 {
24 }
25 
28 {
30  record.set_value ("%");
31  return record;
32 }
33 
34 
40 {
41  bool left_child_can_change = false;
42  bool right_child_can_change = false;
45 
46  if (this->left_)
47  {
48  left_value = this->left_->prune (left_child_can_change);
49  if (!left_child_can_change && dynamic_cast <LeafNode *> (left_) == 0)
50  {
51  delete this->left_;
52  this->left_ = new LeafNode (*(this->logger_), left_value);
53  }
54  }
55  else
56  {
58  "KARL COMPILE ERROR: Modulus has no left expression\n");
59 
60  exit (-1);
61  }
62 
63  if (this->right_)
64  {
65  right_value = this->right_->prune (right_child_can_change);
66  if (!right_child_can_change && dynamic_cast <LeafNode *> (right_) == 0)
67  {
68  delete this->right_;
69  this->right_ = new LeafNode (*(this->logger_), right_value);
70 
71  if (right_value == madara::knowledge::KnowledgeRecord::Integer (0))
72  {
74  "KARL COMPILE ERROR: Modulus results in permanent divide by zero\n");
75 
76  exit (-1);
77  }
78  }
79  }
80  else
81  {
83  "KARL COMPILE ERROR: "
84  "Modulus has no right expression (divide by zero)\n");
85 
86  exit (-1);
87  }
88 
89  can_change = left_child_can_change || right_child_can_change;
90 
91  return left_value % right_value;
92 }
93 
99 {
100  return left_->evaluate (settings) % right_->evaluate (settings);
101 }
102 
103 
104 // accept a visitor
105 void
107 {
108  visitor.visit (*this);
109 }
110 
111 #endif // _MADARA_NO_KARL_
112 
113 #endif /* _MODULUS_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.
ComponentNode * right_
Right expression.
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 left and right node (via inheritance from CompositeUnaryNode).
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
ComponentNode * left_
left expression
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
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 item(void) const
Returns the printable character of the node.
CompositeModulusNode(logger::Logger &logger, ComponentNode *left, ComponentNode *right)
Constructor.
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 expression tree.