org.apache.flink.cep.nfa.compiler.NFACompiler Java Examples

The following examples show how to use org.apache.flink.cep.nfa.compiler.NFACompiler. 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: CepOperatorBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFA(NFA<Event> nfa) {
	return new CepOperatorBuilder<>(
		true,
		new NFACompiler.NFAFactory<Event>() {
			@Override
			public NFA<Event> createNFA() {
				return nfa;
			}
		},
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #2
Source File: CepOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
public CepOperator(
		final TypeSerializer<IN> inputSerializer,
		final boolean isProcessingTime,
		final NFACompiler.NFAFactory<IN> nfaFactory,
		@Nullable final EventComparator<IN> comparator,
		@Nullable final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final PatternProcessFunction<IN, OUT> function,
		@Nullable final OutputTag<IN> lateDataOutputTag) {
	super(function);

	this.inputSerializer = Preconditions.checkNotNull(inputSerializer);
	this.nfaFactory = Preconditions.checkNotNull(nfaFactory);

	this.isProcessingTime = isProcessingTime;
	this.comparator = comparator;
	this.lateDataOutputTag = lateDataOutputTag;

	if (afterMatchSkipStrategy == null) {
		this.afterMatchSkipStrategy = AfterMatchSkipStrategy.noSkip();
	} else {
		this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	}
}
 
Example #3
Source File: CepOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
public CepOperator(
		final TypeSerializer<IN> inputSerializer,
		final boolean isProcessingTime,
		final NFACompiler.NFAFactory<IN> nfaFactory,
		@Nullable final EventComparator<IN> comparator,
		@Nullable final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final PatternProcessFunction<IN, OUT> function,
		@Nullable final OutputTag<IN> lateDataOutputTag) {
	super(function);

	this.inputSerializer = Preconditions.checkNotNull(inputSerializer);
	this.nfaFactory = Preconditions.checkNotNull(nfaFactory);

	this.isProcessingTime = isProcessingTime;
	this.comparator = comparator;
	this.lateDataOutputTag = lateDataOutputTag;

	if (afterMatchSkipStrategy == null) {
		this.afterMatchSkipStrategy = AfterMatchSkipStrategy.noSkip();
	} else {
		this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	}
}
 
Example #4
Source File: CepOperatorBuilder.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFAFactory(NFACompiler.NFAFactory<Event> nfaFactory) {
	return new CepOperatorBuilder<>(
		true,
		nfaFactory,
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #5
Source File: CepOperatorBuilder.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFA(NFA<Event> nfa) {
	return new CepOperatorBuilder<>(
		true,
		new NFACompiler.NFAFactory<Event>() {
			@Override
			public NFA<Event> createNFA() {
				return nfa;
			}
		},
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #6
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 #7
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFAFactory(NFACompiler.NFAFactory<Event> nfaFactory) {
	return new CepOperatorBuilder<>(
		true,
		nfaFactory,
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #8
Source File: CepOperatorTestUtilities.java    From flink with Apache License 2.0 6 votes vote down vote up
public static <K> CepOperator<Event, K, Map<String, List<Event>>> getKeyedCepOpearator(
		boolean isProcessingTime,
		NFACompiler.NFAFactory<Event> nfaFactory,
		EventComparator<Event> comparator,
		OutputTag<Event> outputTag) {

	return new CepOperator<>(
		Event.createTypeSerializer(),
		isProcessingTime,
		nfaFactory,
		comparator,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
					Map<String, List<Event>> match,
					Context ctx,
					Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		outputTag);
}
 
Example #9
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 #10
Source File: CepOperatorTestUtilities.java    From flink with Apache License 2.0 6 votes vote down vote up
public static <K> CepOperator<Event, K, Map<String, List<Event>>> getKeyedCepOpearator(
		boolean isProcessingTime,
		NFACompiler.NFAFactory<Event> nfaFactory,
		EventComparator<Event> comparator,
		OutputTag<Event> outputTag) {

	return new CepOperator<>(
		Event.createTypeSerializer(),
		isProcessingTime,
		nfaFactory,
		comparator,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
					Map<String, List<Event>> match,
					Context ctx,
					Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		outputTag);
}
 
Example #11
Source File: CepOperatorTestUtilities.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static <K> CepOperator<Event, K, Map<String, List<Event>>> getKeyedCepOpearator(
		boolean isProcessingTime,
		NFACompiler.NFAFactory<Event> nfaFactory,
		EventComparator<Event> comparator,
		OutputTag<Event> outputTag) {

	return new CepOperator<>(
		Event.createTypeSerializer(),
		isProcessingTime,
		nfaFactory,
		comparator,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
					Map<String, List<Event>> match,
					Context ctx,
					Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		outputTag);
}
 
Example #12
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFA(NFA<Event> nfa) {
	return new CepOperatorBuilder<>(
		true,
		new NFACompiler.NFAFactory<Event>() {
			@Override
			public NFA<Event> createNFA() {
				return nfa;
			}
		},
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #13
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 6 votes vote down vote up
public static CepOperatorBuilder<Map<String, List<Event>>> createOperatorForNFAFactory(NFACompiler.NFAFactory<Event> nfaFactory) {
	return new CepOperatorBuilder<>(
		true,
		nfaFactory,
		null,
		null,
		new PatternProcessFunction<Event, Map<String, List<Event>>>() {
			private static final long serialVersionUID = -7143807777582726991L;

			@Override
			public void processMatch(
				Map<String, List<Event>> match,
				Context ctx,
				Collector<Map<String, List<Event>>> out) throws Exception {
				out.collect(match);
			}
		},
		null);
}
 
Example #14
Source File: CEPOperatorTest.java    From Flink-CEPplus 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 #15
Source File: CepOperator.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public CepOperator(
		final TypeSerializer<IN> inputSerializer,
		final boolean isProcessingTime,
		final NFACompiler.NFAFactory<IN> nfaFactory,
		@Nullable final EventComparator<IN> comparator,
		@Nullable final AfterMatchSkipStrategy afterMatchSkipStrategy,
		final PatternProcessFunction<IN, OUT> function,
		@Nullable final OutputTag<IN> lateDataOutputTag) {
	super(function);

	this.inputSerializer = Preconditions.checkNotNull(inputSerializer);
	this.nfaFactory = Preconditions.checkNotNull(nfaFactory);

	this.isProcessingTime = isProcessingTime;
	this.comparator = comparator;
	this.lateDataOutputTag = lateDataOutputTag;

	if (afterMatchSkipStrategy == null) {
		this.afterMatchSkipStrategy = AfterMatchSkipStrategy.noSkip();
	} else {
		this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	}
}
 
Example #16
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 #17
Source File: CepProcessFunctionContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private <T> CepOperator<Event, Integer, T> createCepOperator(
		PatternProcessFunction<Event, T> processFunction,
		NFACompiler.NFAFactory<Event> nfaFactory,
		boolean isProcessingTime) throws Exception {
	return new CepOperator<>(
		Event.createTypeSerializer(),
		isProcessingTime,
		nfaFactory,
		null,
		null,
		processFunction,
		null);
}
 
Example #18
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new StartFilter())
			.followedByAny("middle")
			.subtype(SubEvent.class)
			.where(new MiddleFilter())
			.followedByAny("end")
			.where(new EndFilter())
			// add a window timeout to test whether timestamps of elements in the
			// priority queue in CEP operator are correctly checkpointed/restored
			.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #19
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
public CepOperatorBuilder<OUT> withNFA(NFA<Event> nfa) {
	return new CepOperatorBuilder<>(
		false,
		new NFACompiler.NFAFactory<Event>() {
			@Override
			public NFA<Event> createNFA() {
				return nfa;
			}
		},
		comparator,
		skipStrategy,
		function,
		lateDataOutputTag);
}
 
Example #20
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
private CepOperatorBuilder(
	boolean isProcessingTime,
	NFACompiler.NFAFactory<Event> nfaFactory,
	EventComparator<Event> comparator,
	AfterMatchSkipStrategy skipStrategy,
	PatternProcessFunction<Event, OUT> processFunction,
	OutputTag<Event> lateDataOutputTag) {
	this.isProcessingTime = isProcessingTime;
	this.nfaFactory = nfaFactory;
	this.comparator = comparator;
	this.skipStrategy = skipStrategy;
	function = processFunction;
	this.lateDataOutputTag = lateDataOutputTag;
}
 
Example #21
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
public CepOperatorBuilder<OUT> withNFA(NFA<Event> nfa) {
	return new CepOperatorBuilder<>(
		false,
		new NFACompiler.NFAFactory<Event>() {
			@Override
			public NFA<Event> createNFA() {
				return nfa;
			}
		},
		comparator,
		skipStrategy,
		function,
		lateDataOutputTag);
}
 
Example #22
Source File: CepOperatorTestUtilities.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <K> CepOperator<Event, K, Map<String, List<Event>>> getKeyedCepOpearator(
		boolean isProcessingTime,
		NFACompiler.NFAFactory<Event> nfaFactory,
		EventComparator<Event> comparator) {

	return getKeyedCepOpearator(isProcessingTime, nfaFactory, comparator, null);
}
 
Example #23
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new StartFilter())
			.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #24
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start")
		.subtype(SubEvent.class)
		.where(new MiddleFilter())
		.or(new SubEventEndFilter())
		.times(2)
		.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #25
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new StartFilter())
			.followedByAny("middle")
			.subtype(SubEvent.class)
			.where(new MiddleFilter())
			.followedByAny("end")
			.where(new EndFilter())
			// add a window timeout to test whether timestamps of elements in the
			// priority queue in CEP operator are correctly checkpointed/restored
			.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #26
Source File: CepProcessFunctionContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private <T> CepOperator<Event, Integer, T> createCepOperator(
		PatternProcessFunction<Event, T> processFunction,
		NFACompiler.NFAFactory<Event> nfaFactory,
		boolean isProcessingTime) throws Exception {
	return new CepOperator<>(
		Event.createTypeSerializer(),
		isProcessingTime,
		nfaFactory,
		null,
		null,
		processFunction,
		null);
}
 
Example #27
Source File: CepOperatorBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
private CepOperatorBuilder(
	boolean isProcessingTime,
	NFACompiler.NFAFactory<Event> nfaFactory,
	EventComparator<Event> comparator,
	AfterMatchSkipStrategy skipStrategy,
	PatternProcessFunction<Event, OUT> processFunction,
	OutputTag<Event> lateDataOutputTag) {
	this.isProcessingTime = isProcessingTime;
	this.nfaFactory = nfaFactory;
	this.comparator = comparator;
	this.skipStrategy = skipStrategy;
	function = processFunction;
	this.lateDataOutputTag = lateDataOutputTag;
}
 
Example #28
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new StartFilter())
			.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #29
Source File: CepOperatorTestUtilities.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <K> CepOperator<Event, K, Map<String, List<Event>>> getKeyedCepOpearator(
		boolean isProcessingTime,
		NFACompiler.NFAFactory<Event> nfaFactory,
		EventComparator<Event> comparator) {

	return getKeyedCepOpearator(isProcessingTime, nfaFactory, comparator, null);
}
 
Example #30
Source File: CEPMigrationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFA<Event> createNFA() {

	Pattern<Event, ?> pattern = Pattern.<Event>begin("start")
		.subtype(SubEvent.class)
		.where(new MiddleFilter())
		.or(new SubEventEndFilter())
		.times(2)
		.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}