Search in sources :

Example 1 with DMNContextImpl

use of org.kie.dmn.core.impl.DMNContextImpl 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 DMNContextImpl

use of org.kie.dmn.core.impl.DMNContextImpl 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 DMNContextImpl

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

use of org.kie.dmn.core.impl.DMNContextImpl 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

DMNContext (org.kie.dmn.api.core.DMNContext)4 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)4 DMNContextImpl (org.kie.dmn.core.impl.DMNContextImpl)4 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)4 ArrayList (java.util.ArrayList)2 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)1 DMNRuntimeImpl (org.kie.dmn.core.impl.DMNRuntimeImpl)1 EvaluationContextImpl (org.kie.dmn.feel.lang.impl.EvaluationContextImpl)1 FEELEventListenersManager (org.kie.dmn.feel.lang.impl.FEELEventListenersManager)1 FEELImpl (org.kie.dmn.feel.lang.impl.FEELImpl)1 NamedParameter (org.kie.dmn.feel.lang.impl.NamedParameter)1 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)1 FunctionDefinition (org.kie.dmn.model.v1_1.FunctionDefinition)1