MADARA  3.1.8
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  "KARL COMPILE ERROR: System call pow requires 2 arguments,"
66  "e.g., #pow (4,2), which would return 16.\n");
67  }
68 
69  return result;
70 }
71 
77 {
78  knowledge::KnowledgeRecord return_value;
79 
80  if (nodes_.size () == 2)
81  {
83  "System call pow is returning the base taken to the power\n");
84 
85  return_value = knowledge::KnowledgeRecord(
86  pow (nodes_[0]->evaluate (settings).to_double (),
87  nodes_[1]->evaluate (settings).to_double ()));
88  }
89  else
90  {
92  "KARL RUNTIME ERROR: System call pow requires 2 arguments,"
93  "e.g., #pow (4,2), which would return 16.\n");
94  }
95 
96  return return_value;
97 }
98 
99 // accept a visitor
100 void
102 madara::expression::Visitor &visitor) const
103 {
104  visitor.visit (*this);
105 }
106 
107 #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: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
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:90
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.