Java Code Examples for org.apache.flink.cep.utils.NFATestHarness#feedRecords()

The following examples show how to use org.apache.flink.cep.utils.NFATestHarness#feedRecords() . 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: 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 2
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 3
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 4
Source File: AfterMatchSkipITCase.java    From Flink-CEPplus 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);

	compareMaps(resultingPatterns, Lists.newArrayList(
		Lists.newArrayList(a1, a2, a3),
		Lists.newArrayList(a2, a3, a4),
		Lists.newArrayList(a3, a4, a5),
		Lists.newArrayList(a4, a5, a6)
	));
}
 
Example 5
Source File: AfterMatchSkipITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipPastLast() 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.skipPastLastEvent())
		.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);

	compareMaps(resultingPatterns, Lists.newArrayList(
		Lists.newArrayList(a1, a2, a3),
		Lists.newArrayList(a4, a5, a6)
	));
}
 
Example 6
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 7
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);

	compareMaps(resultingPatterns, Lists.newArrayList(
		Lists.newArrayList(a1, a2, a3),
		Lists.newArrayList(a2, a3, a4),
		Lists.newArrayList(a3, a4, a5),
		Lists.newArrayList(a4, a5, a6)
	));
}
 
Example 8
Source File: AfterMatchSkipITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipPastLast() 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.skipPastLastEvent())
		.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);

	compareMaps(resultingPatterns, Lists.newArrayList(
		Lists.newArrayList(a1, a2, a3),
		Lists.newArrayList(a4, a5, a6)
	));
}
 
Example 9
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 10
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 11
Source File: AfterMatchSkipITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipPastLast() 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.skipPastLastEvent())
		.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(a4, a5, a6)
	));
}
 
Example 12
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 13
Source File: UntilConditionITCase.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testUntilConditionWithEmptyWhere() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	Event startEvent = new Event(40, "c", 1.0);
	Event middleEvent1 = new Event(41, "a", 2.0);
	Event middleEvent2 = new Event(42, "a", 3.0);
	Event middleEvent3 = new Event(40, "d", 1.0);
	Event breaking = new Event(44, "a", 5.0);
	Event ignored = new Event(45, "a", 6.0);

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

	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("c");
		}
	}).followedBy("middle").oneOrMore().until(UNTIL_CONDITION);

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

	NFAState nfaState = nfa.createInitialNFAState();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();

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

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3),
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2),
		Lists.newArrayList(startEvent, middleEvent1)
	));

	assertEquals(1, nfaState.getPartialMatches().size());
	assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
 
Example 14
Source File: UntilConditionITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUntilConditionWithEmptyWhere() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	Event startEvent = new Event(40, "c", 1.0);
	Event middleEvent1 = new Event(41, "a", 2.0);
	Event middleEvent2 = new Event(42, "a", 3.0);
	Event middleEvent3 = new Event(40, "d", 1.0);
	Event breaking = new Event(44, "a", 5.0);
	Event ignored = new Event(45, "a", 6.0);

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

	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("c");
		}
	}).followedBy("middle").oneOrMore().until(UNTIL_CONDITION);

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

	NFAState nfaState = nfa.createInitialNFAState();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();

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

	compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3),
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2),
		Lists.newArrayList(startEvent, middleEvent1)
	));

	assertEquals(1, nfaState.getPartialMatches().size());
	assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
 
Example 15
Source File: UntilConditionITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUntilConditionWithEmptyWhere() throws Exception {
	List<StreamRecord<Event>> inputEvents = new ArrayList<>();

	Event startEvent = new Event(40, "c", 1.0);
	Event middleEvent1 = new Event(41, "a", 2.0);
	Event middleEvent2 = new Event(42, "a", 3.0);
	Event middleEvent3 = new Event(40, "d", 1.0);
	Event breaking = new Event(44, "a", 5.0);
	Event ignored = new Event(45, "a", 6.0);

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

	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("c");
		}
	}).followedBy("middle").oneOrMore().until(UNTIL_CONDITION);

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

	NFAState nfaState = nfa.createInitialNFAState();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();

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

	comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3),
		Lists.newArrayList(startEvent, middleEvent1, middleEvent2),
		Lists.newArrayList(startEvent, middleEvent1)
	));

	assertEquals(1, nfaState.getPartialMatches().size());
	assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}