Search in sources :

Example 11 with DMNRuntime

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

the class DMNRuntimeTest method testSingletonlist_function_call.

@Test
public void testSingletonlist_function_call() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("singletonlist_fuction_call.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_0768879b-5ee1-410f-92f0-7732573b069d", "expression function subst [a] with a");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext ctx = runtime.newContext();
    ctx.set("InputLineItem", prototype(entry("Line", "0015"), entry("Description", "additional Battery")));
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("The Battery"), is(prototype(entry("Line", "0010"), entry("Description", "Battery"))));
    assertThat((List<?>) result.get("Remove Battery"), contains(prototype(entry("Line", "0020"), entry("Description", "Case")), prototype(entry("Line", "0030"), entry("Description", "Power Supply"))));
    assertThat((List<?>) result.get("Remove Battery"), not(contains(prototype(entry("Line", "0010"), entry("Description", "Battery")))));
    assertThat((List<?>) result.get("Insert before Line 0020"), contains(prototype(entry("Line", "0010"), entry("Description", "Battery")), prototype(entry("Line", "0015"), entry("Description", "additional Battery")), prototype(entry("Line", "0020"), entry("Description", "Case")), prototype(entry("Line", "0030"), entry("Description", "Power Supply"))));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 12 with DMNRuntime

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

the class DMNRuntimeTest method testFiltering.

@Test
public void testFiltering() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Person_filtering_by_age.dmn", getClass());
    runtime.addListener(DMNRuntimeUtil.createListener());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_e215ed7a-701b-4c53-b8df-4b4d23d5fe32", "Person filtering by age");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("Min Age", 50);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), ((List) dmnResult.getContext().get("Filtering")).size(), is(2));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 13 with DMNRuntime

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

the class DMNRuntimeTest method testPriority_table.

@Test
public void testPriority_table() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("priority_table.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_ff54a44d-b8f5-48fc-b2b7-43db767e8a1c", "not quite all or nothing P");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = runtime.newContext();
    context.set("isAffordable", false);
    context.set("RiskCategory", "Medium");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Approval Status"), is("Declined"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 14 with DMNRuntime

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

the class DMNRuntimeTest method testEx_6_1.

@Test
public void testEx_6_1() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Ex_6_1.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_5f1269c8-1e6f-4748-9eca-26aa1b1278ef", "Ex 6-1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext ctx = runtime.newContext();
    Map<String, Object> t1 = new HashMap<>();
    t1.put("city", "Los Angeles");
    t1.put("name", "Los Angeles");
    t1.put("wins", 0);
    t1.put("losses", 1);
    t1.put("bonus points", 40);
    Map<String, Object> t2 = new HashMap<>();
    t2.put("city", "San Francisco");
    t2.put("name", "San Francisco");
    t2.put("wins", 1);
    t2.put("losses", 0);
    t2.put("bonus points", 7);
    ctx.set("NBA Pacific", Arrays.asList(new Map[] { t1, t2 }));
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Number of distinct cities"), is(new BigDecimal(2)));
    assertThat(result.get("Second place losses"), is(new BigDecimal(0)));
    assertThat(result.get("Max wins"), is(new BigDecimal(1)));
    assertThat(result.get("Mean wins"), is(new BigDecimal(0.5)));
    assertThat((List<?>) result.get("Positions of Los Angeles teams"), contains(new BigDecimal(1)));
    assertThat(result.get("Number of teams"), is(new BigDecimal(2)));
    assertThat(result.get("Sum of bonus points"), is(new BigDecimal(47)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 15 with DMNRuntime

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

the class DMNRuntimeTest method testCycleDetectionDeadlyDiamond.

@Test
public void testCycleDetectionDeadlyDiamond() {
    DecisionNodeImpl a = new DecisionNodeImpl();
    DecisionNodeImpl b = new DecisionNodeImpl();
    DecisionNodeImpl c = new DecisionNodeImpl();
    DecisionNodeImpl d = new DecisionNodeImpl();
    a.addDependency("b", b);
    a.addDependency("c", c);
    b.addDependency("d", d);
    c.addDependency("d", d);
    DMNModelImpl model = new DMNModelImpl();
    model.addDecision(a);
    model.addDecision(b);
    model.addDecision(c);
    model.addDecision(d);
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime(this.getClass());
    DMNResult result = runtime.evaluateAll(model, DMNFactory.newContext());
    assertFalse(result.hasErrors());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Test(org.junit.Test)

Aggregations

DMNRuntime (org.kie.dmn.api.core.DMNRuntime)178 Test (org.junit.Test)164 DMNModel (org.kie.dmn.api.core.DMNModel)152 DMNContext (org.kie.dmn.api.core.DMNContext)132 DMNResult (org.kie.dmn.api.core.DMNResult)128 KieContainer (org.kie.api.runtime.KieContainer)34 KieServices (org.kie.api.KieServices)30 BigDecimal (java.math.BigDecimal)26 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26 HashMap (java.util.HashMap)25 List (java.util.List)22 ArrayList (java.util.ArrayList)21 Map (java.util.Map)21 DMNMessage (org.kie.dmn.api.core.DMNMessage)16 CoreMatchers.is (org.hamcrest.CoreMatchers.is)13 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)13 Results (org.kie.api.builder.Results)13 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)13 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)13