[Mulgara-svn] r351 - branches/nw-interface/src/jar/itql/java/org/mulgara/itql

pag at mulgara.org pag at mulgara.org
Sat Aug 18 18:34:05 UTC 2007


Author: pag
Date: 2007-08-18 13:34:03 -0500 (Sat, 18 Aug 2007)
New Revision: 351

Modified:
   branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlInterpreter.java
Log:
Continuing to convert to an AST builder.  All the removed code will appear in an "Executor" class of some description.

Modified: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlInterpreter.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlInterpreter.java	2007-08-18 18:32:56 UTC (rev 350)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/TqlInterpreter.java	2007-08-18 18:34:03 UTC (rev 351)
@@ -59,8 +59,13 @@
 import org.mulgara.query.ast.CommandAst;
 import org.mulgara.query.ast.Commit;
 import org.mulgara.query.ast.CreateGraph;
+import org.mulgara.query.ast.Deletion;
 import org.mulgara.query.ast.DropGraph;
+import org.mulgara.query.ast.ExecuteScript;
 import org.mulgara.query.ast.Help;
+import org.mulgara.query.ast.Insertion;
+import org.mulgara.query.ast.Load;
+import org.mulgara.query.ast.Modification;
 import org.mulgara.query.ast.Quit;
 import org.mulgara.query.ast.Rollback;
 import org.mulgara.query.ast.SetUser;
@@ -71,6 +76,7 @@
 import org.mulgara.server.SessionFactory;
 import org.mulgara.server.driver.SessionFactoryFinder;
 import org.mulgara.server.driver.SessionFactoryFinderException;
+import org.mulgara.util.Reflect;
 
 // emory util package
 import edu.emory.mathcs.util.remote.io.*;
@@ -516,250 +522,38 @@
    */
   public void outALoadCommand(ALoadCommand node) {
 
-    this.setLastError(null);
-    this.setLastAnswer(null);
-    this.setLastMessage("");
-
-    RemoteInputStream remoteInputStream = null;
-    RemoteInputStreamSrvImpl srv = null;
-
     // log the command
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Processing load command " + node);
 
-      logger.debug("Processing load command " + node);
-    }
-
     // get constituents of the load command
     URI sourceURI = toURI(node.getSource());
     URI destinationURI = toURI(node.getDestination());
 
-    try {
-
-      long stmtCount = 0;
-
-      // update the session
-      this.updateSession(new ModelResource(destinationURI));
-
-
-      // are we loading the file locally from the client?
-      if ( node.getLocality() != null &&
-           (node.getLocality() instanceof ALocalLocality) ) {
-
-        if ( logger.isInfoEnabled() ) {
-          logger.info("loading local resource : " + sourceURI );
-        }
-
-        try {
-
-          //open an InputStream
-          InputStream inputStream = sourceURI.toURL().openStream();
-
-          //is the file/stream compressed?
-          String path = (sourceURI == null) ? "" : sourceURI.toString();
-          inputStream = adjustForCompression(path, inputStream);
-
-          // open and wrap the inputstream
-          srv = new RemoteInputStreamSrvImpl(inputStream);
-
-          // prepare it for exporting
-          UnicastRemoteObject.exportObject(srv);
-
-          remoteInputStream = new RemoteInputStream(srv);
-
-          // modify the database
-          stmtCount =
-              this.getSession().setModel(remoteInputStream,
-                                         destinationURI, // model to redefine
-                                         new ModelResource(sourceURI));
-        }
-        catch (IOException ex) {
-          logger.error("Error attempting to load : " + sourceURI, ex);
-          throw new QueryException("Error attempting to load : " + sourceURI, ex);
-        } finally {
-          if ( srv != null ) {
-            try {
-              UnicastRemoteObject.unexportObject(srv, false);
-            } catch ( NoSuchObjectException ex ) {};
-          }
-        }
-      }
-      else {
-
-        if ( logger.isInfoEnabled() ) {
-          logger.info("loading remote resource : " + sourceURI );
-        }
-
-        // modify the database using a remote file located on the server
-        // default behaviour
-
-        stmtCount =
-            this.getSession().setModel(destinationURI, // model to redefine
-                                       new ModelResource(sourceURI));
-      }
-      update();
-
-      // log that we've loaded the contents of the file
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Loaded " + stmtCount + " statements from " + sourceURI +
-                     " into " + destinationURI);
-      }
-
-      // tell the user
-      if (stmtCount > 0L) {
-
-        this.setLastMessage("Successfully loaded " + stmtCount +
-                            " statements from " + sourceURI + " into " +
-                            destinationURI);
-      }
-      else {
-
-        this.setLastMessage("WARNING: No valid RDF statements found in " +
-                            sourceURI);
-      } // end if
-    }
-    catch (QueryException qe) {
-
-      // let the user know the problem
-      this.setLastError(qe);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not load " + sourceURI + " into " +
-                          destinationURI + EOL + this.getCause(qe, 2));
-      logger.warn("Failed to load " + sourceURI + " into " + destinationURI,
-                  qe);
-    }
-    catch (RuntimeException re) {
-
-      // let the user know the problem
-      this.setLastError(re);
-      this.setLastAnswer(null);
-      this.setLastMessage("Failed to load statements:" + this.getCause(re, 0));
-      logger.fatal("Failed to load statements", re);
-    }
-    finally {
-
-      // close the inputstream in-case the server was not able to
-      // complete the task.
-      if ( remoteInputStream != null ) {
-        try {
-          remoteInputStream.close();
-        } catch ( Exception ex ) {};
-      }
-
-    }
-    // try-catch
-
+    lastCommand = new Load(sourceURI, destinationURI);
   }
 
