Search in sources :

Example 21 with SimpleCondition

use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.

the class UntilConditionITCase method testUntilConditionFollowedByOneOrMoreConsecutive.

@Test
public void testUntilConditionFollowedByOneOrMoreConsecutive() 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().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)));
    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 22 with SimpleCondition

use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.

the class UntilConditionITCase method testIterativeUntilConditionOneOrMore.

@Test
public void testIterativeUntilConditionOneOrMore() 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(40, "d", 1.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").oneOrMore().until(new IterativeCondition<Event>() {

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            double sum = 0;
            for (Event middle : ctx.getEventsForPattern("middle")) {
                sum += middle.getPrice();
            }
            return sum == 6.0;
        }
    });
    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), Lists.newArrayList(startEvent, middleEvent1, middleEvent2), Lists.newArrayList(startEvent, middleEvent1)));
    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 23 with SimpleCondition

use of org.apache.flink.cep.pattern.conditions.SimpleCondition 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 24 with SimpleCondition

use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.

the class SameElementITCase method testZeroOrMoreSameElement.

@Test
public void testZeroOrMoreSameElement() 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 middleEvent1a = new Event(41, "a", 2.0);
    Event middleEvent2 = new Event(42, "a", 3.0);
    Event middleEvent3 = new Event(43, "a", 4.0);
    Event middleEvent3a = new Event(43, "a", 4.0);
    Event end1 = new Event(44, "b", 5.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent1a, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(new Event(50, "d", 6.0), 5));
    inputEvents.add(new StreamRecord<>(middleEvent3, 6));
    inputEvents.add(new StreamRecord<>(middleEvent3a, 6));
    inputEvents.add(new StreamRecord<>(end1, 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");
        }
    }).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().optional().allowCombinations().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");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1a, end1), Lists.newArrayList(startEvent, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent3a, end1), Lists.newArrayList(startEvent, end1)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 25 with SimpleCondition

use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.

the class SameElementITCase method testRepeatingPatternWSameElement.

@Test
public void testRepeatingPatternWSameElement() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent = new Event(40, "c", 1.0);
    Event middle1Event1 = new Event(40, "a", 2.0);
    Event middle1Event2 = new Event(40, "a", 3.0);
    Event middle1Event3 = new Event(40, "a", 4.0);
    Event middle2Event1 = new Event(40, "b", 5.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middle1Event1, 3));
    inputEvents.add(new StreamRecord<>(middle1Event1, 3));
    inputEvents.add(new StreamRecord<>(middle1Event2, 3));
    inputEvents.add(new StreamRecord<>(new Event(40, "d", 6.0), 5));
    inputEvents.add(new StreamRecord<>(middle2Event1, 6));
    inputEvents.add(new StreamRecord<>(middle1Event3, 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("middle1").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().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");
        }
    }).optional().followedBy("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middle1Event1), Lists.newArrayList(startEvent, middle1Event1, middle1Event1), Lists.newArrayList(startEvent, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2), Lists.newArrayList(startEvent, middle1Event1, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2, middle2Event1, middle1Event3)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) Event(org.apache.flink.cep.Event) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)219 Test (org.junit.Test)219 Event (org.apache.flink.cep.Event)204 ArrayList (java.util.ArrayList)201 List (java.util.List)183 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)183 SubEvent (org.apache.flink.cep.SubEvent)78 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)69 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)18 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)15 Map (java.util.Map)12 IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)12 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)9 NFA (org.apache.flink.cep.nfa.NFA)9 Pattern (org.apache.flink.cep.pattern.Pattern)9 Watermark (org.apache.flink.streaming.api.watermark.Watermark)9 Duration (java.time.Duration)6 Arrays (java.util.Arrays)6 Collection (java.util.Collection)6 Comparator (java.util.Comparator)6