Search in sources :

Example 36 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class AfterMatchSkipITCase method testSkipToFirstAtStartPosition.

@Test
public void testSkipToFirstAtStartPosition() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event ab1 = new Event(1, "ab1", 0.0);
    Event c1 = new Event(2, "c1", 0.0);
    Event ab2 = new Event(3, "ab2", 0.0);
    Event c2 = new Event(4, "c2", 0.0);
    streamEvents.add(new StreamRecord<Event>(ab1));
    streamEvents.add(new StreamRecord<Event>(c1));
    streamEvents.add(new StreamRecord<Event>(ab2));
    streamEvents.add(new StreamRecord<Event>(c2));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("b", AfterMatchSkipStrategy.skipToFirst("b")).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("b");
        }
    }).next("c").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("c");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(ab1, c1), Lists.newArrayList(ab2, c2)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Test(org.junit.Test)

Example 37 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class AfterMatchSkipITCase method testSkipToLastWithOneOrMoreAtBeginning.

/**
 * Example from docs.
 */
@Test
public void testSkipToLastWithOneOrMoreAtBeginning() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    Event a2 = new Event(2, "a2", 0.0);
    Event a3 = new Event(3, "a3", 0.0);
    Event b1 = new Event(4, "b1", 0.0);
    streamEvents.add(new StreamRecord<>(a1));
    streamEvents.add(new StreamRecord<>(a2));
    streamEvents.add(new StreamRecord<>(a3));
    streamEvents.add(new StreamRecord<>(b1));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipToLast("a")).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("a");
        }
    }).oneOrMore().consecutive().greedy().next("b").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("b");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, a2, a3, b1), Lists.newArrayList(a3, b1)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 38 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class AfterMatchSkipITCase method testNoSkip.

@Test
public void testNoSkip() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a", 0.0);
    Event a2 = new Event(2, "a", 0.0);
    Event a3 = new Event(3, "a", 0.0);
    Event a4 = new Event(4, "a", 0.0);
    Event a5 = new Event(5, "a", 0.0);
    Event a6 = new Event(6, "a", 0.0);
    streamEvents.add(new StreamRecord<Event>(a1));
    streamEvents.add(new StreamRecord<Event>(a2));
    streamEvents.add(new StreamRecord<Event>(a3));
    streamEvents.add(new StreamRecord<Event>(a4));
    streamEvents.add(new StreamRecord<Event>(a5));
    streamEvents.add(new StreamRecord<Event>(a6));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.noSkip()).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).times(3);
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, a2, a3), Lists.newArrayList(a2, a3, a4), Lists.newArrayList(a3, a4, a5), Lists.newArrayList(a4, a5, a6)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 39 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class AfterMatchSkipITCase method testSkipToFirstElementOfMatch.

@Test(expected = FlinkRuntimeException.class)
public void testSkipToFirstElementOfMatch() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    streamEvents.add(new StreamRecord<Event>(a1));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipToFirst("a").throwExceptionOnMiss()).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("a");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
// skip to first element of a match should throw exception if they are enabled,
// this mode is used in MATCH RECOGNIZE which assumes that skipping to first element
// would result in infinite loop. In CEP by default(with exceptions disabled), we use no
// skip
// strategy in this case.
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Test(org.junit.Test)

Example 40 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class AfterMatchSkipITCase method testSkipToFirstWithOneOrMore.

@Test
public void testSkipToFirstWithOneOrMore() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    Event b1 = new Event(2, "b1", 0.0);
    Event a2 = new Event(3, "a2", 0.0);
    Event b2 = new Event(4, "b2", 0.0);
    Event b3 = new Event(5, "b3", 0.0);
    Event a3 = new Event(3, "a3", 0.0);
    Event b4 = new Event(4, "b4", 0.0);
    streamEvents.add(new StreamRecord<Event>(a1));
    streamEvents.add(new StreamRecord<Event>(b1));
    streamEvents.add(new StreamRecord<Event>(a2));
    streamEvents.add(new StreamRecord<Event>(b2));
    streamEvents.add(new StreamRecord<Event>(b3));
    streamEvents.add(new StreamRecord<Event>(a3));
    streamEvents.add(new StreamRecord<Event>(b4));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipToFirst("b")).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("a");
        }
    }).next("b").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("b");
        }
    }).oneOrMore().consecutive();
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, b1), Lists.newArrayList(a2, b2), Lists.newArrayList(a3, b4)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) Test(org.junit.Test)

Aggregations

Event (org.apache.flink.cep.Event)46 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)46 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)40 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)39 List (java.util.List)37 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)23 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)15 SubEvent (org.apache.flink.cep.SubEvent)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SkipPastLastStrategy (org.apache.flink.cep.nfa.aftermatch.SkipPastLastStrategy)1 Pattern (org.apache.flink.cep.pattern.Pattern)1 TestTimerService (org.apache.flink.cep.utils.TestTimerService)1 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1