Search in sources :

Example 26 with FEELPropertyAccessible

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

the class DMNStronglyTypedSupportTest method testTimeFunction.

@Test
public void testTimeFunction() {
    final DMNRuntime runtime = createRuntime("TimeFromDate.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ecf4ea54-2abc-4e2f-a101-4fe14e356a46", "Dessin 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("datetimestring", "2016-07-29T05:48:23");
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.getContext().get("time"), is(LocalTime.of(5, 48, 23)));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("time"), is(LocalTime.of(5, 48, 23)));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) 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 27 with FEELPropertyAccessible

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

the class DMNStronglyTypedSupportTest method testDateAndTime.

@Test
public void testDateAndTime() {
    final DMNRuntime runtime = createRuntime("0007-date-time.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    final 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));
    final 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");
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    final DMNContext ctx = dmnResult.getContext();
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Date-Time"), is(ZonedDateTime.of(2016, 12, 24, 23, 59, 0, 0, ZoneOffset.ofHours(-5))));
        FEELPropertyAccessible resultDate = (FEELPropertyAccessible) allProperties.get("Date");
        assertThat(resultDate.getClass().getSimpleName(), is("TDateVariants"));
        assertThat(resultDate.getFEELProperty("fromString").toOptional().get(), is(LocalDate.of(2015, 12, 24)));
        assertThat(resultDate.getFEELProperty("fromDateTime").toOptional().get(), is(LocalDate.of(2016, 12, 24)));
        assertThat(resultDate.getFEELProperty("fromYearMonthDay").toOptional().get(), is(LocalDate.of(1999, 11, 22)));
        assertThat(allProperties.get("Time"), is(OffsetTime.of(0, 0, 1, 0, ZoneOffset.ofHours(-1))));
        assertThat(allProperties.get("Date-Time2"), is(ZonedDateTime.of(2015, 12, 24, 0, 0, 1, 0, ZoneOffset.ofHours(-1))));
        assertThat(allProperties.get("Time2"), is(OffsetTime.of(0, 0, 1, 0, ZoneOffset.ofHours(-1))));
        assertThat(allProperties.get("Time3"), is(OffsetTime.of(12, 59, 1, 300000000, ZoneOffset.ofHours(-1))));
        assertThat(allProperties.get("dtDuration1"), is(Duration.parse("P13DT2H14S")));
        assertThat(allProperties.get("dtDuration2"), is(Duration.parse("P367DT3H58M59S")));
        assertThat(allProperties.get("hoursInDuration"), is(new BigDecimal("3")));
        assertThat(allProperties.get("sumDurations"), is(Duration.parse("PT9125H59M13S")));
        assertThat(allProperties.get("ymDuration2"), is(ComparablePeriod.parse("P1Y")));
        assertThat(allProperties.get("cDay"), is(BigDecimal.valueOf(24)));
        assertThat(allProperties.get("cYear"), is(BigDecimal.valueOf(2015)));
        assertThat(allProperties.get("cMonth"), is(BigDecimal.valueOf(12)));
        assertThat(allProperties.get("cHour"), is(BigDecimal.valueOf(0)));
        assertThat(allProperties.get("cMinute"), is(BigDecimal.valueOf(0)));
        assertThat(allProperties.get("cSecond"), is(BigDecimal.valueOf(1)));
        assertThat(allProperties.get("cTimezone"), is("GMT-01:00"));
        assertThat(allProperties.get("years"), is(BigDecimal.valueOf(1)));
        assertThat(allProperties.get("d1seconds"), is(BigDecimal.valueOf(14)));
    } else {
        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(0, 0, 1, 0, ZoneOffset.ofHours(-1))));
        assertThat(ctx.get("Date-Time2"), is(ZonedDateTime.of(2015, 12, 24, 0, 0, 1, 0, ZoneOffset.ofHours(-1))));
        assertThat(ctx.get("Time2"), is(OffsetTime.of(0, 0, 1, 0, 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(ComparablePeriod.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("GMT-01:00"));
        assertThat(ctx.get("years"), is(BigDecimal.valueOf(1)));
        assertThat(ctx.get("d1seconds"), is(BigDecimal.valueOf(14)));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) 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 28 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testTopLevelCompositeCollection.

@Test
public void testTopLevelCompositeCollection() {
    final DMNRuntime runtime = createRuntime("topLevelCompositeCollection.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_3ED2F714-24F0-4764-88FA-04217901C05A", "topLevelCompositeCollection");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    List<?> pairs = Arrays.asList(mapOf(entry("letter", "A"), entry("num", new BigDecimal(1))), mapOf(entry("letter", "B"), entry("num", new BigDecimal(2))), mapOf(entry("letter", "C"), entry("num", new BigDecimal(3))));
    context.set("InputData-1", pairs);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        List<FEELPropertyAccessible> pairList = (List<FEELPropertyAccessible>) allProperties.get("Decision-1");
        FEELPropertyAccessible pair1 = pairList.get(0);
        FEELPropertyAccessible pair2 = pairList.get(1);
        assertThat(pair1.getFEELProperty("letter").toOptional().get(), anyOf(is("ABC"), is("DEF")));
        assertThat(pair1.getFEELProperty("num").toOptional().get(), anyOf(is(EvalHelper.coerceNumber(123)), is(EvalHelper.coerceNumber(456))));
        assertThat(pair2.getFEELProperty("letter").toOptional().get(), anyOf(is("ABC"), is("DEF")));
        assertThat(pair2.getFEELProperty("num").toOptional().get(), anyOf(is(EvalHelper.coerceNumber(123)), is(EvalHelper.coerceNumber(456))));
    } else {
        assertThat((List<?>) dmnResult.getContext().get("Decision-1"), contains(mapOf(entry("letter", "ABC"), entry("num", EvalHelper.coerceNumber(123))), mapOf(entry("letter", "DEF"), entry("num", EvalHelper.coerceNumber(456)))));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) BigDecimal(java.math.BigDecimal) ArrayList(java.util.ArrayList) List(java.util.List) DMNModel(org.kie.dmn.api.core.DMNModel) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) DMNRuntimeTest(org.kie.dmn.core.DMNRuntimeTest) Test(org.junit.Test) DMNDecisionServicesTest(org.kie.dmn.core.v1_2.DMNDecisionServicesTest)

Example 29 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testCapitalLetterConflictItemDef.

@Test
public void testCapitalLetterConflictItemDef() {
    final DMNRuntime runtime = createRuntime("capitalLetterConflictItemDef.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_DA986720-823F-4334-8AB5-5CBA76FD1B9E", "capitalLetterConflictItemDef");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final Map<String, Object> person = new HashMap<>();
    person.put("name", "john");
    person.put("Name", "John");
    final DMNContext context = DMNFactory.newContext();
    context.set("InputData-1", person);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        FEELPropertyAccessible myPersonOut = (FEELPropertyAccessible) allProperties.get("Decision-1");
        assertThat(myPersonOut.getClass().getSimpleName(), is("TPerson"));
        assertThat(myPersonOut.getFEELProperty("name").toOptional().get(), is("paul"));
        assertThat(myPersonOut.getFEELProperty("Name").toOptional().get(), is("Paul"));
    } else {
        Map<String, Object> outPerson = (Map<String, Object>) dmnResult.getContext().get("Decision-1");
        assertThat(outPerson.get("name"), is("paul"));
        assertThat(outPerson.get("Name"), is("Paul"));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) HashMap(java.util.HashMap) Map(java.util.Map) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) DMNRuntimeTest(org.kie.dmn.core.DMNRuntimeTest) Test(org.junit.Test) DMNDecisionServicesTest(org.kie.dmn.core.v1_2.DMNDecisionServicesTest)

