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

pag at mulgara.org pag at mulgara.org
Thu Mar 27 02:26:02 UTC 2008


Author: pag
Date: 2008-03-26 19:26:01 -0700 (Wed, 26 Mar 2008)
New Revision: 716

Added:
   trunk/src/jar/resolver/java/org/mulgara/resolver/ApplyRulesOperation.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java
Modified:
   trunk/src/jar/krule/build.xml
   trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
   trunk/src/jar/krule/java/org/mulgara/krule/Rule.java
   trunk/src/jar/krule/java/org/mulgara/krule/RuleStructure.java
   trunk/src/jar/query/java/org/mulgara/server/Session.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
   trunk/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java
   trunk/src/jar/rules/java/org/mulgara/rules/Rules.java
   trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.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:
Updated Krule to use the new transaction framework, fixing a number of transaction bugs that it had. This needed changes in other systems for visibility, like OperationContext becoming public, and the Session method signatures for rule operations had to be made more consistent with other transactional Operations. Generics were also added to the Session implementations, since these classes were being updated already.

Modified: trunk/src/jar/krule/build.xml
===================================================================
--- trunk/src/jar/krule/build.xml	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/krule/build.xml	2008-03-27 02:26:01 UTC (rev 716)
@@ -27,6 +27,7 @@
     <pathelement path="${itql.dist.dir}/${itql.jar}"/>
     <pathelement path="${tuples.dist.dir}/${tuples.jar}"/>
     <pathelement path="${rules.dist.dir}/${rules.jar}"/>
+    <pathelement path="${resolver.dist.dir}/${resolver.jar}"/>
   </path>
 
   <path id="krule-test-classpath">
@@ -57,7 +58,7 @@
   </target>
 
   <target name="krule-compile"
-          depends="-krule-prepare, driver-jar, itql-jar, rules-jar, tuples-jar"
+          depends="-krule-prepare, driver-jar, itql-jar, rules-jar, tuples-jar, resolver-jar"
           description="Compiles all krule related files included generated source code">
 
     <javac destdir="${krule.obj.dir}/classes" debug="on"

Modified: trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java
===================================================================
--- trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/krule/java/org/mulgara/krule/KruleLoader.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -42,8 +42,8 @@
 import org.mulgara.query.rdf.LiteralImpl;
 import org.mulgara.query.rdf.TripleImpl;
 import org.mulgara.query.rdf.URIReferenceImpl;
+import org.mulgara.resolver.OperationContext;
 import org.mulgara.rules.*;
-import org.mulgara.server.*;
 
 /**
  * This object is used for parsing an RDF graph and building a rules structure
@@ -66,7 +66,7 @@
   private URI systemModel;
 
   /** The database session for querying. */
-  private Session session;
+  private OperationContext operationContext;
 
   /** The interpreter for parsing queries. */
   private TqlInterpreter interpreter;
@@ -204,7 +204,7 @@
     this.destModel = destModel;
 
     // set the query objects to null
-    session = null;
+    operationContext = null;
     interpreter = null;
 
     // initialize the aliases
@@ -231,13 +231,13 @@
   /**
    * Reads the ruleModel in the database and constructs the rules from it.
    *
-   * @param sessionParam The session for querying on.
+   * @param opContextParam The operationContext for querying on.
    * @param systemModel The system model.
    * @return A new rule structure.
    * @throws InitializerException There was a problem reading and creating the rules.
    */
-  public Rules readRules(Object sessionParam, URI systemModel) throws InitializerException, RemoteException {
-    this.session = (Session)sessionParam;
+  public Rules readRules(Object opContextParam, URI systemModel) throws InitializerException, RemoteException {
+    this.operationContext = (OperationContext)opContextParam;
     this.systemModel = systemModel;
 
     // get a new interpreter
@@ -245,7 +245,6 @@
 
     rules = null;
     try {
-      session.setAutoCommit(false);
       logger.debug("Initializing for rule queries.");
       // initialise the utility models
       initializeUtilityModels();
@@ -255,8 +254,7 @@
 
       logger.debug("Querying for rules");
       rules = findRules();
-      // set the base and target models
-      rules.setBaseModel(baseModel);
+      // set the target model
       rules.setTargetModel(destModel);
 
       // find the triggers
@@ -284,13 +282,6 @@
     } catch (Throwable t) {
       logger.error("Unexpected error during loading: " + t.getMessage(), t);
       throw new InitializerException("Unexpected error loading rules", t);
-    } finally {
-      try {
-        session.setAutoCommit(true);
-      } catch (QueryException e) {
-        logger.error("Unable to close transaction.", e);
-        throw new InitializerException("Unable to close transaction", e);
-      }
     }
 
     return rules;
@@ -337,7 +328,7 @@
       throw new QueryException("Invalid query.", e);
     }
     
-    Answer ruleAnswer = session.query(query);
+    Answer ruleAnswer = query(query);
     logger.debug("Got response for rule query");
 
     // create the rule structure for all the rules
@@ -372,7 +363,7 @@
       throw new QueryException("Invalid query.", e);
     }
 
-    Answer answer = session.query(query);
+    Answer answer = query(query);
 
     try {
       // link all the rules together
@@ -415,7 +406,7 @@
       } catch (Exception e) {
         throw new QueryException("Invalid query.", e);
       }
-      Answer answer = session.query(query);
+      Answer answer = query(query);
 
       // get the length of the sequence prefix
       int prefixLength = ((URI)aliases.get("rdf")).toString().length() + 1;
@@ -463,7 +454,7 @@
       } catch (Exception e) {
         throw new QueryException("Invalid query.", e);
       }
-      answer = session.query(query);
+      answer = query(query);
 
       try {
         // attach the correct constraint tree to the query structure
@@ -518,7 +509,7 @@
     } catch (Exception e) {
       throw new QueryException("Invalid query.", e);
     }
-    Answer answer = session.query(query);
+    Answer answer = query(query);
 
     // prepare the set of axioms
     Set<org.jrdf.graph.Triple> axioms = new HashSet<org.jrdf.graph.Triple>();
@@ -599,7 +590,7 @@
       throw new QueryException("Invalid query.", e);
     }
     
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found prefix models");
 
     try {
@@ -635,7 +626,7 @@
       throw new QueryException("Invalid query.", e);
     }
     
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all URI references.");
 
     // create the mapping
@@ -674,7 +665,7 @@
       throw new QueryException("Invalid query.", e);
     }
     
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all variable references.");
 
     // create the mapping
