[Mulgara-svn] r857 - in trunk: . tools tools/src tools/src/org tools/src/org/mulgara tools/src/org/mulgara/tools
pag at mulgara.org
pag at mulgara.org
Sat Apr 26 02:48:47 UTC 2008
Author: pag
Date: 2008-04-25 19:48:46 -0700 (Fri, 25 Apr 2008)
New Revision: 857
Added:
trunk/tools/
trunk/tools/README.txt
trunk/tools/src/
trunk/tools/src/org/
trunk/tools/src/org/mulgara/
trunk/tools/src/org/mulgara/tools/
trunk/tools/src/org/mulgara/tools/Sparql.java
Log:
Added some example code that is also a useful tool for working with SPARQL
Added: trunk/tools/README.txt
===================================================================
--- trunk/tools/README.txt (rev 0)
+++ trunk/tools/README.txt 2008-04-26 02:48:46 UTC (rev 857)
@@ -0,0 +1,14 @@
+This is a space for creating tools that manipulate Mulgara datastores, and communicate with them.
+
+There is no build script here yet.
+
+To compile the Sparql example on a Unix-style system, use:
+
+javac -cp ../dist/itql-1.2.1.jar:src src/org/mulgara/tools/Sparql.java
+
+
+
+The command can then be run with:
+
+java -cp ../dist/itql-1.2.1.jar:src org.mulgara.tools.Sparql
+
Added: trunk/tools/src/org/mulgara/tools/Sparql.java
===================================================================
--- trunk/tools/src/org/mulgara/tools/Sparql.java (rev 0)
+++ trunk/tools/src/org/mulgara/tools/Sparql.java 2008-04-26 02:48:46 UTC (rev 857)
@@ -0,0 +1,127 @@
+package org.mulgara.tools;
+
+import org.mulgara.sparql.*;
+import org.mulgara.connection.*;
+import org.mulgara.parser.Interpreter;
+import org.mulgara.query.*;
+import org.mulgara.query.operation.*;
+
+import java.net.URI;
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+
+/**
+ * This is a simple class that demonstrates using the SPARQL interface. This class creates
+ * a connection to a default server on the local host, and issues a SPARQL query against
+ * it. The output is simple text.
+ *
+ * Usage: java org.mulgara.tools.Sparql [queryfile ...]
+ *
+ * The query files given on the command line each contain a SPARQL query. If no query files
+ * are given, then the query is parsed from stdin.
+ */
+public class Sparql {
+
+ /**
+ * A URI for the default server on the local host. This is a simple example, so the
+ * connection to the host is not configurable.
+ */
+ private static final URI HOST = URI.create("rmi://localhost/server1");
+
+ /**
+ * Run a set of SPARQL queries against a local server.
+ * @param args A list of filenames containing the queries to run.
+ * @throws Exception Any kind of exception is just displayed on stderr, without any handling.
+ */
+ public static void main(String[] args) throws Exception {
+ // Connect to the server
+ ConnectionFactory factory = new ConnectionFactory();
+ Connection conn = factory.newConnection(HOST);
+
+ // get the list of queries
+ Iterator<String> queryStrings;
+ if (args.length == 0) queryStrings = getStdIn();
+ else queryStrings = getFiles(args);
+
+ // iterate over all the query strings
+ while (queryStrings.hasNext()) {
+ // parse the string into a Query object
+ Query query = new SparqlInterpreter().parseQuery(queryStrings.next());
+
+ // execute the query, and get back the answer
+ Answer a = (Answer)conn.execute(query);
+ // print the results
+ System.out.println("Result: " + query.getResultMessage());
+ printAnswer(a);
+ a.close();
+
+ System.out.println("---");
+ }
+
+ // clean up the server connection
+ conn.close();
+ }
+
+ /**
+ * Iterate through an Answer and print each of the lines to STDOUT.
+ * @param a The answer to print.
+ */
+ private static void printAnswer(Answer a) throws Exception {
+ System.out.println("Showing " + a.getRowCount() + " results");
+ int width = a.getNumberOfVariables();
+ a.beforeFirst();
+ while (a.next()) {
+ for (int c = 0; c < width; c++) System.out.print(toString(a.getObject(c)) + " ");
+ System.out.println();
+ }
+ }
+
+ /**
+ * Convert any parameter to a string, or "<null>" if no parameter is provided.
+ * @param The object to print. May be <code>null</code>.
+ * @return A string representation of o, or "<null>" if o is <code>null</code>.
+ */
+ private static String toString(Object o) {
+ return (o == null) ? "<null>" : o.toString();
+ }
+
+ /**
+ * Get a String iterator that returns the single string formed from STDIN.
+ * @return An iterator for a singleton list that contains the contents of STDIN.
+ */
+ private static Iterator<String> getStdIn() throws IOException {
+ return Collections.singletonList(loadToString(new InputStreamReader(System.in))).iterator();
+ }
+
+ /**
+ * Get a String iterator that returns the contents of a list of files.
+ * @param args A list of file names (absolute or relative) that contains text to read.
+ * @return An iterator for strings that contain the context of the files from args.
+ */
+ private static Iterator<String> getFiles(String[] args) throws IOException {
+ LinkedList<String> fileData = new LinkedList<String>();
+ for (String f: args) fileData.add(loadToString(new FileReader(f)));
+ return fileData.iterator();
+ }
+
+ /**
+ * Reads the contents of a {@link java.io.Reader} into a string.
+ * @param reader A Reader to be read to completion.
+ * @return A string containing everything that could be retrieved from reader.
+ */
+ private static String loadToString(Reader reader) throws IOException {
+ BufferedReader br = new BufferedReader(reader);
+ StringBuffer data = new StringBuffer();
+ String line;
+ while (null != (line = br.readLine())) data.append(line).append("\n");
+ return data.toString();
+ }
+}
+
More information about the Mulgara-svn
mailing list