Search in sources :

Example 1 with BusinessKnowledgeModelNodeImpl

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

the class DMNCompilerImpl method processDrgElements.

private void processDrgElements(DMNCompilerContext ctx, DMNFEELHelper feel, DMNModelImpl model, Definitions dmndefs) {
    for (DRGElement e : dmndefs.getDrgElement()) {
        boolean foundIt = false;
        for (DRGElementCompiler dc : drgCompilers) {
            if (dc.accept(e)) {
                foundIt = true;
                dc.compileNode(e, this, model);
                continue;
            }
        }
        if (!foundIt) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, e, model, null, null, Msg.UNSUPPORTED_ELEMENT, e.getClass().getSimpleName(), e.getId());
        }
    }
    for (BusinessKnowledgeModelNode bkm : model.getBusinessKnowledgeModels()) {
        BusinessKnowledgeModelNodeImpl bkmi = (BusinessKnowledgeModelNodeImpl) bkm;
        for (DRGElementCompiler dc : drgCompilers) {
            if (bkmi.getEvaluator() == null && dc.accept(bkm)) {
                dc.compileEvaluator(bkm, this, ctx, model);
            }
        }
    }
    for (DecisionNode d : model.getDecisions()) {
        DecisionNodeImpl di = (DecisionNodeImpl) d;
        for (DRGElementCompiler dc : drgCompilers) {
            if (di.getEvaluator() == null && dc.accept(d)) {
                dc.compileEvaluator(d, this, ctx, model);
            }
        }
    }
    detectCycles(model);
}
Also used : BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DRGElement(org.kie.dmn.model.v1_1.DRGElement)

Example 2 with BusinessKnowledgeModelNodeImpl

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

the class DMNCompilerImpl method processDrgElements.

private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Definitions dmndefs) {
    for (DRGElement e : dmndefs.getDrgElement()) {
        boolean foundIt = false;
        for (DRGElementCompiler dc : drgCompilers) {
            if (dc.accept(e)) {
                foundIt = true;
                dc.compileNode(e, this, model);
                continue;
            }
        }
        if (!foundIt) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, e, model, null, null, Msg.UNSUPPORTED_ELEMENT, e.getClass().getSimpleName(), e.getId());
        }
    }
    // in DMN v1.1 the DecisionService is not on the DRGElement but as an extension
    if (dmndefs.getExtensionElements() != null) {
        List<DecisionServices> decisionServices = dmndefs.getExtensionElements().getAny().stream().filter(DecisionServices.class::isInstance).map(DecisionServices.class::cast).collect(Collectors.toList());
        for (DecisionServices dss : decisionServices) {
            for (DecisionService ds : dss.getDecisionService()) {
                // compatibility with DMN v1.1, create Decision Service's variable:
                if (ds.getVariable() == null) {
                    InformationItem variable = new TInformationItem();
                    variable.setId(UUID.randomUUID().toString());
                    variable.setName(ds.getName());
                    variable.setParent(ds);
                    // the introduction of an on-the-fly ItemDefinition has been removed. The variable type will be evaluated as feel:any, or in v1.2 will receive the (user-defined, explicit) ItemDefinition type.
                    ds.setVariable(variable);
                }
                // continuing with normal compilation of Decision Service:
                boolean foundIt = false;
                for (DRGElementCompiler dc : drgCompilers) {
                    if (dc.accept(ds)) {
                        foundIt = true;
                        dc.compileNode(ds, this, model);
                        continue;
                    }
                }
            }
        }
    }
    for (DecisionServiceNode ds : model.getDecisionServices()) {
        DecisionServiceNodeImpl dsi = (DecisionServiceNodeImpl) ds;
        dsi.addModelImportAliases(model.getImportAliasesForNS());
        for (DRGElementCompiler dc : drgCompilers) {
            if (dsi.getEvaluator() == null && dc.accept(dsi)) {
                // will compile in fact all DS belonging to this model (not the imported ones).
                dc.compileEvaluator(dsi, this, ctx, model);
            }
        }
    }
    for (BusinessKnowledgeModelNode bkm : model.getBusinessKnowledgeModels()) {
        BusinessKnowledgeModelNodeImpl bkmi = (BusinessKnowledgeModelNodeImpl) bkm;
        bkmi.addModelImportAliases(model.getImportAliasesForNS());
        for (DRGElementCompiler dc : drgCompilers) {
            if (bkmi.getEvaluator() == null && dc.accept(bkm)) {
                dc.compileEvaluator(bkm, this, ctx, model);
            }
        }
    }
    for (DecisionNode d : model.getDecisions()) {
        DecisionNodeImpl di = (DecisionNodeImpl) d;
        di.addModelImportAliases(model.getImportAliasesForNS());
        for (DRGElementCompiler dc : drgCompilers) {
            if (di.getEvaluator() == null && dc.accept(d)) {
                dc.compileEvaluator(d, this, ctx, model);
            }
        }
    }
    for (AfterProcessDrgElements callback : afterDRGcallbacks) {
        logger.debug("About to invoke callback: {}", callback);
        callback.callback(this, ctx, model);
    }
    detectCycles(model);
}
Also used : DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DecisionServices(org.kie.dmn.model.v1_1.extensions.DecisionServices) TInformationItem(org.kie.dmn.model.v1_1.TInformationItem) InformationItem(org.kie.dmn.model.api.InformationItem) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DecisionService(org.kie.dmn.model.api.DecisionService) TInformationItem(org.kie.dmn.model.v1_1.TInformationItem) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) DRGElement(org.kie.dmn.model.api.DRGElement) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl)

