org.apache.flink.cep.nfa.StateTransitionAction Java Examples

The following examples show how to use org.apache.flink.cep.nfa.StateTransitionAction. 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: NFACompilerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private <T> Set<Tuple2<String, StateTransitionAction>> unfoldTransitions(final State<T> state) {
	final Set<Tuple2<String, StateTransitionAction>> transitions = new HashSet<>();
	for (StateTransition<T> transition : state.getStateTransitions()) {
		transitions.add(Tuple2.of(
			transition.getTargetState().getName(),
			transition.getAction()));
	}
	return transitions;
}
 
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: NFACompilerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private <T> Set<Tuple2<String, StateTransitionAction>> unfoldTransitions(final State<T> state) {
	final Set<Tuple2<String, StateTransitionAction>> transitions = new HashSet<>();
	for (StateTransition<T> transition : state.getStateTransitions()) {
		transitions.add(Tuple2.of(
			transition.getTargetState().getName(),
			transition.getAction()));
	}
	return transitions;
}
 
Example #5
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 #6
Source File: NFACompilerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private <T> Set<Tuple2<String, StateTransitionAction>> unfoldTransitions(final State<T> state) {
	final Set<Tuple2<String, StateTransitionAction>> transitions = new HashSet<>();
	for (StateTransition<T> transition : state.getStateTransitions()) {
		transitions.add(Tuple2.of(
			transition.getTargetState().getName(),
			transition.getAction()));
	}
	return transitions;
}
 
Example #7
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 #8
Source File: NFACompilerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the NFACompiler generates the correct NFA from a given Pattern.
 */
@Test
public void testNFACompilerWithSimplePattern() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.followedBy("middle").subtype(SubEvent.class)
		.next("end").where(endFilter);

	NFA<Event> nfa = compile(pattern, false);

	Collection<State<Event>> states = nfa.getStates();
	assertEquals(4, states.size());

	Map<String, State<Event>> stateMap = new HashMap<>();
	for (State<Event> state : states) {
		stateMap.put(state.getName(), state);
	}

	assertTrue(stateMap.containsKey("start"));
	State<Event> startState = stateMap.get("start");
	assertTrue(startState.isStart());
	final Set<Tuple2<String, StateTransitionAction>> startTransitions = unfoldTransitions(startState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.TAKE)
	), startTransitions);

	assertTrue(stateMap.containsKey("middle"));
	State<Event> middleState = stateMap.get("middle");
	final Set<Tuple2<String, StateTransitionAction>> middleTransitions = unfoldTransitions(middleState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.IGNORE),
		Tuple2.of("end", StateTransitionAction.TAKE)
	), middleTransitions);

	assertTrue(stateMap.containsKey("end"));
	State<Event> endState = stateMap.get("end");
	final Set<Tuple2<String, StateTransitionAction>> endTransitions = unfoldTransitions(endState);
	assertEquals(Sets.newHashSet(
		Tuple2.of(NFACompiler.ENDING_STATE_NAME, StateTransitionAction.TAKE)
	), endTransitions);

	assertTrue(stateMap.containsKey(NFACompiler.ENDING_STATE_NAME));
	State<Event> endingState = stateMap.get(NFACompiler.ENDING_STATE_NAME);
	assertTrue(endingState.isFinal());
	assertEquals(0, endingState.getStateTransitions().size());
}
 
Example #9
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 #10
Source File: NFACompilerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the NFACompiler generates the correct NFA from a given Pattern.
 */
@Test
public void testNFACompilerWithSimplePattern() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.followedBy("middle").subtype(SubEvent.class)
		.next("end").where(endFilter);

	NFA<Event> nfa = compile(pattern, false);

	Collection<State<Event>> states = nfa.getStates();
	assertEquals(4, states.size());

	Map<String, State<Event>> stateMap = new HashMap<>();
	for (State<Event> state : states) {
		stateMap.put(state.getName(), state);
	}

	assertTrue(stateMap.containsKey("start"));
	State<Event> startState = stateMap.get("start");
	assertTrue(startState.isStart());
	final Set<Tuple2<String, StateTransitionAction>> startTransitions = unfoldTransitions(startState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.TAKE)
	), startTransitions);

	assertTrue(stateMap.containsKey("middle"));
	State<Event> middleState = stateMap.get("middle");
	final Set<Tuple2<String, StateTransitionAction>> middleTransitions = unfoldTransitions(middleState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.IGNORE),
		Tuple2.of("end", StateTransitionAction.TAKE)
	), middleTransitions);

	assertTrue(stateMap.containsKey("end"));
	State<Event> endState = stateMap.get("end");
	final Set<Tuple2<String, StateTransitionAction>> endTransitions = unfoldTransitions(endState);
	assertEquals(Sets.newHashSet(
		Tuple2.of(NFACompiler.ENDING_STATE_NAME, StateTransitionAction.TAKE)
	), endTransitions);

	assertTrue(stateMap.containsKey(NFACompiler.ENDING_STATE_NAME));
	State<Event> endingState = stateMap.get(NFACompiler.ENDING_STATE_NAME);
	assertTrue(endingState.isFinal());
	assertEquals(0, endingState.getStateTransitions().size());
}
 
Example #11
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 #12
Source File: NFACompilerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the NFACompiler generates the correct NFA from a given Pattern.
 */
@Test
public void testNFACompilerWithSimplePattern() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.followedBy("middle").subtype(SubEvent.class)
		.next("end").where(endFilter);

	NFA<Event> nfa = compile(pattern, false);

	Collection<State<Event>> states = nfa.getStates();
	assertEquals(4, states.size());

	Map<String, State<Event>> stateMap = new HashMap<>();
	for (State<Event> state : states) {
		stateMap.put(state.getName(), state);
	}

	assertTrue(stateMap.containsKey("start"));
	State<Event> startState = stateMap.get("start");
	assertTrue(startState.isStart());
	final Set<Tuple2<String, StateTransitionAction>> startTransitions = unfoldTransitions(startState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.TAKE)
	), startTransitions);

	assertTrue(stateMap.containsKey("middle"));
	State<Event> middleState = stateMap.get("middle");
	final Set<Tuple2<String, StateTransitionAction>> middleTransitions = unfoldTransitions(middleState);
	assertEquals(Sets.newHashSet(
		Tuple2.of("middle", StateTransitionAction.IGNORE),
		Tuple2.of("end", StateTransitionAction.TAKE)
	), middleTransitions);

	assertTrue(stateMap.containsKey("end"));
	State<Event> endState = stateMap.get("end");
	final Set<Tuple2<String, StateTransitionAction>> endTransitions = unfoldTransitions(endState);
	assertEquals(Sets.newHashSet(
		Tuple2.of(NFACompiler.ENDING_STATE_NAME, StateTransitionAction.TAKE)
	), endTransitions);

	assertTrue(stateMap.containsKey(NFACompiler.ENDING_STATE_NAME));
	State<Event> endingState = stateMap.get(NFACompiler.ENDING_STATE_NAME);
	assertTrue(endingState.isFinal());
	assertEquals(0, endingState.getStateTransitions().size());
}