Search in sources :

Example 1 with IterativeCondition

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

the class NFAStatusChangeITCase method testNFAChangedOnTimeoutWithoutPrune.

@Test
public void testNFAChangedOnTimeoutWithoutPrune() throws Exception {
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("start");
        }
    }).followedBy("end").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("end");
        }
    }).within(Time.milliseconds(10));
    NFA<Event> nfa = compile(pattern, true);
    NFAState nfaState = nfa.createInitialNFAState();
    nfaState.resetStateChanged();
    nfa.advanceTime(sharedBufferAccessor, nfaState, 6L);
    nfa.process(sharedBufferAccessor, nfaState, new Event(6, "start", 1.0), 6L, skipStrategy, timerService);
    nfaState.resetStateChanged();
    nfa.advanceTime(sharedBufferAccessor, nfaState, 17L);
    assertTrue(nfaState.isStateChanged());
}
Also used : IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) Event(org.apache.flink.cep.Event) Test(org.junit.Test)

Example 2 with IterativeCondition

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

the class NFAStateAccessTest method testIterativeWithABACPattern.

@Test
public void testIterativeWithABACPattern() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    final Event startEvent1 = new Event(40, "start", 1.0);
    final Event startEvent2 = new Event(40, "start", 2.0);
    final Event startEvent3 = new Event(40, "start", 3.0);
    final Event startEvent4 = new Event(40, "start", 4.0);
    final SubEvent middleEvent1 = new SubEvent(41, "foo1", 1.0, 10);
    final SubEvent middleEvent2 = new SubEvent(42, "foo2", 2.0, 10);
    final SubEvent middleEvent3 = new SubEvent(43, "foo3", 3.0, 10);
    final SubEvent middleEvent4 = new SubEvent(43, "foo4", 1.0, 10);
    final Event endEvent = new Event(46, "end", 1.0);
    // 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");
        }
    });
    TestSharedBuffer<Event> sharedBuffer = TestSharedBuffer.createTestBuffer(Event.createTypeSerializer());
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).withSharedBuffer(sharedBuffer).build();
    nfaTestHarness.consumeRecords(inputEvents);
    assertEquals(90, sharedBuffer.getStateReads());
    assertEquals(31, sharedBuffer.getStateWrites());
    assertEquals(121, sharedBuffer.getStateAccesses());
}
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) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) Test(org.junit.Test)

Example 3 with IterativeCondition

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

the class NFATest method testNFASerialization.

@Test
public void testNFASerialization() throws Exception {
    Pattern<Event, ?> pattern1 = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 1858562682635302605L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("b");
        }
    }).oneOrMore().optional().allowCombinations().followedByAny("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    Pattern<Event, ?> pattern2 = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 1858562682635302605L;

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

        private static final long serialVersionUID = -6085237016591726715L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("b");
        }
    }).oneOrMore().optional().allowCombinations().followedByAny("end").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, IterativeCondition.Context<Event> ctx) throws Exception {
            double sum = 0.0;
            for (Event e : ctx.getEventsForPattern("middle")) {
                sum += e.getPrice();
            }
            return sum > 5.0;
        }
    });
    Pattern<Event, ?> pattern3 = Pattern.<Event>begin("start").notFollowedBy("not").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = -6085237016591726715L;

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

        private static final long serialVersionUID = 8061969839441121955L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("d");
        }
    });
    List<Pattern<Event, ?>> patterns = new ArrayList<>();
    patterns.add(pattern1);
    patterns.add(pattern2);
    patterns.add(pattern3);
    for (Pattern<Event, ?> p : patterns) {
        NFA<Event> nfa = compile(p, false);
        Event a = new Event(40, "a", 1.0);
        Event b = new Event(41, "b", 2.0);
        Event c = new Event(42, "c", 3.0);
        Event b1 = new Event(41, "b", 3.0);
        Event b2 = new Event(41, "b", 4.0);
        Event b3 = new Event(41, "b", 5.0);
        Event d = new Event(43, "d", 4.0);
        NFAState nfaState = nfa.createInitialNFAState();
        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
        nfaTestHarness.consumeRecord(new StreamRecord<>(a, 1));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b, 2));
        nfaTestHarness.consumeRecord(new StreamRecord<>(c, 3));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b1, 4));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b2, 5));
        nfaTestHarness.consumeRecord(new StreamRecord<>(b3, 6));
        nfaTestHarness.consumeRecord(new StreamRecord<>(d, 7));
        nfaTestHarness.consumeRecord(new StreamRecord<>(a, 8));
        NFAStateSerializer serializer = new NFAStateSerializer();
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializer.serialize(nfaState, new DataOutputViewStreamWrapper(baos));
        baos.close();
        // copy
        ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.duplicate().copy(new DataInputViewStreamWrapper(in), new DataOutputViewStreamWrapper(out));
        in.close();
        out.close();
        // deserialize
        ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());
        NFAState copy = serializer.duplicate().deserialize(new DataInputViewStreamWrapper(bais));
        bais.close();
        assertEquals(nfaState, copy);
    }
}
Also used : Pattern(org.apache.flink.cep.pattern.Pattern) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) DataOutputViewStreamWrapper(org.apache.flink.core.memory.DataOutputViewStreamWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) Test(org.junit.Test)

