use of org.kie.dmn.feel.runtime.functions.CustomFEELFunction in project drools by kiegroup.
the class FunctionDefNode method evaluate.
@Override
public Object evaluate(EvaluationContext ctx) {
List<String> params = formalParameters.stream().map(p -> p.evaluate(ctx)).collect(Collectors.toList());
if (external) {
try {
// creating a simple algorithm to find the method in java
// without using any external libraries in this initial implementation
Map<String, Object> conf = (Map<String, Object>) this.body.evaluate(ctx);
Map<String, Object> java = (Map<String, Object>) conf.get("java");
if (java != null) {
// this is a java function
String clazzName = (String) java.get("class");
String methodSignature = (String) java.get("method signature");
if (clazzName != null && methodSignature != null) {
// might need to explicitly use a classloader here
Class<?> clazz = Class.forName(clazzName);
if (clazz != null) {
String[] mp = parseMethod(methodSignature);
if (mp != null) {
String methodName = mp[0];
String[] paramTypeNames = parseParams(mp[1]);
int numberOfParams = paramTypeNames.length;
if (numberOfParams == params.size()) {
Class[] paramTypes = new Class[numberOfParams];
for (int i = 0; i < numberOfParams; i++) {
paramTypes[i] = getType(paramTypeNames[i]);
}
Method method = clazz.getMethod(methodName, paramTypes);
return new JavaFunction(ANONYMOUS, params, clazz, method);
} else {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.PARAMETER_COUNT_MISMATCH_ON_FUNCTION_DEFINITION, getText())));
return null;
}
}
}
}
}
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.UNABLE_TO_FIND_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText())));
} catch (Exception e) {
ctx.notifyEvt(astEvent(Severity.ERROR, Msg.createMessage(Msg.ERROR_RESOLVING_EXTERNAL_FUNCTION_AS_DEFINED_BY, getText()), e));
}
return null;
} else {
return new CustomFEELFunction(ANONYMOUS, params, body);
}
}
Aggregations