Search in sources :

Example 31 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testListBasic.

@Test
public void testListBasic() {
    final DMNRuntime runtime = createRuntime("listBasic.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_B84B17F3-3E84-4DED-996E-AA630A6BF9C4", "new-file");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("listNumber", Arrays.asList(1, 2, 3));
    context.set("vowel", "e");
    context.set("listVowel", Arrays.asList("a", "e"));
    context.set("justA", "a");
    context.set("listOfA", Arrays.asList("a"));
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("DecisionListNumber").getResult(), is(new BigDecimal(3)));
    assertThat(dmnResult.getDecisionResultByName("DecisionVowel").getResult(), is("the e"));
    assertThat(dmnResult.getDecisionResultByName("DecisionListVowel").getResult(), is(new BigDecimal(2)));
    assertThat(dmnResult.getDecisionResultByName("DecisionJustA").getResult(), is("the a"));
    assertThat(dmnResult.getDecisionResultByName("DecisionListOfA").getResult(), is(new BigDecimal(1)));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("DecisionListNumber"), is(new BigDecimal(3)));
        assertThat(allProperties.get("DecisionVowel"), is("the e"));
        assertThat(allProperties.get("DecisionListVowel"), is(new BigDecimal(2)));
        assertThat(allProperties.get("DecisionJustA"), is("the a"));
        assertThat(allProperties.get("DecisionListOfA"), is(new BigDecimal(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) 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 32 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testComponentCollection.

@Test
public void testComponentCollection() {
    final DMNRuntime runtime = createRuntime("collections.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_2A93F258-EF3B-4150-A202-1D02A893DF2B", "collections");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    final Map<String, Object> addr1 = prototype(entry("city", "city1"), entry("street", "street1"));
    final Map<String, Object> addr2 = prototype(entry("city", "city2"), entry("street", "street2"));
    final Map<String, Object> person = prototype(entry("name", "John"), entry("addressList", Arrays.asList(addr1, addr2)));
    context.set("inputPerson", 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 typedOutputPerson = (FEELPropertyAccessible) allProperties.get("outputPerson");
        assertThat(typedOutputPerson.getClass().getSimpleName(), is("TPerson"));
        assertThat(typedOutputPerson.getFEELProperty("name").toOptional().get(), is("Paul"));
        List<FEELPropertyAccessible> addressList = (List<FEELPropertyAccessible>) typedOutputPerson.getFEELProperty("addressList").toOptional().get();
        FEELPropertyAccessible typedOutputAddr1 = addressList.get(0);
        FEELPropertyAccessible typedOutputAddr2 = addressList.get(1);
        assertThat(typedOutputAddr1.getFEELProperty("city").toOptional().get(), anyOf(is("cityA"), is("cityB")));
        assertThat(typedOutputAddr1.getFEELProperty("street").toOptional().get(), anyOf(is("streetA"), is("streetB")));
        assertThat(typedOutputAddr2.getFEELProperty("city").toOptional().get(), anyOf(is("cityA"), is("cityB")));
        assertThat(typedOutputAddr2.getFEELProperty("street").toOptional().get(), anyOf(is("streetA"), is("streetB")));
    } else {
        Map<String, Object> outputPerson = (Map<String, Object>) dmnResult.getContext().get("outputPerson");
        assertThat(outputPerson.get("name"), is("Paul"));
        Map<String, Object> outputAddr1 = (Map<String, Object>) ((List) outputPerson.get("addressList")).get(0);
        Map<String, Object> outputAddr2 = (Map<String, Object>) ((List) outputPerson.get("addressList")).get(1);
        assertThat(outputAddr1.get("city"), anyOf(is("cityA"), is("cityB")));
        assertThat(outputAddr1.get("street"), anyOf(is("streetA"), is("streetB")));
        assertThat(outputAddr2.get("city"), anyOf(is("cityA"), is("cityB")));
        assertThat(outputAddr2.get("street"), anyOf(is("streetA"), is("streetB")));
    }
}
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) 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 33 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testRecursiveEmployee.

@Test
public void testRecursiveEmployee() {
    final DMNRuntime runtime = createRuntime("recursiveEmployee.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_d1e3d83e-230d-42fb-bc58-313463f7f40b", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    Map<String, Object> report1 = mapOf(entry("full name", "Bob"), entry("age", new BigDecimal(48)), // in FEEL there cannot be recursion in values, only in type definitions; these nulls are expected.
    entry("manager", null), entry("direct reports", null));
    Map<String, Object> report2 = mapOf(entry("full name", "Carl"), entry("age", new BigDecimal(49)), entry("manager", null), entry("direct reports", null));
    Map<String, Object> mgr = mapOf(entry("full name", "John's Manager"), entry("age", new BigDecimal(46)), entry("manager", null), entry("direct reports", null));
    Map<String, Object> john = mapOf(entry("full name", "John Doe"), entry("age", new BigDecimal(47)), entry("manager", mgr), entry("direct reports", Arrays.asList(report1, report2)));
    final DMNContext context = DMNFactory.newContext();
    context.set("an Employee", john);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("highlights").getResult(), is("John Doe: reports to John's Manager and is manager of 2 : [ Bob, Carl ]"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("highlights"), is("John Doe: reports to John's Manager and is manager of 2 : [ Bob, Carl ]"));
    }
}
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 34 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testBKM.

@Test
public void testBKM() {
    final DMNRuntime runtime = createRuntime("0009-invocation-arithmetic.dmn", DMNRuntimeTest.class);
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_cb28c255-91cd-4c01-ac7b-1a9cb1ecdb11", "literal invocation1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final Map<String, Object> loan = new HashMap<>();
    loan.put("amount", BigDecimal.valueOf(600000));
    loan.put("rate", new BigDecimal("0.0375"));
    loan.put("term", BigDecimal.valueOf(360));
    final DMNContext context = DMNFactory.newContext();
    context.set("fee", 100);
    context.set("Loan", loan);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    assertThat(((BigDecimal) dmnResult.getContext().get("MonthlyPayment")).setScale(8, BigDecimal.ROUND_DOWN), is(new BigDecimal("2878.69354943277").setScale(8, BigDecimal.ROUND_DOWN)));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        Object fee = allProperties.get("fee");
        assertThat(fee, is(100));
        FEELPropertyAccessible loanOut = (FEELPropertyAccessible) allProperties.get("Loan");
        assertThat(loanOut.getClass().getSimpleName(), is("TLoan"));
        assertThat(loanOut.getFEELProperty("amount").toOptional().get(), is(BigDecimal.valueOf(600000)));
        assertThat(loanOut.getFEELProperty("rate").toOptional().get(), is(new BigDecimal("0.0375")));
        assertThat(loanOut.getFEELProperty("term").toOptional().get(), is(BigDecimal.valueOf(360)));
        Object bkm = allProperties.get("PMT");
        assertThat(bkm, instanceOf(FEELFunction.class));
        assertThat(((FEELFunction) bkm).getName(), is("PMT"));
        Object monthlyPayment = allProperties.get("MonthlyPayment");
        assertThat(((BigDecimal) monthlyPayment).setScale(8, BigDecimal.ROUND_DOWN), is(new BigDecimal("2878.69354943277").setScale(8, BigDecimal.ROUND_DOWN)));
    }
}
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) 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) 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 35 with FEELPropertyAccessible

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

