Search in sources :

Example 36 with SimpleCondition

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

the class CEPITCase method testSimpleOrFilterPatternCEP.

/**
 * Checks that a certain event sequence is recognized with an OR filter.
 *
 * @throws Exception
 */
@Test
public void testSimpleOrFilterPatternCEP() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(envConfiguration);
    DataStream<Event> input = env.fromElements(new Event(1, "start", 1.0), new Event(2, "middle", 2.0), new Event(3, "end", 3.0), new Event(4, "start", 4.0), new Event(5, "middle", 5.0), new Event(6, "end", 6.0));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

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

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getPrice() == 2.0;
        }
    }).or(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getPrice() == 5.0;
        }
    }).followedByAny("end").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    DataStream<String> result = CEP.pattern(input, pattern).inProcessingTime().select(new PatternSelectFunction<Event, String>() {

        @Override
        public String select(Map<String, List<Event>> pattern) {
            StringBuilder builder = new StringBuilder();
            builder.append(pattern.get("start").get(0).getId()).append(",").append(pattern.get("middle").get(0).getId()).append(",").append(pattern.get("end").get(0).getId());
            return builder.toString();
        }
    });
    List<String> resultList = new ArrayList<>();
    DataStreamUtils.collect(result).forEachRemaining(resultList::add);
    List<String> expected = Arrays.asList("1,5,6", "1,2,3", "4,5,6", "1,2,6");
    expected.sort(String::compareTo);
    resultList.sort(String::compareTo);
    assertEquals(expected, resultList);
}
Also used : SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 37 with SimpleCondition

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

the class AfterMatchSkipITCase method testSkipToLastWithOneOrMoreAtBeginning.

/**
 * Example from docs.
 */
@Test
public void testSkipToLastWithOneOrMoreAtBeginning() 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.skipToLast("a")).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, Lists.newArrayList(Lists.newArrayList(a1, a2, a3, b1), Lists.newArrayList(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 38 with SimpleCondition

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

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

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

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

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

the class GreedyITCase method testGreedyUntilOneOrMoreWithDummyEventsBeforeQuantifier.

@Test
public void testGreedyUntilOneOrMoreWithDummyEventsBeforeQuantifier() 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", 3.0);
    Event a3 = new Event(43, "a", 3.0);
    Event d = new Event(45, "d", 3.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(new Event(44, "a", 4.0), 2));
    inputEvents.add(new StreamRecord<>(a1, 3));
    inputEvents.add(new StreamRecord<>(a2, 4));
    inputEvents.add(new StreamRecord<>(a3, 5));
    inputEvents.add(new StreamRecord<>(d, 6));
    // 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().greedy().until(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getPrice() > 3.0;
        }
    }).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());
}
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)

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