Search in sources :

Example 6 with FEELEvent

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);
}
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 7 with FEELEvent

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);
}
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) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with FEELEvent

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);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) DMNContext(org.kie.dmn.api.core.DMNContext) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 9 with FEELEvent

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));
    }
}
Also used : FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Test(org.junit.Test)

Example 10 with FEELEvent

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"));
}
Also used : FEEL(org.kie.dmn.feel.FEEL) CompilerContext(org.kie.dmn.feel.lang.CompilerContext) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Test(org.junit.Test)

Aggregations

FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)19 Test (org.junit.Test)5 FEELEventListener (org.kie.dmn.api.feel.runtime.events.FEELEventListener)5 FEELEventBase (org.kie.dmn.feel.runtime.events.FEELEventBase)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 EvaluationContextImpl (org.kie.dmn.feel.lang.impl.EvaluationContextImpl)4 List (java.util.List)3 Severity (org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity)3 FEEL (org.kie.dmn.feel.FEEL)3 HitPolicyViolationEvent (org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent)3 BigDecimal (java.math.BigDecimal)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)2 InvalidInputEvent (org.kie.dmn.feel.runtime.events.InvalidInputEvent)2 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)2 Logger (org.slf4j.Logger)2