diff src/org/qmul/eecs/c4dm/sparql/utilities/SparqlWrapperMethods.java @ 51:7d3ef5f70b90

organised imports close QueryExecution objects after use added executeConstructQuery method which works against a Dataset uses (jena's) QueryExecutionFactory instead of (pellet's) SparqlDLExecutionFactory added queryDataset method which works against a Dataset added querySparqlService method
author stevenh
date Tue, 02 Apr 2013 23:02:27 +0100
parents c572e86fb2fc
children 37d8b5284727
line wrap: on
line diff
--- a/src/org/qmul/eecs/c4dm/sparql/utilities/SparqlWrapperMethods.java	Tue Apr 02 22:53:50 2013 +0100
+++ b/src/org/qmul/eecs/c4dm/sparql/utilities/SparqlWrapperMethods.java	Tue Apr 02 23:02:27 2013 +0100
@@ -1,16 +1,20 @@
 package org.qmul.eecs.c4dm.sparql.utilities;
 
-import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
+import java.util.List;
+
+import com.hp.hpl.jena.query.Dataset;
 import com.hp.hpl.jena.query.Query;
 import com.hp.hpl.jena.query.QueryExecution;
 import com.hp.hpl.jena.query.QueryExecutionFactory;
 import com.hp.hpl.jena.query.QueryFactory;
+import com.hp.hpl.jena.query.ReadWrite;
 import com.hp.hpl.jena.query.ResultSet;
 import com.hp.hpl.jena.query.ResultSetFormatter;
 import com.hp.hpl.jena.query.Syntax;
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.Statement;
 import com.hp.hpl.jena.rdf.model.StmtIterator;
+import com.hp.hpl.jena.update.UpdateAction;
 
 public class SparqlWrapperMethods {
 
@@ -27,7 +31,8 @@
 																// (e.g.) LET in
 																// SPARQL
 
-		QueryExecution qe = QueryExecutionFactory.create(q, m);
+		QueryExecution qe = QueryExecutionFactory.create(q, m); // Jena query
+//		QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // Pellet query
 
 		// We want to execute a CONSTRUCT query, do it, and return the new
 		// triples
@@ -39,6 +44,49 @@
 		// Print the new triples
 		StmtIterator iter = newModel.listStatements();
 		printStmts(iter);
+		
+		qe.close();
+
+		return newModel;
+	}
+
+	/**
+	 * @param query
+	 * @param dataset
+	 * @return
+	 */
+	public static Model executeConstructQuery(String query, Dataset dataset) {
+
+		// Now read the query file into a query object
+		Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
+																// if using
+																// (e.g.) LET in
+																// SPARQL
+
+		QueryExecution qe = QueryExecutionFactory.create(q, dataset); // Jena query
+
+		// We want to execute a CONSTRUCT query, do it, and return the new
+		// triples
+		Model newModel;
+        dataset.begin(ReadWrite.WRITE) ;
+        try {
+    		newModel = qe.execConstruct();
+        	dataset.commit();
+        	System.out.println("dataset.commit() for sparql construct done");
+        } finally {
+        	dataset.end();
+        	System.out.println("dataset.end() sparql construct done");
+        }
+        
+
+		// Print the query for better understanding
+		System.out.println(q.toString());
+
+		// Print the new triples
+		StmtIterator iter = newModel.listStatements();
+		printStmts(iter);
+		
+		qe.close();
 
 		return newModel;
 	}
@@ -57,7 +105,7 @@
 
 		// Create a SPARQL-DL query execution for the given query and
 		// ontology model
-		QueryExecution qe = SparqlDLExecutionFactory.create(q, m);
+		QueryExecution qe = QueryExecutionFactory.create(q, m);
 
 		// We want to execute a SELECT query, do it, and return the result set
 		ResultSet rs = qe.execSelect();
@@ -72,6 +120,69 @@
 
 		// And an empty line to make it pretty
 		System.out.println();
+		
+		qe.close();
+	}
+	
+	public static void queryDataset(String query, Dataset dataset) {
+
+		// Now read the query file into a query object
+		Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
+																// if using
+																// (e.g.) LET in
+																// SPARQL
+
+		// Create a SPARQL-DL query execution for the given query and
+		// ontology model
+		QueryExecution qe = QueryExecutionFactory.create(q, dataset);
+
+		// We want to execute a SELECT query, do it, and return the result set
+		ResultSet rs = qe.execSelect();
+
+		// Print the query for better understanding
+		System.out.println(q.toString());
+
+		// There are different things we can do with the result set, for
+		// instance iterate over it and process the query solutions or, what we
+		// do here, just print out the results
+		ResultSetFormatter.out(rs);
+
+		// And an empty line to make it pretty
+		System.out.println();
+		
+		qe.close();
+		
+	}
+	
+	public static ResultSet querySparqlService(String query, String service) {
+
+		// Read the query file into a query object
+		Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
+																// if using
+																// (e.g.) LET in
+																// SPARQL
+
+		// Create a SPARQL-DL query execution for the given query and
+		// ontology model
+		QueryExecution qe = QueryExecutionFactory.sparqlService(service, q);
+				
+		// We want to execute a SELECT query, do it, and return the result set
+		ResultSet rs = qe.execSelect();
+
+		// Print the query for better understanding
+		System.out.println(q.toString());
+
+		// There are different things we can do with the result set, for
+		// instance iterate over it and process the query solutions or, what we
+		// do here, just print out the results
+		ResultSetFormatter.out(rs);
+
+		// And an empty line to make it pretty
+		System.out.println();
+		
+		qe.close();
+		
+		return rs;		
 	}
 
 	/**
@@ -85,7 +196,7 @@
 
 		// Create a SPARQL-DL query execution for the given query and
 		// ontology model
-		QueryExecution qe = SparqlDLExecutionFactory.create(q, m);
+		QueryExecution qe = QueryExecutionFactory.create(q, m);
 
 		// We want to execute a SELECT query, do it, and return the result set
 		boolean result = qe.execAsk();
@@ -98,6 +209,8 @@
 
 		// And an empty line to make it pretty
 		System.out.println();
+		
+		qe.close();
 	}
 
 	/**