org.apache.flink.cep.pattern.conditions.IterativeCondition Java Examples

The following examples show how to use org.apache.flink.cep.pattern.conditions.IterativeCondition. 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
/**
 * 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-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 #3
Source File: NFAIterativeConditionTimeContextTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventTimestamp() throws Exception {
	final Event event = event().withId(1).build();
	final long timestamp = 3;

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.timestamp() == timestamp;
		}
	});

	final NFATestHarness testHarness = forPattern(pattern).build();

	final List<List<Event>> resultingPattern = testHarness.feedRecord(new StreamRecord<>(event, timestamp));

	compareMaps(resultingPattern, Collections.singletonList(
		Collections.singletonList(event)
	));
}
 
Example #4
Source File: Pattern.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Applies a stop condition for a looping state. It allows cleaning the underlying state.
 *
 * @param untilCondition a condition an event has to satisfy to stop collecting events into looping state
 * @return The same pattern with applied untilCondition
 */
public Pattern<T, F> until(IterativeCondition<F> untilCondition) {
	Preconditions.checkNotNull(untilCondition, "The condition cannot be null");

	if (this.untilCondition != null) {
		throw new MalformedPatternException("Only one until condition can be applied.");
	}

	if (!quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) {
		throw new MalformedPatternException("The until condition is only applicable to looping states.");
	}

	ClosureCleaner.clean(untilCondition, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true);
	this.untilCondition = untilCondition;

	return this;
}
 
Example #5
Source File: Pattern.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Applies a stop condition for a looping state. It allows cleaning the underlying state.
 *
 * @param untilCondition a condition an event has to satisfy to stop collecting events into looping state
 * @return The same pattern with applied untilCondition
 */
public Pattern<T, F> until(IterativeCondition<F> untilCondition) {
	Preconditions.checkNotNull(untilCondition, "The condition cannot be null");

	if (this.untilCondition != null) {
		throw new MalformedPatternException("Only one until condition can be applied.");
	}

	if (!quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) {
		throw new MalformedPatternException("The until condition is only applicable to looping states.");
	}

	ClosureCleaner.clean(untilCondition, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true);
	this.untilCondition = untilCondition;

	return this;
}
 
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: NFAIterativeConditionTimeContextTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventTimestamp() throws Exception {
	final Event event = event().withId(1).build();
	final long timestamp = 3;

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.timestamp() == timestamp;
		}
	});

	final NFATestHarness testHarness = forPattern(pattern).build();

	final List<List<Event>> resultingPattern = testHarness.feedRecord(new StreamRecord<>(event, timestamp));

	comparePatterns(resultingPattern, Collections.singletonList(
		Collections.singletonList(event)
	));
}
 
Example #8
Source File: NFAIterativeConditionTimeContextTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventTimestamp() throws Exception {
	final Event event = event().withId(1).build();
	final long timestamp = 3;

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.timestamp() == timestamp;
		}
	});

	final NFATestHarness testHarness = forPattern(pattern).build();

	final List<List<Event>> resultingPattern = testHarness.feedRecord(new StreamRecord<>(event, timestamp));

	compareMaps(resultingPattern, Collections.singletonList(
		Collections.singletonList(event)
	));
}
 
Example #9
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * This method extends the given condition with stop(until) condition if necessary.
 * The until condition needs to be applied only if both of the given conditions are not null.
 *
 * @param condition the condition to extend
 * @param untilCondition the until condition to join with the given condition
 * @param isTakeCondition whether the {@code condition} is for {@code TAKE} edge
 * @return condition with AND applied or the original condition
 */
private IterativeCondition<T> extendWithUntilCondition(
		IterativeCondition<T> condition,
		IterativeCondition<T> untilCondition,
		boolean isTakeCondition) {
	if (untilCondition != null && condition != null) {
		return new RichAndCondition<>(new RichNotCondition<>(untilCondition), condition);
	} else if (untilCondition != null && isTakeCondition) {
		return new RichNotCondition<>(untilCondition);
	}

	return condition;
}
 
Example #10
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * @return An true function extended with stop(until) condition if necessary.
 */
