Search in sources :

Example 16 with DMNContext

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

the class DMNRuntimeTest method testDateAndTime.

@Test
public void testDateAndTime() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0007-date-time.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_69430b3e-17b8-430d-b760-c505bf6469f9", "dateTime Table 58");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("dateString", "2015-12-24");
    context.set("timeString", "00:00:01-01:00");
    context.set("dateTimeString", "2016-12-24T23:59:00-05:00");
    context.set("Hours", 12);
    context.set("Minutes", 59);
    context.set("Seconds", new BigDecimal("1.3"));
    context.set("Timezone", "PT-1H");
    context.set("Year", 1999);
    context.set("Month", 11);
    context.set("Day", 22);
    // <variable name="oneHour" typeRef="feel:days and time duration"/>
    context.set("oneHour", Duration.parse("PT1H"));
    // <variable name="durationString" typeRef="feel:string"/>
    context.set("durationString", "P13DT2H14S");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    DMNContext ctx = dmnResult.getContext();
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(ctx.get("Date-Time"), is(ZonedDateTime.of(2016, 12, 24, 23, 59, 0, 0, ZoneOffset.ofHours(-5))));
    assertThat(ctx.get("Date"), is(new HashMap<String, Object>() {

        {
            put("fromString", LocalDate.of(2015, 12, 24));
            put("fromDateTime", LocalDate.of(2016, 12, 24));
            put("fromYearMonthDay", LocalDate.of(1999, 11, 22));
        }
    }));
    assertThat(ctx.get("Time"), is(OffsetTime.of(00, 00, 01, 00, ZoneOffset.ofHours(-1))));
    assertThat(ctx.get("Date-Time2"), is(ZonedDateTime.of(2015, 12, 24, 00, 00, 01, 00, ZoneOffset.ofHours(-1))));
    assertThat(ctx.get("Time2"), is(OffsetTime.of(00, 00, 01, 00, ZoneOffset.ofHours(-1))));
    assertThat(ctx.get("Time3"), is(OffsetTime.of(12, 59, 1, 300000000, ZoneOffset.ofHours(-1))));
    assertThat(ctx.get("dtDuration1"), is(Duration.parse("P13DT2H14S")));
    assertThat(ctx.get("dtDuration2"), is(Duration.parse("P367DT3H58M59S")));
    assertThat(ctx.get("hoursInDuration"), is(new BigDecimal("3")));
    assertThat(ctx.get("sumDurations"), is(Duration.parse("PT9125H59M13S")));
    assertThat(ctx.get("ymDuration2"), is(Period.parse("P1Y")));
    assertThat(ctx.get("cDay"), is(BigDecimal.valueOf(24)));
    assertThat(ctx.get("cYear"), is(BigDecimal.valueOf(2015)));
    assertThat(ctx.get("cMonth"), is(BigDecimal.valueOf(12)));
    assertThat(ctx.get("cHour"), is(BigDecimal.valueOf(0)));
    assertThat(ctx.get("cMinute"), is(BigDecimal.valueOf(0)));
    assertThat(ctx.get("cSecond"), is(BigDecimal.valueOf(1)));
    assertThat(ctx.get("cTimezone"), is(Duration.parse("PT-1H")));
    assertThat(ctx.get("years"), is(BigDecimal.valueOf(1)));
    assertThat(ctx.get("seconds"), is(BigDecimal.valueOf(14)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 17 with DMNContext

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

the class DMNRuntimeTest method testBuiltInFunctionInvocation.

@Test
public void testBuiltInFunctionInvocation() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("BuiltInFunctionInvocation.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_b77219ee-ec28-48e3-b240-8e0dbbabefeb", "built in function invocation");
    assertThat(dmnModel, notNullValue());
    assertThat(dmnModel.getMessages().toString(), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("a", 10);
    context.set("b", 5);
    context.set("x", "Hello, World!");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getContext().get("calc min"), is(BigDecimal.valueOf(5)));
    assertThat(dmnResult.getContext().get("fixed params"), is("World!"));
    assertThat(dmnResult.getContext().get("out of order"), is(BigDecimal.valueOf(5)));
}
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 18 with DMNContext

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

the class DMNRuntimeTest method testEnhancedForLoop2.

@Test
public void testEnhancedForLoop2() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("MACD-enhanced_iteration.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6cfe7d88-6741-45d1-968c-b61a597d0964", "MACD-enhanced iteration");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    Map<String, Object> d1 = prototype(entry("aDate", LocalDate.of(2018, 3, 5)), entry("Close", 1010));
    Map<String, Object> d2 = prototype(entry("aDate", LocalDate.of(2018, 3, 6)), entry("Close", 1020));
    Map<String, Object> d3 = prototype(entry("aDate", LocalDate.of(2018, 3, 7)), entry("Close", 1030));
    context.set("DailyTable", Arrays.asList(d1, d2, d3));
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext resultContext = dmnResult.getContext();
    assertThat(((Map<String, Object>) ((List) resultContext.get("MACDTable")).get(0)).get("aDate"), is(LocalDate.of(2018, 3, 5)));
    assertThat(((Map<String, Object>) ((List) resultContext.get("MACDTable")).get(1)).get("aDate"), is(LocalDate.of(2018, 3, 6)));
    assertThat(((Map<String, Object>) ((List) resultContext.get("MACDTable")).get(2)).get("aDate"), is(LocalDate.of(2018, 3, 7)));
}
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) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 19 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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 20 with DMNContext

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

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