Search in sources :

Example 1 with DMNRuntimeImpl

use of org.kie.dmn.core.impl.DMNRuntimeImpl in project drools by kiegroup.

the class DMNRuntimeTypeCheckTest method testAskTypeCheckInKModuleButOverrideRuntime.

@Test
public void testAskTypeCheckInKModuleButOverrideRuntime() {
    DMNRuntime runtime = getRuntimeWithTypeCheckOption("true", ks.getResources().newClassPathResource("forTypeCheckTest.dmn", this.getClass()));
    ((DMNRuntimeImpl) runtime).setOption(new RuntimeTypeCheckOption(false));
    assertPerformTypeCheck(runtime);
}
Also used : DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Test(org.junit.Test)

Example 2 with DMNRuntimeImpl

use of org.kie.dmn.core.impl.DMNRuntimeImpl in project drools by kiegroup.

the class DMNRuntimeTypeCheckTest method testExplicitDisableTypeCheckInKModuleButOverrideRuntime.

@Test
public void testExplicitDisableTypeCheckInKModuleButOverrideRuntime() {
    DMNRuntime runtime = getRuntimeWithTypeCheckOption("false", ks.getResources().newClassPathResource("forTypeCheckTest.dmn", this.getClass()));
    ((DMNRuntimeImpl) runtime).setOption(new RuntimeTypeCheckOption(true));
    assertPerformTypeCheck(runtime);
}
Also used : DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Test(org.junit.Test)

Example 3 with DMNRuntimeImpl

use of org.kie.dmn.core.impl.DMNRuntimeImpl in project drools by kiegroup.

the class DMNRuntimeTypeCheckTest method testDefaultNoTypeCheckButOverrideRuntime.

@Test
public void testDefaultNoTypeCheckButOverrideRuntime() {
    // do NOT use the DMNRuntimeUtil as that enables typeSafe check override for runtime.
    final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0"), ks.getResources().newClassPathResource("forTypeCheckTest.dmn", this.getClass()));
    DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    ((DMNRuntimeImpl) runtime).setOption(new RuntimeTypeCheckOption(true));
    assertPerformTypeCheck(runtime);
}
Also used : DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 4 with DMNRuntimeImpl

use of org.kie.dmn.core.impl.DMNRuntimeImpl in project drools by kiegroup.

the class DMNRuntimeUtil method typeSafeGetKieRuntime.

private static DMNRuntime typeSafeGetKieRuntime(final KieContainer kieContainer) {
    DMNRuntime dmnRuntime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    ((DMNRuntimeImpl) dmnRuntime).setOption(new RuntimeTypeCheckOption(true));
    return dmnRuntime;
}
Also used : DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) DMNRuntime(org.kie.dmn.api.core.DMNRuntime)

Example 5 with DMNRuntimeImpl

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

Aggregations

DMNRuntimeImpl (org.kie.dmn.core.impl.DMNRuntimeImpl)5 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)4 RuntimeTypeCheckOption (org.kie.dmn.core.compiler.RuntimeTypeCheckOption)4 Test (org.junit.Test)3 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 KieContainer (org.kie.api.runtime.KieContainer)1 DMNContext (org.kie.dmn.api.core.DMNContext)1 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)1 DMNContextImpl (org.kie.dmn.core.impl.DMNContextImpl)1 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)1 FunctionDefinition (org.kie.dmn.model.v1_1.FunctionDefinition)1