Java Code Examples for org.apache.flink.cep.nfa.sharedbuffer.SharedBufferAccessor#releaseNode()

The following examples show how to use org.apache.flink.cep.nfa.sharedbuffer.SharedBufferAccessor#releaseNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AfterMatchSkipStrategy.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Prunes matches/partial matches based on the chosen strategy.
 *
 * @param matchesToPrune current partial matches
 * @param matchedResult  already completed matches
 * @param sharedBufferAccessor   accessor to corresponding shared buffer
 * @throws Exception thrown if could not access the state
 */
public void prune(
		Collection<ComputationState> matchesToPrune,
		Collection<Map<String, List<EventId>>> matchedResult,
		SharedBufferAccessor<?> sharedBufferAccessor) throws Exception {

	EventId pruningId = getPruningId(matchedResult);
	if (pruningId != null) {
		List<ComputationState> discardStates = new ArrayList<>();
		for (ComputationState computationState : matchesToPrune) {
			if (computationState.getStartEventID() != null &&
				shouldPrune(computationState.getStartEventID(), pruningId)) {
				sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());
				discardStates.add(computationState);
			}
		}
		matchesToPrune.removeAll(discardStates);
	}
}
 
Example 2
Source File: AfterMatchSkipStrategy.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Prunes matches/partial matches based on the chosen strategy.
 *
 * @param matchesToPrune current partial matches
 * @param matchedResult  already completed matches
 * @param sharedBufferAccessor   accessor to corresponding shared buffer
 * @throws Exception thrown if could not access the state
 */
public void prune(
		Collection<ComputationState> matchesToPrune,
		Collection<Map<String, List<EventId>>> matchedResult,
		SharedBufferAccessor<?> sharedBufferAccessor) throws Exception {

	EventId pruningId = getPruningId(matchedResult);
	if (pruningId != null) {
		List<ComputationState> discardStates = new ArrayList<>();
		for (ComputationState computationState : matchesToPrune) {
			if (computationState.getStartEventID() != null &&
				shouldPrune(computationState.getStartEventID(), pruningId)) {
				sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());
				discardStates.add(computationState);
			}
		}
		matchesToPrune.removeAll(discardStates);
	}
}
 
Example 3
Source File: AfterMatchSkipStrategy.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Prunes matches/partial matches based on the chosen strategy.
 *
 * @param matchesToPrune current partial matches
 * @param matchedResult  already completed matches
 * @param sharedBufferAccessor   accessor to corresponding shared buffer
 * @throws Exception thrown if could not access the state
 */
public void prune(
		Collection<ComputationState> matchesToPrune,
		Collection<Map<String, List<EventId>>> matchedResult,
		SharedBufferAccessor<?> sharedBufferAccessor) throws Exception {

	EventId pruningId = getPruningId(matchedResult);
	if (pruningId != null) {
		List<ComputationState> discardStates = new ArrayList<>();
		for (ComputationState computationState : matchesToPrune) {
			if (computationState.getStartEventID() != null &&
				shouldPrune(computationState.getStartEventID(), pruningId)) {
				sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());
				discardStates.add(computationState);
			}
		}
		matchesToPrune.removeAll(discardStates);
	}
}
 