the class DMNRuntimeTypesTest method testShareTypeForInputAndOutput.

@Test
public void testShareTypeForInputAndOutput() {
    final DMNRuntime runtime = createRuntime("shareTypeForInputAndOutput.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_DBEFBA7B-C568-4631-A89E-AA31F7C6564B", "shareTypeForInputAndOutput");
    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("age", 28);
    person.put("employmentPeriod", Period.of(1, 2, 1));
    final DMNContext context = DMNFactory.newContext();
    context.set("inputPerson", 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("outputPerson");
        assertThat(myPersonOut.getClass().getSimpleName(), is("TPerson"));
        assertThat(myPersonOut.getFEELProperty("name").toOptional().get(), is("Paul"));
        assertThat(myPersonOut.getFEELProperty("age").toOptional().get(), is(new BigDecimal(20)));
        assertThat(myPersonOut.getFEELProperty("employmentPeriod").toOptional().get(), is(ComparablePeriod.of(1, 3, 1)));
    } else {
        Map<String, Object> outputPerson = (Map<String, Object>) dmnResult.getContext().get("outputPerson");
        assertThat(outputPerson.get("name"), is("Paul"));
        assertThat(outputPerson.get("age"), is(new BigDecimal(20)));
        assertThat(outputPerson.get("employmentPeriod"), is(ComparablePeriod.of(1, 3, 1)));
    }
}
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) 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)

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