Search in sources :

Example 56 with SimpleCondition

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

the class NFAITCase method testZeroOrMoreAfterBranching.

@Test
public void testZeroOrMoreAfterBranching() 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 merging = new Event(42, "f", 3.0);
    Event kleene1 = new Event(43, "d", 4.0);
    Event kleene2 = new Event(44, "d", 4.0);
    Event end = new Event(45, "e", 4.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(merging, 5));
    inputEvents.add(new StreamRecord<>(kleene1, 6));
    inputEvents.add(new StreamRecord<>(kleene2, 7));
    inputEvents.add(new StreamRecord<>(end, 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");
        }
    }).followedByAny("branching").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    }).oneOrMore().allowCombinations().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("e");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, merging, end), Lists.newArrayList(startEvent, middleEvent1, merging, kleene1, end), Lists.newArrayList(startEvent, middleEvent1, merging, kleene2, end), Lists.newArrayList(startEvent, middleEvent1, merging, kleene1, kleene2, end), Lists.newArrayList(startEvent, middleEvent2, merging, end), Lists.newArrayList(startEvent, middleEvent2, merging, kleene1, end), Lists.newArrayList(startEvent, middleEvent2, merging, kleene2, end), Lists.newArrayList(startEvent, middleEvent2, merging, kleene1, kleene2, end)));
}
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) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 57 with SimpleCondition

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

the class NFAITCase method testBranchingPattern.

@Test
public void testBranchingPattern() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent = new Event(40, "start", 1.0);
    SubEvent middleEvent1 = new SubEvent(41, "foo1", 1.0, 10.0);
    SubEvent middleEvent2 = new SubEvent(42, "foo2", 1.0, 10.0);
    SubEvent middleEvent3 = new SubEvent(43, "foo3", 1.0, 10.0);
    SubEvent nextOne1 = new SubEvent(44, "next-one", 1.0, 2.0);
    SubEvent nextOne2 = new SubEvent(45, "next-one", 1.0, 2.0);
    Event endEvent = new Event(46, "end", 1.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<Event>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<Event>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<Event>(middleEvent3, 5));
    inputEvents.add(new StreamRecord<Event>(nextOne1, 6));
    inputEvents.add(new StreamRecord<Event>(nextOne2, 7));
    inputEvents.add(new StreamRecord<>(endEvent, 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("start");
        }
    }).followedByAny("middle-first").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getVolume() > 5.0;
        }
    }).followedByAny("middle-second").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getName().equals("next-one");
        }
    }).followedByAny("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 7056763917392056548L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent1, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne2, endEvent)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SubEvent(org.apache.flink.cep.SubEvent) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 58 with SimpleCondition

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

the class NotPatternITCase method testNotFollowedByBeforeOptional.

@Test
public void testNotFollowedByBeforeOptional() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event a1 = new Event(40, "a", 1.0);
    Event c1 = new Event(41, "c", 2.0);
    Event b1 = new Event(42, "b", 3.0);
    Event c2 = new Event(43, "c", 4.0);
    Event d = new Event(43, "d", 4.0);
    inputEvents.add(new StreamRecord<>(a1, 1));
    inputEvents.add(new StreamRecord<>(c1, 2));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(c2, 4));
    inputEvents.add(new StreamRecord<>(d, 5));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = -2454396370205097543L;

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

        private static final long serialVersionUID = 2749547391611263290L;

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

        private static final long serialVersionUID = -4989511337298217255L;

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

        private static final long serialVersionUID = -8466223836652936608L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> matches = feedNFA(inputEvents, nfa);
    comparePatterns(matches, Lists.<List<Event>>newArrayList(Lists.newArrayList(a1, c1, d)));
}
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 59 with SimpleCondition

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

the class NotPatternITCase method testTimesWithNotFollowedByAfter.

@Test
public void testTimesWithNotFollowedByAfter() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event a1 = new Event(40, "a", 1.0);
    Event e = new Event(41, "e", 2.0);
    Event c1 = new Event(42, "c", 3.0);
    Event b1 = new Event(43, "b", 4.0);
    Event b2 = new Event(44, "b", 5.0);
    Event d1 = new Event(46, "d", 7.0);
    Event d2 = new Event(47, "d", 8.0);
    inputEvents.add(new StreamRecord<>(a1, 1));
    inputEvents.add(new StreamRecord<>(d1, 2));
    inputEvents.add(new StreamRecord<>(e, 1));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(b2, 3));
    inputEvents.add(new StreamRecord<>(c1, 2));
    inputEvents.add(new StreamRecord<>(d2, 5));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6193105689601702341L;

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

        private static final long serialVersionUID = 5195859580923169111L;

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

        private static final long serialVersionUID = 4973027956103783831L;

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

        private static final long serialVersionUID = 2724622546678984894L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> matches = feedNFA(inputEvents, nfa);
    comparePatterns(matches, Lists.<List<Event>>newArrayList());
}
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 60 with SimpleCondition

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

the class GroupITCase method testGroupNestTimesConsecutive.

@Test
public void testGroupNestTimesConsecutive() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event d = new Event(40, "d", 1.0);
    Event a1 = new Event(41, "a", 2.0);
    Event b1 = new Event(42, "b", 3.0);
    Event c1 = new Event(43, "c", 4.0);
    Event b2 = new Event(44, "b", 5.0);
    Event c2 = new Event(45, "c", 4.0);
    Event b3 = new Event(46, "b", 5.0);
    Event c3 = new Event(47, "c", 4.0);
    Event a2 = new Event(48, "a", 2.0);
    Event b4 = new Event(49, "b", 3.0);
    Event c4 = new Event(50, "c", 4.0);
    Event b5 = new Event(51, "b", 5.0);
    Event c5 = new Event(52, "c", 4.0);
    Event b6 = new Event(53, "b", 5.0);
    Event c6 = new Event(54, "c", 4.0);
    Event e = new Event(55, "e", 6.0);
    inputEvents.add(new StreamRecord<>(d, 1));
    inputEvents.add(new StreamRecord<>(a1, 2));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(c1, 4));
    inputEvents.add(new StreamRecord<>(b2, 5));
    inputEvents.add(new StreamRecord<>(c2, 6));
    inputEvents.add(new StreamRecord<>(b3, 7));
    inputEvents.add(new StreamRecord<>(c3, 8));
    inputEvents.add(new StreamRecord<>(a2, 9));
    inputEvents.add(new StreamRecord<>(b4, 10));
    inputEvents.add(new StreamRecord<>(c4, 11));
    inputEvents.add(new StreamRecord<>(new Event(0, "breaking", 99.0), 12));
    inputEvents.add(new StreamRecord<>(b5, 13));
    inputEvents.add(new StreamRecord<>(c5, 14));
    inputEvents.add(new StreamRecord<>(b6, 15));
    inputEvents.add(new StreamRecord<>(c6, 16));
    inputEvents.add(new StreamRecord<>(e, 17));
    // d any (a (b c){3}){0, 2} e
    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("d");
        }
    }).followedByAny(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(Pattern.<Event>begin("middle2").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

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

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("c");
        }
    })).times(3).consecutive()).times(0, 2).consecutive().followedBy("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("e");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(d, e), Lists.newArrayList(d, a1, b1, c1, b2, c2, b3, c3, e)));
}
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