Search in sources :

Example 1 with DMNDecisionServiceEvaluator

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

Aggregations

Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BiConsumer (java.util.function.BiConsumer)1 Collectors (java.util.stream.Collectors)1 QName (javax.xml.namespace.QName)1 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)1 KieRuntimeFactory (org.kie.api.runtime.KieRuntimeFactory)1 DMNContext (org.kie.dmn.api.core.DMNContext)1 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)1 EVALUATING (org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.EVALUATING)1 FAILED (org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.FAILED)1 SKIPPED (org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus.SKIPPED)1 DMNMessage (org.kie.dmn.api.core.DMNMessage)1 DMNModel (org.kie.dmn.api.core.DMNModel)1 DMNResult (org.kie.dmn.api.core.DMNResult)1 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)1