[Mulgara-svn] r1699 - in trunk/src/jar: krule/java/org/mulgara/krule query/java/org/mulgara/query query/java/org/mulgara/query/operation query/java/org/mulgara/rules query/java/org/mulgara/server resolver/java/org/mulgara/resolver server-rmi/java/org/mulgara/server/rmi

alexhall at mulgara.org alexhall at mulgara.org
Fri May 22 18:39:14 UTC 2009


Author: alexhall
Date: 2009-05-22 11:39:13 -0700 (Fri, 22 May 2009)
New Revision: 1699

Modified:
   trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
   trunk/src/jar/krule/java/org/mulgara/krule/QueryStruct.java
   trunk/src/jar/query/java/org/mulgara/query/GraphResource.java
   trunk/src/jar/query/java/org/mulgara/query/operation/ApplyRules.java
   trunk/src/jar/query/java/org/mulgara/rules/RuleLoaderFactory.java
   trunk/src/jar/query/java/org/mulgara/server/Session.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java
   trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java
Log:
Add API support for applying rules to complex graph expressions (as opposed to single graph URI's)

Modified: trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
===================================================================
--- trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -64,8 +64,8 @@
   /** The Graph resource represented by ruleGraphUri. */
   private final GraphResource ruleGraph;
 
-  /** The URI of the graph containing the base data. */
-  private URI baseGraphUri;
+  /** The graph expression containing the base data. */
+  private GraphExpression baseGraphExpr;
 
   /** The URI of the graph to receive the entailed data. */
   private URI destGraphUri;
@@ -98,11 +98,11 @@
    * Principle constructor.
    *
    * @param ruleGraphUri The name of the graph with the rules to run.
-   * @param baseGraphUri The name of the graph with the base data.
+   * @param baseGraphExpr The graph expression with the base data.
    * @param destGraphUri The name of the graph which will receive the entailed data.
    */
-  KruleLoader(URI ruleGraphUri, URI baseGraphUri, URI destGraphUri) {
-    this.baseGraphUri = baseGraphUri;
+  KruleLoader(URI ruleGraphUri, GraphExpression baseGraphExpr, URI destGraphUri) {
+    this.baseGraphExpr = baseGraphExpr;
     this.destGraphUri = destGraphUri;
 
     ruleGraph = new GraphResource(ruleGraphUri);
@@ -122,12 +122,12 @@
    * Factory method.
    *
    * @param ruleModel The name of the model with the rules to run.
-   * @param baseModel The name of the model with the base data.
+   * @param baseGraph The graph expression with the base data.
    * @param destModel The name of the model which will receive the entailed data.
    * @return A new KruleLoader instance.
    */
-  public static RuleLoader newInstance(URI ruleModel, URI baseModel, URI destModel) {
-    return new KruleLoader(ruleModel, baseModel, destModel);
+  public static RuleLoader newInstance(URI ruleModel, GraphExpression baseGraph, URI destModel) {
+    return new KruleLoader(ruleModel, baseGraph, destModel);
   }
 
 
@@ -389,7 +389,7 @@
 
       if (logger.isDebugEnabled()) logger.debug("Setting models for the query");
       // set the models
-      queryStruct.setModelExpression(baseGraphUri, destGraphUri);
+      queryStruct.setGraphExpression(baseGraphExpr, destGraphUri);
 
       if (logger.isDebugEnabled()) logger.debug("Setting query structure for the rule");
       // create a new query and set it for the rule

Modified: trunk/src/jar/krule/java/org/mulgara/krule/QueryStruct.java
===================================================================
--- trunk/src/jar/krule/java/org/mulgara/krule/QueryStruct.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/krule/java/org/mulgara/krule/QueryStruct.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -68,8 +68,8 @@
   /** List of elements which are variables, or ConstantValues. */
   private List<SelectElement> variables;
 
-  /** The model expresison for the query. */
-  private GraphExpression models;
+  /** The graph expresison for the query. */
+  private GraphExpression graphs;
 
   /** The where clause of the query. */
   private ConstraintExpression where;
@@ -153,7 +153,7 @@
       }
     }
 
-    models = null;
+    graphs = null;
     having = null;
   }
 
@@ -201,26 +201,26 @@
 
 
   /**
-   * Sets the default models for the query.
+   * Sets the default graph expression for the query.
    *
-   * @param modelUri The URI of the model for the query.
+   * @param expr The graph expression for the query.
    */
-  public void setModelExpression(URI modelUri) {
-    this.models = new GraphResource(modelUri);
+  public void setGraphExpression(GraphExpression expr) {
+    this.graphs = expr;
   }
 
 
   /**
-   * Sets the default models for the query.
+   * Sets the default graph expression for the query.
    *
-   * @param firstModelUri The first URI of the model for the query.
-   * @param secondModelUri The second URI of the model for the query.
+   * @param expr The base graph expression for the query.
+   * @param secondGraphUri The secondary graph URI for the query.
    */
-  public void setModelExpression(URI firstModelUri, URI secondModelUri) {
-    if (firstModelUri.equals(secondModelUri)) {
-      setModelExpression(firstModelUri);
+  public void setGraphExpression(GraphExpression expr, URI secondGraphUri) {
+    if (GraphResource.sameAs(expr, secondGraphUri)) {
+      setGraphExpression(expr);
     } else {
-      this.models = new GraphUnion(new GraphResource(firstModelUri), new GraphResource(secondModelUri));
+      setGraphExpression(new GraphUnion(expr, new GraphResource(secondGraphUri)));
     }
   }
 
@@ -233,7 +233,7 @@
   @SuppressWarnings("unchecked")
   public Query extractQuery() {
     logger.debug("Extracting query");
-    return new Query(variables, models, where, having, (List<Order>)Collections.EMPTY_LIST, null, 0, new UnconstrainedAnswer());
+    return new Query(variables, graphs, where, having, (List<Order>)Collections.EMPTY_LIST, null, 0, new UnconstrainedAnswer());
   }
 
 }

Modified: trunk/src/jar/query/java/org/mulgara/query/GraphResource.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/GraphResource.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/query/java/org/mulgara/query/GraphResource.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -54,6 +54,18 @@
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
 public class GraphResource implements Graph {
+  
+  /**
+   * Static utility to test whether the given expression is a GraphResource
+   * instance with the given URI.
+   * @param expr An arbitrary graph expression.
+   * @param uri An arbitrary URI.
+   * @return <tt>true</tt> iff the graph expression is a GraphResource whose URI is equal
+   * to the given URI.
+   */
+  public static boolean sameAs(GraphExpression expr, URI uri) {
+    return expr != null && expr instanceof GraphResource && ((GraphResource)expr).getURI().equals(uri);
+  }
 
   /**
    * Allow newer compiled version of the stub to operate when changes

Modified: trunk/src/jar/query/java/org/mulgara/query/operation/ApplyRules.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/operation/ApplyRules.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/query/java/org/mulgara/query/operation/ApplyRules.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -15,9 +15,10 @@
 import java.rmi.RemoteException;
 
 import org.mulgara.connection.Connection;
+import org.mulgara.query.GraphExpression;
+import org.mulgara.query.GraphResource;
 import org.mulgara.query.QueryException;
 import org.mulgara.rules.InitializerException;
-import org.mulgara.rules.RulesException;
 import org.mulgara.rules.RulesRef;
 
 /**
@@ -34,7 +35,7 @@
   private final URI ruleGraph;
   
   /** The graph containing the data to apply the rules to. */
-  private final URI baseGraph;
+  private final GraphExpression baseGraph;
   
   /** The graph to put the rule productions into. */
   private final URI destGraph;
@@ -46,6 +47,16 @@
    * @param destGraph The graph to put the rule productions into.
    */
   public ApplyRules(URI ruleGraph, URI baseGraph, URI destGraph) {
+    this(ruleGraph, new GraphResource(baseGraph), destGraph);
+  }
+  
+  /**
+   * Create a new rules command.
+   * @param ruleGraph The graph containing the rules to be run.
+   * @param baseGraph The graph expression containing the data to apply the rules to.
+   * @param destGraph The graph to put the rule productions into.
+   */
+  public ApplyRules(URI ruleGraph, GraphExpression baseGraph, URI destGraph) {
     super(destGraph);
     this.ruleGraph = ruleGraph;
     this.baseGraph = baseGraph;
@@ -62,7 +73,7 @@
   /**
    * @return the baseGraph
    */
-  public URI getBaseGraph() {
+  public GraphExpression getBaseGraph() {
     return baseGraph;
   }
 
@@ -80,9 +91,8 @@
    * @throws InitializerException The rules were not structured correctly.
    * @throws QueryException Unable to read the rules.
    * @throws RemoteException There was a connectivity problem with the server.
-   * @throws RulesException There was an error with the application of the rules.
    */
-  public Object execute(Connection conn) throws RemoteException, RulesException, QueryException, InitializerException {
+  public Object execute(Connection conn) throws RemoteException, QueryException, InitializerException {
     return execute(conn, conn);
   }
 
@@ -94,15 +104,14 @@
    * @throws InitializerException The rules were not structured correctly.
    * @throws QueryException Unable to read the rules.
    * @throws RemoteException There was a connectivity problem with the server.
-   * @throws RulesException There was an error with the application of the rules.
    */
-  public Object execute(Connection conn, Connection ruleConn) throws RemoteException, RulesException, QueryException, InitializerException {
+  public Object execute(Connection conn, Connection ruleConn) throws RemoteException, QueryException, InitializerException {
     if (conn == null) throw new IllegalArgumentException("Connection may not be null");
     // get the structure from the rule model
     RulesRef rules = ruleConn.getSession().buildRules(ruleGraph, baseGraph, destGraph);
     // create apply the rules to the model
     conn.getSession().applyRules(rules);
-    return setResultMessage("Successfully applied " + ruleGraph + " to " + baseGraph + (destGraph == baseGraph ? "" : " => " + destGraph));
+    return setResultMessage("Successfully applied " + ruleGraph + " to " + baseGraph + (GraphResource.sameAs(baseGraph, destGraph) ? "" : " => " + destGraph));
   }
 
 }

Modified: trunk/src/jar/query/java/org/mulgara/rules/RuleLoaderFactory.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/rules/RuleLoaderFactory.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/query/java/org/mulgara/rules/RuleLoaderFactory.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -20,8 +20,8 @@
 import java.lang.reflect.Method;
 import java.net.URI;
 
-// Third party packages
-import org.apache.log4j.Logger;  // Apache Log4J
+import org.apache.log4j.Logger;
+import org.mulgara.query.GraphExpression;
 
 /**
  * Constructs {@link RuleLoader} instances, given a classname.
@@ -47,7 +47,7 @@
    * @param target  the destination for rule consequences.
    * @return the constructed {@link RuleLoader}
    */
-  public static RuleLoader newRuleLoader(String className, URI source, URI base, URI target) throws InitializerException {
+  public static RuleLoader newRuleLoader(String className, URI source, GraphExpression base, URI target) throws InitializerException {
 
     if (logger.isDebugEnabled()) logger.debug("Creating rule loader " + className);
 
@@ -68,7 +68,7 @@
       Method newInstanceMethod =
         ruleLoaderClass.getMethod(
           "newInstance",
-           new Class[] { URI.class, URI.class, URI.class }
+           new Class[] { URI.class, GraphExpression.class, URI.class }
         );
 
       RuleLoader ruleLoader = (RuleLoader)

Modified: trunk/src/jar/query/java/org/mulgara/server/Session.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/server/Session.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/query/java/org/mulgara/server/Session.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -306,13 +306,13 @@
    * Extract {@link Rules} from the data found in a model.
    *
    * @param ruleModel The URI of the model with the rule structure.
-   * @param baseModel The URI of the model with the base data to read.
+   * @param baseModel The graph expression with the base data to read.
    * @param destModel The URI of the model to receive the entailed data.
    * @return The extracted rule structure.
    * @throws InitializerException If there was a problem accessing the rule loading module.
    * @throws QueryException If there was a problem loading the rule structure.
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException;
+  public RulesRef buildRules(URI ruleModel, GraphExpression baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException;
 
   /**
    * Rules a set of {@link Rules} on its defined model.

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -15,17 +15,16 @@
 // Java 2 standard packages
 import java.net.URI;
 
-// Local packages
-import org.mulgara.resolver.spi.*;
+import org.apache.log4j.Logger;
+import org.mulgara.query.GraphExpression;
+import org.mulgara.resolver.spi.DatabaseMetadata;
+import org.mulgara.resolver.spi.SystemResolver;
 import org.mulgara.rules.RuleLoader;
 import org.mulgara.rules.RuleLoaderFactory;
 import org.mulgara.rules.Rules;
 import org.mulgara.rules.RulesRef;
 import org.mulgara.rules.RulesRefImpl;
 
-//Third party packages
-import org.apache.log4j.Logger;
-
 /**
  * Represents an operation to build a Rule structure for later execution
  *
@@ -44,7 +43,7 @@
   private URI ruleGraph = null;
 
   /** The graph containing the intrinsic data */
-  private URI baseGraph = null;
+  private GraphExpression baseGraph = null;
 
   /** The graph to contain the generated extrinsic data */
   private URI destGraph = null;
@@ -63,7 +62,7 @@
    * @param baseGraph The graph the rules will be run on.
    * @param destGraph The graph the rules will insert generated statements into.
    */
-  BuildRulesOperation(String ruleLoaderClassName, URI ruleGraph, URI baseGraph, URI destGraph) {
+  BuildRulesOperation(String ruleLoaderClassName, URI ruleGraph, GraphExpression baseGraph, URI destGraph) {
     this.ruleLoaderClassName = ruleLoaderClassName;
     this.ruleGraph = ruleGraph;
     this.baseGraph = baseGraph;

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -596,7 +596,7 @@
   /**
    * {@inheritDoc}
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException {
+  public RulesRef buildRules(URI ruleModel, GraphExpression baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException {
     if (logger.isDebugEnabled()) logger.debug("BUILD RULES: " + ruleModel);
 
     BuildRulesOperation operation = new BuildRulesOperation(ruleLoaderClassName, ruleModel, baseModel, destModel);

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -331,13 +331,13 @@
    * Extract {@link Rules} from the data found in a model.
    *
    * @param ruleModel The URI of the model with the rule structure.
-   * @param baseModel The URI of the model with the base data to read.
+   * @param baseModel The graph expression with the base data to read.
    * @param destModel The URI of the model to receive the entailed data.
    * @return The extracted rule structure.
    * @throws InitializerException If there was a problem accessing the rule loading module.
    * @throws QueryException If there was a problem loading the rule structure.
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException, RemoteException;
+  public RulesRef buildRules(URI ruleModel, GraphExpression baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException, RemoteException;
 
   /**
    * Rules a set of {@link Rules} on its defined model.

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -696,7 +696,7 @@
   /**
    * {@inheritDoc}
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException {
+  public RulesRef buildRules(URI ruleModel, GraphExpression baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException {
     try {
       RulesRef ref = remoteSession.buildRules(ruleModel, baseModel, destModel);
       if (logger.isDebugEnabled()) logger.debug("got rules from RMI");

Modified: trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java
===================================================================
--- trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java	2009-05-22 18:08:07 UTC (rev 1698)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java	2009-05-22 18:39:13 UTC (rev 1699)
@@ -29,18 +29,20 @@
 package org.mulgara.server.rmi;
 
 // Java 2 standard packages
-import java.io.*;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.URI;
-import java.rmi.*;
+import java.rmi.RemoteException;
 import java.rmi.server.Unreferenced;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import javax.activation.MimeType;
 
-// Third party packages
 import org.apache.log4j.Logger;
-
-// Locally written packages
 import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
 import org.mulgara.query.ArrayAnswer;
@@ -51,8 +53,7 @@
 import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
 import org.mulgara.rules.InitializerException;
-import org.mulgara.rules.Rules;  // Required only for Javadoc
-import org.mulgara.rules.RulesException;
+import org.mulgara.rules.Rules;
 import org.mulgara.rules.RulesRef;
 import org.mulgara.server.Session;
 
@@ -469,13 +470,13 @@
    * Extract {@link Rules} from the data found in a model.
    *
    * @param ruleModel The URI of the model with the rule structure.
-   * @param baseModel The URI of the model with the base data to read.
+   * @param baseModel The graph expression with the base data to read.
    * @param destModel The URI of the model to receive the entailed data.
    * @return The extracted rule structure.
    * @throws InitializerException If there was a problem accessing the rule loading module.
    * @throws QueryException If there was a problem loading the rule structure.
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException, RemoteException {
+  public RulesRef buildRules(URI ruleModel, GraphExpression baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException, RemoteException {
     RulesRef r = null;
     try {
       r = session.buildRules(ruleModel, baseModel, destModel);
@@ -493,7 +494,6 @@
    * Rules a set of {@link Rules} on its defined model.
    *
    * @param rules The rules to be run.
-   * @throws RulesException An error was encountered executing the rules.
    */ 
   public void applyRules(RulesRef rules) throws QueryException, RemoteException {
     try {




More information about the Mulgara-svn mailing list