Search in sources :

Example 46 with DMNModel

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

the class DMNDecisionTableRuntimeTest method testDecisionTableMultipleResults.

@Test
public void testDecisionTableMultipleResults() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("car_damage_responsibility.dmn", this.getClass());
    final DMNRuntimeEventListener listener = Mockito.mock(DMNRuntimeEventListener.class);
    runtime.addListener(listener);
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_820611e9-c21c-47cd-8e52-5cba2be9f9cc", "Car Damage Responsibility");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = DMNFactory.newContext();
    context.set("Membership Level", "Silver");
    context.set("Damage Types", "Body");
    context.set("Responsible", "Driver");
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat((Map<String, Object>) result.get("Car Damage Responsibility"), hasEntry(is("EU Rent"), is(BigDecimal.valueOf(40))));
    assertThat((Map<String, Object>) result.get("Car Damage Responsibility"), hasEntry(is("Renter"), is(BigDecimal.valueOf(60))));
    assertThat(result.get("Payment method"), is("Check"));
    final ArgumentCaptor<AfterEvaluateDecisionTableEvent> captor = ArgumentCaptor.forClass(AfterEvaluateDecisionTableEvent.class);
    verify(listener, times(2)).afterEvaluateDecisionTable(captor.capture());
    final AfterEvaluateDecisionTableEvent first = captor.getAllValues().get(0);
    assertThat(first.getMatches(), is(Collections.singletonList(5)));
    assertThat(first.getSelected(), is(Collections.singletonList(5)));
    final AfterEvaluateDecisionTableEvent second = captor.getAllValues().get(1);
    assertThat(second.getMatches(), is(Collections.singletonList(3)));
    assertThat(second.getSelected(), is(Collections.singletonList(3)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) AfterEvaluateDecisionTableEvent(org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) Test(org.junit.Test)

Example 47 with DMNModel

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

the class DMNDecisionTableRuntimeTest method testDTInputExpressionLocalXmlnsInference.

@Test
public void testDTInputExpressionLocalXmlnsInference() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("drools1502-InputExpression.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("https://www.drools.org/kie-dmn/definitions", "definitions");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = runtime.newContext();
    context.set("MyInput", "a");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("MyDecision"), is("Decision taken"));
}
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 48 with DMNModel

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

the class DMNDecisionTableRuntimeTest method testSimpleDecisionTableMultipleOutputWrongOutputType.

@Test
public void testSimpleDecisionTableMultipleOutputWrongOutputType() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0004-simpletable-P-multiple-outputs-wrong-output.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0004-simpletable-P-multiple-outputs-wrong-output");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = DMNFactory.newContext();
    context.set("Age", BigDecimal.valueOf(18));
    context.set("RiskCategory", "Medium");
    context.set("isAffordable", true);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.hasErrors(), is(true));
    assertThat(dmnResult.getMessages().stream().filter(message -> message.getFeelEvent().getSourceException() instanceof NullPointerException).count(), is(0L));
}
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 49 with DMNModel

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

the class DMNDecisionTableRuntimeTest method testNullRelation.

@Test
public void testNullRelation() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("nullrelation.dmn", getClass());
    DMNModel model = runtime.getModel("http://www.trisotech.com/definitions/_946a2145-89ae-4197-88b4-40e6f88c8101", "Null in relations");
    assertThat(model, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(model.getMessages()), model.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("Value", "a3");
    DMNResult result = runtime.evaluateByName(model, context, "Mapping");
    assertThat(DMNRuntimeUtil.formatMessages(result.getMessages()), result.hasErrors(), is(false));
}
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 50 with DMNModel

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

the class DMNDecisionTableRuntimeTest method testDTInContext.

@Test
public void testDTInContext() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("DT_in_context.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_4acdcb25-b298-435e-abd5-efd00ed686a5", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    DMNContext context = DMNFactory.newContext();
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.getDecisionResults().size(), is(1));
    assertThat(dmnResult.getDecisionResultByName("D1").getResult(), is(instanceOf(Map.class)));
    DMNContext result = dmnResult.getContext();
    assertThat(((Map) result.get("D1")).get("Text color"), is("red"));
}
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)

Aggregations

DMNModel (org.kie.dmn.api.core.DMNModel)163 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)152 Test (org.junit.Test)143 DMNContext (org.kie.dmn.api.core.DMNContext)131 DMNResult (org.kie.dmn.api.core.DMNResult)123 BigDecimal (java.math.BigDecimal)26 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)22 List (java.util.List)22 Map (java.util.Map)22 KieServices (org.kie.api.KieServices)21 KieContainer (org.kie.api.runtime.KieContainer)21 DMNMessage (org.kie.dmn.api.core.DMNMessage)16 CoreMatchers.is (org.hamcrest.CoreMatchers.is)13 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)13 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)13 UUID (java.util.UUID)11 CoreMatchers.nullValue (org.hamcrest.CoreMatchers.nullValue)11