Search in sources :

Example 1 with EvaluationContextImpl

use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.

the class DMNFEELHelper method valueMatchesInUnaryTests.

public static boolean valueMatchesInUnaryTests(List<UnaryTest> unaryTests, Object value, DMNContext dmnContext) {
    FEELEventListenersManager manager = new FEELEventListenersManager();
    FEELEventsListenerImpl listener = new FEELEventsListenerImpl();
    manager.addListener(listener);
    EvaluationContextImpl ctx = new EvaluationContextImpl(manager);
    try {
        ctx.enterFrame();
        if (dmnContext != null) {
            // need to set the values for in context variables...
            for (Map.Entry<String, Object> entry : dmnContext.getAll().entrySet()) {
                ctx.setValue(entry.getKey(), entry.getValue());
            }
        }
        for (UnaryTest t : unaryTests) {
            try {
                Boolean applyT = t.apply(ctx, value);
                // the unary test above can actually return null, so we have to handle it here
                if (applyT == null) {
                    return false;
                } else if (applyT) {
                    return true;
                }
            } catch (Throwable e) {
                StringBuilder message = new StringBuilder();
                for (FEELEvent feelEvent : listener.getFeelEvents()) {
                    message.append(feelEvent.getMessage());
                    message.append("\n");
                }
                throw new RuntimeException(message.toString(), e);
            }
        }
    } finally {
        ctx.exitFrame();
    }
    return false;
}
Also used : FEELEventListenersManager(org.kie.dmn.feel.lang.impl.FEELEventListenersManager) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) HashMap(java.util.HashMap) Map(java.util.Map) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent)

Example 2 with EvaluationContextImpl

use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.

the class DMNDTExpressionEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager dmrem, DMNResult dmnr) {
    final List<FEELEvent> events = new ArrayList<>();
    DMNResultImpl result = (DMNResultImpl) dmnr;
    EventResults r = null;
    try {
        DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecisionTable(dmrem, node.getName(), dt.getName(), result);
        List<String> paramNames = dt.getParameterNames().get(0);
        Object[] params = new Object[paramNames.size()];
        EvaluationContextImpl ctx = feel.newEvaluationContext(Arrays.asList(events::add), Collections.emptyMap());
        ctx.setPerformRuntimeTypeCheck(((DMNRuntimeImpl) dmrem.getRuntime()).performRuntimeTypeCheck(result.getModel()));
        ctx.enterFrame();
        // need to set the values for in context variables...
        for (Map.Entry<String, Object> entry : result.getContext().getAll().entrySet()) {
            ctx.setValue(entry.getKey(), entry.getValue());
        }
        for (int i = 0; i < params.length; i++) {
            EvaluationContextImpl evalCtx = feel.newEvaluationContext(Arrays.asList(events::add), Collections.emptyMap());
            evalCtx.setValues(result.getContext().getAll());
            params[i] = feel.evaluate(paramNames.get(i), evalCtx);
            ctx.setValue(paramNames.get(i), params[i]);
        }
        Object dtr = dt.invoke(ctx, params).cata(e -> {
            events.add(e);
            return null;
        }, Function.identity());
        // since ctx is a local variable that will be discarded, no need for a try/finally,
        // but still wanted to match the enter/exit frame for future maintainability purposes
        ctx.exitFrame();
        r = processEvents(events, dmrem, result);
        return new EvaluatorResultImpl(dtr, r.hasErrors ? ResultType.FAILURE : ResultType.SUCCESS);
    } finally {
        DMNRuntimeEventManagerUtils.fireAfterEvaluateDecisionTable(dmrem, node.getName(), dt.getName(), result, (r != null ? r.matchedRules : null), (r != null ? r.fired : null));
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) ArrayList(java.util.ArrayList) EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map)

Example 3 with EvaluationContextImpl

use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.

