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

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 3246cf667d8d736d9333b3d3849c0508335e9329
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Sun Oct 24 21:52:07 2021 +0200

    Add an optimization:
    - A AND NOT(A) = false
    - A OR  NOT(A) = true
---
 .../main/java/org/apache/sis/filter/LogicalFilter.java    | 15 ++++++++++++++-
 .../src/main/java/org/apache/sis/filter/package-info.java |  2 +-
 .../java/org/apache/sis/filter/LogicalFilterTest.java     |  9 ++++++---
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/filter/LogicalFilter.java 
b/core/sis-feature/src/main/java/org/apache/sis/filter/LogicalFilter.java
index fe849e4..552e311 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/LogicalFilter.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/LogicalFilter.java
@@ -37,7 +37,7 @@ import org.opengis.filter.LogicalOperatorName;
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.2
  *
  * @param  <R>  the type of resources (e.g. {@link 
org.opengis.feature.Feature}) used as inputs.
  *
@@ -304,6 +304,19 @@ abstract class LogicalFilter<R> extends FilterNode<R> 
implements LogicalOperator
             }
         }
         /*
+         * Simplification after we finished to inline nested logical operators:
+         *
+         *     A AND NOT(A) = FALSE
+         *     A OR  NOT(A) = TRUE
+         */
+        for (Filter<? super R> f : effective) {
+            if (LogicalOperatorName.NOT.equals(f.getOperatorType())) {
+                if (effective.containsAll(((LogicalOperator<?>) 
f).getOperands())) {
+                    return shortCircuit;
+                }
+            }
+        }
+        /*
          * TODO:
          * - Replace BETWEEN(A,B,C) by A >= B and A <= C in order to allow 
optimizations below.
          * - Replace A >= B && A >= C by A >= MAX(B,C) with max evaluated 
immediately if possible.
diff --git 
a/core/sis-feature/src/main/java/org/apache/sis/filter/package-info.java 
b/core/sis-feature/src/main/java/org/apache/sis/filter/package-info.java
index ccccc79..10d45ca 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/package-info.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/package-info.java
@@ -57,7 +57,7 @@
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.2
  *
  * @since 1.1
  * @module
diff --git 
a/core/sis-feature/src/test/java/org/apache/sis/filter/LogicalFilterTest.java 
b/core/sis-feature/src/test/java/org/apache/sis/filter/LogicalFilterTest.java
index 073c715..a600504 100644
--- 
a/core/sis-feature/src/test/java/org/apache/sis/filter/LogicalFilterTest.java
+++ 
b/core/sis-feature/src/test/java/org/apache/sis/filter/LogicalFilterTest.java
@@ -42,7 +42,7 @@ import org.opengis.filter.LogicalOperator;
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.2
  * @since   1.1
  * @module
  */
@@ -182,10 +182,13 @@ public final strictfp class LogicalFilterTest extends 
TestCase {
      */
     @Test
     public void testOptimization() {
-        final Filter<Feature> f1 = factory.isNull(factory.literal("text"));
-        final Filter<Feature> f2 = factory.isNull(factory.literal(null));
+        final Filter<Feature> f1 = factory.isNull(factory.literal("text"));    
 // False
+        final Filter<Feature> f2 = factory.isNull(factory.literal(null));      
 // True
+        final Filter<Feature> f3 = factory.isNull(factory.property("*"));      
 // Indeterminate
         optimize(factory.and(f1, f2), Filter.exclude());
         optimize(factory.or (f1, f2), Filter.include());
+        optimize(factory.and(f3, factory.not(f3)), Filter.exclude());
+        optimize(factory.or (f3, factory.not(f3)), Filter.include());
     }
 
     /**

Reply via email to