use of org.kie.dmn.api.feel.runtime.events.FEELEvent 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.api.feel.runtime.events.FEELEvent 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), 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 = new FEELEventBase(Severity.ERROR, "Error invoking " + toString() + ": " + message, new RuntimeException("Error invoking function " + getSignature() + ".", e));
} catch (IllegalAccessException e) {
String message = e.getCause().getMessage();
capturedException = new FEELEventBase(Severity.ERROR, "Error invoking " + toString() + ": " + message, new RuntimeException("Error invoking function " + getSignature() + ".", e));
} finally {
ctx.exitFrame();
}
return FEELFnResult.ofError(capturedException);
}
use of org.kie.dmn.api.feel.runtime.events.FEELEvent in project drools by kiegroup.
the class InvokeFunction method invoke.
public FEELFnResult<Object> invoke(@ParameterName("ctx") EvaluationContext ctx, @ParameterName("namespace") String namespace, @ParameterName("model name") String modelName, @ParameterName("decision name") String decisionName, @ParameterName("parameters") Map<String, Object> parameters) {
DMNRuntime dmnRuntime = ctx.getDMNRuntime();
if (namespace == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "namespace", "cannot be null"));
}
if (modelName == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "model name", "cannot be null"));
}
if (decisionName == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "decision name", "cannot be null"));
}
if (parameters == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "parameters", "cannot be null"));
}
FEELEvent capturedException = null;
try {
ctx.enterFrame();
DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName);
if (dmnModel == null) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find model '" + modelName + "' in namespace " + namespace, null));
}
if (dmnModel.getDecisionByName(decisionName) == null) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find decision '" + decisionName + "' in the model", null));
}
DMNContext dmnContext = dmnRuntime.newContext();
dmnContext.getAll().putAll(parameters);
DMNResult requiredDecisionResult = dmnRuntime.evaluateByName(dmnModel, dmnContext, decisionName);
if (requiredDecisionResult.hasErrors()) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Errors occurred while invoking the external decision: " + requiredDecisionResult.getMessages(), null));
}
return FEELFnResult.ofResult(requiredDecisionResult.getContext().get(decisionName));
} catch (Exception e) {
capturedException = new FEELEventBase(FEELEvent.Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getName() + ".", e));
} finally {
ctx.exitFrame();
}
return FEELFnResult.ofError(capturedException);
}
use of org.kie.dmn.api.feel.runtime.events.FEELEvent in project drools by kiegroup.
the class BaseFEELTest method testExpression.
@Test
public void testExpression() {
FEELEventListener listener = mock(FEELEventListener.class);
feel.addListener(listener);
feel.addListener(evt -> {
System.out.println(evt);
});
assertResult(expression, result);
if (severity != null) {
ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(listener, atLeastOnce()).onEvent(captor.capture());
assertThat(captor.getValue().getSeverity(), is(severity));
} else {
verify(listener, never()).onEvent(any(FEELEvent.class));
}
}
use of org.kie.dmn.api.feel.runtime.events.FEELEvent in project drools by kiegroup.
the class FEELErrorMessagesTest method unknownVariable.
@Test
public void unknownVariable() {
FEEL feel = FEEL.newInstance();
FEELEventListener fel = Mockito.mock(FEELEventListener.class);
feel.addListener(fel);
CompilerContext ctx = feel.newCompilerContext();
// ctx.addInputVariableType( "a variable name", BuiltInType.STRING );
feel.compile("a variable name", ctx);
ArgumentCaptor<FEELEvent> captor = ArgumentCaptor.forClass(FEELEvent.class);
verify(fel, times(2)).onEvent(captor.capture());
Assert.assertThat(captor.getAllValues().size(), is(2));
Assert.assertThat(captor.getAllValues().get(1), is(instanceOf(UnknownVariableErrorEvent.class)));
Assert.assertThat(((UnknownVariableErrorEvent) captor.getAllValues().get(1)).getOffendingSymbol(), is("a variable name"));
}
Aggregations