MADARA  3.2.3
SystemCallPow.cpp
Go to the documentation of this file.
1 
2 #ifndef _MADARA_NO_KARL_
3 
4 #include <math.h>
5 
10 
11 
14  const ComponentNodes & nodes)
15  : SystemCallNode (context, nodes)
16 {
17 
18 }
19 
20 // Dtor
22 {
23 }
24 
27 {
29 }
30 
36 {
38 
39  if (nodes_.size () == 2)
40  {
41  bool base_can_change = false, power_can_change = false;
42 
43  double base = nodes_[0]->prune (base_can_change).to_double ();
44  double power = nodes_[1]->prune (power_can_change).to_double ();
45 
46  if (!base_can_change && dynamic_cast <LeafNode *> (nodes_[0]) == 0)
47  {
48  delete nodes_[0];
49  nodes_[0] = new LeafNode (*(this->logger_), base);
50  }
51 
52  if (!power_can_change && dynamic_cast <LeafNode *> (nodes_[1]) == 0)
53  {
54  delete nodes_[1];
55  nodes_[1] = new LeafNode (*(this->logger_), power);
56  }
57 
58  result = knowledge::KnowledgeRecord (pow (base, power));
59 
60  can_change = can_change || base_can_change || power_can_change;
61  }
62  else
63  {
65  "madara::expression::SystemCallPow: "
66  "KARL COMPILE ERROR:"
67  "System call pow requires 2 arguments,"
68  "e.g., #pow (4,2), which would return 16.\n");
69 
70  throw KarlException ("madara::expression::SystemCallPow: "
71  "KARL COMPILE ERROR: "
72  "System call pow requires 2 arguments,"
73  "e.g., #pow (4,2), which would return 16.\n");
74  }
75 
76  return result;
77 }
78 
84 {
85  knowledge::KnowledgeRecord return_value;
86 
87  if (nodes_.size () == 2)
88  {
90  "madara::expression::SystemCallPow: "
91  "System call pow is returning the base taken to the power\n");
92 
93  return_value = knowledge::KnowledgeRecord(
94  pow (nodes_[0]->evaluate (settings).to_double (),
95  nodes_[1]->evaluate (settings).to_double ()));
96  }
97  else
98  {
100  "madara::expression::SystemCallPow: "
101  "KARL RUNTIME ERROR:"
102  "System call pow requires 2 arguments,"
103  "e.g., #pow (4,2), which would return 16.\n");
104 
105  throw KarlException ("madara::expression::SystemCallPow: "
106  "KARL RUNTIME ERROR: "
107  "System call pow requires 2 arguments,"
108  "e.g., #pow (4,2), which would return 16.\n");
109  }
110 
111  return return_value;
112 }
113 
114 // accept a visitor
115 void
117 madara::expression::Visitor &visitor) const
118 {
119  visitor.visit (*this);
120 }
121 
122 #endif // _MADARA_NO_KARL_
This class encapsulates an entry in a KnowledgeBase.
virtual ~SystemCallPow(void)
Destructor.
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
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 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
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the value of the node.
#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:91
An exception for unrecoverable KaRL compilation issues.
Definition: KarlException.h:18
Interface for a MADARA system call.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
SystemCallPow(madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.