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

pag at mulgara.org pag at mulgara.org
Sat May 31 01:36:12 UTC 2008


Author: pag
Date: 2008-05-30 18:36:12 -0700 (Fri, 30 May 2008)
New Revision: 968

Modified:
   trunk/src/jar/resolver-store/java/org/mulgara/resolver/store/StatementStoreResolver.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/Database.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java
Log:
Added handling for variable graphs when the same variable is used in another context. Now converts these constraints to have single occurances of their variables, and uses SameTerm filters to perform the matching

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java	2008-05-31 01:33:23 UTC (rev 967)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java	2008-05-31 01:36:12 UTC (rev 968)
@@ -43,6 +43,7 @@
 import org.mulgara.resolver.spi.ConstraintLocalization;
 import org.mulgara.resolver.spi.ConstraintModelRewrite;
 import org.mulgara.resolver.spi.ConstraintResolutionHandler;
+import org.mulgara.resolver.spi.ConstraintVariableRewrite;
 import org.mulgara.resolver.spi.ModelResolutionHandler;
 import org.mulgara.resolver.spi.QueryEvaluationContext;
 import org.mulgara.store.tuples.Tuples;
@@ -79,7 +80,10 @@
       new HashMap<Class<? extends ConstraintExpression>,Object>();
   static Map<Class<? extends ConstraintExpression>,Object> constraintModelRewrites =
       new HashMap<Class<? extends ConstraintExpression>,Object>();
+  static Map<Class<? extends ConstraintExpression>,Object> constraintVariableRewrites =
+    new HashMap<Class<? extends ConstraintExpression>,Object>();
 
+
   static {
     DefaultConstraintHandlers.initializeHandlers();
   }
@@ -106,6 +110,11 @@
              ConstraintExpression.class, ConstraintModelRewrite.class);
   }
 
+  static void addConstraintVariableRewrites(NVPair<Class<? extends ConstraintExpression>,Object>[] resolutionHandlers) throws RuntimeException {
+    addToMap(resolutionHandlers, constraintVariableRewrites,
+             ConstraintExpression.class, ConstraintVariableRewrite.class);
+  }
+
   static void addConstraintLocalizations(NVPair<Class<? extends ConstraintExpression>,Object>[] resolutionHandlers) throws RuntimeException {
     addToMap(resolutionHandlers, constraintLocalizations,
              Constraint.class, ConstraintLocalization.class);
@@ -264,6 +273,31 @@
   }
 
 
+  public static Constraint rewriteConstraintVariable(Variable modelVar, Variable newVar, Constraint constraint) throws QueryException {
+    try {
+      if (logger.isDebugEnabled()) {
+        logger.debug("Rewriting variable " + modelVar + " in " + constraint + " and setting graph");
+      }
+
+      ConstraintVariableRewrite op = (ConstraintVariableRewrite)constraintVariableRewrites.get(constraint.getClass());
+      if (op == null) {
+        throw new QueryException("Unknown Constraint type: " + constraint.getClass() + " known types: " + constraintVariableRewrites.keySet());
+      }
+      Constraint result = op.rewrite(modelVar, newVar, constraint);
+
+      if (logger.isDebugEnabled()) {
+        logger.debug("Rewrote Graph " + modelVar + " in " + constraint + " to " + result);
+      }
+
+      return result;
+    } catch (QueryException eq) {
+      throw eq;
+    } catch (Exception e) {
+      throw new QueryException("Rewriting constraint failed", e);
+    }
+  }
+
+
   public static Constraint replace(Map<Variable,Value> bindings, Constraint constraint) throws QueryException {
     return ConstraintFactory.newConstraint(replace(bindings, constraint.getElement(0)),
                                            replace(bindings, constraint.getElement(1)),

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/Database.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/Database.java	2008-05-31 01:33:23 UTC (rev 967)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/Database.java	2008-05-31 01:36:12 UTC (rev 968)
@@ -60,6 +60,7 @@
 import org.mulgara.query.QueryException;
 import org.mulgara.query.rdf.Mulgara;
 import org.mulgara.resolver.spi.DatabaseMetadata;
+import org.mulgara.resolver.spi.DuplicateVariableTransformer;
 import org.mulgara.resolver.spi.FactoryInitializer;
 import org.mulgara.resolver.spi.InitializerException;
 import org.mulgara.resolver.spi.LocalizeException;
@@ -706,11 +707,15 @@
                                         metadata.getSystemModelNode())
     );
 
+    addModelType(((SystemResolverFactory)temporaryResolverFactory).getSystemModelTypeURI(),
+          temporaryResolverFactory);
+
     // Add the mandatory security adapter that protects the system model
     securityAdapterList.add(
       new SystemModelSecurityAdapter(metadata.getSystemModelNode())
     );
 
