use of org.mule.mvel2.integration.VariableResolverFactory in project drools by kiegroup.
the class MVELCompilationUnit method getFactory.
public VariableResolverFactory getFactory(final Object knowledgeHelper, final Declaration[] prevDecl, final Rule rule, final Tuple tuples, final Object[] otherVars, final InternalWorkingMemory workingMemory, final GlobalResolver globals) {
VariableResolverFactory factory = createFactory();
updateFactory(knowledgeHelper, prevDecl, rule, null, knowledgeHelper, tuples, otherVars, workingMemory, globals, factory);
return factory;
}
use of org.mule.mvel2.integration.VariableResolverFactory in project drools by kiegroup.
the class MVELEnabledExpression method getValue.
public boolean getValue(final Tuple tuple, final Declaration[] declrs, final RuleImpl rule, final WorkingMemory workingMemory) {
VariableResolverFactory factory = unit.getFactory(null, declrs, rule, null, (LeftTuple) tuple, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver());
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
return ((Boolean) MVEL.executeExpression(this.expr, null, factory)).booleanValue();
}
use of org.mule.mvel2.integration.VariableResolverFactory in project drools by kiegroup.
the class MVELEvalExpression method evaluate.
public boolean evaluate(final Tuple tuple, final Declaration[] requiredDeclarations, final WorkingMemory workingMemory, final Object context) throws Exception {
VariableResolverFactory factory = (VariableResolverFactory) context;
unit.updateFactory(null, (LeftTuple) tuple, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
final Boolean result = (Boolean) MVEL.executeExpression(this.expr, null, factory);
return result.booleanValue();
}
use of org.mule.mvel2.integration.VariableResolverFactory in project mvel by mvel.
the class ForNode method getReducedValueAccelerated.
public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
VariableResolverFactory ctxFactory = indexAlloc ? factory : new MapVariableResolverFactory(new HashMap<String, Object>(1), factory);
Object v;
for (initializer.getValue(ctx, thisValue, ctxFactory); (Boolean) condition.getValue(ctx, thisValue, ctxFactory); after.getValue(ctx, thisValue, ctxFactory)) {
v = compiledBlock.getValue(ctx, thisValue, ctxFactory);
if (ctxFactory.tiltFlag())
return v;
}
return null;
}
use of org.mule.mvel2.integration.VariableResolverFactory in project mvel by mvel.
the class PropertyAccessor method getMethod.
/**
* Find an appropriate method, execute it, and return it's response.
*
* @param ctx -
* @param name -
* @return -
*/
@SuppressWarnings({ "unchecked" })
private Object getMethod(Object ctx, String name) {
int _start = cursor;
String tk = cursor != end && property[cursor] == '(' && ((cursor = balancedCapture(property, cursor, '(')) - _start) > 1 ? new String(property, _start + 1, cursor - _start - 1) : "";
cursor++;
Object[] args;
if (tk.length() == 0) {
args = ParseTools.EMPTY_OBJ_ARR;
} else {
List<char[]> subtokens = parseParameterList(tk.toCharArray(), 0, -1);
args = new Object[subtokens.size()];
for (int i = 0; i < subtokens.size(); i++) {
args[i] = eval(subtokens.get(i), thisReference, variableFactory);
}
}
if (first && variableFactory != null && variableFactory.isResolveable(name)) {
Object ptr = variableFactory.getVariableResolver(name).getValue();
if (ptr instanceof Method) {
ctx = ((Method) ptr).getDeclaringClass();
name = ((Method) ptr).getName();
} else if (ptr instanceof MethodStub) {
ctx = ((MethodStub) ptr).getClassReference();
name = ((MethodStub) ptr).getMethodName();
} else if (ptr instanceof FunctionInstance) {
((FunctionInstance) ptr).getFunction().checkArgumentCount(args.length);
return ((FunctionInstance) ptr).call(null, thisReference, variableFactory, args);
} else {
throw new OptimizationFailure("attempt to optimize a method call for a reference that does not point to a method: " + name + " (reference is type: " + (ctx != null ? ctx.getClass().getName() : null) + ")");
}
first = false;
}
if (ctx == null)
throw new CompileException("no such method or function: " + name, property, cursor);
/**
* If the target object is an instance of java.lang.Class itself then do not
* adjust the Class scope target.
*/
Class cls = currType != null ? currType : ((ctx instanceof Class ? (Class) ctx : ctx.getClass()));
currType = null;
if (cls == Proto.ProtoInstance.class) {
return ((Proto.ProtoInstance) ctx).get(name).call(null, thisReference, variableFactory, args);
}
/**
* Check to see if we have already cached this method;
*/
Object[] cache = checkMethodCache(cls, createSignature(name, tk));
Method m;
Class[] parameterTypes;
if (cache != null) {
m = (Method) cache[0];
parameterTypes = (Class[]) cache[1];
} else {
m = null;
parameterTypes = null;
}
/**
* If we have not cached the method then we need to go ahead and try to resolve it.
*/
if (m == null) {
/**
* Try to find an instance method from the class target.
*/
if ((m = getBestCandidate(args, name, cls, cls.getMethods(), false)) != null) {
addMethodCache(cls, createSignature(name, tk), m);
parameterTypes = m.getParameterTypes();
}
if (m == null) {
/**
* If we didn't find anything, maybe we're looking for the actual java.lang.Class methods.
*/
if ((m = getBestCandidate(args, name, cls, cls.getDeclaredMethods(), false)) != null) {
addMethodCache(cls, createSignature(name, tk), m);
parameterTypes = m.getParameterTypes();
}
}
}
// If we didn't find anything and the declared class is different from the actual one try also with the actual one
if (m == null && cls != ctx.getClass() && !(ctx instanceof Class)) {
cls = ctx.getClass();
if ((m = getBestCandidate(args, name, cls, cls.getDeclaredMethods(), false)) != null) {
addMethodCache(cls, createSignature(name, tk), m);
parameterTypes = m.getParameterTypes();
}
}
if (ctx instanceof PrototypalFunctionInstance) {
final VariableResolverFactory funcCtx = ((PrototypalFunctionInstance) ctx).getResolverFactory();
Object prop = funcCtx.getVariableResolver(name).getValue();
if (prop instanceof PrototypalFunctionInstance) {
return ((PrototypalFunctionInstance) prop).call(ctx, thisReference, new InvokationContextFactory(variableFactory, funcCtx), args);
}
}
if (m == null) {
StringAppender errorBuild = new StringAppender();
for (int i = 0; i < args.length; i++) {
errorBuild.append(args[i] != null ? args[i].getClass().getName() : null);
if (i < args.length - 1)
errorBuild.append(", ");
}
if ("size".equals(name) && args.length == 0 && cls.isArray()) {
return getLength(ctx);
}
throw new PropertyAccessException("unable to resolve method: " + cls.getName() + "." + name + "(" + errorBuild.toString() + ") [arglength=" + args.length + "]", property, st, pCtx);
} else {
for (int i = 0; i < args.length; i++) {
args[i] = convert(args[i], paramTypeVarArgsSafe(parameterTypes, i, m.isVarArgs()));
}
/**
* Invoke the target method and return the response.
*/
currType = toNonPrimitiveType(m.getReturnType());
try {
return m.invoke(ctx, normalizeArgsForVarArgs(parameterTypes, args, m.isVarArgs()));
} catch (IllegalAccessException e) {
try {
addMethodCache(cls, createSignature(name, tk), (m = getWidenedTarget(m)));
return m.invoke(ctx, args);
} catch (Exception e2) {
throw new PropertyAccessException("unable to invoke method: " + name, property, cursor, e2, pCtx);
}
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new PropertyAccessException("unable to invoke method: " + name, property, cursor, e, pCtx);
}
}
}
Aggregations