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

The following examples show how to use org.apache.flink.cep.pattern.Quantifier. 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 6 votes vote down vote up
/**
 * Retrieves list of conditions resulting in Stop state and names of the corresponding NOT patterns.
 *
 * <p>A current not condition can be produced in two cases:
 * <ol>
 *     <li>the previous pattern is a {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 *     <li>exists a backward path of {@link Quantifier.QuantifierProperty#OPTIONAL} patterns to
 *       {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 * </ol>
 *
 * <p><b>WARNING:</b> for more info on the second case see: {@link NFAFactoryCompiler#copyWithoutTransitiveNots(State)}
 *
 * @return list of not conditions with corresponding names
 */
private List<Tuple2<IterativeCondition<T>, String>> getCurrentNotCondition() {
	List<Tuple2<IterativeCondition<T>, String>> notConditions = new ArrayList<>();

	Pattern<T, ? extends T> previousPattern = currentPattern;
	while (previousPattern.getPrevious() != null && (
		previousPattern.getPrevious().getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL) ||
		previousPattern.getPrevious().getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW)) {

		previousPattern = previousPattern.getPrevious();

		if (previousPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
			final IterativeCondition<T> notCondition = getTakeCondition(previousPattern);
			notConditions.add(Tuple2.of(notCondition, previousPattern.getName()));
		}
	}
	return notConditions;
}
 
Example #2
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 #3
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 #4
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipTillAnyZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_ANY);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #5
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves list of conditions resulting in Stop state and names of the corresponding NOT patterns.
 *
 * <p>A current not condition can be produced in two cases:
 * <ol>
 *     <li>the previous pattern is a {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 *     <li>exists a backward path of {@link Quantifier.QuantifierProperty#OPTIONAL} patterns to
 *       {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 * </ol>
 *
 * <p><b>WARNING:</b> for more info on the second case see: {@link NFAFactoryCompiler#copyWithoutTransitiveNots(State)}
 *
 * @return list of not conditions with corresponding names
 */
private List<Tuple2<IterativeCondition<T>, String>> getCurrentNotCondition() {
	List<Tuple2<IterativeCondition<T>, String>> notConditions = new ArrayList<>();

	Pattern<T, ? extends T> previousPattern = currentPattern;
	while (previousPattern.getPrevious() != null && (
		previousPattern.getPrevious().getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL) ||
		previousPattern.getPrevious().getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW)) {

		previousPattern = previousPattern.getPrevious();

		if (previousPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
			final IterativeCondition<T> notCondition = getTakeCondition(previousPattern);
			notConditions.add(Tuple2.of(notCondition, previousPattern.getName()));
		}
	}
	return notConditions;
}
 
Example #6
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves list of conditions resulting in Stop state and names of the corresponding NOT patterns.
 *
 * <p>A current not condition can be produced in two cases:
 * <ol>
 *     <li>the previous pattern is a {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 *     <li>exists a backward path of {@link Quantifier.QuantifierProperty#OPTIONAL} patterns to
 *       {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li>
 * </ol>
 *
 * <p><b>WARNING:</b> for more info on the second case see: {@link NFAFactoryCompiler#copyWithoutTransitiveNots(State)}
 *
 * @return list of not conditions with corresponding names
 */
private List<Tuple2<IterativeCondition<T>, String>> getCurrentNotCondition() {
	List<Tuple2<IterativeCondition<T>, String>> notConditions = new ArrayList<>();

	Pattern<T, ? extends T> previousPattern = currentPattern;
	while (previousPattern.getPrevious() != null && (
		previousPattern.getPrevious().getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL) ||
		previousPattern.getPrevious().getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW)) {

		previousPattern = previousPattern.getPrevious();

		if (previousPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
			final IterativeCondition<T> notCondition = getTakeCondition(previousPattern);
			notConditions.add(Tuple2.of(notCondition, previousPattern.getName()));
		}
	}
	return notConditions;
}
 
Example #7
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 #8
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 #9
Source File: NFAITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipTillAnyZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_ANY);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #10
Source File: NFAITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipTillAnyZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_ANY);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #11
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 #12
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 #13
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictEagerZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.STRICT);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #14
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillAnyOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_ANY);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #15
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillNextOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_NEXT);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #16
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillNextZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_NEXT);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #17
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.STRICT);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #18
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillNextZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_NEXT);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #19
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * @return The {@link IterativeCondition condition} for the {@code IGNORE} edge
 * that corresponds to the specified {@link Pattern} and extended with
 * stop(until) condition if necessary. For more on strategy see {@link Quantifier}
 */
