org.apache.flink.cep.pattern.conditions.SimpleCondition Java Examples

The following examples show how to use org.apache.flink.cep.pattern.conditions.SimpleCondition. 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: PatternTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatternWithSubtypingAndFilter() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").next("subevent").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {
		private static final long serialVersionUID = -4118591291880230304L;

		@Override
		public boolean filter(SubEvent value) throws Exception {
			return false;
		}
	}).followedBy("end");

	Pattern<Event, ?> previous;
	Pattern<Event, ?> previous2;

	assertNotNull(previous = pattern.getPrevious());
	assertNotNull(previous2 = previous.getPrevious());
	assertNull(previous2.getPrevious());

	assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, pattern.getQuantifier().getConsumingStrategy());
	assertNotNull(previous.getCondition());

	assertEquals(pattern.getName(), "end");
	assertEquals(previous.getName(), "subevent");
	assertEquals(previous2.getName(), "start");
}
 
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: PatternTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatternWithSubtypingAndFilter() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").next("subevent").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {
		private static final long serialVersionUID = -4118591291880230304L;

		@Override
		public boolean filter(SubEvent value) throws Exception {
			return false;
		}
	}).followedBy("end");

	Pattern<Event, ?> previous;
	Pattern<Event, ?> previous2;

	assertNotNull(previous = pattern.getPrevious());
	assertNotNull(previous2 = previous.getPrevious());
	assertNull(previous2.getPrevious());

	assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, pattern.getQuantifier().getConsumingStrategy());
	assertNotNull(previous.getCondition());

	assertEquals(pattern.getName(), "end");
	assertEquals(previous.getName(), "subevent");
	assertEquals(previous2.getName(), "start");
}
 
Example #4
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 #5
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 #6
Source File: PatternTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Test
public void testPatternWithSubtypingAndFilter() {
	Pattern<Event, Event> pattern = Pattern.<Event>begin("start").next("subevent").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {
		private static final long serialVersionUID = -4118591291880230304L;

		@Override
		public boolean filter(SubEvent value) throws Exception {
			return false;
		}
	}).followedBy("end");

	Pattern<Event, ?> previous;
	Pattern<Event, ?> previous2;

	assertNotNull(previous = pattern.getPrevious());
	assertNotNull(previous2 = previous.getPrevious());
	assertNull(previous2.getPrevious());

	assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, pattern.getQuantifier().getConsumingStrategy());
	assertNotNull(previous.getCondition());

	assertEquals(pattern.getName(), "end");
	assertEquals(previous.getName(), "subevent");
	assertEquals(previous2.getName(), "start");
}
 
