Search in sources :

Example 36 with DMNContextFPAImpl

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

Example 37 with DMNContextFPAImpl

use of org.kie.dmn.core.impl.DMNContextFPAImpl in project drools by kiegroup.

the class DMNRuntimeTypesTest method testInnerCompositeCollection.

@Test
public void testInnerCompositeCollection() {
    final DMNRuntime runtime = createRuntime("innerCompositeCollection.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_D8AE5AF4-1F9E-4423-873A-B8F3C3BE5FE5", "new-file");
    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))));
    Map<String, Object> person = mapOf(entry("full name", "John Doe"), entry("pairs", pairs));
    context.set("person", person);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("Decision-1").getResult(), is("John Doe has 3 pairs."));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Decision-1"), is("John Doe has 3 pairs."));
    }
}
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 38 with DMNContextFPAImpl

use of org.kie.dmn.core.impl.DMNContextFPAImpl in project drools by kiegroup.

the class DMNTypeSafeTest method test.

@Test
public void test() throws Exception {
    assertValidDmnModel(dmnModel);
    DMNAllTypesIndex index = new DMNAllTypesIndex(new DMNTypeSafePackageName.ModelFactory(), dmnModel);
    Map<String, String> allTypesSourceCode = new DMNTypeSafeTypeGenerator(dmnModel, index, modelFactory).processTypes().generateSourceCodeOfAllTypes();
    ClassLoader thisDMNClassLoader = this.getClass().getClassLoader();
    Map<String, Class<?>> compiledClasses = KieMemoryCompiler.compile(allTypesSourceCode, thisDMNClassLoader);
    FEELPropertyAccessible street1 = tAddress(compiledClasses, "Street1", 1);
    FEELPropertyAccessible street2 = tAddress(compiledClasses, "Street2", 2);
    FEELPropertyAccessible tPersonInstance = tPerson(compiledClasses, asList(street1, street2));
    FEELPropertyAccessible context = outputSet(compiledClasses, tPersonInstance);
    DMNResult evaluateAll = evaluateTyped(context, runtime, dmnModel);
    convertContext(evaluateAll, createInstanceFromCompiledClasses(compiledClasses, packageName, "OutputSet"));
    DMNContext result = evaluateAll.getContext();
    Map<String, Object> d = (Map<String, Object>) result.get("d");
    assertThat(d.get("Hello"), is("Hello Mr. x"));
    FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) result).getFpa();
    assertThat(outputSet.getFEELProperty("p").toOptional().get(), equalTo(tPersonInstance));
    Map<String, Object> dContext = (Map<String, Object>) outputSet.getFEELProperty("d").toOptional().get();
    assertThat(dContext.get("Hello"), is("Hello Mr. x"));
    assertThat(dContext.get("the person"), equalTo(tPersonInstance));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) DMNTypeSafeTypeGenerator(org.kie.dmn.typesafe.DMNTypeSafeTypeGenerator) DMNContext(org.kie.dmn.api.core.DMNContext) DMNAllTypesIndex(org.kie.dmn.typesafe.DMNAllTypesIndex) DMNTypeSafePackageName(org.kie.dmn.typesafe.DMNTypeSafePackageName) HashMap(java.util.HashMap) Map(java.util.Map) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) Test(org.junit.Test)

Example 39 with DMNContextFPAImpl

use of org.kie.dmn.core.impl.DMNContextFPAImpl in project drools by kiegroup.

the class DMN13specificTest method testDMNv1_3_ch11_asSpecInputDataValues.

@Test
public void testDMNv1_3_ch11_asSpecInputDataValues() {
    testName = "testDMNv1_3_ch11_asSpecInputDataValues";
    final DMNRuntime runtime = createRuntimeWithAdditionalResources("Chapter 11 Example.dmn", this.getClass(), "Financial.dmn");
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_9d01a0c4-f529-4ad8-ad8e-ec5fb5d96ad4", "Chapter 11 Example");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("Applicant data", mapOf(entry("Age", new BigDecimal(51)), // typo is present in DMNv1.3
    entry("MartitalStatus", "M"), entry("EmploymentStatus", "EMPLOYED"), entry("ExistingCustomer", false), entry("Monthly", mapOf(entry("Income", new BigDecimal(10_000)), entry("Repayments", new BigDecimal(2_500)), entry("Expenses", new BigDecimal(3_000))))));
    context.set("Bureau data", mapOf(entry("Bankrupt", false), entry("CreditScore", new BigDecimal(600))));
    context.set("Requested product", mapOf(entry("ProductType", "STANDARD LOAN"), entry("Rate", new BigDecimal(0.08)), entry("Term", new BigDecimal(36)), entry("Amount", new BigDecimal(100_000))));
    context.set("Supporting documents", null);
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat(result.get("Strategy"), is("THROUGH"));
    assertThat(result.get("Routing"), is("ACCEPT"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Strategy"), is("THROUGH"));
        assertThat(allProperties.get("Routing"), is("ACCEPT"));
    }
}
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) Test(org.junit.Test) BaseVariantTest(org.kie.dmn.core.BaseVariantTest)

Example 40 with DMNContextFPAImpl

use of org.kie.dmn.core.impl.DMNContextFPAImpl in project drools by kiegroup.

the class DMNRuntimeTypesTest method testListBasic_LOVerror.

@Test
public void testListBasic_LOVerror() {
    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));
    // fails allowedValues
    context.set("vowel", "x");
    // fails allowedValues of the inner type
    context.set("listVowel", Arrays.asList("a", "x"));
    // fails allowedValues
    context.set("justA", "e");
    // fails allowedValues of the inner type
    context.set("listOfA", Arrays.asList("e"));
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(true));
    assertThat(dmnResult.getDecisionResultByName("DecisionListNumber").getResult(), is(new BigDecimal(3)));
    assertThat(dmnResult.getDecisionResultByName("DecisionVowel").getEvaluationStatus(), not(DecisionEvaluationStatus.SUCCEEDED));
    assertThat(dmnResult.getDecisionResultByName("DecisionListVowel").getEvaluationStatus(), not(DecisionEvaluationStatus.SUCCEEDED));
    assertThat(dmnResult.getDecisionResultByName("DecisionJustA").getEvaluationStatus(), not(DecisionEvaluationStatus.SUCCEEDED));
    assertThat(dmnResult.getDecisionResultByName("DecisionListOfA").getEvaluationStatus(), not(DecisionEvaluationStatus.SUCCEEDED));
    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"), nullValue());
        assertThat(allProperties.get("DecisionListVowel"), nullValue());
        assertThat(allProperties.get("DecisionJustA"), nullValue());
        assertThat(allProperties.get("DecisionListOfA"), nullValue());
    }
}
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)

Aggregations

DMNContext (org.kie.dmn.api.core.DMNContext)42 DMNContextFPAImpl (org.kie.dmn.core.impl.DMNContextFPAImpl)42 FEELPropertyAccessible (org.kie.dmn.api.core.FEELPropertyAccessible)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