MADARA  3.4.1
SystemCallRandInt.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
10 
13  : SystemCallNode(context, nodes)
14 {
15 }
16 
17 // Dtor
19 
21  void) const
22 {
23  return madara::knowledge::KnowledgeRecord(nodes_.size());
24 }
25 
30  bool& can_change)
31 {
32  can_change = true;
33 
35 
36  for (ComponentNodes::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
37  {
38  bool arg_can_change = false;
39  result = (*i)->prune(arg_can_change);
40 
41  if (!arg_can_change && dynamic_cast<LeafNode*>(*i) == 0)
42  {
43  delete *i;
44  *i = new LeafNode(*(this->logger_), result);
45  }
46  }
47 
48  if (nodes_.size() > 3)
49  {
51  "madara::expression::SystemCallRandInt: "
52  "KARL COMPILE ERROR:"
53  "System call rand_int"
54  " can have up to three arguments, 1) floor, "
55  "2) ceiling and 3) whether to set the random seed\n");
56 
58  "madara::expression::SystemCallRandInt: "
59  "KARL COMPILE ERROR: "
60  "System call rand_int"
61  " can have up to three arguments, 1) floor, "
62  "2) ceiling and 3) whether to set the random seed\n");
63  }
64 
65  return result;
66 }
67 
73 {
74  int64_t floor(0), ceiling(1);
75  bool update_srand = true;
76 
77  if (nodes_.size() > 0)
78  {
79  floor = nodes_[0]->evaluate(settings).to_integer();
80 
81  if (nodes_.size() > 1)
82  {
83  ceiling = nodes_[1]->evaluate(settings).to_integer();
84 
85  if (nodes_.size() > 2)
86  {
87  update_srand = nodes_[2]->evaluate(settings).to_integer() != 0;
88  }
89  }
90  }
91 
93  "madara::expression::SystemCallRandInt: "
94  "System call rand_int called with %" PRId64 ", %" PRId64 ", %d.\n",
95  floor, ceiling, update_srand);
96 
98  utility::rand_int(floor, ceiling, update_srand));
99 }
100 
101 // accept a visitor
103  madara::expression::Visitor& visitor) const
104 {
105  visitor.visit(*this);
106 }
107 
108 #endif // _MADARA_NO_KARL_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
madara::knowledge::KnowledgeRecord KnowledgeRecord
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:21
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value.
Definition: LeafNode.h:25
Interface for a MADARA system call.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
SystemCallRandInt(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual ~SystemCallRandInt(void)
Destructor.
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.
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
int64_t rand_int(int64_t floor, int64_t ceiling, bool set_seed_to_time)
Returns a random integer between a floor and ceiling.
Definition: Utility.cpp:537