@SuppressWarnings("unchecked")
private IterativeCondition<T> getTrueFunction() {
	IterativeCondition<T> trueCondition = BooleanConditions.trueFunction();
	if (currentGroupPattern != null && currentGroupPattern.getUntilCondition() != null) {
		trueCondition = extendWithUntilCondition(
			trueCondition,
			(IterativeCondition<T>) currentGroupPattern.getUntilCondition(),
			true);
	}
	return trueCondition;
}
 
Example #11
Source File: NFACompiler.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * @return the {@link IterativeCondition condition} for the {@code TAKE} edge
 * that corresponds to the specified {@link Pattern} and extended with
 * stop(until) condition if necessary.
 */
@SuppressWarnings("unchecked")
private IterativeCondition<T> getTakeCondition(Pattern<T, ?> pattern) {
	IterativeCondition<T> takeCondition = (IterativeCondition<T>) pattern.getCondition();
	if (currentGroupPattern != null && currentGroupPattern.getUntilCondition() != null) {
		takeCondition = extendWithUntilCondition(
			takeCondition,
			(IterativeCondition<T>) currentGroupPattern.getUntilCondition(),
			true);
	}
	return takeCondition;
}
 
Example #12
Source File: NFACompiler.java    From Flink-CEPplus 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 #13
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 #14
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tear-down method for the NFA.
 */
public void close() throws Exception {
	for (State<T> state : getStates()) {
		for (StateTransition<T> transition : state.getStateTransitions()) {
			IterativeCondition condition = transition.getCondition();
			FunctionUtils.closeFunction(condition);
		}
	}
}
 
Example #15
Source File: Pattern.java    From flink with Apache License 2.0 5 votes vote down vote up
public IterativeCondition<F> getCondition() {
	if (condition != null) {
		return condition;
	} else {
		return BooleanConditions.trueFunction();
	}
}
 
Example #16
Source File: NFAIterativeConditionTimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCurrentProcessingTime() throws Exception {
	final Event event1 = event().withId(1).build();
	final Event event2 = event().withId(2).build();

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.currentProcessingTime() == 3;
		}
	});

	final TestTimerService cepTimerService = new TestTimerService();
	final NFATestHarness testHarness = forPattern(pattern)
		.withTimerService(cepTimerService)
		.build();

	cepTimerService.setCurrentProcessingTime(1);
	final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
	cepTimerService.setCurrentProcessingTime(3);
	final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));

	compareMaps(resultingPatterns1, Collections.emptyList());
	compareMaps(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}
 
Example #17
Source File: NFA.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Initialization method for the NFA. It is called before any element is passed and thus suitable for one time setup
 * work.
 * @param cepRuntimeContext runtime context of the enclosing operator
 * @param conf The configuration containing the parameters attached to the contract.
 */
public void open(RuntimeContext cepRuntimeContext, Configuration conf) throws Exception {
	for (State<T> state : getStates()) {
		for (StateTransition<T> transition : state.getStateTransitions()) {
			IterativeCondition condition = transition.getCondition();
			FunctionUtils.setFunctionRuntimeContext(condition, cepRuntimeContext);
			FunctionUtils.openFunction(condition, conf);
		}
	}
}
 
Example #18
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 #19
Source File: NFACompiler.java    From Flink-CEPplus 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 #20
Source File: PatternTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testRichCondition() {
	Pattern<Event, Event> pattern =
		Pattern.<Event>begin("start")
			.where(mock(IterativeCondition.class))
			.where(mock(IterativeCondition.class))
		.followedBy("end")
			.where(mock(IterativeCondition.class))
			.or(mock(IterativeCondition.class));
	assertTrue(pattern.getCondition() instanceof RichOrCondition);
	assertTrue(pattern.getPrevious().getCondition() instanceof RichAndCondition);
}
 
