You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ma...@apache.org on 2023/02/14 13:09:14 UTC

[flink] branch release-1.15 updated: Revert "[FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern"

This is an automated email from the ASF dual-hosted git repository.

mapohl pushed a commit to branch release-1.15
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/release-1.15 by this push:
     new 735609c4426 Revert "[FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern"
735609c4426 is described below

commit 735609c44261d631527e492dfdfa68f0dcbd51b0
Author: Matthias Pohl <ma...@aiven.io>
AuthorDate: Tue Feb 14 14:06:38 2023 +0100

    Revert "[FLINK-30864][FLINK-30885][CEP] Fix wrong NFA related to optional decoration at the beginning of group pattern"
    
    This reverts commit 1ac1e0d7dd078a85cb0f127aa75e161e03b2fd41.
---
 .../apache/flink/cep/nfa/compiler/NFACompiler.java | 29 ++++----
 .../java/org/apache/flink/cep/nfa/GroupITCase.java | 80 ----------------------
 2 files changed, 16 insertions(+), 93 deletions(-)

diff --git a/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java b/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
index 38211dce865..0e9ed184557 100644
--- a/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
+++ b/flink-libraries/flink-cep/src/main/java/org/apache/flink/cep/nfa/compiler/NFACompiler.java
@@ -553,7 +553,9 @@ public class NFACompiler {
                     proceedState,
                     takeCondition,
                     getIgnoreCondition(currentPattern),
-                    isPatternOptional(currentPattern));
+                    currentPattern
+                            .getQuantifier()
+                            .hasProperty(Quantifier.QuantifierProperty.OPTIONAL));
         }
 
         /**
@@ -597,17 +599,14 @@ public class NFACompiler {
          * pattern, the optional status depends on the group pattern.
          */
         private boolean isPatternOptional(Pattern<T, ?> pattern) {
-            return pattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
-        }
-
-        private boolean isHeadOfOptionalGroupPattern(Pattern<T, ?> pattern) {
-            if (!headOfGroup(pattern)) {
-                return false;
+            if (headOfGroup(pattern)) {
+                return isCurrentGroupPatternFirstOfLoop()
+                        && currentGroupPattern
+                                .getQuantifier()
+                                .hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
+            } else {
+                return pattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
             }
-            return isCurrentGroupPatternFirstOfLoop()
-                    && currentGroupPattern
-                            .getQuantifier()
-                            .hasProperty(Quantifier.QuantifierProperty.OPTIONAL);
         }
 
         /**
@@ -661,7 +660,11 @@ public class NFACompiler {
             // if no element accepted the previous nots are still valid.
             final IterativeCondition<T> proceedCondition = getTrueFunction();
 
-            if (isOptional) {
+            // for the first state of a group pattern, its PROCEED edge should point to the
+            // following state of
+            // that group pattern and the edge will be added at the end of creating the NFA for that
+            // group pattern
+            if (isOptional && !headOfGroup(currentPattern)) {
                 if (currentPattern
                         .getQuantifier()
                         .hasProperty(Quantifier.QuantifierProperty.GREEDY)) {
@@ -686,7 +689,7 @@ public class NFACompiler {
 
             if (ignoreCondition != null) {
                 final State<T> ignoreState;
-                if (isOptional || isHeadOfOptionalGroupPattern(currentPattern)) {
+                if (isOptional) {
                     ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
                     ignoreState.addTake(sink, takeCondition);
                     ignoreState.addIgnore(ignoreCondition);
diff --git a/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java b/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
index c2086b7864a..bc76acb5757 100644
--- a/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
+++ b/flink-libraries/flink-cep/src/test/java/org/apache/flink/cep/nfa/GroupITCase.java
@@ -1470,84 +1470,4 @@ public class GroupITCase extends TestLogger {
         assertEquals(1, nfaState.getPartialMatches().size());
         assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
     }
-
-    @Test
-    public void testGroupStartsWithOptionalPattern() throws Exception {
-        List<StreamRecord<Event>> inputEvents = new ArrayList<>();
-
-        Event a = new Event(40, "a", 1.0);
-        Event c = new Event(41, "c", 2.0);
-        Event d = new Event(42, "d", 3.0);
-
-        inputEvents.add(new StreamRecord<>(a, 1));
-        inputEvents.add(new StreamRecord<>(c, 2));
-        inputEvents.add(new StreamRecord<>(d, 3));
-
-        // a (b? c) d
-        Pattern<Event, ?> pattern =
-                Pattern.<Event>begin("start")
-                        .where(SimpleCondition.of(value -> value.getName().equals("a")))
-                        .next(
-                                Pattern.<Event>begin("middle1")
-                                        .where(
-                                                SimpleCondition.of(
-                                                        value -> value.getName().equals("b")))
-                                        .optional()
-                                        .next("middle2")
-                                        .where(
-                                                SimpleCondition.of(
-                                                        value -> value.getName().equals("c"))))
-                        .next("d")
-                        .where(SimpleCondition.of(value -> value.getName().equals("d")));
-
-        NFA<Event> nfa = compile(pattern, false);
-
-        NFAState nfaState = nfa.createInitialNFAState();
-
-        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
-        final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
-
-        comparePatterns(
-                resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a, c, d)));
-    }
-
-    @Test
-    public void testFollowedByOptionalGroupPattern() throws Exception {
-        List<StreamRecord<Event>> inputEvents = new ArrayList<>();
-
-        Event a = new Event(40, "a", 1.0);
-        Event d1 = new Event(41, "d", 2.0);
-        Event d2 = new Event(42, "d", 3.0);
-
-        inputEvents.add(new StreamRecord<>(a, 1));
-        inputEvents.add(new StreamRecord<>(d1, 2));
-        inputEvents.add(new StreamRecord<>(d2, 3));
-
-        // a -> (b c)? d
-        Pattern<Event, ?> pattern =
-                Pattern.<Event>begin("start")
-                        .where(SimpleCondition.of(value -> value.getName().equals("a")))
-                        .followedBy(
-                                Pattern.<Event>begin("middle1")
-                                        .where(
-                                                SimpleCondition.of(
-                                                        value -> value.getName().equals("b")))
-                                        .next("middle2")
-                                        .where(
-                                                SimpleCondition.of(
-                                                        value -> value.getName().equals("c"))))
-                        .optional()
-                        .next("d")
-                        .where(SimpleCondition.of(value -> value.getName().equals("d")));
-
-        NFA<Event> nfa = compile(pattern, false);
-
-        NFAState nfaState = nfa.createInitialNFAState();
-
-        NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
-        final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
-
-        comparePatterns(
-                resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(a, d1)));
-    }
 }