flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dianfu <...@git.apache.org>
Subject [GitHub] flink pull request #4153: [FLINK-6927] [cep] Support pattern group in CEP
Date Sun, 02 Jul 2017 08:01:09 GMT
Github user dianfu commented on a diff in the pull request:

    https://github.com/apache/flink/pull/4153#discussion_r125176801
  
    --- Diff: flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
---
    @@ -0,0 +1,807 @@
    +/*
    + * 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.nfa;
    +
    +import org.apache.flink.cep.Event;
    +import org.apache.flink.cep.nfa.compiler.NFACompiler;
    +import org.apache.flink.cep.pattern.GroupPattern;
    +import org.apache.flink.cep.pattern.Pattern;
    +import org.apache.flink.cep.pattern.conditions.SimpleCondition;
    +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
    +import org.apache.flink.util.TestLogger;
    +
    +import com.google.common.collect.Lists;
    +import org.junit.Test;
    +
    +import java.util.ArrayList;
    +import java.util.List;
    +
    +import static org.apache.flink.cep.nfa.NFATestUtilities.compareMaps;
    +import static org.apache.flink.cep.nfa.NFATestUtilities.feedNFA;
    +
    +/**
    + * IT tests covering {@link GroupPattern}.
    + */
    +@SuppressWarnings("unchecked")
    +public class GroupITCase extends TestLogger {
    +
    +	@Test
    +	public void testGroupFollowedBy() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event d = new Event(45, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(a2, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(d, 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("c");
    +			}
    +		}).followedBy(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).times(2).followedBy("end").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, a1, b1, a2, b2, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupFollowedByOptional() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event a2 = new Event(42, "a", 2.0);
    +		Event b = new Event(43, "b", 3.0);
    +		Event d = new Event(44, "d", 4.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(a2, 3));
    +		inputEvents.add(new StreamRecord<>(b, 4));
    +		inputEvents.add(new StreamRecord<>(d, 5));
    +
    +		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(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).optional().followedBy("end").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, d),
    +			Lists.newArrayList(c, a1, b, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testFollowedByGroupTimesOptional() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event d = new Event(45, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(d, 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("c");
    +			}
    +		}).followedBy(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).times(2).optional().followedBy("end").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupFollowedByOneOrMore() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event d = new Event(45, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(a2, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(d, 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("c");
    +			}
    +		}).followedBy(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).oneOrMore().followedBy("end").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, a1, b1, d),
    +			Lists.newArrayList(c, a1, b1, a2, b2, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupFollowedByZeroOrMore() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event d = new Event(45, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(a2, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(d, 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("c");
    +			}
    +		}).followedBy(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).oneOrMore().optional().followedBy("end").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, d),
    +			Lists.newArrayList(c, a1, b1, d),
    +			Lists.newArrayList(c, a1, b1, a2, b2, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupFollowedByAnyTimesCombinations() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event a3 = new Event(45, "a", 4.0);
    +		Event b3 = new Event(46, "b", 5.0);
    +		Event d = new Event(47, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(a2, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(a3, 6));
    +		inputEvents.add(new StreamRecord<>(b3, 7));
    +		inputEvents.add(new StreamRecord<>(d, 8));
    +
    +		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");
    +			}
    +		}).followedByAny(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).times(2).allowCombinations().followedBy("end").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, a1, b1, a2, b2, d),
    +			Lists.newArrayList(c, a1, b1, a3, b3, d),
    +			Lists.newArrayList(c, a2, b2, a3, b3, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupFollowedByAnyTimesOptional() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event a3 = new Event(45, "a", 4.0);
    +		Event b3 = new Event(46, "b", 5.0);
    +		Event d = new Event(47, "d", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(a2, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(a3, 6));
    +		inputEvents.add(new StreamRecord<>(b3, 7));
    +		inputEvents.add(new StreamRecord<>(d, 8));
    +
    +		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");
    +			}
    +		}).followedByAny(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).times(2).optional().followedBy("end").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, d),
    +			Lists.newArrayList(c, a1, b1, a2, b2, d),
    +			Lists.newArrayList(c, a2, b2, a3, b3, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupNextZeroOrMore() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event c = new Event(40, "c", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event a2 = new Event(43, "a", 3.0);
    +		Event b2 = new Event(44, "b", 3.0);
    +		Event a3 = new Event(45, "a", 4.0);
    +		Event b3 = new Event(46, "b", 3.0);
    +		Event d = new Event(47, "d", 1.0);
    +
    +		inputEvents.add(new StreamRecord<>(c, 1L));
    +		inputEvents.add(new StreamRecord<>(new Event(1, "event", 1.0), 2L));
    +		inputEvents.add(new StreamRecord<>(a1, 3L));
    +		inputEvents.add(new StreamRecord<>(b1, 4L));
    +		inputEvents.add(new StreamRecord<>(a2, 5L));
    +		inputEvents.add(new StreamRecord<>(b2, 6L));
    +		inputEvents.add(new StreamRecord<>(a3, 7L));
    +		inputEvents.add(new StreamRecord<>(b3, 8L));
    +		inputEvents.add(new StreamRecord<>(d, 9L));
    +
    +		Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 6215754202506583964L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("c");
    +			}
    +		}).next(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("a");
    +			}
    +		}).followedBy("middle2").where(new SimpleCondition<Event>() {
    +			private static final long serialVersionUID = 5726188262756267490L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("b");
    +			}
    +		})).oneOrMore().optional().consecutive().followedBy("end").where(new SimpleCondition<Event>()
{
    +			private static final long serialVersionUID = 7056763917392056548L;
    +
    +			@Override
    +			public boolean filter(Event value) throws Exception {
    +				return value.getName().equals("d");
    +			}
    +		});
    +
    +		NFA<Event> nfa = NFACompiler.compile(pattern, Event.createTypeSerializer(), false);
    +
    +		final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    +
    +		compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(
    +			Lists.newArrayList(c, d)
    +		));
    +	}
    +
    +	@Test
    +	public void testGroupNest() {
    +		List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    +
    +		Event d = new Event(40, "d", 1.0);
    +		Event a1 = new Event(41, "a", 2.0);
    +		Event b1 = new Event(42, "b", 3.0);
    +		Event c1 = new Event(43, "c", 4.0);
    +		Event b2 = new Event(44, "b", 5.0);
    +		Event c2 = new Event(45, "c", 4.0);
    +		Event e = new Event(46, "e", 6.0);
    +
    +		inputEvents.add(new StreamRecord<>(d, 1));
    +		inputEvents.add(new StreamRecord<>(a1, 2));
    +		inputEvents.add(new StreamRecord<>(b1, 3));
    +		inputEvents.add(new StreamRecord<>(c1, 4));
    +		inputEvents.add(new StreamRecord<>(b2, 5));
    +		inputEvents.add(new StreamRecord<>(c2, 6));
    +		inputEvents.add(new StreamRecord<>(e, 7));
    +
    --- End diff --
    
    make much sense. Updated.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message