Search in sources :

Example 66 with SimpleCondition

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

the class GreedyITCase method testGreedyZeroOrMoreInBetween.

@Test
public void testGreedyZeroOrMoreInBetween() 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 a3 = new Event(43, "a", 2.0);
    Event d = new Event(44, "d", 3.0);
    inputEvents.add(new StreamRecord<>(c, 1));
    inputEvents.add(new StreamRecord<>(new Event(1, "dummy", 1111), 2));
    inputEvents.add(new StreamRecord<>(a1, 3));
    inputEvents.add(new StreamRecord<>(new Event(1, "dummy", 1111), 4));
    inputEvents.add(new StreamRecord<>(a2, 5));
    inputEvents.add(new StreamRecord<>(new Event(1, "dummy", 1111), 6));
    inputEvents.add(new StreamRecord<>(a3, 7));
    inputEvents.add(new StreamRecord<>(d, 8));
    // 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, a3, 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 67 with SimpleCondition

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

the class CEPOperatorTest method testCEPOperatorSerializationWRocksDB.

@Test
public void testCEPOperatorSerializationWRocksDB() throws Exception {
    String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
    RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
    rocksDBStateBackend.setDbStoragePath(rocksDbPath);
    final Event startEvent1 = new Event(40, "start", 1.0);
    final Event startEvent2 = new Event(40, "start", 2.0);
    final SubEvent middleEvent1 = new SubEvent(40, "foo1", 1.0, 10);
    final SubEvent middleEvent2 = new SubEvent(40, "foo2", 2.0, 10);
    final SubEvent middleEvent3 = new SubEvent(40, "foo3", 3.0, 10);
    final SubEvent middleEvent4 = new SubEvent(40, "foo4", 1.0, 10);
    final Event nextOne = new Event(40, "next-one", 1.0);
    final Event endEvent = new Event(40, "end", 1.0);
    final 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("start");
        }
    }).followedBy("middle").subtype(SubEvent.class).where(new IterativeCondition<SubEvent>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(SubEvent value, Context<SubEvent> ctx) throws Exception {
            if (!value.getName().startsWith("foo")) {
                return false;
            }
            double sum = 0.0;
            for (Event event : ctx.getEventsForPattern("middle")) {
                sum += event.getPrice();
            }
            sum += value.getPrice();
            return Double.compare(sum, 5.0) < 0;
        }
    }).oneOrMore().allowCombinations().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");
        }
    });
    CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOperator(false, new NFACompiler.NFAFactory<Event>() {

        private static final long serialVersionUID = 477082663248051994L;

        @Override
        public NFA<Event> createNFA() {
            return NFACompiler.compileFactory(pattern, false).createNFA();
        }
    });
    OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(operator);
    try {
        harness.setStateBackend(rocksDBStateBackend);
        harness.open();
        harness.processWatermark(0L);
        harness.processElement(new StreamRecord<>(startEvent1, 1));
        harness.processElement(new StreamRecord<Event>(middleEvent1, 2));
        harness.processWatermark(2L);
        harness.processElement(new StreamRecord<Event>(middleEvent3, 5));
        harness.processElement(new StreamRecord<Event>(middleEvent2, 3));
        harness.processElement(new StreamRecord<>(startEvent2, 4));
        harness.processWatermark(5L);
        harness.processElement(new StreamRecord<>(nextOne, 7));
        harness.processElement(new StreamRecord<>(endEvent, 8));
        harness.processElement(new StreamRecord<Event>(middleEvent4, 6));
        harness.processWatermark(100L);
        List<List<Event>> resultingPatterns = new ArrayList<>();
        while (!harness.getOutput().isEmpty()) {
            Object o = harness.getOutput().poll();
            if (!(o instanceof Watermark)) {
                StreamRecord<Map<String, List<Event>>> el = (StreamRecord<Map<String, List<Event>>>) o;
                List<Event> res = new ArrayList<>();
                for (List<Event> le : el.getValue().values()) {
                    res.addAll(le);
                }
                resultingPatterns.add(res);
            }
        }
        compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent1, endEvent, middleEvent1, middleEvent2, middleEvent4), Lists.newArrayList(startEvent1, endEvent, middleEvent2, middleEvent1), Lists.newArrayList(startEvent1, endEvent, middleEvent3, middleEvent1), Lists.newArrayList(startEvent2, endEvent, middleEvent3, middleEvent4), Lists.newArrayList(startEvent1, endEvent, middleEvent4, middleEvent1), Lists.newArrayList(startEvent1, endEvent, middleEvent1), Lists.newArrayList(startEvent2, endEvent, middleEvent3)));
    } finally {
        harness.close();
    }
}
Also used : RocksDBStateBackend(org.apache.flink.contrib.streaming.state.RocksDBStateBackend) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) MemoryStateBackend(org.apache.flink.runtime.state.memory.MemoryStateBackend) NFA(org.apache.flink.cep.nfa.NFA) CepOperatorBuilder.createOperatorForNFA(org.apache.flink.cep.utils.CepOperatorBuilder.createOperatorForNFA) ArrayList(java.util.ArrayList) NFACompiler(org.apache.flink.cep.nfa.compiler.NFACompiler) List(java.util.List) ArrayList(java.util.ArrayList) SubEvent(org.apache.flink.cep.SubEvent) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) Map(java.util.Map) HashMap(java.util.HashMap) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Example 68 with SimpleCondition

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

the class SameElementITCase method testZeroOrMoreSameElement.

@Test
public void testZeroOrMoreSameElement() 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 middleEvent1a = new Event(41, "a", 2.0);
    Event middleEvent2 = new Event(42, "a", 3.0);
    Event middleEvent3 = new Event(43, "a", 4.0);
    Event middleEvent3a = 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<>(middleEvent1a, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(new Event(50, "d", 6.0), 5));
    inputEvents.add(new StreamRecord<>(middleEvent3, 6));
    inputEvents.add(new StreamRecord<>(middleEvent3a, 6));
    inputEvents.add(new StreamRecord<>(end1, 7));
    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().optional().allowCombinations().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, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1a, end1), Lists.newArrayList(startEvent, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent3a, 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) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 69 with SimpleCondition

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

the class SameElementITCase method testIterativeConditionWSameElement.

@Test
public void testIterativeConditionWSameElement() 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 middleEvent1a = new Event(41, "a", 2.0);
    Event middleEvent1b = new Event(41, "a", 2.0);
    final Event end = new Event(44, "b", 5.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent1a, 3));
    inputEvents.add(new StreamRecord<>(middleEvent1b, 3));
    inputEvents.add(new StreamRecord<>(end, 7));
    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().optional().allowCombinations().followedBy("end").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = -5566639743229703237L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            double sum = 0.0;
            for (Event event : ctx.getEventsForPattern("middle")) {
                sum += event.getPrice();
            }
            return Double.compare(sum, 4.0) == 0;
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, end), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent1b), Lists.newArrayList(startEvent, middleEvent1a, middleEvent1b, end)));
}
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 70 with SimpleCondition

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

the class UntilConditionITCase method testUntilConditionFollowedByOneOrMoreConsecutive.

@Test
public void testUntilConditionFollowedByOneOrMoreConsecutive() 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 breaking = new Event(45, "a", 5.0);
    Event ignored = new Event(46, "a", 6.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<>(breaking, 7));
    inputEvents.add(new StreamRecord<>(ignored, 8));
    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().consecutive().until(UNTIL_CONDITION).followedBy("end").where(UNTIL_CONDITION);
    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(startEvent, middleEvent1, middleEvent2, breaking), Lists.newArrayList(startEvent, middleEvent1, breaking)));
    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