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)));
}
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));
}
}
}
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);
}
Aggregations