Search in sources :

Example 11 with DecisionNodeImpl

use of org.kie.dmn.core.ast.DecisionNodeImpl in project drools by kiegroup.

the class DMNRuntimeImpl method evaluateDecision.

private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode d, boolean typeCheck) {
    DecisionNodeImpl decision = (DecisionNodeImpl) d;
    if (result.getContext().isDefined(decision.getName())) {
        // already resolved
        return true;
    } else {
        // check if the decision was already evaluated before and returned error
        DMNDecisionResult.DecisionEvaluationStatus status = result.getDecisionResultById(decision.getId()).getEvaluationStatus();
        if (FAILED == status || SKIPPED == status || EVALUATING == status) {
            return false;
        }
    }
    try {
        DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecision(eventManager, decision, result);
        boolean missingInput = false;
        DMNDecisionResultImpl dr = (DMNDecisionResultImpl) result.getDecisionResultById(decision.getId());
        dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.EVALUATING);
        for (DMNNode dep : decision.getDependencies().values()) {
            try {
                if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
                    missingInput = true;
                    DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(decision), getIdentifier(dep), MsgUtil.clipString(result.getContext().get(dep.getName()).toString(), 50), ((DMNBaseNode) dep).getType());
                    reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                }
            } catch (Exception e) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getIdentifier(dep), e.getMessage());
            }
            if (!result.getContext().isDefined(dep.getName())) {
                if (dep instanceof DecisionNode) {
                    if (!evaluateDecision(context, result, (DecisionNode) dep, typeCheck)) {
                        missingInput = true;
                        DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.UNABLE_TO_EVALUATE_DECISION_REQ_DEP, getIdentifier(decision), getIdentifier(dep));
                        reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                    }
                } else if (dep instanceof BusinessKnowledgeModelNode) {
                    evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
                } else {
                    missingInput = true;
                    DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getIdentifier(dep), getIdentifier(decision));
                    reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                }
            }
        }
        if (missingInput) {
            return false;
        }
        if (decision.getEvaluator() == null) {
            DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decision.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_DECISION, getIdentifier(decision));
            reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
            return false;
        }
        try {
            EvaluatorResult er = decision.getEvaluator().evaluate(this, result);
            if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
                Object value = er.getResult();
                if (!decision.getResultType().isCollection() && value instanceof Collection && ((Collection) value).size() == 1) {
                    // spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
                    // and vice-versa
                    value = ((Collection) value).toArray()[0];
                }
                try {
                    if (typeCheck && !d.getResultType().isAssignableValue(value)) {
                        DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, getIdentifier(decision), decision.getResultType(), value);
                        reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
                        return false;
                    }
                } catch (Exception e) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getIdentifier(decision), e.getMessage());
                    return false;
                }
                result.getContext().set(decision.getDecision().getVariable().getName(), value);
                dr.setResult(value);
                dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED);
            } else {
                dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.FAILED);
                return false;
            }
        } catch (Throwable t) {
            DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, t, null, Msg.ERROR_EVAL_DECISION_NODE, getIdentifier(decision), t.getMessage());
            reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
        }
        return true;
    } finally {
        DMNRuntimeEventManagerUtils.fireAfterEvaluateDecision(eventManager, decision, result);
    }
}
Also used : EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) Collection(java.util.Collection) DMNNode(org.kie.dmn.api.core.ast.DMNNode)

Example 12 with DecisionNodeImpl

use of org.kie.dmn.core.ast.DecisionNodeImpl in project drools by kiegroup.

the class DMNRuntimeTest method testCycleDetectionSelfReference.

@Test
public void testCycleDetectionSelfReference() {
    DecisionNodeImpl decision = new DecisionNodeImpl();
    decision.addDependency("self", decision);
    DMNModelImpl model = new DMNModelImpl();
    model.addDecision(decision);
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime(this.getClass());
    DMNResult result = runtime.evaluateAll(model, DMNFactory.newContext());
    assertTrue(result.hasErrors());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Test(org.junit.Test)

Aggregations

DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)12 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)5 Test (org.junit.Test)4 DMNResult (org.kie.dmn.api.core.DMNResult)4 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)4 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)4 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 DMNNode (org.kie.dmn.api.core.ast.DMNNode)3 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DMNType (org.kie.dmn.api.core.DMNType)2 DRGElement (org.kie.dmn.model.v1_1.DRGElement)2 Decision (org.kie.dmn.model.v1_1.Decision)2 Collection (java.util.Collection)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Optional (java.util.Optional)1 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)1 DMNMessage (org.kie.dmn.api.core.DMNMessage)1