Search in sources :

Example 96 with DMNResult

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

the class DMNRuntimeTest method testNowFunction.

@Test
public void testNowFunction() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("today_function_test.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_4ad80959-5fd8-46b7-8c9a-ab2fa58cb5b4", "When is it");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("The date", LocalDate.of(2017, 01, 12));
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getContext().get("When is it"), is("It is in the past"));
}
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 97 with DMNResult

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

the class DMNRuntimeTest method testInvokeFunctionCallerError.

@Test
public void testInvokeFunctionCallerError() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("Caller.dmn", this.getClass(), "Calling.dmn");
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_b0a696d6-3d57-4e97-b5d4-b44a63909d67", "Caller");
    assertThat(dmnModel, notNullValue());
    DMNContext wrongContext = DMNFactory.newContext();
    wrongContext.set("My Name", "John Doe");
    wrongContext.set("My Number", "<not a number>");
    wrongContext.set("Call ns", "http://www.trisotech.com/definitions/_88156d21-3acc-43b6-8b81-385caf0bb6ca");
    wrongContext.set("Call name", "Calling");
    wrongContext.set("Invoke decision", "Final Result");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, wrongContext);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(true));
    // total of: 2. x1 error in calling external decision, and x1 error in making final decision as it depends on the former.
    // please notice it will print 4 lines in the log, 2x are the "external invocation" and then 2x are the one by the caller, checked herebelow:
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getMessages().size(), is(2));
}
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 98 with DMNResult

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

the class DMNRuntimeTest method testOutputReuse.

@Test
public void testOutputReuse() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Input_reuse_in_output.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_098bb607-eff7-4772-83ac-6ded8b371fa7", "Input reuse in output");
    assertThat(dmnModel, notNullValue());
    DMNContext context = DMNFactory.newContext();
    context.set("Age", 40);
    context.set("Requested Product", "Fixed30");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("My Decision"), is("Fixed30"));
}
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 99 with DMNResult

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

the class DMNRuntimeTest method testEx_4_3simplifiedASD.

@Test
public void testEx_4_3simplifiedASD() {
    // DROOLS-2117 improve Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Ex_4_3simplified.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_5c5a9c72-627e-4666-ae85-31356fed3658", "Ex_4_3simplified");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("number", "ciao");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    System.out.println(dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(true));
    // we want the error message to include not only which value was incompatible, but the type which was expected.
    // in this case the value is `ciao` for a String
    // but should have been a FEEL:number.
    assertThat(dmnResult.getMessages().stream().filter(m -> m.getMessageType() == DMNMessageType.ERROR_EVAL_NODE).anyMatch(m -> m.getMessage().endsWith("is not allowed by the declared type (DMNType{ http://www.omg.org/spec/FEEL/20140401 : number })")), is(true));
}
Also used : BeforeEvaluateDecisionTableEvent(org.kie.dmn.api.core.event.BeforeEvaluateDecisionTableEvent) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) ZonedDateTime(java.time.ZonedDateTime) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Assert.assertThat(org.junit.Assert.assertThat) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) Duration(java.time.Duration) Map(java.util.Map) LocalTime(java.time.LocalTime) ZoneOffset(java.time.ZoneOffset) OffsetTime(java.time.OffsetTime) BeforeEvaluateDecisionEvent(org.kie.dmn.api.core.event.BeforeEvaluateDecisionEvent) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNFactory(org.kie.dmn.core.api.DMNFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Collection(java.util.Collection) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) UUID(java.util.UUID) ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) Collectors(java.util.stream.Collectors) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Assert.assertFalse(org.junit.Assert.assertFalse) LocalDate(java.time.LocalDate) DMNContext(org.kie.dmn.api.core.DMNContext) FEELStringMarshaller(org.kie.dmn.feel.marshaller.FEELStringMarshaller) KieHelper(org.kie.dmn.core.util.KieHelper) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) AfterEvaluateDecisionEvent(org.kie.dmn.api.core.event.AfterEvaluateDecisionEvent) EvalHelper(org.kie.dmn.feel.util.EvalHelper) Mockito.mock(org.mockito.Mockito.mock) AfterEvaluateDecisionTableEvent(org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent) DynamicTypeUtils.prototype(org.kie.dmn.core.util.DynamicTypeUtils.prototype) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) CoreMatchers.not(org.hamcrest.CoreMatchers.not) DynamicTypeUtils.entry(org.kie.dmn.core.util.DynamicTypeUtils.entry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) KieServices(org.kie.api.KieServices) Matchers.hasSize(org.hamcrest.Matchers.hasSize) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Period(java.time.Period) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.verify(org.mockito.Mockito.verify) Ignore(org.junit.Ignore) Assert.assertEquals(org.junit.Assert.assertEquals) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) 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 100 with DMNResult

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

the class DMNRuntimeTest method testRelation.

@Test
public void testRelation() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("relation-expression.dmn", getClass());
    // runtime.addListener( DMNRuntimeUtil.createListener() );
    DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "relation-expression");
    assertThat(dmnModel, notNullValue());
    assertThat(dmnModel.getMessages().toString(), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getContext().get("Employee Relation"), is(instanceOf(List.class)));
    List<Map<String, Object>> employees = (List<Map<String, Object>>) dmnResult.getContext().get("Employee Relation");
    Map<String, Object> e = employees.get(0);
    assertThat(e.get("Name"), is("John"));
    assertThat(e.get("Dept"), is("Sales"));
    assertThat(e.get("Salary"), is(BigDecimal.valueOf(100000)));
    e = employees.get(1);
    assertThat(e.get("Name"), is("Mary"));
    assertThat(e.get("Dept"), is("Finances"));
    assertThat(e.get("Salary"), is(BigDecimal.valueOf(120000)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) List(java.util.List) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Aggregations

DMNResult (org.kie.dmn.api.core.DMNResult)127 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)125 DMNContext (org.kie.dmn.api.core.DMNContext)122 DMNModel (org.kie.dmn.api.core.DMNModel)120 Test (org.junit.Test)119 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 BigDecimal (java.math.BigDecimal)22 HashMap (java.util.HashMap)22 KieServices (org.kie.api.KieServices)19 KieContainer (org.kie.api.runtime.KieContainer)19 ArrayList (java.util.ArrayList)16 List (java.util.List)15 Map (java.util.Map)15 DMNMessage (org.kie.dmn.api.core.DMNMessage)12 CoreMatchers.is (org.hamcrest.CoreMatchers.is)10 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)10 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)10 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)10 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)10 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)10