Search in sources :

Example 1 with InvalidInputEvent

use of org.kie.dmn.feel.runtime.events.InvalidInputEvent in project drools by kiegroup.

the class CustomFEELFunction method invoke.

public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params) {
    if (params.length != parameters.size()) {
        return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params) + " )", getSignature()));
    }
    FEELEvent capturedException = null;
    try {
        ctx.enterFrame();
        for (int i = 0; i < parameters.size(); i++) {
            ctx.setValue(parameters.get(i), params[i]);
        }
        Object result = this.body.evaluate(ctx);
        return FEELFnResult.ofResult(result);
    } catch (Exception e) {
        capturedException = new FEELEventBase(Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getSignature() + ".", e));
    } finally {
        ctx.exitFrame();
    }
    return FEELFnResult.ofError(capturedException);
}
Also used : FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent)

Example 2 with InvalidInputEvent

use of org.kie.dmn.feel.runtime.events.InvalidInputEvent in project drools by kiegroup.

the class JavaFunction method invoke.

public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params) {
    if (params.length != parameters.size()) {
        return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params) + " )", getSignature()));
    }
    FEELEvent capturedException = null;
    try {
        ctx.enterFrame();
        for (int i = 0; i < parameters.size(); i++) {
            ctx.setValue(parameters.get(i).name, params[i]);
        }
        Object[] actualParams = prepareParams(params);
        Object result = method.invoke(clazz, actualParams);
        return FEELFnResult.ofResult(result);
    } catch (InvocationTargetException e) {
        String message = e.getTargetException().getMessage();
        capturedException = buildCaptured(e, message);
    } catch (IllegalAccessException e) {
        String message = e.getCause().getMessage();
        capturedException = buildCaptured(e, message);
    } catch (Exception e) {
        String message = e.getMessage();
        capturedException = buildCaptured(e, message);
    } finally {
        ctx.exitFrame();
    }
    return FEELFnResult.ofError(capturedException);
}
Also used : InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with InvalidInputEvent

use of org.kie.dmn.feel.runtime.events.InvalidInputEvent in project drools by kiegroup.

the class AbstractCustomFEELFunction method invoke.

public FEELFnResult<Object> invoke(EvaluationContext ctx, Object[] params) {
    if (params.length != parameters.size()) {
        return FEELFnResult.ofError(new InvalidInputEvent(Severity.ERROR, "Illegal invocation of function", getName(), getName() + "( " + Arrays.asList(params) + " )", getSignature()));
    }
    FEELEvent capturedException = null;
    try {
        ctx.enterFrame();
        for (int i = 0; i < parameters.size(); i++) {
            final String paramName = parameters.get(i).name;
            if (parameters.get(i).type.isAssignableValue(params[i])) {
                ctx.setValue(paramName, params[i]);
            } else {
                ctx.setValue(paramName, null);
                ctx.notifyEvt(() -> {
                    InvalidParametersEvent evt = new InvalidParametersEvent(Severity.WARN, paramName, "not conformant");
                    evt.setNodeName(getName());
                    evt.setActualParameters(parameters.stream().map(FEELFunction.Param::getName).collect(Collectors.toList()), Arrays.asList(params));
                    return evt;
                });
            }
        }
        Object result = internalInvoke(ctx);
        return FEELFnResult.ofResult(result);
    } catch (Exception e) {
        capturedException = new FEELEventBase(Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getSignature() + ".", e));
    } finally {
        ctx.exitFrame();
    }
    return FEELFnResult.ofError(capturedException);
}
Also used : FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent)

Example 4 with InvalidInputEvent

use of org.kie.dmn.feel.runtime.events.InvalidInputEvent in project drools by kiegroup.

