Search in sources :

Example 16 with DMNContextFPAImpl

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

the class DMNRuntimeTypesTest method testJavaKeywords.

@Test
public void testJavaKeywords() {
    final DMNRuntime runtime = createRuntime("javaKeywords.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_C41C1676-0DA9-47EA-90AD-F9BAA257129F", "A1B1A8AD-B0DC-453D-86A7-C9475450C982");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    Map<String, Object> aThing = mapOf(entry("name", "name"), entry("const", "const"), entry("class", "class"));
    context.set("a thing", aThing);
    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("nameconstclass"));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        assertThat(allProperties.get("Decision-1"), is("nameconstclass"));
    }
}
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)

Example 17 with DMNContextFPAImpl

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

the class DMNRuntimeTypesTest method testEvaluateByIdAndName.

@Test
public void testEvaluateByIdAndName() {
    final DMNRuntime runtime = createRuntimeWithAdditionalResources("2decisions.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_6453A539-85B5-4A4E-800E-6721C50B6B55", "2decisions");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("InputData-1", mapOf(entry("name", "John"), entry("age", 30)));
    final DMNResult dmnResult1 = evaluateById(runtime, dmnModel, context, "_0BD595AB-B8C6-4FBF-B2DD-BEB49420EDFE");
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult1.getMessages()), dmnResult1.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult1.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        FEELPropertyAccessible person = (FEELPropertyAccessible) allProperties.get("Decision-1");
        assertThat(person.getClass().getSimpleName(), is("TPerson"));
        assertThat(person.getFEELProperty("name").toOptional().get(), is("Paul"));
        assertThat(person.getFEELProperty("age").toOptional().get(), is(EvalHelper.coerceNumber(28)));
        assertThat(allProperties.get("Decision-2"), nullValue());
    } else {
        Map<String, Object> person = (Map<String, Object>) dmnResult1.getContext().get("Decision-1");
        assertThat(person.get("name"), is("Paul"));
        assertThat(person.get("age"), is(EvalHelper.coerceNumber(28)));
        assertThat(dmnResult1.getContext().get("Decision-2"), nullValue());
    }
    final DMNResult dmnResult2 = evaluateByName(runtime, dmnModel, context, "Decision-2");
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult2.getMessages()), dmnResult2.hasErrors(), is(false));
    if (isTypeSafe()) {
        FEELPropertyAccessible outputSet = ((DMNContextFPAImpl) dmnResult2.getContext()).getFpa();
        Map<String, Object> allProperties = outputSet.allFEELProperties();
        FEELPropertyAccessible person = (FEELPropertyAccessible) allProperties.get("Decision-2");
        assertThat(person.getClass().getSimpleName(), is("TPerson"));
        assertThat(person.getFEELProperty("name").toOptional().get(), is("George"));
        assertThat(person.getFEELProperty("age").toOptional().get(), is(EvalHelper.coerceNumber(27)));
        assertThat(allProperties.get("Decision-1"), nullValue());
    } else {
        Map<String, Object> person = (Map<String, Object>) dmnResult2.getContext().get("Decision-2");
        assertThat(person.get("name"), is("George"));
        assertThat(person.get("age"), is(EvalHelper.coerceNumber(27)));
        assertThat(dmnResult2.getContext().get("Decision-1"), 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) 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 18 with DMNContextFPAImpl

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

the class AnnotationsTest method testNextDays.

@Test
public void testNextDays() throws Exception {
    final DMNRuntime runtime = createRuntime("nextDays.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://kiegroup.org/dmn/_8A1F9719-02AA-4517-97D4-5C4F5D22FE82", "nextDays");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    if (!isTypeSafe()) {
        context.set("few dates", Arrays.asList(LocalDate.of(2019, 12, 31), LocalDate.of(2020, 2, 21)));
    } else {
        JsonMapper mapper = JsonMapper.builder().addModule(new JavaTimeModule()).build();
        final String JSON = "{\n" + "    \"few dates\": [ \"2019-12-31\", \"2020-02-21\" ]\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("Decision-1").getResult(), is(Arrays.asList(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 2, 22))));
    if (strongly) {
        Class<?> inputSetClass = getStronglyClassByName(dmnModel, "InputSet");
        Field directionAsField = inputSetClass.getDeclaredField("few_32dates");
        org.eclipse.microprofile.openapi.annotations.media.Schema annMP = directionAsField.getDeclaredAnnotation(org.eclipse.microprofile.openapi.annotations.media.Schema.class);
        Assertions.assertThat(annMP).isNotNull();
        Assertions.assertThat(annMP.type()).isEqualTo(org.eclipse.microprofile.openapi.annotations.enums.SchemaType.ARRAY);
        io.swagger.v3.oas.annotations.media.Schema annIOSwagger = directionAsField.getDeclaredAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
        Assertions.assertThat(annIOSwagger).isNotNull();
        Assertions.assertThat(annIOSwagger.type()).isEqualTo("array");
    }
}
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) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Field(java.lang.reflect.Field) DMNModel(org.kie.dmn.api.core.DMNModel) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) Test(org.junit.Test)

Example 19 with DMNContextFPAImpl

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

the class DMNTypeSafeTest method testDynamic.

@Test
public void testDynamic() throws Exception {
    assertValidDmnModel(dmnModel);
    Map<String, Class<?>> classes = generateSourceCodeAndCreateInput(dmnModel, modelFactory, this.getClass().getClassLoader());
    FEELPropertyAccessible context = createInstanceFromCompiledClasses(classes, packageName, "OutputSet");
    Map<String, Object> inputSetMap = new HashMap<>();
    inputSetMap.put("p", mapOf(entry("age", new BigDecimal(35)), entry("name", "Mr. x"), entry("addresses", asList(mapOf(entry("streetName", "Street1"), entry("streetNumber", 1)), mapOf(entry("streetName", "Street2"), entry("streetNumber", 2))))));
    context.fromMap(inputSetMap);
    DMNResult evaluateAll = evaluateTyped(context, runtime, dmnModel);
    convertContext(evaluateAll, createInstanceFromCompiledClasses(classes, 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(context.getFEELProperty("p").toOptional().get()));
    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(context.getFEELProperty("p").toOptional().get()));
}
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) BigDecimal(java.math.BigDecimal) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) Test(org.junit.Test)

Example 20 with DMNContextFPAImpl

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

the class DMNTypeSafeTest method testMetadata.

@Test
public void testMetadata() throws Exception {
    assertValidDmnModel(dmnModel);
    Map<String, Class<?>> classes = generateSourceCodeAndCreateInput(dmnModel, modelFactory, this.getClass().getClassLoader());
    FEELPropertyAccessible feelPropertyAccessibleContext = createInstanceFromCompiledClasses(classes, packageName, "InputSet");
    String metadataKey = "test";
    String metadataValue = "value";
    DMNContext context = new DMNContextFPAImpl(feelPropertyAccessibleContext);
    context.getMetadata().set(metadataKey, metadataValue);
    assertEquals(metadataValue, context.getMetadata().get(metadataKey));
    assertEquals(metadataValue, context.clone().getMetadata().get(metadataKey));
}
Also used : DMNContextFPAImpl(org.kie.dmn.core.impl.DMNContextFPAImpl) FEELPropertyAccessible(org.kie.dmn.api.core.FEELPropertyAccessible) DMNContext(org.kie.dmn.api.core.DMNContext) BaseVariantTest(org.kie.dmn.core.BaseVariantTest) Test(org.junit.Test)

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