Search in sources :

Example 6 with SimpleCondition

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

the class GroupITCase method testGroupNotFollowedBy.

@Test
public void testGroupNotFollowedBy() 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 d = new Event(43, "d", 3.0);
    Event a2 = new Event(44, "a", 4.0);
    Event b2 = new Event(45, "b", 5.0);
    Event e = new Event(46, "e", 6.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(a1, 2));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(d, 4));
    inputEvents.add(new StreamRecord<>(a2, 5));
    inputEvents.add(new StreamRecord<>(b2, 6));
    inputEvents.add(new StreamRecord<>(e, 7));
    // c (a b) ^d 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("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");
        }
    })).notFollowedBy("notPattern").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @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("e");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, a2, b2, 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 7 with SimpleCondition

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

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 8 with SimpleCondition

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

the class GroupITCase method testGroupNest.

@Test
public void testGroupNest() 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 e = new Event(46, "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<>(e, 7));
    // d (a (b c)*)? 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");
        }
    }).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(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");
        }
    })).oneOrMore().optional()).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(d, e), Lists.newArrayList(d, a1, e), Lists.newArrayList(d, a1, b1, c1, e), Lists.newArrayList(d, a1, b1, c1, b2, c2, 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 9 with SimpleCondition

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

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)

Example 10 with SimpleCondition

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

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