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

The following examples show how to use org.apache.flink.cep.nfa.State. 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 with Apache License 2.0 6 votes vote down vote up
private State<T> convertPattern(final State<T> sinkState) {
	final State<T> lastSink;

	final Quantifier quantifier = currentPattern.getQuantifier();
	if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) {

		// if loop has started then all notPatterns previous to the optional states are no longer valid
		setCurrentGroupPatternFirstOfLoop(false);
		final State<T> sink = copyWithoutTransitiveNots(sinkState);
		final State<T> looping = createLooping(sink);

		setCurrentGroupPatternFirstOfLoop(true);
		lastSink = createTimesState(looping, sinkState, currentPattern.getTimes());
	} else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) {
		lastSink = createTimesState(sinkState, sinkState, currentPattern.getTimes());
	} else {
		lastSink = createSingletonState(sinkState);
	}
	addStopStates(lastSink);

	return lastSink;
}
 
Example #2
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the states for the group pattern as a looping one.
 *
 * @param groupPattern the group pattern to create the states for
 * @param sinkState the state that the group pattern being converted should point to
 * @return the first state of the states of the group pattern
 */
private State<T> createLoopingGroupPatternState(
	final GroupPattern<T, ?> groupPattern,
	final State<T> sinkState) {
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	Pattern<T, ?> oldCurrentPattern = currentPattern;
	Pattern<T, ?> oldFollowingPattern = followingPattern;
	GroupPattern<T, ?> oldGroupPattern = currentGroupPattern;

	final State<T> dummyState = createState(currentPattern.getName(), State.StateType.Normal);
	State<T> lastSink = dummyState;
	currentGroupPattern = groupPattern;
	currentPattern = groupPattern.getRawPattern();
	lastSink = createMiddleStates(lastSink);
	lastSink = convertPattern(lastSink);
	lastSink.addProceed(sinkState, proceedCondition);
	dummyState.addProceed(lastSink, proceedCondition);
	currentPattern = oldCurrentPattern;
	followingPattern = oldFollowingPattern;
	currentGroupPattern = oldGroupPattern;
	return lastSink;
}
 
Example #3
Source File: NFACompilerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoUnnecessaryStateCopiesCreated() {
	final Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.notFollowedBy("not").where(startFilter)
		.followedBy("oneOrMore").where(startFilter).oneOrMore()
		.followedBy("end").where(endFilter);

	final NFACompiler.NFAFactoryCompiler<Event> nfaFactoryCompiler = new NFACompiler.NFAFactoryCompiler<>(pattern);
	nfaFactoryCompiler.compileFactory();

	int endStateCount = 0;
	for (State<Event> state : nfaFactoryCompiler.getStates()) {
		if (state.getName().equals("end")) {
			endStateCount++;
		}
	}

	assertEquals(1, endStateCount);
}
 
Example #4
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
		 * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create
		 * multiple NFAs.
		 */
//		修改default->public
		public void compileFactory() {
			if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
				throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!");
			}

			checkPatternNameUniqueness();

			checkPatternSkipStrategy();

			// we're traversing the pattern from the end to the beginning --> the first state is the final state
			State<T> sinkState = createEndingState();
			// add all the normal states
			sinkState = createMiddleStates(sinkState);
			// add the beginning state
			createStartState(sinkState);
		}
 
Example #5
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private State<T> convertPattern(final State<T> sinkState) {
	final State<T> lastSink;

	final Quantifier quantifier = currentPattern.getQuantifier();
	if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) {

		// if loop has started then all notPatterns previous to the optional states are no longer valid
		setCurrentGroupPatternFirstOfLoop(false);
		final State<T> sink = copyWithoutTransitiveNots(sinkState);
		final State<T> looping = createLooping(sink);

		setCurrentGroupPatternFirstOfLoop(true);
		lastSink = createTimesState(looping, sinkState, currentPattern.getTimes());
	} else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) {
		lastSink = createTimesState(sinkState, sinkState, currentPattern.getTimes());
	} else {
		lastSink = createSingletonState(sinkState);
	}
	addStopStates(lastSink);

	return lastSink;
}
 
