Search in sources :

Example 11 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testDecisionService.

@Test
public void testDecisionService() {
    final DMNRuntime runtime = createRuntime("DecisionServiceABC_DMN12.dmn", DMNDecisionServicesTest.class);
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_2443d3f5-f178-47c6-a0c9-b1fd1c933f60", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    // DecisionService only
    final DMNContext context = DMNFactory.newContext();
    final DMNResult dmnResult1 = evaluateDecisionService(runtime, dmnModel, context, "Decision Service ABC");
    LOG.debug("{}", dmnResult1);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult1.getMessages()), dmnResult1.hasErrors(), is(false));
    final DMNContext result = dmnResult1.getContext();
    // assertThat(result.getAll(), not(hasEntry(is("Invoking Decision"), anything()))); // we invoked only the Decision Service, not this other Decision in the model.
    assertThat(result.get("Invoking Decision"), nullValue());
    assertThat(result.get("ABC"), is("abc"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult1.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Invoking Decision"), nullValue());
        Object abc = allProperties.get("ABC");
        assertThat(abc, instanceOf(String.class));
        assertThat(abc, is("abc"));
    }
    // evaluateAll
    final DMNContext context2 = DMNFactory.newContext();
    final DMNResult dmnResult2 = evaluateModel(runtime, dmnModel, context2);
    LOG.debug("{}", dmnResult2);
    dmnResult2.getDecisionResults().forEach(x -> LOG.debug("{}", x));
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(false));
    final DMNContext result2 = dmnResult2.getContext();
    assertThat(result2.get("ABC"), is("abc"));
    assertThat(result2.get("Invoking Decision"), is("abc"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult2.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        Object decisionService = allProperties.get("Decision Service ABC");
        assertThat(decisionService, instanceOf(FEELFunction.class));
        assertThat(((FEELFunction) decisionService).getName(), is("Decision Service ABC"));
        Object invokingDecision = allProperties.get("Invoking Decision");
        assertThat(invokingDecision, instanceOf(String.class));
        assertThat(invokingDecision, is("abc"));
        Object abc = allProperties.get("ABC");
        assertThat(abc, instanceOf(String.class));
        assertThat(abc, is("abc"));
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNContext(org.kie.dmn.api.core.DMNContext) 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 12 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testCollectionOfCollectionOfCollection.

@Test
public void testCollectionOfCollectionOfCollection() {
    final DMNRuntime runtime = createRuntime("topLevelColOfColOfCol.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_74636626-ACB0-4A1F-9AD3-D4E0AFA1A24A", "topLevelColOfColOfCol");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    // create ColC -> ColB -> ColA -> Person data
    List<Map<String, Object>> personList = new ArrayList<>();
    for (int i = 0; i < 8; 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, 3);
    final List<Map<String, Object>> colA3 = personList.subList(4, 6);
    final List<Map<String, Object>> colA4 = personList.subList(6, 8);
    final List<List<Map<String, Object>>> colB1 = Arrays.asList(colA1, colA2);
    final List<List<Map<String, Object>>> colB2 = Arrays.asList(colA3, colA4);
    final List<List<List<Map<String, Object>>>> colC = Arrays.asList(colB1, colB2);
    context.set("InputData-1", colC);
    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<List<FEELPropertyAccessible>>> colCOut = (List<List<List<FEELPropertyAccessible>>>) allProperties.get("Decision-1");
        List<FEELPropertyAccessible> personOutList = colCOut.stream().flatMap(colB -> colB.stream()).flatMap(colA -> colA.stream()).collect(Collectors.toList());
        personOutList.stream().forEach(person -> assertPersonInDeepCol(person));
    } else {
        List<List<List<Map<String, Object>>>> colCOut = (List<List<List<Map<String, Object>>>>) dmnResult.getContext().get("Decision-1");
        List<Map<String, Object>> personOutList = colCOut.stream().flatMap(colB -> colB.stream()).flatMap(colA -> colA.stream()).collect(Collectors.toList());
        personOutList.stream().forEach(person -> assertPersonMapInDeepCol(person));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) CoreMatchers.anyOf(org.hamcrest.CoreMatchers.anyOf) DMNResult(org.kie.dmn.api.core.DMNResult) DynamicTypeUtils.prototype(org.kie.dmn.core.util.DynamicTypeUtils.prototype) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) CoreMatchers.not(org.hamcrest.CoreMatchers.not) DynamicTypeUtils.entry(org.kie.dmn.core.util.DynamicTypeUtils.entry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Duration(java.time.Duration) Map(java.util.Map) DecisionEvaluationStatus(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus) LocalTime(java.time.LocalTime) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Period(java.time.Period) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) DMNFactory(org.kie.dmn.core.api.DMNFactory) Logger(org.slf4j.Logger) DMNRuntimeTest(org.kie.dmn.core.DMNRuntimeTest) EvalHelper.coerceNumber(org.kie.dmn.feel.util.EvalHelper.coerceNumber) Test(org.junit.Test) Collectors(java.util.stream.Collectors) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) DynamicTypeUtils.mapOf(org.kie.dmn.core.util.DynamicTypeUtils.mapOf) List(java.util.List) Matchers.contains(org.hamcrest.Matchers.contains) DMNDecisionServicesTest(org.kie.dmn.core.v1_2.DMNDecisionServicesTest) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) LocalDate(java.time.LocalDate) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) DMNContext(org.kie.dmn.api.core.DMNContext) EvalHelper(org.kie.dmn.feel.util.EvalHelper) 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) 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 13 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testFieldCapitalization.

