org.apache.flink.cep.pattern.GroupPattern Java Examples

The following examples show how to use org.apache.flink.cep.pattern.GroupPattern. 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
/**
 * 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 #2
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 #3
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 #4
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the current group pattern as the head of the TIMES quantifier or not.
 *
 * @param isFirstOfLoop whether the current group pattern is the head of the TIMES quantifier
 */
@SuppressWarnings("unchecked")
private void setCurrentGroupPatternFirstOfLoop(boolean isFirstOfLoop) {
	if (currentPattern instanceof GroupPattern) {
		firstOfLoopMap.put((GroupPattern<T, ?>) currentPattern, isFirstOfLoop);
	}
}
 
Example #5
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 #6
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the current group pattern as the head of the TIMES quantifier or not.
 *
 * @param isFirstOfLoop whether the current group pattern is the head of the TIMES quantifier
 */
@SuppressWarnings("unchecked")
private void setCurrentGroupPatternFirstOfLoop(boolean isFirstOfLoop) {
	if (currentPattern instanceof GroupPattern) {
		firstOfLoopMap.put((GroupPattern<T, ?>) currentPattern, isFirstOfLoop);
	}
}
 
Example #7
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the given pattern's name is already used or not. If yes, it
 * throws a {@link MalformedPatternException}.
 *
 * @param pattern The pattern to be checked
 */
private void checkPatternNameUniqueness(final Pattern pattern) {
	if (pattern instanceof GroupPattern) {
		Pattern patternToCheck = ((GroupPattern) pattern).getRawPattern();
		while (patternToCheck != null) {
			checkPatternNameUniqueness(patternToCheck);
			patternToCheck = patternToCheck.getPrevious();
		}
	} else {
		stateNameHandler.checkNameUniqueness(pattern.getName());
	}
}
 
Example #8
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 #9
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the current group pattern as the head of the TIMES quantifier or not.
 *
 * @param isFirstOfLoop whether the current group pattern is the head of the TIMES quantifier
 */
@SuppressWarnings("unchecked")
private void setCurrentGroupPatternFirstOfLoop(boolean isFirstOfLoop) {
	if (currentPattern instanceof GroupPattern) {
		firstOfLoopMap.put((GroupPattern<T, ?>) currentPattern, isFirstOfLoop);
	}
}
 
Example #10
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the given pattern's name is already used or not. If yes, it
 * throws a {@link MalformedPatternException}.
 *
 * @param pattern The pattern to be checked
 */
private void checkPatternNameUniqueness(final Pattern pattern) {
	if (pattern instanceof GroupPattern) {
		Pattern patternToCheck = ((GroupPattern) pattern).getRawPattern();
		while (patternToCheck != null) {
			checkPatternNameUniqueness(patternToCheck);
			patternToCheck = patternToCheck.getPrevious();
		}
	} else {
		stateNameHandler.checkNameUniqueness(pattern.getName());
	}
}
 
Example #11
Source File: NFACompiler.java    From Flink-CEPplus 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 #12
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the given pattern's name is already used or not. If yes, it
 * throws a {@link MalformedPatternException}.
 *
 * @param pattern The pattern to be checked
 */
private void checkPatternNameUniqueness(final Pattern pattern) {
	if (pattern instanceof GroupPattern) {
		Pattern patternToCheck = ((GroupPattern) pattern).getRawPattern();
		while (patternToCheck != null) {
			checkPatternNameUniqueness(patternToCheck);
			patternToCheck = patternToCheck.getPrevious();
		}
	} else {
		stateNameHandler.checkNameUniqueness(pattern.getName());
	}
}
 
Example #13
Source File: NFACompiler.java    From flink with Apache License 2.0 4 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 ignoreCondition condition that should be applied to IGNORE transition
 * @param sinkState state that the state being converted should point to
 * @param proceedState state that the state being converted should proceed to
 * @param isOptional whether the state being converted is optional
 * @return the created state
 */
