Search in sources :

Example 16 with DMNModel

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

the class DMNRuntimeTest method testPriority_table.

@Test
public void testPriority_table() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("priority_table.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_ff54a44d-b8f5-48fc-b2b7-43db767e8a1c", "not quite all or nothing P");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = runtime.newContext();
    context.set("isAffordable", false);
    context.set("RiskCategory", "Medium");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Approval Status"), is("Declined"));
}
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 17 with DMNModel

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

the class DMNRuntimeTest method testEx_6_1.

@Test
public void testEx_6_1() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Ex_6_1.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_5f1269c8-1e6f-4748-9eca-26aa1b1278ef", "Ex 6-1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext ctx = runtime.newContext();
    Map<String, Object> t1 = new HashMap<>();
    t1.put("city", "Los Angeles");
    t1.put("name", "Los Angeles");
    t1.put("wins", 0);
    t1.put("losses", 1);
    t1.put("bonus points", 40);
    Map<String, Object> t2 = new HashMap<>();
    t2.put("city", "San Francisco");
    t2.put("name", "San Francisco");
    t2.put("wins", 1);
    t2.put("losses", 0);
    t2.put("bonus points", 7);
    ctx.set("NBA Pacific", Arrays.asList(new Map[] { t1, t2 }));
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Number of distinct cities"), is(new BigDecimal(2)));
    assertThat(result.get("Second place losses"), is(new BigDecimal(0)));
    assertThat(result.get("Max wins"), is(new BigDecimal(1)));
    assertThat(result.get("Mean wins"), is(new BigDecimal(0.5)));
    assertThat((List<?>) result.get("Positions of Los Angeles teams"), contains(new BigDecimal(1)));
    assertThat(result.get("Number of teams"), is(new BigDecimal(2)));
    assertThat(result.get("Sum of bonus points"), is(new BigDecimal(47)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) 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) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 18 with DMNModel

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

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

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

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