Search in sources :

Example 1 with DMNRuntimeEventListener

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

the class DMNDecisionTableRuntimeTest method testDecisionTableDefaultValue.

@Test
public void testDecisionTableDefaultValue() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("decisiontable-default-value.dmn", this.getClass());
    final DMNRuntimeEventListener listener = Mockito.mock(DMNRuntimeEventListener.class);
    runtime.addListener(listener);
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "decisiontable-default-value");
    assertThat(dmnModel, notNullValue());
    assertThat(dmnModel.getMessages().toString(), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("Age", new BigDecimal(16));
    context.set("RiskCategory", "Medium");
    context.set("isAffordable", true);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.getMessages().toString(), dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat(result.get("Approval Status"), is("Declined"));
    final ArgumentCaptor<AfterEvaluateDecisionTableEvent> captor = ArgumentCaptor.forClass(AfterEvaluateDecisionTableEvent.class);
    verify(listener).afterEvaluateDecisionTable(captor.capture());
    assertThat(captor.getValue().getMatches(), is(empty()));
    assertThat(captor.getValue().getSelected(), is(empty()));
}
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) BigDecimal(java.math.BigDecimal) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) Test(org.junit.Test)

Example 2 with DMNRuntimeEventListener

use of org.kie.dmn.api.core.event.DMNRuntimeEventListener 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 3 with DMNRuntimeEventListener

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

the class DMNDecisionTableRuntimeTest method testTwoDecisionTables.

@Test
public void testTwoDecisionTables() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("two_decision_tables.dmn", this.getClass());
    final DMNRuntimeEventListener listener = Mockito.mock(DMNRuntimeEventListener.class);
    runtime.addListener(listener);
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_bbb692e7-3d95-407a-bf39-353085bf57f0", "Invocation with two decision table as parameters");
    assertThat(dmnModel, notNullValue());
    assertThat(dmnModel.getMessages().toString(), dmnModel.hasErrors(), is(false));
    final DMNContext context = DMNFactory.newContext();
    context.set("Number", 50);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(dmnResult.getMessages().toString(), dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat((Map<String, Object>) result.get("Decision Logic 2"), hasEntry("the 5 analysis", "A number greater than 5"));
    assertThat((Map<String, Object>) result.get("Decision Logic 2"), hasEntry("the 100 analysis", "A number smaller than 100"));
    final ArgumentCaptor<AfterEvaluateDecisionTableEvent> captor = ArgumentCaptor.forClass(AfterEvaluateDecisionTableEvent.class);
    verify(listener, times(2)).afterEvaluateDecisionTable(captor.capture());
    assertThat(captor.getAllValues().get(0).getDecisionTableName(), is("a"));
    assertThat(captor.getAllValues().get(1).getDecisionTableName(), is("b"));
}
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 4 with DMNRuntimeEventListener

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

the class DMNRuntimeTest method testEventListeners.

@Test
public void testEventListeners() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("car_damage_responsibility.dmn", this.getClass());
    DMNRuntimeEventListener listener = mock(DMNRuntimeEventListener.class);
    runtime.addListener(listener);
    runtime.addListener(DMNRuntimeUtil.createListener());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_820611e9-c21c-47cd-8e52-5cba2be9f9cc", "Car Damage Responsibility");
    assertThat(dmnModel, notNullValue());
    DMNContext context = DMNFactory.newContext();
    context.set("Membership Level", "Silver");
    context.set("Damage Types", "Body");
    context.set("Responsible", "Driver");
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    ArgumentCaptor<AfterEvaluateDecisionTableEvent> argument = ArgumentCaptor.forClass(AfterEvaluateDecisionTableEvent.class);
    verify(listener, times(2)).beforeEvaluateDecision(any(BeforeEvaluateDecisionEvent.class));
    verify(listener, times(2)).afterEvaluateDecision(any(AfterEvaluateDecisionEvent.class));
    verify(listener, times(2)).beforeEvaluateDecisionTable(any(BeforeEvaluateDecisionTableEvent.class));
    verify(listener, times(2)).afterEvaluateDecisionTable(argument.capture());
    AfterEvaluateDecisionTableEvent dte = argument.getAllValues().get(0);
    assertThat(dte.getDecisionTableName(), is("Car Damage Responsibility"));
    // rows are 1-based
    assertThat(dte.getMatches(), is(Arrays.asList(5)));
    dte = argument.getAllValues().get(1);
    assertThat(dte.getDecisionTableName(), is("Payment method"));
    // rows are 1-based
    assertThat(dte.getMatches(), is(Arrays.asList(3)));
    assertThat(dmnResult.hasErrors(), is(false));
    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"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) BeforeEvaluateDecisionEvent(org.kie.dmn.api.core.event.BeforeEvaluateDecisionEvent) AfterEvaluateDecisionEvent(org.kie.dmn.api.core.event.AfterEvaluateDecisionEvent) BeforeEvaluateDecisionTableEvent(org.kie.dmn.api.core.event.BeforeEvaluateDecisionTableEvent) DMNContext(org.kie.dmn.api.core.DMNContext) AfterEvaluateDecisionTableEvent(org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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)

Aggregations

Test (org.junit.Test)4 DMNContext (org.kie.dmn.api.core.DMNContext)4 DMNModel (org.kie.dmn.api.core.DMNModel)4 DMNResult (org.kie.dmn.api.core.DMNResult)4 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)4 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)4 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)4 BigDecimal (java.math.BigDecimal)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 AfterEvaluateDecisionEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionEvent)1 BeforeEvaluateDecisionEvent (org.kie.dmn.api.core.event.BeforeEvaluateDecisionEvent)1 BeforeEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.BeforeEvaluateDecisionTableEvent)1