Search in sources :

Example 21 with DMNModel

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

the class DMNRuntimeTest method test_countCSATradeRatings.

@Test
public void test_countCSATradeRatings() {
    // DROOLS-1563
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("countCSATradeRatings.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_1a7d184c-2e38-4462-ae28-15591ef6d534", "countCSATradeRatings");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext ctx = runtime.newContext();
    List<Map<?, ?>> ratings = new ArrayList<>();
    ratings.add(prototype(entry("Agency", "FITCH"), entry("Value", "val1")));
    ratings.add(prototype(entry("Agency", "MOODY"), entry("Value", "val2")));
    ctx.set("CSA Trade Ratings", ratings);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Trade Ratings"), is(new BigDecimal(2)));
    DMNContext ctx2 = runtime.newContext();
    ctx2.set("CSA Trade Ratings", null);
    DMNResult dmnResult2 = runtime.evaluateAll(dmnModel, ctx2);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(false));
    DMNContext result2 = dmnResult2.getContext();
    assertThat(result2.get("Trade Ratings"), nullValue());
    DMNResult dmnResult3 = runtime.evaluateAll(dmnModel, runtime.newContext());
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult3.getMessages()), dmnResult3.hasErrors(), is(true));
    assertThat(dmnResult3.getMessages().stream().anyMatch(m -> m.getMessageType().equals(DMNMessageType.REQ_NOT_FOUND)), 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) ArrayList(java.util.ArrayList) 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 22 with DMNModel

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

the class DMNRuntimeTest method testInvalidVariableNames.

@Test
public void testInvalidVariableNames() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("invalid-variable-names.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0001-input-data-string");
    assertThat(dmnModel, notNullValue());
    assertThat(dmnModel.hasErrors(), is(true));
}
Also used : DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 23 with DMNModel

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

the class DMNRuntimeTypeCheckTest method assertPerformTypeCheck.

private void assertPerformTypeCheck(DMNRuntime runtime) {
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6d8af9a2-dcf4-4b9e-8d90-6ccddc8c1bbd", "forTypeCheckTest");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("a number", "ciao");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat("Should throw several errors, not only for 1 specific Decision: " + DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getMessages(DMNMessage.Severity.ERROR).stream().allMatch(m -> m.getSourceId().equals(dmnModel.getDecisionByName("hundred minus number").getId())), is(false));
    DMNDecisionResult textPlusNumberDR = dmnResult.getDecisionResultByName("text plus number");
    // dependency failed type check
    assertThat(textPlusNumberDR.getEvaluationStatus(), is(DecisionEvaluationStatus.SKIPPED));
    DMNDecisionResult hundredMinusNumber = dmnResult.getDecisionResultByName("hundred minus number");
    // dependency failed type check
    assertThat(textPlusNumberDR.getEvaluationStatus(), is(DecisionEvaluationStatus.SKIPPED));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) KieFileSystem(org.kie.api.builder.KieFileSystem) DMNFactory(org.kie.dmn.core.api.DMNFactory) KieBuilder(org.kie.api.builder.KieBuilder) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test) UUID(java.util.UUID) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) Assert.assertThat(org.junit.Assert.assertThat) Resource(org.kie.api.io.Resource) DMNModel(org.kie.dmn.api.core.DMNModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DecisionEvaluationStatus(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus) KieServices(org.kie.api.KieServices) DMNContext(org.kie.dmn.api.core.DMNContext) KieHelper(org.kie.dmn.core.util.KieHelper) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 24 with DMNModel

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

the class DMNRuntimeTypeCheckTest method assertNoTypeCheck.

private void assertNoTypeCheck(DMNRuntime runtime) {
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6d8af9a2-dcf4-4b9e-8d90-6ccddc8c1bbd", "forTypeCheckTest");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("a number", "ciao");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getMessages(DMNMessage.Severity.ERROR).stream().allMatch(m -> m.getSourceId().equals(dmnModel.getDecisionByName("hundred minus number").getId())), is(true));
    DMNDecisionResult textPlusNumberDR = dmnResult.getDecisionResultByName("text plus number");
    assertThat(textPlusNumberDR.getEvaluationStatus(), is(DecisionEvaluationStatus.SUCCEEDED));
    assertThat(textPlusNumberDR.getResult(), is("The input number is: ciao"));
    DMNDecisionResult hundredMinusNumber = dmnResult.getDecisionResultByName("hundred minus number");
    assertThat(hundredMinusNumber.getEvaluationStatus(), is(DecisionEvaluationStatus.FAILED));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) KieFileSystem(org.kie.api.builder.KieFileSystem) DMNFactory(org.kie.dmn.core.api.DMNFactory) KieBuilder(org.kie.api.builder.KieBuilder) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test) UUID(java.util.UUID) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) Assert.assertThat(org.junit.Assert.assertThat) Resource(org.kie.api.io.Resource) DMNModel(org.kie.dmn.api.core.DMNModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) DecisionEvaluationStatus(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus) KieServices(org.kie.api.KieServices) DMNContext(org.kie.dmn.api.core.DMNContext) KieHelper(org.kie.dmn.core.util.KieHelper) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 25 with DMNModel

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

the class FlightRebookingTest method testSolution1.

@Test
public void testSolution1() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0019-flight-rebooking.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("https://www.drools.org/kie-dmn", "0019-flight-rebooking");
    assertThat(dmnModel, notNullValue());
    // need proper type support to enable this
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    List passengerList = loadPassengerList();
    List flightList = loadFlightList();
    context.set("Passenger List", passengerList);
    context.set("Flight List", flightList);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Rebooked Passengers"), is(loadExpectedResult()));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) ArrayList(java.util.ArrayList) List(java.util.List) 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