Search in sources :

Example 16 with CompiledExpression

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;
}
Also used : IntStream(java.util.stream.IntStream) FEEL(org.kie.dmn.feel.FEEL) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Logger(org.slf4j.Logger) DTOutputClause(org.kie.dmn.feel.runtime.decisiontables.DTOutputClause) HitPolicy(org.kie.dmn.feel.runtime.decisiontables.HitPolicy) LoggerFactory(org.slf4j.LoggerFactory) DTInputClause(org.kie.dmn.feel.runtime.decisiontables.DTInputClause) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) GwtIncompatible(org.kie.dmn.model.api.GwtIncompatible) Range(org.kie.dmn.feel.runtime.Range) ArrayList(java.util.ArrayList) List(java.util.List) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) Msg(org.kie.dmn.feel.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 17 with CompiledExpression

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);
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 18 with CompiledExpression

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);
    }
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 19 with CompiledExpression

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)"));
}
Also used : CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Example 20 with CompiledExpression

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"));
}
Also used : CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Test(org.junit.Test)

Aggregations

CompiledExpression (org.kie.dmn.feel.lang.CompiledExpression)23 CompilerContext (org.kie.dmn.feel.lang.CompilerContext)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)7 MapBackedType (org.kie.dmn.feel.lang.impl.MapBackedType)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collections (java.util.Collections)3 Collectors (java.util.stream.Collectors)3 DMNType (org.kie.dmn.api.core.DMNType)3 DMNLiteralExpressionEvaluator (org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator)3 Map (java.util.Map)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Collectors.toList (java.util.stream.Collectors.toList)2 QName (javax.xml.namespace.QName)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2