Example 4
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes states assuming there will be no events with timestamp <b>lower</b> than the given one.
 * It clears the sharedBuffer and also emits all timed out partial matches.
 *
 * @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 timestamp    timestamp that indicates that there will be no more events with lower timestamp
 * @return all timed outed partial matches
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Tuple2<Map<String, List<T>>, Long>> advanceTime(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final long timestamp) throws Exception {

	final Collection<Tuple2<Map<String, List<T>>, Long>> timeoutResult = new ArrayList<>();
	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	for (ComputationState computationState : nfaState.getPartialMatches()) {
		if (isStateTimedOut(computationState, timestamp)) {

			if (handleTimeout) {
				// extract the timed out event pattern
				Map<String, List<T>> timedOutPattern = sharedBufferAccessor.materializeMatch(extractCurrentMatches(
					sharedBufferAccessor,
					computationState));
				timeoutResult.add(Tuple2.of(timedOutPattern, computationState.getStartTimestamp() + windowTime));
			}

			sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());

			nfaState.setStateChanged();
		} else {
			newPartialMatches.add(computationState);
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	sharedBufferAccessor.advanceTime(timestamp);

	return timeoutResult;
}
 
Example 5
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes states assuming there will be no events with timestamp <b>lower</b> than the given one.
 * It clears the sharedBuffer and also emits all timed out partial matches.
 *
 * @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 timestamp    timestamp that indicates that there will be no more events with lower timestamp
 * @return all timed outed partial matches
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Tuple2<Map<String, List<T>>, Long>> advanceTime(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final long timestamp) throws Exception {

	final Collection<Tuple2<Map<String, List<T>>, Long>> timeoutResult = new ArrayList<>();
	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	for (ComputationState computationState : nfaState.getPartialMatches()) {
		if (isStateTimedOut(computationState, timestamp)) {

			if (handleTimeout) {
				// extract the timed out event pattern
				Map<String, List<T>> timedOutPattern = sharedBufferAccessor.materializeMatch(extractCurrentMatches(
					sharedBufferAccessor,
					computationState));
				timeoutResult.add(Tuple2.of(timedOutPattern, computationState.getStartTimestamp() + windowTime));
			}

			sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());

			nfaState.setStateChanged();
		} else {
			newPartialMatches.add(computationState);
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	sharedBufferAccessor.advanceTime(timestamp);

	return timeoutResult;
}
 
Example 6
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Prunes states assuming there will be no events with timestamp <b>lower</b> than the given one.
 * It clears the sharedBuffer and also emits all timed out partial matches.
 *
 * @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 timestamp    timestamp that indicates that there will be no more events with lower timestamp
 * @return all timed outed partial matches
 * @throws Exception Thrown if the system cannot access the state.
 */
public Collection<Tuple2<Map<String, List<T>>, Long>> advanceTime(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final long timestamp) throws Exception {

	final Collection<Tuple2<Map<String, List<T>>, Long>> timeoutResult = new ArrayList<>();
	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	for (ComputationState computationState : nfaState.getPartialMatches()) {
		if (isStateTimedOut(computationState, timestamp)) {

			if (handleTimeout) {
				// extract the timed out event pattern
				Map<String, List<T>> timedOutPattern = sharedBufferAccessor.materializeMatch(extractCurrentMatches(
					sharedBufferAccessor,
					computationState));
				timeoutResult.add(Tuple2.of(timedOutPattern, computationState.getStartTimestamp() + windowTime));
			}

			sharedBufferAccessor.releaseNode(computationState.getPreviousBufferEntry());

			nfaState.setStateChanged();
		} else {
			newPartialMatches.add(computationState);
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	sharedBufferAccessor.advanceTime(timestamp);

	return timeoutResult;
}
 
Example 7
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example 8
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void processMatchesAccordingToSkipStrategy(
		SharedBufferAccessor<T> sharedBufferAccessor,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		PriorityQueue<ComputationState> potentialMatches,
		PriorityQueue<ComputationState> partialMatches,
		List<Map<String, List<T>>> result) throws Exception {

	nfaState.getCompletedMatches().addAll(potentialMatches);

	ComputationState earliestMatch = nfaState.getCompletedMatches().peek();

	if (earliestMatch != null) {

		ComputationState earliestPartialMatch;
		while (earliestMatch != null && ((earliestPartialMatch = partialMatches.peek()) == null ||
			isEarlier(earliestMatch, earliestPartialMatch))) {

			nfaState.setStateChanged();
			nfaState.getCompletedMatches().poll();
			List<Map<String, List<EventId>>> matchedResult =
				sharedBufferAccessor.extractPatterns(earliestMatch.getPreviousBufferEntry(), earliestMatch.getVersion());

			afterMatchSkipStrategy.prune(
				partialMatches,
				matchedResult,
				sharedBufferAccessor);

			afterMatchSkipStrategy.prune(
				nfaState.getCompletedMatches(),
				matchedResult,
				sharedBufferAccessor);

			result.add(sharedBufferAccessor.materializeMatch(matchedResult.get(0)));
			sharedBufferAccessor.releaseNode(earliestMatch.getPreviousBufferEntry());
			earliestMatch = nfaState.getCompletedMatches().peek();
		}

		nfaState.getPartialMatches().removeIf(pm -> pm.getStartEventID() != null && !partialMatches.contains(pm));
	}
}
 
Example 9
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example 10
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private void processMatchesAccordingToSkipStrategy(
		SharedBufferAccessor<T> sharedBufferAccessor,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		PriorityQueue<ComputationState> potentialMatches,
		PriorityQueue<ComputationState> partialMatches,
		List<Map<String, List<T>>> result) throws Exception {

	nfaState.getCompletedMatches().addAll(potentialMatches);

	ComputationState earliestMatch = nfaState.getCompletedMatches().peek();

	if (earliestMatch != null) {

		ComputationState earliestPartialMatch;
		while (earliestMatch != null && ((earliestPartialMatch = partialMatches.peek()) == null ||
			isEarlier(earliestMatch, earliestPartialMatch))) {

			nfaState.setStateChanged();
			nfaState.getCompletedMatches().poll();
			List<Map<String, List<EventId>>> matchedResult =
				sharedBufferAccessor.extractPatterns(earliestMatch.getPreviousBufferEntry(), earliestMatch.getVersion());

			afterMatchSkipStrategy.prune(
				partialMatches,
				matchedResult,
				sharedBufferAccessor);

			afterMatchSkipStrategy.prune(
				nfaState.getCompletedMatches(),
				matchedResult,
				sharedBufferAccessor);

			result.add(sharedBufferAccessor.materializeMatch(matchedResult.get(0)));
			sharedBufferAccessor.releaseNode(earliestMatch.getPreviousBufferEntry());
			earliestMatch = nfaState.getCompletedMatches().peek();
		}

		nfaState.getPartialMatches().removeIf(pm -> pm.getStartEventID() != null && !partialMatches.contains(pm));
	}
}
 
Example 11
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private Collection<Map<String, List<T>>> doProcess(
		final SharedBufferAccessor<T> sharedBufferAccessor,
		final NFAState nfaState,
		final EventWrapper event,
		final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final TimerService timerService) throws Exception {

	final PriorityQueue<ComputationState> newPartialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);
	final PriorityQueue<ComputationState> potentialMatches = new PriorityQueue<>(NFAState.COMPUTATION_STATE_COMPARATOR);

	// iterate over all current computations
	for (ComputationState computationState : nfaState.getPartialMatches()) {
		final Collection<ComputationState> newComputationStates = computeNextStates(
			sharedBufferAccessor,
			computationState,
			event,
			timerService);

		if (newComputationStates.size() != 1) {
			nfaState.setStateChanged();
		} else if (!newComputationStates.iterator().next().equals(computationState)) {
			nfaState.setStateChanged();
		}

		//delay adding new computation states in case a stop state is reached and we discard the path.
		final Collection<ComputationState> statesToRetain = new ArrayList<>();
		//if stop state reached in this path
		boolean shouldDiscardPath = false;
		for (final ComputationState newComputationState : newComputationStates) {

			if (isFinalState(newComputationState)) {
				potentialMatches.add(newComputationState);
			} else if (isStopState(newComputationState)) {
				//reached stop state. release entry for the stop state
				shouldDiscardPath = true;
				sharedBufferAccessor.releaseNode(newComputationState.getPreviousBufferEntry());
			} else {
				// add new computation state; it will be processed once the next event arrives
				statesToRetain.add(newComputationState);
			}
		}

		if (shouldDiscardPath) {
			// a stop state was reached in this branch. release branch which results in removing previous event from
			// the buffer
			for (final ComputationState state : statesToRetain) {
				sharedBufferAccessor.releaseNode(state.getPreviousBufferEntry());
			}
		} else {
			newPartialMatches.addAll(statesToRetain);
		}
	}

	if (!potentialMatches.isEmpty()) {
		nfaState.setStateChanged();
	}

	List<Map<String, List<T>>> result = new ArrayList<>();
	if (afterMatchSkipStrategy.isSkipStrategy()) {
		processMatchesAccordingToSkipStrategy(sharedBufferAccessor,
			nfaState,
			afterMatchSkipStrategy,
			potentialMatches,
			newPartialMatches,
			result);
	} else {
		for (ComputationState match : potentialMatches) {
			Map<String, List<T>> materializedMatch =
				sharedBufferAccessor.materializeMatch(
					sharedBufferAccessor.extractPatterns(
						match.getPreviousBufferEntry(),
						match.getVersion()).get(0)
				);

			result.add(materializedMatch);
			sharedBufferAccessor.releaseNode(match.getPreviousBufferEntry());
		}
	}

	nfaState.setNewPartialMatches(newPartialMatches);

	return result;
}
 
Example 12
Source File: NFA.java    From flink with Apache License 2.0 4 votes vote down vote up
private void processMatchesAccordingToSkipStrategy(
		SharedBufferAccessor<T> sharedBufferAccessor,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		PriorityQueue<ComputationState> potentialMatches,
		PriorityQueue<ComputationState> partialMatches,
		List<Map<String, List<T>>> result) throws Exception {

	nfaState.getCompletedMatches().addAll(potentialMatches);

	ComputationState earliestMatch = nfaState.getCompletedMatches().peek();

	if (earliestMatch != null) {

		ComputationState earliestPartialMatch;
		while (earliestMatch != null && ((earliestPartialMatch = partialMatches.peek()) == null ||
			isEarlier(earliestMatch, earliestPartialMatch))) {

			nfaState.setStateChanged();
			nfaState.getCompletedMatches().poll();
			List<Map<String, List<EventId>>> matchedResult =
				sharedBufferAccessor.extractPatterns(earliestMatch.getPreviousBufferEntry(), earliestMatch.getVersion());

			afterMatchSkipStrategy.prune(
				partialMatches,
				matchedResult,
				sharedBufferAccessor);

			afterMatchSkipStrategy.prune(
				nfaState.getCompletedMatches(),
				matchedResult,
				sharedBufferAccessor);

			result.add(sharedBufferAccessor.materializeMatch(matchedResult.get(0)));
			sharedBufferAccessor.releaseNode(earliestMatch.getPreviousBufferEntry());
			earliestMatch = nfaState.getCompletedMatches().peek();
		}

		nfaState.getPartialMatches().removeIf(pm -> pm.getStartEventID() != null && !partialMatches.contains(pm));
	}
}