use of org.kie.dmn.feel.lang.CompiledExpression 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.CompiledExpression in project drools by kiegroup.
the class FEELImpl method evaluate.
@Override
public Object evaluate(String expression, EvaluationContext ctx) {
CompilerContext compilerCtx = newCompilerContext(ctx.getListeners());
Map<String, Object> inputVariables = ctx.getAllValues();
if (inputVariables != null) {
inputVariables.entrySet().stream().forEach(e -> compilerCtx.addInputVariable(e.getKey(), e.getValue()));
}
CompiledExpression expr = compile(expression, compilerCtx);
return evaluate(expr, ctx);
}
use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.
the class FEELImpl method evaluate.
@Override
public Object evaluate(String expression, Map<String, Object> inputVariables) {
CompilerContext ctx = newCompilerContext();
if (inputVariables != null) {
inputVariables.entrySet().stream().forEach(e -> ctx.addInputVariable(e.getKey(), e.getValue()));
}
CompiledExpression expr = compile(expression, ctx);
if (inputVariables == null) {
return evaluate(expr, EMPTY_INPUT);
} else {
return evaluate(expr, inputVariables);
}
}
use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.
the class CompileEvaluateTest method testExternalFnMissingMethod.
@Test
public void testExternalFnMissingMethod() {
CompiledExpression compiledExpression = feel.compile("{ maximum : function( v1, v2 ) external { java : { class : \"" + Math.class.getCanonicalName() + "\", method signature: \"max(int,long)\" } }, the max : maximum( 10, 20 ) }.the max", feel.newCompilerContext());
Object result = feel.evaluate(compiledExpression, new HashMap<>());
assertThat(errors).anyMatch(fe -> fe.getMessage().contains("max(int,int)") && fe.getMessage().contains("max(long,long)"));
}
use of org.kie.dmn.feel.lang.CompiledExpression in project drools by kiegroup.
the class CompileEvaluateTest method testExternalFnMissingClass.
@Test
public void testExternalFnMissingClass() {
CompiledExpression compiledExpression = feel.compile("{ maximum : function( v1, v2 ) external { java : { class : \"java.lang.Meth\", method signature: \"max(long,long)\" } }, the max : maximum( 10, 20 ) }.the max", feel.newCompilerContext());
Object result = feel.evaluate(compiledExpression, new HashMap<>());
assertThat(errors).anyMatch(fe -> fe.getMessage().contains("java.lang.Meth"));
}
Aggregations