use of org.kie.dmn.api.feel.runtime.events.FEELEventListener 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.FEELEventListener 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"));
}
use of org.kie.dmn.api.feel.runtime.events.FEELEventListener in project drools by kiegroup.
the class DecisionTableFunction method toDecisionRule.
/**
* Convert row to DTDecisionRule
* @param mainCtx the main context is used to identify the hosted FEELEventManager
* @param embeddedFEEL a possibly cached embedded FEEL to compile the output expression, error will be reported up to the mainCtx
* @param index
* @param rule
* @param inputSize
* @return
*/
private static DTDecisionRule toDecisionRule(EvaluationContext mainCtx, FEEL embeddedFEEL, int index, List<?> rule, int inputSize) {
// TODO should be check indeed block of inputSize n inputs, followed by block of outputs.
DTDecisionRule dr = new DTDecisionRule(index);
for (int i = 0; i < rule.size(); i++) {
Object o = rule.get(i);
if (i < inputSize) {
dr.getInputEntry().add(toUnaryTest(mainCtx, o));
} else {
FEELEventListener ruleListener = event -> mainCtx.notifyEvt(() -> new FEELEventBase(event.getSeverity(), Msg.createMessage(Msg.ERROR_COMPILE_EXPR_DT_FUNCTION_RULE_IDX, index + 1, event.getMessage()), event.getSourceException()));
embeddedFEEL.addListener(ruleListener);
CompiledExpression compiledExpression = embeddedFEEL.compile((String) o, embeddedFEEL.newCompilerContext());
dr.getOutputEntry().add(compiledExpression);
embeddedFEEL.removeListener(ruleListener);
}
}
return dr;
}
use of org.kie.dmn.api.feel.runtime.events.FEELEventListener in project drools by kiegroup.
the class SimpleDecisionTablesTest method testMain.
@Test
public void testMain() {
List<FEELEvent> events = new ArrayList<>();
FEELEventListener listener = evt -> events.add(evt);
feel.addListener(listener);
String expression = loadExpression("simple_decision_tables.feel");
Map context = (Map) feel.evaluate(expression);
System.out.println(printContext(context));
assertThat(context.get("result1"), is("Adult"));
assertThat(context.get("result2"), is("Medium"));
assertThat((Map<?, ?>) context.get("result3"), hasSize(2));
assertThat((Map<?, ?>) context.get("result3"), hasEntry("Out1", "out1b"));
assertThat((Map<?, ?>) context.get("result3"), hasEntry("Out2", "out2b"));
assertThat((Map<?, ?>) context.get("result4"), hasSize(2));
assertThat((Map<?, ?>) context.get("result4"), hasEntry("Out1", "io1a"));
assertThat((Map<?, ?>) context.get("result4"), hasEntry("Out2", "io2a"));
assertThat(context.get("result5"), nullValue());
assertTrue(events.stream().anyMatch(e -> e instanceof HitPolicyViolationEvent && e.getSeverity().equals(Severity.WARN)));
feel.removeListener(listener);
}
use of org.kie.dmn.api.feel.runtime.events.FEELEventListener in project drools by kiegroup.
the class KieExtendedBaseFEELTest 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));
}
}
Aggregations