@@ -713,7 +704,7 @@
       throw new QueryException("Invalid query.", e);
     }
     
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all Literals.");
 
     // create the mapping
@@ -754,7 +745,7 @@
       throw new QueryException("Invalid query.", e);
     }
 
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all simple constraints.");
 
     // create a mapping of URIs to simple constraint structures
@@ -820,7 +811,7 @@
       throw new QueryException("Invalid query.", e);
     }
 
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all join constraints.");
 
     // accumulate all the constraint links and types
@@ -896,7 +887,7 @@
       throw new QueryException("Invalid query.", e);
     }
 
-    Answer answer = session.query(query);
+    Answer answer = query(query);
     logger.debug("Found all having constraints.");
 
     try {
@@ -928,7 +919,7 @@
     } catch (Exception e) {
       throw new QueryException("Invalid query.", e);
     }
-    Answer answer = session.query(query);
+    Answer answer = query(query);
 
     logger.debug("Retrieved all transitive constraints.");
 
@@ -1126,4 +1117,19 @@
     }
   }
 
+  /**
+   * Local wrapper for querying on an OperationContext. Since {@link OperationContext#doQuery(Query)}
+   * throws an {@link Exception}, this is captured and wrapped in or cast to a {@link QueryException}.
+   * @param q The query to execute.
+   * @return The Answer to the query.
+   * @throws QueryException If the query fails.
+   */
+  private Answer query(Query q) throws QueryException {
+    try {
+      return operationContext.doQuery(q);
+    } catch (Exception e) {
+      if (e instanceof QueryException) throw (QueryException)e;
+      throw new QueryException("Unable to execute query", e);
+    }
+  }
 }

Modified: trunk/src/jar/krule/java/org/mulgara/krule/Rule.java
===================================================================
--- trunk/src/jar/krule/java/org/mulgara/krule/Rule.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/krule/java/org/mulgara/krule/Rule.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -29,7 +29,6 @@
 
 // Java 2 standard packages
 import java.io.Serializable;
-import java.net.*;
 import java.util.*;
 
 // Third party packages
@@ -37,7 +36,13 @@
 
 // Locally written packages
 import org.mulgara.query.*;