Example 30 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testCollectionOfCollection.

public void testCollectionOfCollection() {
    final DMNRuntime runtime = createRuntime("topLevelColOfCol.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_74636626-ACB0-4A1F-9AD3-D4E0AFA1A24A", "topLevelColOfCol");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    // create ColB -> ColA -> Person data
    List<Map<String, Object>> personList = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        personList.add(prototype(entry("name", "John" + i), entry("age", 20 + i)));
    }
    final List<Map<String, Object>> colA1 = personList.subList(0, 2);
    final List<Map<String, Object>> colA2 = personList.subList(2, 4);
    final List<List<Map<String, Object>>> colB = Arrays.asList(colA1, colA2);
    context.set("InputData-1", colB);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        List<List<FEELPropertyAccessible>> colBOut = (List<List<FEELPropertyAccessible>>) allProperties.get("Decision-1");
        List<FEELPropertyAccessible> colAOut1 = colBOut.get(0);
        assertPersonInCol(colAOut1.get(0));
        assertPersonInCol(colAOut1.get(1));
        List<FEELPropertyAccessible> colAOut2 = colBOut.get(1);
        assertPersonInCol(colAOut2.get(0));
        assertPersonInCol(colAOut2.get(1));
    } else {
        List<List<Map<String, Object>>> colBOut = (List<List<Map<String, Object>>>) dmnResult.getContext().get("Decision-1");
        List<Map<String, Object>> colAOut1 = colBOut.get(0);
        assertPersonMapInCol(colAOut1.get(0));
        assertPersonMapInCol(colAOut1.get(1));
        List<Map<String, Object>> colAOut2 = colBOut.get(1);
        assertPersonMapInCol(colAOut2.get(0));
        assertPersonMapInCol(colAOut2.get(1));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) DMNContext(org.kie.dmn.api.core.DMNContext) ArrayList(java.util.ArrayList) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) DMNModel(org.kie.dmn.api.core.DMNModel)

Aggregations

FEELPropertyAccessible (org.kie.dmn.api.core.FEELPropertyAccessible)44 DMNContext (org.kie.dmn.api.core.DMNContext)41 DMNContextFPAImpl (org.kie.dmn.core.impl.DMNContextFPAImpl)41 DMNResult (org.kie.dmn.api.core.DMNResult)40 Test (org.junit.Test)36 DMNModel (org.kie.dmn.api.core.DMNModel)34 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)34 BaseVariantTest (org.kie.dmn.core.BaseVariantTest)31 DMNRuntimeTest (org.kie.dmn.core.DMNRuntimeTest)23 DMNDecisionServicesTest (org.kie.dmn.core.v1_2.DMNDecisionServicesTest)23 BigDecimal (java.math.BigDecimal)16 HashMap (java.util.HashMap)14 Map (java.util.Map)10 ArrayList (java.util.ArrayList)6 List (java.util.List)6 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)3 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)3 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)3 Field (java.lang.reflect.Field)1 Duration (java.time.Duration)1