Example 3 with BusinessKnowledgeModelNodeImpl

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

the class BusinessKnowledgeModelCompiler method compileNode.

@Override
public void compileNode(DRGElement de, DMNCompilerImpl compiler, DMNModelImpl model) {
    BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) de;
    BusinessKnowledgeModelNodeImpl bkmn = new BusinessKnowledgeModelNodeImpl(bkm);
    DMNType type = null;
    DMNType fnType = null;
    if (bkm.getVariable() == null) {
        DMNCompilerHelper.reportMissingVariable(model, de, bkm, Msg.MISSING_VARIABLE_FOR_BKM);
        return;
    }
    DMNCompilerHelper.checkVariableName(model, bkm, bkm.getName());
    if (bkm.getVariable().getTypeRef() != null) {
        // variable must be present, otherwise error was already reported above.
        type = compiler.resolveTypeRef(model, bkm, bkm.getVariable(), bkm.getVariable().getTypeRef());
        if (type instanceof SimpleFnTypeImpl) {
            fnType = type;
            type = ((SimpleFnTypeImpl) type).getReturnType();
        }
        // consistency checks
        if (bkm.getEncapsulatedLogic() != null && bkm.getEncapsulatedLogic().getTypeRef() != null) {
            DMNType bkmELType = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic(), bkm.getEncapsulatedLogic().getTypeRef());
            if (!areSameDMNType(fnType != null ? fnType : type, bkmELType)) {
                MsgUtil.reportMessage(LOG, DMNMessage.Severity.WARN, bkm.getEncapsulatedLogic(), model, null, null, Msg.VARIABLE_TYPE_MISMATCH_FOR_BKM_EL, bkm.getEncapsulatedLogic().getTypeRef(), bkm.getVariable().getTypeRef());
            }
        }
        if (fnType == null && bkm.getEncapsulatedLogic() != null && bkm.getEncapsulatedLogic().getExpression() != null && bkm.getEncapsulatedLogic().getExpression().getTypeRef() != null) {
            // if fnType != null, this is already taken care of DROOLS-6488 in method checkFnConsistency, further below.
            DMNType elExprType = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic().getExpression(), bkm.getEncapsulatedLogic().getExpression().getTypeRef());
            if (!areSameDMNType(type, elExprType)) {
                MsgUtil.reportMessage(LOG, DMNMessage.Severity.WARN, bkm.getEncapsulatedLogic(), model, null, null, Msg.VARIABLE_TYPE_MISMATCH_FOR_BKM_EL_BODY, bkm.getEncapsulatedLogic().getExpression().getTypeRef(), type.getName());
            }
        }
    } else if (bkm.getVariable().getTypeRef() == null && bkm.getEncapsulatedLogic().getExpression() != null && bkm.getEncapsulatedLogic().getExpression().getTypeRef() != null) {
        type = compiler.resolveTypeRef(model, bkm, bkm.getEncapsulatedLogic().getExpression(), bkm.getEncapsulatedLogic().getExpression().getTypeRef());
    } else {
        // for now the call bellow will return type UNKNOWN
        type = compiler.resolveTypeRef(model, bkm, bkm, null);
    }
    bkmn.setType(fnType);
    bkmn.setResultType(type);
    model.addBusinessKnowledgeModel(bkmn);
}
Also used : BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) SimpleFnTypeImpl(org.kie.dmn.core.impl.SimpleFnTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 4 with BusinessKnowledgeModelNodeImpl

use of org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl 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 {
        loadInCtx(bkmi, ctx, model);
        // 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();
        if (bkmi.getType() != null && funcDef != null) {
            checkFnConsistency(model, bkmi, bkmi.getType(), funcDef);
        }
        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) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl)

