Search in sources :

Example 56 with DMNModel

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

the class DMNInputRuntimeTest method testDMNInputDataNodeTypeTest.

@Test
public void testDMNInputDataNodeTypeTest() {
    // DROOLS-1569
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("DMNInputDataNodeTypeTest.dmn", this.getClass());
    final String MODEL_NAMESPACE = "http://www.trisotech.com/definitions/_17396034-163a-48aa-9a7f-c6eb17f9cc6c";
    final String FEEL_NAMESPACE = "http://www.omg.org/spec/FEEL/20140401";
    final DMNModel dmnModel = runtime.getModel(MODEL_NAMESPACE, "DMNInputDataNodeTypeTest");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final InputDataNode idnMembership = dmnModel.getInputs().stream().filter(idn -> idn.getName().equals("Membership Level")).findFirst().get();
    assertThat(idnMembership.getType().getBaseType().getNamespace(), is(FEEL_NAMESPACE));
    assertThat(idnMembership.getType().getBaseType().getName(), is("string"));
    assertThat(idnMembership.getType().isCollection(), is(false));
    assertThat(idnMembership.getType().isComposite(), is(false));
    assertThat(idnMembership.getType().getAllowedValues().size(), is(3));
    assertThat(idnMembership.getType().getAllowedValues().get(0).toString(), is("\"Gold\""));
    assertThat(idnMembership.getType().getAllowedValues().get(1).toString(), is("\"Silver\""));
    assertThat(idnMembership.getType().getAllowedValues().get(2).toString(), is("\"None\""));
    final InputDataNode idnMembershipLevels = dmnModel.getInputs().stream().filter(idn -> idn.getName().equals("Membership Levels")).findFirst().get();
    assertThat(idnMembershipLevels.getType().getBaseType().getNamespace(), is(MODEL_NAMESPACE));
    assertThat(idnMembershipLevels.getType().getBaseType().getName(), is("tMembershipLevel"));
    assertThat(idnMembershipLevels.getType().isCollection(), is(true));
    assertThat(idnMembershipLevels.getType().isComposite(), is(false));
    assertThat(idnMembershipLevels.getType().getAllowedValues().isEmpty(), is(true));
    final InputDataNode idnPercent = dmnModel.getInputs().stream().filter(idn -> idn.getName().equals("Percent")).findFirst().get();
    assertThat(idnPercent.getType().getBaseType().getNamespace(), is(FEEL_NAMESPACE));
    assertThat(idnPercent.getType().getBaseType().getName(), is("number"));
    assertThat(idnPercent.getType().isCollection(), is(false));
    assertThat(idnPercent.getType().isComposite(), is(false));
    assertThat(idnPercent.getType().getAllowedValues().size(), is(1));
    assertThat(idnPercent.getType().getAllowedValues().get(0).toString(), is("[0..100]"));
    final InputDataNode idnCarDamageResponsibility = dmnModel.getInputs().stream().filter(idn -> idn.getName().equals("Car Damage Responsibility")).findFirst().get();
    assertThat(idnCarDamageResponsibility.getType().getBaseType(), is(nullValue()));
    assertThat(idnCarDamageResponsibility.getType().isCollection(), is(false));
    assertThat(idnCarDamageResponsibility.getType().isComposite(), is(true));
}
Also used : InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 57 with DMNModel

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

the class DMNInputRuntimeTest method testAllowedValuesChecks.

