MADARA  3.4.1
SystemCallPow.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <math.h>
5 
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 {
33 
34  if (nodes_.size() == 2)
35  {
36  bool base_can_change = false, power_can_change = false;
37 
38  double base = nodes_[0]->prune(base_can_change).to_double();
39  double power = nodes_[1]->prune(power_can_change).to_double();
40 
41  if (!base_can_change && dynamic_cast<LeafNode*>(nodes_[0]) == 0)
42  {
43  delete nodes_[0];
44  nodes_[0] = new LeafNode(*(this->logger_), base);
45  }
46 
47  if (!power_can_change && dynamic_cast<LeafNode*>(nodes_[1]) == 0)
48  {
49  delete nodes_[1];
50  nodes_[1] = new LeafNode(*(this->logger_), power);
51  }
52 
53  result = knowledge::KnowledgeRecord(pow(base, power));
54 
55  can_change = can_change || base_can_change || power_can_change;
56  }
57  else
58  {
60  "madara::expression::SystemCallPow: "
61  "KARL COMPILE ERROR:"
62  "System call pow requires 2 arguments,"
63  "e.g., #pow (4,2), which would return 16.\n");
64 
66  "madara::expression::SystemCallPow: "
67  "KARL COMPILE ERROR: "
68  "System call pow requires 2 arguments,"
69  "e.g., #pow (4,2), which would return 16.\n");
70  }
71 
72  return result;
73 }
74 
79 {
80  knowledge::KnowledgeRecord return_value;
81 
82  if (nodes_.size() == 2)
83  {
85  "madara::expression::SystemCallPow: "
86  "System call pow is returning the base taken to the power\n");
87 
88  return_value = knowledge::KnowledgeRecord(
89  pow(nodes_[0]->evaluate(settings).to_double(),
90  nodes_[1]->evaluate(settings).to_double()));
91  }
92  else
93  {
95  "madara::expression::SystemCallPow: "
96  "KARL RUNTIME ERROR:"
97  "System call pow requires 2 arguments,"
98  "e.g., #pow (4,2), which would return 16.\n");
99 
101  "madara::expression::SystemCallPow: "
102  "KARL RUNTIME ERROR: "
103  "System call pow requires 2 arguments,"
104  "e.g., #pow (4,2), which would return 16.\n");
105  }
106 
107  return return_value;
108 }
109 
110 // accept a visitor
112  madara::expression::Visitor& visitor) const
113 {
114  visitor.visit(*this);
115 }
116 
117 #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 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.
SystemCallPow(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
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 ~SystemCallPow(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.
double to_double(void) const
converts the value to a float/double.
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