Example #21
Source File: NFAIterativeConditionTimeContextTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCurrentProcessingTime() throws Exception {
	final Event event1 = event().withId(1).build();
	final Event event2 = event().withId(2).build();

	final Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new IterativeCondition<Event>() {
		@Override
		public boolean filter(Event value, Context<Event> ctx) throws Exception {
			return ctx.currentProcessingTime() == 3;
		}
	});

	final TestTimerService cepTimerService = new TestTimerService();
	final NFATestHarness testHarness = forPattern(pattern)
		.withTimerService(cepTimerService)
		.build();

	cepTimerService.setCurrentProcessingTime(1);
	final List<List<Event>> resultingPatterns1 = testHarness.feedRecord(new StreamRecord<>(event1, 7));
	cepTimerService.setCurrentProcessingTime(3);
	final List<List<Event>> resultingPatterns2 = testHarness.feedRecord(new StreamRecord<>(event2, 8));

	compareMaps(resultingPatterns1, Collections.emptyList());
	compareMaps(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}
 
Example #22
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 #23
Source File: Pattern.java    From flink with Apache License 2.0 5 votes vote down vote up
public IterativeCondition<F> getCondition() {
	if (condition != null) {
		return condition;
	} else {
		return BooleanConditions.trueFunction();
	}
}
 
Example #24
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 #25
Source File: Pattern.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a condition that has to be satisfied by an event
 * in order to be considered a match. If another condition has already been
 * set, the new one is going to be combined with the previous with a
 * logical {@code AND}. In other case, this is going to be the only
 * condition.
 *
 * @param condition The condition as an {@link IterativeCondition}.
 * @return The pattern with the new condition is set.
 */
public Pattern<T, F> where(IterativeCondition<F> condition) {
	Preconditions.checkNotNull(condition, "The condition cannot be null.");

	ClosureCleaner.clean(condition, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true);
	if (this.condition == null) {
		this.condition = condition;
	} else {
		this.condition = new RichAndCondition<>(this.condition, condition);
	}
	return this;
}
 
Example #26
Source File: Pattern.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a condition that has to be satisfied by an event
 * in order to be considered a match. If another condition has already been
 * set, the new one is going to be combined with the previous with a
 * logical {@code OR}. In other case, this is going to be the only
 * condition.
 *
 * @param condition The condition as an {@link IterativeCondition}.
 * @return The pattern with the new condition is set.
 */
public Pattern<T, F> or(IterativeCondition<F> condition) {
	Preconditions.checkNotNull(condition, "The condition cannot be null.");

	ClosureCleaner.clean(condition, ExecutionConfig.ClosureCleanerLevel.RECURSIVE, true);

	if (this.condition == null) {
		this.condition = condition;
	} else {
		this.condition = new RichOrCondition<>(this.condition, condition);
	}
	return this;
}
 
Example #27
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Initialization method for the NFA. It is called before any element is passed and thus suitable for one time setup
 * work.
 * @param cepRuntimeContext runtime context of the enclosing operator
 * @param conf The configuration containing the parameters attached to the contract.
 */
public void open(RuntimeContext cepRuntimeContext, Configuration conf) throws Exception {
	for (State<T> state : getStates()) {
		for (StateTransition<T> transition : state.getStateTransitions()) {
			IterativeCondition condition = transition.getCondition();
			FunctionUtils.setFunctionRuntimeContext(condition, cepRuntimeContext);
			FunctionUtils.openFunction(condition, conf);
		}
	}
}
 
Example #28
Source File: NFA.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tear-down method for the NFA.
 */
public void close() throws Exception {
	for (State<T> state : getStates()) {
		for (StateTransition<T> transition : state.getStateTransitions()) {
			IterativeCondition condition = transition.getCondition();
			FunctionUtils.closeFunction(condition);
		}
	}
}
 
Example #29
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) {
			// the window time is the global minimum of all window times of each state
			windowTime = currentWindowTime.toMilliseconds();
		}
	}
	return lastSink;
}
 
Example #30
Source File: PatternTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRichCondition() {
	Pattern<Event, Event> pattern =
		Pattern.<Event>begin("start")
			.where(mock(IterativeCondition.class))
			.where(mock(IterativeCondition.class))
		.followedBy("end")
			.where(mock(IterativeCondition.class))
			.or(mock(IterativeCondition.class));
	assertTrue(pattern.getCondition() instanceof RichOrCondition);
	assertTrue(pattern.getPrevious().getCondition() instanceof RichAndCondition);
}