view src/org/qmul/eecs/c4dm/sparql/utilities/SparqlWrapperMethods.java @ 76:37d8b5284727

added extra query methods
author stevenh
date Fri, 02 Aug 2013 15:48:25 +0100
parents 7d3ef5f70b90
children
line wrap: on
line source
package org.qmul.eecs.c4dm.sparql.utilities;

import java.util.List;

import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
import com.hp.hpl.jena.ontology.OntModel;
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 {

	/**
	 * @param query
	 * @param m
	 * @return
	 */
	public static Model executeConstructQuery(String query, Model m) {

		// 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, m); // Jena query

		// We want to execute a CONSTRUCT query, do it, and return the new
		// triples
		Model newModel = qe.execConstruct();

		// 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;
	}

	/**
	 * @param query
	 * @param m
	 * @return
	 */
	public static Model executePelletConstructQuery(String query, Model m) {

		// 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 = SparqlDLExecutionFactory.create(q, m); // Pellet query

		// We want to execute a CONSTRUCT query, do it, and return the new
		// triples
		Model newModel = qe.execConstruct();

		// 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;
	}

	/**
	 * @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;
	}

	/**
	 * @param query
	 * @param m
	 */
	public static void queryTheModel(String query, Model m) {

		// 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, m);

		// 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();
	}
	
	/**
	 * @param query
	 * @param ontModel
	 */
	public static void queryOntModel(String query, OntModel ontModel) {

		// 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, ontModel);

		// 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();
	}
	
	/**
	 * @param query
	 * @param ontModel
	 */
	public static void queryPelletOntModel(String query, OntModel ontModel) {

		// 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 = SparqlDLExecutionFactory.create(q, ontModel); // Pellet query

		// 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 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;		
	}

	/**
	 * @param query
	 * @param m
	 */
	public static void askTheModel(String query, Model m) {

		// Now read the query file into a query object
		Query q = QueryFactory.read(query);

		// Create a SPARQL-DL query execution for the given query and
		// ontology model
		QueryExecution qe = QueryExecutionFactory.create(q, m);

		// We want to execute a SELECT query, do it, and return the result set
		boolean result = qe.execAsk();

		// Print the query for better understanding
		System.out.println(q.toString());

		// Print the result
		System.out.println("Result: " + result);

		// And an empty line to make it pretty
		System.out.println();
		
		qe.close();
	}

	/**
	 * @param iter
	 */
	private static void printStmts(StmtIterator iter) {
		Statement statement;

		while (iter.hasNext()) {
			statement = iter.nextStatement();
			System.out.println(" | <" + statement.getSubject() + "> | <"
					+ statement.getPredicate() + "> | <"
					+ statement.getObject() + "> | ");
		}

		// And an empty line to make it pretty
		System.out.println();
	}
}