Github user dawidwys commented on a diff in the pull request: https://github.com/apache/flink/pull/4331#discussion_r132175300 --- Diff: flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/AfterMatchSkipITCase.java --- @@ -0,0 +1,431 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.cep; + +import org.apache.flink.cep.nfa.AfterMatchSkipStrategy; +import org.apache.flink.cep.pattern.MalformedPatternException; +import org.apache.flink.cep.pattern.Pattern; +import org.apache.flink.cep.pattern.conditions.SimpleCondition; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.util.StreamingMultipleProgramsTestBase; + +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.util.List; +import java.util.Map; + +/** + * After match skip tests. + */ +public class AfterMatchSkipITCase extends StreamingMultipleProgramsTestBase { + + private String resultPath; + private String expected; + + private String lateEventPath; + private String expectedLateEvents; + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Before + public void before() throws Exception { + resultPath = tempFolder.newFile().toURI().toString(); + expected = ""; + + lateEventPath = tempFolder.newFile().toURI().toString(); + expectedLateEvents = ""; + } + + @After + public void after() throws Exception { + compareResultsByLinesInMemory(expected, resultPath); + compareResultsByLinesInMemory(expectedLateEvents, lateEventPath); + } + + private PatternSelectFunction<Event, String> newIdSelectFunction(String ... names) { + return new PatternSelectFunction<Event, String>() { + + @Override + public String select(Map<String, List<Event>> pattern) { + StringBuilder builder = new StringBuilder(); + for (String name: names) { + for (Event e : pattern.get(name)) { + builder.append(e.getId()).append(","); + } + } + return builder.toString(); + } + }; + } + + @Test + public void testSkipToNext() throws Exception { + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream<Event> input = env.fromElements( + new Event(1, "a", 0.0), + new Event(2, "a", 0.0), + new Event(3, "a", 0.0), + new Event(4, "a", 0.0), + new Event(5, "a", 0.0), + new Event(6, "a", 0.0) + ); + Pattern<Event, ?> pattern = Pattern.<Event>begin("start", + new AfterMatchSkipStrategy(AfterMatchSkipStrategy.SkipStrategy.SKIP_TO_NEXT_EVENT)) + .where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().equals("a"); + } + }).times(3); + DataStream<String> result = CEP.pattern(input, pattern).select(newIdSelectFunction("start")); + + result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); + + // expected sequence of matching event ids + expected = "1,2,3,\n2,3,4,\n3,4,5,\n4,5,6,"; + + env.execute(); + } + + @Test + public void testSkipPastLast() throws Exception { + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream<Event> input = env.fromElements( + new Event(1, "a", 0.0), + new Event(2, "a", 0.0), + new Event(3, "a", 0.0), + new Event(4, "a", 0.0), + new Event(5, "a", 0.0), + new Event(6, "a", 0.0) + ); + Pattern<Event, ?> pattern = Pattern.<Event>begin("start", + new AfterMatchSkipStrategy(AfterMatchSkipStrategy.SkipStrategy.SKIP_PAST_LAST_EVENT)).where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().equals("a"); + } + }).times(3); + + DataStream<String> result = CEP.pattern(input, pattern).select(newIdSelectFunction("start")); + + result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); + + // expected sequence of matching event ids + expected = "1,2,3,\n4,5,6,"; + + env.execute(); + } + + @Test + public void testSkipToFirst() throws Exception { + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream<Event> input = env.fromElements( + new Event(1, "ab", 0.0), + new Event(2, "ab", 0.0), + new Event(3, "ab", 0.0), + new Event(4, "ab", 0.0), + new Event(5, "ab", 0.0), + new Event(6, "ab", 0.0), + new Event(7, "ab", 0.0) + ); + Pattern<Event, ?> pattern = Pattern.<Event>begin("start", + new AfterMatchSkipStrategy( + AfterMatchSkipStrategy.SkipStrategy.SKIP_TO_FIRST, "end")) + .where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("a"); + } + }).times(2).next("end").where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("b"); + } + }).times(2); + + DataStream<String> result = CEP.pattern(input, pattern).select(newIdSelectFunction("start", "end")); + + result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); + + // expected sequence of matching event ids + expected = "1,2,3,4,\n3,4,5,6,"; + + env.execute(); + } + + @Test + public void testSkipToLast() throws Exception { + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream<Event> input = env.fromElements( + new Event(1, "ab", 0.0), + new Event(2, "ab", 0.0), + new Event(3, "ab", 0.0), + new Event(4, "ab", 0.0), + new Event(5, "ab", 0.0), + new Event(6, "ab", 0.0), + new Event(7, "ab", 0.0) + ); + Pattern<Event, ?> pattern = Pattern.<Event>begin("start", new AfterMatchSkipStrategy( + AfterMatchSkipStrategy.SkipStrategy.SKIP_TO_LAST, "end")).where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("a"); + } + }).times(2).next("end").where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("b"); + } + }).times(2); + DataStream<String> result = CEP.pattern(input, pattern).select(newIdSelectFunction("start", "end")); + + result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); + + // expected sequence of matching event ids + expected = "1,2,3,4,\n4,5,6,7,"; + + env.execute(); + } + + @Test(expected = MalformedPatternException.class) + public void testSkipToLastWithEmptyException() throws Exception { + StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); + + DataStream<Event> input = env.fromElements( + new Event(1, "ab", 0.0), + new Event(2, "c", 0.0), + new Event(3, "ab", 0.0), + new Event(4, "c", 0.0) + ); + Pattern<Event, ?> pattern = Pattern.<Event>begin("start", new AfterMatchSkipStrategy( + AfterMatchSkipStrategy.SkipStrategy.SKIP_TO_LAST, "middle")).where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("a"); + } + }).next("middle").where( + new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("d"); + } + } + ).oneOrMore().optional().next("end").where(new SimpleCondition<Event>() { + + @Override + public boolean filter(Event value) throws Exception { + return value.getName().contains("c"); + } + }); + DataStream<String> result = CEP.pattern(input, pattern).select(newIdSelectFunction("start", "end")); + + result.writeAsText(resultPath, FileSystem.WriteMode.OVERWRITE); + + env.execute(); + } + + @Test(expected = MalformedPatternException.class) --- End diff -- same as above
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---