-  // outALoadCommand()
-
   /**
-   * Executes an iTQL script.
+   * Executes a TQL script.
    *
    * @param node the execute command
    */
   public void outAExecuteCommand(AExecuteCommand node) {
 
-    this.setLastError(null);
-    this.setLastAnswer(null);
-    this.setLastMessage("");
-
     // log the command
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Processing execute command " + node);
 
-      logger.debug("Processing execute command " + node);
-    }
-
     // get the name of the script to execute
     String resource = node.getResource().getText();
 
-    // keep a record of the line number
-    int line = 0;
-
     try {
-
-      // convert the script to a URL
-      URL scriptURL = new URL(resource);
-
-      // log that we're executing a script
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Executing  script " + scriptURL);
-      }
-
-      // create a buffer to hold the results in
-      StringBuffer resultsMsg = new StringBuffer();
-
-      // create a reader to read the contents of the script
-      BufferedReader scriptIn =
-          new BufferedReader(new InputStreamReader(scriptURL.openStream()));
-
-      // execute the script!
-      String command = scriptIn.readLine();
-
-      while (command != null) {
-
-        // increment the line number
-        line++;
-
-        if (!command.equals("")) {
-
-          // execute the command
-          executeCommand(command);
-        }
-
-        // end if
-        // get the next command
-        command = scriptIn.readLine();
-      }
-
-      // end if
-      // tell the user
-      resultsMsg.append("Completed execution of script " + resource);
-      this.setLastMessage(resultsMsg.toString());
-    }
-    catch (ParserException pe) {
-
+      lastCommand = new ExecuteScript(new URL(resource));
+    } catch (MalformedURLException mue) {
       // let the user know the problem
-      this.setLastError(pe);
-      this.setLastAnswer(null);
-      this.setLastMessage("Syntax error in script (line " + line + "): " +
-                          pe.getMessage());
-      logger.warn("Unable to execute script - " + resource + EOL +
-                  this.getCause(pe, 0));
+      logger.warn("Invalid script source URL: " + resource);
+      lastError = mue;
     }