@SuppressWarnings("unchecked")
private State<T> createSingletonState(final State<T> sinkState,
	final State<T> proceedState,
	final IterativeCondition<T> takeCondition,
	final IterativeCondition<T> ignoreCondition,
	final boolean isOptional) {
	if (currentPattern instanceof GroupPattern) {
		return createGroupPatternState((GroupPattern) currentPattern, sinkState, proceedState, isOptional);
	}

	final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal);
	// if event is accepted then all notPatterns previous to the optional states are no longer valid
	final State<T> sink = copyWithoutTransitiveNots(sinkState);
	singletonState.addTake(sink, takeCondition);

	// if no element accepted the previous nots are still valid.
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	// for the first state of a group pattern, its PROCEED edge should point to the following state of
	// that group pattern and the edge will be added at the end of creating the NFA for that group pattern
	if (isOptional && !headOfGroup(currentPattern)) {
		if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
			final IterativeCondition<T> untilCondition =
				(IterativeCondition<T>) currentPattern.getUntilCondition();
			if (untilCondition != null) {
				singletonState.addProceed(
					originalStateMap.get(proceedState.getName()),
					new RichAndCondition<>(proceedCondition, untilCondition));
			}
			singletonState.addProceed(proceedState,
				untilCondition != null
					? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
					: proceedCondition);
		} else {
			singletonState.addProceed(proceedState, proceedCondition);
		}
	}

	if (ignoreCondition != null) {
		final State<T> ignoreState;
		if (isOptional) {
			ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
			ignoreState.addTake(sink, takeCondition);
			ignoreState.addIgnore(ignoreCondition);
			addStopStates(ignoreState);
		} else {
			ignoreState = singletonState;
		}
		singletonState.addIgnore(ignoreState, ignoreCondition);
	}
	return singletonState;
}
 
Example #14
Source File: NFACompiler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the given state as a looping one. Looping state is one with TAKE edge to itself and
 * PROCEED edge to the sinkState. 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 the state that the converted state should point to
 * @return the first state of the created complex state
 */
@SuppressWarnings("unchecked")
private State<T> createLooping(final State<T> sinkState) {
	if (currentPattern instanceof GroupPattern) {
		return createLoopingGroupPatternState((GroupPattern) currentPattern, sinkState);
	}
	final IterativeCondition<T> untilCondition = (IterativeCondition<T>) currentPattern.getUntilCondition();

	final IterativeCondition<T> ignoreCondition = extendWithUntilCondition(
		getInnerIgnoreCondition(currentPattern),
		untilCondition,
		false);
	final IterativeCondition<T> takeCondition = extendWithUntilCondition(
		getTakeCondition(currentPattern),
		untilCondition,
		true);

	IterativeCondition<T> proceedCondition = getTrueFunction();
	final State<T> loopingState = createState(currentPattern.getName(), State.StateType.Normal);

	if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
		if (untilCondition != null) {
			State<T> sinkStateCopy = copy(sinkState);
			loopingState.addProceed(sinkStateCopy, new RichAndCondition<>(proceedCondition, untilCondition));
			originalStateMap.put(sinkState.getName(), sinkStateCopy);
		}
		loopingState.addProceed(sinkState,
			untilCondition != null
				? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
				: proceedCondition);
		updateWithGreedyCondition(sinkState, getTakeCondition(currentPattern));
	} else {
		loopingState.addProceed(sinkState, proceedCondition);
	}
	loopingState.addTake(takeCondition);

	addStopStateToLooping(loopingState);

	if (ignoreCondition != null) {
		final State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
		ignoreState.addTake(loopingState, takeCondition);
		ignoreState.addIgnore(ignoreCondition);
		loopingState.addIgnore(ignoreState, ignoreCondition);

		addStopStateToLooping(ignoreState);
	}
	return loopingState;
}
 
Example #15
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the given state as a looping one. Looping state is one with TAKE edge to itself and
 * PROCEED edge to the sinkState. 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 the state that the converted state should point to
 * @return the first state of the created complex state
 */
