Search in sources :

Example 26 with SubEvent

use of org.apache.flink.cep.SubEvent in project flink by apache.

the class NFAITCase method testBranchingPattern.

@Test
public void testBranchingPattern() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event startEvent = new Event(40, "start", 1.0);
    SubEvent middleEvent1 = new SubEvent(41, "foo1", 1.0, 10.0);
    SubEvent middleEvent2 = new SubEvent(42, "foo2", 1.0, 10.0);
    SubEvent middleEvent3 = new SubEvent(43, "foo3", 1.0, 10.0);
    SubEvent nextOne1 = new SubEvent(44, "next-one", 1.0, 2.0);
    SubEvent nextOne2 = new SubEvent(45, "next-one", 1.0, 2.0);
    Event endEvent = new Event(46, "end", 1.0);
    inputEvents.add(new StreamRecord<>(startEvent, 1));
    inputEvents.add(new StreamRecord<Event>(middleEvent1, 3));
    inputEvents.add(new StreamRecord<Event>(middleEvent2, 4));
    inputEvents.add(new StreamRecord<Event>(middleEvent3, 5));
    inputEvents.add(new StreamRecord<Event>(nextOne1, 6));
    inputEvents.add(new StreamRecord<Event>(nextOne2, 7));
    inputEvents.add(new StreamRecord<>(endEvent, 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("start");
        }
    }).followedByAny("middle-first").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getVolume() > 5.0;
        }
    }).followedByAny("middle-second").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getName().equals("next-one");
        }
    }).followedByAny("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 7056763917392056548L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent1, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne2, endEvent)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SubEvent(org.apache.flink.cep.SubEvent) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 27 with SubEvent

use of org.apache.flink.cep.SubEvent in project flink by apache.

the class IterativeConditionsITCase method testIterativeWithABACPattern.

@Test
public void testIterativeWithABACPattern() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    // 1
    inputEvents.add(new StreamRecord<>(startEvent1, 1L));
    // 1
    inputEvents.add(new StreamRecord<Event>(middleEvent1, 2L));
    // 2
    inputEvents.add(new StreamRecord<>(startEvent2, 2L));
    // 3
    inputEvents.add(new StreamRecord<>(startEvent3, 2L));
    // 2
    inputEvents.add(new StreamRecord<Event>(middleEvent2, 2L));
    // 4
    inputEvents.add(new StreamRecord<>(startEvent4, 2L));
    // 3
    inputEvents.add(new StreamRecord<Event>(middleEvent3, 2L));
    // 1
    inputEvents.add(new StreamRecord<Event>(middleEvent4, 2L));
    inputEvents.add(new StreamRecord<>(endEvent, 4L));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6215754202506583964L;

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

        private static final long serialVersionUID = 2178338526904474690L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getName().startsWith("foo");
        }
    }).followedBy("middle2").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = -1223388426808292695L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            if (!value.getName().equals("start")) {
                return false;
            }
            double sum = 0.0;
            for (Event e : ctx.getEventsForPattern("middle2")) {
                sum += e.getPrice();
            }
            sum += value.getPrice();
            return Double.compare(sum, 5.0) <= 0;
        }
    }).oneOrMore().followedBy("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 562590474115118323L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent1, startEvent2, startEvent3, middleEvent1, endEvent), Lists.newArrayList(startEvent1, middleEvent1, startEvent2, endEvent), Lists.newArrayList(startEvent1, middleEvent2, startEvent4, endEvent), Lists.newArrayList(startEvent2, middleEvent2, startEvent4, endEvent), Lists.newArrayList(startEvent3, middleEvent2, startEvent4, endEvent)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SubEvent(org.apache.flink.cep.SubEvent) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

Event (org.apache.flink.cep.Event)27 SubEvent (org.apache.flink.cep.SubEvent)27 Test (org.junit.Test)27 Map (java.util.Map)21 OperatorSubtaskState (org.apache.flink.runtime.checkpoint.OperatorSubtaskState)15 Watermark (org.apache.flink.streaming.api.watermark.Watermark)14 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)13 HashMap (java.util.HashMap)11 List (java.util.List)10 KeySelector (org.apache.flink.api.java.functions.KeySelector)9 KeyedOneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness)8 ArrayList (java.util.ArrayList)7 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)7 RocksDBStateBackend (org.apache.flink.contrib.streaming.state.RocksDBStateBackend)4 MemoryStateBackend (org.apache.flink.runtime.state.memory.MemoryStateBackend)4 Ignore (org.junit.Ignore)3 ValueState (org.apache.flink.api.common.state.ValueState)2 NullByteKeySelector (org.apache.flink.api.java.functions.NullByteKeySelector)2 IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)2 NFA (org.apache.flink.cep.nfa.NFA)1