Uploaded image for project: 'Flink'
  1. Flink
  2. FLINK-8914

CEP's greedy() modifier doesn't work

Agile BoardRank to TopRank to BottomAttach filesAttach ScreenshotBulk Copy AttachmentsBulk Move AttachmentsVotersStop watchingWatchersCreate sub-taskConvert to sub-taskLinkCloneLabelsUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not A Problem
    • 1.4.0, 1.4.1
    • None
    • Library / CEP

    Description

      When applied to the first or last component of a CEP Pattern, greedy() doesn't work correctly. Here's an example:

      package com.dataartisans.flinktraining.exercises.datastream_java.cep;
      
      import org.apache.flink.cep.CEP;
      import org.apache.flink.cep.PatternSelectFunction;
      import org.apache.flink.cep.PatternStream;
      import org.apache.flink.cep.pattern.Pattern;
      import org.apache.flink.cep.pattern.conditions.SimpleCondition;
      import org.apache.flink.streaming.api.datastream.DataStream;
      import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
      
      import java.util.List;
      import java.util.Map;
      
      public class RunLength {
      
        public static void main(String[] args) throws Exception {
      
          StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
          env.setParallelism(1);
      
          DataStream<Integer> input = env.fromElements(1, 1, 1, 1, 1, 0, 1, 1, 1, 0);
      
          Pattern<Integer, ?> onesThenZero = Pattern.<Integer>begin("ones")
            .where(new SimpleCondition<Integer>() {
              @Override
              public boolean filter(Integer value) throws Exception {
                return value == 1;
              }
            })
            .oneOrMore()
            .greedy()
            .consecutive()
            .next("zero")
            .where(new SimpleCondition<Integer>() {
              @Override
              public boolean filter(Integer value) throws Exception {
                return value == 0;
              }
            });
      
            PatternStream<Integer> patternStream = CEP.pattern(input, onesThenZero);
      
            // Expected: 5 3
            // Actual: 5 4 3 2 1 3 2 1
            patternStream.select(new LengthOfRun()).print();
      
            env.execute();
          }
      
          public static class LengthOfRun implements PatternSelectFunction<Integer, Integer> {
            public Integer select(Map<String, List<Integer>> pattern) {
            return pattern.get("ones").size();
          }
        }
      }
      

      The only workaround for now seems to be to rewrite the pattern so that greedy() isn't needed – i.e. by bracketing the greedy section with a prefix and suffix that both have to be matched.

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            aitozi WenJun Min
            alpinegizmo David Anderson
            Votes:
            1 Vote for this issue
            Watchers:
            7 Stop watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment