Search in sources :

Example 16 with NFATestHarness

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

the class NFAStateAccessTest method testComplexBranchingAfterZeroOrMore.

@Test
public void testComplexBranchingAfterZeroOrMore() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent = new Event(40, "c", 1.0);
    Event middleEvent1 = new Event(41, "a", 2.0);
    Event middleEvent2 = new Event(42, "a", 3.0);
    Event middleEvent3 = new Event(43, "a", 4.0);
    Event end1 = new Event(44, "b", 5.0);
    Event end2 = new Event(45, "d", 6.0);
    Event end3 = new Event(46, "d", 7.0);
    Event end4 = new Event(47, "e", 8.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(middleEvent3, 5));
    inputEvents.add(new StreamRecord<>(end1, 6));
    inputEvents.add(new StreamRecord<>(end2, 7));
    inputEvents.add(new StreamRecord<>(end3, 8));
    inputEvents.add(new StreamRecord<>(end4, 9));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).oneOrMore().allowCombinations().optional().followedByAny("end1").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    }).followedByAny("end3").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("e");
        }
    });
    TestSharedBuffer<Event> sharedBuffer = TestSharedBuffer.createTestBuffer(Event.createTypeSerializer());
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).withSharedBuffer(sharedBuffer).build();
    nfaTestHarness.consumeRecords(inputEvents);
    assertEquals(58, sharedBuffer.getStateReads());
    assertEquals(33, sharedBuffer.getStateWrites());
    assertEquals(91, sharedBuffer.getStateAccesses());
}
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) SubEvent(org.apache.flink.cep.SubEvent) Test(org.junit.Test)

Example 17 with NFATestHarness

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

the class NFATest method testWindowBorders.

/**
 * Tests that elements whose timestamp difference is exactly the window length are not matched.
 * The reason is that the right window side (later elements) is exclusive.
 */
@Test
public void testWindowBorders() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
    streamEvents.add(new StreamRecord<>(new Event(2, "end", 2.0), 3L));
    List<Map<String, List<Event>>> expectedPatterns = Collections.emptyList();
    NFA<Event> nfa = createStartEndNFA();
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
    Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);
    assertEquals(expectedPatterns, actualPatterns);
}
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) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 18 with NFATestHarness

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

the class SameElementITCase method testClearingBuffer.

@Test
public void testClearingBuffer() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event a1 = new Event(40, "a", 1.0);
    Event b1 = new Event(41, "b", 2.0);
    Event c1 = new Event(41, "c", 2.0);
    Event d = new Event(41, "d", 2.0);
    inputEvents.add(new StreamRecord<>(a1, 1));
    inputEvents.add(new StreamRecord<>(b1, 2));
    inputEvents.add(new StreamRecord<>(c1, 2));
    inputEvents.add(new StreamRecord<>(d, 2));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a").where(new SimpleCondition<Event>() {

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

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

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

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    NFAState nfaState = nfa.createInitialNFAState();
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a1, b1, c1, d)));
    assertEquals(1, nfaState.getPartialMatches().size());
    assertEquals("a", nfaState.getPartialMatches().peek().getCurrentStateName());
}
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) Test(org.junit.Test)

Example 19 with NFATestHarness

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

the class NFAITCase method testZeroOrMoreClearingBuffer.

@Test
public void testZeroOrMoreClearingBuffer() throws Exception {
    Event startEvent = new Event(40, "c", 1.0);
    Event middleEvent1 = new Event(41, "a", 2.0);
    Event middleEvent2 = new Event(42, "a", 3.0);
    Event end1 = new Event(44, "b", 5.0);
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).oneOrMore().allowCombinations().optional().followedBy("end1").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("b");
        }
    }).within(Time.milliseconds(8));
    NFA<Event> nfa = compile(pattern, false);
    NFAState nfaState = nfa.createInitialNFAState();
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
    nfaTestHarness.consumeRecord(new StreamRecord<>(startEvent, 1));
    nfaTestHarness.consumeRecord(new StreamRecord<>(middleEvent1, 3));
    nfaTestHarness.consumeRecord(new StreamRecord<>(middleEvent2, 4));
    nfaTestHarness.consumeRecord(new StreamRecord<>(end1, 6));
    // pruning element
    nfa.advanceTime(sharedBufferAccessor, nfaState, 10);
    assertEquals(1, nfaState.getPartialMatches().size());
    assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
Also used : SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) Test(org.junit.Test)

Example 20 with NFATestHarness

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

the class NFAITCase method testNFAResultOrdering.

@Test
public void testNFAResultOrdering() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent1 = new Event(41, "a-1", 2.0);
    Event startEvent2 = new Event(41, "a-2", 3.0);
    Event startEvent3 = new Event(41, "a-3", 4.0);
    Event startEvent4 = new Event(41, "a-4", 5.0);
    Event endEvent1 = new Event(41, "b-1", 6.0);
    Event endEvent2 = new Event(41, "b-2", 7.0);
    Event endEvent3 = new Event(41, "b-3", 8.0);
    inputEvents.add(new StreamRecord<>(startEvent1, 1));
    inputEvents.add(new StreamRecord<>(startEvent2, 3));
    inputEvents.add(new StreamRecord<>(startEvent3, 4));
    inputEvents.add(new StreamRecord<>(startEvent4, 5));
    inputEvents.add(new StreamRecord<>(endEvent1, 6));
    inputEvents.add(new StreamRecord<>(endEvent2, 7));
    inputEvents.add(new StreamRecord<>(endEvent3, 10));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6452194090480345053L;

        @Override
        public boolean filter(Event s) throws Exception {
            return s.getName().startsWith("a-");
        }
    }).times(4).allowCombinations().followedByAny("middle").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = -6838398439317275390L;

        public boolean filter(Event s) throws Exception {
            return s.getName().startsWith("b-");
        }
    }).times(3).consecutive();
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    Collection<Map<String, List<Event>>> resultingPatterns = nfaTestHarness.consumeRecords(inputEvents);
    Assert.assertEquals(1L, resultingPatterns.size());
    Map<String, List<Event>> match = resultingPatterns.iterator().next();
    Assert.assertArrayEquals(match.get("start").toArray(), Lists.newArrayList(startEvent1, startEvent2, startEvent3, startEvent4).toArray());
    Assert.assertArrayEquals(match.get("middle").toArray(), Lists.newArrayList(endEvent1, endEvent2, endEvent3).toArray());
}
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) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) 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