/** * Process the given event by giving it to the NFA and outputting the produced set of matched * event sequences. * * @param nfaState Our NFAState object * @param event The current event to be processed * @param timestamp The timestamp of the event */ private void processEvent(NFAState nfaState, IN event, long timestamp) throws Exception { try (SharedBufferAccessor<IN> sharedBufferAccessor = partialMatches.getAccessor()) { Collection<Map<String, List<IN>>> patterns = nfa.process(sharedBufferAccessor, nfaState, event, timestamp, afterMatchSkipStrategy, cepTimerService); processMatchedSequences(patterns, timestamp); } }
@Test public void testProcessingTimestampisPassedToNFA() throws Exception { final NFA<Event> nfa = NFACompiler.compileFactory(Pattern.<Event>begin("begin"), true).createNFA(); final NFA<Event> spyNFA = spy(nfa); try ( OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(createOperatorForNFA(spyNFA).build())) { long timestamp = 5; harness.open(); harness.setProcessingTime(timestamp); StreamRecord<Event> event = event().withTimestamp(3).asStreamRecord(); harness.processElement(event); verify(spyNFA).process( any(SharedBufferAccessor.class), any(NFAState.class), eq(event.getValue()), eq(timestamp), any(AfterMatchSkipStrategy.class), any(TimerService.class)); } }
public Collection<Map<String, List<Event>>> consumeRecord(StreamRecord<Event> inputEvent) throws Exception { try (SharedBufferAccessor<Event> sharedBufferAccessor = sharedBuffer.getAccessor()) { nfa.advanceTime(sharedBufferAccessor, nfaState, inputEvent.getTimestamp()); return nfa.process( sharedBufferAccessor, nfaState, inputEvent.getValue(), inputEvent.getTimestamp(), afterMatchSkipStrategy, timerService); } }
@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); } } }
nfa.advanceTime(sharedBufferAccessor, nfaState, event.getTimestamp()); Collection<Map<String, List<Event>>> matchedPatterns = nfa.process(sharedBufferAccessor, nfaState, event.getValue(),
@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()); }
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()); 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()); 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()); 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()); 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()); 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());
@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()); } }
@Override protected void processEvent(NFA<IN> nfa, IN event, long timestamp) { Tuple2<Collection<Map<String, List<IN>>>, Collection<Tuple2<Map<String, List<IN>>, Long>>> patterns = nfa.process(event, timestamp); emitMatchedSequences(patterns.f0, timestamp); }
@Override protected void advanceTime(NFA<IN> nfa, long timestamp) { Tuple2<Collection<Map<String, List<IN>>>, Collection<Tuple2<Map<String, List<IN>>, Long>>> patterns = nfa.process(null, timestamp); emitMatchedSequences(patterns.f0, timestamp); }
/** * Processes the next input event. If some of the computations reach a final state then the * resulting event sequences are returned. If computations time out and timeout handling is * activated, then the timed out event patterns are returned. * * <p>If computations reach a stop state, the path forward is discarded and currently constructed path is returned * with the element that resulted in the stop state. * * @param sharedBufferAccessor the accessor to SharedBuffer object that we need to work upon while processing * @param nfaState The NFAState object that we need to affect while processing * @param event The current event to be processed or null if only pruning shall be done * @param timestamp The timestamp of the current event * @return Tuple of the collection of matched patterns (e.g. the result of computations which have * reached a final state) and the collection of timed out patterns (if timeout handling is * activated) * @throws Exception Thrown if the system cannot access the state. */ public Collection<Map<String, List<T>>> process( final SharedBufferAccessor<T> sharedBufferAccessor, final NFAState nfaState, final T event, final long timestamp) throws Exception { return process(sharedBufferAccessor, nfaState, event, timestamp, AfterMatchSkipStrategy.noSkip()); }
/** * Processes the next input event. If some of the computations reach a final state then the * resulting event sequences are returned. If computations time out and timeout handling is * activated, then the timed out event patterns are returned. * * <p>If computations reach a stop state, the path forward is discarded and currently constructed path is returned * with the element that resulted in the stop state. * * @param sharedBufferAccessor the accessor to SharedBuffer object that we need to work upon while processing * @param nfaState The NFAState object that we need to affect while processing * @param event The current event to be processed or null if only pruning shall be done * @param timestamp The timestamp of the current event * @return Tuple of the collection of matched patterns (e.g. the result of computations which have * reached a final state) and the collection of timed out patterns (if timeout handling is * activated) * @throws Exception Thrown if the system cannot access the state. */ public Collection<Map<String, List<T>>> process( final SharedBufferAccessor<T> sharedBufferAccessor, final NFAState nfaState, final T event, final long timestamp) throws Exception { return process(sharedBufferAccessor, nfaState, event, timestamp, AfterMatchSkipStrategy.noSkip()); }
@Override protected void advanceTime(NFA<IN> nfa, long timestamp) { Tuple2<Collection<Map<String, List<IN>>>, Collection<Tuple2<Map<String, List<IN>>, Long>>> patterns = nfa.process(null, timestamp); emitMatchedSequences(patterns.f0, timestamp); emitTimedOutSequences(patterns.f1, timestamp); }
@Override protected void processEvent(NFA<IN> nfa, IN event, long timestamp) { Tuple2<Collection<Map<String, List<IN>>>, Collection<Tuple2<Map<String, List<IN>>, Long>>> patterns = nfa.process(event, timestamp); emitMatchedSequences(patterns.f0, timestamp); emitTimedOutSequences(patterns.f1, timestamp); }
/** * Process the given event by giving it to the NFA and outputting the produced set of matched * event sequences. * * @param nfaState Our NFAState object * @param event The current event to be processed * @param timestamp The timestamp of the event */ private void processEvent(NFAState nfaState, IN event, long timestamp) throws Exception { try (SharedBufferAccessor<IN> sharedBufferAccessor = partialMatches.getAccessor()) { Collection<Map<String, List<IN>>> patterns = nfa.process(sharedBufferAccessor, nfaState, event, timestamp, afterMatchSkipStrategy); processMatchedSequences(patterns, timestamp); } }
/** * Process the given event by giving it to the NFA and outputting the produced set of matched * event sequences. * * @param nfaState Our NFAState object * @param event The current event to be processed * @param timestamp The timestamp of the event */ private void processEvent(NFAState nfaState, IN event, long timestamp) throws Exception { try (SharedBufferAccessor<IN> sharedBufferAccessor = partialMatches.getAccessor()) { Collection<Map<String, List<IN>>> patterns = nfa.process(sharedBufferAccessor, nfaState, event, timestamp, afterMatchSkipStrategy); processMatchedSequences(patterns, timestamp); } }