2 #ifndef _FUNCTION_NODE_CPP_ 3 #define _FUNCTION_NODE_CPP_ 5 #ifndef _MADARA_NO_KARL_ 21 #ifdef _MADARA_PYTHON_CALLBACKS_ 23 #include <boost/python/call.hpp> 30 #include "madara_jni.h" 42 name_ (name), context_ (context),
43 function_ (context.retrieve_function (name))
79 for (ComponentNodes::size_type i = 0; i <
nodes_.size (); ++i)
81 bool arg_can_change =
false;
82 result =
nodes_[i]->prune (arg_can_change);
84 if (!arg_can_change && dynamic_cast <LeafNode *> (
nodes_[i]) == 0)
92 std::stringstream buffer;
115 for (ComponentNodes::iterator i =
nodes_.begin (); i !=
nodes_.end ();
118 args[j] = (*i)->evaluate (settings);
127 "Function %s is being called with %d args.\n",
128 this->
name_.c_str (), args.size ());
142 JNIEnv * env = jvm.
env;
148 jclass jvarClass = madara::utility::java::find_class (
149 env,
"ai/madara/knowledge/Variables");
150 jclass jlistClass = madara::utility::java::find_class (
151 env,
"ai/madara/knowledge/KnowledgeList");
153 jmethodID fromPointerCall = env->GetStaticMethodID (jvarClass,
154 "fromPointer",
"(J)Lai/madara/knowledge/Variables;");
155 jobject jvariables = env->CallStaticObjectMethod (jvarClass,
156 fromPointerCall, (jlong) &variables);
159 jmethodID listConstructor = env->GetMethodID(jlistClass,
162 jlongArray ret = env->NewLongArray((jsize)args.size());
163 jlong * tmp =
new jlong [(jsize)args.size()];
165 for (
unsigned int x = 0; x < args.size(); x++)
167 tmp[x] = (jlong) args[x].clone ();
170 env->SetLongArrayRegion(ret, 0, (jsize)args.size(), tmp);
174 jobject jlist = env->NewObject (jlistClass, listConstructor, ret);
180 jmethodID filterMethod = env->GetMethodID (filterClass,
182 "(Lai/madara/knowledge/KnowledgeList;" 183 "Lai/madara/knowledge/Variables;)Lai/madara/knowledge/KnowledgeRecord;");
187 filterMethod, jlist, jvariables);
189 jmethodID getPtrMethod = env->GetMethodID (
190 env->GetObjectClass(jresult),
"getCPtr",
"()J");
191 jlong cptr = env->CallLongMethod (jresult, getPtrMethod);
195 jvm.
env->DeleteLocalRef (jresult);
196 jvm.
env->DeleteLocalRef (filterClass);
197 jvm.
env->DeleteLocalRef (jlist);
198 jvm.
env->DeleteLocalRef (ret);
199 jvm.
env->DeleteLocalRef (jvariables);
200 jvm.
env->DeleteWeakGlobalRef (jlistClass);
201 jvm.
env->DeleteWeakGlobalRef (jvarClass);
205 #ifdef _MADARA_PYTHON_CALLBACKS_ 207 return boost::python::call <madara::knowledge::KnowledgeRecord> (
209 boost::ref (args), boost::ref (variables));
215 "CompositeFunctionNode:" 216 "KARL RUNTIME EXCEPTION: " 217 "Attempt at calling an undefined function\n");
220 "KARL RUNTIME EXCEPTION: " 221 "Attempt at calling an undefined function");
236 visitor.
visit (*
this);
239 #endif // _MADARA_NO_KARL_ This class encapsulates an entry in a KnowledgeBase.
JNIEnv * env
The Java environment.
void deep_copy(const KnowledgeRecord &source)
Creates a deep copy of the knowledge record.
This class encapsulates attaching and detaching to a VM.
knowledge::KnowledgeRecord(* extern_named)(const char *, FunctionArguments &, Variables &)
std::vector< knowledge::KnowledgeRecord * > compiled_args_
virtual madara::knowledge::KnowledgeRecord item(void) const
Returns the printable character of the node.
knowledge::KnowledgeRecord(* extern_unnamed)(FunctionArguments &, Variables &)
knowledge::Function * function_
std::deque< ComponentNode * > ComponentNodes
a vector of Component Nodes
logger::Logger * logger_
handle the context
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.
bool is_python_callable(void) const
virtual ~CompositeFunctionNode(void)
Destructor.
Defines a node that contains a madara::knowledge::KnowledgeRecord::Integer value. ...
std::vector< KnowledgeRecord > FunctionArguments
void set_value(const KnowledgeRecord &new_value)
Sets the value from another KnowledgeRecord, does not copy clock and write_quality.
bool is_uninitialized(void) const
virtual void accept(Visitor &visitor) const
Accepts a visitor subclassed from the Visitor class.
virtual madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings)
Evaluates the expression tree.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
bool is_extern_named(void) const
ThreadSafeContext * context_
Variables context that is directly used by the KaRL engine.
Abstract base class for all visitors to all classes that derive from ComponentNode.
boost::python::object python_function
An exception for unrecoverable KaRL compilation issues.
bool is_java_callable(void) const
CompositeFunctionNode(const std::string &name, madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
KnowledgeRecord * get_record(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge record from the key.
Settings for applying knowledge updates.
virtual void visit(const LeafNode &node)=0
Visit a LeafNode.
knowledge::ThreadSafeContext & context_
void resize(size_t new_size)
resizes an array to a new size
Provides an interface for external functions into the MADARA KaRL variable settings.
bool is_extern_unnamed(void) const
madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Evaluates the expression tree.
madara::expression::ExpressionTree function_contents