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

The following examples show how to use org.apache.flink.cep.pattern.Pattern. 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: CepOperator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
	 * @Description: 触发新逻辑注入时通过调用用户代码得到返回的pattern更新NFA
	 * @param: []
	 * @return: void
	 * @auther: greenday
	 * @date: 2019/9/9 15:54
	 */
	private void changeNFA(IN flagElement)throws Exception{
		Pattern pattern ;
		pattern = userFunction.getNewPattern(flagElement);
		NFACompiler.NFAFactoryCompiler<IN> nfaFactoryCompiler = new NFACompiler.NFAFactoryCompiler<IN>((Pattern<IN,?>)pattern);
		nfaFactoryCompiler.compileFactory();
		boolean timeoutHandling = userFunction instanceof TimedOutPartialMatchHandler;
		NFACompiler.NFAFactory nfaFactory = NFACompiler.compileFactory(pattern, timeoutHandling);
//		得到工厂的nfa
		NFA<IN> newNFA = nfaFactory.createNFA();
//		这个地方为所有的边transition设置了cepRuntimeContext
		newNFA.open(cepRuntimeContext, new Configuration());
//		覆盖
		nfa = newNFA;
//		清理以前的未完成的数据,以及共享缓存
		cleanBeforeMatch();
		cleanSharedBuffer();
	}
 
Example #2
Source File: NFAITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example #3
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example #4
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 #5
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void testStartWithOneOrZeroOrMoreStrict(Pattern<Event, ?> pattern) throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	inputEvents.add(new StreamRecord<>(ConsecutiveData.startEvent, 1));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent1, 3));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.startEvent, 4));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent2, 5));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent3, 6));

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

	List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.middleEvent1),
		Lists.newArrayList(ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3),
		Lists.newArrayList(ConsecutiveData.middleEvent2),
		Lists.newArrayList(ConsecutiveData.middleEvent3)
	));
}
 
Example #6
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 #7
Source File: CEPOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testProcessingTimestampisPassedToNFA() throws Exception {

	final NFA<Event> nfa = NFACompiler.compileFactory(Pattern.<Event>begin("begin"), true).createNFA();
	final NFA<Event> spyNFA = spy(nfa);

	try (
		OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness =
			CepOperatorTestUtilities.getCepTestHarness(createOperatorForNFA(spyNFA).build())) {

		long timestamp = 5;
		harness.open();
		harness.setProcessingTime(timestamp);
		StreamRecord<Event> event = event().withTimestamp(3).asStreamRecord();
		harness.processElement(event);
		verify(spyNFA).process(
			any(SharedBufferAccessor.class),
			any(NFAState.class),
			eq(event.getValue()),
			eq(timestamp),
			any(AfterMatchSkipStrategy.class),
			any(TimerService.class));
	}
}
 
Example #8
Source File: NFAITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferClearing() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end");

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);

	NFA<Event> nfa = compile(pattern, false);
	TestTimerService timerService = new TestTimerService();
	try (SharedBufferAccessor<Event> accessor = Mockito.spy(sharedBuffer.getAccessor())) {
		nfa.process(accessor, nfa.createInitialNFAState(), a, 1, AfterMatchSkipStrategy.noSkip(),
			timerService);
		nfa.process(accessor, nfa.createInitialNFAState(), b, 2, AfterMatchSkipStrategy.noSkip(),
			timerService);
		Mockito.verify(accessor, Mockito.never()).advanceTime(anyLong());
		nfa.advanceTime(accessor, nfa.createInitialNFAState(), 2);
		Mockito.verify(accessor, Mockito.times(1)).advanceTime(2);
	}
}
 
Example #9
Source File: NFAITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
private void testStartWithOneOrZeroOrMoreStrict(Pattern<Event, ?> pattern) throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	inputEvents.add(new StreamRecord<>(ConsecutiveData.startEvent, 1));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent1, 3));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.startEvent, 4));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent2, 5));
	inputEvents.add(new StreamRecord<>(ConsecutiveData.middleEvent3, 6));

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

	List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(ConsecutiveData.middleEvent1),
		Lists.newArrayList(ConsecutiveData.middleEvent2, ConsecutiveData.middleEvent3),
		Lists.newArrayList(ConsecutiveData.middleEvent2),
		Lists.newArrayList(ConsecutiveData.middleEvent3)
	));
}
 
