Class Pattern<T,​F extends T>

  • Type Parameters:
    T - Base type of the elements appearing in the pattern
    F - 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 a NFA.

    
     Pattern<T, F> pattern = Pattern.<T>begin("start")
       .next("middle").subtype(F.class)
       .followedBy("end").where(new MyCondition());
     
    • Method Detail

      • getPrevious

        public Pattern<T,​? extends T> getPrevious()
      • getName

        public String getName()
      • getQuantifier

        public Quantifier getQuantifier()
      • 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 sequence
        afterMatchSkipStrategy - the AfterMatchSkipStrategy.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 logical AND. In other case, this is going to be the only condition.
        Parameters:
        condition - The condition as an IterativeCondition.
        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 logical OR. In other case, this is going to be the only condition.
        Parameters:
        condition - The condition as an IterativeCondition.
        Returns:
        The pattern with the new condition is set.
      • subtype

        public <S extends FPattern<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 events
        windowTime - 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 occur one 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 events A1 A2 B appears, this will generate patterns: A1 B and A1 A2 B. See also allowCombinations().

        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 occur one 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 events A1 A2 B appears, this will generate patterns: A1 B and A1 A2 B. See also allowCombinations().

        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 appear
        windowTime - 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 least
        to - 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 least
        to - number of times matching event must appear at most
        windowTime - 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 appear
        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.
      • allowCombinations

        public Pattern<T,​F> allowCombinations()
        Applicable only to Quantifier.looping(ConsumingStrategy) and Quantifier.times(ConsumingStrategy) patterns, this option allows more flexibility to the matching events.

        If allowCombinations() is not applied for a pattern A.oneOrMore().followedBy(B) and a sequence of events A1 A2 B appears, this will generate patterns: A1 B and A1 A2 B. If this method is applied, we will have A1 B, A2 B and A1 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 with oneOrMore() or times(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 with
        afterMatchSkipStrategy - the AfterMatchSkipStrategy.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.