Search in sources :

Example 81 with SimpleCondition

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

the class NFAITCase method testNextZeroOrMore.

@Test
public void testNextZeroOrMore() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent = new Event(40, "start", 1.0);
    Event middleEvent1 = new Event(40, "middle", 2.0);
    Event middleEvent2 = new Event(40, "middle", 3.0);
    Event middleEvent3 = new Event(40, "middle", 4.0);
    Event endEvent = new Event(46, "end", 1.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1L));
    inputEvents.add(new StreamRecord<>(new Event(1, "event", 1.0), 2L));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3L));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4L));
    inputEvents.add(new StreamRecord<>(middleEvent3, 5L));
    inputEvents.add(new StreamRecord<>(endEvent, 6L));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6215754202506583964L;

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

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("middle");
        }
    }).oneOrMore().optional().consecutive().followedBy("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);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, endEvent)));
}
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 82 with SimpleCondition

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

the class NFAITCase method testAtLeastOneEager.

@Test
public void testAtLeastOneEager() 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);
    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));
    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().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, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, 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) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 83 with SimpleCondition

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

the class NFAITCase method testZeroOrMore.

@Test
public void testZeroOrMore() 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 end1 = new Event(44, "b", 5.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(end1, 6));
    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().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");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent2, 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) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 84 with SimpleCondition

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

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)

Example 85 with SimpleCondition

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

the class GroupITCase method testGroupFollowedByAnyTimesCombinations.

@Test
public void testGroupFollowedByAnyTimesCombinations() 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 a3 = new Event(45, "a", 4.0);
    Event b3 = new Event(46, "b", 5.0);
    Event d = new Event(47, "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<>(a3, 6));
    inputEvents.add(new StreamRecord<>(b3, 7));
    inputEvents.add(new StreamRecord<>(d, 8));
    // c any (a b){2} 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");
        }
    }).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("middle2").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("b");
        }
    })).times(2).allowCombinations().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);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, a1, b1, a2, b2, d), Lists.newArrayList(c, a1, b1, a3, b3, d), Lists.newArrayList(c, a2, b2, a3, b3, 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)

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