+    addSymbolicTransformation(new DuplicateVariableTransformer());
     this.ruleLoaderClassName = ruleLoaderClassName;
 
     if (logger.isDebugEnabled()) {
@@ -951,9 +956,14 @@
 
     // Make sure some other resolver factory hasn't claimed this model type
     if (internalResolverFactoryMap.containsKey(modelTypeURI)) {
-      throw new InitializerException(
-          "Model type " + modelTypeURI + " is already registered to " +
-          internalResolverFactoryMap.get(modelTypeURI));
+      // check if the other resolver factory is actually the current one
+      InternalResolverFactory rf = internalResolverFactoryMap.get(modelTypeURI);
+      if (!rf.resolverFactory.getClass().equals(resolverFactory.getClass())) {
+        throw new InitializerException("Model type " + modelTypeURI + " is already registered to " + rf.resolverFactory);
+      } else {
+        // already registered
+        return;
+      }
     }
 
     // Register this resolver factory as handling this model type

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java	2008-05-31 01:33:23 UTC (rev 967)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DefaultConstraintHandlers.java	2008-05-31 01:36:12 UTC (rev 968)
@@ -54,11 +54,14 @@
 
 // Local packages
 import org.mulgara.query.*;
+import org.mulgara.query.filter.SameTerm;
+import org.mulgara.query.filter.value.Var;
 import org.mulgara.query.rdf.URIReferenceImpl;
 import org.mulgara.resolver.spi.ConstraintBindingHandler;
 import org.mulgara.resolver.spi.ConstraintLocalization;
 import org.mulgara.resolver.spi.ConstraintModelRewrite;
 import org.mulgara.resolver.spi.ConstraintResolutionHandler;
+import org.mulgara.resolver.spi.ConstraintVariableRewrite;
 import org.mulgara.resolver.spi.ModelResolutionHandler;
 import org.mulgara.resolver.spi.QueryEvaluationContext;
 import org.mulgara.store.tuples.Tuples;
@@ -84,6 +87,7 @@
     initializeConstraintResolutionHandlers();
     initializeConstraintBindingHandlers();
     initializeConstraintModelRewrites();
+    initializeConstraintVariableRewrites();
     initializeConstraintLocalizations();
   }
 
@@ -130,12 +134,25 @@
         new NVPair(ModelVariable.class, new ModelResolutionHandler() {
           public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr,
                                 Constraint constraint) throws Exception {
-            return context.resolve(null, ConstraintOperations.rewriteConstraintModel(((ModelVariable)modelExpr).getVariable(), constraint));
+            Variable modelVar = ((ModelVariable)modelExpr).getVariable();
+            if (constraint.getVariables().contains(modelVar)) {
+              // need to change the re-write and wrap the result in a filter
+              Variable newVar = new Variable("*" + modelVar.getName() + "0");
+              constraint = ConstraintOperations.rewriteConstraintVariable(modelVar, newVar, constraint);
+              Tuples result = context.resolve(null, constraint);
+              return TuplesOperations.filter(result, new SameTerm(convert(newVar), convert(modelVar)), context);
+            }
+            return context.resolve(null, ConstraintOperations.rewriteConstraintModel(modelVar, constraint));
           }
         })
       });
   }
 
