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);
}
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);
}
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);
}
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));
}
}
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);
}
Aggregations