Search in sources :

Example 41 with DMNContextFPAImpl

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

the class DMNRuntimeTypesTest method testOneOfEachType.

@Test
public void testOneOfEachType() throws Exception {
    final DMNRuntime runtime = createRuntime("OneOfEachType.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_4f5608e9-4d74-4c22-a47e-ab657257fc9c", "OneOfEachType");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    if (!isTypeSafe()) {
        context.set("InputString", "John Doe");
        context.set("InputNumber", BigDecimal.ONE);
        context.set("InputBoolean", true);
        context.set("InputDTDuration", Duration.parse("P1D"));
        context.set("InputYMDuration", Period.parse("P1M"));
        context.set("InputDateAndTime", LocalDateTime.of(2020, 4, 2, 9, 0));
        context.set("InputDate", LocalDate.of(2020, 4, 2));
        context.set("InputTime", LocalTime.of(9, 0));
    } else {
        JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        final String JSON = "{\n" + "    \"InputBoolean\": true,\n" + "    \"InputDTDuration\": \"P1D\",\n" + "    \"InputDate\": \"2020-04-02\",\n" + "    \"InputDateAndTime\": \"2020-04-02T09:00:00\",\n" + "    \"InputNumber\": 1,\n" + "    \"InputString\": \"John Doe\",\n" + "    \"InputTime\": \"09:00\",\n" + "    \"InputYMDuration\": \"P1M\"\n" + "}";
        Class<?> inputSetClass = getStronglyClassByName(dmnModel, "InputSet");
        FEELPropertyAccessible inputSet = (FEELPropertyAccessible) mapper.readValue(JSON, inputSetClass);
        context = new DMNContextFPAImpl(inputSet);
    }
    final DMNResult dmnResult = evaluateModel(runtime, dmnModel, context);
    LOG.debug("{}", dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    assertThat(dmnResult.getDecisionResultByName("DecisionString").getResult(), is("Hello, John Doe"));
    assertThat(dmnResult.getDecisionResultByName("DecisionNumber").getResult(), is(new BigDecimal(2)));
    assertThat(dmnResult.getDecisionResultByName("DecisionBoolean").getResult(), is(false));
    assertThat(dmnResult.getDecisionResultByName("DecisionDTDuration").getResult(), is(Duration.parse("P2D")));
    assertThat(dmnResult.getDecisionResultByName("DecisionYMDuration").getResult(), is(ComparablePeriod.parse("P2M")));
    assertThat(dmnResult.getDecisionResultByName("DecisionDateAndTime").getResult(), is(LocalDateTime.of(2020, 4, 2, 10, 0)));
    assertThat(dmnResult.getDecisionResultByName("DecisionDate").getResult(), is(LocalDate.of(2020, 4, 3)));
    assertThat(dmnResult.getDecisionResultByName("DecisionTime").getResult(), is(LocalTime.of(10, 0)));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("DecisionString"), is("Hello, John Doe"));
        assertThat(allProperties.get("DecisionNumber"), is(new BigDecimal(2)));
        assertThat(allProperties.get("DecisionBoolean"), is(false));
        assertThat(allProperties.get("DecisionDTDuration"), is(Duration.parse("P2D")));
        assertThat(allProperties.get("DecisionYMDuration"), is(ComparablePeriod.parse("P2M")));
        assertThat(allProperties.get("DecisionDateAndTime"), is(LocalDateTime.of(2020, 4, 2, 10, 0)));
        assertThat(allProperties.get("DecisionDate"), is(LocalDate.of(2020, 4, 3)));
        assertThat(allProperties.get("DecisionTime"), is(LocalTime.of(10, 0)));
    }
}
Also used : DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) DMNResult(org.kie.dmn.api.core.DMNResult) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) BigDecimal(java.math.BigDecimal) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) 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 42 with DMNContextFPAImpl

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

the class DMNRuntimeTypesTest method testFixInnerComposite.

@Test
public void testFixInnerComposite() {
    final DMNRuntime runtime = createRuntime("fixInnerComposite.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_E82058C1-27D3-44F3-B1B3-4C02D17B7A05", "new-file");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    Map<String, Object> employee = mapOf(entry("Name", "John Doe"), entry("Marital Status", "S"));
    context.set("InputData-1", employee);
    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 is S"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Decision-1"), is("John Doe is S"));
    }
}
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) 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