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