MADARA  3.1.8
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 <iostream>
8 
13 
14 
15 // Ctor
18  ComponentNode *left,
19  ComponentNode *right)
20 : CompositeBinaryNode (logger, left, right)
21 {
22 }
23 
24 // Dtor
26 {
27 }
28 
31 {
33  record.set_value ("/");
34  return record;
35 }
36 
42 {
43  bool left_child_can_change = false;
44  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  "KARL COMPILE ERROR: Division has no left expression\n");
62  exit (-1);
63  }
64 
65  if (this->right_)
66  {
67  right_value = this->right_->prune (right_child_can_change);
68 
69  if (!right_child_can_change && dynamic_cast <LeafNode *> (right_) == 0)
70  {
71  // leave this check which is important
72  if (right_value.is_false ())
73  {
75  "KARL COMPILE ERROR: Division results in permanent divide by zero\n");
76 
77  exit (-1);
78  }
79  // the only time we should delete right is if we have a clean division
80  if (!left_child_can_change &&
81  left_value % right_value == zero)
82  {
83  // don't worry about allocating anything. This is about to be
84  // reclaimed anyway since !right_can_change and !left_can_change
85  delete this->right_;
86  this->right_ = 0;
87  }
88  else
89  right_child_can_change = true;
90  }
91 
92  //if (!right_child_can_change)
93  // right_child_can_change = true;
94  }
95  else
96  {
98  "KARL COMPILE ERROR: Division has no right expression (divide by zero)\n");
99  exit (-1);
100  }
101 
102  can_change = left_child_can_change || right_child_can_change;
103 
104  return left_value / right_value;
105 }
106 
112 {
113  // only evaluate right if left evaluates to non-zero (0/{any_number} = 0)
116  if (lvalue.is_true ())
117  return lvalue / right_->evaluate (settings);
118 
119  // note that we are not handling divide by zero. Still unsure whether I
120  // want to use exceptions throughout evaluation or not. This should be as
121  // quick as possible. We should probably only try to catch exception like
122  // things in prune
123 
124  return zero;
125 }
126 
127 // accept a visitor
128 void
130 {
131  visitor.visit (*this);
132 }
133 
134 #endif // _MADARA_NO_KARL_
135 
136 #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.
ComponentNode * right_
Right expression.
CompositeDivideNode(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 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:35
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:90
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.
bool is_false(void) const
Checks to see if the record is false.
virtual ~CompositeDivideNode(void)
Destructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.