@SuppressWarnings("unchecked")
private IterativeCondition<T> getIgnoreCondition(Pattern<T, ?> pattern) {
	Quantifier.ConsumingStrategy consumingStrategy = pattern.getQuantifier().getConsumingStrategy();
	if (headOfGroup(pattern)) {
		// for the head pattern of a group pattern, we should consider the inner consume strategy
		// of the group pattern if the group pattern is not the head of the TIMES/LOOPING quantifier;
		// otherwise, we should consider the consume strategy of the group pattern
		if (isCurrentGroupPatternFirstOfLoop()) {
			consumingStrategy = currentGroupPattern.getQuantifier().getConsumingStrategy();
		} else {
			consumingStrategy = currentGroupPattern.getQuantifier().getInnerConsumingStrategy();
		}
	}

	IterativeCondition<T> ignoreCondition = null;
	switch (consumingStrategy) {
		case STRICT:
			ignoreCondition = null;
			break;
		case SKIP_TILL_NEXT:
			ignoreCondition = new RichNotCondition<>((IterativeCondition<T>) pattern.getCondition());
			break;
		case SKIP_TILL_ANY:
			ignoreCondition = BooleanConditions.trueFunction();
			break;
	}

	if (currentGroupPattern != null && currentGroupPattern.getUntilCondition() != null) {
		ignoreCondition = extendWithUntilCondition(
			ignoreCondition,
			(IterativeCondition<T>) currentGroupPattern.getUntilCondition(),
			false);
	}
	return ignoreCondition;
}
 
Example #20
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Creates all the states between Start and Final state.
 *
 * @param sinkState the state that last state should point to (always the Final state)
 * @return the next state after Start in the resulting graph
 */
private State<T> createMiddleStates(final State<T> sinkState) {
	State<T> lastSink = sinkState;
	while (currentPattern.getPrevious() != null) {

		if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
			//skip notFollow patterns, they are converted into edge conditions
		} else if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_NEXT) {
			final State<T> notNext = createState(currentPattern.getName(), State.StateType.Normal);
			final IterativeCondition<T> notCondition = getTakeCondition(currentPattern);
			final State<T> stopState = createStopState(notCondition, currentPattern.getName());

			if (lastSink.isFinal()) {
				//so that the proceed to final is not fired
				notNext.addIgnore(lastSink, new RichNotCondition<>(notCondition));
			} else {
				notNext.addProceed(lastSink, new RichNotCondition<>(notCondition));
			}
			notNext.addProceed(stopState, notCondition);
			lastSink = notNext;
		} else {
			lastSink = convertPattern(lastSink);
		}

		// we traverse the pattern graph backwards
		followingPattern = currentPattern;
		currentPattern = currentPattern.getPrevious();

		final Time currentWindowTime = currentPattern.getWindowTime();
		if (currentWindowTime != null && currentWindowTime.toMilliseconds() < windowTime.orElse(Long.MAX_VALUE)) {
			// the window time is the global minimum of all window times of each state
			windowTime = Optional.of(currentWindowTime.toMilliseconds());
		}
	}
	return lastSink;
}
 
Example #21
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 #22
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the given pattern is optional. If the given pattern is the head of a group pattern,
 * the optional status depends on the group pattern.
 */
private boolean isPatternOptional(Pattern<T, ?> pattern) {
	if (headOfGroup(pattern)) {
		return isCurrentGroupPatternFirstOfLoop() &&
			currentGroupPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
	} else {
		return pattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
	}
}
 
Example #23
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * @return The {@link IterativeCondition condition} for the {@code IGNORE} edge
 * that corresponds to the specified {@link Pattern} and extended with stop(until) condition
 * if necessary. It is applicable only for inner states of a complex state like looping or times.
 */
@SuppressWarnings("unchecked")
private IterativeCondition<T> getInnerIgnoreCondition(Pattern<T, ?> pattern) {
	Quantifier.ConsumingStrategy consumingStrategy = pattern.getQuantifier().getInnerConsumingStrategy();
	if (headOfGroup(pattern)) {
		// for the head pattern of a group pattern, we should consider the
		// inner consume strategy of the group pattern
		consumingStrategy = currentGroupPattern.getQuantifier().getInnerConsumingStrategy();
	}

	IterativeCondition<T> innerIgnoreCondition = null;
	switch (consumingStrategy) {
		case STRICT:
			innerIgnoreCondition = null;
			break;
		case SKIP_TILL_NEXT:
			innerIgnoreCondition = new RichNotCondition<>((IterativeCondition<T>) pattern.getCondition());
			break;
		case SKIP_TILL_ANY:
			innerIgnoreCondition = BooleanConditions.trueFunction();
			break;
	}

	if (currentGroupPattern != null && currentGroupPattern.getUntilCondition() != null) {
		innerIgnoreCondition = extendWithUntilCondition(
			innerIgnoreCondition,
			(IterativeCondition<T>) currentGroupPattern.getUntilCondition(),
			false);
	}
	return innerIgnoreCondition;
}
 
