annotate 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
rev   line source
stevenh@31 1 package org.qmul.eecs.c4dm.sparql.utilities;
stevenh@31 2
stevenh@51 3 import java.util.List;
stevenh@51 4
stevenh@51 5 import com.hp.hpl.jena.query.Dataset;
stevenh@31 6 import com.hp.hpl.jena.query.Query;
stevenh@31 7 import com.hp.hpl.jena.query.QueryExecution;
stevenh@31 8 import com.hp.hpl.jena.query.QueryExecutionFactory;
stevenh@31 9 import com.hp.hpl.jena.query.QueryFactory;
stevenh@51 10 import com.hp.hpl.jena.query.ReadWrite;
stevenh@31 11 import com.hp.hpl.jena.query.ResultSet;
stevenh@31 12 import com.hp.hpl.jena.query.ResultSetFormatter;
stevenh@31 13 import com.hp.hpl.jena.query.Syntax;
stevenh@31 14 import com.hp.hpl.jena.rdf.model.Model;
stevenh@31 15 import com.hp.hpl.jena.rdf.model.Statement;
stevenh@31 16 import com.hp.hpl.jena.rdf.model.StmtIterator;
stevenh@51 17 import com.hp.hpl.jena.update.UpdateAction;
stevenh@31 18
stevenh@31 19 public class SparqlWrapperMethods {
stevenh@31 20
stevenh@31 21 /**
stevenh@31 22 * @param query
stevenh@31 23 * @param m
stevenh@31 24 * @return
stevenh@31 25 */
stevenh@31 26 public static Model executeConstructQuery(String query, Model m) {
stevenh@31 27
stevenh@31 28 // Now read the query file into a query object
stevenh@31 29 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
stevenh@31 30 // if using
stevenh@31 31 // (e.g.) LET in
stevenh@31 32 // SPARQL
stevenh@31 33
stevenh@51 34 QueryExecution qe = QueryExecutionFactory.create(q, m); // Jena query
stevenh@51 35 // QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // Pellet query
stevenh@31 36
stevenh@31 37 // We want to execute a CONSTRUCT query, do it, and return the new
stevenh@31 38 // triples
stevenh@31 39 Model newModel = qe.execConstruct();
stevenh@31 40
stevenh@31 41 // Print the query for better understanding
stevenh@31 42 System.out.println(q.toString());
stevenh@31 43
stevenh@31 44 // Print the new triples
stevenh@31 45 StmtIterator iter = newModel.listStatements();
stevenh@31 46 printStmts(iter);
stevenh@51 47
stevenh@51 48 qe.close();
stevenh@51 49
stevenh@51 50 return newModel;
stevenh@51 51 }
stevenh@51 52
stevenh@51 53 /**
stevenh@51 54 * @param query
stevenh@51 55 * @param dataset
stevenh@51 56 * @return
stevenh@51 57 */
stevenh@51 58 public static Model executeConstructQuery(String query, Dataset dataset) {
stevenh@51 59
stevenh@51 60 // Now read the query file into a query object
stevenh@51 61 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
stevenh@51 62 // if using
stevenh@51 63 // (e.g.) LET in
stevenh@51 64 // SPARQL
stevenh@51 65
stevenh@51 66 QueryExecution qe = QueryExecutionFactory.create(q, dataset); // Jena query
stevenh@51 67
stevenh@51 68 // We want to execute a CONSTRUCT query, do it, and return the new
stevenh@51 69 // triples
stevenh@51 70 Model newModel;
stevenh@51 71 dataset.begin(ReadWrite.WRITE) ;
stevenh@51 72 try {
stevenh@51 73 newModel = qe.execConstruct();
stevenh@51 74 dataset.commit();
stevenh@51 75 System.out.println("dataset.commit() for sparql construct done");
stevenh@51 76 } finally {
stevenh@51 77 dataset.end();
stevenh@51 78 System.out.println("dataset.end() sparql construct done");
stevenh@51 79 }
stevenh@51 80
stevenh@51 81
stevenh@51 82 // Print the query for better understanding
stevenh@51 83 System.out.println(q.toString());
stevenh@51 84
stevenh@51 85 // Print the new triples
stevenh@51 86 StmtIterator iter = newModel.listStatements();
stevenh@51 87 printStmts(iter);
stevenh@51 88
stevenh@51 89 qe.close();
stevenh@31 90
stevenh@31 91 return newModel;
stevenh@31 92 }
stevenh@31 93
stevenh@31 94 /**
stevenh@31 95 * @param query
stevenh@31 96 * @param m
stevenh@31 97 */
stevenh@31 98 public static void queryTheModel(String query, Model m) {
stevenh@31 99
stevenh@31 100 // Now read the query file into a query object
stevenh@31 101 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
stevenh@31 102 // if using
stevenh@31 103 // (e.g.) LET in
stevenh@31 104 // SPARQL
stevenh@31 105
stevenh@31 106 // Create a SPARQL-DL query execution for the given query and
stevenh@31 107 // ontology model
stevenh@51 108 QueryExecution qe = QueryExecutionFactory.create(q, m);
stevenh@31 109
stevenh@31 110 // We want to execute a SELECT query, do it, and return the result set
stevenh@31 111 ResultSet rs = qe.execSelect();
stevenh@31 112
stevenh@31 113 // Print the query for better understanding
stevenh@31 114 System.out.println(q.toString());
stevenh@31 115
stevenh@31 116 // There are different things we can do with the result set, for
stevenh@31 117 // instance iterate over it and process the query solutions or, what we
stevenh@31 118 // do here, just print out the results
stevenh@31 119 ResultSetFormatter.out(rs);
stevenh@31 120
stevenh@31 121 // And an empty line to make it pretty
stevenh@31 122 System.out.println();
stevenh@51 123
stevenh@51 124 qe.close();
stevenh@51 125 }
stevenh@51 126
stevenh@51 127 public static void queryDataset(String query, Dataset dataset) {
stevenh@51 128
stevenh@51 129 // Now read the query file into a query object
stevenh@51 130 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
stevenh@51 131 // if using
stevenh@51 132 // (e.g.) LET in
stevenh@51 133 // SPARQL
stevenh@51 134
stevenh@51 135 // Create a SPARQL-DL query execution for the given query and
stevenh@51 136 // ontology model
stevenh@51 137 QueryExecution qe = QueryExecutionFactory.create(q, dataset);
stevenh@51 138
stevenh@51 139 // We want to execute a SELECT query, do it, and return the result set
stevenh@51 140 ResultSet rs = qe.execSelect();
stevenh@51 141
stevenh@51 142 // Print the query for better understanding
stevenh@51 143 System.out.println(q.toString());
stevenh@51 144
stevenh@51 145 // There are different things we can do with the result set, for
stevenh@51 146 // instance iterate over it and process the query solutions or, what we
stevenh@51 147 // do here, just print out the results
stevenh@51 148 ResultSetFormatter.out(rs);
stevenh@51 149
stevenh@51 150 // And an empty line to make it pretty
stevenh@51 151 System.out.println();
stevenh@51 152
stevenh@51 153 qe.close();
stevenh@51 154
stevenh@51 155 }
stevenh@51 156
stevenh@51 157 public static ResultSet querySparqlService(String query, String service) {
stevenh@51 158
stevenh@51 159 // Read the query file into a query object
stevenh@51 160 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
stevenh@51 161 // if using
stevenh@51 162 // (e.g.) LET in
stevenh@51 163 // SPARQL
stevenh@51 164
stevenh@51 165 // Create a SPARQL-DL query execution for the given query and
stevenh@51 166 // ontology model
stevenh@51 167 QueryExecution qe = QueryExecutionFactory.sparqlService(service, q);
stevenh@51 168
stevenh@51 169 // We want to execute a SELECT query, do it, and return the result set
stevenh@51 170 ResultSet rs = qe.execSelect();
stevenh@51 171
stevenh@51 172 // Print the query for better understanding
stevenh@51 173 System.out.println(q.toString());
stevenh@51 174
stevenh@51 175 // There are different things we can do with the result set, for
stevenh@51 176 // instance iterate over it and process the query solutions or, what we
stevenh@51 177 // do here, just print out the results
stevenh@51 178 ResultSetFormatter.out(rs);
stevenh@51 179
stevenh@51 180 // And an empty line to make it pretty
stevenh@51 181 System.out.println();
stevenh@51 182
stevenh@51 183 qe.close();
stevenh@51 184
stevenh@51 185 return rs;
stevenh@31 186 }
stevenh@31 187
stevenh@31 188 /**
stevenh@31 189 * @param query
stevenh@31 190 * @param m
stevenh@31 191 */
stevenh@31 192 public static void askTheModel(String query, Model m) {
stevenh@31 193
stevenh@31 194 // Now read the query file into a query object
stevenh@31 195 Query q = QueryFactory.read(query);
stevenh@31 196
stevenh@31 197 // Create a SPARQL-DL query execution for the given query and
stevenh@31 198 // ontology model
stevenh@51 199 QueryExecution qe = QueryExecutionFactory.create(q, m);
stevenh@31 200
stevenh@31 201 // We want to execute a SELECT query, do it, and return the result set
stevenh@31 202 boolean result = qe.execAsk();
stevenh@31 203
stevenh@31 204 // Print the query for better understanding
stevenh@31 205 System.out.println(q.toString());
stevenh@31 206
stevenh@31 207 // Print the result
stevenh@31 208 System.out.println("Result: " + result);
stevenh@31 209
stevenh@31 210 // And an empty line to make it pretty
stevenh@31 211 System.out.println();
stevenh@51 212
stevenh@51 213 qe.close();
stevenh@31 214 }
stevenh@31 215
stevenh@31 216 /**
stevenh@31 217 * @param iter
stevenh@31 218 */
stevenh@31 219 private static void printStmts(StmtIterator iter) {
stevenh@31 220 Statement statement;
stevenh@31 221
stevenh@31 222 while (iter.hasNext()) {
stevenh@31 223 statement = iter.nextStatement();
stevenh@31 224 System.out.println(" | <" + statement.getSubject() + "> | <"
stevenh@31 225 + statement.getPredicate() + "> | <"
stevenh@31 226 + statement.getObject() + "> | ");
stevenh@31 227 }
stevenh@31 228
stevenh@31 229 // And an empty line to make it pretty
stevenh@31 230 System.out.println();
stevenh@31 231 }
stevenh@31 232 }