Example #6
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
private State<T> convertPattern(final State<T> sinkState) {
	final State<T> lastSink;

	final Quantifier quantifier = currentPattern.getQuantifier();
	if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) {

		// if loop has started then all notPatterns previous to the optional states are no longer valid
		setCurrentGroupPatternFirstOfLoop(false);
		final State<T> sink = copyWithoutTransitiveNots(sinkState);
		final State<T> looping = createLooping(sink);

		setCurrentGroupPatternFirstOfLoop(true);
		lastSink = createTimesState(looping, sinkState, currentPattern.getTimes());
	} else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) {
		lastSink = createTimesState(sinkState, sinkState, currentPattern.getTimes());
	} else {
		lastSink = createSingletonState(sinkState);
	}
	addStopStates(lastSink);

	return lastSink;
}
 
Example #7
Source File: NFACompilerTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoUnnecessaryStateCopiesCreated() {
	final Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.notFollowedBy("not").where(startFilter)
		.followedBy("oneOrMore").where(startFilter).oneOrMore()
		.followedBy("end").where(endFilter);

	final NFACompiler.NFAFactoryCompiler<Event> nfaFactoryCompiler = new NFACompiler.NFAFactoryCompiler<>(pattern);
	nfaFactoryCompiler.compileFactory();

	int endStateCount = 0;
	for (State<Event> state : nfaFactoryCompiler.getStates()) {
		if (state.getName().equals("end")) {
			endStateCount++;
		}
	}

	assertEquals(1, endStateCount);
}
 
Example #8
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create
 * multiple NFAs.
 */
void compileFactory() {
	if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
		throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!");
	}

	checkPatternNameUniqueness();

	checkPatternSkipStrategy();

	// we're traversing the pattern from the end to the beginning --> the first state is the final state
	State<T> sinkState = createEndingState();
	// add all the normal states
	sinkState = createMiddleStates(sinkState);
	// add the beginning state
	createStartState(sinkState);
}
 
Example #9
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create
 * multiple NFAs.
 */
void compileFactory() {
	if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
		throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!");
	}

	checkPatternNameUniqueness();

	checkPatternSkipStrategy();

	// we're traversing the pattern from the end to the beginning --> the first state is the final state
	State<T> sinkState = createEndingState();
	// add all the normal states
	sinkState = createMiddleStates(sinkState);
	// add the beginning state
	createStartState(sinkState);
}
 
Example #10
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Create the states for the group pattern as a looping one.
 *
 * @param groupPattern the group pattern to create the states for
 * @param sinkState the state that the group pattern being converted should point to
 * @return the first state of the states of the group pattern
 */
private State<T> createLoopingGroupPatternState(
	final GroupPattern<T, ?> groupPattern,
	final State<T> sinkState) {
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	Pattern<T, ?> oldCurrentPattern = currentPattern;
	Pattern<T, ?> oldFollowingPattern = followingPattern;
	GroupPattern<T, ?> oldGroupPattern = currentGroupPattern;

	final State<T> dummyState = createState(currentPattern.getName(), State.StateType.Normal);
	State<T> lastSink = dummyState;
	currentGroupPattern = groupPattern;
	currentPattern = groupPattern.getRawPattern();
	lastSink = createMiddleStates(lastSink);
	lastSink = convertPattern(lastSink);
	lastSink.addProceed(sinkState, proceedCondition);
	dummyState.addProceed(lastSink, proceedCondition);
	currentPattern = oldCurrentPattern;
	followingPattern = oldFollowingPattern;
	currentGroupPattern = oldGroupPattern;
	return lastSink;
}
 
Example #11
Source File: NFACompilerTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoUnnecessaryStateCopiesCreated() {
	final Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter)
		.notFollowedBy("not").where(startFilter)
		.followedBy("oneOrMore").where(startFilter).oneOrMore()
		.followedBy("end").where(endFilter);

	final NFACompiler.NFAFactoryCompiler<Event> nfaFactoryCompiler = new NFACompiler.NFAFactoryCompiler<>(pattern);
	nfaFactoryCompiler.compileFactory();

	int endStateCount = 0;
	for (State<Event> state : nfaFactoryCompiler.getStates()) {
		if (state.getName().equals("end")) {
			endStateCount++;
		}
	}

	assertEquals(1, endStateCount);
}
 
Example #12
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Create the states for the group pattern as a looping one.
 *
 * @param groupPattern the group pattern to create the states for
 * @param sinkState the state that the group pattern being converted should point to
 * @return the first state of the states of the group pattern
 */