Example #24
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * @return The {@link IterativeCondition condition} for the {@code IGNORE} edge
 * that corresponds to the specified {@link Pattern} and extended with
 * stop(until) condition if necessary. For more on strategy see {@link Quantifier}
 */
@SuppressWarnings("unchecked")
private IterativeCondition<T> getIgnoreCondition(Pattern<T, ?> pattern) {
	Quantifier.ConsumingStrategy consumingStrategy = pattern.getQuantifier().getConsumingStrategy();
	if (headOfGroup(pattern)) {
		// for the head pattern of a group pattern, we should consider the inner consume strategy
		// of the group pattern if the group pattern is not the head of the TIMES/LOOPING quantifier;
		// otherwise, we should consider the consume strategy of the group pattern
		if (isCurrentGroupPatternFirstOfLoop()) {
			consumingStrategy = currentGroupPattern.getQuantifier().getConsumingStrategy();
		} else {
			consumingStrategy = currentGroupPattern.getQuantifier().getInnerConsumingStrategy();
		}
	}

	IterativeCondition<T> ignoreCondition = null;
	switch (consumingStrategy) {
		case STRICT:
			ignoreCondition = null;
			break;
		case SKIP_TILL_NEXT:
			ignoreCondition = new RichNotCondition<>((IterativeCondition<T>) pattern.getCondition());
			break;
		case SKIP_TILL_ANY:
			ignoreCondition = BooleanConditions.trueFunction();
			break;
	}

	if (currentGroupPattern != null && currentGroupPattern.getUntilCondition() != null) {
		ignoreCondition = extendWithUntilCondition(
			ignoreCondition,
			(IterativeCondition<T>) currentGroupPattern.getUntilCondition(),
			false);
	}
	return ignoreCondition;
}
 
Example #25
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.STRICT);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #26
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillNextOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_NEXT);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #27
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipTillAnyOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.SKIP_TILL_ANY);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent4, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #28
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictEagerZeroOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testZeroOrMore(Quantifier.ConsumingStrategy.STRICT);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.end)
	));
}
 
Example #29
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictOneOrMore() throws Exception {
	List<List<Event>> resultingPatterns = testOneOrMore(Quantifier.ConsumingStrategy.STRICT);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.middleEvent2, ConsecutiveData.end),
		Lists.newArrayList(ConsecutiveData.startEvent, ConsecutiveData.middleEvent1, ConsecutiveData.end)
	));
}
 
Example #30
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates all the states between Start and Final state.
 *
 * @param sinkState the state that last state should point to (always the Final state)
 * @return the next state after Start in the resulting graph
 */
private State<T> createMiddleStates(final State<T> sinkState) {
	State<T> lastSink = sinkState;
	while (currentPattern.getPrevious() != null) {

		if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) {
			//skip notFollow patterns, they are converted into edge conditions
		} else if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_NEXT) {
			final State<T> notNext = createState(currentPattern.getName(), State.StateType.Normal);
			final IterativeCondition<T> notCondition = getTakeCondition(currentPattern);
			final State<T> stopState = createStopState(notCondition, currentPattern.getName());

			if (lastSink.isFinal()) {
				//so that the proceed to final is not fired
				notNext.addIgnore(lastSink, new RichNotCondition<>(notCondition));
			} else {
				notNext.addProceed(lastSink, new RichNotCondition<>(notCondition));
			}
			notNext.addProceed(stopState, notCondition);
			lastSink = notNext;
		} else {
			lastSink = convertPattern(lastSink);
		}

		// we traverse the pattern graph backwards
		followingPattern = currentPattern;
		currentPattern = currentPattern.getPrevious();

		final Time currentWindowTime = currentPattern.getWindowTime();
		if (currentWindowTime != null && currentWindowTime.toMilliseconds() < windowTime) {
			// the window time is the global minimum of all window times of each state
			windowTime = currentWindowTime.toMilliseconds();
		}
	}
	return lastSink;
}