Search in sources :

Example 26 with DMNContext

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

the class SignavioTest method assertStartsWithAnA.

private void assertStartsWithAnA(final DMNRuntime runtime, final String testString, final boolean startsWithAnA) {
    DMNContext context = runtime.newContext();
    context.set("surname", testString);
    DMNModel model0 = runtime.getModels().get(0);
    DMNResult evaluateAll = runtime.evaluateAll(model0, context);
    evaluateAll.getMessages().forEach(System.out::println);
    assertFalse(evaluateAll.getMessages().toString(), evaluateAll.hasErrors());
    assertEquals(startsWithAnA, evaluateAll.getContext().get("startsWithAnA"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 27 with DMNContext

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

the class DMNRuntimeImpl method evaluateBKM.

private void evaluateBKM(DMNContext context, DMNResultImpl result, BusinessKnowledgeModelNode b, boolean typeCheck) {
    BusinessKnowledgeModelNodeImpl bkm = (BusinessKnowledgeModelNodeImpl) b;
    if (isNodeValueDefined(result, bkm)) {
        // TODO: do we need to check if the defined variable is a function as it should?
        return;
    }
    // TODO: do we need to check/resolve dependencies?
    if (bkm.getEvaluator() == null) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, bkm.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_BKM, getIdentifier(bkm));
        return;
    }
    try {
        DMNRuntimeEventManagerUtils.fireBeforeEvaluateBKM(eventManager, bkm, result);
        for (DMNNode dep : bkm.getDependencies().values()) {
            if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(bkm), getIdentifier(dep), MsgUtil.clipString(result.getContext().get(dep.getName()).toString(), 50), ((DMNBaseNode) dep).getType());
                return;
            }
            if (!isNodeValueDefined(result, dep)) {
                if (dep instanceof BusinessKnowledgeModelNode) {
                    evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getIdentifier(dep), getIdentifier(bkm));
                    return;
                }
            }
        }
        EvaluatorResult er = bkm.getEvaluator().evaluate(this, result);
        if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
            FEELFunction resultFn = (FEELFunction) er.getResult();
            if (bkm.getModelNamespace().equals(result.getModel().getNamespace())) {
                // TODO check of the return type will need calculation/inference of function return type.
                result.getContext().set(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
            } else {
                DMNModelImpl model = (DMNModelImpl) result.getModel();
                Optional<String> importAlias = model.getImportAliasFor(bkm.getModelNamespace(), bkm.getModelName());
                Map<String, Object> aliasContext = (Map) result.getContext().getAll().computeIfAbsent(importAlias.get(), x -> new LinkedHashMap<>());
                aliasContext.put(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
            }
        }
    } catch (Throwable t) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, t, null, Msg.ERROR_EVAL_BKM_NODE, getIdentifier(bkm), t.getMessage());
    } finally {
        DMNRuntimeEventManagerUtils.fireAfterEvaluateBKM(eventManager, bkm, result);
    }
}
Also used : ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNPackage(org.kie.dmn.api.core.DMNPackage) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) DMNOption(org.kie.dmn.core.compiler.DMNOption) EVALUATING(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.EVALUATING) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DMNModel(org.kie.dmn.api.core.DMNModel) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Map(java.util.Map) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) DMNProfile(org.kie.dmn.core.compiler.DMNProfile) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNFactory(org.kie.dmn.core.api.DMNFactory) Logger(org.slf4j.Logger) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) SKIPPED(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.SKIPPED) Collection(java.util.Collection) Set(java.util.Set) FAILED(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.FAILED) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) List(java.util.List) KieRuntime(org.kie.api.runtime.KieRuntime) DMNContext(org.kie.dmn.api.core.DMNContext) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) Msg(org.kie.dmn.core.util.Msg) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) LinkedHashMap(java.util.LinkedHashMap) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 28 with DMNContext

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

the class DMNAssemblerTest method testStrictMode.

@Test
public void testStrictMode() {
    System.setProperty("org.kie.dmn.strictConformance", "true");
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("strictMode.dmn", this.getClass());
    DMNModel model = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ecf4ea54-2abc-4e2f-a101-4fe14e356a46", "strictMode");
    DMNContext ctx = runtime.newContext();
    ctx.set("timestring", "2016-12-20T14:30:22z");
    DMNResult result = runtime.evaluateAll(model, ctx);
    assertNull(result.getDecisionResultByName("time").getResult());
}
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 29 with DMNContext

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

the class DMNAssemblerTest method testStrictModeProp.

@Test
public void testStrictModeProp() {
    final KieServices services = KieServices.Factory.get();
    final KieFileSystem fileSystem = services.newKieFileSystem();
    KieModuleModel moduleModel = services.newKieModuleModel();
    moduleModel.setConfigurationProperty("org.kie.dmn.strictConformance", "true");
    fileSystem.writeKModuleXML(moduleModel.toXML());
    fileSystem.write(services.getResources().newClassPathResource("strictMode.dmn", this.getClass()));
    services.newKieBuilder(fileSystem).buildAll();
    final KieContainer container = services.newKieContainer(services.getRepository().getDefaultReleaseId());
    DMNRuntime runtime = container.newKieSession().getKieRuntime(DMNRuntime.class);
    DMNModel model = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ecf4ea54-2abc-4e2f-a101-4fe14e356a46", "strictMode");
    DMNContext ctx = runtime.newContext();
    ctx.set("timestring", "2016-12-20T14:30:22z");
    DMNResult result = runtime.evaluateAll(model, ctx);
    assertNull(result.getDecisionResultByName("time").getResult());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DMNContext(org.kie.dmn.api.core.DMNContext) KieServices(org.kie.api.KieServices) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 30 with DMNContext

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

the class DMNCompilerTest method testImport.

@Test
public void testImport() {
    System.out.println(null instanceof Definitions);
    DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("Importing_Model.dmn", this.getClass(), "Imported_Model.dmn");
    DMNModel importedModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f27bb64b-6fc7-4e1f-9848-11ba35e0df36", "Imported Model");
    assertThat(importedModel, notNullValue());
    for (DMNMessage message : importedModel.getMessages()) {
        LOG.debug("{}", message);
    }
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_f79aa7a4-f9a3-410a-ac95-bea496edab52", "Importing Model");
    assertThat(dmnModel, notNullValue());
    for (DMNMessage message : dmnModel.getMessages()) {
        LOG.debug("{}", message);
    }
    DMNContext context = runtime.newContext();
    context.set("A Person", mapOf(entry("name", "John"), entry("age", 47)));
    DMNResult evaluateAll = runtime.evaluateAll(dmnModel, context);
    for (DMNMessage message : evaluateAll.getMessages()) {
        LOG.debug("{}", message);
    }
    LOG.debug("{}", evaluateAll);
    assertThat(evaluateAll.getDecisionResultByName("Greeting").getResult(), is("Hello John!"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.v1_1.Definitions) 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)

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