Search in sources :

Example 1 with FEELEventListener

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

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

Example 3 with FEELEventListener

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;
}
Also used : IntStream(java.util.stream.IntStream) FEEL(org.kie.dmn.feel.FEEL) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) Logger(org.slf4j.Logger) DTOutputClause(org.kie.dmn.feel.runtime.decisiontables.DTOutputClause) HitPolicy(org.kie.dmn.feel.runtime.decisiontables.HitPolicy) LoggerFactory(org.slf4j.LoggerFactory) DTInputClause(org.kie.dmn.feel.runtime.decisiontables.DTInputClause) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Range(org.kie.dmn.feel.runtime.Range) ArrayList(java.util.ArrayList) List(java.util.List) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) EvaluationContext(org.kie.dmn.feel.lang.EvaluationContext) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) Msg(org.kie.dmn.feel.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 4 with FEELEventListener

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);
}
Also used : FEEL(org.kie.dmn.feel.FEEL) CoreMatchers(org.hamcrest.CoreMatchers) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) LoggerFactory(org.slf4j.LoggerFactory) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) BigDecimal(java.math.BigDecimal) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map) IsCollectionWithSize(org.hamcrest.collection.IsCollectionWithSize) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Description(org.hamcrest.Description) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) Logger(org.slf4j.Logger) Severity(org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity) Collection(java.util.Collection) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) List(java.util.List) Ignore(org.junit.Ignore) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matcher(org.hamcrest.Matcher) ArrayList(java.util.ArrayList) FEELEventListener(org.kie.dmn.api.feel.runtime.events.FEELEventListener) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) Map(java.util.Map) HitPolicyViolationEvent(org.kie.dmn.feel.runtime.events.HitPolicyViolationEvent) Test(org.junit.Test)

Example 5 with FEELEventListener

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));
    }
}
Also used : 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)6 FEELEventListener (org.kie.dmn.api.feel.runtime.events.FEELEventListener)6 Test (org.junit.Test)5 FEEL (org.kie.dmn.feel.FEEL)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BigDecimal (java.math.BigDecimal)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 CoreMatchers (org.hamcrest.CoreMatchers)1 Description (org.hamcrest.Description)1 Matcher (org.hamcrest.Matcher)1 Matchers (org.hamcrest.Matchers)1