org.apache.flink.cep.nfa.NFA Java Examples

The following examples show how to use org.apache.flink.cep.nfa.NFA. 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
private void migrateOldState() throws Exception {
	getKeyedStateBackend().applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		new ValueStateDescriptor<>(
			"nfaOperatorStateName",
			new NFA.NFASerializer<>(inputSerializer)
		),
		new KeyedStateFunction<Object, ValueState<MigratedNFA<IN>>>() {
			@Override
			public void process(Object key, ValueState<MigratedNFA<IN>> state) throws Exception {
				MigratedNFA<IN> oldState = state.value();
				computationStates.update(new NFAState(oldState.getComputationStates()));
				org.apache.flink.cep.nfa.SharedBuffer<IN> sharedBuffer = oldState.getSharedBuffer();
				partialMatches.init(sharedBuffer.getEventsBuffer(), sharedBuffer.getPages());
				state.clear();
			}
		}
	);
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
Source File: CepOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private void migrateOldState() throws Exception {
	getKeyedStateBackend().applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		new ValueStateDescriptor<>(
			"nfaOperatorStateName",
			new NFA.NFASerializer<>(inputSerializer)
		),
		new KeyedStateFunction<Object, ValueState<MigratedNFA<IN>>>() {
			@Override
			public void process(Object key, ValueState<MigratedNFA<IN>> state) throws Exception {
				MigratedNFA<IN> oldState = state.value();
				computationStates.update(new NFAState(oldState.getComputationStates()));
				org.apache.flink.cep.nfa.SharedBuffer<IN> sharedBuffer = oldState.getSharedBuffer();
				partialMatches.init(sharedBuffer.getEventsBuffer(), sharedBuffer.getPages());
				state.clear();
			}
		}
	);
}
 
Example #9
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #10
Source File: CepRuntimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCepRuntimeContextIsSetInNFA() throws Exception {

	@SuppressWarnings("unchecked")
	final NFA<Event> mockNFA = mock(NFA.class);

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

		harness.open();
		verify(mockNFA).open(any(CepRuntimeContext.class), any(Configuration.class));
	}
}
 
Example #11
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFATestHarness build() {
	final NFA<Event> nfa = NFAUtils.compile(pattern, timeoutHandling);
	return new NFATestHarness(
		sharedBuffer,
		nfa,
		nfa.createInitialNFAState(),
		afterMatchSkipStrategy,
		timerService);
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
Source File: CepRuntimeContextTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCepRuntimeContextIsSetInNFA() throws Exception {

	@SuppressWarnings("unchecked")
	final NFA<Event> mockNFA = mock(NFA.class);

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

		harness.open();
		verify(mockNFA).open(any(CepRuntimeContext.class), any(Configuration.class));
	}
}
 
Example #17
Source File: NFATestUtilities.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static List<List<Event>> feedNFA(
		List<StreamRecord<Event>> inputEvents,
		NFA<Event> nfa) throws Exception {
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
	return nfaTestHarness.feedRecords(inputEvents);
}
 
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")
		.subtype(SubEvent.class)
		.where(new MiddleFilter())
		.or(new SubEventEndFilter())
		.times(2)
		.within(Time.milliseconds(10L));

	return NFACompiler.compileFactory(pattern, handleTimeout).createNFA();
}
 
Example #19
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 #20
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 #21
Source File: CepOperatorBuilder.java    From Flink-CEPplus 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: NFATestUtilities.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static List<List<Event>> feedNFA(
		List<StreamRecord<Event>> inputEvents,
		NFA<Event> nfa) throws Exception {
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
	return nfaTestHarness.feedRecords(inputEvents);
}
 
Example #23
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #24
Source File: NFATestHarness.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public NFATestHarness build() {
	final NFA<Event> nfa = NFAUtils.compile(pattern, timeoutHandling);
	return new NFATestHarness(
		sharedBuffer,
		nfa,
		nfa.createInitialNFAState(),
		afterMatchSkipStrategy,
		timerService);
}
 
Example #25
Source File: NFATestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public NFATestHarness build() {
	final NFA<Event> nfa = NFAUtils.compile(pattern, timeoutHandling);
	return new NFATestHarness(
		sharedBuffer,
		nfa,
		nfa.createInitialNFAState(),
		afterMatchSkipStrategy,
		timerService);
}
 
Example #26
Source File: NFATestHarness.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private NFATestHarness(
		SharedBuffer<Event> sharedBuffer,
		NFA<Event> nfa,
		NFAState nfaState,
		AfterMatchSkipStrategy afterMatchSkipStrategy,
		TimerService timerService) {
	this.sharedBuffer = sharedBuffer;
	this.nfa = nfa;
	this.nfaState = nfaState;
	this.afterMatchSkipStrategy = afterMatchSkipStrategy;
	this.timerService = timerService;
}
 
Example #27
Source File: NFATestUtilities.java    From flink with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static List<List<Event>> feedNFA(
		List<StreamRecord<Event>> inputEvents,
		NFA<Event> nfa) throws Exception {
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();
	return nfaTestHarness.feedRecords(inputEvents);
}
 
Example #28
Source File: CepRuntimeContextTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testCepRuntimeContextIsSetInNFA() throws Exception {

	@SuppressWarnings("unchecked")
	final NFA<Event> mockNFA = mock(NFA.class);

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

		harness.open();
		verify(mockNFA).open(any(CepRuntimeContext.class), any(Configuration.class));
	}
}
 
Example #29
Source File: CEPMigrationTest.java    From Flink-CEPplus 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 #30
Source File: CEPMigrationTest.java    From Flink-CEPplus 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();
}