-import org.mulgara.server.Session;
+import org.mulgara.resolver.OperationContext;
+import org.mulgara.resolver.spi.LocalizedTuples;
+import org.mulgara.resolver.spi.Resolver;
+import org.mulgara.resolver.spi.ResolverException;
+import org.mulgara.resolver.spi.SystemResolver;
+import org.mulgara.resolver.spi.TuplesWrapperStatements;
+import static org.mulgara.krule.RuleStructure.UNINITIALIZED;
 
 /**
  * Represents a single executable rule.
@@ -52,8 +57,8 @@
  */
 public class Rule implements Serializable {
 
-  static final long serialVersionUID = -3762458306756060166L;
-  
+  private static final long serialVersionUID = 3080424724378196982L;
+
   /** Logger.  */
   private static Logger logger = Logger.getLogger(Rule.class.getName());
 
@@ -61,16 +66,13 @@
   private String name;
 
   /** The rules to be triggered when this rule generates statements.*/
-  private Set triggerSet;
+  private Set<Rule> triggerSet;
 
   /** The query for this rule. */
   private Query query;
 
-  /** The model containing the base data. */
-  private URI baseModel;
-  
-  /** The model containing the inferred data. */
-  private URI targetModel;
+  /** The graph receiving the inferred data. */
+  private long targetGraph = UNINITIALIZED;
 
   /** The most recent size of the data matching this rule. */
   private long lastCount;
@@ -86,7 +88,7 @@
    * @param name The name of the rule.
    */
   public Rule(String name) {
-    triggerSet = new HashSet();
+    triggerSet = new HashSet<Rule>();
     this.name = name;
   }
 
@@ -112,22 +114,12 @@
 
 
   /**
-   * Set the base model for the rule.
+   * Set the target graph for the rule.
    *
-   * @param base The URI of the base data to apply rules to.
-   */
-  public void setBaseModel(URI base) {
-    baseModel = base;
-  }
-
-
-  /**
-   * Set the target model for the rule.
-   *
    * @param target The URI of the model to insert inferences into.
    */
-  public void setTargetModel(URI target) {
-    targetModel = target;
+  public void setTargetGraph(long target) {
+    targetGraph = target;
   }
 
 
@@ -156,7 +148,7 @@
    *
    * @return an immutable set of the subordinate rules.
    */
-  public Set getTriggerTargets() {
+  public Set<Rule> getTriggerTargets() {
     return Collections.unmodifiableSet(triggerSet);
   }
 
@@ -174,36 +166,49 @@
   /**
    * Runs this rule.
    * TODO: count the size of each individual constraint
+   * TODO: Go back to using a Session once they have been properly refactored for transactions
    * 
-   * @param session The session to execute the rule against.
+   * @param context The context to query against.
+   * @param resolver The resolver to add data with.
+   * @param sysResolver The resolver to localize data with.
    */
-  public void execute(Session session) throws QueryException, TuplesException {
+  public void execute(OperationContext context, Resolver resolver, SystemResolver sysResolver) throws QueryException, TuplesException, ResolverException {
+    if (targetGraph == UNINITIALIZED) throw new IllegalStateException("Target graph has not been set");
     // see if this rule needs to be run
-  	Answer answer = session.query(query);
-  	// compare the size of the result data  	
-  	long newCount = answer.getRowCount(); 
-  	if (newCount == lastCount) {
-  	  logger.debug("Rule <" + name + "> is up to date.");
-  	  // this rule does not need to be run
-  	  return;
+  	Answer answer = null;
+  	try {
+    	try {
+        answer = context.doQuery(query);
+      } catch (Exception e) {
+        throw new QueryException("Unable to access data in rule.", e);
+      }
+    	// compare the size of the result data  	
+    	long newCount = answer.getRowCount(); 
+    	if (newCount == lastCount) {
+    	  logger.debug("Rule <" + name + "> is up to date.");
+    	  // this rule does not need to be run
+    	  return;
+    	}
+    	logger.debug("Rule <" + name + "> has increased by " + (newCount - lastCount) + " entries");
+      logger.debug("Inserting results of: " + query);
+      if (answer instanceof AnswerImpl) {
+        AnswerImpl a = (AnswerImpl)answer;
+        String list = "[ ";
+        Variable[] v = a.getVariables();
+        for (int i = 0; i < v.length; i++) {
+          list += v[i] + " ";
+        }
+        list += "]";
+        logger.debug("query has " + a.getNumberOfVariables() + " variables: " + list);
+      }
+    	// insert the resulting data
+      insertData(answer, resolver, sysResolver);
+      // update the count
+      lastCount = newCount;
+      logger.debug("Insertion complete, triggering rules for scheduling.");
+  	} finally {
+  	  answer.close();
   	}
-  	logger.debug("Rule <" + name + "> has increased by " + (newCount - lastCount) + " entries");
-    logger.debug("Inserting results of: " + query);
-    if (answer instanceof AnswerImpl) {
-      AnswerImpl a = (AnswerImpl)answer;
-      String list = "[ ";
-      Variable[] v = a.getVariables();
-      for (int i = 0; i < v.length; i++) {
-        list += v[i] + " ";
-      }
-      list += "]";
-      logger.debug("query has " + a.getNumberOfVariables() + " variables: " + list);
-    }
-  	// insert the resulting data
-  	session.insert(targetModel, query);
-  	// update the count
-  	lastCount = newCount;
-  	logger.debug("Insertion complete, triggering rules for scheduling.");
   	// trigger subsequent rules
   	scheduleTriggeredRules();
   }
@@ -213,11 +218,43 @@
    * Schedule subsequent rules.
    */
   private void scheduleTriggeredRules() {
-  	Iterator it = triggerSet.iterator();
+  	Iterator<Rule> it = triggerSet.iterator();
   	while (it.hasNext()) {
-  	  Rule rule = (Rule)it.next();
-  	  ruleStruct.schedule(rule);
+  	  ruleStruct.schedule(it.next());
   	}
   }
 
+
+  /**
+   * Inserts an Answer into the data store on the current transaction.
+   * @param answer The data to be inserted.
+   * @param resolver The mechanism for adding data in the current transaction.
+   * @param sysResolver Used for localizing the globalized data in the answer parameter.
+   *        TODO: use a localized Tuples instead of Answer when src and dest are on the same server.
+   * @throws TuplesException There was an error localizing the answer.
+   * @throws ResolverException There was an error inserting the data.
+   */
+  private void insertData(Answer answer, Resolver resolver, SystemResolver sysResolver) throws TuplesException, ResolverException {
+    TuplesWrapperStatements statements = convertToStatements(answer, sysResolver);
+    try {
+      resolver.modifyModel(targetGraph, statements, true);
+    } finally {
+      statements.close();
+    }
+  }
+
+
+  /**
+   * Converts an Answer with 3 selection values to a set of statements for insertion.
+   * @param answer The answer to convert.
+   * @param resolver The resolver used for localizing the results, since Answers are globalized.
+   *        TODO: remove this round trip of local->global->local.
+   * @return A set of Statements.
+   * @throws TuplesException The statements could not be instantiated.
+   */
+  private TuplesWrapperStatements convertToStatements(Answer answer, SystemResolver resolver) throws TuplesException {
+    Variable[] vars = answer.getVariables();
+    assert vars.length == 3;
+    return new TuplesWrapperStatements(new LocalizedTuples(resolver, answer, true), vars[0], vars[1], vars[2]);
+  }
 }

Modified: trunk/src/jar/krule/java/org/mulgara/krule/RuleStructure.java
===================================================================
--- trunk/src/jar/krule/java/org/mulgara/krule/RuleStructure.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/krule/java/org/mulgara/krule/RuleStructure.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -39,8 +39,17 @@
 
 import org.apache.log4j.Logger;
 
+import org.mulgara.query.rdf.URIReferenceImpl;
 import org.mulgara.query.QueryException;
 import org.mulgara.query.TuplesException;
+import org.mulgara.resolver.OperationContext;
+import org.mulgara.resolver.spi.LocalizeException;
+import org.mulgara.resolver.spi.Resolver;
+import org.mulgara.resolver.spi.ResolverException;
+import org.mulgara.resolver.spi.ResolverSession;
+import org.mulgara.resolver.spi.Statements;
+import org.mulgara.resolver.spi.SystemResolver;
+import org.mulgara.resolver.spi.TripleSetWrapperStatements;
 import org.mulgara.rules.InitializerException;
 import org.mulgara.rules.Rules;
 import org.mulgara.rules.RulesException;
@@ -59,8 +68,11 @@
  */
 public class RuleStructure implements Rules, Serializable {
 
-  static final long serialVersionUID = 452741614702661812L;
+  private static final long serialVersionUID = 7891222973611830607L;
 
+  /** Used to indicate that a gNode is not configured */
+  static final long UNINITIALIZED = -1;
+
   /** Logger.  */
   private static Logger logger = Logger.getLogger(RuleStructure.class.getName());
 
@@ -70,11 +82,11 @@
   /** Map of rule names to the rule. */
   private Map<String,Rule> ruleMap;
 
-  /** The model containing the base data.  Current unused. */
-  private URI baseModel;
+  /** The URI of the target graph to contain the entailments. */
+  private URI targetGraphURI;
 
-  /** The terget model to contain the entailments. */
-  private URI targetModel;
+  /** The target graph to contain the entailments. */
+  private long targetGraph = UNINITIALIZED;
 
   /** The current list of rules that have to be run. */
   private LinkedHashSet<Rule> runQueue;
@@ -181,34 +193,12 @@
 
 
   /**
-   * Set the base model for the rules.
+   * Set the target model for the rules. This will get localized when the rules are run.
    *
-   * @param base The URI of the base data to apply rules to.
+   * @param target The URI of the target graph to insert inferences into.
    */
-  public void setBaseModel(URI base) {
-    baseModel = base;
-    for (Rule rule: rules) rule.setBaseModel(base);
-  }
-
-
-  /**
-   * Get the base model for the rules.  Currently unused.
-   *
-   * @param base The URI of the base data to apply rules to.
-   */
-  public URI setBaseModel() {
-    return baseModel;
-  }
-
-
-  /**
-   * Set the target model for the rules.
-   *
-   * @param target The URI of the target model to insert inferences into.
-   */
   public void setTargetModel(URI target) {
-    targetModel = target;
-    for (Rule rule: rules) rule.setTargetModel(target);
+    targetGraphURI = target;
   }
 
 
@@ -217,56 +207,46 @@
    * This means that any triggered rules are scheduled for evaluation,
    * and are not run immediately.
    *
-   * @param params the session to use.
+   * @param params An array containing the transactionally controlled {@link OperationContext}
+   *        and {@link SystemResolver} to use. This is not a structured parameter as it will
+   *        eventually drop back to a single {@link Session} parameter like it used to be. 
    */
   public void run(Object params) throws RulesException {
     logger.debug("Run called");
-    if (!(params instanceof Session)) {
-      throw new IllegalArgumentException("Rules must be run with a session");
-    }
-    Session session = (Session)params;
+    validateParams(params);
+    // set up the operating parameters
+    OperationContext context = (OperationContext)((Object[])params)[0];
+    SystemResolver systemResolver = (SystemResolver)((Object[])params)[1];
+
+    // determine the graph to insert into, and the resolver for that graph
+    localizeRuleTarget(context, systemResolver);
+    Resolver resolver = extractTargetResolver(context);
+
     // set up the run queue
     runQueue = new LinkedHashSet<Rule>(rules);
     // fill the run queue
     runQueue.addAll(rules);
     Rule currentRule = null;
     try {
-      // use a single transaction
-      session.setAutoCommit(false);
       // start by inserting the axioms
-      insertAxioms(session);
+      insertAxioms(resolver, systemResolver);
       // process the queue
       while (runQueue.size() > 0) {
         // get the first rule from the queue
         currentRule = popRunQueue();
         logger.debug("Executing rule: " + currentRule);
         // execute the rule
-        currentRule.execute(session);
+        currentRule.execute(context, resolver, systemResolver);
       }
     } catch (TuplesException te) {
       logger.error("Error getting data within rule: " + currentRule);
-      try {
-        session.rollback();
-      } catch (QueryException e) {
-        logger.error("Error during rollback: " + currentRule);
-      }
       throw new RulesException("Error getting data within rule: " + currentRule, te);
+    } catch (ResolverException re) {
+      logger.error("Error inserting data from rule: " + currentRule);
+      throw new RulesException("Error inserting data from rule: " + currentRule, re);
     } catch (QueryException qe) {
       logger.error("Error executing rule: " + currentRule, qe);
-      try {
-        session.rollback();
-      } catch (QueryException e) {
-        logger.error("Error during rollback: " + currentRule);
-      }
       throw new RulesException("Error executing rule: " + currentRule, qe);
-    } finally {
-      try {
-        // this will commit the current phase, or do nothing if we rolled back
-        session.setAutoCommit(true);
-      } catch (QueryException e) {
-        logger.error("Unable to close transaction.", e);
-        throw new RulesException("Unable to close transaction", e);
-      }
     }
     logger.debug("All rules complete");
   }
@@ -303,17 +283,79 @@
   /**
    * Inserts all axioms into the output model in the current session.
    *
-   * @param session The session to use for writing.
+   * @param resolver The resolver to use for writing.
    */
-  private void insertAxioms(Session session) throws QueryException {
+  private void insertAxioms(Resolver resolver, ResolverSession resolverSession) throws QueryException {
     logger.debug("Inserting axioms");
     // check if axioms were provided
     if (axioms == null) {
       logger.debug("No axioms provided");
       return;
     }
-    // insert the statements
-    session.insert(targetModel, axioms);
+    try {
+      // Create the statements
+      Statements stmts = new TripleSetWrapperStatements(axioms, resolverSession, TripleSetWrapperStatements.PERSIST);
+      // insert the statements
+      resolver.modifyModel(targetGraph, stmts, true);
+    } catch (TuplesException te) {
+      throw new QueryException("Unable to convert axioms for storage", te);
+    } catch (ResolverException re) {
+      throw new QueryException("Unable to store axioms", re);
+    }
   }
 
+
+  /**
+   * Calculate the localized gNode for the target graph, and update all rules with this info.
+   * @param context The context to query for the canonical form of the graph.
+   * @param sysResolver The resolver to localize the graph info on.
+   * @throws RulesException If the graph URI could not be localized.
+   */
+  private void localizeRuleTarget(OperationContext context, SystemResolver sysResolver) throws RulesException {
+    try {
+      targetGraph = sysResolver.localize(new URIReferenceImpl(targetGraphURI));
+    } catch (LocalizeException e) {
+      throw new RulesException("Unable to make a determination on the destination graph: " + targetGraphURI, e);
+    }
+    targetGraph = context.getCanonicalModel(targetGraph);
+    // tell the rules which graph they need to modify
+    for (Rule rule: rules) rule.setTargetGraph(targetGraph);
+  }
+
+
+  /**
+   * Determine the resolver to use for modifications to the target graph.
+   * @param context The context to extract the resolver from.
+   * @return A resolver associated with the current transactional context.
+   * @throws RulesException If a resolver could not be obtained.
+   */
+  private Resolver extractTargetResolver(OperationContext context) throws RulesException {
+    if (targetGraph == UNINITIALIZED) throw new IllegalStateException("Target graph has not been resolved");
+    try {
+      return context.obtainResolver(context.findModelResolverFactory(targetGraph));
+    } catch (QueryException e) {
+      throw new RulesException("Unable to get access to modify the target graph: " + targetGraphURI);
+    }
+  }
+
+
+  /**
+   * Confirm that the parameters for {@link #run(Object)} are the expected type, since
+   * they are not subject to static type checking. The complexity of this object will be
+   * reduced when it is replaced by a {@link Session} again.
+   * @param params The parameters to {@link #run(Object)}. For the moment this is a 2 element
+   *        array of Object, with elements of {@link OperationContext} and {@link SystemResolver}.
+   * @throws IllegalArgumentException If the parameters are not of the expected form.
+   */
+  private void validateParams(Object params) {
+    if (!(params instanceof Object[])) {
+      throw new IllegalArgumentException("Rules must be run with parameters of OperationContext/SystemResolver");
+    }
+    if (!(((Object[])params)[0] instanceof OperationContext)) {
+      throw new IllegalArgumentException("Rules must be run with an OperationContext");
+    }
+    if (!(((Object[])params)[1] instanceof SystemResolver)) {
+      throw new IllegalArgumentException("Rules must be run with a SystemResolver");
+    }
+  }
 }

Modified: trunk/src/jar/query/java/org/mulgara/server/Session.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/server/Session.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/query/java/org/mulgara/server/Session.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -44,7 +44,6 @@
 import org.mulgara.query.rdf.Mulgara;
 import org.mulgara.rules.InitializerException;
 import org.mulgara.rules.Rules;  // Required only for Javadoc
-import org.mulgara.rules.RulesException;
 import org.mulgara.rules.RulesRef;
 
 /**
@@ -241,15 +240,16 @@
    * @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, java.rmi.RemoteException;
+  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException;
 
   /**
    * 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.
+   * @throws QueryException An error was encountered executing the rules.
+   * @throws QueryException An error was encountered accessing the rules accross a network.
    */
-  public void applyRules(RulesRef rules) throws RulesException, java.rmi.RemoteException;
+  public void applyRules(RulesRef rules) throws QueryException, java.rmi.RemoteException;
 
   /**
    * Sets whether permanent changes made to the database in this session

Added: trunk/src/jar/resolver/java/org/mulgara/resolver/ApplyRulesOperation.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/ApplyRulesOperation.java	                        (rev 0)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/ApplyRulesOperation.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -0,0 +1,84 @@
+/*
+ * 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.resolver;
+
+// Java 2 standard packages
+import java.rmi.RemoteException;
+
+// Third party packages
+import org.apache.log4j.Logger;
+
+// Local packages
+import org.mulgara.resolver.spi.DatabaseMetadata;
+import org.mulgara.resolver.spi.SystemResolver;
+import org.mulgara.rules.Rules;
+import org.mulgara.rules.RulesRef;
+
+/**
+ * An {@link Operation} that applies a set of rules to a graph. These rules are built with
+ * the corresponding {@link BuildRulesOperation}.
+ *
+ * @created Mar 25, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+class ApplyRulesOperation implements Operation {
+
+  @SuppressWarnings("unused")
+  /** Logger. */
+  private static final Logger logger = Logger.getLogger(ApplyRulesOperation.class.getName());
+
+  /** The rules to run in this operation. */
+  Rules rules;
+
+  /**
+   * Create and set up the operation.
+   *
+   * @param rulesRef A reference to the rules that this operation will execute.
+   * @throws IllegalArgumentException if the rules are null.
+   * @throws RemoteException if the rules reference could not be accessed over a network.
+   */
+  public ApplyRulesOperation(RulesRef rulesRef) throws RemoteException {
+    if (rulesRef == null) throw new IllegalArgumentException("Illegal to use a null set of rules");
+    rules = rulesRef.getRules();
+  }
+
+  /**
+   * @see org.mulgara.resolver.Operation#execute(org.mulgara.resolver.OperationContext, org.mulgara.resolver.spi.SystemResolver, org.mulgara.resolver.spi.ResolverSessionFactory, org.mulgara.resolver.spi.DatabaseMetadata)
+   * Runs the rules over the configured data.
+   */
+  public void execute(OperationContext         operationContext,
+                      SystemResolver           systemResolver,
+                      DatabaseMetadata         metadata) throws Exception {
+    rules.run(packageParams(operationContext, systemResolver));
+  }
+
+  /**
+   * @see org.mulgara.resolver.Operation#isWriteOperation()
+   * @return <code>true</code>
+   */
+  public boolean isWriteOperation() {
+    return true;
+  }
+  
+  /**
+   * Packages up the parameters for the rules to run with.
+   * This was a Session, and will become a Session, but for the moment it is the
+   * OperationContext and SystemResolver.
+   * TODO: change this back to Session
+   */
+  private Object[] packageParams(OperationContext context, SystemResolver resolver) {
+    return new Object[] { context, resolver };
+  }
+}

Added: trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java	                        (rev 0)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/BuildRulesOperation.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -0,0 +1,103 @@
+/*
+ * 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.resolver;
+
+// Java 2 standard packages
+import java.net.URI;
+
+// Local packages
+import org.mulgara.resolver.spi.*;
+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
+ *
+ * @created Mar 25, 2008
+ * @author Paul Gearon
+ * @copyright &copy; 2007 <a href="mailto:pgearon at users.sourceforge.net">Paul Gearon</a>
+ * @licence <a href="{@docRoot}/../../LICENCE.txt">Open Software License v3.0</a>
+ */
+class BuildRulesOperation implements Operation {
+
+  @SuppressWarnings("unused")
+  /** Logger. */
+  private static final Logger logger = Logger.getLogger(BuildRulesOperation.class.getName());
+
+  /** The graph containing the rules to run */
+  private URI ruleGraph = null;
+
+  /** The graph containing the intrinsic data */
+  private URI baseGraph = null;
+
+  /** The graph to contain the generated extrinsic data */
+  private URI destGraph = null;
+
+  /** The name of the class that loads rules */
+  private String ruleLoaderClassName = null;
+
+  /** The rules structure that can be shipped over RMI */
+  private RulesRefImpl result;
+
+
+  /**
+   * Create an configure this operation.
+   * @param ruleLoaderClassName The name of the class that can load the configured rules.
+   * @param ruleGraph The graph containing the rules to read.
+   * @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) {
+    this.ruleLoaderClassName = ruleLoaderClassName;
+    this.ruleGraph = ruleGraph;
+    this.baseGraph = baseGraph;
+    this.destGraph = destGraph;
+  }
+
+  /**
+   * @see org.mulgara.resolver.Operation#execute(org.mulgara.resolver.OperationContext, org.mulgara.resolver.spi.SystemResolver, org.mulgara.resolver.spi.ResolverSessionFactory, org.mulgara.resolver.spi.DatabaseMetadata)
+   */
+  public void execute(OperationContext       operationContext,
+                      SystemResolver         systemResolver,
+                      DatabaseMetadata       metadata) throws Exception {
+    // Set up the rule parser
+    RuleLoader ruleLoader = RuleLoaderFactory.newRuleLoader(ruleLoaderClassName, ruleGraph, baseGraph, destGraph);
+    if (ruleLoader == null) throw new org.mulgara.rules.InitializerException("No rule loader available");
+  
+    // read in the rules
+    Rules rules =  ruleLoader.readRules(operationContext, metadata.getSystemModelURI());
+    result = new RulesRefImpl(rules);
+  }
+
+  /**
+   * @see org.mulgara.resolver.Operation#isWriteOperation()
+   */
+  public boolean isWriteOperation() {
+    return false;
+  }
+
+  /**
+   * Obtains the results of this operation, after the transactional work is completed.
+   * @return A reference to a set of rules.
+   */  
+  RulesRef getResult() {
+    return result;
+  }
+
+}

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/DatabaseSession.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -493,28 +493,19 @@
   /**
    * {@inheritDoc}
    */
-  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException, java.rmi.RemoteException {
-    if (logger.isInfoEnabled()) {
-      logger.info("BUILD RULES: " + ruleModel);
-    }
+  public RulesRef buildRules(URI ruleModel, URI baseModel, URI destModel) throws QueryException, org.mulgara.rules.InitializerException {
+    if (logger.isInfoEnabled()) logger.info("BUILD RULES: " + ruleModel);
 
-    // Set up the rule parser
-    RuleLoader ruleLoader = RuleLoaderFactory.newRuleLoader(ruleLoaderClassName, ruleModel, baseModel, destModel);
-    if (ruleLoader == null) {
-      throw new org.mulgara.rules.InitializerException("No rule loader available");
-    }
-
-    // read in the rules
-    Rules rules =  ruleLoader.readRules(this, metadata.getSystemModelURI());
-    return new RulesRefImpl(rules);
+    BuildRulesOperation operation = new BuildRulesOperation(ruleLoaderClassName, ruleModel, baseModel, destModel);
+    execute(operation, "Failed to create rules");
+    return operation.getResult();
   }
 
   /**
    * {@inheritDoc}
    */
-  public void applyRules(RulesRef rulesRef) throws RulesException, java.rmi.RemoteException {
-    Rules rules = rulesRef.getRules();
-    rules.run(this);
+  public void applyRules(RulesRef rulesRef) throws QueryException, java.rmi.RemoteException {
+    execute(new ApplyRulesOperation(rulesRef), "Unable to apply rules");
   }
 
 
@@ -626,8 +617,7 @@
    * @param operation  the {@link Operation} to execute
    * @throws QueryException if the <var>operation</var> fails
    */
-  private void execute(Operation operation, String errorString) throws QueryException
-  {
+  private void execute(Operation operation, String errorString) throws QueryException {
     ensureTransactionFactorySelected();
     try {
       MulgaraTransaction transaction =

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/OperationContext.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -52,8 +52,8 @@
  *   Technology, Inc</a>
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
-interface OperationContext
-{
+public interface OperationContext {
+
   /**
    * Find a {@link ResolverFactory} capable of generating a {#link Resolver} to
    * manipulate a specified model.

Modified: trunk/src/jar/rules/java/org/mulgara/rules/Rules.java
===================================================================
--- trunk/src/jar/rules/java/org/mulgara/rules/Rules.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/rules/java/org/mulgara/rules/Rules.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -47,13 +47,6 @@
   static final long serialVersionUID = -4614382685160461725L;
 
   /**
-   * Set the base model for the rules.
-   *
-   * @param base The URI of the base data to apply rules to.
-   */
-  public void setBaseModel(URI base);
-
-  /**
    * Set the target model for the rules.
    *
    * @param base The URI of the target model to insert the inferences into.

Modified: trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java
===================================================================
--- trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/server-beep/java/org/mulgara/server/beep/BEEPSession.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -535,7 +535,7 @@
  /**
   * {@inheritDoc}
   */
- public void applyRules(RulesRef rules) throws RulesException {
+ public void applyRules(RulesRef rules) {
    throw new UnsupportedOperationException("This operation is only supported on local sessions.");
  }
 

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	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSession.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -34,20 +34,13 @@
 import java.util.*;
 import java.net.*;
 
-// JRDF
-import org.jrdf.graph.*;
-
-// emory util package
-import edu.emory.mathcs.util.remote.io.*;
-
 // Locally written packages
-import org.mulgara.query.Answer;
+import org.jrdf.graph.Triple;
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
 import org.mulgara.rules.InitializerException;
 import org.mulgara.rules.Rules;  // Required only for Javadoc
-import org.mulgara.rules.RulesException;
 import org.mulgara.rules.RulesRef;
 
 
@@ -81,7 +74,7 @@
    * @throws QueryException if the insert cannot be completed.
    * @throws RemoteException EXCEPTION TO DO
    */
-  public void insert(URI modelURI, Set statements) throws QueryException,
+  public void insert(URI modelURI, Set<? extends Triple> statements) throws QueryException,
       RemoteException;
 
   /**
@@ -103,7 +96,7 @@
    * @throws QueryException if the deletion cannot be completed.
    * @throws RemoteException EXCEPTION TO DO
    */
-  public void delete(URI modelURI, Set statements) throws QueryException,
+  public void delete(URI modelURI, Set<? extends Triple> statements) throws QueryException,
       RemoteException;
 
   /**
@@ -260,13 +253,14 @@
    * Make a list of TQL query.
    *
    * @param queries A list of queries
-       * @return A List of non-<code>null</code> answers to the <var>queries</var> .
+   * @return A List of non-<code>null</code> answers to the <var>queries</var>.
    *      The position of the answer corresponds to the position of the
-   *      parameter query.
+   *      parameter query. Each element of the list is either an Answer, or a RemoteAnswer,
+   *      which do not share a common parent class, hence the list of Objects.
    * @throws QueryException if <var>query</var> can't be answered
    * @throws RemoteException if the remote connection fails
    */
-  public List query(List queries) throws QueryException, RemoteException;
+  public List<Object> query(List<Query> queries) throws QueryException, RemoteException;
 
   /**
    * Extract {@link Rules} from the data found in a model.
@@ -284,9 +278,9 @@
    * 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.
+   * @throws QueryException An error was encountered executing the rules.
    */
-  public void applyRules(RulesRef rules) throws RulesException, RemoteException;
+  public void applyRules(RulesRef rules) throws QueryException, RemoteException;
 
   /**
    * Release resources associated with this session. The session won't be usable

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	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/RemoteSessionWrapperSession.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -46,7 +46,6 @@
 import org.mulgara.query.ModelExpression;
 import org.mulgara.query.Query;
 import org.mulgara.query.QueryException;
-import org.mulgara.rules.RulesException;
 import org.mulgara.rules.RulesRef;
 import org.mulgara.server.NonRemoteSessionException;
 import org.mulgara.server.Session;
@@ -437,15 +436,16 @@
 
     try {
 
-      List<Object> remoteAnswers = (List<Object>)remoteSession.query(queries);
+      List<Object> remoteAnswers = remoteSession.query(queries);
       resetRetries();
       List<Answer> localAnswers = new ArrayList<Answer>(remoteAnswers.size());
 
       for (Object ans: remoteAnswers) {
-        if (!(ans instanceof RemoteAnswer)) {
+        if (!(ans instanceof RemoteAnswer) && !(ans instanceof Answer)) {
           throw new QueryException("Non-answer returned from query.");
         }
-        localAnswers.add(new RemoteAnswerWrapperAnswer((RemoteAnswer)ans));
+        if (ans instanceof RemoteAnswer) localAnswers.add(new RemoteAnswerWrapperAnswer((RemoteAnswer)ans));
+        else localAnswers.add((Answer)ans);
       }
 
       return localAnswers;
@@ -604,11 +604,11 @@
   /**
    * {@inheritDoc}
    */
-  public void applyRules(RulesRef rules) throws RulesException {
+  public void applyRules(RulesRef rules) throws QueryException {
     try {
       remoteSession.applyRules(rules);
     } catch (RemoteException re) {
-      throw new RulesException("Java RMI reconnection failure", re);
+      throw new QueryException("Java RMI reconnection failure", re);
     }
   }
 

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	2008-03-27 02:20:53 UTC (rev 715)
+++ trunk/src/jar/server-rmi/java/org/mulgara/server/rmi/SessionWrapperRemoteSession.java	2008-03-27 02:26:01 UTC (rev 716)
@@ -32,14 +32,13 @@
 import java.io.*;
 import java.net.URI;
 import java.rmi.*;
-import java.rmi.server.UnicastRemoteObject;
 import java.util.*;
 
 // Third party packages
-import org.jrdf.graph.*;
 import org.apache.log4j.Logger;
 
 // Locally written packages
+import org.jrdf.graph.Triple;
 import org.mulgara.query.Answer;
 import org.mulgara.query.ArrayAnswer;
 import org.mulgara.query.ModelExpression;
@@ -58,28 +57,19 @@
  * {@link RemoteSession}.
  *
  * @author <a href="http://staff.pisoftware.com/raboczi">Simon Raboczi</a>
- *
  * @created 2002-01-03
- *
- * @version $Revision: 1.11 $
- *
- * @modified $Date: 2005/06/26 12:48:16 $ by $Author: pgearon $
- *
  * @company <A href="mailto:info at PIsoftware.com">Plugged In Software</A>
- *
  * @copyright &copy; 2002-2003 <A href="http://www.PIsoftware.com/">Plugged In
  *      Software Pty Ltd</A>
- *
  * @licence <a href="{@docRoot}/../../LICENCE">Mozilla Public License v1.1</a>
  */
 class SessionWrapperRemoteSession implements RemoteSession {
 
+  @SuppressWarnings("unused")
   /** Logger.  */
   private static final Logger logger = Logger.getLogger(SessionWrapperRemoteSession.class.getName());
 
-  /**
-   * The wrapped {@link Session}
-   */
+  /** The wrapped {@link Session} */
   private final Session session;
 
   //
@@ -93,11 +83,8 @@
   protected SessionWrapperRemoteSession(Session session) {
 
     // Validate "session" parameter
-    if (session == null) {
+    if (session == null) throw new IllegalArgumentException("Null \"session\" parameter");
 
-      throw new IllegalArgumentException("Null \"session\" parameter");
-    }
-
     // Initialize fields
     this.session = session;
   }
@@ -111,12 +98,10 @@
    * @throws QueryException EXCEPTION TO DO
    * @throws RemoteException EXCEPTION TO DO
    */
-  public long setModel(URI uri,
-      ModelExpression modelExpression) throws QueryException, RemoteException {
+  public long setModel(URI uri, ModelExpression modelExpression) throws QueryException, RemoteException {
     try {
       return session.setModel(uri, modelExpression);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -130,14 +115,10 @@
    * @return The number of statements inserted into the model
    * @throws QueryException if the model can't be modified
    */
-  public long setModel(InputStream inputStream, URI uri,
-                       ModelExpression modelExpression)
-    throws QueryException {
-
+  public long setModel(InputStream inputStream, URI uri, ModelExpression modelExpression) throws QueryException {
     try {
       return session.setModel(inputStream, uri, modelExpression);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -150,13 +131,10 @@
    * @throws QueryException EXCEPTION TO DO
    * @throws RemoteException EXCEPTION TO DO
    */
-  public void setAutoCommit(boolean autoCommit) throws QueryException,
-      RemoteException {
-
+  public void setAutoCommit(boolean autoCommit) throws QueryException, RemoteException {
     try {
       session.setAutoCommit(autoCommit);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -165,46 +143,34 @@
   // Methods implementing the RemoteSession interface
   //
 
-  public void insert(URI modelURI, Set statements) throws QueryException,
-      RemoteException {
-
+  public void insert(URI modelURI, Set<? extends Triple> statements) throws QueryException, RemoteException {
     try {
       session.insert(modelURI, statements);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
-  public void insert(URI modelURI, Query query) throws QueryException,
-      RemoteException {
-
+  public void insert(URI modelURI, Query query) throws QueryException, RemoteException {
     try {
       session.insert(modelURI, query);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
-  public void delete(URI modelURI, Set statements) throws QueryException,
-      RemoteException {
-
+  public void delete(URI modelURI, Set<? extends Triple> statements) throws QueryException, RemoteException {
     try {
       session.delete(modelURI, statements);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
-  public void delete(URI modelURI, Query query) throws QueryException,
-      RemoteException {
-
+  public void delete(URI modelURI, Query query) throws QueryException, RemoteException {
     try {
       session.delete(modelURI, query);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -217,13 +183,10 @@
    * @param destinationURI The URI of the file to backup into.
    * @throws QueryException if the backup cannot be completed.
    */
-  public void backup(URI sourceURI, URI destinationURI) throws QueryException,
-      RemoteException {
-
+  public void backup(URI sourceURI, URI destinationURI) throws QueryException, RemoteException {
     try {
       session.backup(sourceURI, destinationURI);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -236,13 +199,10 @@
    * @param outputStream The stream to receive the contents
    * @throws QueryException if the backup cannot be completed.
    */
-  public void backup(URI sourceURI, OutputStream outputStream)
-    throws QueryException, RemoteException {
-
+  public void backup(URI sourceURI, OutputStream outputStream) throws QueryException, RemoteException {
     try {
       session.backup(sourceURI, outputStream);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -256,13 +216,10 @@
    * @param sourceURI The URI of the backup file to restore from.
    * @throws QueryException if the restore cannot be completed.
    */
-  public void restore(URI serverURI, URI sourceURI) throws QueryException,
-      RemoteException {
-
+  public void restore(URI serverURI, URI sourceURI) throws QueryException, RemoteException {
     try {
       session.restore(serverURI, sourceURI);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -278,92 +235,85 @@
    * @param sourceURI The URI of the backup file to restore from.
    * @throws QueryException if the restore cannot be completed.
    */
-  public void restore(InputStream inputStream, URI serverURI, URI sourceURI)
-      throws QueryException, RemoteException {
-
+  public void restore(InputStream inputStream, URI serverURI, URI sourceURI) throws QueryException, RemoteException {
     try {
       session.restore(inputStream, serverURI, sourceURI);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
-
   }
 
 
   /**
-   * METHOD TO DO
+   * Adds a new graph to the SystemGraph set.
    *
-   * @param modelURI PARAMETER TO DO
-   * @param modelTypeURI PARAMETER TO DO
-   * @throws QueryException EXCEPTION TO DO
-   * @throws RemoteException EXCEPTION TO DO
+   * @param graphURI The URI of the graph to create.
+   * @param graphTypeURI The URI of the type for the new graph.
+   * @throws QueryException Unable to create the new graph.
+   * @throws RemoteException Network error attempting to create the new graph.
    */
-  public void createModel(URI modelURI, URI modelTypeURI) throws QueryException,
-      RemoteException {
-
+  public void createModel(URI modelURI, URI modelTypeURI) throws QueryException, RemoteException {
     try {
       session.createModel(modelURI, modelTypeURI);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
   /**
-   * METHOD TO DO
+   * Remove a graph and all its statements.
    *
-   * @param uri PARAMETER TO DO
-   * @throws QueryException EXCEPTION TO DO
-   * @throws RemoteException EXCEPTION TO DO
+   * @param uri The URI of the graph.
+   * @throws QueryException Unable to remove the graph.
+   * @throws RemoteException Network error attempting to remove the new graph.
    */
   public void removeModel(URI uri) throws QueryException, RemoteException {
-
     try {
       session.removeModel(uri);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
+  /**
+   * Tests if a graph exists.
+   * @param uri the URI of the graph.
+   * @see org.mulgara.server.rmi.RemoteSession#modelExists(java.net.URI)
+   */
   public boolean modelExists(URI uri) throws QueryException, RemoteException {
     try {
       return session.modelExists(uri);
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
   /**
-   * METHOD TO DO
+   * Commits a transaction on this session.
+   * NOTE: This is not for general use. Use the transaction API.
    *
-   * @throws QueryException EXCEPTION TO DO
-   * @throws RemoteException EXCEPTION TO DO
+   * @throws QueryException Unable to commit the transaction.
+   * @throws RemoteException There was a network error.
    */
   public void commit() throws QueryException, RemoteException {
-
     try {
       session.commit();
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
 
   /**
-   * METHOD TO DO
+   * Rolls back a transaction on this session.
+   * NOTE: This is not for general use. Use the transaction API.
    *
-   * @throws QueryException EXCEPTION TO DO
-   * @throws RemoteException EXCEPTION TO DO
+   * @throws QueryException Unable to roll back the transaction.
+   * @throws RemoteException There was a network error.
    */
   public void rollback() throws QueryException, RemoteException {
-
     try {
       session.rollback();
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -386,16 +336,13 @@
               ArrayAnswer(ans));
           ans.close();
           return serialAnswer;
-        }
-        else {
+        } else {
           return new AnswerWrapperRemoteAnswer(ans);
         }
-      }
-      catch (TuplesException e) {
+      } catch (TuplesException e) {
         throw new QueryException("Error getting information for answer", e);
       }
-    }
-    catch (Throwable t) {
+    } catch (Throwable t) {
       throw convertToQueryException(t);
     }
   }
@@ -409,31 +356,27 @@
    * @throws QueryException There was an exception on one of the queries, or a query returned a non-Answer.
    * @throws RemoteException Thrown when there is a network error.
    */
-  public List query(List queries) throws QueryException, RemoteException {
+  public List<Object> query(List<Query> queries) throws QueryException, RemoteException {
 
     try {
-      List localAnswers = session.query(queries);
-      List servedAnswers = new ArrayList(localAnswers.size());
+      List<Answer> localAnswers = session.query(queries);
+      List<Object> servedAnswers = new ArrayList<Object>(localAnswers.size());
 
-      Iterator i = localAnswers.iterator();
+      Iterator<Answer> i = localAnswers.iterator();
       while (i.hasNext()) {
         Object servedAnswer = null;
-        Object ans = i.next();
-        if (!(ans instanceof Answer))
-          throw new QueryException("Non-answer returned from query.");
+        Answer ans = i.next();
         try {
-          if (((Answer) ans).getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
+          if (ans.getRowUpperBound() <= RemoteAnswer.MARSHALL_SIZE_LIMIT) {
             // don't need to wrap this in an
             // AnswerWrapperRemoteAnswerSerialised as the other end can handle
             // any kind of object as it comes out of the list
-            servedAnswer = new ArrayAnswer((Answer) ans);
+            servedAnswer = new ArrayAnswer(ans);
+          } else {
+            servedAnswer = new AnswerWrapperRemoteAnswer(ans);
           }
-          else {
-            servedAnswer = new AnswerWrapperRemoteAnswer((Answer) ans);
-          }
-          ((Answer) ans).close();
-        }
-        catch (TuplesException e) {
+          ans.close();
+        } catch (TuplesException e) {
           throw new QueryException("Error getting information for answer", e);
         }
         servedAnswers.add(servedAnswer);
@@ -476,13 +419,13 @@
    * @param rules The rules to be run.
    * @throws RulesException An error was encountered executing the rules.
    */ 
-  public void applyRules(RulesRef rules) throws RulesException, RemoteException {
+  public void applyRules(RulesRef rules) throws QueryException, RemoteException {
     try {
       session.applyRules(rules);
-    } catch (RulesException re) {
+    } catch (QueryException re) {
       throw re;
     } catch (Throwable t) {
-      throw new RulesException(t.toString(), t);
+      throw new QueryException(t.toString(), t);
     }
   }
 
@@ -538,7 +481,7 @@
   protected Throwable mapThrowable(Throwable t) {
     Throwable cause = t.getCause();
     Throwable mappedCause = cause != null ? mapThrowable(cause) : null;
-    Class tClass = t.getClass();
+    Class<? extends Throwable> tClass = t.getClass();
     String tClassName = tClass.getName();
 
     if (




More information about the Mulgara-svn mailing list