-    catch (LexerException le) {
-
-      // let the user know the problem
-      this.setLastError(le);
-      this.setLastAnswer(null);
-      this.setLastMessage("Syntax error in script (line " + line + "): " +
-                          le.getMessage());
-      logger.warn("Unable to execute script - " + resource + EOL +
-                  this.getCause(le, 0));
-    }
-    catch (MalformedURLException mue) {
-
-      // let the user know the problem
-      this.setLastError(mue);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not execute script: Invalid script URL.");
-      logger.warn("Invalid script source URL." + EOL + this.getCause(mue, 0));
-    }
-    catch (Exception e) {
-
-      // let the user know the problem
-      this.setLastError(e);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not execute script." + EOL +
-                          this.getCause(e, 0));
-      logger.error("Unable to execute script - " + resource + EOL +
-                   this.getCause(e, 0));
-    }
-    // try-catch
   }
 
-  // outAExecuteCommand()
 
   /**
    * Inserts a triple, model, database or the results of a query into a model or
@@ -768,76 +562,14 @@
    * @param node the insert command
    */
   public void outAInsertCommand(AInsertCommand node) {
-
-    this.setLastError(null);
-    this.setLastAnswer(null);
-    this.setLastMessage("");
-
     // log the command
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Processing insert command " + node);
 
-      logger.debug("Processing insert command " + node);
-    }
-
     // get the resource we're inserting data into
-    URI resourceURI = toURI(node.getResource());
+    URI graphURI = toURI(node.getResource());
 
-    try {
-
-      // log that we're inserting the statments
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Inserting statements into " + resourceURI);
-      }
-
-      // update the session
-      this.updateSession(new ModelResource(resourceURI));
-
-      // insert the statements into the model
-      insertStatements(resourceURI, node.getTripleFactor());
-      update();
-
-      // log that we've inserted the statments
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Completed inserting statements into " + resourceURI);
-      }
-
-      // tell the user
-      this.setLastMessage(
-          "Successfully inserted statements into " + resourceURI
-          );
-    }
-    catch (QueryException qe) {
-
-      // let the user know the problem
-      this.setLastError(qe);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not insert statements into " + resourceURI +
-                          EOL + this.getCause(qe, 2));
-      logger.warn("Failed to insert statements into " + resourceURI, qe);
-    }
-    catch (URISyntaxException use) {
-
-      // let the user know the problem
-      this.setLastError(use);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not insert into resource: Invalid resource " +
-                          "URI.");
-      logger.warn("Invalid resource URI." + EOL + this.getCause(use, 0));
-    }
-    catch (RuntimeException re) {
-
-      // let the user know the problem
-      this.setLastError(re);
-      this.setLastAnswer(null);
-      this.setLastMessage("Failed to insert statements:" + this.getCause(re, 0));
-      logger.fatal("Failed to insert statements", re);
-    }
+    lastCommand = buildModification(graphURI, node.getTripleFactor(), Insertion.class);
   }
-
-  // outAInsertCommand()
-
   /**
    * Deletes a triple, model, database or the results of a query from a model or
    * database.
@@ -846,76 +578,15 @@
    */
   public void outADeleteCommand(ADeleteCommand node) {
 
-    this.setLastError(null);
-    this.setLastAnswer(null);
-    this.setLastMessage("");
-
     // log the command
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Processing delete command " + node);
 
-      logger.debug("Processing delete command " + node);
-    }
+    // get the resource we're inserting data into
+    URI graphURI = toURI(node.getResource());
 
-    // get the resource we're deleting data from
-    URI resourceURI = toURI(node.getResource());
-
-    try {
-
-      // log that we're deleting the statments
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Deleting statements from " + resourceURI);
-      }
-
-      // update the session
-      this.updateSession(new ModelResource(resourceURI));
-
-      // delete the statements from the model
-      deleteStatements(resourceURI, node.getTripleFactor());
-      update();
-
-      // log that we've inserted the statments
-      if (logger.isDebugEnabled()) {
-
-        logger.debug("Completed deleting statements from " + resourceURI);
-      }
-
-      // tell the user
-      this.setLastMessage(
-          "Successfully deleted statements from " + resourceURI
-          );
-    }
-    catch (QueryException qe) {
-
-      // let the user know the problem
-      this.setLastError(qe);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not delete statements from " + resourceURI +
-                          EOL + this.getCause(qe, 2));
-      logger.warn("Failed to delete statements from " + resourceURI, qe);
-    }
-    catch (URISyntaxException use) {
-
-      // let the user know the problem
-      this.setLastError(use);
-      this.setLastAnswer(null);
-      this.setLastMessage("Could not delete from resource: Invalid resource " +
-                          "URI.");
-      logger.warn("Invalid resource URI." + EOL + this.getCause(use, 0));
-    }
-    catch (RuntimeException re) {
-
-      // let the user know the problem
-      this.setLastError(re);
-      this.setLastAnswer(null);
-      this.setLastMessage("Failed to delete statements:" + this.getCause(re, 0));
-      logger.fatal("Failed to delete statements", re);
-    }
-
-    // try-catch
+    lastCommand = buildModification(graphURI, node.getTripleFactor(), Deletion.class);
   }
 
