Search in sources :

Example 1 with DMNNode

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

the class BusinessKnowledgeModelCompiler method compileEvaluator.

@Override
public void compileEvaluator(DMNNode node, DMNCompilerImpl compiler, DMNCompilerContext ctx, DMNModelImpl model) {
    BusinessKnowledgeModelNodeImpl bkmi = (BusinessKnowledgeModelNodeImpl) node;
    compiler.linkRequirements(model, bkmi);
    ctx.enterFrame();
    try {
        for (DMNNode dep : bkmi.getDependencies().values()) {
            if (dep instanceof BusinessKnowledgeModelNode) {
                // might need to create a DMNType for "functions" and replace the type here by that
                ctx.setVariable(dep.getName(), ((BusinessKnowledgeModelNode) dep).getResultType());
            }
        }
        // to allow recursive call from inside a BKM node, a variable for self must be available for the compiler context:
        ctx.setVariable(bkmi.getName(), bkmi.getResultType());
        FunctionDefinition funcDef = bkmi.getBusinessKnowledModel().getEncapsulatedLogic();
        DMNExpressionEvaluator exprEvaluator = compiler.getEvaluatorCompiler().compileExpression(ctx, model, bkmi, bkmi.getName(), funcDef);
        bkmi.setEvaluator(exprEvaluator);
    } finally {
        ctx.exitFrame();
    }
}
Also used : DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DMNNode(org.kie.dmn.api.core.ast.DMNNode) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)

Example 2 with DMNNode

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

the class DMNCompilerImpl method detectCycles.

private void detectCycles(DecisionNodeImpl node, Map<DecisionNodeImpl, Boolean> registry, DMNModelImpl model) {
    if (Boolean.TRUE.equals(registry.get(node)))
        return;
    if (Boolean.FALSE.equals(registry.put(node, Boolean.FALSE))) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node.getSource(), model, null, null, Msg.CYCLIC_DEP_FOR_NODE, node.getName());
        registry.put(node, Boolean.TRUE);
    }
    for (DMNNode dependency : node.getDependencies().values()) {
        if (dependency instanceof DecisionNodeImpl) {
            detectCycles((DecisionNodeImpl) dependency, registry, model);
        }
    }
    registry.put(node, Boolean.TRUE);
}
Also used : DMNNode(org.kie.dmn.api.core.ast.DMNNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl)

Example 3 with DMNNode

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

the class DecisionCompiler method compileEvaluator.

