MADARA  3.2.3
CompositeConstArray.cpp
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 #ifndef _FUNCTION_NODE_CPP_
3 #define _FUNCTION_NODE_CPP_
4 
5 #ifndef _MADARA_NO_KARL_
6 
7 #include <iostream>
8 #include <sstream>
9 
16 
17 
20 
21 #ifdef _MADARA_PYTHON_CALLBACKS_
22 
23  #include <boost/python/call.hpp>
24 
25 #endif
26 
27 #ifdef _MADARA_JAVA_
28 
29 #include "madara_jni.h"
30 
31 #endif
32 
33 // Ctor
34 
37  const ComponentNodes & nodes)
38 : madara::expression::CompositeTernaryNode (logger, nodes)
39 {
40 
41 }
42 
43 // Dtor
45 {
46 }
47 
50 {
52  return record;
53 }
54 
60 {
61  can_change = false;
62 
65  args.resize (nodes_.size ());
66 
67  bool is_double (false);
68  ComponentNodes::size_type i = 0;
69 
70  for (; i < nodes_.size (); ++i)
71  {
72  bool arg_can_change = false;
73  args[i] = nodes_[i]->prune (arg_can_change);
74 
75  // we cannot initialize the array until we know if there are any doubles
76  if (args[i].type () == knowledge::KnowledgeRecord::DOUBLE)
77  {
78  is_double = true;
79  }
80 
81  if (!arg_can_change && dynamic_cast <LeafNode *> (nodes_[i]) == 0)
82  {
83  delete nodes_[i];
84  nodes_[i] = new LeafNode (*(this->logger_), result);
85  }
86  else if (arg_can_change)
87  {
88  can_change = true;
89  }
90  }
91 
92  // if we are looking at a static const array, then leafize it
93  if (i > 0)
94  {
95  result.clear_value ();
96 
97  if (is_double)
98  result.set_index (i - 1, args[i - 1].to_double ());
99  else
100  result.set_index (i - 1, args[i - 1].to_integer ());
101 
102  for (i = 0; i < args.size () - 1; ++i)
103  {
104  if (is_double)
105  result.set_index (i, args[i].to_double ());
106  else
107  result.set_index (i, args[i].to_integer ());
108  }
109  }
110  else
111  {
113  "KARL COMPILE ERROR: Array initialized with no elements\n");
114  }
115 
116  return result;
117 }
118 
124 {
127 
128  args.resize (nodes_.size ());
129 
130  bool is_double (false);
131 
132  int j = 0;
133 
134  for (ComponentNodes::iterator i = nodes_.begin (); i != nodes_.end ();
135  ++i, ++j)
136  {
137  args[j] = (*i)->evaluate (settings);
138 
139  // we cannot initialize the array until we know if there are any doubles
140  if (args[j].type () == knowledge::KnowledgeRecord::DOUBLE)
141  {
142  is_double = true;
143  }
144  else if (args[j].type () != knowledge::KnowledgeRecord::INTEGER)
145  {
146  args[j] = knowledge::KnowledgeRecord ((*i)->evaluate (settings).to_integer ());
147  }
148  }
149 
150  // check if we have any args or if this was a poorly done array
151  if (args.size () > 0)
152  {
153  // set last element first so there is only one resize
154  if (is_double)
155  result.set_index (j - 1, args[j - 1].to_double ());
156  else
157  result.set_index (j - 1, args[j - 1].to_integer ());
158 
159  for (size_t i = 0; i < args.size () - 1; ++i)
160  {
161  if (is_double)
162  result.set_index (i, args[i].to_double ());
163  else
164  result.set_index (i, args[i].to_integer ());
165  }
166  }
167 
168  return result;
169 }
170 
171 // accept a visitor
172 void
174 {
175  visitor.visit (*this);
176 }
177 
178 #endif // _MADARA_NO_KARL_
179 
180 #endif /* _FUNCTION_NODE_CPP_ */
This class encapsulates an entry in a KnowledgeBase.
madara::knowledge::KnowledgeRecord KnowledgeRecord
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
void clear_value(void) noexcept
clears any dynamic values.
logger::Logger * logger_
handle the context
Definition: ComponentNode.h:97
virtual madara::knowledge::KnowledgeRecord prune(bool &can_change)
Prunes the expression tree of unnecessary nodes.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
Definition: LeafNode.h:23
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:11
std::vector< KnowledgeRecord > FunctionArguments
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:88
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32
virtual ~CompositeConstArray(void)
Destructor.
Abstract base class for all visitors to all classes that derive from ComponentNode.
Definition: Visitor.h:91
void set_index(size_t index, T value)
sets the value at the index to the specified value.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
Copyright (c) 2015 Carnegie Mellon University.
void resize(size_t new_size)
resizes an array to a new size
CompositeConstArray(logger::Logger &logger, const ComponentNodes &nodes)
Constructor.