use of org.kie.dmn.feel.lang.EvaluationContext in project drools by kiegroup.
the class DecisionTableFunction method toDecisionRule.
/**
* Convert row to DTDecisionRule
* @param mainCtx the main context is used to identify the hosted FEELEventManager
* @param embeddedFEEL a possibly cached embedded FEEL to compile the output expression, error will be reported up to the mainCtx
* @param index
* @param rule
* @param inputSize
* @return
*/
private static DTDecisionRule toDecisionRule(EvaluationContext mainCtx, FEEL embeddedFEEL, int index, List<?> rule, int inputSize) {
// TODO should be check indeed block of inputSize n inputs, followed by block of outputs.
DTDecisionRule dr = new DTDecisionRule(index);
for (int i = 0; i < rule.size(); i++) {
Object o = rule.get(i);
if (i < inputSize) {
dr.getInputEntry().add(toUnaryTest(mainCtx, o));
} else {
FEELEventListener ruleListener = event -> mainCtx.notifyEvt(() -> new FEELEventBase(event.getSeverity(), Msg.createMessage(Msg.ERROR_COMPILE_EXPR_DT_FUNCTION_RULE_IDX, index + 1, event.getMessage()), event.getSourceException()));
embeddedFEEL.addListener(ruleListener);
CompiledExpression compiledExpression = embeddedFEEL.compile((String) o, embeddedFEEL.newCompilerContext());
dr.getOutputEntry().add(compiledExpression);
embeddedFEEL.removeListener(ruleListener);
}
}
return dr;
}
use of org.kie.dmn.feel.lang.EvaluationContext 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