[Mulgara-svn] r907 - in trunk/src/jar: resolver/java/org/mulgara/resolver tuples/java/org/mulgara/store/tuples

pag at mulgara.org pag at mulgara.org
Wed May 7 14:30:51 UTC 2008


Author: pag
Date: 2008-05-07 07:30:50 -0700 (Wed, 07 May 2008)
New Revision: 907

Modified:
   trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java
   trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoinUnitTest.java
   trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
Log:
Using the new ConstraintOptionalJoin/LeftJoin code which includes filters

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java	2008-05-07 14:29:35 UTC (rev 906)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java	2008-05-07 14:30:50 UTC (rev 907)
@@ -183,14 +183,12 @@
         new NVPair(ConstraintOptionalJoin.class, new ConstraintResolutionHandler() {
           public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
             List<Tuples> args = context.resolveConstraintOperation(modelExpr, (ConstraintOperation)constraintExpr);
-            LinkedList<Tuples> stackedArgs;
-            // we know this is a linked list, but test just in case it is ever changed.
-            if (args instanceof LinkedList) stackedArgs = (LinkedList<Tuples>)args;
-            else stackedArgs = new LinkedList<Tuples>(args);
+            assert args.size() == 2;
             try {
-              return TuplesOperations.optionalJoin(stackedArgs);
+              return TuplesOperations.optionalJoin((Tuples)args.get(0), (Tuples)args.get(1), ((ConstraintOptionalJoin)constraintExpr).getFilter(), context);
             } finally {
-              for (Tuples t: stackedArgs) t.close();
+              ((Tuples)args.get(0)).close();
+              ((Tuples)args.get(1)).close();
             }
           }
         }),

Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoinUnitTest.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoinUnitTest.java	2008-05-07 14:29:35 UTC (rev 906)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/LeftJoinUnitTest.java	2008-05-07 14:30:50 UTC (rev 907)
@@ -18,6 +18,7 @@
 
 // Locally written packages
 import org.mulgara.query.Variable;
+import static org.mulgara.query.filter.value.Bool.TRUE;
 import static org.mulgara.store.tuples.Tuples.UNBOUND;
 
 /**
@@ -112,7 +113,7 @@
     Tuples rhs = TuplesFactory.newInstance().newTuples(
             new TestTuples(z, 5).and(w, 6).or(z, 7).and(w, 8));
 
-    Tuples tuples = new LeftJoin(lhs, rhs);
+    Tuples tuples = new LeftJoin(lhs, rhs, TRUE, null);
     tuples.beforeFirst();
     assertTrue(tuples.next());
     assertTrue(tuples.next());
@@ -148,7 +149,7 @@
     rhs.appendTuple(new long[] {12, 32});
     rhs.appendTuple(new long[] {15, 16});
 
-    Tuples actual = new LeftJoin(lhs, rhs);
+    Tuples actual = new LeftJoin(lhs, rhs, TRUE, null);
 //    Tuples actual = TuplesOperations.sort(opt);
 //
 //    // check the variables
@@ -212,7 +213,7 @@
     lhs.appendTuple(new long[] {12, 11});
     lhs.appendTuple(new long[] {14, 13});
 
-    actual = new LeftJoin(rhs, lhs);
+    actual = new LeftJoin(rhs, lhs, TRUE, null);
 
     variables = actual.getVariables();
     assertEquals(3, variables.length);
@@ -258,7 +259,7 @@
     rhs.appendTuple(new long[] {7, 14});
     rhs.appendTuple(new long[] {8, 9});
 
-    LeftJoin join = new LeftJoin(lhs, rhs);
+    LeftJoin join = new LeftJoin(lhs, rhs, TRUE, null);
     Tuples actual = TuplesOperations.sort(join);
 
     // check the variables
@@ -312,7 +313,7 @@
     lhs.appendTuple(new long[] {16, 17, 15});
     lhs.appendTuple(new long[] {7, 14, 15});
 
-    actual = TuplesOperations.sort(new LeftJoin(rhs, lhs));
+    actual = TuplesOperations.sort(new LeftJoin(rhs, lhs, TRUE, null));
 
     actual.beforeFirst();
     TuplesTestingUtil.testTuplesRow(actual, new long[] {5, 6, 4});
@@ -332,7 +333,7 @@
     rhs.appendTuple(new long[] {7, 14, 2});
     rhs.appendTuple(new long[] {8, 9, 11});
 
-    actual = TuplesOperations.sort(new LeftJoin(rhs, lhs));
+    actual = TuplesOperations.sort(new LeftJoin(rhs, lhs, TRUE, null));
 
     actual.beforeFirst();
     TuplesTestingUtil.testTuplesRow(actual, new long[] {5, 6, 3, 4});

Modified: trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java
===================================================================
--- trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java	2008-05-07 14:29:35 UTC (rev 906)
+++ trunk/src/jar/tuples/java/org/mulgara/store/tuples/TuplesOperations.java	2008-05-07 14:30:50 UTC (rev 907)
@@ -37,7 +37,9 @@
 
 // Local packages
 import org.mulgara.query.*;
+import org.mulgara.query.filter.And;
 import org.mulgara.query.filter.Filter;
+import org.mulgara.query.filter.value.Bool;
 import org.mulgara.resolver.spi.*;
 
 /**
@@ -340,39 +342,24 @@
     }
   }
 
-  /**
-   * Does a left-associative optionalJoin along a list of arguments.
-   * @param args The arguments to operate on.
-   * @return A tuples containing all of the entries from args.get(0) and matching
-   *   entries from the remainder of the list. All variables from the entire list
-   *   will be present in the result.
-   * @throws TuplesException On error accessing elements in any argument.
-   */
-  public static Tuples optionalJoin(LinkedList<Tuples> args) throws TuplesException {
-    // check for termination. The first couple are for <NIL> syntax elements in the query
-    if (args.size() == 0) return empty();
-    if (args.size() == 1) return args.get(0);
-    if (args.size() == 2) return optionalJoin(args.get(0), args.get(1));
-    // join the head of the list, and then iterate
-    args.addFirst(optionalJoin(args.removeFirst(), args.removeFirst()));
-    return optionalJoin(args);
-  }
 
   /**
    * Does a left-outer-join between two tuples. Parameters are not closed during this
    * operation.
    * @param standard The standard pattern that appears in all results.
    * @param optional The optional pattern that may or may not be bound in each result
+   * @param context The query evaluation context to evaluate any nested fitlers in.
    * @return A Tuples containing variables from both parameters. All variables from
    *   <em>standard</em> will be bound at all times. Variables from <em>optional</em>
    *   may or may not be bound.
    */