@SuppressWarnings("unchecked")
private State<T> createLooping(final State<T> sinkState) {
	if (currentPattern instanceof GroupPattern) {
		return createLoopingGroupPatternState((GroupPattern) currentPattern, sinkState);
	}
	final IterativeCondition<T> untilCondition = (IterativeCondition<T>) currentPattern.getUntilCondition();

	final IterativeCondition<T> ignoreCondition = extendWithUntilCondition(
		getInnerIgnoreCondition(currentPattern),
		untilCondition,
		false);
	final IterativeCondition<T> takeCondition = extendWithUntilCondition(
		getTakeCondition(currentPattern),
		untilCondition,
		true);

	IterativeCondition<T> proceedCondition = getTrueFunction();
	final State<T> loopingState = createState(currentPattern.getName(), State.StateType.Normal);

	if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
		if (untilCondition != null) {
			State<T> sinkStateCopy = copy(sinkState);
			loopingState.addProceed(sinkStateCopy, new RichAndCondition<>(proceedCondition, untilCondition));
			originalStateMap.put(sinkState.getName(), sinkStateCopy);
		}
		loopingState.addProceed(sinkState,
			untilCondition != null
				? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
				: proceedCondition);
		updateWithGreedyCondition(sinkState, getTakeCondition(currentPattern));
	} else {
		loopingState.addProceed(sinkState, proceedCondition);
	}
	loopingState.addTake(takeCondition);

	addStopStateToLooping(loopingState);

	if (ignoreCondition != null) {
		final State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
		ignoreState.addTake(loopingState, takeCondition);
		ignoreState.addIgnore(ignoreCondition);
		loopingState.addIgnore(ignoreState, ignoreCondition);

		addStopStateToLooping(ignoreState);
	}
	return loopingState;
}
 
Example #16
Source File: NFACompiler.java    From flink with Apache License 2.0 4 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 ignoreCondition condition that should be applied to IGNORE transition
 * @param sinkState state that the state being converted should point to
 * @param proceedState state that the state being converted should proceed to
 * @param isOptional whether the state being converted is optional
 * @return the created state
 */
@SuppressWarnings("unchecked")
private State<T> createSingletonState(final State<T> sinkState,
	final State<T> proceedState,
	final IterativeCondition<T> takeCondition,
	final IterativeCondition<T> ignoreCondition,
	final boolean isOptional) {
	if (currentPattern instanceof GroupPattern) {
		return createGroupPatternState((GroupPattern) currentPattern, sinkState, proceedState, isOptional);
	}

	final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal);
	// if event is accepted then all notPatterns previous to the optional states are no longer valid
	final State<T> sink = copyWithoutTransitiveNots(sinkState);
	singletonState.addTake(sink, takeCondition);

	// if no element accepted the previous nots are still valid.
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	// for the first state of a group pattern, its PROCEED edge should point to the following state of
	// that group pattern and the edge will be added at the end of creating the NFA for that group pattern
	if (isOptional && !headOfGroup(currentPattern)) {
		if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
			final IterativeCondition<T> untilCondition =
				(IterativeCondition<T>) currentPattern.getUntilCondition();
			if (untilCondition != null) {
				singletonState.addProceed(
					originalStateMap.get(proceedState.getName()),
					new RichAndCondition<>(proceedCondition, untilCondition));
			}
			singletonState.addProceed(proceedState,
				untilCondition != null
					? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
					: proceedCondition);
		} else {
			singletonState.addProceed(proceedState, proceedCondition);
		}
	}

	if (ignoreCondition != null) {
		final State<T> ignoreState;
		if (isOptional) {
			ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
			ignoreState.addTake(sink, takeCondition);
			ignoreState.addIgnore(ignoreCondition);
			addStopStates(ignoreState);
		} else {
			ignoreState = singletonState;
		}
		singletonState.addIgnore(ignoreState, ignoreCondition);
	}
	return singletonState;
}
 
Example #17
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 4 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 ignoreCondition condition that should be applied to IGNORE transition
 * @param sinkState state that the state being converted should point to
 * @param proceedState state that the state being converted should proceed to
 * @param isOptional whether the state being converted is optional
 * @return the created state
 */