@Test
public void testFieldCapitalization() {
    final DMNRuntime runtime = createRuntimeWithAdditionalResources("Traffic Violation.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("Driver", mapOf(entry("Name", "Luca"), entry("Age", 35), entry("State", "Italy"), entry("City", "Milan"), entry("Points", 2000)));
    context.set("Violation", mapOf(entry("Code", "s"), entry("Date", LocalDate.of(1984, 11, 6)), entry("Type", "speed"), entry("Actual Speed", 120), entry("Speed Limit", 100)));
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("Should the driver be suspended?").getResult(), is("Yes"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        FEELPropertyAccessible driver = (FEELPropertyAccessible) allProperties.get("Driver");
        assertThat(driver.getClass().getSimpleName(), is("TDriver"));
        assertThat(driver.getFEELProperty("Name").toOptional().get(), is("Luca"));
        assertThat(driver.getFEELProperty("Age").toOptional().get(), is(35));
        assertThat(driver.getFEELProperty("State").toOptional().get(), is("Italy"));
        assertThat(driver.getFEELProperty("City").toOptional().get(), is("Milan"));
        assertThat(driver.getFEELProperty("Points").toOptional().get(), is(2000));
        FEELPropertyAccessible violation = (FEELPropertyAccessible) allProperties.get("Violation");
        assertThat(violation.getClass().getSimpleName(), is("TViolation"));
        assertThat(violation.getFEELProperty("Code").toOptional().get(), is("s"));
        assertThat(violation.getFEELProperty("Date").toOptional().get(), is(LocalDate.of(1984, 11, 6)));
        assertThat(violation.getFEELProperty("Type").toOptional().get(), is("speed"));
        assertThat(violation.getFEELProperty("Actual Speed").toOptional().get(), is(120));
        assertThat(violation.getFEELProperty("Speed Limit").toOptional().get(), is(100));
        FEELPropertyAccessible fine = (FEELPropertyAccessible) allProperties.get("Fine");
        assertThat(fine.getClass().getSimpleName(), is("TFine"));
        assertThat(fine.getFEELProperty("Amount").toOptional().get(), is(new BigDecimal("500")));
        assertThat(fine.getFEELProperty("Points").toOptional().get(), is(new BigDecimal("3")));
        Object suspended = allProperties.get("Should the driver be suspended?");
        assertThat(suspended, instanceOf(String.class));
        assertThat(suspended, is("Yes"));
    }
}
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) BigDecimal(java.math.BigDecimal) 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 14 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testInnerComposite.

@Test
public void testInnerComposite() {
    final DMNRuntime runtime = createRuntime("innerComposite.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_641BCEBF-8D10-4E08-B47F-A9181C737A82", "new-file");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    Map<String, Object> yearly = mapOf(entry("Q1", 1), entry("Q2", new BigDecimal(2)), entry("Q3", 3), entry("Q4", new BigDecimal(4)));
    context.set("Yearly", yearly);
    Map<String, Object> employee = mapOf(entry("Name", "John Doe"), entry("Yearly", mapOf(entry("H1", 1), entry("H2", new BigDecimal(2)))));
    context.set("Employee", employee);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("Decision Yearly").getResult(), is("Total Yearly 10"));
    assertThat(dmnResult.getDecisionResultByName("Decision Employee").getResult(), is("For John Doe total: 3"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Decision Yearly"), is("Total Yearly 10"));
        assertThat(allProperties.get("Decision Employee"), is("For John Doe total: 3"));
    }
}
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) BigDecimal(java.math.BigDecimal) 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 15 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testTopLevelTypeCollection.

@Test
public void testTopLevelTypeCollection() {
    final DMNRuntime runtime = createRuntime("PersonListHelloBKM2.dmn", DMNRuntimeTest.class);
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_7e41a76e-2df6-4899-bf81-ae098757a3b6", "PersonListHelloBKM2");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    final Map<String, Object> p1 = prototype(entry("Full Name", "John Doe"), entry("Age", 33));
    final Map<String, Object> p2 = prototype(entry("Full Name", "47"), entry("Age", 47));
    context.set("My Input Data", Arrays.asList(p1, p2));
    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> personList = (List<FEELPropertyAccessible>) allProperties.get("My Decision");
        FEELPropertyAccessible person1 = personList.get(0);
        FEELPropertyAccessible person2 = personList.get(1);
        assertThat(person1.getFEELProperty("Full Name").toOptional().get(), anyOf(is("Prof. John Doe"), is("Prof. 47")));
        assertThat(person1.getFEELProperty("Age").toOptional().get(), anyOf(is(EvalHelper.coerceNumber(33)), is(EvalHelper.coerceNumber(47))));
        assertThat(person2.getFEELProperty("Full Name").toOptional().get(), anyOf(is("Prof. John Doe"), is("Prof. 47")));
        assertThat(person2.getFEELProperty("Age").toOptional().get(), anyOf(is(EvalHelper.coerceNumber(33)), is(EvalHelper.coerceNumber(47))));
    } else {
        assertThat((List<?>) dmnResult.getContext().get("My Decision"), contains(prototype(entry("Full Name", "Prof. John Doe"), entry("Age", EvalHelper.coerceNumber(33))), prototype(entry("Full Name", "Prof. 47"), entry("Age", EvalHelper.coerceNumber(47)))));
    }
}
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) List(java.util.List) 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)

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