@Override public NFA<T> createNFA() { return new NFA<>(states, windowTime, timeoutHandling); } }
@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); } } }
final OutgoingEdges<T> outgoingEdges = createDecisionGraph(context, computationState, event.getEvent()); switch (edge.getAction()) { case IGNORE: { if (!isStartState(computationState)) { final DeweyNumber version; if (isEquivalentState(edge.getTargetState(), getState(computationState))) { final int toIncrease = calculateIncreasingSelfState( outgoingEdges.getTotalIgnoreBranches(), outgoingEdges.getTotalTakeBranches()); addComputationState( sharedBufferAccessor, resultingComputationStates, if (isStartState(computationState)) { startTimestamp = event.getTimestamp(); startEventId = event.getEventId(); addComputationState( sharedBufferAccessor, resultingComputationStates, final State<T> finalState = findFinalStateAfterProceed(context, nextState, event.getEvent()); if (finalState != null) { addComputationState( sharedBufferAccessor, resultingComputationStates,
ComputationState computationState, T event) { State<T> state = getState(computationState); final OutgoingEdges<T> outgoingEdges = new OutgoingEdges<>(state); if (checkFilterCondition(context, stateTransition.getCondition(), event)) {
if (isStateTimedOut(computationState, timestamp)) { Map<String, List<T>> timedOutPattern = sharedBufferAccessor.materializeMatch(extractCurrentMatches( sharedBufferAccessor, computationState));
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); } }
final Collection<ComputationState> newComputationStates = computeNextStates( sharedBufferAccessor, computationState, for (final ComputationState newComputationState : newComputationStates) { if (isFinalState(newComputationState)) { potentialMatches.add(newComputationState); } else if (isStopState(newComputationState)) { processMatchesAccordingToSkipStrategy(sharedBufferAccessor, nfaState, afterMatchSkipStrategy,
final OutgoingEdges<T> outgoingEdges = createDecisionGraph(computationState, event); if (!computationState.isStartState()) { final DeweyNumber version; if (isEquivalentState(edge.getTargetState(), computationState.getState())) { final int toIncrease = calculateIncreasingSelfState( outgoingEdges.getTotalIgnoreBranches(), outgoingEdges.getTotalTakeBranches()); addComputationState( resultingComputationStates, edge.getTargetState(), addComputationState( resultingComputationStates, nextState, final State<T> finalState = findFinalStateAfterProceed(nextState, event, computationState); if (finalState != null) { addComputationState( resultingComputationStates, finalState, int totalBranches = calculateIncreasingSelfState( outgoingEdges.getTotalIgnoreBranches(), outgoingEdges.getTotalTakeBranches());
private NFAState getNFAState() throws IOException { NFAState nfaState = computationStates.value(); return nfaState != null ? nfaState : nfa.createInitialNFAState(); }
/** * 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); } }
NFAState nfaState = nfa.createInitialNFAState(); NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build(); nfa.advanceTime(sharedBufferAccessor, nfaState, 10);
@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()); }
private State<T> findFinalStateAfterProceed( ConditionContext context, State<T> state, T event) { final Stack<State<T>> statesToCheck = new Stack<>(); statesToCheck.push(state); try { while (!statesToCheck.isEmpty()) { final State<T> currentState = statesToCheck.pop(); for (StateTransition<T> transition : currentState.getStateTransitions()) { if (transition.getAction() == StateTransitionAction.PROCEED && checkFilterCondition(context, transition.getCondition(), event)) { if (transition.getTargetState().isFinal()) { return transition.getTargetState(); } else { statesToCheck.push(transition.getTargetState()); } } } } } catch (Exception e) { throw new FlinkRuntimeException("Failure happened in filter function.", e); } return null; }
Map<String, List<T>> timedoutPattern = extractCurrentMatches(computationState); timeoutResult.add(Tuple2.of(timedoutPattern, timestamp)); newComputationStates = computeNextStates(computationState, event, timestamp); } else { newComputationStates = Collections.singleton(computationState); Map<String, List<T>> matchedPattern = extractCurrentMatches(newComputationState); result.add(matchedPattern);
/** * Advances the time for the given NFA to the given timestamp. This means that no more events with timestamp * <b>lower</b> than the given timestamp should be passed to the nfa, This can lead to pruning and timeouts. */ private void advanceTime(NFAState nfaState, long timestamp) throws Exception { try (SharedBufferAccessor<IN> sharedBufferAccessor = partialMatches.getAccessor()) { Collection<Tuple2<Map<String, List<IN>>, Long>> timedOut = nfa.advanceTime(sharedBufferAccessor, nfaState, timestamp); if (!timedOut.isEmpty()) { processTimedOutSequences(timedOut); } } }
final Collection<ComputationState> newComputationStates = computeNextStates( sharedBufferAccessor, computationState, for (final ComputationState newComputationState : newComputationStates) { if (isFinalState(newComputationState)) { potentialMatches.add(newComputationState); } else if (isStopState(newComputationState)) { processMatchesAccordingToSkipStrategy(sharedBufferAccessor, nfaState, afterMatchSkipStrategy,
NFATestHarnessBuilderNFA(NFA<Event> nfa) { super(AfterMatchSkipStrategy.noSkip()); this.nfa = nfa; this.nfaState = nfa.createInitialNFAState(); }
@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)); } }
NFAState nfaState = nfa.createInitialNFAState(); NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build(); nfa.advanceTime(sharedBufferAccessor, nfaState, 10);