Example #10
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 #11
Source File: Quantifier.java    From flink-cep-dsl with Apache License 2.0 6 votes vote down vote up
public Pattern<Event, Event> apply(Pattern<Event, Event> pattern) {
    if (oneOrMore) {
        pattern = pattern.oneOrMore();
    }
    if (zeroOrMore) {
        pattern = pattern.oneOrMore().optional();
    }
    if (lowerBound != null && upperBound == null) {
        pattern = pattern.times(lowerBound);
    }
    if (lowerBound != null && upperBound != null) {
        if (upperBound < Integer.MAX_VALUE) {
            pattern = pattern.times(lowerBound, upperBound);
        }
        else {
            pattern = pattern.timesOrMore(lowerBound);
        }
    }
    if (optional) {
        pattern = pattern.optional();
    }
    if (greedy) {
        pattern = pattern.greedy();
    }
    return pattern;
}
 
Example #12
Source File: NFACompiler.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Check pattern after match skip strategy.
 */
private void checkPatternSkipStrategy() {
	if (afterMatchSkipStrategy.getPatternName().isPresent()) {
		String patternName = afterMatchSkipStrategy.getPatternName().get();
		Pattern<T, ?> pattern = currentPattern;
		while (pattern.getPrevious() != null && !pattern.getName().equals(patternName)) {
			pattern = pattern.getPrevious();
		}

		// pattern name match check.
		if (!pattern.getName().equals(patternName)) {
			throw new MalformedPatternException("The pattern name specified in AfterMatchSkipStrategy " +
				"can not be found in the given Pattern");
		}
	}
}
 
Example #13
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 #14
Source File: AfterMatchSkipITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferIsProperlyCleared() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	for (int i = 0; i < 4; i++) {
		inputEvents.add(new StreamRecord<>(new Event(1, "a", 1.0), i));
	}

	SkipPastLastStrategy matchSkipStrategy = AfterMatchSkipStrategy.skipPastLastEvent();
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start", matchSkipStrategy)
		.where(new SimpleCondition<Event>() {
			private static final long serialVersionUID = 5726188262756267490L;

			@Override
			public boolean filter(Event value) throws Exception {
				return true;
			}
		}).times(2);

	SharedBuffer<Event> sharedBuffer = TestSharedBuffer.createTestBuffer(Event.createTypeSerializer());
	NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).withSharedBuffer(sharedBuffer).build();

	nfaTestHarness.feedRecords(inputEvents);

	assertThat(sharedBuffer.isEmpty(), Matchers.is(true));
}
 
Example #15
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 #16
Source File: AfterMatchSkipITCase.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedBufferIsProperlyCleared() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	for (int i = 0; i < 4; i++) {
		inputEvents.add(new StreamRecord<>(new Event(1, "a", 1.0), i));
	}

	SkipPastLastStrategy matchSkipStrategy = AfterMatchSkipStrategy.skipPastLastEvent();
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start", matchSkipStrategy)
		.where(new SimpleCondition<Event>() {
			private static final long serialVersionUID = 5726188262756267490L;

			@Override
			public boolean filter(Event value) throws Exception {
				return true;
			}
		}).times(2);

	SharedBuffer<Event> sharedBuffer = TestSharedBuffer.createTestBuffer(Event.createTypeSerializer());
	NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).withSharedBuffer(sharedBuffer).build();

	nfaTestHarness.feedRecords(inputEvents);

	assertThat(sharedBuffer.isEmpty(), Matchers.is(true));
}
 
Example #17
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 #18
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 #19
Source File: AfterMatchSkipITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoSkip() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	Event a1 = new Event(1, "a", 0.0);
	Event a2 = new Event(2, "a", 0.0);
	Event a3 = new Event(3, "a", 0.0);
	Event a4 = new Event(4, "a", 0.0);
	Event a5 = new Event(5, "a", 0.0);
	Event a6 = new Event(6, "a", 0.0);

	streamEvents.add(new StreamRecord<Event>(a1));
	streamEvents.add(new StreamRecord<Event>(a2));
	streamEvents.add(new StreamRecord<Event>(a3));
	streamEvents.add(new StreamRecord<Event>(a4));
	streamEvents.add(new StreamRecord<Event>(a5));
	streamEvents.add(new StreamRecord<Event>(a6));

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.noSkip())
		.where(new SimpleCondition<Event>() {

			@Override
			public boolean filter(Event value) throws Exception {
				return value.getName().equals("a");
			}
		}).times(3);

	NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();

	List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);

	comparePatterns(resultingPatterns, Lists.newArrayList(
		Lists.newArrayList(a1, a2, a3),
		Lists.newArrayList(a2, a3, a4),
		Lists.newArrayList(a3, a4, a5),
		Lists.newArrayList(a4, a5, a6)
	));
}
 
Example #20
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 #21
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 #22
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Check if there are duplicate pattern names. If yes, it
 * throws a {@link MalformedPatternException}.
 */
private void checkPatternNameUniqueness() {
	// make sure there is no pattern with name "$endState$"
	stateNameHandler.checkNameUniqueness(ENDING_STATE_NAME);
	Pattern patternToCheck = currentPattern;
	while (patternToCheck != null) {
		checkPatternNameUniqueness(patternToCheck);
		patternToCheck = patternToCheck.getPrevious();
	}
	stateNameHandler.clear();
}
 
