MADARA  3.4.1
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 // Ctor
17 
19  ComponentNode* precondition, ComponentNode* condition,
20  ComponentNode* postcondition, ComponentNode* body,
22  : ComponentNode(context.get_logger()),
23  precondition_(precondition),
24  condition_(condition),
25  postcondition_(postcondition),
26  body_(body)
27 {
28 }
29 
30 // Dtor
32 
34  void) const
35 {
37  record.set_value("for (;;)");
38  return record;
39 }
40 
45  bool& can_change)
46 {
47  // user can always change a function, and we have no control over
48  // what it does. Consequently, a function node cannot be pruned out
49  // under any situation
50  can_change = true;
51 
53  return zero;
54 }
55 
61 {
63  "CompositeForLoop::evaluate: Executing precondition\n");
64 
65  precondition_->evaluate(settings);
66 
68  while (condition_->evaluate(settings).is_true())
69  {
71  "CompositeForLoop::evaluate: Executing loop body\n");
72 
73  body_->evaluate(settings);
74 
76  "CompositeForLoop::evaluate: Executing postcondition\n");
77 
78  postcondition_->evaluate(settings);
79  ++count;
80  }
81 
82  // return is the number of successful body executions
83 
85  evaluations.set_value(count);
86  return evaluations;
87 }
88 
89 // accept a visitor
91 {
92  visitor.visit(*this);
93 }
94 
95 #endif // _MADARA_NO_KARL_
96 
97 #endif /* _FOR_LOOP_CPP_ */
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
An abstract base class defines a simple abstract implementation of an expression tree node.
Definition: ComponentNode.h:37
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
CompositeForLoop(ComponentNode *precondition, ComponentNode *condition, ComponentNode *postcondition, ComponentNode *body, madara::knowledge::ThreadSafeContext &context)
Constructor.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
virtual ~CompositeForLoop(void)
Destructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:93
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
This class encapsulates an entry in a KnowledgeBase.
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy toi, clock, and write_quality.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...