Search in sources :

Example 1 with Indexed

use of org.kie.dmn.feel.runtime.decisiontables.Indexed in project drools by kiegroup.

the class Results method applyHitPolicy.

public Object applyHitPolicy(EvaluationContext evaluationContext, HitPolicy hitPolicy, DecisionTable decisionTable) {
    if (items.hasNoIndexes()) {
        // }
        if (hitPolicy.getDefaultValue() != null) {
            return hitPolicy.getDefaultValue();
        }
        events.add(new HitPolicyViolationEvent(FEELEvent.Severity.WARN, String.format("No rule matched for decision table '%s' and no default values were defined. Setting result to null.", decisionTable.getName()), decisionTable.getName(), Collections.emptyList()));
    }
    List<? extends Indexed> matchIndexes = items.matches();
    evaluationContext.notifyEvt(() -> {
        List<Integer> matchedIndexes = matchIndexes.stream().map(dr -> dr.getIndex() + 1).collect(Collectors.toList());
        return new DecisionTableRulesMatchedEvent(FEELEvent.Severity.INFO, String.format("Rules matched for decision table '%s': %s", decisionTable.getName(), matchIndexes), decisionTable.getName(), decisionTable.getName(), matchedIndexes);
    });
    List<Object> resultObjects = items.evaluateResults(evaluationContext);
    Map<Integer, String> errorMessages = checkResults(decisionTable.getOutputs(), evaluationContext, matchIndexes, resultObjects);
    if (!errorMessages.isEmpty()) {
        List<Integer> offending = new ArrayList<>(errorMessages.keySet());
        events.add(new HitPolicyViolationEvent(FEELEvent.Severity.ERROR, String.format("Errors found evaluating decision table '%s': \n%s", decisionTable.getName(), String.join("\n", errorMessages.values())), decisionTable.getName(), offending));
        return null;
    }
    return hitPolicy.getDti().dti(evaluationContext, decisionTable, matchIndexes, resultObjects);
}
Also used : HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) DecisionTableImpl.checkResults(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl.checkResults) DecisionTableRulesMatchedEvent(org.kie.dmn.feel.runtime.events.DecisionTableRulesMatchedEvent) DecisionTable(org.kie.dmn.feel.runtime.decisiontables.DecisionTable) HitPolicy(org.kie.dmn.feel.runtime.decisiontables.HitPolicy) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Function1(org.drools.model.functions.Function1) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) Map(java.util.Map) Collections(java.util.Collections) Indexed(org.kie.dmn.feel.runtime.decisiontables.Indexed) DecisionTableRulesMatchedEvent(org.kie.dmn.feel.runtime.events.DecisionTableRulesMatchedEvent) ArrayList(java.util.ArrayList) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 Function1 (org.drools.model.functions.Function1)1 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)1 EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)1 DecisionTable (org.kie.dmn.feel.runtime.decisiontables.DecisionTable)1 DecisionTableImpl.checkResults (org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl.checkResults)1 HitPolicy (org.kie.dmn.feel.runtime.decisiontables.HitPolicy)1 Indexed (org.kie.dmn.feel.runtime.decisiontables.Indexed)1 DecisionTableRulesMatchedEvent (org.kie.dmn.feel.runtime.events.DecisionTableRulesMatchedEvent)1 HitPolicyViolationEvent (org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent)1