Search in sources :

Example 1 with TestTimerService

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

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

the class NFAITCase method testSharedBufferClearing.

@Test
public void testSharedBufferClearing() throws Exception {
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");
    Event a = new Event(40, "a", 1.0);
    Event b = new Event(41, "b", 2.0);
    NFA<Event> nfa = compile(pattern, false);
    TestTimerService timerService = new TestTimerService();
    try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
        nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(), timerService);
        nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(), timerService);
        Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
        nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
        Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
    }
}
Also used : Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) TestTimerService(org.apache.flink.cep.utils.TestTimerService) Test(org.junit.Test)

Example 3 with TestTimerService

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

the class NFAITCase method testLoopClearing.

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

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("a");
        }
    }).within(Time.milliseconds(3));
    Event a1 = new Event(40, "a", 1.0);
    Event a2 = new Event(40, "a", 1.0);
    NFA<Event> nfa = compile(pattern, false);
    TestTimerService timerService = new TestTimerService();
    NFAState nfaState = nfa.createInitialNFAState();
    try (SharedBufferAccessor<Event> accessor = sharedBuffer.getAccessor()) {
        nfa.process(accessor, nfaState, a1, 1, AfterMatchSkipStrategy.noSkip(), timerService);
        nfa.process(accessor, nfaState, a2, 2, AfterMatchSkipStrategy.noSkip(), timerService);
        nfa.advanceTime(accessor, nfaState, 4);
    }
    assertThat(sharedBuffer.getEventsBufferSize(), equalTo(1));
}
Also used : SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) TestTimerService(org.apache.flink.cep.utils.TestTimerService) Test(org.junit.Test)

Example 4 with TestTimerService

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

the class NFAITCase method testSimplePatternWithTimeoutHandling.

/**
 * Tests that the NFA successfully returns partially matched event sequences when they've timed
 * out.
 */
@Test
public void testSimplePatternWithTimeoutHandling() throws Exception {
    List<StreamRecord<Event>> events = new ArrayList<>();
    List<Map<String, List<Event>>> resultingPatterns = new ArrayList<>();
    Set<Tuple2<Map<String, List<Event>>, Long>> resultingTimeoutPatterns = new HashSet<>();
    Set<Tuple2<Map<String, List<Event>>, Long>> expectedTimeoutPatterns = new HashSet<>();
    events.add(new StreamRecord<>(new Event(1, "start", 1.0), 1));
    events.add(new StreamRecord<>(new Event(2, "start", 1.0), 2));
    events.add(new StreamRecord<>(new Event(3, "middle", 1.0), 3));
    events.add(new StreamRecord<>(new Event(4, "foobar", 1.0), 4));
    events.add(new StreamRecord<>(new Event(5, "end", 1.0), 11));
    events.add(new StreamRecord<>(new Event(6, "end", 1.0), 13));
    Map<String, List<Event>> timeoutPattern1 = new HashMap<>();
    timeoutPattern1.put("start", Collections.singletonList(new Event(1, "start", 1.0)));
    timeoutPattern1.put("middle", Collections.singletonList(new Event(3, "middle", 1.0)));
    Map<String, List<Event>> timeoutPattern2 = new HashMap<>();
    timeoutPattern2.put("start", Collections.singletonList(new Event(2, "start", 1.0)));
    timeoutPattern2.put("middle", Collections.singletonList(new Event(3, "middle", 1.0)));
    Map<String, List<Event>> timeoutPattern3 = new HashMap<>();
    timeoutPattern3.put("start", Collections.singletonList(new Event(1, "start", 1.0)));
    Map<String, List<Event>> timeoutPattern4 = new HashMap<>();
    timeoutPattern4.put("start", Collections.singletonList(new Event(2, "start", 1.0)));
    expectedTimeoutPatterns.add(Tuple2.of(timeoutPattern1, 11L));
    expectedTimeoutPatterns.add(Tuple2.of(timeoutPattern2, 12L));
    expectedTimeoutPatterns.add(Tuple2.of(timeoutPattern3, 11L));
    expectedTimeoutPatterns.add(Tuple2.of(timeoutPattern4, 12L));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 7907391379273505897L;

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

        private static final long serialVersionUID = -3268741540234334074L;

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

        private static final long serialVersionUID = -8995174172182138608L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    }).within(Time.milliseconds(10));
    NFA<Event> nfa = compile(pattern, true);
    NFAState nfaState = nfa.createInitialNFAState();
    for (StreamRecord<Event> event : events) {
        Collection<Tuple2<Map<String, List<Event>>, Long>> timeoutPatterns = nfa.advanceTime(sharedBufferAccessor, nfaState, event.getTimestamp());
        Collection<Map<String, List<Event>>> matchedPatterns = nfa.process(sharedBufferAccessor, nfaState, event.getValue(), event.getTimestamp(), AfterMatchSkipStrategy.noSkip(), new TestTimerService());
        resultingPatterns.addAll(matchedPatterns);
        resultingTimeoutPatterns.addAll(timeoutPatterns);
    }
    assertEquals(1, resultingPatterns.size());
    assertEquals(expectedTimeoutPatterns.size(), resultingTimeoutPatterns.size());
    assertEquals(expectedTimeoutPatterns, resultingTimeoutPatterns);
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TestTimerService(org.apache.flink.cep.utils.TestTimerService) Tuple2(org.apache.flink.api.java.tuple.Tuple2) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Event (org.apache.flink.cep.Event)4 TestTimerService (org.apache.flink.cep.utils.TestTimerService)4 Test (org.junit.Test)4 SubEvent (org.apache.flink.cep.SubEvent)3 List (java.util.List)2 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)1 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)1