@SuppressWarnings("unchecked")
private State<T> createSingletonState(final State<T> sinkState,
	final State<T> proceedState,
	final IterativeCondition<T> takeCondition,
	final IterativeCondition<T> ignoreCondition,
	final boolean isOptional) {
	if (currentPattern instanceof GroupPattern) {
		return createGroupPatternState((GroupPattern) currentPattern, sinkState, proceedState, isOptional);
	}

	final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal);
	// if event is accepted then all notPatterns previous to the optional states are no longer valid
	final State<T> sink = copyWithoutTransitiveNots(sinkState);
	singletonState.addTake(sink, takeCondition);

	// if no element accepted the previous nots are still valid.
	final IterativeCondition<T> proceedCondition = getTrueFunction();

	// for the first state of a group pattern, its PROCEED edge should point to the following state of
	// that group pattern and the edge will be added at the end of creating the NFA for that group pattern
	if (isOptional && !headOfGroup(currentPattern)) {
		if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
			final IterativeCondition<T> untilCondition =
				(IterativeCondition<T>) currentPattern.getUntilCondition();
			if (untilCondition != null) {
				singletonState.addProceed(
					originalStateMap.get(proceedState.getName()),
					new RichAndCondition<>(proceedCondition, untilCondition));
			}
			singletonState.addProceed(proceedState,
				untilCondition != null
					? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
					: proceedCondition);
		} else {
			singletonState.addProceed(proceedState, proceedCondition);
		}
	}

	if (ignoreCondition != null) {
		final State<T> ignoreState;
		if (isOptional) {
			ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
			ignoreState.addTake(sink, takeCondition);
			ignoreState.addIgnore(ignoreCondition);
			addStopStates(ignoreState);
		} else {
			ignoreState = singletonState;
		}
		singletonState.addIgnore(ignoreState, ignoreCondition);
	}
	return singletonState;
}
 
Example #18
Source File: NFACompiler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Creates the given state as a looping one. Looping state is one with TAKE edge to itself and
 * PROCEED edge to the sinkState. 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 the state that the converted state should point to
 * @return the first state of the created complex state
 */
@SuppressWarnings("unchecked")
private State<T> createLooping(final State<T> sinkState) {
	if (currentPattern instanceof GroupPattern) {
		return createLoopingGroupPatternState((GroupPattern) currentPattern, sinkState);
	}
	final IterativeCondition<T> untilCondition = (IterativeCondition<T>) currentPattern.getUntilCondition();

	final IterativeCondition<T> ignoreCondition = extendWithUntilCondition(
		getInnerIgnoreCondition(currentPattern),
		untilCondition,
		false);
	final IterativeCondition<T> takeCondition = extendWithUntilCondition(
		getTakeCondition(currentPattern),
		untilCondition,
		true);

	IterativeCondition<T> proceedCondition = getTrueFunction();
	final State<T> loopingState = createState(currentPattern.getName(), State.StateType.Normal);

	if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
		if (untilCondition != null) {
			State<T> sinkStateCopy = copy(sinkState);
			loopingState.addProceed(sinkStateCopy, new RichAndCondition<>(proceedCondition, untilCondition));
			originalStateMap.put(sinkState.getName(), sinkStateCopy);
		}
		loopingState.addProceed(sinkState,
			untilCondition != null
				? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition))
				: proceedCondition);
		updateWithGreedyCondition(sinkState, getTakeCondition(currentPattern));
	} else {
		loopingState.addProceed(sinkState, proceedCondition);
	}
	loopingState.addTake(takeCondition);

	addStopStateToLooping(loopingState);

	if (ignoreCondition != null) {
		final State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
		ignoreState.addTake(loopingState, takeCondition);
		ignoreState.addIgnore(ignoreCondition);
		loopingState.addIgnore(ignoreState, ignoreCondition);

		addStopStateToLooping(ignoreState);
	}
	return loopingState;
}