[Mulgara-svn] r831 - branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter

pag at mulgara.org pag at mulgara.org
Thu Apr 24 03:03:16 UTC 2008


Author: pag
Date: 2008-04-23 20:03:16 -0700 (Wed, 23 Apr 2008)
New Revision: 831

Added:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/NAryOperatorFilter.java
Removed:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryOperatorFilter.java
Modified:
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/And.java
   branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Or.java
Log:
Refactored And, Or, and their abstract superclass BinaryOperator to handle a list of operands instead of just two. This is needed since SPARQL builds flat lists of these operations, despite only defining the operators as binary.

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/And.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/And.java	2008-04-24 03:01:16 UTC (rev 830)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/And.java	2008-04-24 03:03:16 UTC (rev 831)
@@ -11,6 +11,8 @@
  */
 package org.mulgara.query.filter;
 
+import java.util.List;
+
 import org.mulgara.query.QueryException;
 
 
@@ -22,32 +24,35 @@
  * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
  * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
  */
-public class And extends BinaryOperatorFilter {
+public class And extends NAryOperatorFilter {
 
   /**
-   * Create a product of two other filters.
-   * @param lhs The first operands of the product
-   * @param rhs The first operands of the product
+   * Create a product of other filters.
+   * @param operands The operands of the product
    */
-  public And(Filter lhs, Filter rhs) {
-    super(lhs, rhs);
+  public And(Filter... operands) {
+    super(operands);
   }
 
   /**
+   * Returns the head of the list ANDed with the product of the remainder of the list.
    * <code>false</code> and Exception is <code>false</code>.
    * <code>true</code> and Exception is Exception.
    * @see org.mulgara.query.filter.Filter#test()
    */
-  boolean testOp(Context context) throws QueryException {
+  boolean testList(Context context, List<Filter> filters) throws QueryException {
+    Filter head = filters.get(0);
+    if (filters.size() == 1) return head.test(context);
+
     boolean result = false;
     try {
-      result = lhs.test(context);
+      result = head.test(context);
     } catch (QueryException e) {
-      // false on the RHS gives false
-      if (!rhs.test(context)) return false;
+      // false on the remainder gives false
+      if (!testList(context, tail(filters))) return false;
       throw e;
     }
-    return result && rhs.test(context);
+    return result && testList(context, tail(filters));
   }
 
 }

Deleted: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryOperatorFilter.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryOperatorFilter.java	2008-04-24 03:01:16 UTC (rev 830)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryOperatorFilter.java	2008-04-24 03:03:16 UTC (rev 831)
@@ -1,58 +0,0 @@
-/**
- * The contents of this file are subject to the Open Software License
- * Version 3.0 (the "License"); you may not use this file except in
- * compliance with the License. You may obtain a copy of the License at
- * http://www.opensource.org/licenses/osl-3.0.txt
- *
- * Software distributed under the License is distributed on an "AS IS"
- * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
- * the License for the specific language governing rights and limitations
- * under the License.
- */
-package org.mulgara.query.filter;
-
-import org.mulgara.query.QueryException;
-import org.mulgara.query.filter.value.Bool;
-import org.mulgara.query.filter.value.ValueLiteral;
-
-
-/**
- * Represents a filter that takes other filters as parameters.
- *
- * @created Mar 7, 2008
- * @author Paul Gearon
- * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
- * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
- */
-public abstract class BinaryOperatorFilter extends AbstractFilterValue implements ValueLiteral {
-
-  /** The first operand */
-  protected Filter lhs;
-
-  /** The second operand */
-  protected Filter rhs;
-
-  /**
-   * Creates a binary operation
-   * @param lhs The left filter expression
-   * @param rhs The right filter expression
-   */
-  BinaryOperatorFilter(Filter lhs, Filter rhs) {
-    super(lhs, rhs);
-    this.lhs = lhs;
-    this.rhs = rhs;
-  }
-
-  public RDFTerm resolve() throws QueryException {
-    return testOp(getCurrentContext()) ? Bool.TRUE : Bool.FALSE;
-  }
-
-  /**
-   * An operation specific test.
-   * @param context The context to resolve the test in.
-   * @return <code>true</code> if the operation passes.
-   * @throws QueryException If there was an <em>unrecoverable</em> error resolving the operands 
-   */
-  abstract boolean testOp(Context context) throws QueryException;
-
-}

Copied: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/NAryOperatorFilter.java (from rev 788, branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/BinaryOperatorFilter.java)
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/NAryOperatorFilter.java	                        (rev 0)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/NAryOperatorFilter.java	2008-04-24 03:03:16 UTC (rev 831)
@@ -0,0 +1,75 @@
+/**
+ * The contents of this file are subject to the Open Software License
+ * Version 3.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ * http://www.opensource.org/licenses/osl-3.0.txt
+ *
+ * Software distributed under the License is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+ * the License for the specific language governing rights and limitations
+ * under the License.
+ */
+package org.mulgara.query.filter;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.mulgara.query.QueryException;
+import org.mulgara.query.filter.value.Bool;
+import org.mulgara.query.filter.value.ValueLiteral;
+
+
+/**
+ * Represents a filter that takes other filters as parameters.
+ *
+ * @created Mar 7, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+public abstract class NAryOperatorFilter extends AbstractFilterValue implements ValueLiteral {
+
+  /**
+   * Creates a multiple operand operation
+   * @param operands The operands for this filter operation.
+   */
+  NAryOperatorFilter(Filter... operands) {
+    super(operands);
+  }
+
+  public RDFTerm resolve() throws QueryException {
+    return testOp(getCurrentContext()) ? Bool.TRUE : Bool.FALSE;
+  }
+
+  /**
+   * Runs the test on all the operands.
+   * @param context The context to resolve the test in.
+   * @return <code>true</code> if the operation passes.
+   * @throws QueryException If there was an <em>unrecoverable</em> error resolving the operands 
+   */
+  boolean testOp(Context context) throws QueryException {
+    // we can cast operands, since it was provided as a Filter[]
+    return testList(context, Arrays.asList((Filter[])operands));
+  }
+
+  /**
+   * An operation specific test. This will apply the test to all operands given in the list,
+   * short circuiting where possible, in order to provide a correct result even in the event
+   * of an exception.
+   * @param context The context to resolve the list in.
+   * @param filters The operands of the operation. These are resolved in the provided context.
+   * @return A boolean value, according to the specifics of the test.
+   * @throws QueryException If there was an <em>unrecoverable</em> error resolving the operands.
+   */
+  abstract boolean testList(Context context, List<Filter> filters) throws QueryException;
+
+  /**
+   * Utility to return the tail of a list. This is the entire list, minus the first entry.
+   * @param l The list to get the tail of.
+   * @return A new list, with a size equal to l.size()-1. ie. all of l except l.get(0)
+   */
+  static List<Filter> tail(List<Filter> l) {
+    return l.subList(1, l.size());
+  }
+
+}

Modified: branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Or.java
===================================================================
--- branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Or.java	2008-04-24 03:01:16 UTC (rev 830)
+++ branches/mgr-61-sparql/src/jar/query/java/org/mulgara/query/filter/Or.java	2008-04-24 03:03:16 UTC (rev 831)
@@ -11,6 +11,8 @@
  */
 package org.mulgara.query.filter;
 
+import java.util.List;
+
 import org.mulgara.query.QueryException;
 
 
@@ -22,32 +24,35 @@
  * @copyright &copy; 2008 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
  * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
  */
-public class Or extends BinaryOperatorFilter {
+public class Or extends NAryOperatorFilter {
 
   /**
-   * Create a sum of two other filters.
-   * @param lhs The first operands of the sum
-   * @param rhs The first operands of the sum
+   * Create a sum of other filters.
+   * @param operands The operands of the sum.
    */
-  public Or(Filter lhs, Filter rhs) {
-    super(lhs, rhs);
+  public Or(Filter... operands) {
+    super(operands);
   }
 
   /**
+   * Returns the head of the list ORed with the sum of the remainder of the list.
    * <code>true</code> or Exception is <code>true</code>.
    * <code>false</code> or Exception is Exception.
    * @see org.mulgara.query.filter.Filter#test()
    */
-  boolean testOp(Context context) throws QueryException {
+  boolean testList(Context context, List<Filter> filters) throws QueryException {
+    Filter head = filters.get(0);
+    if (filters.size() == 1) return head.test(context);
+
     boolean result;
     try {
-      result = lhs.test(context);
+      result = filters.get(0).test(context);
     } catch (QueryException e) {
       // true on the RHS gives true
-      if (!rhs.test(context)) return true;
+      if (!testList(context, tail(filters))) return true;
       throw e;
     }
-    return result || rhs.test(context);
+    return result || testList(context, tail(filters));
   }
 
 }




More information about the Mulgara-svn mailing list