2 #ifndef _FUNCTION_NODE_CPP_ 3 #define _FUNCTION_NODE_CPP_ 5 #ifndef _MADARA_NO_KARL_ 20 #ifdef _MADARA_PYTHON_CALLBACKS_ 22 #include <boost/python/call.hpp> 29 #include "madara_jni.h" 41 name_ (name), context_ (context),
42 function_ (context.retrieve_function (name))
78 for (ComponentNodes::size_type i = 0; i <
nodes_.size (); ++i)
80 bool arg_can_change =
false;
81 result =
nodes_[i]->prune (arg_can_change);
83 if (!arg_can_change && dynamic_cast <LeafNode *> (
nodes_[i]) == 0)
91 std::stringstream buffer;
114 for (ComponentNodes::iterator i =
nodes_.begin (); i !=
nodes_.end ();
117 args[j] = (*i)->evaluate (settings);
126 "Function %s is being called with %d args.\n",
127 this->
name_.c_str (), args.size ());
141 JNIEnv * env = jvm.
env;
147 jclass jvarClass = madara::utility::java::find_class (
148 env,
"com/madara/Variables");
149 jclass jlistClass = madara::utility::java::find_class (
150 env,
"com/madara/KnowledgeList");
152 jmethodID fromPointerCall = env->GetStaticMethodID (jvarClass,
153 "fromPointer",
"(J)Lcom/madara/Variables;");
154 jobject jvariables = env->CallStaticObjectMethod (jvarClass,
155 fromPointerCall, (jlong) &variables);
158 jmethodID listConstructor = env->GetMethodID(jlistClass,
161 jlongArray ret = env->NewLongArray((jsize)args.size());
162 jlong * tmp =
new jlong [(jsize)args.size()];
164 for (
unsigned int x = 0; x < args.size(); x++)
166 tmp[x] = (jlong) args[x].clone ();
169 env->SetLongArrayRegion(ret, 0, (jsize)args.size(), tmp);
173 jobject jlist = env->NewObject (jlistClass, listConstructor, ret);
176 jclass filterClass = env->GetObjectClass(
function_->java_object);
179 jmethodID filterMethod = env->GetMethodID (filterClass,
181 "(Lcom/madara/KnowledgeList;Lcom/madara/Variables;)Lcom/madara/KnowledgeRecord;");
184 jobject jresult = env->CallObjectMethod (
function_->java_object,
185 filterMethod, jlist, jvariables);
187 jmethodID getPtrMethod = env->GetMethodID (
188 env->GetObjectClass(jresult),
"getCPtr",
"()J");
189 jlong cptr = env->CallLongMethod (jresult, getPtrMethod);
191 bool do_delete =
true;
193 for (
unsigned int x = 0; x < args.size(); x++)
195 if (cptr == (jlong)&(args[x]))
204 jvm.
env->DeleteLocalRef (jresult);
205 jvm.
env->DeleteLocalRef (filterClass);
206 jvm.
env->DeleteLocalRef (jlist);
207 jvm.
env->DeleteLocalRef (ret);
208 jvm.
env->DeleteLocalRef (jvariables);
209 jvm.
env->DeleteWeakGlobalRef (jlistClass);
210 jvm.
env->DeleteWeakGlobalRef (jvarClass);
217 #ifdef _MADARA_PYTHON_CALLBACKS_ 218 else if (
function_->is_python_callable ())
219 return boost::python::call <madara::knowledge::KnowledgeRecord> (
221 boost::ref (args), boost::ref (variables));
237 visitor.
visit (*
this);
240 #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.
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.
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.
KnowledgeRecord * get_record(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a knowledge record from the key.
CompositeFunctionNode(const std::string &name, madara::knowledge::ThreadSafeContext &context, const ComponentNodes &nodes)
Constructor.
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