[Mulgara-svn] r433 - in branches/mgr-58/src/jar: resolver/java/org/mulgara/resolver resolver-spi/java/org/mulgara/resolver/spi

andrae at mulgara.org andrae at mulgara.org
Thu Sep 13 11:23:15 UTC 2007


Author: andrae
Date: 2007-09-13 06:23:14 -0500 (Thu, 13 Sep 2007)
New Revision: 433

Modified:
   branches/mgr-58/src/jar/resolver-spi/java/org/mulgara/resolver/spi/DatabaseMetadata.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BasicDatabaseSessionUnitTest.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BootstrapOperation.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/CreateModelOperation.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseMetadataImpl.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseSessionListQueryUnitTest.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/ModifyModelOperation.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/RemoveModelOperation.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SetModelOperation.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SubqueryAnswerUnitTest.java
   branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/XADatabaseSessionUnitTest.java
Log:
Model-name fix is now almost complete.

With this commit all the resolver-test testcases pass.

We no longer support non-hierarcical URI's as model URL's, and this will require
modifying any remaining tests that refer to local:database#... or similar to use
local://database#... or local:///database#... ; I don't expect this to be a
problem.

I will be posting to the list to discuss issues I have with the semantics of
this solution - however 99% of the fix below is essential regardless of the
semantics we choose to use.



Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/AdvDatabaseSessionUnitTest.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -85,13 +85,13 @@
 
   static {
     try {
-      databaseURI    = new URI("local:database");
-      systemModelURI = new URI("local:database#");
-      modelURI       = new URI("local:database#model");
-      model2URI      = new URI("local:database#model2");
-      model3URI      = new URI("local:database#model3");
-      model4URI      = new URI("local:database#model4");
-      model5URI      = new URI("local:database#model5");
+      databaseURI    = new URI("local:///database");
+      systemModelURI = new URI("local:///database#");
+      modelURI       = new URI("local:///database#model");
+      model2URI      = new URI("local:///database#model2");
+      model3URI      = new URI("local:///database#model3");
+      model4URI      = new URI("local:///database#model4");
+      model5URI      = new URI("local:///database#model5");
 
       xsdModelTypeURI = new URI(Mulgara.NAMESPACE + "XMLSchemaModel");
     } catch (URISyntaxException e) {

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BasicDatabaseSessionUnitTest.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BasicDatabaseSessionUnitTest.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BasicDatabaseSessionUnitTest.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -78,8 +78,8 @@
 
   static {
     try {
-      databaseURI    = new URI("local:database");
-      systemModelURI = new URI("local:database#");
+      databaseURI    = new URI("local:///database");
+      systemModelURI = new URI("local://database#");
       memoryModelURI = new URI(Mulgara.NAMESPACE+"MemoryModel");
     } catch (URISyntaxException e) {
       throw new Error("Bad hardcoded URI", e);
@@ -224,13 +224,20 @@
             0,                                  // OFFSET
             new UnconstrainedAnswer()           // GIVEN
           )));
+        answer.beforeFirst();
+        assertTrue(answer.next());
+        URI systemModel = ((URIReferenceImpl)answer.getObject(0)).getURI();
+        assertEquals("rdfdb", systemModel.getScheme());
+        // Note: The Authority is a UUID and therefore non-deterministic
+        assertEquals("/_database", systemModel.getPath());
+        assertEquals("", systemModel.getFragment());
+        assertFalse(answer.next());
 
-
         // Compose the expected result of the query
         Answer expectedAnswer = new ArrayAnswer(
             new Variable[] { subjectVariable, predicateVariable, objectVariable },
             new Object[] {
-              new URIReferenceImpl(systemModelURI),
+              new URIReferenceImpl(systemModel),
               new URIReferenceImpl(RDF.TYPE),
               new URIReferenceImpl(memoryModelURI)
             });
@@ -253,7 +260,7 @@
   {
     logger.info("testSetModel");
     URI fileURI  = new File("data/dc.rdfs").toURI();
-    URI modelURI = new URI("local:database#model");
+    URI modelURI = new URI("local:///database#model");
 
     try {
       // Register the URL resolver so we can load test data

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BootstrapOperation.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BootstrapOperation.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/BootstrapOperation.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -86,6 +86,13 @@
                       SystemResolver         systemResolver,
                       DatabaseMetadata       metadata) throws Exception {
 
+    if (logger.isDebugEnabled()) {
+      logger.debug("Localizing bootstrapModel, rdfType, serverURIType - " +
+          databaseMetadata.getBootstrapModelURI() + ", " +
+          databaseMetadata.getRdfTypeURI() + ", " +
+          databaseMetadata.getServerURIType());
+    }
+
     long bootstrapModel = systemResolver.localizePersistent(
         new URIReferenceImpl(databaseMetadata.getBootstrapModelURI()));
     long rdfType = systemResolver.localizePersistent(
@@ -101,6 +108,11 @@
     long systemModelType = systemResolver.localizePersistent(
         new URIReferenceImpl(databaseMetadata.getSystemModelTypeURI()));
 
+    if (logger.isDebugEnabled()) {
+      logger.debug("Bootstrapping systemModel - " +
+          databaseMetadata.getSystemModelURI() + "::" + databaseMetadata.getSystemModelTypeURI());
+    }
+
     // Ensure the system model exists.
     systemResolver.modifyModel(systemModel,
         new SingletonStatements(systemModel, rdfType, systemModelType), true);

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/CreateModelOperation.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/CreateModelOperation.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/CreateModelOperation.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -149,8 +149,13 @@
       model = systemResolver.localize(new URIReferenceImpl(modelURL));
     }
 
-    if (modelExists(model, systemResolver, metadata)) {
-      throw new QueryException(modelURL + " already exists");
+    URI existingModelType = getModelType(model, systemResolver, metadata);
+    if (existingModelType != null) {
+      if (existingModelType.equals(modelTypeURI)) {
+        return; // Model already exists - create is idempotent.
+      } else {
+        throw new QueryException(modelURL + " already exists with type: " + existingModelType);
+      }
     }
 
     // Look up the resolver factory for the model type
@@ -197,8 +202,9 @@
     return true;
   }
 
-  private static boolean modelExists(long model, SystemResolver systemResolver,
-      DatabaseMetadata metadata) throws TuplesException, QueryException, ResolverException {
+  // FIXME: This _really_ belongs in DatabaseOperationContext!!!
+  private static URI getModelType(long model, SystemResolver systemResolver, DatabaseMetadata metadata)
+      throws TuplesException, QueryException, ResolverException, GlobalizeException {
     Resolution resolution = systemResolver.resolve(new ConstraintImpl(
         new LocalNode(model),
         new LocalNode(metadata.getRdfTypeNode()),
@@ -207,8 +213,14 @@
 
     try {
       resolution.beforeFirst();
+      if (resolution.next()) {
+        Node o = systemResolver.globalize(resolution.getColumnValue(0));
+        if (o instanceof URIReferenceImpl) {
+          return ((URIReferenceImpl)o).getURI();
+        }
+      }
 
-      return resolution.next();
+      return null;
     } finally {
       resolution.close();
     }

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseMetadataImpl.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseMetadataImpl.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseMetadataImpl.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -35,6 +35,7 @@
 import java.util.Set;
 
 // Third-party packages
+import org.apache.log4j.Logger; // Log4J
 import org.jrdf.vocabulary.RDF;
 
 // Local packages
@@ -57,6 +58,9 @@
 
 public class DatabaseMetadataImpl implements DatabaseMetadata
 {
+  /** Logger.  */
+  private static Logger logger = Logger.getLogger(DatabaseMetadataImpl.class.getName());
+
   private static final URI PREALLOCATED_SUBJECT_URI;
   private static final URI PREALLOCATED_PREDICATE_URI;
   private static final URI PREALLOCATED_MODEL_URI;
@@ -95,6 +99,8 @@
 
   DatabaseMetadataImpl(URI uri, Set hostnameAliases, URI securityDomainURI, URI systemModelTypeURI)
   {
+    logger.debug("Instantiating with uri=" + uri + " hostnameAliases=" + hostnameAliases +
+        " securityDomainURI=" + securityDomainURI + " systemModelTypeURI=" + systemModelTypeURI);
     // Validate parameters.
     if (uri == null) {
       throw new IllegalArgumentException("uri null");
@@ -158,11 +164,23 @@
     return hostnameAliases;
   }
 
-  public String getServerName(URI serverURI) {
-    if (serverURI.getPath() != null) {
+  private String getServerName(URI serverURI) {
+    if (serverURI.getPath() != null && !serverURI.getPath().equals("")) {
+      logger.debug("serverURI.path not null/empty: " + serverURI.getPath());
       return serverURI.getPath().replaceAll("/", "");
+    } else if (serverURI.getScheme().equals("local")) {
+      logger.debug("serverURI.scheme not null/empty: " + serverURI.getScheme());
+      if (serverURI.getAuthority() != null && !serverURI.getAuthority().equals("")) {
+        logger.debug("serverURI.authority not null/empty: " + serverURI.getAuthority());
+        return serverURI.getAuthority();
+      }
+      if (serverURI.getSchemeSpecificPart() != null && !serverURI.getSchemeSpecificPart().equals("")) {
+        logger.debug("serverURI.ssp not null/empty: " + serverURI.getSchemeSpecificPart());
+        return serverURI.getSchemeSpecificPart();
+      }
     }
-    return null;
+
+    return "no_server_name_found";
   }
 
   public String getServerName() {
@@ -305,7 +323,7 @@
     }
 
     this.serverURI = serverURI;
-    this.systemModelURI = new URI(serverURI.getScheme(), serverURI.getAuthority(), "");
+    this.systemModelURI = new URI(serverURI.getScheme(), serverURI.getAuthority(), serverURI.getPath(), "");
   }
 
   public URI getServerURI() {

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseOperationContext.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -327,51 +327,22 @@
     return resolver;
   }
 
-
-  /**
-   * Returns the canonical form of the model, leaving the model alone if it is recognised or unknown.
-   *
-   * @param model The model to check.
-   * @return The new model node, or the current model if it is already canonical or unknown.
-   */
-  public long getCanonicalModel(long model) {
-    // globalize to a URI
-    try {
-      Node modelNode = systemResolver.globalize(model);
-      if (!(modelNode instanceof URIReference)) {
-        logger.warn(modelNode.toString() + " is not a valid Model");
-        return model;
-      }
-      URI modelURI = ((URIReference)modelNode).getURI();
-
-      // check if this is really a reference to a local model, using a different server name
-      Node aliasedNode = getCanonicalAlias(modelURI);
-      if (aliasedNode != null) {
-        return systemResolver.localize(aliasedNode);
-      }
-    } catch (Exception e) {
-      // unable to get a canonical form, so leave this model alone
-    }
-    // model was not recognised as being on this server, so leave it alone
-    return model;
-  }
-
   //
   // Methods required by SymbolicTransformationContext
   //
 
-  public URI mapToModelTypeURI(URI modelURI) throws QueryException {
+  public URI mapToModelTypeURI(URI modelURL) throws QueryException {
     try {
       if (logger.isDebugEnabled()) {
-        logger.debug("Finding modelTypeURI for " + modelURI);
+        logger.debug("Finding modelTypeURI for " + modelURL);
       }
-      long rawModel = systemResolver.localize(new URIReferenceImpl(modelURI));
-      long canModel = getCanonicalModel(rawModel);
+      URI modelURI = convertModelURLtoURI(modelURL);
+      long model = systemResolver.localize(new URIReferenceImpl(modelURI));
 
-      URI modelTypeURI = findModelTypeURI(canModel);
+      URI modelTypeURI = findModelTypeURI(model);
 
       if (logger.isInfoEnabled()) {
-        logger.info("Mapped " + modelURI + " via " + rawModel + ":" + canModel + " to ModelTypeURI: " + modelTypeURI);
+        logger.info("Mapped " + modelURI + " via " + model + " to ModelTypeURI: " + modelTypeURI);
       }
 
       return modelTypeURI;
@@ -398,6 +369,10 @@
   private URI findModelTypeURI(long model)
     throws QueryException, GlobalizeException
   {
+    if (logger.isDebugEnabled()) {
+      logger.debug("Finding ModelTypeURI for " + model + " := " + systemResolver.globalize(model));
+    }
+
     // If model is a query-node, model cannot exist in the system model so return null.
     if (model < 0) {
       return null;
@@ -407,6 +382,9 @@
     LocalNode modelLocalNode = new LocalNode(model);
     URI modelTypeURI = (URI) systemModelCacheMap.get(modelLocalNode);
     if (modelTypeURI != null) {
+      if (logger.isDebugEnabled()) {
+        logger.debug("Found cached uri: " + modelTypeURI);
+      }
       return modelTypeURI;
     }
 
@@ -436,8 +414,16 @@
         modelTypeURI = ((URIReferenceImpl) modelNode).getURI();
         systemModelCacheMap.put(modelLocalNode, modelTypeURI);
 
+        if (logger.isDebugEnabled()) {
+          logger.debug("Found Type URI in system model: " + modelTypeURI);
+        }
+
         return modelTypeURI;
       } else {
+        if (logger.isDebugEnabled()) {
+          logger.debug("Couldn't find Type URI in system model.");
+        }
+
         return null;
       }
     } catch (TuplesException e) {
@@ -478,22 +464,6 @@
   }
 
   /**
-   * Check if the given model actually refers to a model on the local server.
-   *
-   * @param modelURI The URI of the model being searched for.
-   * @return The Node for the local model, or <code>null</code> if not found.
-   * @throws QueryException When the model URI cannot be manipulated.
-   */
-  private Node getCanonicalAlias(URI modelURI) throws QueryException {
-    URI uri = convertModelURLtoURI(modelURI);
-    if (uri == null) {
-      return null;
-    } else {
-      return new URIReferenceImpl(uri);
-    }
-  }
-
-  /**
    * Resolve a localized constraint into the tuples which satisfy it.
    *
    * This method must be called within a transactional context.
@@ -510,7 +480,7 @@
    */
   public Tuples resolve(Constraint constraint) throws QueryException {
     if (logger.isDebugEnabled()) {
-      logger.debug("Resolving " + constraint);
+      logger.debug("Resolving " + constraint, new Throwable());
     }
 
     // Validate "constraint" parameter
@@ -522,47 +492,47 @@
     if (modelElem instanceof Variable) {
       return resolveVariableModel(constraint);
     } else if (modelElem instanceof LocalNode) {
-      long model = ((LocalNode) modelElem).getValue();
-      long realModel = getCanonicalModel(model);
+      return resolveConstantModel(constraint);
+    } else {
+      throw new QueryException("Non-localized model in resolve: " + modelElem);
+    }
+  }
 
-      // Make sure security adapters are satisfied
-      for (Iterator i = securityAdapterList.iterator(); i.hasNext();) {
-        SecurityAdapter securityAdapter = (SecurityAdapter) i.next();
+  private Tuples resolveConstantModel(Constraint constraint) throws QueryException {
+    if (!(constraint.getModel() instanceof LocalNode)) {
+      throw new QueryException("Model not localized prior to resolution: " + constraint);
+    }
+    long model = ((LocalNode)constraint.getModel()).getValue();
 
-        // Lie to the user
-        if (!securityAdapter.canSeeModel(realModel, systemResolver)) {
-          try {
-            throw new QueryException(
-              "No such model " + systemResolver.globalize(realModel));
-          } catch (GlobalizeException e) {
-            logger.warn("Unable to globalize model " + realModel);
-            throw new QueryException("No such model");
-          }
+    // Make sure security adapters are satisfied
+    for (Iterator i = securityAdapterList.iterator(); i.hasNext();) {
+      SecurityAdapter securityAdapter = (SecurityAdapter) i.next();
+
+      // Lie to the user
+      if (!securityAdapter.canSeeModel(model, systemResolver)) {
+        try {
+          throw new QueryException("No such model " + systemResolver.globalize(model));
+        } catch (GlobalizeException e) {
+          logger.warn("Unable to globalize model " + model);
+          throw new QueryException("No such model");
         }
       }
+    }
 
-      for (Iterator i = securityAdapterList.iterator(); i.hasNext();) {
-        SecurityAdapter securityAdapter = (SecurityAdapter) i.next();
+    for (Iterator i = securityAdapterList.iterator(); i.hasNext();) {
+      SecurityAdapter securityAdapter = (SecurityAdapter) i.next();
 
-        // Tell a different lie to the user
-        if (!securityAdapter.canResolve(realModel, systemResolver)) {
-          return TuplesOperations.empty();
-        }
+      // Tell a different lie to the user
+      if (!securityAdapter.canResolve(model, systemResolver)) {
+        return TuplesOperations.empty();
       }
+    }
 
-      // if the model was changed then update the constraint
-      if (model != realModel) {
-        constraint = ConstraintOperations.rewriteConstraintModel(new LocalNode(realModel), constraint);
-      }
+    // Evaluate the constraint
+    Tuples result = obtainResolver(findModelResolverFactory(model)).resolve(constraint);
+    assert result != null;
 
-      // Evaluate the constraint
-      Tuples result = obtainResolver(findModelResolverFactory(realModel)).resolve(constraint);
-      assert result != null;
-
-      return result;
-    } else {
-      throw new QueryException("Non-localized model in resolve: " + modelElem);
-    }
+    return result;
   }
 
   /**
@@ -811,16 +781,23 @@
 
 
   public URI convertModelURLtoURI(URI modelURL) throws QueryException {
-    if (!isLocalModelURL(modelURL)) {
-      return null;
+    if (!isInternalModelURL(modelURL)) {
+      if (logger.isDebugEnabled()) {
+        logger.debug(modelURL + " determined to be External");
+      }
+      return modelURL;
     } else {
       URI serverURI = metadata.getServerURI();
       try {
-        return new URI(serverURI.getScheme(),
+        URI modelURI = new URI(serverURI.getScheme(),
             serverURI.getAuthority(),
             serverURI.getPath(),
             modelURL.getQuery(),
             modelURL.getFragment());
+        if (logger.isDebugEnabled()) {
+          logger.debug(modelURL + " determined to be Internal: " + modelURI);
+        }
+        return modelURI;
       } catch (URISyntaxException eu) {
         throw new QueryException("Invalid model URI generated from serverURI: " +
             serverURI + " and modelURL: " + modelURL, eu);
@@ -828,15 +805,38 @@
     }
   }
 
-  private boolean isLocalModelURL(URI modelURL) throws QueryException {
-    if (modelURL.getScheme().equals("local")) {
-      return modelURL.getAuthority().equals(metadata.getServerName());
+  private boolean isInternalModelURL(URI modelURL) throws QueryException {
+    if (modelURL == null) {
+      throw new IllegalArgumentException("Null 'modelURL' passed to isInternalModelURL");
     }
+
+    if (logger.isDebugEnabled()) {
+      logger.debug("Checking isInternal for: " + modelURL +
+          " with scheme: " + modelURL.getScheme() + 
+          " with authority: " + modelURL.getAuthority() +
+          " with path: " + modelURL.getPath() +
+          " with serverName: " + metadata.getServerName());
+    }
+
+    if ("local".equals(modelURL.getScheme())) {
+      if ((modelURL.getAuthority() != null && metadata.getServerName().equals(modelURL.getAuthority())) ||
+          (modelURL.getPath() != null && ("/" + metadata.getServerName()).equals(modelURL.getPath()))) {
+        return true;
+      } else {
+        throw new QueryException("local: scheme URL: " + modelURL +
+            " doesn't match server-name: " + metadata.getServerName());
+      }
+    }
     if (!modelURL.getPath().equals(metadata.getServerName())) {
       return false;
     }
     if (modelURL.getScheme().equals("rdfdb")) {
-      return modelURL.getAuthority().equals(metadata.getServerURI().getAuthority());
+      if (modelURL.getAuthority().equals(metadata.getServerURI().getAuthority())) {
+        return true;
+      } else {
+        throw new QueryException("rdfdb: scheme URL: " + modelURL +
+            " doesn't match server-name: " + metadata.getServerName());
+      }
     }
 
     String host = modelURL.getHost();

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseSessionListQueryUnitTest.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseSessionListQueryUnitTest.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/DatabaseSessionListQueryUnitTest.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -68,12 +68,12 @@
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
 public class DatabaseSessionListQueryUnitTest extends TestCase {
-  /** The URI of the {@link #database}: <code>local:database</code>.  */
+  /** The URI of the {@link #database}: <code>local://database</code>.  */
   private static final URI databaseURI;
 
   /**
    * The URI of the {@link #database}'s system model:
-   * <code>local:database#</code>.
+   * <code>local://database#</code>.
    */
   private static final URI systemModelURI;
 
@@ -81,7 +81,7 @@
   private static final URI memoryModelURI;
 
   /** A list of model URIs that can be queried.
-   * local:database#model0 -> local:database#modelNUM_MODELS
+   * local://database#model0 -> local://database#modelNUM_MODELS
    */
   private static final List modelURIs;
 
@@ -93,14 +93,14 @@
 
   static {
     try {
-      databaseURI = new URI("local:database");
-      systemModelURI = new URI("local:database#");
+      databaseURI = new URI("local://database");
+      systemModelURI = new URI("local://database#");
       memoryModelURI = new URI(Mulgara.NAMESPACE + "MemoryModel");
 
       //generate a number of models to be 'batch' queried
       modelURIs = new ArrayList();
       for (int i = 0; i < NUM_MODELS; i++) {
-        modelURIs.add(new URI("local:database#model" + i));
+        modelURIs.add(new URI("local://database#model" + i));
       }
     }
     catch (URISyntaxException e) {

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/LocalQueryResolver.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -182,21 +182,32 @@
     assert modelResource != null || !constraint.getModel().equals(Variable.FROM);
     assert constraint != null;
 
-    // Delegate constraint resolution back to the database session
     try {
-      Constraint localized = ConstraintOperations.localize(this, constraint);
-
-      if (localized.getModel().equals(Variable.FROM)) {
-        localized = ConstraintOperations.rewriteConstraintModel(
-            localize(new URIReferenceImpl(modelResource.getURI())), localized);
+      ConstraintElement model = constraint.getModel();
+      if (model == null) {
+        throw new QueryException("Constraint returned null model: " + model);
       }
+      if (model instanceof LocalNode) {
+        throw new QueryException("AWM:I'd prefer to avoid localized constraint-elements this early in the resolution");
+      }
+      // First allow for the delgation to the from-clause.
+      if (model.equals(Variable.FROM)) {
+        model = new URIReferenceImpl(modelResource.getURI());
+      }
+      // Then allow for the use of model URL's.
+      if (model instanceof URIReference) {
+        model = new URIReferenceImpl(operationContext.convertModelURLtoURI(((URIReference)model).getURI()));
+      }
+      // Obtain new constraint that now uses the correct explicit model URI.
+      Constraint canonicalized = ConstraintOperations.rewriteConstraintModel(model, constraint);
 
+      Constraint localized = ConstraintOperations.localize(this, canonicalized);
+    
       Tuples result = operationContext.resolve(localized);
 
       return result;
     } catch (LocalizeException e) {
-      throw new QueryException("Unable to resolve FROM " + modelResource +
-                               " WHERE " + constraint, e);
+      throw new QueryException("Unable to resolve FROM " + modelResource + " WHERE " + constraint, e);
     } catch (QueryException eq) {
       throw new QueryException("Error resolving " + constraint + " from " + modelResource, eq);
     } catch (Exception e) {

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/ModifyModelOperation.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/ModifyModelOperation.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/ModifyModelOperation.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -78,7 +78,7 @@
   /**
    * The URI of the model to be removed.
    */
-  private final URI modelURI;
+  private final URI modelURL;
 
   /**
    * The statements whose occurence is to be modified.
@@ -107,19 +107,19 @@
   /**
    * Construct a modification of a literal set of statements.
    *
-   * @param modelURI  the {@link URI} of the model to be removed, never
+   * @param modelURL  the {@link URI} of the model to be removed, never
    *   <code>null</code>
    * @param tripleSet  the statements to be modified, never <code>null</code>
    * @param insert  whether to insert or delete the <var>tripleSet</var> from
    *   the model
-   * @throws IllegalArgumentException if <var>modelURI</var> or
+   * @throws IllegalArgumentException if <var>modelURL</var> or
    *   <var>tripleSet</var> are <code>null</code>
    */
-  ModifyModelOperation(URI modelURI, Set tripleSet, boolean insert)
+  ModifyModelOperation(URI modelURL, Set tripleSet, boolean insert)
   {
-    // Validate "modelURI" parameter
-    if (modelURI == null) {
-      throw new IllegalArgumentException("Null \"modelURI\" parameter");
+    // Validate "modelURL" parameter
+    if (modelURL == null) {
+      throw new IllegalArgumentException("Null \"modelURL\" parameter");
     }
 
     // Validate "statements" parameter
@@ -128,7 +128,7 @@
     }
 
     // Initialize fields
-    this.modelURI  = modelURI;
+    this.modelURL  = modelURL;
     this.tripleSet = tripleSet;
     this.query     = null;
     this.insert    = insert;
@@ -138,23 +138,23 @@
   /**
    * Construct a modification of a query-generated set of statements.
    *
-   * @param modelURI  the {@link URI} of the model to be removed, never
+   * @param modelURL  the {@link URI} of the model to be removed, never
    *   <code>null</code>
    * @param query  the query used to generate the statements to be modified,
    *   never <code>null</code>
    * @param insert  whether to insert or delete statements from the model
-   * @throws IllegalArgumentException if <var>modelURI</var> or
+   * @throws IllegalArgumentException if <var>modelURL</var> or
    *   <var>query</var> are <code>null</code>
    * @throws QueryException if <var>query</var> doesn't have exactly three
    *   variables in its <code>SELECT</code> clause
    */
-  ModifyModelOperation(URI modelURI, Query query, boolean insert,
+  ModifyModelOperation(URI modelURL, Query query, boolean insert,
                        DatabaseSession databaseSession)
     throws QueryException
   {
-    // Validate "modelURI" parameter
-    if (modelURI == null) {
-      throw new IllegalArgumentException("Null \"modelURI\" parameter");
+    // Validate "modelURL" parameter
+    if (modelURL == null) {
+      throw new IllegalArgumentException("Null \"modelURL\" parameter");
     }
 
     // Validate "query" parameter
@@ -172,7 +172,7 @@
     }
 
     // Initialize fields
-    this.modelURI  = modelURI;
+    this.modelURL  = modelURL;
     this.tripleSet = null;
     this.query     = query;
     this.insert    = insert;
@@ -211,7 +211,7 @@
     assert statements != null;
 
     try {
-      doModify(operationContext, systemResolver, modelURI, statements, insert);
+      doModify(operationContext, systemResolver, modelURL, statements, insert);
     } finally {
       statements.close();
     }
@@ -220,14 +220,14 @@
 
   protected void doModify(OperationContext operationContext,
                           SystemResolver   systemResolver,
-                          URI              modelURI,
+                          URI              modelURL,
                           Statements       statements,
                           boolean          insert) throws Exception
   {
+    URI modelURI = operationContext.convertModelURLtoURI(modelURL);
     long model = systemResolver.localize(new URIReferenceImpl(modelURI));
+    logger.info("converted " + modelURL + " to " + modelURI + " localized to " + model);
 
-    model = operationContext.getCanonicalModel(model);
-
     // Make sure security adapters are satisfied
     for (Iterator i = operationContext.getSecurityAdapterList().iterator();
          i.hasNext(); )
@@ -236,12 +236,12 @@
 
       // Lie to the user
       if (!securityAdapter.canSeeModel(model, systemResolver)) {
-        throw new QueryException("No such model " + modelURI);
+        throw new QueryException("No such model " + modelURL);
       }
 
       // Tell the truth to the user
       if (!securityAdapter.canModifyModel(model, systemResolver)) {
-        throw new QueryException("You aren't allowed to modify " + modelURI);
+        throw new QueryException("You aren't allowed to modify " + modelURL);
       }
     }
 
@@ -251,13 +251,13 @@
     assert resolver != null;
 
     if (logger.isDebugEnabled()) {
-      logger.debug("Modifying " + modelURI + " using " + resolver);
+      logger.debug("Modifying " + modelURL + " using " + resolver);
     }
 
     resolver.modifyModel(model, statements, insert);
 
     if (logger.isDebugEnabled()) {
-      logger.debug("Modified " + modelURI);
+      logger.debug("Modified " + modelURL);
     }
   }
 

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -108,12 +108,13 @@
 
 
   /**
-   * Returns the canonical form of the model, leaving the model alone if it is recognised or unknown.
+   * Converts a model URL into a model URI if the URL refers to an internal
+   * model.  Returns the URL unchanged if it refers to an external model or
+   * throws an exception if it is a malformed internal URL.
    *
-   * @param model The model to check.
-   * @return The new model node, or the current model if it is already canonical or unknown.
+   * @return The URI that corresponds to the provided model URL.
    */
-  public long getCanonicalModel(long model);
+  public URI convertModelURLtoURI(URI modelURL) throws QueryException;
 
   /**
    * Here for the moment while we fix transactions.
@@ -123,9 +124,4 @@
   public Answer doQuery(Query query) throws Exception;
 
   public SystemResolver getSystemResolver(); // FIXME: Scaffolding for transactions.
-
-  /**
-   * Converts a given model-URL into a model-URI for this server.
-   */
-  public URI convertModelURLtoURI(URI modelURL) throws QueryException;
 }

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/RemoveModelOperation.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/RemoveModelOperation.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/RemoveModelOperation.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -90,7 +90,7 @@
   /**
    * The URI of the model to be removed.
    */
-  private final URI modelURI;
+  private final URI modelURL;
 
   //
   // Constructor
@@ -99,20 +99,20 @@
   /**
    * Sole constructor.
    *
-   * @param modelURI  the {@link URI} of the model to be removed, never
+   * @param modelURL  the {@link URI} of the model to be removed, never
    *   <code>null</code>
-   * @throws IllegalArgumentException if <var>modelURI</var> is
+   * @throws IllegalArgumentException if <var>modelURL</var> is
    *   <code>null</code>
    */
-  RemoveModelOperation(URI modelURI)
+  RemoveModelOperation(URI modelURL)
   {
-    // Validate "modelURI" parameter
-    if (modelURI == null) {
-      throw new IllegalArgumentException("Null \"modelURI\" parameter");
+    // Validate "modelURL" parameter
+    if (modelURL == null) {
+      throw new IllegalArgumentException("Null \"modelURL\" parameter");
     }
 
     // Initialize fields
-    this.modelURI = modelURI;
+    this.modelURL = modelURL;
   }
 
   //
@@ -123,8 +123,9 @@
                       SystemResolver         systemResolver,
                       DatabaseMetadata       metadata) throws Exception
   {
+    URI modelURI = operationContext.convertModelURLtoURI(modelURL);
     long model = systemResolver.localize(new URIReferenceImpl(modelURI));
-    model = operationContext.getCanonicalModel(model);
+    logger.info("converted " + modelURL + " to " + modelURI + " localized to " + model);
 
     // Make sure security adapters are satisfied
     for (Iterator i = operationContext.getSecurityAdapterList().iterator();
@@ -134,12 +135,12 @@
 
       // Lie to the user
       if (model == NodePool.NONE || !securityAdapter.canSeeModel(model, systemResolver)) {
-        throw new QueryException("No such model " + modelURI);
+        throw new QueryException("No such model " + modelURL);
       }
 
       // Tell the truth to the user
       if (!securityAdapter.canRemoveModel(model, systemResolver)) {
-        throw new QueryException("You aren't allowed to remove " + modelURI);
+        throw new QueryException("You aren't allowed to remove " + modelURL);
       }
     }
 
@@ -149,7 +150,7 @@
 
     if (resolverFactory == null) {
       throw new QueryException(
-        "Could not obtain a resolver factory for " + modelURI
+        "Could not obtain a resolver factory for " + modelURL
       );
     }
 

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SetModelOperation.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SetModelOperation.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SetModelOperation.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -88,8 +88,8 @@
   private static final Logger logger =
     Logger.getLogger(SetModelOperation.class.getName());
 
-  private final URI         srcModelURI;
-  private final URI         destModelURI;
+  private final URI         srcModelURL;
+  private final URI         destModelURL;
   private final InputStream inputStream;
 
   private Content               content;
@@ -109,19 +109,19 @@
   /**
    * Sole constructor.
    *
-   * @param srcModelURI the {@link URI} of the model to be redefined
-   * @param destModelURI the {@link URI} of the model to be redefined
-   * @param inputStream  a stream containing the content at the <var>srcModelURI</var>
+   * @param srcModelURL the {@link URI} of the model to be redefined
+   * @param destModelURL the {@link URI} of the model to be redefined
+   * @param inputStream  a stream containing the content at the <var>srcModelURL</var>
    *   or <code>null</code> if the content must be fetched
    */
-  SetModelOperation(URI         srcModelURI,
-                    URI         destModelURI,
+  SetModelOperation(URI         srcModelURL,
+                    URI         destModelURL,
                     InputStream inputStream,
                     ContentHandlerManager contentHandlers,
                     DatabaseSession databaseSession)
   {
-    this.srcModelURI      = srcModelURI;
-    this.destModelURI = destModelURI;
+    this.srcModelURL      = srcModelURL;
+    this.destModelURL = destModelURL;
     this.inputStream         = inputStream;
     this.contentHandlers = contentHandlers;
     this.databaseSession    = databaseSession;
@@ -140,14 +140,17 @@
       throw new IllegalStateException("SetModelOperation already executed.  Cannot reexecute.");
     }
 
+    URI srcModelURI = operationContext.convertModelURLtoURI(srcModelURL);
     long sourceModel = systemResolver.localize(new URIReferenceImpl(srcModelURI));
+    logger.info("converted " + srcModelURL + " to " + srcModelURI + " localized to " + sourceModel);
+
+    URI destModelURI = operationContext.convertModelURLtoURI(destModelURL);
     long destinationModel = systemResolver.localize(new URIReferenceImpl(destModelURI));
+    logger.info("converted " + destModelURL + " to " + destModelURI + " localized to " + destinationModel);
 
     if (destinationModel == sourceModel) {
-      throw new QueryException("Identical source and destination: " + destModelURI);
+      throw new QueryException("Identical source and destination: " + destModelURL);
     }
-    // update the destination to the canonical form
-    destinationModel = operationContext.getCanonicalModel(destinationModel);
 
     // Obtain a resolver for the destination model type
     Resolver destinationResolver = operationContext.obtainResolver(
@@ -209,12 +212,12 @@
     // create a StreamContent if an inputStream has been supplied
     if (inputStream != null) {
       if (logger.isDebugEnabled()) {
-        logger.debug("Detected inputstream associated with " + srcModelURI );
+        logger.debug("Detected inputstream associated with " + srcModelURL );
       }
-      content = new StreamContent(inputStream, srcModelURI);
+      content = new StreamContent(inputStream, srcModelURL);
     } else {
       try {
-        content = ContentFactory.getContent(srcModelURI);
+        content = ContentFactory.getContent(srcModelURL);
       } catch (ContentHandlerException ec) {
         throw new QueryException("Failed to find Content for uri", ec);
       }
@@ -231,7 +234,7 @@
 
     if (loader != null) {
       try {
-        statementCount = loader.load(content, new ModelResource(destModelURI), databaseSession);
+        statementCount = loader.load(content, new ModelResource(destModelURL), databaseSession);
 
         return false; // notifies caller that a call to execute will fail.
       } catch (ContentHandlerException handlerException) {

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SubqueryAnswerUnitTest.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SubqueryAnswerUnitTest.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/SubqueryAnswerUnitTest.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -200,13 +200,11 @@
     public Resolver obtainResolver(ResolverFactory resolverFactory)
       throws QueryException { return null; }
 
-    public long getCanonicalModel(long model) { return 0; }
+    public URI convertModelURLtoURI(URI model) { return null; }
 
     public Answer doQuery(Query query)
     throws Exception { return null; }
 
     public SystemResolver getSystemResolver() { return null; } // FIXME: Scaffolding for transactions.
-
-    public URI convertModelURLtoURI(URI modelURL) { return null; }
   }
 }

Modified: branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/XADatabaseSessionUnitTest.java
===================================================================
--- branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/XADatabaseSessionUnitTest.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver/java/org/mulgara/resolver/XADatabaseSessionUnitTest.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -65,12 +65,12 @@
 * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
 */
 public class XADatabaseSessionUnitTest extends TestCase {
-  /** The URI of the {@link #database}: <code>local:database</code>.  */
+  /** The URI of the {@link #database}: <code>local:///database</code>.  */
   private static final URI databaseURI;
 
   /**
   * The URI of the {@link #database}'s system model:
-  * <code>local:database#</code>.
+  * <code>local:///database#</code>.
   */
   private static final URI systemModelURI;
 
@@ -79,8 +79,8 @@
 
   static {
     try {
-      databaseURI    = new URI("local:database");
-      systemModelURI = new URI("local:database#");
+      databaseURI    = new URI("local:///database");
+      systemModelURI = new URI("local:///database#");
       memoryModelURI = new URI(Mulgara.NAMESPACE+"MemoryModel");
     } catch (URISyntaxException e) {
       throw new Error("Bad hardcoded URI", e);
@@ -124,9 +124,9 @@
   static {
     try {
       //String baseuri = "rmi://" + InetAddress.getLocalHost().getCanonicalHostName() + "/server1#";
-      String baseuri = "local:database#";
+      String baseuri = "local:///database#";
       modelURIs = new URI[] {
-        new URI(baseuri), //new URI("local:database#"),
+        new URI(baseuri), //new URI("local:///database#"),
         new URI(baseuri + "test-model1"),
         new URI(baseuri + "test-model2"),
         new URI(baseuri + "test-model3"),
@@ -211,14 +211,14 @@
                  new String[] { "org.mulgara.resolver.store.StatementStoreResolverFactory" },
                  "(query V0 (| V0 V4 V2))",
                  "M11",
-                 "(result _node84 _node85)",
+                 "(result _node87 _node88)",
                  "testSubjectVarObjModelPrimitive"),
 
     parser.parse("testModel",
                  new String[] { "org.mulgara.resolver.store.StatementStoreResolverFactory" },
                  "(query V0 (| V0 V4 V7))",
                  "M11",
-                 "(result _node84)",
+                 "(result _node87)",
                  "testSubjectLitObjModelPrimitive"),
 
     parser.parse("testModel",

Modified: branches/mgr-58/src/jar/resolver-spi/java/org/mulgara/resolver/spi/DatabaseMetadata.java
===================================================================
--- branches/mgr-58/src/jar/resolver-spi/java/org/mulgara/resolver/spi/DatabaseMetadata.java	2007-09-13 05:50:00 UTC (rev 432)
+++ branches/mgr-58/src/jar/resolver-spi/java/org/mulgara/resolver/spi/DatabaseMetadata.java	2007-09-13 11:23:14 UTC (rev 433)
@@ -65,7 +65,7 @@
    * @return the name of the server based on the URI or null if no path is
    *   found.
    */
-  public String getServerName(URI serverURI);
+//  public String getServerName(URI serverURI);
 
   public URI getServerURI();
 




More information about the Mulgara-svn mailing list