Search in sources :

Example 26 with NFATestHarness

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

the class NFATest method testTimeoutWindowPruning.

@Test
public void testTimeoutWindowPruning() 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, "bar", 2.0), 2L));
    streamEvents.add(new StreamRecord<>(new Event(3, "start", 3.0), 3L));
    streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 4L));
    List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();
    Map<String, List<Event>> secondPattern = new HashMap<>();
    secondPattern.put("start", Collections.singletonList(new Event(3, "start", 3.0)));
    secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));
    expectedPatterns.add(secondPattern);
    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) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 27 with NFATestHarness

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

the class NFATest method testNFASerialization.

@Test
public void testNFASerialization() throws Exception {
    Pattern<Event, ?> pattern1 = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 1858562682635302605L;

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

        private static final long serialVersionUID = 8061969839441121955L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    Pattern<Event, ?> pattern2 = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 1858562682635302605L;

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

        private static final long serialVersionUID = -6085237016591726715L;

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

        private static final long serialVersionUID = 8061969839441121955L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, IterativeCondition.Context<Event> ctx) throws Exception {
            double sum = 0.0;
            for (Event e : ctx.getEventsForPattern("middle")) {
                sum += e.getPrice();
            }
            return sum > 5.0;
        }
    });
    Pattern<Event, ?> pattern3 = Pattern.<Event>begin("start").notFollowedBy("not").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = -6085237016591726715L;

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

        private static final long serialVersionUID = 8061969839441121955L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    List<Pattern<Event, ?>> patterns = new ArrayList<>();
    patterns.add(pattern1);
    patterns.add(pattern2);
    patterns.add(pattern3);
    for (Pattern<Event, ?> p : patterns) {
        NFA<Event> nfa = compile(p, false);
        Event a = new Event(40, "a", 1.0);
        Event b = new Event(41, "b", 2.0);
        Event c = new Event(42, "c", 3.0);
        Event b1 = new Event(41, "b", 3.0);
        Event b2 = new Event(41, "b", 4.0);
        Event b3 = new Event(41, "b", 5.0);
        Event d = new Event(43, "d", 4.0);
        NFAState nfaState = nfa.createInitialNFAState();
        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
        nfaTestHarness.consumeRecord(new StreamRecord<>(a, 1));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b, 2));
        nfaTestHarness.consumeRecord(new StreamRecord<>(c, 3));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b1, 4));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b2, 5));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b3, 6));
        nfaTestHarness.consumeRecord(new StreamRecord<>(d, 7));
        nfaTestHarness.consumeRecord(new StreamRecord<>(a, 8));
        NFAStateSerializer serializer = new NFAStateSerializer();
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializer.serialize(nfaState, new DataOutputViewStreamWrapper(baos));
        baos.close();
        // copy
        ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.duplicate().copy(new DataInputViewStreamWrapper(in), new DataOutputViewStreamWrapper(out));
        in.close();
        out.close();
        // deserialize
        ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
        NFAState copy = serializer.duplicate().deserialize(new DataInputViewStreamWrapper(bais));
        bais.close();
        assertEquals(nfaState, copy);
    }
}
Also used : Pattern(org.apache.flink.cep.pattern.Pattern) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) Test(org.junit.Test)

Example 28 with NFATestHarness

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

the class SameElementITCase method testClearingBufferWithUntilAtTheEnd.

@Test
public void testClearingBufferWithUntilAtTheEnd() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event a1 = new Event(40, "a", 1.0);
    Event d1 = new Event(41, "d", 2.0);
    Event d2 = new Event(41, "d", 2.0);
    Event d3 = new Event(41, "d", 2.0);
    Event d4 = new Event(41, "d", 2.0);
    inputEvents.add(new StreamRecord<>(a1, 1));
    inputEvents.add(new StreamRecord<>(d1, 2));
    inputEvents.add(new StreamRecord<>(d2, 2));
    inputEvents.add(new StreamRecord<>(d3, 2));
    inputEvents.add(new StreamRecord<>(d4, 4));
    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("d").where(new SimpleCondition<Event>() {

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

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return Iterators.size(ctx.getEventsForPattern("d").iterator()) == 3;
        }
    });
    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, d1, d2, d3), Lists.newArrayList(a1, d1, d2), Lists.newArrayList(a1, d1)));
    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 29 with NFATestHarness

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

the class UntilConditionITCase method testUntilConditionFollowedByZeroOrMoreConsecutive.

@Test
public void testUntilConditionFollowedByZeroOrMoreConsecutive() 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 breaking = new Event(45, "a", 5.0);
    Event ignored = new Event(46, "a", 6.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(breaking, 7));
    inputEvents.add(new StreamRecord<>(ignored, 8));
    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().optional().consecutive().until(UNTIL_CONDITION).followedBy("end").where(UNTIL_CONDITION);
    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(startEvent, middleEvent1, middleEvent2, breaking), Lists.newArrayList(startEvent, middleEvent1, breaking), Lists.newArrayList(startEvent, breaking)));
    assertEquals(1, nfaState.getPartialMatches().size());
    assertEquals("start", 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 30 with NFATestHarness

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

the class UntilConditionITCase method testUntilConditionFollowedByOneOrMoreCombinations.

@Test
public void testUntilConditionFollowedByOneOrMoreCombinations() 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 breaking = new Event(44, "a", 5.0);
    Event ignored = new Event(45, "a", 6.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<>(breaking, 6));
    inputEvents.add(new StreamRecord<>(ignored, 7));
    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().until(UNTIL_CONDITION).followedBy("end").where(UNTIL_CONDITION);
    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(startEvent, middleEvent1, middleEvent2, middleEvent3, breaking), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, breaking), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, breaking), Lists.newArrayList(startEvent, middleEvent1, breaking)));
    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)

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