@Override
public void compileEvaluator(DMNNode node, DMNCompilerImpl compiler, DMNCompilerContext ctx, DMNModelImpl model) {
    DecisionNodeImpl di = (DecisionNodeImpl) node;
    compiler.linkRequirements(model, di);
    ctx.enterFrame();
    try {
        Map<String, DMNType> importedTypes = new HashMap<>();
        for (DMNNode dep : di.getDependencies().values()) {
            if (dep instanceof DecisionNode) {
                ctx.setVariable(dep.getName(), ((DecisionNode) dep).getResultType());
            } else if (dep instanceof InputDataNode) {
                ctx.setVariable(dep.getName(), ((InputDataNode) dep).getType());
            } else if (dep instanceof BusinessKnowledgeModelNode) {
                if (dep.getModelNamespace().equals(model.getNamespace())) {
                    // might need to create a DMNType for "functions" and replace the type here by that
                    ctx.setVariable(dep.getName(), ((BusinessKnowledgeModelNode) dep).getResultType());
                } else {
                    // then the BKM dependency is an imported BKM.
                    Optional<String> alias = model.getImportAliasFor(dep.getModelNamespace(), dep.getModelName());
                    if (alias.isPresent()) {
                        CompositeTypeImpl importedComposite = (CompositeTypeImpl) importedTypes.computeIfAbsent(alias.get(), a -> new CompositeTypeImpl());
                        importedComposite.addField(dep.getName(), ((BusinessKnowledgeModelNode) dep).getResultType());
                    }
                }
            }
        }
        for (Entry<String, DMNType> importedType : importedTypes.entrySet()) {
            ctx.setVariable(importedType.getKey(), importedType.getValue());
        }
        DMNExpressionEvaluator evaluator = compiler.getEvaluatorCompiler().compileExpression(ctx, model, di, di.getName(), di.getDecision().getExpression());
        di.setEvaluator(evaluator);
    } finally {
        ctx.exitFrame();
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) DMNNode(org.kie.dmn.api.core.ast.DMNNode) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) HashMap(java.util.HashMap) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Decision(org.kie.dmn.model.v1_1.Decision) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) Map(java.util.Map) DRGElement(org.kie.dmn.model.v1_1.DRGElement) Entry(java.util.Map.Entry) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Msg(org.kie.dmn.core.util.Msg) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) HashMap(java.util.HashMap) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNNode(org.kie.dmn.api.core.ast.DMNNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 4 with DMNNode

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

the class DMNRuntimeImpl method evaluateBKM.

private void evaluateBKM(DMNContext context, DMNResultImpl result, BusinessKnowledgeModelNode b, boolean typeCheck) {
    BusinessKnowledgeModelNodeImpl bkm = (BusinessKnowledgeModelNodeImpl) b;
    if (isNodeValueDefined(result, bkm)) {
        // TODO: do we need to check if the defined variable is a function as it should?
        return;
    }
    // TODO: do we need to check/resolve dependencies?
    if (bkm.getEvaluator() == null) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, bkm.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_BKM, getIdentifier(bkm));
        return;
    }
    try {
        DMNRuntimeEventManagerUtils.fireBeforeEvaluateBKM(eventManager, bkm, result);
        for (DMNNode dep : bkm.getDependencies().values()) {
            if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(bkm), getIdentifier(dep), MsgUtil.clipString(result.getContext().get(dep.getName()).toString(), 50), ((DMNBaseNode) dep).getType());
                return;
            }
            if (!isNodeValueDefined(result, dep)) {
                if (dep instanceof BusinessKnowledgeModelNode) {
                    evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getIdentifier(dep), getIdentifier(bkm));
                    return;
                }
            }
        }
        EvaluatorResult er = bkm.getEvaluator().evaluate(this, result);
        if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
            FEELFunction resultFn = (FEELFunction) er.getResult();
            if (bkm.getModelNamespace().equals(result.getModel().getNamespace())) {
                // TODO check of the return type will need calculation/inference of function return type.
                result.getContext().set(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
            } else {
                DMNModelImpl model = (DMNModelImpl) result.getModel();
                Optional<String> importAlias = model.getImportAliasFor(bkm.getModelNamespace(), bkm.getModelName());
                Map<String, Object> aliasContext = (Map) result.getContext().getAll().computeIfAbsent(importAlias.get(), x -> new LinkedHashMap<>());
                aliasContext.put(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
            }
        }
    } catch (Throwable t) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, t, null, Msg.ERROR_EVAL_BKM_NODE, getIdentifier(bkm), t.getMessage());
    } finally {
        DMNRuntimeEventManagerUtils.fireAfterEvaluateBKM(eventManager, bkm, result);
    }
}
Also used : ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNPackage(org.kie.dmn.api.core.DMNPackage) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) LoggerFactory(org.slf4j.LoggerFactory) ResourceType(org.kie.api.io.ResourceType) DMNOption(org.kie.dmn.core.compiler.DMNOption) EVALUATING(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.EVALUATING) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DMNModel(org.kie.dmn.api.core.DMNModel) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Map(java.util.Map) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) DMNProfile(org.kie.dmn.core.compiler.DMNProfile) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNFactory(org.kie.dmn.core.api.DMNFactory) Logger(org.slf4j.Logger) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) SKIPPED(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.SKIPPED) Collection(java.util.Collection) Set(java.util.Set) FAILED(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.FAILED) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) List(java.util.List) KieRuntime(org.kie.api.runtime.KieRuntime) DMNContext(org.kie.dmn.api.core.DMNContext) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) Msg(org.kie.dmn.core.util.Msg) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) LinkedHashMap(java.util.LinkedHashMap) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with DMNNode

use of org.kie.dmn.api.core.ast.DMNNode 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)

Aggregations

DMNNode (org.kie.dmn.api.core.ast.DMNNode)5 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)4 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)4 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)3 Collection (java.util.Collection)2 Map (java.util.Map)2 Optional (java.util.Optional)2 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)2 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)2 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)2 BusinessKnowledgeModelNodeImpl (org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl)2 DMNBaseNode (org.kie.dmn.core.ast.DMNBaseNode)2 Msg (org.kie.dmn.core.util.Msg)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1