@Test
public void testAllowedValuesChecks() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("AllowedValuesChecks.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_238bd96d-47cd-4746-831b-504f3e77b442", "AllowedValuesChecks");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext ctx1 = runtime.newContext();
    ctx1.set("p1", prototype(entry("Name", "P1"), entry("Interests", Arrays.asList("Golf"))));
    final DMNResult dmnResult1 = runtime.evaluateAll(dmnModel, ctx1);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult1.getMessages()), dmnResult1.hasErrors(), is(false));
    assertThat(dmnResult1.getContext().get("MyDecision"), is("The Person P1 likes 1 thing(s)."));
    final DMNContext ctx2 = runtime.newContext();
    ctx2.set("p1", prototype(entry("Name", "P2"), entry("Interests", Arrays.asList("x"))));
    final DMNResult dmnResult2 = runtime.evaluateAll(dmnModel, ctx2);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(true));
    assertThat(dmnResult2.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.ERROR_EVAL_NODE)), is(true));
    final DMNContext ctx3 = runtime.newContext();
    ctx3.set("p1", prototype(entry("Name", "P3"), entry("Interests", Arrays.asList("Golf", "Computer"))));
    final DMNResult dmnResult3 = runtime.evaluateAll(dmnModel, ctx3);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult3.getMessages()), dmnResult3.hasErrors(), is(false));
    assertThat(dmnResult3.getContext().get("MyDecision"), is("The Person P3 likes 2 thing(s)."));
    final DMNContext ctx4 = runtime.newContext();
    ctx4.set("p1", prototype(entry("Name", "P4"), entry("Interests", Arrays.asList("Golf", "x"))));
    final DMNResult dmnResult4 = runtime.evaluateAll(dmnModel, ctx4);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult4.getMessages()), dmnResult4.hasErrors(), is(true));
    assertThat(dmnResult4.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.ERROR_EVAL_NODE)), is(true));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) DMNFactory(org.kie.dmn.core.api.DMNFactory) DynamicTypeUtils.prototype(org.kie.dmn.core.util.DynamicTypeUtils.prototype) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) DynamicTypeUtils.entry(org.kie.dmn.core.util.DynamicTypeUtils.entry) Set(java.util.Set) Test(org.junit.Test) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) Assert(org.junit.Assert) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) 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 58 with DMNModel

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

the class DMNInputRuntimeTest method testInputDataTypeRefWithAllowedValues.

@Test
public void testInputDataTypeRefWithAllowedValues() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("actualInputMatchInputValues-forTypeRef.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://www.drools.org/kie-dmn/definitions", "definitions");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = runtime.newContext();
    // <<< `zzz` is NOT in the list of allowed value as declared by the typeRef for this inputdata
    context.set("MyInput", "zzz");
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(true));
    assertThat(dmnResult.getMessages().size(), is(1));
    assertThat(dmnResult.getMessages().get(0).getSourceId(), is("_3d560678-a126-4654-a686-bc6d941fe40b"));
}
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 59 with DMNModel

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

the class DMNInputRuntimeTest method testGetRequiredInputsByName.

@Test
public void testGetRequiredInputsByName() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0001-input-data-string.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/drools/kie-dmn", "_0001-input-data-string");
    assertThat(dmnModel, notNullValue());
    Set<InputDataNode> inputs = dmnModel.getRequiredInputsForDecisionName("Greeting Message");
    assertThat(inputs.size(), is(1));
    assertThat(inputs.iterator().next().getName(), is("Full Name"));
    inputs = dmnModel.getRequiredInputsForDecisionName("nonExistantDecisionName");
    assertThat(inputs.size(), is(0));
}
Also used : InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 60 with DMNModel

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

the class DMNRuntimeTest method testTimeFunction.

@Test
public void testTimeFunction() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("TimeFromDate.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ecf4ea54-2abc-4e2f-a101-4fe14e356a46", "Dessin 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("datetimestring", "2016-07-29T05:48:23");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getContext().get("time"), is(LocalTime.of(5, 48, 23)));
}
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)

Aggregations

DMNModel (org.kie.dmn.api.core.DMNModel)163 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)152 Test (org.junit.Test)143 DMNContext (org.kie.dmn.api.core.DMNContext)131 DMNResult (org.kie.dmn.api.core.DMNResult)123 BigDecimal (java.math.BigDecimal)26 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Map (java.util.Map)22 KieServices (org.kie.api.KieServices)21 KieContainer (org.kie.api.runtime.KieContainer)21 DMNMessage (org.kie.dmn.api.core.DMNMessage)16 CoreMatchers.is (org.hamcrest.CoreMatchers.is)13 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)13 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)13 UUID (java.util.UUID)11 CoreMatchers.nullValue (org.hamcrest.CoreMatchers.nullValue)11