the class DMNAlphaNetworkEvaluatorImpl method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnResult) {
    DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecisionTable(eventManager, node.getName(), decisionTableName, dmnResult);
    EvaluationContext evalCtx = createEvaluationContext(results.getEvents(), eventManager, dmnResult);
    evalCtx.enterFrame();
    DMNDTExpressionEvaluator.EventResults eventResults = null;
    try {
        Optional<InvalidInputEvent> potentialError = compiledNetwork.validate(evalCtx);
        if (potentialError.isPresent()) {
            InvalidInputEvent actualError = potentialError.get();
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node.getSource(), (DMNResultImpl) dmnResult, null, actualError, Msg.FEEL_ERROR, actualError.getMessage());
            return new EvaluatorResultImpl(null, EvaluatorResult.ResultType.FAILURE);
        }
        Object result = compiledNetwork.evaluate(evalCtx, feelDecisionTable);
        eventResults = processEvents(results.getEvents(), eventManager, (DMNResultImpl) dmnResult, node);
        return new EvaluatorResultImpl(result, eventResults.hasErrors ? EvaluatorResult.ResultType.FAILURE : EvaluatorResult.ResultType.SUCCESS);
    } catch (RuntimeException e) {
        logger.error(e.toString(), e);
        throw e;
    } finally {
        evalCtx.exitFrame();
        DMNRuntimeEventManagerUtils.fireAfterEvaluateDecisionTable(eventManager, node.getName(), decisionTableName, dmnResult, (eventResults != null ? eventResults.matchedRules : null), (eventResults != null ? eventResults.fired : null));
    }
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) DMNDTExpressionEvaluator(org.kie.dmn.core.ast.DMNDTExpressionEvaluator) InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext)

Example 5 with InvalidInputEvent

use of org.kie.dmn.feel.runtime.events.InvalidInputEvent in project drools by kiegroup.

the class DecisionTableImpl method actualInputsMatchInputValues.

/**
 * If valid input values are defined, check that all parameters match the respective valid inputs
 * @param ctx
 * @param params
 * @return
 */
private Either<FEELEvent, Object> actualInputsMatchInputValues(EvaluationContext ctx, Object[] params) {
    // check that all the parameters match the input list values if they are defined
    for (int i = 0; i < params.length; i++) {
        final DTInputClause input = inputs.get(i);
        // if a list of values is defined, check the the parameter matches the value
        if (input.getInputValues() != null && !input.getInputValues().isEmpty()) {
            final Object parameter = params[i];
            boolean satisfies = true;
            if (input.isCollection() && parameter instanceof Collection) {
                for (Object parameterItem : (Collection<?>) parameter) {
                    satisfies &= input.getInputValues().stream().map(ut -> ut.apply(ctx, parameterItem)).filter(x -> x != null && x).findAny().orElse(false);
                }
            } else {
                satisfies = input.getInputValues().stream().map(ut -> ut.apply(ctx, parameter)).filter(x -> x != null && x).findAny().orElse(false);
            }
            if (!satisfies) {
                String values = input.getInputValuesText();
                return Either.ofLeft(new InvalidInputEvent(FEELEvent.Severity.ERROR, input.getInputExpression() + "='" + parameter + "' does not match any of the valid values " + values + " for decision table '" + getName() + "'.", getName(), null, values));
            }
        }
    }
    return Either.ofRight(true);
}
Also used : IntStream(java.util.stream.IntStream) FEEL(org.kie.dmn.feel.FEEL) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) Either(org.kie.dmn.feel.util.Either) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FEELFnResult(org.kie.dmn.feel.runtime.functions.FEELFnResult) Collectors.toMap(java.util.stream.Collectors.toMap) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) Map(java.util.Map) EvaluationContextImpl(org.kie.dmn.feel.lang.impl.EvaluationContextImpl) InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Logger(org.slf4j.Logger) Severity(org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity) DecisionTableRulesMatchedEvent(org.kie.dmn.feel.runtime.events.DecisionTableRulesMatchedEvent) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) TreeMap(java.util.TreeMap) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) CompiledFEELExpression(org.kie.dmn.feel.codegen.feel11.CompiledFEELExpression) Collections(java.util.Collections) Collection(java.util.Collection) InvalidInputEvent(org.kie.dmn.feel.runtime.events.InvalidInputEvent)

Aggregations

InvalidInputEvent (org.kie.dmn.feel.runtime.events.InvalidInputEvent)5 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)4 FEELEventBase (org.kie.dmn.feel.runtime.events.FEELEventBase)3 EvaluationContext (org.kie.dmn.feel.lang.EvaluationContext)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Severity (org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity)1 DMNDTExpressionEvaluator (org.kie.dmn.core.ast.DMNDTExpressionEvaluator)1 EvaluatorResultImpl (org.kie.dmn.core.ast.EvaluatorResultImpl)1