Java Code Examples for org.apache.flink.cep.nfa.StateTransitionAction#PROCEED

The following examples show how to use org.apache.flink.cep.nfa.StateTransitionAction#PROCEED . 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: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies if the provided pattern can possibly generate empty match. Example of patterns that can possibly
 * generate empty matches are: A*, A?, A* B? etc.
 *
 * @param pattern pattern to check
 * @return true if empty match could potentially match the pattern, false otherwise
 */
public static boolean canProduceEmptyMatches(final Pattern<?, ?> pattern) {
	NFAFactoryCompiler<?> compiler = new NFAFactoryCompiler<>(checkNotNull(pattern));
	compiler.compileFactory();
	State<?> startState = compiler.getStates().stream().filter(State::isStart).findFirst().orElseThrow(
		() -> new IllegalStateException("Compiler produced no start state. It is a bug. File a jira."));

	Set<State<?>> visitedStates = new HashSet<>();
	final Stack<State<?>> statesToCheck = new Stack<>();
	statesToCheck.push(startState);
	while (!statesToCheck.isEmpty()) {
		final State<?> currentState = statesToCheck.pop();
		if (visitedStates.contains(currentState)) {
			continue;
		} else {
			visitedStates.add(currentState);
		}

		for (StateTransition<?> transition : currentState.getStateTransitions()) {
			if (transition.getAction() == StateTransitionAction.PROCEED) {
				if (transition.getTargetState().isFinal()) {
					return true;
				} else {
					statesToCheck.push(transition.getTargetState());
				}
			}
		}
	}

	return false;
}
 
Example 2
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies if the provided pattern can possibly generate empty match. Example of patterns that can possibly
 * generate empty matches are: A*, A?, A* B? etc.
 *
 * @param pattern pattern to check
 * @return true if empty match could potentially match the pattern, false otherwise
 */
public static boolean canProduceEmptyMatches(final Pattern<?, ?> pattern) {
	NFAFactoryCompiler<?> compiler = new NFAFactoryCompiler<>(checkNotNull(pattern));
	compiler.compileFactory();
	State<?> startState = compiler.getStates().stream().filter(State::isStart).findFirst().orElseThrow(
		() -> new IllegalStateException("Compiler produced no start state. It is a bug. File a jira."));

	Set<State<?>> visitedStates = new HashSet<>();
	final Stack<State<?>> statesToCheck = new Stack<>();
	statesToCheck.push(startState);
	while (!statesToCheck.isEmpty()) {
		final State<?> currentState = statesToCheck.pop();
		if (visitedStates.contains(currentState)) {
			continue;
		} else {
			visitedStates.add(currentState);
		}

		for (StateTransition<?> transition : currentState.getStateTransitions()) {
			if (transition.getAction() == StateTransitionAction.PROCEED) {
				if (transition.getTargetState().isFinal()) {
					return true;
				} else {
					statesToCheck.push(transition.getTargetState());
				}
			}
		}
	}

	return false;
}
 
Example 3
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies if the provided pattern can possibly generate empty match. Example of patterns that can possibly
 * generate empty matches are: A*, A?, A* B? etc.
 *
 * @param pattern pattern to check
 * @return true if empty match could potentially match the pattern, false otherwise
 */
public static boolean canProduceEmptyMatches(final Pattern<?, ?> pattern) {
	NFAFactoryCompiler<?> compiler = new NFAFactoryCompiler<>(checkNotNull(pattern));
	compiler.compileFactory();
	State<?> startState = compiler.getStates().stream().filter(State::isStart).findFirst().orElseThrow(
		() -> new IllegalStateException("Compiler produced no start state. It is a bug. File a jira."));

	Set<State<?>> visitedStates = new HashSet<>();
	final Stack<State<?>> statesToCheck = new Stack<>();
	statesToCheck.push(startState);
	while (!statesToCheck.isEmpty()) {
		final State<?> currentState = statesToCheck.pop();
		if (visitedStates.contains(currentState)) {
			continue;
		} else {
			visitedStates.add(currentState);
		}

		for (StateTransition<?> transition : currentState.getStateTransitions()) {
			if (transition.getAction() == StateTransitionAction.PROCEED) {
				if (transition.getTargetState().isFinal()) {
					return true;
				} else {
					statesToCheck.push(transition.getTargetState());
				}
			}
		}
	}

	return false;
}
 
Example 4
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * This method creates an alternative state that is target for TAKE transition from an optional State.
 * Accepting an event in optional State discards all not Patterns that were present before it.
 *
 * <p>E.g for a Pattern begin("a").notFollowedBy("b").followedByAny("c").optional().followedByAny("d")
 * a sequence like : {a c b d} is a valid match, but {a b d} is not.
 *
 * <p><b>NOTICE:</b> This method creates copy only if it necessary.
 *
 * @param sinkState a state to create copy without transitive nots
 * @return the copy of the state itself if no modifications were needed
 */
