MADARA  3.1.8
SystemCallSetClock.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 
10 
11 
14  const ComponentNodes & nodes)
15  : SystemCallNode (context, nodes)
16 {
17 
18 }
19 
20 // Dtor
22 {
23 }
24 
27 {
29 }
30 
36 {
37  // user can always change a function, and we have no control over
38  // what it does. Consequently, a function node cannot be pruned out
39  // under any situation
40  can_change = true;
41 
43 
44  for (ComponentNodes::iterator i = nodes_.begin (); i != nodes_.end ();
45  ++i)
46  {
47  bool arg_can_change = false;
48  result = (*i)->prune (arg_can_change);
49 
50  if (!arg_can_change && dynamic_cast <LeafNode *> (*i) == 0)
51  {
52  delete *i;
53  *i = new LeafNode (*(this->logger_), result);
54  }
55  }
56 
57  if (nodes_.size () > 2 || nodes_.size () == 0)
58  {
60  "KARL COMPILE ERROR: System call set_clock requires 1-2 arguments, "
61  "e.g., set_clock (5) or set_clock (var, 5)\n");
62  }
63 
64 
65  return result;
66 }
67 
73 {
74  knowledge::KnowledgeRecord return_value;
75 
76  if (nodes_.size () == 1)
77  {
79  "System call set_clock is setting the system clock\n");
80 
81  context_.set_clock (nodes_[0]->evaluate (settings).to_integer ());
82 
84  }
85  else if (nodes_.size () == 2)
86  {
88  "System call set_clock is setting a variable clock\n");
89 
90  VariableNode * variable = dynamic_cast <VariableNode *> (nodes_[0]);
91  if (variable)
92  {
93  variable->get_record ()->clock =
94  (uint64_t) nodes_[1]->evaluate (settings).to_integer ();
95 
97  variable->get_record ()->clock);
98  }
99  else
100  {
102  "KARL RUNTIME ERROR: System call set_clock with 2 arguments "
103  "requires the first argument to be a variable.\n");
104  }
105 
106  }
107  else
108  {
110  "KARL RUNTIME ERROR: System call set_clock requires 1-2 arguments, "
111  "e.g., set_clock (5) or set_clock (var, 5)\n");
112  }
113 
114  return return_value;
115 }
116 
117 // accept a visitor
118 void
120  madara::expression::Visitor &visitor) const
121 {
122  visitor.visit (*this);
123 }
124 
125 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
virtual ~SystemCallSetClock(void)
Destructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
Defines a terminal node of that references the current value stored in a variable.
Definition: VariableNode.h:28
uint64_t set_clock(uint64_t clock)
Atomically sets the lamport clock.
madara::knowledge::KnowledgeRecord KnowledgeRecord
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:96
This class stores variables and their values for use by any entity needing state information in a thr...
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
madara::knowledge::KnowledgeRecord * get_record(void)
Retrieves the underlying knowledge::KnowledgeRecord in the context (useful for system calls)...
Definition: VariableNode.h:98
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:90
Integer to_integer(void) const
converts the value to an integer
madara::knowledge::ThreadSafeContext & context_
Interface for a MADARA system call.
uint64_t get_clock(void) const
Atomically gets the Lamport clock.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
uint64_t clock
last modification time
SystemCallSetClock(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.