MADARA  3.2.3
CompositeDivideNode.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _DIVIDE_NODE_CPP_
3 #define _DIVIDE_NODE_CPP_
4 
5 #ifndef _MADARA_NO_KARL_
6 
7 #include <math.h>
8 #include <iostream>
9 
14 
15 
16 // Ctor
19  ComponentNode *left,
20  ComponentNode *right)
21 : CompositeBinaryNode (logger, left, right)
22 {
23 }
24 
25 // Dtor
27 {
28 }
29 
32 {
34  record.set_value ("/");
35  return record;
36 }
37 
43 {
44  bool left_child_can_change = false;
45  bool right_child_can_change = false;
48 
49  if (this->left_)
50  {
51  left_value = this->left_->prune (left_child_can_change);
52  if (!left_child_can_change && dynamic_cast <LeafNode *> (left_) == 0)
53  {
54  delete this->left_;
55  this->left_ = new LeafNode (*(this->logger_), left_value);
56  }
57  }
58  else
59  {
61  "madara::expression::CompositeDivideNode: "
62  "KARL COMPILE ERROR: Division has no left expression\n");
63 
64  throw KarlException ("madara::expression::CompositeDivideNode: "
65  "KARL COMPILE ERROR: "
66  "Division has no left expression\n");
67  }
68 
69  if (this->right_)
70  {
71  right_value = this->right_->prune (right_child_can_change);
72 
73  if (!right_child_can_change && dynamic_cast <LeafNode *> (right_) == 0)
74  {
75  // leave this check which is important
76  if (right_value.is_true ())
77  {
78  delete right_;
79  right_ = new LeafNode (*(this->logger_), right_value);
80  }
81  }
82 
83  //if (!right_child_can_change)
84  // right_child_can_change = true;
85  }
86  else
87  {
89  "KARL COMPILE ERROR: Division has no right expression (divide by zero)\n");
90 
91  throw KarlException ("madara::expression::CompositeDivideNode: "
92  "KARL COMPILE ERROR: "
93  "Division has no right expression\n");
94  }
95 
96  can_change = left_child_can_change || right_child_can_change;
97 
98  return left_value / right_value;
99 }
100 
106 {
107  return left_->evaluate (settings) / right_->evaluate (settings);
108 }
109 
110 // accept a visitor
111 void
113 {
114  visitor.visit (*this);
115 }
116 
117 #endif // _MADARA_NO_KARL_
118 
119 #endif /* _DIVIDE_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.
bool is_true(void) const
Checks to see if the record is true.
ComponentNode * right_
Right expression.
CompositeDivideNode(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 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
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
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.
#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:36
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
virtual ~CompositeDivideNode(void)
Destructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.