MADARA  3.2.3
CompositeForLoop.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _FOR_LOOP_CPP_
3 #define _FOR_LOOP_CPP_
4 
5 #ifndef _MADARA_NO_KARL_
6 
7 #include <iostream>
8 
15 
16 
17 
18 // Ctor
19 
21  ComponentNode * precondition,
22  ComponentNode * condition,
23  ComponentNode * postcondition,
24  ComponentNode * body,
26 : ComponentNode (context.get_logger ()),
27  precondition_ (precondition), condition_ (condition),
28  postcondition_ (postcondition), body_ (body)
29 {
30 
31 }
32 
33 // Dtor
35 {
36 }
37 
40 {
42  record.set_value ("for (;;)");
43  return record;
44 }
45 
51 {
52  // user can always change a function, and we have no control over
53  // what it does. Consequently, a function node cannot be pruned out
54  // under any situation
55  can_change = true;
56 
58  return zero;
59 }
60 
66 {
68  "CompositeForLoop::evaluate: Executing precondition\n");
69 
70  precondition_->evaluate (settings);
71 
73  while (condition_->evaluate (settings).is_true ())
74  {
76  "CompositeForLoop::evaluate: Executing loop body\n");
77 
78  body_->evaluate (settings);
79 
81  "CompositeForLoop::evaluate: Executing postcondition\n");
82 
83  postcondition_->evaluate (settings);
84  ++count;
85  }
86 
87  // return is the number of successful body executions
88 
90  evaluations.set_value (count);
91  return evaluations;
92 }
93 
94 // accept a visitor
95 void
97 {
98  visitor.visit (*this);
99 }
100 
101 #endif // _MADARA_NO_KARL_
102 
103 #endif /* _FOR_LOOP_CPP_ */
This class encapsulates an entry in a KnowledgeBase.
bool is_true(void) const
Checks to see if the record is true.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:97
This class stores variables and their values for use by any entity needing state information in a thr...
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)=0
Evaluates the expression tree.
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
virtual ~CompositeForLoop(void)
Destructor.
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
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.
CompositeForLoop(ComponentNode *precondition, ComponentNode *condition, ComponentNode *postcondition, ComponentNode *body, madara::knowledge::ThreadSafeContext &context)
Constructor.