Search in sources :

Example 76 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNRuntimeTest method testCompositeItemDefinition.

@Test
public void testCompositeItemDefinition() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0008-LX-arithmetic.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0008-LX-arithmetic");
    assertThat(dmnModel, notNullValue());
    DMNContext context = DMNFactory.newContext();
    Map loan = new HashMap();
    loan.put("principal", 600000);
    loan.put("rate", 0.0375);
    loan.put("termMonths", 360);
    context.set("loan", loan);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("payment"), is(new BigDecimal("2778.693549432766768088520383236299")));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) Map(java.util.Map) HashMap(java.util.HashMap) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 77 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNRuntimeTest method testDateAllowedValues.

@Test
public void testDateAllowedValues() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("date_allowed_values.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_fbf002a3-615b-4f02-98e4-c28d4676225a", "Error with constraints verification");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext ctx = runtime.newContext();
    Object duration = BuiltInType.DURATION.fromString("P20Y");
    ctx.set("yearsMonth", duration);
    Object dateTime = BuiltInType.DATE_TIME.fromString("2017-05-16T17:58:00.000");
    ctx.set("dateTime", dateTime);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
    DMNContext result = dmnResult.getContext();
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat((Map<String, Object>) result.get("Decision Logic 1"), hasEntry("years and months", duration));
    assertThat((Map<String, Object>) result.get("Decision Logic 1"), hasEntry("Date Time", dateTime));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 78 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNRuntimeTest method testVerifyExtendedKieFEELFunction_today.

@Test
public void testVerifyExtendedKieFEELFunction_today() {
    // DROOLS-2322
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("just_today.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436", "just today");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext emptyContext = DMNFactory.newContext();
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, emptyContext);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("a decision just today").getResult(), notNullValue());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 79 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNContextEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    // when this evaluator is executed, it should either return a Map of key/value pairs
    // where keys are the name of the entries and values are the result of the evaluations
    // OR if a default result is implemented, it should return the result instead
    Map<String, Object> results = new HashMap<>();
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    try {
        for (ContextEntryDef ed : entries) {
            try {
                EvaluatorResult er = ed.getEvaluator().evaluate(eventManager, result);
                if (er.getResultType() == ResultType.SUCCESS) {
                    Object value = er.getResult();
                    if (!ed.getType().isCollection() && value instanceof Collection && ((Collection) value).size() == 1) {
                        // spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
                        // and vice-versa
                        value = ((Collection) value).toArray()[0];
                    }
                    if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
                        if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
                            // checking directly the result type...
                            if (ed.getType() != null && !ed.getType().isAssignableValue(value)) {
                                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, ed.getName(), ed.getType(), value);
                                return new EvaluatorResultImpl(results, ResultType.FAILURE);
                            }
                        } else {
                        // TODO ...will need calculation/inference of function return type.
                        }
                    }
                    results.put(ed.getName(), value);
                    dmnContext.set(ed.getName(), value);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX, ed.getName(), name);
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
            } catch (Exception e) {
                logger.error("Error invoking expression for node '" + name + "'.", e);
                return new EvaluatorResultImpl(results, ResultType.FAILURE);
            }
        }
    } finally {
        result.setContext(previousContext);
    }
    if (results.containsKey(RESULT_ENTRY)) {
        return new EvaluatorResultImpl(results.get(RESULT_ENTRY), ResultType.SUCCESS);
    } else {
        return new EvaluatorResultImpl(results, ResultType.SUCCESS);
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) HashMap(java.util.HashMap) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNContext(org.kie.dmn.api.core.DMNContext) Collection(java.util.Collection) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 80 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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)

Aggregations

DMNContext (org.kie.dmn.api.core.DMNContext)142 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)130 DMNModel (org.kie.dmn.api.core.DMNModel)129 Test (org.junit.Test)127 DMNResult (org.kie.dmn.api.core.DMNResult)123 HashMap (java.util.HashMap)24 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)24 BigDecimal (java.math.BigDecimal)23 ArrayList (java.util.ArrayList)20 List (java.util.List)20 Map (java.util.Map)20 KieServices (org.kie.api.KieServices)19 KieContainer (org.kie.api.runtime.KieContainer)19 DMNMessage (org.kie.dmn.api.core.DMNMessage)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)11 CoreMatchers.is (org.hamcrest.CoreMatchers.is)10 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)10 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)10 Collection (java.util.Collection)8 UUID (java.util.UUID)8