view src/org/qmul/eecs/c4dm/sparql/utilities/SparqlWrapperMethods.java @ 31:c572e86fb2fc

new
author stevenh
date Tue, 08 Jan 2013 18:49:01 +0000
parents
children 7d3ef5f70b90
line wrap: on
line source
package org.qmul.eecs.c4dm.sparql.utilities;

import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
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.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;

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);

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

		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 = SparqlDLExecutionFactory.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();
	}

	/**
	 * @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 = SparqlDLExecutionFactory.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();
	}

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