Search in sources :

Example 1 with HitPolicyViolationEvent

use of org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent in project drools by kiegroup.

the class DMNDecisionTableHitPolicyTest method testSimpleDecisionTableHitPolicyUniqueNullWarn.

@Test
public void testSimpleDecisionTableHitPolicyUniqueNullWarn() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0004-simpletable-U-noinputvalues.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0004-simpletable-U-noinputvalues");
    assertThat(dmnModel, notNullValue());
    final DMNContext context = getSimpleTableContext(BigDecimal.valueOf(18), "ASD", false);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    final DMNContext result = dmnResult.getContext();
    assertThat(result.get("Approval Status"), nullValue());
    assertTrue(dmnResult.getMessages().size() > 0);
    assertTrue(dmnResult.getMessages().stream().anyMatch(dm -> dm.getSeverity().equals(DMNMessage.Severity.WARN) && dm.getFeelEvent() instanceof HitPolicyViolationEvent && dm.getFeelEvent().getSeverity().equals(FEELEvent.Severity.WARN)));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) DMNFactory(org.kie.dmn.core.api.DMNFactory) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) Test(org.junit.Test) DMNRuntimeUtil(org.kie.dmn.core.util.DMNRuntimeUtil) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) BigDecimal(java.math.BigDecimal) List(java.util.List) DMNModel(org.kie.dmn.api.core.DMNModel) Matchers.contains(org.hamcrest.Matchers.contains) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map) DMNContext(org.kie.dmn.api.core.DMNContext) Matchers.hasSize(org.hamcrest.Matchers.hasSize) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Assert(org.junit.Assert) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) 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) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) Test(org.junit.Test)

Example 2 with HitPolicyViolationEvent

use of org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent in project drools by kiegroup.

the class DecisionTableImpl method evaluate.

/**
 * Evaluates this decision table returning the result
 * @param ctx
 * @param params these are the required information items, not to confuse with the columns of the
 *               decision table that are expressions derived from these parameters
 * @return
 */
public FEELFnResult<Object> evaluate(EvaluationContext ctx, Object[] params) {
    if (decisionRules.isEmpty()) {
        return FEELFnResult.ofError(new FEELEventBase(Severity.WARN, "Decision table is empty", null));
    }
    Object[] actualInputs = resolveActualInputs(ctx, feel);
    Either<FEELEvent, Object> actualInputMatch = actualInputsMatchInputValues(ctx, actualInputs);
    if (actualInputMatch.isLeft()) {
        return actualInputMatch.cata(e -> FEELFnResult.ofError(e), e -> FEELFnResult.ofError(null));
    }
    List<DTDecisionRule> matches = findMatches(ctx, actualInputs);
    if (!matches.isEmpty()) {
        List<Object> results = evaluateResults(ctx, feel, actualInputs, matches);
        Map<Integer, String> msgs = checkResults(ctx, matches, results);
        if (msgs.isEmpty()) {
            Object result = hitPolicy.getDti().dti(ctx, this, actualInputs, matches, results);
            return FEELFnResult.ofResult(result);
        } else {
            List<Integer> offending = msgs.keySet().stream().collect(Collectors.toList());
            return FEELFnResult.ofError(new HitPolicyViolationEvent(Severity.ERROR, "Errors found evaluating decision table '" + getName() + "': \n" + (msgs.values().stream().collect(Collectors.joining("\n"))), name, offending));
        }
    } else {
        // check if there is a default value set for the outputs
        if (hasDefaultValues) {
            Object result = defaultToOutput(ctx, feel);
            return FEELFnResult.ofResult(result);
        } else {
            if (hitPolicy.getDefaultValue() != null) {
                return FEELFnResult.ofResult(hitPolicy.getDefaultValue());
            }
            return FEELFnResult.ofError(new HitPolicyViolationEvent(Severity.WARN, "No rule matched for decision table '" + name + "' and no default values were defined. Setting result to null.", name, Collections.EMPTY_LIST));
        }
    }
}
Also used : FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent)

Example 3 with HitPolicyViolationEvent

use of org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent in project drools by kiegroup.

the class SimpleDecisionTablesTest method testMain.

@Test
public void testMain() {
    List<FEELEvent> events = new ArrayList<>();
    FEELEventListener listener = evt -> events.add(evt);
    feel.addListener(listener);
    String expression = loadExpression("simple_decision_tables.feel");
    Map context = (Map) feel.evaluate(expression);
    System.out.println(printContext(context));
    assertThat(context.get("result1"), is("Adult"));
    assertThat(context.get("result2"), is("Medium"));
    assertThat((Map<?, ?>) context.get("result3"), hasSize(2));
    assertThat((Map<?, ?>) context.get("result3"), hasEntry("Out1", "out1b"));
    assertThat((Map<?, ?>) context.get("result3"), hasEntry("Out2", "out2b"));
    assertThat((Map<?, ?>) context.get("result4"), hasSize(2));
    assertThat((Map<?, ?>) context.get("result4"), hasEntry("Out1", "io1a"));
    assertThat((Map<?, ?>) context.get("result4"), hasEntry("Out2", "io2a"));
    assertThat(context.get("result5"), nullValue());
    assertTrue(events.stream().anyMatch(e -> e instanceof HitPolicyViolationEvent && e.getSeverity().equals(Severity.WARN)));
    feel.removeListener(listener);
}
Also used : FEEL(org.kie.dmn.feel.FEEL) CoreMatchers(org.hamcrest.CoreMatchers) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) BigDecimal(java.math.BigDecimal) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map) IsCollectionWithSize(org.hamcrest.collection.IsCollectionWithSize) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Description(org.hamcrest.Description) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) Logger(org.slf4j.Logger) Severity(org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity) Collection(java.util.Collection) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) List(java.util.List) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matcher(org.hamcrest.Matcher) ArrayList(java.util.ArrayList) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) Test(org.junit.Test)

Aggregations

FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)3 HitPolicyViolationEvent (org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent)3 BigDecimal (java.math.BigDecimal)2 List (java.util.List)2 Map (java.util.Map)2 Matchers.hasEntry (org.hamcrest.Matchers.hasEntry)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 CoreMatchers (org.hamcrest.CoreMatchers)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)1 CoreMatchers.nullValue (org.hamcrest.CoreMatchers.nullValue)1 Description (org.hamcrest.Description)1 Matcher (org.hamcrest.Matcher)1 Matchers (org.hamcrest.Matchers)1 Matchers.contains (org.hamcrest.Matchers.contains)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1