stevenh@31: package org.qmul.eecs.c4dm.sparql.utilities; stevenh@31: stevenh@51: import java.util.List; stevenh@51: stevenh@51: import com.hp.hpl.jena.query.Dataset; stevenh@31: import com.hp.hpl.jena.query.Query; stevenh@31: import com.hp.hpl.jena.query.QueryExecution; stevenh@31: import com.hp.hpl.jena.query.QueryExecutionFactory; stevenh@31: import com.hp.hpl.jena.query.QueryFactory; stevenh@51: import com.hp.hpl.jena.query.ReadWrite; stevenh@31: import com.hp.hpl.jena.query.ResultSet; stevenh@31: import com.hp.hpl.jena.query.ResultSetFormatter; stevenh@31: import com.hp.hpl.jena.query.Syntax; stevenh@31: import com.hp.hpl.jena.rdf.model.Model; stevenh@31: import com.hp.hpl.jena.rdf.model.Statement; stevenh@31: import com.hp.hpl.jena.rdf.model.StmtIterator; stevenh@51: import com.hp.hpl.jena.update.UpdateAction; stevenh@31: stevenh@31: public class SparqlWrapperMethods { stevenh@31: stevenh@31: /** stevenh@31: * @param query stevenh@31: * @param m stevenh@31: * @return stevenh@31: */ stevenh@31: public static Model executeConstructQuery(String query, Model m) { stevenh@31: stevenh@31: // Now read the query file into a query object stevenh@31: Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required stevenh@31: // if using stevenh@31: // (e.g.) LET in stevenh@31: // SPARQL stevenh@31: stevenh@51: QueryExecution qe = QueryExecutionFactory.create(q, m); // Jena query stevenh@51: // QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // Pellet query stevenh@31: stevenh@31: // We want to execute a CONSTRUCT query, do it, and return the new stevenh@31: // triples stevenh@31: Model newModel = qe.execConstruct(); stevenh@31: stevenh@31: // Print the query for better understanding stevenh@31: System.out.println(q.toString()); stevenh@31: stevenh@31: // Print the new triples stevenh@31: StmtIterator iter = newModel.listStatements(); stevenh@31: printStmts(iter); stevenh@51: stevenh@51: qe.close(); stevenh@51: stevenh@51: return newModel; stevenh@51: } stevenh@51: stevenh@51: /** stevenh@51: * @param query stevenh@51: * @param dataset stevenh@51: * @return stevenh@51: */ stevenh@51: public static Model executeConstructQuery(String query, Dataset dataset) { stevenh@51: stevenh@51: // Now read the query file into a query object stevenh@51: Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required stevenh@51: // if using stevenh@51: // (e.g.) LET in stevenh@51: // SPARQL stevenh@51: stevenh@51: QueryExecution qe = QueryExecutionFactory.create(q, dataset); // Jena query stevenh@51: stevenh@51: // We want to execute a CONSTRUCT query, do it, and return the new stevenh@51: // triples stevenh@51: Model newModel; stevenh@51: dataset.begin(ReadWrite.WRITE) ; stevenh@51: try { stevenh@51: newModel = qe.execConstruct(); stevenh@51: dataset.commit(); stevenh@51: System.out.println("dataset.commit() for sparql construct done"); stevenh@51: } finally { stevenh@51: dataset.end(); stevenh@51: System.out.println("dataset.end() sparql construct done"); stevenh@51: } stevenh@51: stevenh@51: stevenh@51: // Print the query for better understanding stevenh@51: System.out.println(q.toString()); stevenh@51: stevenh@51: // Print the new triples stevenh@51: StmtIterator iter = newModel.listStatements(); stevenh@51: printStmts(iter); stevenh@51: stevenh@51: qe.close(); stevenh@31: stevenh@31: return newModel; stevenh@31: } stevenh@31: stevenh@31: /** stevenh@31: * @param query stevenh@31: * @param m stevenh@31: */ stevenh@31: public static void queryTheModel(String query, Model m) { stevenh@31: stevenh@31: // Now read the query file into a query object stevenh@31: Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required stevenh@31: // if using stevenh@31: // (e.g.) LET in stevenh@31: // SPARQL stevenh@31: stevenh@31: // Create a SPARQL-DL query execution for the given query and stevenh@31: // ontology model stevenh@51: QueryExecution qe = QueryExecutionFactory.create(q, m); stevenh@31: stevenh@31: // We want to execute a SELECT query, do it, and return the result set stevenh@31: ResultSet rs = qe.execSelect(); stevenh@31: stevenh@31: // Print the query for better understanding stevenh@31: System.out.println(q.toString()); stevenh@31: stevenh@31: // There are different things we can do with the result set, for stevenh@31: // instance iterate over it and process the query solutions or, what we stevenh@31: // do here, just print out the results stevenh@31: ResultSetFormatter.out(rs); stevenh@31: stevenh@31: // And an empty line to make it pretty stevenh@31: System.out.println(); stevenh@51: stevenh@51: qe.close(); stevenh@51: } stevenh@51: stevenh@51: public static void queryDataset(String query, Dataset dataset) { stevenh@51: stevenh@51: // Now read the query file into a query object stevenh@51: Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required stevenh@51: // if using stevenh@51: // (e.g.) LET in stevenh@51: // SPARQL stevenh@51: stevenh@51: // Create a SPARQL-DL query execution for the given query and stevenh@51: // ontology model stevenh@51: QueryExecution qe = QueryExecutionFactory.create(q, dataset); stevenh@51: stevenh@51: // We want to execute a SELECT query, do it, and return the result set stevenh@51: ResultSet rs = qe.execSelect(); stevenh@51: stevenh@51: // Print the query for better understanding stevenh@51: System.out.println(q.toString()); stevenh@51: stevenh@51: // There are different things we can do with the result set, for stevenh@51: // instance iterate over it and process the query solutions or, what we stevenh@51: // do here, just print out the results stevenh@51: ResultSetFormatter.out(rs); stevenh@51: stevenh@51: // And an empty line to make it pretty stevenh@51: System.out.println(); stevenh@51: stevenh@51: qe.close(); stevenh@51: stevenh@51: } stevenh@51: stevenh@51: public static ResultSet querySparqlService(String query, String service) { stevenh@51: stevenh@51: // Read the query file into a query object stevenh@51: Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required stevenh@51: // if using stevenh@51: // (e.g.) LET in stevenh@51: // SPARQL stevenh@51: stevenh@51: // Create a SPARQL-DL query execution for the given query and stevenh@51: // ontology model stevenh@51: QueryExecution qe = QueryExecutionFactory.sparqlService(service, q); stevenh@51: stevenh@51: // We want to execute a SELECT query, do it, and return the result set stevenh@51: ResultSet rs = qe.execSelect(); stevenh@51: stevenh@51: // Print the query for better understanding stevenh@51: System.out.println(q.toString()); stevenh@51: stevenh@51: // There are different things we can do with the result set, for stevenh@51: // instance iterate over it and process the query solutions or, what we stevenh@51: // do here, just print out the results stevenh@51: ResultSetFormatter.out(rs); stevenh@51: stevenh@51: // And an empty line to make it pretty stevenh@51: System.out.println(); stevenh@51: stevenh@51: qe.close(); stevenh@51: stevenh@51: return rs; stevenh@31: } stevenh@31: stevenh@31: /** stevenh@31: * @param query stevenh@31: * @param m stevenh@31: */ stevenh@31: public static void askTheModel(String query, Model m) { stevenh@31: stevenh@31: // Now read the query file into a query object stevenh@31: Query q = QueryFactory.read(query); stevenh@31: stevenh@31: // Create a SPARQL-DL query execution for the given query and stevenh@31: // ontology model stevenh@51: QueryExecution qe = QueryExecutionFactory.create(q, m); stevenh@31: stevenh@31: // We want to execute a SELECT query, do it, and return the result set stevenh@31: boolean result = qe.execAsk(); stevenh@31: stevenh@31: // Print the query for better understanding stevenh@31: System.out.println(q.toString()); stevenh@31: stevenh@31: // Print the result stevenh@31: System.out.println("Result: " + result); stevenh@31: stevenh@31: // And an empty line to make it pretty stevenh@31: System.out.println(); stevenh@51: stevenh@51: qe.close(); stevenh@31: } stevenh@31: stevenh@31: /** stevenh@31: * @param iter stevenh@31: */ stevenh@31: private static void printStmts(StmtIterator iter) { stevenh@31: Statement statement; stevenh@31: stevenh@31: while (iter.hasNext()) { stevenh@31: statement = iter.nextStatement(); stevenh@31: System.out.println(" | <" + statement.getSubject() + "> | <" stevenh@31: + statement.getPredicate() + "> | <" stevenh@31: + statement.getObject() + "> | "); stevenh@31: } stevenh@31: stevenh@31: // And an empty line to make it pretty stevenh@31: System.out.println(); stevenh@31: } stevenh@31: }