Search in sources :

Example 91 with SimpleCondition

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

the class GreedyITCase method testGreedyZeroOrMoreWithDummyEventsAfterQuantifier.

@Test
public void testGreedyZeroOrMoreWithDummyEventsAfterQuantifier() 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 a2 = new Event(42, "a", 2.0);
    Event d = new Event(44, "d", 3.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(a1, 2));
    inputEvents.add(new StreamRecord<>(a2, 3));
    inputEvents.add(new StreamRecord<>(new Event(43, "dummy", 2.0), 4));
    inputEvents.add(new StreamRecord<>(d, 5));
    // c a* 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("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().greedy().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, a2, 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) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 92 with SimpleCondition

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

the class GreedyITCase method testGreedyZeroOrMoreWithDummyEventsBeforeQuantifier.

@Test
public void testGreedyZeroOrMoreWithDummyEventsBeforeQuantifier() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event c = new Event(40, "c", 1.0);
    Event d = new Event(44, "d", 3.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(new Event(43, "dummy", 2.0), 2));
    inputEvents.add(new StreamRecord<>(d, 5));
    // c a* 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("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().greedy().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, 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) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with SimpleCondition

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

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

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

the class AfterMatchSkipITCase method testSkipPastLastWithOneOrMoreAtBeginning.

/**
 * Example from docs.
 */
@Test
public void testSkipPastLastWithOneOrMoreAtBeginning() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a1", 0.0);
    Event a2 = new Event(2, "a2", 0.0);
    Event a3 = new Event(3, "a3", 0.0);
    Event b1 = new Event(4, "b1", 0.0);
    streamEvents.add(new StreamRecord<>(a1));
    streamEvents.add(new StreamRecord<>(a2));
    streamEvents.add(new StreamRecord<>(a3));
    streamEvents.add(new StreamRecord<>(b1));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipPastLastEvent()).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("a");
        }
    }).oneOrMore().consecutive().greedy().next("b").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("b");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Collections.singletonList(Lists.newArrayList(a1, a2, a3, b1)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) 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 95 with SimpleCondition

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

the class AfterMatchSkipITCase method testSkipToLast.

@Test
public void testSkipToLast() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event ab1 = new Event(1, "ab", 0.0);
    Event ab2 = new Event(2, "ab", 0.0);
    Event ab3 = new Event(3, "ab", 0.0);
    Event ab4 = new Event(4, "ab", 0.0);
    Event ab5 = new Event(5, "ab", 0.0);
    Event ab6 = new Event(6, "ab", 0.0);
    Event ab7 = new Event(7, "ab", 0.0);
    streamEvents.add(new StreamRecord<Event>(ab1));
    streamEvents.add(new StreamRecord<Event>(ab2));
    streamEvents.add(new StreamRecord<Event>(ab3));
    streamEvents.add(new StreamRecord<Event>(ab4));
    streamEvents.add(new StreamRecord<Event>(ab5));
    streamEvents.add(new StreamRecord<Event>(ab6));
    streamEvents.add(new StreamRecord<Event>(ab7));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.skipToLast("end")).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("a");
        }
    }).times(2).next("end").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().contains("b");
        }
    }).times(2);
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(ab1, ab2, ab3, ab4), Lists.newArrayList(ab4, ab5, ab6, ab7)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) FlinkRuntimeException(org.apache.flink.util.FlinkRuntimeException) 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