Search in sources :

Example 1 with EvaluatorResult

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

the class DMNListEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    List<Object> results = new ArrayList<>();
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    try {
        int index = 0;
        for (DMNExpressionEvaluator ee : elements) {
            try {
                EvaluatorResult er = ee.evaluate(eventManager, result);
                if (er.getResultType() == ResultType.SUCCESS) {
                    results.add(er.getResult());
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_LIST_ELEMENT_ON_POSITION_ON_LIST, index + 1, name);
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
            } catch (Exception e) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_EVAL_LIST_ELEMENT_ON_POSITION_ON_LIST, index + 1, name);
                return new EvaluatorResultImpl(results, ResultType.FAILURE);
            } finally {
                index++;
            }
        }
    } finally {
        result.setContext(previousContext);
    }
    return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 2 with EvaluatorResult

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

the class DMNRelationEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    List<Map<String, Object>> results = new ArrayList<>();
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    try {
        for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
            List<DMNExpressionEvaluator> row = rows.get(rowIndex);
            Map<String, Object> element = new HashMap<>();
            for (int i = 0; i < columns.size(); i++) {
                try {
                    EvaluatorResult er = row.get(i).evaluate(eventManager, result);
                    if (er.getResultType() == ResultType.SUCCESS) {
                        element.put(columns.get(i), er.getResult());
                    } else {
                        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_ROW_ELEMENT_ON_POSITION_ON_ROW_OF_RELATION, i + 1, rowIndex + 1, name);
                        return new EvaluatorResultImpl(results, ResultType.FAILURE);
                    }
                } catch (Exception e) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_EVAL_ROW_ELEMENT_ON_POSITION_ON_ROW_OF_RELATION, i + 1, rowIndex + 1, name);
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
            }
            results.add(element);
        }
    } finally {
        result.setContext(previousContext);
    }
    return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DMNContext(org.kie.dmn.api.core.DMNContext) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 3 with EvaluatorResult

use of org.kie.dmn.core.api.EvaluatorResult 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 4 with EvaluatorResult

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

the class DMNContextEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    // when this evaluator is executed, it should either return a Map of key/value pairs
    // where keys are the name of the entries and values are the result of the evaluations
    // OR if a default result is implemented, it should return the result instead
    Map<String, Object> results = new HashMap<>();
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    try {
        for (ContextEntryDef ed : entries) {
            try {
                EvaluatorResult er = ed.getEvaluator().evaluate(eventManager, result);
                if (er.getResultType() == ResultType.SUCCESS) {
                    Object value = er.getResult();
                    if (!ed.getType().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];
                    }
                    if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
                        if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
                            // checking directly the result type...
                            if (ed.getType() != null && !ed.getType().isAssignableValue(value)) {
                                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, ed.getName(), ed.getType(), value);
                                return new EvaluatorResultImpl(results, ResultType.FAILURE);
                            }
                        } else {
                        // TODO ...will need calculation/inference of function return type.
                        }
                    }
                    results.put(ed.getName(), value);
                    dmnContext.set(ed.getName(), value);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX, ed.getName(), name);
                    return new EvaluatorResultImpl(results, ResultType.FAILURE);
                }
            } catch (Exception e) {
                logger.error("Error invoking expression for node '" + name + "'.", e);
                return new EvaluatorResultImpl(results, ResultType.FAILURE);
            }
        }
    } finally {
        result.setContext(previousContext);
    }
    if (results.containsKey(RESULT_ENTRY)) {
        return new EvaluatorResultImpl(results.get(RESULT_ENTRY), ResultType.SUCCESS);
    } else {
        return new EvaluatorResultImpl(results, ResultType.SUCCESS);
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) HashMap(java.util.HashMap) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) DMNContext(org.kie.dmn.api.core.DMNContext) Collection(java.util.Collection) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 5 with EvaluatorResult

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

the class DMNInvocationEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    final List<FEELEvent> events = new ArrayList<>();
    DMNResultImpl result = (DMNResultImpl) dmnr;
    DMNContext previousContext = result.getContext();
    DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
    result.setContext(dmnContext);
    Object invocationResult = null;
    try {
        FEELFunction function = this.functionLocator.apply(previousContext, functionName);
        if (function == null) {
            // check if it is a configured/built-in function
            Object r = null;
            if (feel != null) {
                r = ((FEELImpl) feel).newEvaluationContext(Collections.emptyList(), Collections.emptyMap()).getValue(functionName);
            } else {
                r = RootExecutionFrame.INSTANCE.getValue(functionName);
            }
            if (r != null && r instanceof FEELFunction) {
                function = (FEELFunction) r;
            }
        }
        if (function == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.FUNCTION_NOT_FOUND, functionName, nodeName);
            return new EvaluatorResultImpl(null, ResultType.FAILURE);
        }
        Object[] namedParams = new Object[parameters.size()];
        int index = 0;
        for (ActualParameter param : parameters) {
            try {
                EvaluatorResult value = param.expression.evaluate(eventManager, result);
                if (value.getResultType() == ResultType.SUCCESS) {
                    namedParams[index++] = new NamedParameter(param.name, value.getResult());
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_PARAM_FOR_INVOCATION_ON_NODE, param.name, functionName, nodeName);
                    return new EvaluatorResultImpl(null, ResultType.FAILURE);
                }
            } catch (Exception e) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_INVOKING_PARAM_EXPR_FOR_PARAM_ON_NODE, param.name, nodeName);
                return new EvaluatorResultImpl(null, ResultType.FAILURE);
            }
        }
        FEELEventListenersManager listenerMgr = new FEELEventListenersManager();
        listenerMgr.addListener(events::add);
        EvaluationContextImpl ctx = new EvaluationContextImpl(listenerMgr, eventManager.getRuntime());
        invocationResult = function.invokeReflectively(ctx, namedParams);
        boolean hasErrors = hasErrors(events, eventManager, result);
        return new EvaluatorResultImpl(invocationResult, hasErrors ? ResultType.FAILURE : ResultType.SUCCESS);
    } catch (Throwable t) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, t, null, Msg.ERR_INVOKING_FUNCTION_ON_NODE, functionName, nodeName);
        return new EvaluatorResultImpl(null, ResultType.FAILURE);
    } finally {
        result.setContext(previousContext);
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) NamedParameter(org.kie.dmn.feel.lang.impl.NamedParameter) ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) FEELEventListenersManager(org.kie.dmn.feel.lang.impl.FEELEventListenersManager) EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) FEELImpl(org.kie.dmn.feel.lang.impl.FEELImpl) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent)

Aggregations

EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)6 DMNContext (org.kie.dmn.api.core.DMNContext)5 DMNContextImpl (org.kie.dmn.core.impl.DMNContextImpl)4 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)2 DMNNode (org.kie.dmn.api.core.ast.DMNNode)2 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)2 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)2 DMNBaseNode (org.kie.dmn.core.ast.DMNBaseNode)2 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1