private State<T> createLoopingGroupPatternState(
	final GroupPattern<T, ?> groupPattern,
	final State<T> sinkState) {
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	Pattern<T, ?> oldCurrentPattern = currentPattern;
	Pattern<T, ?> oldFollowingPattern = followingPattern;
	GroupPattern<T, ?> oldGroupPattern = currentGroupPattern;

	final State<T> dummyState = createState(currentPattern.getName(), State.StateType.Normal);
	State<T> lastSink = dummyState;
	currentGroupPattern = groupPattern;
	currentPattern = groupPattern.getRawPattern();
	lastSink = createMiddleStates(lastSink);
	lastSink = convertPattern(lastSink);
	lastSink.addProceed(sinkState, proceedCondition);
	dummyState.addProceed(lastSink, proceedCondition);
	currentPattern = oldCurrentPattern;
	followingPattern = oldFollowingPattern;
	currentGroupPattern = oldGroupPattern;
	return lastSink;
}
 
Example #13
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFAFactoryImpl(
		long windowTime,
		Collection<State<T>> states,
		boolean timeoutHandling) {

	this.windowTime = windowTime;
	this.states = states;
	this.timeoutHandling = timeoutHandling;
}
 
Example #14
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create all the states for the group pattern.
 *
 * @param groupPattern the group pattern to create the states for
 * @param sinkState the state that the group pattern being converted should point to
 * @param proceedState the state that the group pattern being converted should proceed to
 * @param isOptional whether the group pattern being converted is optional
 * @return the first state of the states of the group pattern
 */
private State<T> createGroupPatternState(
	final GroupPattern<T, ?> groupPattern,
	final State<T> sinkState,
	final State<T> proceedState,
	final boolean isOptional) {
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	Pattern<T, ?> oldCurrentPattern = currentPattern;
	Pattern<T, ?> oldFollowingPattern = followingPattern;
	GroupPattern<T, ?> oldGroupPattern = currentGroupPattern;

	State<T> lastSink = sinkState;
	currentGroupPattern = groupPattern;
	currentPattern = groupPattern.getRawPattern();
	lastSink = createMiddleStates(lastSink);
	lastSink = convertPattern(lastSink);
	if (isOptional) {
		// for the first state of a group pattern, its PROCEED edge should point to
		// the following state of that group pattern
		lastSink.addProceed(proceedState, proceedCondition);
	}
	currentPattern = oldCurrentPattern;
	followingPattern = oldFollowingPattern;
	currentGroupPattern = oldGroupPattern;
	return lastSink;
}
 
