Search in sources :

Example 6 with NFATestHarness

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

the class AfterMatchSkipITCase method testSkipBeforeOtherAlreadyCompleted.

@Test
public void testSkipBeforeOtherAlreadyCompleted() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    Event c1 = new Event(2, "c1", 0.0);
    Event a2 = new Event(3, "a2", 1.0);
    Event c2 = new Event(4, "c2", 0.0);
    Event b1 = new Event(5, "b1", 1.0);
    Event b2 = new Event(6, "b2", 0.0);
    streamEvents.add(new StreamRecord<>(a1));
    streamEvents.add(new StreamRecord<>(c1));
    streamEvents.add(new StreamRecord<>(a2));
    streamEvents.add(new StreamRecord<>(c2));
    streamEvents.add(new StreamRecord<>(b1));
    streamEvents.add(new StreamRecord<>(b2));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipToFirst("c")).where(new SimpleCondition<Event>() {

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

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

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().contains("b") && ctx.getEventsForPattern("a").iterator().next().getPrice() == value.getPrice();
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, c1, b2), Lists.newArrayList(a2, c2, b1)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) 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 7 with NFATestHarness

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

the class GroupITCase method testGroupFollowedByOneOrMoreWithUntilCondition.

@Test
public void testGroupFollowedByOneOrMoreWithUntilCondition() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event c = new Event(40, "c", 1.0);
    Event a1 = new Event(41, "a", 2.0);
    Event b1 = new Event(42, "b", 3.0);
    Event a2 = new Event(43, "a", 4.0);
    Event b2 = new Event(44, "b", 5.0);
    Event d = new Event(45, "d", 6.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(a1, 2));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(a2, 4));
    inputEvents.add(new StreamRecord<>(b2, 5));
    inputEvents.add(new StreamRecord<>(d, 6));
    // c (a b)+ d
    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(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

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

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

        private static final long serialVersionUID = 5726188262756267490L;

        @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();
    final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, a1, b1, d), Lists.newArrayList(c, a1, b1, a2, b2, d)));
    assertEquals(1, nfaState.getPartialMatches().size());
    assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
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 8 with NFATestHarness

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

the class NFAITCase method testTimesClearingBuffer.

// /////////////////////////////     Clearing SharedBuffer
// ////////////////////////////////////////
@Test
public void testTimesClearingBuffer() 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 middleEvent3 = new Event(43, "a", 4.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");
        }
    }).next("middle").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).times(2).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.feedRecord(new StreamRecord<>(startEvent, 1));
    nfaTestHarness.feedRecord(new StreamRecord<>(middleEvent1, 2));
    nfaTestHarness.feedRecord(new StreamRecord<>(middleEvent2, 3));
    nfaTestHarness.feedRecord(new StreamRecord<>(middleEvent3, 4));
    nfaTestHarness.feedRecord(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 9 with NFATestHarness

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

the class AfterMatchSkipITCase method testSkipPastLast3.

@Test
public void testSkipPastLast3() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    Event c = new Event(2, "c", 0.0);
    Event a2 = new Event(3, "a2", 0.0);
    Event b2 = new Event(4, "b2", 0.0);
    streamEvents.add(new StreamRecord<Event>(a1));
    streamEvents.add(new StreamRecord<Event>(c));
    streamEvents.add(new StreamRecord<Event>(a2));
    streamEvents.add(new StreamRecord<Event>(b2));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipPastLastEvent()).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");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a2, b2)));
}
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 10 with NFATestHarness

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

the class AfterMatchSkipITCase method testSkipPastLastWithOneOrMoreAtBeginning.

/**
 * Example from docs.
 */
@Test
public void testSkipPastLastWithOneOrMoreAtBeginning() 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.skipPastLastEvent()).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, Collections.singletonList(Lists.newArrayList(a1, a2, 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)

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