Search in sources :

Example 11 with NFATestHarness

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

the class NFAIterativeConditionTimeContextTest method testCurrentProcessingTime.

@Test
public void testCurrentProcessingTime() throws Exception {
    final Event event1 = event().withId(1).build();
    final Event event2 = event().withId(2).build();
    final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            return ctx.currentProcessingTime() == 3;
        }
    });
    final TestTimerService cepTimerService = new TestTimerService();
    final NFATestHarness testHarness = forPattern(pattern).withTimerService(cepTimerService).build();
    cepTimerService.setCurrentProcessingTime(1);
    final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
    cepTimerService.setCurrentProcessingTime(3);
    final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));
    comparePatterns(resultingPatterns1, Collections.emptyList());
    comparePatterns(resultingPatterns2, Collections.singletonList(Collections.singletonList(event2)));
}
Also used : NFATestHarness(org.apache.flink.cep.utils.NFATestHarness) Event(org.apache.flink.cep.Event) List(java.util.List) TestTimerService(org.apache.flink.cep.utils.TestTimerService) Test(org.junit.Test)

Example 12 with NFATestHarness

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

the class UntilConditionITCase method testUntilConditionFollowedByZeroOrMore.

@Test
public void testUntilConditionFollowedByZeroOrMore() 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(44, "a", 5.0);
    Event ignored = new Event(45, "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, 6));
    inputEvents.add(new StreamRecord<>(ignored, 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");
        }
    }).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().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), Lists.newArrayList(startEvent, 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)

Example 13 with NFATestHarness

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

the class UntilConditionITCase method testUntilConditionFollowedByOneOrMoreConsecutive2.

@Test
public void testUntilConditionFollowedByOneOrMoreConsecutive2() 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, "b", 3.0);
    Event middleEvent3 = new Event(43, "a", 4.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<>(middleEvent3, 5));
    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, 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)

Example 14 with NFATestHarness

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

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)

Example 15 with NFATestHarness

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

the class UntilConditionITCase method testIterativeUntilConditionOneOrMore.

@Test
public void testIterativeUntilConditionOneOrMore() 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 middleEvent3 = new Event(40, "d", 1.0);
    Event breaking = new Event(44, "a", 5.0);
    Event ignored = new Event(45, "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<>(middleEvent3, 5));
    inputEvents.add(new StreamRecord<>(breaking, 6));
    inputEvents.add(new StreamRecord<>(ignored, 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");
        }
    }).followedBy("middle").oneOrMore().until(new IterativeCondition<Event>() {

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            double sum = 0;
            for (Event middle : ctx.getEventsForPattern("middle")) {
                sum += middle.getPrice();
            }
            return sum == 6.0;
        }
    });
    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, middleEvent3), Lists.newArrayList(startEvent, middleEvent1, middleEvent2), Lists.newArrayList(startEvent, middleEvent1)));
    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

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