private State<T> copyWithoutTransitiveNots(final State<T> sinkState) {
	final List<Tuple2<IterativeCondition<T>, String>> currentNotCondition = getCurrentNotCondition();

	if (currentNotCondition.isEmpty() ||
		!currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL)) {
		//we do not create an alternative path if we are NOT in an OPTIONAL state or there is no NOTs prior to
		//the optional state
		return sinkState;
	}

	final State<T> copyOfSink = createState(sinkState.getName(), sinkState.getStateType());

	for (StateTransition<T> tStateTransition : sinkState.getStateTransitions()) {

		if (tStateTransition.getAction() == StateTransitionAction.PROCEED) {
			State<T> targetState = tStateTransition.getTargetState();
			boolean remove = false;
			if (targetState.isStop()) {
				for (Tuple2<IterativeCondition<T>, String> notCondition : currentNotCondition) {
					if (targetState.getName().equals(notCondition.f1)) {
						remove = true;
					}
				}
			} else {
				targetState = copyWithoutTransitiveNots(tStateTransition.getTargetState());
			}

			if (!remove) {
				copyOfSink.addStateTransition(tStateTransition.getAction(), targetState, tStateTransition.getCondition());
			}
		} else {
			copyOfSink.addStateTransition(
					tStateTransition.getAction(),
					tStateTransition.getTargetState().equals(tStateTransition.getSourceState())
							? copyOfSink
							: tStateTransition.getTargetState(),
					tStateTransition.getCondition()
			);
		}

	}
	return copyOfSink;
}
 
Example 5
Source File: NFACompiler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This method creates an alternative state that is target for TAKE transition from an optional State.
 * Accepting an event in optional State discards all not Patterns that were present before it.
 *
 * <p>E.g for a Pattern begin("a").notFollowedBy("b").followedByAny("c").optional().followedByAny("d")
 * a sequence like : {a c b d} is a valid match, but {a b d} is not.
 *
 * <p><b>NOTICE:</b> This method creates copy only if it necessary.
 *
 * @param sinkState a state to create copy without transitive nots
 * @return the copy of the state itself if no modifications were needed
 */
private State<T> copyWithoutTransitiveNots(final State<T> sinkState) {
	final List<Tuple2<IterativeCondition<T>, String>> currentNotCondition = getCurrentNotCondition();

	if (currentNotCondition.isEmpty() ||
		!currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL)) {
		//we do not create an alternative path if we are NOT in an OPTIONAL state or there is no NOTs prior to
		//the optional state
		return sinkState;
	}

	final State<T> copyOfSink = createState(sinkState.getName(), sinkState.getStateType());

	for (StateTransition<T> tStateTransition : sinkState.getStateTransitions()) {

		if (tStateTransition.getAction() == StateTransitionAction.PROCEED) {
			State<T> targetState = tStateTransition.getTargetState();
			boolean remove = false;
			if (targetState.isStop()) {
				for (Tuple2<IterativeCondition<T>, String> notCondition : currentNotCondition) {
					if (targetState.getName().equals(notCondition.f1)) {
						remove = true;
					}
				}
			} else {
				targetState = copyWithoutTransitiveNots(tStateTransition.getTargetState());
			}

			if (!remove) {
				copyOfSink.addStateTransition(tStateTransition.getAction(), targetState, tStateTransition.getCondition());
			}
		} else {
			copyOfSink.addStateTransition(
					tStateTransition.getAction(),
					tStateTransition.getTargetState().equals(tStateTransition.getSourceState())
							? copyOfSink
							: tStateTransition.getTargetState(),
					tStateTransition.getCondition()
			);
		}

	}
	return copyOfSink;
}
 
Example 6
Source File: NFACompiler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This method creates an alternative state that is target for TAKE transition from an optional State.
 * Accepting an event in optional State discards all not Patterns that were present before it.
 *
 * <p>E.g for a Pattern begin("a").notFollowedBy("b").followedByAny("c").optional().followedByAny("d")
 * a sequence like : {a c b d} is a valid match, but {a b d} is not.
 *
 * <p><b>NOTICE:</b> This method creates copy only if it necessary.
 *
 * @param sinkState a state to create copy without transitive nots
 * @return the copy of the state itself if no modifications were needed
 */
private State<T> copyWithoutTransitiveNots(final State<T> sinkState) {
	final List<Tuple2<IterativeCondition<T>, String>> currentNotCondition = getCurrentNotCondition();

	if (currentNotCondition.isEmpty() ||
		!currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL)) {
		//we do not create an alternative path if we are NOT in an OPTIONAL state or there is no NOTs prior to
		//the optional state
		return sinkState;
	}

	final State<T> copyOfSink = createState(sinkState.getName(), sinkState.getStateType());

	for (StateTransition<T> tStateTransition : sinkState.getStateTransitions()) {

		if (tStateTransition.getAction() == StateTransitionAction.PROCEED) {
			State<T> targetState = tStateTransition.getTargetState();
			boolean remove = false;
			if (targetState.isStop()) {
				for (Tuple2<IterativeCondition<T>, String> notCondition : currentNotCondition) {
					if (targetState.getName().equals(notCondition.f1)) {
						remove = true;
					}
				}
			} else {
				targetState = copyWithoutTransitiveNots(tStateTransition.getTargetState());
			}

			if (!remove) {
				copyOfSink.addStateTransition(tStateTransition.getAction(), targetState, tStateTransition.getCondition());
			}
		} else {
			copyOfSink.addStateTransition(
					tStateTransition.getAction(),
					tStateTransition.getTargetState().equals(tStateTransition.getSourceState())
							? copyOfSink
							: tStateTransition.getTargetState(),
					tStateTransition.getCondition()
			);
		}

	}
	return copyOfSink;
}