Search in sources :

Example 1 with DecisionServiceNode

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

Example 2 with DecisionServiceNode

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

the class DMNRuntimeImpl method evaluateDecisionService.

@Override
public DMNResult evaluateDecisionService(DMNModel model, DMNContext context, String decisionServiceName) {
    Objects.requireNonNull(model, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "model"));
    Objects.requireNonNull(context, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "context"));
    Objects.requireNonNull(decisionServiceName, () -> MsgUtil.createMessage(Msg.PARAM_CANNOT_BE_NULL, "decisionServiceName"));
    boolean typeCheck = performRuntimeTypeCheck(model);
    DMNResultImpl result = createResultImpl(model, context);
    // the engine should evaluate all belonging to the "local" model namespace, not imported nodes explicitly.
    Optional<DecisionServiceNode> lookupDS = ((DMNModelImpl) model).getDecisionServices().stream().filter(d -> d.getModelNamespace().equals(model.getNamespace())).filter(ds -> ds.getName().equals(decisionServiceName)).findFirst();
    if (lookupDS.isPresent()) {
        DecisionServiceNodeImpl decisionService = (DecisionServiceNodeImpl) lookupDS.get();
        for (DMNNode dep : decisionService.getInputParameters().values()) {
            if (!isNodeValueDefined(result, decisionService, dep)) {
                DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decisionService.getSource(), result, null, null, Msg.REQ_INPUT_NOT_FOUND_FOR_DS, getDependencyIdentifier(decisionService, dep), getIdentifier(decisionService));
                final boolean walkingIntoScope = walkIntoImportScope(result, decisionService, dep);
                result.getContext().set(dep.getName(), null);
                if (walkingIntoScope) {
                    result.getContext().popScope();
                }
            } else {
                final boolean walkingIntoScope = walkIntoImportScope(result, decisionService, dep);
                final Object originalValue = result.getContext().get(dep.getName());
                DMNType depType = ((DMNModelImpl) model).getTypeRegistry().unknown();
                if (dep instanceof InputDataNode) {
                    depType = ((InputDataNode) dep).getType();
                } else if (dep instanceof DecisionNode) {
                    depType = ((DecisionNode) dep).getResultType();
                }
                Object c = coerceUsingType(originalValue, depType, typeCheck, (r, t) -> MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decisionService.getDecisionService(), result, null, null, Msg.PARAMETER_TYPE_MISMATCH_DS, dep.getName(), t, MsgUtil.clipString(r.toString(), 50)));
                if (c != originalValue) {
                    // intentional by-reference
                    result.getContext().set(dep.getName(), c);
                }
                if (walkingIntoScope) {
                    result.getContext().popScope();
                }
            }
        }
        // please note singleton output coercion does not influence anyway when invoked DS on a model.
        EvaluatorResult evaluate = new DMNDecisionServiceEvaluator(decisionService, true, false).evaluate(this, result);
    } else {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, null, result, null, null, Msg.DECISION_SERVICE_NOT_FOUND_FOR_NAME, decisionServiceName);
    }
    return result;
}
Also used : MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNType(org.kie.dmn.api.core.DMNType) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) LoggerFactory(org.slf4j.LoggerFactory) DMNDecisionServiceEvaluator(org.kie.dmn.core.ast.DMNDecisionServiceEvaluator) DMNOption(org.kie.dmn.core.compiler.DMNOption) EVALUATING(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.EVALUATING) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) 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) BiConsumer(java.util.function.BiConsumer) 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) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) BeforeEvaluateDecisionEvent(org.kie.dmn.api.core.event.BeforeEvaluateDecisionEvent) 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) DMNFunctionWithReturnType(org.kie.dmn.core.ast.DMNFunctionWithReturnType) 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) Collectors(java.util.stream.Collectors) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) RuntimeTypeCheckOption(org.kie.dmn.core.compiler.RuntimeTypeCheckOption) Objects(java.util.Objects) List(java.util.List) DMNContext(org.kie.dmn.api.core.DMNContext) Optional(java.util.Optional) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) QName(javax.xml.namespace.QName) 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) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNDecisionServiceEvaluator(org.kie.dmn.core.ast.DMNDecisionServiceEvaluator) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNNode(org.kie.dmn.api.core.ast.DMNNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 3 with DecisionServiceNode

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

the class DynamicDMNContextBuilder method populateContextForDecisionServiceWith.

public DMNContext populateContextForDecisionServiceWith(String decisionServiceName, Map<String, Object> json) {
    DecisionServiceNode dsNode = model.getDecisionServices().stream().filter(ds -> ds.getName().equals(decisionServiceName)).findFirst().orElseThrow(IllegalArgumentException::new);
    for (Entry<String, Object> kv : json.entrySet()) {
        DecisionServiceNodeImpl dsNodeImpl = (DecisionServiceNodeImpl) dsNode;
        DMNNode node = dsNodeImpl.getInputParameters().get(kv.getKey());
        if (node instanceof InputDataNode) {
            processInputDataNode(kv, (InputDataNode) node);
        } else if (node instanceof DecisionNode) {
            processDecisionNode(kv, (DecisionNode) node);
        } else {
            LOG.debug("The key {} was not a RequiredInput nor a RequiredDecision for the DecisionService, setting it as-is.", kv.getKey());
            context.set(kv.getKey(), kv.getValue());
        }
    }
    return context;
}
Also used : DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DMNNode(org.kie.dmn.api.core.ast.DMNNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl)

Example 4 with DecisionServiceNode

use of org.kie.dmn.api.core.ast.DecisionServiceNode 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 5 with DecisionServiceNode

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

the class DecisionCompiler method loadInCtx.

public static void loadInCtx(DMNBaseNode node, DMNCompilerContext ctx, DMNModelImpl model) {
    Map<String, DMNType> importedTypes = new HashMap<>();
    for (DMNNode dep : node.getDependencies().values()) {
        DMNType depType = null;
        if (dep instanceof DecisionNode) {
            depType = ((DecisionNode) dep).getResultType();
        } else if (dep instanceof InputDataNode) {
            depType = ((InputDataNode) dep).getType();
        } else if (dep instanceof BusinessKnowledgeModelNode) {
            depType = ((BusinessKnowledgeModelNode) dep).getResultType();
        } else if (dep instanceof DecisionServiceNode) {
            depType = ((DecisionServiceNode) dep).getResultType();
        }
        if (dep.getModelNamespace().equals(model.getNamespace())) {
            // for BKMs might need to create a DMNType for "functions" and replace the type here by that
            ctx.setVariable(dep.getName(), depType);
        } else {
            // then the dependency is an imported dependency.
            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(), depType);
            }
        }
    }
    for (Entry<String, DMNType> importedType : importedTypes.entrySet()) {
        ctx.setVariable(importedType.getKey(), importedType.getValue());
    }
}
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) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Map(java.util.Map) Entry(java.util.Map.Entry) Optional(java.util.Optional) Decision(org.kie.dmn.model.api.Decision) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DRGElement(org.kie.dmn.model.api.DRGElement) Msg(org.kie.dmn.core.util.Msg) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) HashMap(java.util.HashMap) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) 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)

Aggregations

DecisionServiceNode (org.kie.dmn.api.core.ast.DecisionServiceNode)9 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)8 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)8 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)6 DMNNode (org.kie.dmn.api.core.ast.DMNNode)5 DMNType (org.kie.dmn.api.core.DMNType)4 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)4 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)3 BusinessKnowledgeModelNodeImpl (org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl)3 DMNBaseNode (org.kie.dmn.core.ast.DMNBaseNode)3 DecisionServiceNodeImpl (org.kie.dmn.core.ast.DecisionServiceNodeImpl)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 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2 BeforeEvaluateDecisionEvent (org.kie.dmn.api.core.event.BeforeEvaluateDecisionEvent)2 DMNFunctionWithReturnType (org.kie.dmn.core.ast.DMNFunctionWithReturnType)2