+  /** Utility for converting a Variable to a filterable Var */
+  static Var convert(Variable v) {
+    return new Var(v.getName());
+  }
+
   @SuppressWarnings("unchecked")
   static void initializeConstraintResolutionHandlers() {
     ConstraintOperations.addConstraintResolutionHandlers(new NVPair[]
@@ -201,16 +218,22 @@
         }),
         new NVPair(ConstraintImpl.class, new ConstraintResolutionHandler() {
           public Tuples resolve(QueryEvaluationContext context, ModelExpression modelExpr, ConstraintExpression constraintExpr) throws Exception {
-            ConstraintElement constraintElem =
-              ((ConstraintImpl) constraintExpr).getModel();
+            ConstraintImpl constraint = (ConstraintImpl)constraintExpr;
+            ConstraintElement constraintElem = constraint.getModel();
             assert constraintElem != null;
             if (constraintElem.equals(Variable.FROM)) {
-              return ConstraintOperations.resolveModelExpression(context, modelExpr, (Constraint)constraintExpr);
+              return ConstraintOperations.resolveModelExpression(context, modelExpr, constraint);
             } else if (constraintElem instanceof URIReference) {
-              return ConstraintOperations.resolveModelExpression(context, new ModelResource(((URIReference)constraintElem).getURI()), (Constraint)constraintExpr);
+              return ConstraintOperations.resolveModelExpression(context, new ModelResource(((URIReference)constraintElem).getURI()), constraint);
             } else if (constraintElem instanceof LocalNode) {
-              return context.resolve(null, (ConstraintImpl)constraintExpr);
+              return context.resolve(null, constraint);
             } else if (constraintElem instanceof Variable) {
+              for (int i = 0; i < 3; i++) {
+                if (constraintElem.equals(constraint.getElement(i))) {
+                  ModelVariable modelVar = new ModelVariable((Variable)constraintElem);
+                  return ConstraintOperations.resolveModelExpression(context, modelVar, constraint);
+                }
+              }
               return context.resolve(null, (ConstraintImpl)constraintExpr);
             }
             else {
@@ -344,6 +367,25 @@
   }
 
   @SuppressWarnings("unchecked")
+  static void initializeConstraintVariableRewrites() {
+    ConstraintOperations.addConstraintVariableRewrites(new NVPair[]
+      {
+        new NVPair(ConstraintImpl.class, new ConstraintVariableRewrite() {
+          public Constraint rewrite(Variable modelVar, Variable newVar, Constraint constraint) throws Exception {
+            ConstraintElement[] ce = new ConstraintElement[3];
+            for (int e = 0; e < ce.length; e++) {
+              ce[e] = constraint.getElement(e);
+              if (ce[e] instanceof Variable && ((Variable)ce[e]).getName().equals(modelVar.getName())) {
+                ce[e] = newVar;
+              }
+            }
+            return new ConstraintImpl(ce[0], ce[1], ce[2], modelVar);
+          }
+        }),
+      });
+  }
+
+  @SuppressWarnings("unchecked")
   static void initializeConstraintLocalizations() {
     ConstraintOperations.addConstraintLocalizations(new NVPair[]
       {

Modified: trunk/src/jar/resolver-store/java/org/mulgara/resolver/store/StatementStoreResolver.java
===================================================================
--- trunk/src/jar/resolver-store/java/org/mulgara/resolver/store/StatementStoreResolver.java	2008-05-31 01:33:23 UTC (rev 967)
+++ trunk/src/jar/resolver-store/java/org/mulgara/resolver/store/StatementStoreResolver.java	2008-05-31 01:36:12 UTC (rev 968)
@@ -33,21 +33,15 @@
 import java.io.IOException;
 import java.io.Writer;
 import java.net.URI;
-import java.util.Iterator;
-import java.util.Set;
 import javax.transaction.xa.XAResource;
 
 // Third party packages
 import org.apache.log4j.Logger;
-import org.jrdf.graph.GraphException;
 import org.jrdf.graph.Node;
-import org.jrdf.graph.Triple;
 import org.jrdf.graph.URIReference;
-import org.jrdf.vocabulary.RDF;
 
 // Locally written packages
 import org.mulgara.query.*;
-import org.mulgara.query.rdf.URIReferenceImpl;
 import org.mulgara.resolver.spi.*;
 import org.mulgara.store.nodepool.NodePool;
 import org.mulgara.store.nodepool.NodePoolException;
@@ -57,7 +51,6 @@
 import org.mulgara.store.stringpool.SPObjectFactory;
 import org.mulgara.store.stringpool.StringPoolException;
 import org.mulgara.store.tuples.Tuples;
-import org.mulgara.store.tuples.TuplesOperations;
 import org.mulgara.store.xa.SimpleXAResource;
 import org.mulgara.store.xa.SimpleXAResourceException;
 import org.mulgara.store.xa.XAResolverSession;
@@ -117,6 +110,7 @@
 
   private final XAResource xaresource;
 
+  @SuppressWarnings("unused")
   private boolean isSystemResolver;
 
   private long systemModel;
@@ -405,7 +399,7 @@
             throw new QueryException("Unable to resolve constraint " + constraint + " unknown type");
           }
         } else {
-          return new StatementStoreDuplicateResolution(constraint, statementStore);
+          throw new QueryException("Duplicate variable found during resolution");
         }
       } else {
         return new EmptyResolution(constraint, true);
@@ -509,33 +503,6 @@
   // Internal methods
   //
 
-  /**
-   * Convert {@link ConstraintElement} objects into node numbers suitable for
-   * invoking the {@link StatementStore#findTuples}.
-   *
-   * @param constraintElement  the element to convert
-   * @return {@link NodePool#NONE} if the <var>constraintElement</var> is
-   *  {@link Variable}, otherwise the node number if it's a {@link LocalNode}
-   * @throws QueryException if <var>constraintElement</var> is neither a
-   *  {@link Variable} nor a {@link LocalNode}
-   */
-  private static long toGraphTuplesIndex(ConstraintElement constraintElement)
-    throws QueryException
-  {
-    assert constraintElement != null;
-
-    if (constraintElement instanceof Variable) {
-      return NodePool.NONE;
-    } else if (constraintElement instanceof LocalNode) {
-      return ((LocalNode) constraintElement).getValue();
-    } else {
-      throw new QueryException("Unsupported constraint element: " +
-                               constraintElement + " (" +
-                               constraintElement.getClass() + ")");
-    }
-  }
-
-
   public void abort() {
     try {
       try {




More information about the Mulgara-svn mailing list