Search in sources :

Example 21 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class NFAITCase method testNFAResultKeyOrdering.

@Test
public void testNFAResultKeyOrdering() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    Event a1 = new Event(41, "a", 2.0);
    Event b1 = new Event(41, "b", 3.0);
    Event aa1 = new Event(41, "aa", 4.0);
    Event bb1 = new Event(41, "bb", 5.0);
    Event ab1 = new Event(41, "ab", 6.0);
    inputEvents.add(new StreamRecord<>(a1, 1));
    inputEvents.add(new StreamRecord<>(b1, 3));
    inputEvents.add(new StreamRecord<>(aa1, 4));
    inputEvents.add(new StreamRecord<>(bb1, 5));
    inputEvents.add(new StreamRecord<>(ab1, 6));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("a").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6452194090480345053L;

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

        @Override
        public boolean filter(Event s) throws Exception {
            return s.getName().equals("b");
        }
    }).next("aa").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event s) throws Exception {
            return s.getName().equals("aa");
        }
    }).next("bb").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event s) throws Exception {
            return s.getName().equals("bb");
        }
    }).next("ab").where(new SimpleCondition<Event>() {

        @Override
        public boolean filter(Event s) throws Exception {
            return s.getName().equals("ab");
        }
    });
    NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
    Collection<Map<String, List<Event>>> resultingPatterns = nfaTestHarness.consumeRecords(inputEvents);
    Assert.assertEquals(1L, resultingPatterns.size());
    Map<String, List<Event>> match = resultingPatterns.iterator().next();
    List<String> expectedOrder = Lists.newArrayList("a", "b", "aa", "bb", "ab");
    List<String> resultOrder = new ArrayList<>();
    for (String key : match.keySet()) {
        resultOrder.add(key);
    }
    Assert.assertEquals(expectedOrder, resultOrder);
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 22 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class NFAITCase method testAtLeastOneClearingBuffer.

@Test
public void testAtLeastOneClearingBuffer() throws Exception {
    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 end1 = new Event(44, "b", 5.0);
    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().allowCombinations().followedBy("end1").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 5726188262756267490L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("b");
        }
    }).within(Time.milliseconds(8));
    NFA<Event> nfa = compile(pattern, false);
    NFAState nfaState = nfa.createInitialNFAState();
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
    nfaTestHarness.consumeRecord(new StreamRecord<>(startEvent, 1));
    nfaTestHarness.consumeRecord(new StreamRecord<>(middleEvent1, 3));
    nfaTestHarness.consumeRecord(new StreamRecord<>(middleEvent2, 4));
    nfaTestHarness.consumeRecord(new StreamRecord<>(end1, 6));
    // pruning element
    nfa.advanceTime(sharedBufferAccessor, nfaState, 10);
    assertEquals(1, nfaState.getPartialMatches().size());
    assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
Also used : SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) Test(org.junit.Test)

Example 23 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness 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 24 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class NFATest method testSimpleNFA.

@Test
public void testSimpleNFA() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
    streamEvents.add(new StreamRecord<>(new Event(2, "bar", 2.0), 2L));
    streamEvents.add(new StreamRecord<>(new Event(3, "start", 3.0), 3L));
    streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 4L));
    State<Event> startState = new State<>("start", State.StateType.Start);
    State<Event> endState = new State<>("end", State.StateType.Normal);
    State<Event> endingState = new State<>("", State.StateType.Final);
    startState.addTake(endState, new SimpleCondition<Event>() {

        private static final long serialVersionUID = -4869589195918650396L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("start");
        }
    });
    endState.addTake(endingState, new SimpleCondition<Event>() {

        private static final long serialVersionUID = 2979804163709590673L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    endState.addIgnore(BooleanConditions.<Event>trueFunction());
    List<State<Event>> states = new ArrayList<>();
    states.add(startState);
    states.add(endState);
    states.add(endingState);
    List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();
    Map<String, List<Event>> firstPattern = new HashMap<>();
    firstPattern.put("start", Collections.singletonList(new Event(1, "start", 1.0)));
    firstPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));
    Map<String, List<Event>> secondPattern = new HashMap<>();
    secondPattern.put("start", Collections.singletonList(new Event(3, "start", 3.0)));
    secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));
    expectedPatterns.add(firstPattern);
    expectedPatterns.add(secondPattern);
    NFA<Event> nfa = new NFA<>(states, 0, false);
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
    Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);
    assertEquals(expectedPatterns, actualPatterns);
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 25 with NFATestHarness

use of org.apache.flink.cep.utils.NFATestHarness in project flink by apache.

the class NFATest method testTimeoutWindowPruningWindowBorders.

/**
 * Tests that pruning shared buffer elements and computations state use the same window border
 * semantics (left side inclusive and right side exclusive).
 */
@Test
public void testTimeoutWindowPruningWindowBorders() throws Exception {
    List<StreamRecord<Event>> streamEvents = new ArrayList<>();
    streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
    streamEvents.add(new StreamRecord<>(new Event(2, "start", 2.0), 2L));
    streamEvents.add(new StreamRecord<>(new Event(3, "foobar", 3.0), 3L));
    streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 3L));
    List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();
    Map<String, List<Event>> secondPattern = new HashMap<>();
    secondPattern.put("start", Collections.singletonList(new Event(2, "start", 2.0)));
    secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));
    expectedPatterns.add(secondPattern);
    NFA<Event> nfa = createStartEndNFA();
    NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
    Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);
    assertEquals(expectedPatterns, actualPatterns);
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) HashMap(java.util.HashMap) 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) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Event (org.apache.flink.cep.Event)46 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)46 Test (org.junit.Test)46 ArrayList (java.util.ArrayList)40 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)39 List (java.util.List)37 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)23 FlinkRuntimeException (org.apache.flink.util.FlinkRuntimeException)15 SubEvent (org.apache.flink.cep.SubEvent)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SkipPastLastStrategy (org.apache.flink.cep.nfa.aftermatch.SkipPastLastStrategy)1 Pattern (org.apache.flink.cep.pattern.Pattern)1 TestTimerService (org.apache.flink.cep.utils.TestTimerService)1 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1