Class Pattern<T,F extends T>
- java.lang.Object
-
- org.apache.flink.cep.pattern.Pattern<T,F>
-
- Type Parameters:
T
- Base type of the elements appearing in the patternF
- Subtype of T to which the current pattern operator is constrained
- Direct Known Subclasses:
GroupPattern
public class Pattern<T,F extends T> extends Object
Base class for a pattern definition.A pattern definition is used by
NFACompiler
to create aNFA
.Pattern<T, F> pattern = Pattern.<T>begin("start") .next("middle").subtype(F.class) .followedBy("end").where(new MyCondition());
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Pattern(String name, Pattern<T,? extends T> previous, Quantifier.ConsumingStrategy consumingStrategy, AfterMatchSkipStrategy afterMatchSkipStrategy)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Pattern<T,F>
allowCombinations()
Applicable only toQuantifier.looping(ConsumingStrategy)
andQuantifier.times(ConsumingStrategy)
patterns, this option allows more flexibility to the matching events.static <X> Pattern<X,X>
begin(String name)
Starts a new pattern sequence.static <X> Pattern<X,X>
begin(String name, AfterMatchSkipStrategy afterMatchSkipStrategy)
Starts a new pattern sequence.static <T,F extends T>
GroupPattern<T,F>begin(Pattern<T,F> group)
Starts a new pattern sequence.static <T,F extends T>
GroupPattern<T,F>begin(Pattern<T,F> group, AfterMatchSkipStrategy afterMatchSkipStrategy)
Starts a new pattern sequence.Pattern<T,F>
consecutive()
Works in conjunction withoneOrMore()
ortimes(int)
.Pattern<T,T>
followedBy(String name)
Appends a new pattern to the existing one.GroupPattern<T,F>
followedBy(Pattern<T,F> group)
Appends a new group pattern to the existing one.Pattern<T,T>
followedByAny(String name)
Appends a new pattern to the existing one.GroupPattern<T,F>
followedByAny(Pattern<T,F> group)
Appends a new group pattern to the existing one.AfterMatchSkipStrategy
getAfterMatchSkipStrategy()
IterativeCondition<F>
getCondition()
String
getName()
Pattern<T,? extends T>
getPrevious()
Quantifier
getQuantifier()
Quantifier.Times
getTimes()
IterativeCondition<F>
getUntilCondition()
Optional<Duration>
getWindowSize()
Optional<Duration>
getWindowSize(WithinType withinType)
Pattern<T,F>
greedy()
Specifies that this pattern is greedy.Pattern<T,T>
next(String name)
Appends a new pattern to the existing one.GroupPattern<T,F>
next(Pattern<T,F> group)
Appends a new group pattern to the existing one.Pattern<T,T>
notFollowedBy(String name)
Appends a new pattern to the existing one.Pattern<T,T>
notNext(String name)
Appends a new pattern to the existing one.Pattern<T,F>
oneOrMore()
Specifies that this pattern can occurone or more
times.Pattern<T,F>
oneOrMore(Duration windowTime)
Specifies that this pattern can occurone or more
times and time interval corresponds to the maximum time gap between previous and current event for each times.Pattern<T,F>
optional()
Specifies that this pattern is optional for a final match of the pattern sequence to happen.Pattern<T,F>
or(IterativeCondition<F> condition)
Adds a condition that has to be satisfied by an event in order to be considered a match.<S extends F>
Pattern<T,S>subtype(Class<S> subtypeClass)
Applies a subtype constraint on the current pattern.Pattern<T,F>
times(int times)
Specifies exact number of times that this pattern should be matched.Pattern<T,F>
times(int from, int to)
Specifies that the pattern can occur between from and to times.Pattern<T,F>
times(int from, int to, Duration windowTime)
Specifies that the pattern can occur between from and to times with time interval corresponds to the maximum time gap between previous and current event for each times.Pattern<T,F>
times(int times, Duration windowTime)
Specifies exact number of times that this pattern should be matched and time interval corresponds to the maximum time gap between previous and current event for each times.Pattern<T,F>
timesOrMore(int times)
Specifies that this pattern can occur the specified times at least.Pattern<T,F>
timesOrMore(int times, Duration windowTime)
Specifies that this pattern can occur the specified times at least with interval corresponds to the maximum time gap between previous and current event for each times.String
toString()
Pattern<T,F>
until(IterativeCondition<F> untilCondition)
Applies a stop condition for a looping state.Pattern<T,F>
where(IterativeCondition<F> condition)
Adds a condition that has to be satisfied by an event in order to be considered a match.Pattern<T,F>
within(Duration windowTime)
Defines the maximum time interval in which a matching pattern has to be completed in order to be considered valid.Pattern<T,F>
within(Duration windowTime, WithinType withinType)
Defines the maximum time interval in which a matching pattern has to be completed in order to be considered valid.
-
-
-
Constructor Detail
-
Pattern
protected Pattern(String name, Pattern<T,? extends T> previous, Quantifier.ConsumingStrategy consumingStrategy, AfterMatchSkipStrategy afterMatchSkipStrategy)
-
-
Method Detail
-
getTimes
public Quantifier.Times getTimes()
-
getName
public String getName()
-
getWindowSize
public Optional<Duration> getWindowSize(WithinType withinType)
-
getQuantifier
public Quantifier getQuantifier()
-
getCondition
public IterativeCondition<F> getCondition()
-
getUntilCondition
public IterativeCondition<F> getUntilCondition()
-
begin
public static <X> Pattern<X,X> begin(String name)
Starts a new pattern sequence. The provided name is the one of the initial pattern of the new sequence. Furthermore, the base type of the event sequence is set.- Type Parameters:
X
- Base type of the event pattern- Parameters:
name
- The name of starting pattern of the new pattern sequence- Returns:
- The first pattern of a pattern sequence
-
begin
public static <X> Pattern<X,X> begin(String name, AfterMatchSkipStrategy afterMatchSkipStrategy)
Starts a new pattern sequence. The provided name is the one of the initial pattern of the new sequence. Furthermore, the base type of the event sequence is set.- Type Parameters:
X
- Base type of the event pattern- Parameters:
name
- The name of starting pattern of the new pattern sequenceafterMatchSkipStrategy
- theAfterMatchSkipStrategy.SkipStrategy
to use after each match.- Returns:
- The first pattern of a pattern sequence
-
where
public Pattern<T,F> where(IterativeCondition<F> condition)
Adds a condition that has to be satisfied by an event in order to be considered a match. If another condition has already been set, the new one is going to be combined with the previous with a logicalAND
. In other case, this is going to be the only condition.- Parameters:
condition
- The condition as anIterativeCondition
.- Returns:
- The pattern with the new condition is set.
-
or
public Pattern<T,F> or(IterativeCondition<F> condition)
Adds a condition that has to be satisfied by an event in order to be considered a match. If another condition has already been set, the new one is going to be combined with the previous with a logicalOR
. In other case, this is going to be the only condition.- Parameters:
condition
- The condition as anIterativeCondition
.- Returns:
- The pattern with the new condition is set.
-
subtype
public <S extends F> Pattern<T,S> subtype(Class<S> subtypeClass)
Applies a subtype constraint on the current pattern. This means that an event has to be of the given subtype in order to be matched.- Type Parameters:
S
- Type of the subtype- Parameters:
subtypeClass
- Class of the subtype- Returns:
- The same pattern with the new subtype constraint
-
until
public Pattern<T,F> until(IterativeCondition<F> untilCondition)
Applies a stop condition for a looping state. It allows cleaning the underlying state.- Parameters:
untilCondition
- a condition an event has to satisfy to stop collecting events into looping state- Returns:
- The same pattern with applied untilCondition
-
within
public Pattern<T,F> within(@Nullable Duration windowTime)
Defines the maximum time interval in which a matching pattern has to be completed in order to be considered valid. This interval corresponds to the maximum time gap between first and the last event.- Parameters:
windowTime
- Time of the matching window- Returns:
- The same pattern operator with the new window length
-
within
public Pattern<T,F> within(@Nullable Duration windowTime, WithinType withinType)
Defines the maximum time interval in which a matching pattern has to be completed in order to be considered valid. This interval corresponds to the maximum time gap between events.- Parameters:
withinType
- Type of the within interval between eventswindowTime
- Time of the matching window- Returns:
- The same pattern operator with the new window length
-
next
public Pattern<T,T> next(String name)
Appends a new pattern to the existing one. The new pattern enforces strict temporal contiguity. This means that the whole pattern sequence matches only if an event which matches this pattern directly follows the preceding matching event. Thus, there cannot be any events in between two matching events.- Parameters:
name
- Name of the new pattern- Returns:
- A new pattern which is appended to this one
-
notNext
public Pattern<T,T> notNext(String name)
Appends a new pattern to the existing one. The new pattern enforces that there is no event matching this pattern right after the preceding matched event.- Parameters:
name
- Name of the new pattern- Returns:
- A new pattern which is appended to this one
-
followedBy
public Pattern<T,T> followedBy(String name)
Appends a new pattern to the existing one. The new pattern enforces non-strict temporal contiguity. This means that a matching event of this pattern and the preceding matching event might be interleaved with other events which are ignored.- Parameters:
name
- Name of the new pattern- Returns:
- A new pattern which is appended to this one
-
notFollowedBy
public Pattern<T,T> notFollowedBy(String name)
Appends a new pattern to the existing one. The new pattern enforces that there is no event matching this pattern between the preceding pattern and succeeding this one.NOTE: There has to be other pattern after this one.
- Parameters:
name
- Name of the new pattern- Returns:
- A new pattern which is appended to this one
-
followedByAny
public Pattern<T,T> followedByAny(String name)
Appends a new pattern to the existing one. The new pattern enforces non-strict temporal contiguity. This means that a matching event of this pattern and the preceding matching event might be interleaved with other events which are ignored.- Parameters:
name
- Name of the new pattern- Returns:
- A new pattern which is appended to this one
-
optional
public Pattern<T,F> optional()
Specifies that this pattern is optional for a final match of the pattern sequence to happen.- Returns:
- The same pattern as optional.
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
oneOrMore
public Pattern<T,F> oneOrMore()
Specifies that this pattern can occurone or more
times. This means at least one and at most infinite number of events can be matched to this pattern.If this quantifier is enabled for a pattern
A.oneOrMore().followedBy(B)
and a sequence of eventsA1 A2 B
appears, this will generate patterns:A1 B
andA1 A2 B
. See alsoallowCombinations()
.- Returns:
- The same pattern with a
Quantifier.looping(ConsumingStrategy)
quantifier applied. - Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
oneOrMore
public Pattern<T,F> oneOrMore(@Nullable Duration windowTime)
Specifies that this pattern can occurone or more
times and time interval corresponds to the maximum time gap between previous and current event for each times. This means at least one and at most infinite number of events can be matched to this pattern.If this quantifier is enabled for a pattern
A.oneOrMore().followedBy(B)
and a sequence of eventsA1 A2 B
appears, this will generate patterns:A1 B
andA1 A2 B
. See alsoallowCombinations()
.- Parameters:
windowTime
- time of the matching window between times- Returns:
- The same pattern with a
Quantifier.looping(ConsumingStrategy)
quantifier applied. - Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
greedy
public Pattern<T,F> greedy()
Specifies that this pattern is greedy. This means as many events as possible will be matched to this pattern.- Returns:
- The same pattern with
Quantifier.greedy()
set to true. - Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
times
public Pattern<T,F> times(int times)
Specifies exact number of times that this pattern should be matched.- Parameters:
times
- number of times matching event must appear- Returns:
- The same pattern with number of times applied
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
times
public Pattern<T,F> times(int times, @Nullable Duration windowTime)
Specifies exact number of times that this pattern should be matched and time interval corresponds to the maximum time gap between previous and current event for each times.- Parameters:
times
- number of times matching event must appearwindowTime
- time of the matching window between times- Returns:
- The same pattern with number of times applied
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
times
public Pattern<T,F> times(int from, int to)
Specifies that the pattern can occur between from and to times.- Parameters:
from
- number of times matching event must appear at leastto
- number of times matching event must appear at most- Returns:
- The same pattern with the number of times range applied
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
times
public Pattern<T,F> times(int from, int to, @Nullable Duration windowTime)
Specifies that the pattern can occur between from and to times with time interval corresponds to the maximum time gap between previous and current event for each times.- Parameters:
from
- number of times matching event must appear at leastto
- number of times matching event must appear at mostwindowTime
- time of the matching window between times- Returns:
- The same pattern with the number of times range applied
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
timesOrMore
public Pattern<T,F> timesOrMore(int times)
Specifies that this pattern can occur the specified times at least. This means at least the specified times and at most infinite number of events can be matched to this pattern.- Returns:
- The same pattern with a
Quantifier.looping(ConsumingStrategy)
quantifier applied. - Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
timesOrMore
public Pattern<T,F> timesOrMore(int times, @Nullable Duration windowTime)
Specifies that this pattern can occur the specified times at least with interval corresponds to the maximum time gap between previous and current event for each times. This means at least the specified times and at most infinite number of events can be matched to this pattern.- Parameters:
times
- number of times at least matching event must appearwindowTime
- time of the matching window between times- Returns:
- The same pattern with a
Quantifier.looping(ConsumingStrategy)
quantifier applied. - Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
allowCombinations
public Pattern<T,F> allowCombinations()
Applicable only toQuantifier.looping(ConsumingStrategy)
andQuantifier.times(ConsumingStrategy)
patterns, this option allows more flexibility to the matching events.If
allowCombinations()
is not applied for a patternA.oneOrMore().followedBy(B)
and a sequence of eventsA1 A2 B
appears, this will generate patterns:A1 B
andA1 A2 B
. If this method is applied, we will haveA1 B
,A2 B
andA1 A2 B
.- Returns:
- The same pattern with the updated quantifier. *
- Throws:
MalformedPatternException
- if the quantifier is not applicable to this pattern.
-
consecutive
public Pattern<T,F> consecutive()
Works in conjunction withoneOrMore()
ortimes(int)
. Specifies that any not matching element breaks the loop.E.g. a pattern like:
{@code Pattern.
begin("start").where(new SimpleCondition () { - Returns:
- pattern with continuity changed to strict
-
begin
public static <T,F extends T> GroupPattern<T,F> begin(Pattern<T,F> group, AfterMatchSkipStrategy afterMatchSkipStrategy)
Starts a new pattern sequence. The provided pattern is the initial pattern of the new sequence.- Parameters:
group
- the pattern to begin withafterMatchSkipStrategy
- theAfterMatchSkipStrategy.SkipStrategy
to use after each match.- Returns:
- The first pattern of a pattern sequence
-
begin
public static <T,F extends T> GroupPattern<T,F> begin(Pattern<T,F> group)
Starts a new pattern sequence. The provided pattern is the initial pattern of the new sequence.- Parameters:
group
- the pattern to begin with- Returns:
- the first pattern of a pattern sequence
-
followedBy
public GroupPattern<T,F> followedBy(Pattern<T,F> group)
Appends a new group pattern to the existing one. The new pattern enforces non-strict temporal contiguity. This means that a matching event of this pattern and the preceding matching event might be interleaved with other events which are ignored.- Parameters:
group
- the pattern to append- Returns:
- A new pattern which is appended to this one
-
followedByAny
public GroupPattern<T,F> followedByAny(Pattern<T,F> group)
Appends a new group pattern to the existing one. The new pattern enforces non-strict temporal contiguity. This means that a matching event of this pattern and the preceding matching event might be interleaved with other events which are ignored.- Parameters:
group
- the pattern to append- Returns:
- A new pattern which is appended to this one
-
next
public GroupPattern<T,F> next(Pattern<T,F> group)
Appends a new group pattern to the existing one. The new pattern enforces strict temporal contiguity. This means that the whole pattern sequence matches only if an event which matches this pattern directly follows the preceding matching event. Thus, there cannot be any events in between two matching events.- Parameters:
group
- the pattern to append- Returns:
- A new pattern which is appended to this one
-
getAfterMatchSkipStrategy
public AfterMatchSkipStrategy getAfterMatchSkipStrategy()
- Returns:
- the pattern's
AfterMatchSkipStrategy.SkipStrategy
after match.
-
-