the class DMNInvocationEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    final List<FEELEvent> events = new ArrayList<>();
    DMNResultImpl result = (DMNResultImpl) dmnr;
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    Object invocationResult = null;
    try {
        FEELFunction function = this.functionLocator.apply(previousContext, functionName);
        if (function == null) {
            // check if it is a configured/built-in function
            Object r = null;
            if (feel != null) {
                r = ((FEELImpl) feel).newEvaluationContext(Collections.emptyList(), Collections.emptyMap()).getValue(functionName);
            } else {
                r = RootExecutionFrame.INSTANCE.getValue(functionName);
            }
            if (r != null && r instanceof FEELFunction) {
                function = (FEELFunction) r;
            }
        }
        if (function == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.FUNCTION_NOT_FOUND, functionName, nodeName);
            return new EvaluatorResultImpl(null, ResultType.FAILURE);
        }
        Object[] namedParams = new Object[parameters.size()];
        int index = 0;
        for (ActualParameter param : parameters) {
            try {
                EvaluatorResult value = param.expression.evaluate(eventManager, result);
                if (value.getResultType() == ResultType.SUCCESS) {
                    namedParams[index++] = new NamedParameter(param.name, value.getResult());
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_PARAM_FOR_INVOCATION_ON_NODE, param.name, functionName, nodeName);
                    return new EvaluatorResultImpl(null, ResultType.FAILURE);
                }
            } catch (Exception e) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_INVOKING_PARAM_EXPR_FOR_PARAM_ON_NODE, param.name, nodeName);
                return new EvaluatorResultImpl(null, ResultType.FAILURE);
            }
        }
        FEELEventListenersManager listenerMgr = new FEELEventListenersManager();
        listenerMgr.addListener(events::add);
        EvaluationContextImpl ctx = new EvaluationContextImpl(listenerMgr, eventManager.getRuntime());
        invocationResult = function.invokeReflectively(ctx, namedParams);
        boolean hasErrors = hasErrors(events, eventManager, result);
        return new EvaluatorResultImpl(invocationResult, hasErrors ? ResultType.FAILURE : ResultType.SUCCESS);
    } catch (Throwable t) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, t, null, Msg.ERR_INVOKING_FUNCTION_ON_NODE, functionName, nodeName);
        return new EvaluatorResultImpl(null, ResultType.FAILURE);
    } finally {
        result.setContext(previousContext);
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) NamedParameter(org.kie.dmn.feel.lang.impl.NamedParameter) ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) FEELEventListenersManager(org.kie.dmn.feel.lang.impl.FEELEventListenersManager) EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) FEELImpl(org.kie.dmn.feel.lang.impl.FEELImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent)

Example 4 with EvaluationContextImpl

use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.

the class DMNCompilerTest method testItemDefAllowedValuesString.

@Test
public void testItemDefAllowedValuesString() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0003-input-data-string-allowed-values.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0003-input-data-string-allowed-values");
    assertThat(dmnModel, notNullValue());
    ItemDefNode itemDef = dmnModel.getItemDefinitionByName("tEmploymentStatus");
    assertThat(itemDef.getName(), is("tEmploymentStatus"));
    assertThat(itemDef.getId(), is(nullValue()));
    DMNType type = itemDef.getType();
    assertThat(type, is(notNullValue()));
    assertThat(type.getName(), is("tEmploymentStatus"));
    assertThat(type.getId(), is(nullValue()));
    assertThat(type, is(instanceOf(SimpleTypeImpl.class)));
    SimpleTypeImpl feelType = (SimpleTypeImpl) type;
    EvaluationContext ctx = new EvaluationContextImpl(null);
    assertThat(feelType.getFeelType(), is(BuiltInType.STRING));
    assertThat(feelType.getAllowedValuesFEEL().size(), is(4));
    assertThat(feelType.getAllowedValuesFEEL().get(0).apply(ctx, "UNEMPLOYED"), is(true));
    assertThat(feelType.getAllowedValuesFEEL().get(1).apply(ctx, "EMPLOYED"), is(true));
    assertThat(feelType.getAllowedValuesFEEL().get(2).apply(ctx, "SELF-EMPLOYED"), is(true));
    assertThat(feelType.getAllowedValuesFEEL().get(3).apply(ctx, "STUDENT"), is(true));
}
Also used : EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) SimpleTypeImpl(org.kie.dmn.core.impl.SimpleTypeImpl) ItemDefNode(org.kie.dmn.api.core.ast.ItemDefNode) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) DMNType(org.kie.dmn.api.core.DMNType) Test(org.junit.Test)

Aggregations

EvaluationContextImpl (org.kie.dmn.feel.lang.impl.EvaluationContextImpl)4 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)2 FEELEventListenersManager (org.kie.dmn.feel.lang.impl.FEELEventListenersManager)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 DMNContext (org.kie.dmn.api.core.DMNContext)1 DMNModel (org.kie.dmn.api.core.DMNModel)1 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)1 DMNType (org.kie.dmn.api.core.DMNType)1 ItemDefNode (org.kie.dmn.api.core.ast.ItemDefNode)1 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)1 DMNContextImpl (org.kie.dmn.core.impl.DMNContextImpl)1 SimpleTypeImpl (org.kie.dmn.core.impl.SimpleTypeImpl)1 EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)1 FEELImpl (org.kie.dmn.feel.lang.impl.FEELImpl)1 NamedParameter (org.kie.dmn.feel.lang.impl.NamedParameter)1 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)1