Search in sources :

Example 11 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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 DMNContext

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

the class DMNRuntimeTest method testDROOLS2286bis.

@Test
public void testDROOLS2286bis() {
    // DROOLS-2286 (map case)
    Map<String, Object> johnCena = prototype(entry("name", "John"), entry("surname", "Cena"));
    Map<String, Object> leslieBrown = prototype(entry("name", "Leslie"), entry("surname", "Brown"));
    Map<String, Object> johnWick = prototype(entry("name", "John"), entry("surname", "Wick"));
    List<Map<String, Object>> personList = new ArrayList<>();
    personList.add(johnCena);
    personList.add(leslieBrown);
    personList.add(johnWick);
    DMNContext context = DMNFactory.newContext();
    context.set("PersonList", personList);
    assertDROOLS2286(context);
}
Also used : ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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 14 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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 15 with DMNContext

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

Aggregations

DMNContext (org.kie.dmn.api.core.DMNContext)142 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)130 DMNModel (org.kie.dmn.api.core.DMNModel)129 Test (org.junit.Test)127 DMNResult (org.kie.dmn.api.core.DMNResult)123 HashMap (java.util.HashMap)24 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)24 BigDecimal (java.math.BigDecimal)23 ArrayList (java.util.ArrayList)20 List (java.util.List)20 Map (java.util.Map)20 KieServices (org.kie.api.KieServices)19 KieContainer (org.kie.api.runtime.KieContainer)19 DMNMessage (org.kie.dmn.api.core.DMNMessage)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)11 CoreMatchers.is (org.hamcrest.CoreMatchers.is)10 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)10 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)10 Collection (java.util.Collection)8 UUID (java.util.UUID)8