-  // outADeleteCommand()
 
   /**
    * Sets an interpreter property.
@@ -924,38 +595,18 @@
    */
   public void outASetCommand(ASetCommand node) {
 
-    this.setLastError(null);
-    this.setLastAnswer(null);
-    this.setLastMessage("");
-
     // log the command
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Processing set command " + node);
 
-      logger.debug("Processing set command " + node);
-    }
-
     // get the option to set
     PSetOption option = node.getSetOption();
 
     // log that we've got the option
-    if (logger.isDebugEnabled()) {
+    if (logger.isDebugEnabled()) logger.debug("Found option " + option);
 
-      logger.debug("Found option " + option);
-    }
-
     // get the value
-    PSetOptionMode optionMode = node.getSetOptionMode();
-    boolean optionSet = false;
+    boolean optionSet = !(node.getSetOptionMode() instanceof AOffSetOptionMode);
 
-    if (optionMode instanceof AOffSetOptionMode) {
-
-      optionSet = false;
-    }
-    else {
-
-      optionSet = true;
-    } // end if
-
     // set the option
     if (option instanceof ATimeSetOption) {
 
@@ -1948,6 +1599,43 @@
   
   
   /**
+   * @param graphURI
+   * @param tripleFactor
+   */
+  private Modification buildModification(URI graphURI, PTripleFactor tripleFactor, Class<? extends Modification> clazz) {
+    // get the set of triples out of the factor
+    PSetOfTriples setOfTriples = null;
+    if (tripleFactor instanceof ABracedTripleFactor) {
+      setOfTriples = ((ABracedTripleFactor)tripleFactor).getSetOfTriples();
+    } else if (tripleFactor instanceof AUnbracedTripleFactor) {
+      setOfTriples = ((AUnbracedTripleFactor)tripleFactor).getSetOfTriples();
+    } else throw new RuntimeException("Unhandled Grammar Exception: Unknown type of triple factor: " + tripleFactor.getClass().getName());
+
+    try {
+      if (setOfTriples instanceof AResourceSetOfTriples) {
+        throw new UnsupportedOperationException("No support for direct model to model insertion.");
+      } else if (setOfTriples instanceof ASelectSetOfTriples) {
+        // This is an INSERT/SELECT
+        // build the query
+        Query query = this.buildQuery((ASelectSetOfTriples)setOfTriples);
+        if (logger.isDebugEnabled()) logger.debug("Insert query " + query);
+        return Reflect.newInstance(clazz, graphURI, query);
+      } else if (setOfTriples instanceof ATripleSetOfTriples) {
+        Set<Triple> statements = getStatements((ATripleSetOfTriples)setOfTriples, new HashMap<String,VariableNodeImpl>());
+        return Reflect.newInstance(clazz, graphURI, statements);
+      }
+    } catch (URISyntaxException ue) {
+      logger.warn("Invalid URL in the insertion data: " + ue.getMessage());
+      lastError = ue;
+    } catch (QueryException qe) {
+      logger.warn("Bad query for insertion: " + qe.getMessage());
+      lastError = qe;
+    }
+    return null;
+  }
+
+
+  /**
    * Notify that the current session has been updated.
    */
   private void update() {




More information about the Mulgara-svn mailing list