org.apache.flink.cep.utils.NFATestHarness Java Examples

The following examples show how to use org.apache.flink.cep.utils.NFATestHarness. 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: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that pruning shared buffer elements and computations state use the same window border
 * semantics (left side inclusive and right side exclusive).
 */
@Test
public void testTimeoutWindowPruningWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "start", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "foobar", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(2, "start", 2.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
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: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that pruning shared buffer elements and computations state use the same window border
 * semantics (left side inclusive and right side exclusive).
 */
@Test
public void testTimeoutWindowPruningWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "start", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "foobar", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(2, "start", 2.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #4
Source File: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that elements whose timestamp difference is exactly the window length are not matched.
 * The reason is that the right window side (later elements) is exclusive.
 */
@Test
public void testWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "end", 2.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = Collections.emptyList();

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #5
Source File: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeoutWindowPruning() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "bar", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "start", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 4L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(3, "start", 3.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #6
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 #7
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 #8
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 #9
Source File: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that elements whose timestamp difference is exactly the window length are not matched.
 * The reason is that the right window side (later elements) is exclusive.
 */
@Test
public void testWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "end", 2.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = Collections.emptyList();

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #10
Source File: NFATest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeoutWindowPruning() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "bar", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "start", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 4L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(3, "start", 3.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #11
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 #12
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 #13
Source File: NFATest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeoutWindowPruning() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "bar", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "start", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 4L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(3, "start", 3.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #14
Source File: NFATest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that elements whose timestamp difference is exactly the window length are not matched.
 * The reason is that the right window side (later elements) is exclusive.
 */
@Test
public void testWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "end", 2.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = Collections.emptyList();

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #15
Source File: NFATest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that pruning shared buffer elements and computations state use the same window border
 * semantics (left side inclusive and right side exclusive).
 */
@Test
public void testTimeoutWindowPruningWindowBorders() throws Exception {
	List<StreamRecord<Event>> streamEvents = new ArrayList<>();

	streamEvents.add(new StreamRecord<>(new Event(1, "start", 1.0), 1L));
	streamEvents.add(new StreamRecord<>(new Event(2, "start", 2.0), 2L));
	streamEvents.add(new StreamRecord<>(new Event(3, "foobar", 3.0), 3L));
	streamEvents.add(new StreamRecord<>(new Event(4, "end", 4.0), 3L));

	List<Map<String, List<Event>>> expectedPatterns = new ArrayList<>();

	Map<String, List<Event>> secondPattern = new HashMap<>();
	secondPattern.put("start", Collections.singletonList(new Event(2, "start", 2.0)));
	secondPattern.put("end", Collections.singletonList(new Event(4, "end", 4.0)));

	expectedPatterns.add(secondPattern);

	NFA<Event> nfa = createStartEndNFA();
	NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).build();

	Collection<Map<String, List<Event>>> actualPatterns = nfaTestHarness.consumeRecords(streamEvents);

	assertEquals(expectedPatterns, actualPatterns);
}
 
Example #16
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 #17
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 #18
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 #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: 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));

	comparePatterns(resultingPatterns1, Collections.emptyList());
	comparePatterns(resultingPatterns2, Collections.singletonList(
		Collections.singletonList(event2)
	));
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
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 #28
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());
}
 
Example #29
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 #30
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());
}