Example 5 with BusinessKnowledgeModelNodeImpl

use of org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl 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, 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), getDependencyIdentifier(bkm, dep), MsgUtil.clipString(Objects.toString(result.getContext().get(dep.getName())), 50), ((DMNBaseNode) dep).getType());
                return;
            }
            if (!isNodeValueDefined(result, bkm, dep)) {
                boolean walkingIntoScope = walkIntoImportScope(result, bkm, dep);
                if (dep instanceof BusinessKnowledgeModelNode) {
                    evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
                } else if (dep instanceof DecisionServiceNode) {
                    evaluateDecisionService(context, result, (DecisionServiceNode) dep, typeCheck);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getDependencyIdentifier(bkm, dep), getIdentifier(bkm));
                    return;
                }
                if (walkingIntoScope) {
                    result.getContext().popScope();
                }
            }
        }
        EvaluatorResult er = bkm.getEvaluator().evaluate(this, result);
        if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
            final FEELFunction original_fn = (FEELFunction) er.getResult();
            FEELFunction resultFn = original_fn;
            if (typeCheck) {
                DMNType resultType = b.getResultType();
                resultFn = new DMNFunctionWithReturnType(original_fn, resultType, result, b);
            }
            result.getContext().set(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 : EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNFunctionWithReturnType(org.kie.dmn.core.ast.DMNFunctionWithReturnType) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DMNType(org.kie.dmn.api.core.DMNType)

Aggregations

BusinessKnowledgeModelNodeImpl (org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl)7 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)3 DMNType (org.kie.dmn.api.core.DMNType)2 DMNNode (org.kie.dmn.api.core.ast.DMNNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2 DecisionServiceNode (org.kie.dmn.api.core.ast.DecisionServiceNode)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)1 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)1 DMNFunctionWithReturnType (org.kie.dmn.core.ast.DMNFunctionWithReturnType)1 DecisionServiceNodeImpl (org.kie.dmn.core.ast.DecisionServiceNodeImpl)1 SimpleFnTypeImpl (org.kie.dmn.core.impl.SimpleFnTypeImpl)1 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)1 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)1 DRGElement (org.kie.dmn.model.api.DRGElement)1 DecisionService (org.kie.dmn.model.api.DecisionService)1 FunctionDefinition (org.kie.dmn.model.api.FunctionDefinition)1 InformationItem (org.kie.dmn.model.api.InformationItem)1