Example #15
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 #16
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a state with {@link State.StateType#Normal} and adds it to the collection of created states.
 * Should be used instead of instantiating with new operator.
 *
 * @return the created state
 */
private State<T> createState(String name, State.StateType stateType) {
	String stateName = stateNameHandler.getUniqueInternalName(name);
	State<T> state = new State<>(stateName, stateType);
	states.add(state);
	return state;
}
 
Example #17
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private State<T> createStopState(final IterativeCondition<T> notCondition, final String name) {
	// We should not duplicate the notStates. All states from which we can stop should point to the same one.
	State<T> stopState = stopStates.get(name);
	if (stopState == null) {
		stopState = createState(name, State.StateType.Stop);
		stopState.addTake(notCondition);
		stopStates.put(name, stopState);
	}
	return stopState;
}
 
Example #18
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private State<T> copy(final State<T> state) {
	final State<T> copyOfState = createState(
		NFAStateNameHandler.getOriginalNameFromInternal(state.getName()),
		state.getStateType());
	for (StateTransition<T> tStateTransition : state.getStateTransitions()) {
		copyOfState.addStateTransition(
			tStateTransition.getAction(),
			tStateTransition.getTargetState().equals(tStateTransition.getSourceState())
					? copyOfState
					: tStateTransition.getTargetState(),
			tStateTransition.getCondition());
	}
	return copyOfState;
}
 
Example #19
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void addStopStateToLooping(final State<T> loopingState) {
	if (followingPattern != null &&
			followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
		final IterativeCondition<T> notCondition = getTakeCondition(followingPattern);
		final State<T> stopState = createStopState(notCondition, followingPattern.getName());
		loopingState.addProceed(stopState, notCondition);
	}
}
 
Example #20
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a simple single state. For an OPTIONAL state it also consists
 * of a similar state without the PROCEED edge, so that for each PROCEED transition branches
 * in computation state graph  can be created only once.
 *
 * @param sinkState state that the state being converted should point to
 * @return the created state
 */
@SuppressWarnings("unchecked")
private State<T> createSingletonState(final State<T> sinkState) {
	return createSingletonState(
		sinkState,
		sinkState,
		getTakeCondition(currentPattern),
		getIgnoreCondition(currentPattern),
		isPatternOptional(currentPattern));
}
 
Example #21
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the Start {@link State} of the resulting NFA graph.
 *
 * @param sinkState the state that Start state should point to (always first state of middle states)
 * @return created state
 */
@SuppressWarnings("unchecked")
private State<T> createStartState(State<T> sinkState) {
	final State<T> beginningState = convertPattern(sinkState);
	beginningState.makeStart();
	return beginningState;
}
 
Example #22
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFAFactoryImpl(
		long windowTime,
		Collection<State<T>> states,
		boolean timeoutHandling) {

	this.windowTime = windowTime;
	this.states = states;
	this.timeoutHandling = timeoutHandling;
}
 
Example #23
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private State<T> copy(final State<T> state) {
	final State<T> copyOfState = createState(
		NFAStateNameHandler.getOriginalNameFromInternal(state.getName()),
		state.getStateType());
	for (StateTransition<T> tStateTransition : state.getStateTransitions()) {
		copyOfState.addStateTransition(
			tStateTransition.getAction(),
			tStateTransition.getTargetState().equals(tStateTransition.getSourceState())
					? copyOfState
					: tStateTransition.getTargetState(),
			tStateTransition.getCondition());
	}
	return copyOfState;
}
 
Example #24
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void updateWithGreedyCondition(
	State<T> state,
	IterativeCondition<T> takeCondition) {
	for (StateTransition<T> stateTransition : state.getStateTransitions()) {
		stateTransition.setCondition(
			new RichAndCondition<>(stateTransition.getCondition(), new RichNotCondition<>(takeCondition)));
	}
}
 
Example #25
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 #26
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Create all the states for the group pattern.
 *
 * @param groupPattern the group pattern to create the states for
 * @param sinkState the state that the group pattern being converted should point to
 * @param proceedState the state that the group pattern being converted should proceed to
 * @param isOptional whether the group pattern being converted is optional
 * @return the first state of the states of the group pattern
 */
private State<T> createGroupPatternState(
	final GroupPattern<T, ?> groupPattern,
	final State<T> sinkState,
	final State<T> proceedState,
	final boolean isOptional) {
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	Pattern<T, ?> oldCurrentPattern = currentPattern;
	Pattern<T, ?> oldFollowingPattern = followingPattern;
	GroupPattern<T, ?> oldGroupPattern = currentGroupPattern;

	State<T> lastSink = sinkState;
	currentGroupPattern = groupPattern;
	currentPattern = groupPattern.getRawPattern();
	lastSink = createMiddleStates(lastSink);
	lastSink = convertPattern(lastSink);
	if (isOptional) {
		// for the first state of a group pattern, its PROCEED edge should point to
		// the following state of that group pattern
		lastSink.addProceed(proceedState, proceedCondition);
	}
	currentPattern = oldCurrentPattern;
	followingPattern = oldFollowingPattern;
	currentGroupPattern = oldGroupPattern;
	return lastSink;
}
 
Example #27
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 #28
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private State<T> createStopState(final IterativeCondition<T> notCondition, final String name) {
	// We should not duplicate the notStates. All states from which we can stop should point to the same one.
	State<T> stopState = stopStates.get(name);
	if (stopState == null) {
		stopState = createState(name, State.StateType.Stop);
		stopState.addTake(notCondition);
		stopStates.put(name, stopState);
	}
	return stopState;
}
 
Example #29
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a simple single state. For an OPTIONAL state it also consists
 * of a similar state without the PROCEED edge, so that for each PROCEED transition branches
 * in computation state graph  can be created only once.
 *
 * @param sinkState state that the state being converted should point to
 * @return the created state
 */
@SuppressWarnings("unchecked")
private State<T> createSingletonState(final State<T> sinkState) {
	return createSingletonState(
		sinkState,
		sinkState,
		getTakeCondition(currentPattern),
		getIgnoreCondition(currentPattern),
		isPatternOptional(currentPattern));
}
 
Example #30
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
private void addStopStateToLooping(final State<T> loopingState) {
	if (followingPattern != null &&
			followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
		final IterativeCondition<T> notCondition = getTakeCondition(followingPattern);
		final State<T> stopState = createStopState(notCondition, followingPattern.getName());
		loopingState.addProceed(stopState, notCondition);
	}
}