-  public static Tuples optionalJoin(Tuples standard, Tuples optional) throws TuplesException {
+  public static Tuples optionalJoin(Tuples standard, Tuples optional, Filter filter, QueryEvaluationContext context) throws TuplesException {
     try {
 
       if (logger.isDebugEnabled()) {
         logger.debug("optional join of " + standard + " optional { " + optional + " }");
       }
+
       // get the matching columns
       Set<Variable> matchingVars = getMatchingVars(standard, optional);
       // check for empty parameters
@@ -406,7 +393,7 @@
 
       // return the difference
       try {
-        return new LeftJoin(standard, sortedOptional);
+        return new LeftJoin(standard, sortedOptional, filter, context);
       } finally {
         if (sortedOptional != optional) sortedOptional.close();
       }
@@ -420,6 +407,7 @@
     }
   }
 
+
   /**
    * Flattens any nested joins to allow polyadic join operations.
    */
@@ -1114,11 +1102,11 @@
    * @param rhs The second tuples to check the variables of.
    * @return A set containing all of the shared variables from lhs and rhs.
    */
-  static Set getMatchingVars(Tuples lhs, Tuples rhs) {
+  static Set<Variable> getMatchingVars(Tuples lhs, Tuples rhs) {
     // get all the variables from the lhs
-    Set commonVarSet = new HashSet(Arrays.asList(lhs.getVariables()));
+    Set<Variable> commonVarSet = new HashSet<Variable>(Arrays.asList(lhs.getVariables()));
     // get all the variables from the rhs
-    Set rhsVars = new HashSet(Arrays.asList(rhs.getVariables()));
+    Set<Variable> rhsVars = new HashSet<Variable>(Arrays.asList(rhs.getVariables()));
 
     // find the intersecting set of variables
     commonVarSet.retainAll(rhsVars);
@@ -1133,7 +1121,7 @@
    * @param vars The variables to check for.
    * @return <code>true</code> when all of the tuples' variables are in <code>vars</code>.
    */
-  private static boolean checkForExtraVariables(Tuples tuples, Collection vars) {
+  private static boolean checkForExtraVariables(Tuples tuples, Collection<Variable> vars) {
     // get the variable list
     Variable[] sv = tuples.getVariables();
     for (int i = 0; i < sv.length; i++) {




More information about the Mulgara-svn mailing list