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)));
}
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)));
}
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)));
}
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));
}
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));
}
Aggregations