Search in sources :

Example 46 with DMNRuntime

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

the class DMNDecisionTableHitPolicyTest method testSimpleDecisionTableHitPolicyOutputOrderMultipleOutputs.

@Test
public void testSimpleDecisionTableHitPolicyOutputOrderMultipleOutputs() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0004-simpletable-O-multiple-outputs.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0004-simpletable-O-multiple-outputs");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = getSimpleTableContext(BigDecimal.valueOf(18), "Medium", true);
    final DMNContext result = evaluateSimpleTableWithContext(dmnModel, runtime, context);
    final List<Map<String, String>> decisionResult = (List<Map<String, String>>) result.get("Decision Result");
    assertThat(decisionResult, hasSize(4));
    // Must be ordered, so we can read from the list by index
    checkMultipleOutputResult(decisionResult.get(0), "Declined", "Needs verification");
    checkMultipleOutputResult(decisionResult.get(1), "Declined", "Decision final");
    checkMultipleOutputResult(decisionResult.get(2), "Approved", "Needs verification");
    checkMultipleOutputResult(decisionResult.get(3), "Approved", "Decision final");
}
Also used : DMNContext(org.kie.dmn.api.core.DMNContext) List(java.util.List) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Map(java.util.Map) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 47 with DMNRuntime

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

the class DMNDecisionTableHitPolicyTest method testSimpleDecisionTableHitPolicyCollectSumMultipleOutputs.

@Test
public void testSimpleDecisionTableHitPolicyCollectSumMultipleOutputs() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0004-simpletable-C-sum-multiple-outputs.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0004-simpletable-C-sum-multiple-outputs");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = getSimpleTableContext(BigDecimal.valueOf(70), "Medium", true);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    final DMNContext result = dmnResult.getContext();
    final Map<String, Object> decisionResult = (Map<String, Object>) result.get("Decision Result");
    assertThat(decisionResult.values(), hasSize(2));
    assertThat(decisionResult, hasEntry("Value1", BigDecimal.valueOf(25)));
    assertThat(decisionResult, hasEntry("Value2", BigDecimal.valueOf(32)));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Map(java.util.Map) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 48 with DMNRuntime

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

the class DMNDecisionTableHitPolicyTest method testSimpleDecisionTableHitPolicyUnique.

@Test
public void testSimpleDecisionTableHitPolicyUnique() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0004-simpletable-U.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0004-simpletable-U");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = getSimpleTableContext(BigDecimal.valueOf(18), "Medium", true);
    final DMNContext result = evaluateSimpleTableWithContext(dmnModel, runtime, context);
    assertThat(result.get("Approval Status"), is("Approved"));
}
Also used : 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 DMNRuntime

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

the class DMNDecisionTableHitPolicyTest method testSimpleDecisionTableHitPolicyAny.

private void testSimpleDecisionTableHitPolicyAny(final String resurceName, final String modelName, final boolean equalRules) {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime(resurceName, this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", modelName);
    assertThat(dmnModel, notNullValue());
    final DMNContext context = getSimpleTableContext(BigDecimal.valueOf(18), "Medium", true);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    final DMNContext result = dmnResult.getContext();
    if (equalRules) {
        assertThat(result.get("Approval Status"), is("Approved"));
    } else {
        assertThat(dmnResult.hasErrors(), is(true));
        assertThat((String) result.get("Approval Status"), isEmptyOrNullString());
    }
}
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)

Example 50 with DMNRuntime

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

the class DMNDecisionTableRuntimeTest method testDecisionTableWithCalculatedResult_parallel.

@Test(timeout = 30_000L)
public void testDecisionTableWithCalculatedResult_parallel() throws Throwable {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("calculation1.dmn", this.getClass());
    Runnable task = () -> {
        checkDecisionTableWithCalculatedResult(runtime);
    };
    List<Throwable> problems = Collections.synchronizedList(new ArrayList<>());
    List<CompletableFuture<Void>> tasks = new ArrayList<>();
    for (int i = 0; i < 10_000; i++) {
        CompletableFuture<Void> newtask = CompletableFuture.runAsync(task).exceptionally(t -> {
            problems.add(t);
            return null;
        });
        tasks.add(newtask);
    }
    CompletableFuture.allOf(tasks.toArray(new CompletableFuture<?>[] {})).get();
    for (Throwable t : problems) {
        throw t;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) 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