Search in sources :

Example 1 with SimpleCondition

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

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

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

the class AfterMatchSkipITCase method testNoSkipWithOneOrMoreAtBeginning.

/**
 * Example from docs.
 */
@Test
public void testNoSkipWithOneOrMoreAtBeginning() 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.noSkip()).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(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 3 with SimpleCondition

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

the class AfterMatchSkipITCase method testSkipToFirst.

@Test
public void testSkipToFirst() 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);
    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));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.skipToFirst("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(ab3, ab4, ab5, ab6)));
}
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 4 with SimpleCondition

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

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)

Example 5 with SimpleCondition

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

the class AfterMatchSkipITCase method testSkipPastLast.

@Test
public void testSkipPastLast() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    Event a1 = new Event(1, "a", 0.0);
    Event a2 = new Event(2, "a", 0.0);
    Event a3 = new Event(3, "a", 0.0);
    Event a4 = new Event(4, "a", 0.0);
    Event a5 = new Event(5, "a", 0.0);
    Event a6 = new Event(6, "a", 0.0);
    streamEvents.add(new StreamRecord<Event>(a1));
    streamEvents.add(new StreamRecord<Event>(a2));
    streamEvents.add(new StreamRecord<Event>(a3));
    streamEvents.add(new StreamRecord<Event>(a4));
    streamEvents.add(new StreamRecord<Event>(a5));
    streamEvents.add(new StreamRecord<Event>(a6));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.skipPastLastEvent()).where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).times(3);
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
    comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, a2, a3), Lists.newArrayList(a4, a5, a6)));
}
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)73 Test (org.junit.Test)73 Event (org.apache.flink.cep.Event)68 ArrayList (java.util.ArrayList)67 List (java.util.List)61 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)61 SubEvent (org.apache.flink.cep.SubEvent)26 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)23 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)6 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)5 Map (java.util.Map)4 IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)4 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)3 NFA (org.apache.flink.cep.nfa.NFA)3 Pattern (org.apache.flink.cep.pattern.Pattern)3 Duration (java.time.Duration)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2