Example #7
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 #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);

	comparePatterns(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
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 #10
Source File: PatternTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private SimpleCondition<Object> dummyCondition() {
	return new SimpleCondition<Object>() {
		private static final long serialVersionUID = -2205071036073867531L;

		@Override
		public boolean filter(Object value) throws Exception {
			return true;
		}
	};
}
 
Example #11
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithOneOrMoreStrict() throws Exception {

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

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #12
Source File: Sort.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
		env.setParallelism(1);

		DataStream<Event> eventStream = env.addSource(new OutOfOrderEventSource())
				.assignTimestampsAndWatermarks(new TimestampsAndWatermarks());

		Pattern<Event, ?> matchEverything =
				Pattern.<Event>begin("any")
						.where(new SimpleCondition<Event>() {
							@Override
							public boolean filter(Event event) throws Exception {
								return true;
							}
						});

		PatternStream<Event> patternStream = CEP.pattern(eventStream, matchEverything);
		OutputTag<Event> lateDataOutputTag = new OutputTag<Event>("late-events"){};

		SingleOutputStreamOperator<Event> sorted = patternStream
				.sideOutputLateData(lateDataOutputTag)
				.select(new PatternSelectFunction<Event, Event>() {
					@Override
					public Event select(Map<String, List<Event>> map) throws Exception {
						return map.get("any").get(0);
					}
				});

		sorted.print();
		sorted
				.getSideOutput(lateDataOutputTag)
				.map(e -> new Tuple2<>(e, "LATE"))
				.returns(Types.TUPLE(TypeInformation.of(Event.class), Types.STRING))
				.print();

		env.execute();
	}
 
Example #13
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithZeroOrMoreStrict() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
		private static final long serialVersionUID = 5726188262756267490L;

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #14
Source File: HighWindWarningPattern.java    From FlinkExperiments with MIT License 5 votes vote down vote up
@Override
public Pattern<LocalWeatherData, ?> getEventPattern() {
    return Pattern
            .<LocalWeatherData>begin("First Event").where(
                    new SimpleCondition<LocalWeatherData>() {
                        @Override
                        public boolean filter(LocalWeatherData event) throws Exception {
                            return event.getWindSpeed() >= 39 && event.getWindSpeed() <= 110;
                        }
                    });
}
 
Example #15
Source File: ExtremeWindWarningPattern.java    From FlinkExperiments with MIT License 5 votes vote down vote up
@Override
public Pattern<LocalWeatherData, ?> getEventPattern() {
    return Pattern
            .<LocalWeatherData>begin("First Event").where(
                    new SimpleCondition<LocalWeatherData>() {
                        @Override
                        public boolean filter(LocalWeatherData event) throws Exception {
                            return event.getWindSpeed() > 110;
                        }
                    });
}
 
Example #16
Source File: IndividualPatternQuantifier.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final ParameterTool parameterTool = ExecutionEnvUtil.createParameterTool(args);
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(parameterTool);
    env.setParallelism(1);

    DataStreamSource<String> data = env.socketTextStream("127.0.0.1", 9200);

    Pattern<String, String> pattern = Pattern.<String>begin("start")
            .where(new SimpleCondition<String>() {
                @Override
                public boolean filter(String s) throws Exception {
                    return "a".equals(s);
                }
            })
            .times(5).optional();

    CEP.pattern(data, pattern)
            .select(new PatternSelectFunction<String, String>() {
                @Override
                public String select(Map<String, List<String>> map) throws Exception {
                    log.info(map.toString());
                    return map.get("start").get(0);
                }
            }).print();
    env.execute("flink learning cep Individual Pattern Quantifier");
}
 
Example #17
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithOneOrMoreStrict() throws Exception {

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

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #18
Source File: NFAITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithZeroOrMoreStrict() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
		private static final long serialVersionUID = 5726188262756267490L;

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #19
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithZeroOrMoreStrict() throws Exception {
	Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
		private static final long serialVersionUID = 5726188262756267490L;

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #20
Source File: PatternTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private SimpleCondition<Object> dummyCondition() {
	return new SimpleCondition<Object>() {
		private static final long serialVersionUID = -2205071036073867531L;

		@Override
		public boolean filter(Object value) throws Exception {
			return true;
		}
	};
}
 
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-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 #23
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 #24
Source File: NFAITCase.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartWithOneOrMoreStrict() throws Exception {

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

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

	testStartWithOneOrZeroOrMoreStrict(pattern);
}
 
Example #25
Source File: IndividualPatternQuantifier.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final ParameterTool parameterTool = ExecutionEnvUtil.createParameterTool(args);
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().setGlobalJobParameters(parameterTool);
    env.setParallelism(1);

    DataStreamSource<String> data = env.socketTextStream("127.0.0.1", 9200);

    Pattern<String, String> pattern = Pattern.<String>begin("start")
            .where(new SimpleCondition<String>() {
                @Override
                public boolean filter(String s) throws Exception {
                    return "a".equals(s);
                }
            })
            .times(5).optional();

    CEP.pattern(data, pattern)
            .select(new PatternSelectFunction<String, String>() {
                @Override
                public String select(Map<String, List<String>> map) throws Exception {
                    log.info(map.toString());
                    return map.get("start").get(0);
                }
            }).print();
    env.execute("flink learning cep Individual Pattern Quantifier");
}
 
Example #26
Source File: PatternTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private SimpleCondition<Object> dummyCondition() {
	return new SimpleCondition<Object>() {
		private static final long serialVersionUID = -2205071036073867531L;

		@Override
		public boolean filter(Object value) throws Exception {
			return true;
		}
	};
}
 
Example #27
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 #28
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 #29
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 #30
Source File: NFAITCase.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartAndEndWithZeroOrMore() 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(43, "a", 4.0);
	Event end1 = new Event(44, "d", 5.0);
	Event end2 = new Event(45, "d", 5.0);
	Event end3 = new Event(46, "d", 5.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<>(end1, 6));
	inputEvents.add(new StreamRecord<>(end2, 6));
	inputEvents.add(new StreamRecord<>(end3, 6));

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

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

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

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

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