Example 4 with IterativeCondition

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

the class NFAStatusChangeITCase method testNFAChange.

@Test
public void testNFAChange() throws Exception {
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 1858562682635302605L;

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

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("b");
        }
    }).oneOrMore().optional().allowCombinations().followedBy("middle2").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("d");
        }
    }).followedBy("end").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("e");
        }
    }).within(Time.milliseconds(10));
    NFA<Event> nfa = compile(pattern, true);
    NFAState nfaState = nfa.createInitialNFAState();
    nfa.process(sharedBufferAccessor, nfaState, new Event(1, "b", 1.0), 1L, skipStrategy, timerService);
    assertFalse("NFA status should not change as the event does not match the take condition of the 'start' state", nfaState.isStateChanged());
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(2, "a", 1.0), 2L, skipStrategy, timerService);
    assertTrue("NFA status should change as the event matches the take condition of the 'start' state", nfaState.isStateChanged());
    // the status of the queue of ComputationStatus changed,
    // more than one ComputationStatus is generated by the event from some ComputationStatus
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(3, "f", 1.0), 3L, skipStrategy, timerService);
    assertTrue("NFA status should change as the event matches the ignore condition and proceed condition of the 'middle:1' state", nfaState.isStateChanged());
    // both the queue of ComputationStatus and eventSharedBuffer have not changed
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(4, "f", 1.0), 4L, skipStrategy, timerService);
    assertFalse("NFA status should not change as the event only matches the ignore condition of the 'middle:2' state and the target state is still 'middle:2'", nfaState.isStateChanged());
    // both the queue of ComputationStatus and eventSharedBuffer have changed
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(5, "b", 1.0), 5L, skipStrategy, timerService);
    assertTrue("NFA status should change as the event matches the take condition of 'middle:2' state", nfaState.isStateChanged());
    // both the queue of ComputationStatus and eventSharedBuffer have changed
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(6, "d", 1.0), 6L, skipStrategy, timerService);
    assertTrue("NFA status should change as the event matches the take condition of 'middle2' state", nfaState.isStateChanged());
    // both the queue of ComputationStatus and eventSharedBuffer have not changed
    // as the timestamp is within the window
    nfaState.resetStateChanged();
    nfa.advanceTime(sharedBufferAccessor, nfaState, 8L);
    assertFalse("NFA status should not change as the timestamp is within the window", nfaState.isStateChanged());
    // timeout ComputationStatus will be removed from the queue of ComputationStatus and timeout
    // event will
    // be removed from eventSharedBuffer as the timeout happens
    nfaState.resetStateChanged();
    Collection<Tuple2<Map<String, List<Event>>, Long>> timeoutResults = nfa.advanceTime(sharedBufferAccessor, nfaState, 12L);
    assertTrue("NFA status should change as timeout happens", nfaState.isStateChanged() && !timeoutResults.isEmpty());
}
Also used : IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Event(org.apache.flink.cep.Event) List(java.util.List) Test(org.junit.Test)

Example 5 with IterativeCondition

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

the class NFAStatusChangeITCase method testNFAChangedOnOneNewComputationState.

@Test
public void testNFAChangedOnOneNewComputationState() throws Exception {
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

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

        private static final long serialVersionUID = 1858562682635302605L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).oneOrMore().optional().next("end").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = 8061969839441121955L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return value.getName().equals("b");
        }
    }).within(Time.milliseconds(10));
    NFA<Event> nfa = compile(pattern, true);
    NFAState nfaState = nfa.createInitialNFAState();
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(6, "start", 1.0), 6L, skipStrategy, timerService);
    nfaState.resetStateChanged();
    nfa.process(sharedBufferAccessor, nfaState, new Event(6, "a", 1.0), 7L, skipStrategy, timerService);
    assertTrue(nfaState.isStateChanged());
}
Also used : SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) Event(org.apache.flink.cep.Event) Test(org.junit.Test)

Aggregations

IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)8 Event (org.apache.flink.cep.Event)6 Test (org.junit.Test)6 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 SubEvent (org.apache.flink.cep.SubEvent)2 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)2 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 Pattern (org.apache.flink.cep.pattern.Pattern)1 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1