Example #23
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 #24
Source File: AfterMatchSkipITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test(expected = FlinkRuntimeException.class)
public void testSkipToFirstElementOfMatch() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	Event a1 = new Event(1, "a1", 0.0);

	streamEvents.add(new StreamRecord<Event>(a1));

	Pattern<Event, ?> pattern = Pattern.<Event>begin("a",
		AfterMatchSkipStrategy.skipToFirst("a").throwExceptionOnMiss()
	).where(
		new SimpleCondition<Event>() {

			@Override
			public boolean filter(Event value) throws Exception {
				return value.getName().contains("a");
			}
		}
	);
	NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();

	List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);

	//skip to first element of a match should throw exception if they are enabled,
	//this mode is used in MATCH RECOGNIZE which assumes that skipping to first element
	//would result in infinite loop. In CEP by default(with exceptions disabled), we use no skip
	//strategy in this case.
}
 
Example #25
Source File: AfterMatchSkipITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test(expected = FlinkRuntimeException.class)
public void testSkipToFirstElementOfMatch() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	Event a1 = new Event(1, "a1", 0.0);

	streamEvents.add(new StreamRecord<Event>(a1));

	Pattern<Event, ?> pattern = Pattern.<Event>begin("a",
		AfterMatchSkipStrategy.skipToFirst("a").throwExceptionOnMiss()
	).where(
		new SimpleCondition<Event>() {

			@Override
			public boolean filter(Event value) throws Exception {
				return value.getName().contains("a");
			}
		}
	);
	NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();

	List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);

	//skip to first element of a match should throw exception if they are enabled,
	//this mode is used in MATCH RECOGNIZE which assumes that skipping to first element
	//would result in infinite loop. In CEP by default(with exceptions disabled), we use no skip
	//strategy in this case.
}
 
Example #26
Source File: NFACompiler.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create
 * multiple NFAs.
 *
 * @param pattern Definition of sequence pattern
 * @param timeoutHandling True if the NFA shall return timed out event patterns
 * @param <T> Type of the input events
 * @return Factory for NFAs corresponding to the given pattern
 */
@SuppressWarnings("unchecked")
public static <T> NFAFactory<T> compileFactory(
	final Pattern<T, ?> pattern,
	boolean timeoutHandling) {
	if (pattern == null) {
		// return a factory for empty NFAs
		return new NFAFactoryImpl<>(0, Collections.<State<T>>emptyList(), timeoutHandling);
	} else {
		final NFAFactoryCompiler<T> nfaFactoryCompiler = new NFAFactoryCompiler<>(pattern);
		nfaFactoryCompiler.compileFactory();
		return new NFAFactoryImpl<>(nfaFactoryCompiler.getWindowTime(), nfaFactoryCompiler.getStates(), timeoutHandling);
	}
}
 
Example #27
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithZeroOrMoreStrict() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
		private static final long serialVersionUID = 5726188262756267490L;

		@Override
		public boolean filter(Event value) throws Exception {
			return value.getName().equals("a");
		}
	}).oneOrMore().optional().consecutive();

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #28
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoConditionLoopingNFA() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	Event a = new Event(40, "a", 1.0);
	Event b = new Event(41, "b", 2.0);
	Event c = new Event(42, "c", 3.0);
	Event d = new Event(43, "d", 4.0);
	Event e = new Event(44, "e", 5.0);

	inputEvents.add(new StreamRecord<>(a, 1));
	inputEvents.add(new StreamRecord<>(b, 2));
	inputEvents.add(new StreamRecord<>(c, 3));
	inputEvents.add(new StreamRecord<>(d, 4));
	inputEvents.add(new StreamRecord<>(e, 5));

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").followedBy("end").oneOrMore();

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

	List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(a, b, c, d, e),
		Lists.newArrayList(a, b, c, d),
		Lists.newArrayList(a, b, c),
		Lists.newArrayList(a, b),
		Lists.newArrayList(b, c, d, e),
		Lists.newArrayList(b, c, d),
		Lists.newArrayList(b, c),
		Lists.newArrayList(c, d, e),
		Lists.newArrayList(c, d),
		Lists.newArrayList(d, e)
	));
}
 
Example #29
Source File: PatternStreamBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
private PatternStreamBuilder(
		final DataStream<IN> inputStream,
		final Pattern<IN, ?> pattern,
		@Nullable final EventComparator<IN> comparator,
		@Nullable final OutputTag<IN> lateDataOutputTag) {

	this.inputStream = checkNotNull(inputStream);
	this.pattern = checkNotNull(pattern);
	this.comparator = comparator;
	this.lateDataOutputTag = lateDataOutputTag;
}
 
Example #30
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 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;
}