stevenh@31
|
1 package org.qmul.eecs.c4dm.sparql.utilities;
|
stevenh@31
|
2
|
stevenh@51
|
3 import java.util.List;
|
stevenh@51
|
4
|
stevenh@76
|
5 import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
|
stevenh@76
|
6 import com.hp.hpl.jena.ontology.OntModel;
|
stevenh@51
|
7 import com.hp.hpl.jena.query.Dataset;
|
stevenh@31
|
8 import com.hp.hpl.jena.query.Query;
|
stevenh@31
|
9 import com.hp.hpl.jena.query.QueryExecution;
|
stevenh@31
|
10 import com.hp.hpl.jena.query.QueryExecutionFactory;
|
stevenh@31
|
11 import com.hp.hpl.jena.query.QueryFactory;
|
stevenh@51
|
12 import com.hp.hpl.jena.query.ReadWrite;
|
stevenh@31
|
13 import com.hp.hpl.jena.query.ResultSet;
|
stevenh@31
|
14 import com.hp.hpl.jena.query.ResultSetFormatter;
|
stevenh@31
|
15 import com.hp.hpl.jena.query.Syntax;
|
stevenh@31
|
16 import com.hp.hpl.jena.rdf.model.Model;
|
stevenh@31
|
17 import com.hp.hpl.jena.rdf.model.Statement;
|
stevenh@31
|
18 import com.hp.hpl.jena.rdf.model.StmtIterator;
|
stevenh@51
|
19 import com.hp.hpl.jena.update.UpdateAction;
|
stevenh@31
|
20
|
stevenh@31
|
21 public class SparqlWrapperMethods {
|
stevenh@31
|
22
|
stevenh@31
|
23 /**
|
stevenh@31
|
24 * @param query
|
stevenh@31
|
25 * @param m
|
stevenh@31
|
26 * @return
|
stevenh@31
|
27 */
|
stevenh@31
|
28 public static Model executeConstructQuery(String query, Model m) {
|
stevenh@31
|
29
|
stevenh@31
|
30 // Now read the query file into a query object
|
stevenh@31
|
31 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@31
|
32 // if using
|
stevenh@31
|
33 // (e.g.) LET in
|
stevenh@31
|
34 // SPARQL
|
stevenh@31
|
35
|
stevenh@51
|
36 QueryExecution qe = QueryExecutionFactory.create(q, m); // Jena query
|
stevenh@76
|
37
|
stevenh@76
|
38 // We want to execute a CONSTRUCT query, do it, and return the new
|
stevenh@76
|
39 // triples
|
stevenh@76
|
40 Model newModel = qe.execConstruct();
|
stevenh@76
|
41
|
stevenh@76
|
42 // Print the query for better understanding
|
stevenh@76
|
43 System.out.println(q.toString());
|
stevenh@76
|
44
|
stevenh@76
|
45 // Print the new triples
|
stevenh@76
|
46 StmtIterator iter = newModel.listStatements();
|
stevenh@76
|
47 printStmts(iter);
|
stevenh@76
|
48
|
stevenh@76
|
49 qe.close();
|
stevenh@76
|
50
|
stevenh@76
|
51 return newModel;
|
stevenh@76
|
52 }
|
stevenh@76
|
53
|
stevenh@76
|
54 /**
|
stevenh@76
|
55 * @param query
|
stevenh@76
|
56 * @param m
|
stevenh@76
|
57 * @return
|
stevenh@76
|
58 */
|
stevenh@76
|
59 public static Model executePelletConstructQuery(String query, Model m) {
|
stevenh@76
|
60
|
stevenh@76
|
61 // Now read the query file into a query object
|
stevenh@76
|
62 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@76
|
63 // if using
|
stevenh@76
|
64 // (e.g.) LET in
|
stevenh@76
|
65 // SPARQL
|
stevenh@76
|
66
|
stevenh@76
|
67 QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // Pellet query
|
stevenh@31
|
68
|
stevenh@31
|
69 // We want to execute a CONSTRUCT query, do it, and return the new
|
stevenh@31
|
70 // triples
|
stevenh@31
|
71 Model newModel = qe.execConstruct();
|
stevenh@31
|
72
|
stevenh@31
|
73 // Print the query for better understanding
|
stevenh@31
|
74 System.out.println(q.toString());
|
stevenh@31
|
75
|
stevenh@31
|
76 // Print the new triples
|
stevenh@31
|
77 StmtIterator iter = newModel.listStatements();
|
stevenh@31
|
78 printStmts(iter);
|
stevenh@51
|
79
|
stevenh@51
|
80 qe.close();
|
stevenh@51
|
81
|
stevenh@51
|
82 return newModel;
|
stevenh@51
|
83 }
|
stevenh@51
|
84
|
stevenh@51
|
85 /**
|
stevenh@51
|
86 * @param query
|
stevenh@51
|
87 * @param dataset
|
stevenh@51
|
88 * @return
|
stevenh@51
|
89 */
|
stevenh@51
|
90 public static Model executeConstructQuery(String query, Dataset dataset) {
|
stevenh@51
|
91
|
stevenh@51
|
92 // Now read the query file into a query object
|
stevenh@51
|
93 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@51
|
94 // if using
|
stevenh@51
|
95 // (e.g.) LET in
|
stevenh@51
|
96 // SPARQL
|
stevenh@51
|
97
|
stevenh@51
|
98 QueryExecution qe = QueryExecutionFactory.create(q, dataset); // Jena query
|
stevenh@51
|
99
|
stevenh@51
|
100 // We want to execute a CONSTRUCT query, do it, and return the new
|
stevenh@51
|
101 // triples
|
stevenh@51
|
102 Model newModel;
|
stevenh@51
|
103 dataset.begin(ReadWrite.WRITE) ;
|
stevenh@51
|
104 try {
|
stevenh@51
|
105 newModel = qe.execConstruct();
|
stevenh@51
|
106 dataset.commit();
|
stevenh@51
|
107 System.out.println("dataset.commit() for sparql construct done");
|
stevenh@51
|
108 } finally {
|
stevenh@51
|
109 dataset.end();
|
stevenh@51
|
110 System.out.println("dataset.end() sparql construct done");
|
stevenh@51
|
111 }
|
stevenh@51
|
112
|
stevenh@51
|
113
|
stevenh@51
|
114 // Print the query for better understanding
|
stevenh@51
|
115 System.out.println(q.toString());
|
stevenh@51
|
116
|
stevenh@51
|
117 // Print the new triples
|
stevenh@51
|
118 StmtIterator iter = newModel.listStatements();
|
stevenh@51
|
119 printStmts(iter);
|
stevenh@51
|
120
|
stevenh@51
|
121 qe.close();
|
stevenh@31
|
122
|
stevenh@31
|
123 return newModel;
|
stevenh@31
|
124 }
|
stevenh@31
|
125
|
stevenh@31
|
126 /**
|
stevenh@31
|
127 * @param query
|
stevenh@31
|
128 * @param m
|
stevenh@31
|
129 */
|
stevenh@31
|
130 public static void queryTheModel(String query, Model m) {
|
stevenh@31
|
131
|
stevenh@31
|
132 // Now read the query file into a query object
|
stevenh@31
|
133 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@31
|
134 // if using
|
stevenh@31
|
135 // (e.g.) LET in
|
stevenh@31
|
136 // SPARQL
|
stevenh@31
|
137
|
stevenh@31
|
138 // Create a SPARQL-DL query execution for the given query and
|
stevenh@31
|
139 // ontology model
|
stevenh@51
|
140 QueryExecution qe = QueryExecutionFactory.create(q, m);
|
stevenh@31
|
141
|
stevenh@31
|
142 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@31
|
143 ResultSet rs = qe.execSelect();
|
stevenh@31
|
144
|
stevenh@31
|
145 // Print the query for better understanding
|
stevenh@31
|
146 System.out.println(q.toString());
|
stevenh@31
|
147
|
stevenh@31
|
148 // There are different things we can do with the result set, for
|
stevenh@31
|
149 // instance iterate over it and process the query solutions or, what we
|
stevenh@31
|
150 // do here, just print out the results
|
stevenh@31
|
151 ResultSetFormatter.out(rs);
|
stevenh@31
|
152
|
stevenh@31
|
153 // And an empty line to make it pretty
|
stevenh@31
|
154 System.out.println();
|
stevenh@51
|
155
|
stevenh@51
|
156 qe.close();
|
stevenh@51
|
157 }
|
stevenh@51
|
158
|
stevenh@76
|
159 /**
|
stevenh@76
|
160 * @param query
|
stevenh@76
|
161 * @param ontModel
|
stevenh@76
|
162 */
|
stevenh@76
|
163 public static void queryOntModel(String query, OntModel ontModel) {
|
stevenh@76
|
164
|
stevenh@76
|
165 // Now read the query file into a query object
|
stevenh@76
|
166 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@76
|
167 // if using
|
stevenh@76
|
168 // (e.g.) LET in
|
stevenh@76
|
169 // SPARQL
|
stevenh@76
|
170
|
stevenh@76
|
171 // Create a SPARQL-DL query execution for the given query and
|
stevenh@76
|
172 // ontology model
|
stevenh@76
|
173 QueryExecution qe = QueryExecutionFactory.create(q, ontModel);
|
stevenh@76
|
174
|
stevenh@76
|
175 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@76
|
176 ResultSet rs = qe.execSelect();
|
stevenh@76
|
177
|
stevenh@76
|
178 // Print the query for better understanding
|
stevenh@76
|
179 System.out.println(q.toString());
|
stevenh@76
|
180
|
stevenh@76
|
181 // There are different things we can do with the result set, for
|
stevenh@76
|
182 // instance iterate over it and process the query solutions or, what we
|
stevenh@76
|
183 // do here, just print out the results
|
stevenh@76
|
184 ResultSetFormatter.out(rs);
|
stevenh@76
|
185
|
stevenh@76
|
186 // And an empty line to make it pretty
|
stevenh@76
|
187 System.out.println();
|
stevenh@76
|
188
|
stevenh@76
|
189 qe.close();
|
stevenh@76
|
190 }
|
stevenh@76
|
191
|
stevenh@76
|
192 /**
|
stevenh@76
|
193 * @param query
|
stevenh@76
|
194 * @param ontModel
|
stevenh@76
|
195 */
|
stevenh@76
|
196 public static void queryPelletOntModel(String query, OntModel ontModel) {
|
stevenh@76
|
197
|
stevenh@76
|
198 // Now read the query file into a query object
|
stevenh@76
|
199 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@76
|
200 // if using
|
stevenh@76
|
201 // (e.g.) LET in
|
stevenh@76
|
202 // SPARQL
|
stevenh@76
|
203
|
stevenh@76
|
204 // Create a SPARQL-DL query execution for the given query and
|
stevenh@76
|
205 // ontology model
|
stevenh@76
|
206 QueryExecution qe = SparqlDLExecutionFactory.create(q, ontModel); // Pellet query
|
stevenh@76
|
207
|
stevenh@76
|
208 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@76
|
209 ResultSet rs = qe.execSelect();
|
stevenh@76
|
210
|
stevenh@76
|
211 // Print the query for better understanding
|
stevenh@76
|
212 System.out.println(q.toString());
|
stevenh@76
|
213
|
stevenh@76
|
214 // There are different things we can do with the result set, for
|
stevenh@76
|
215 // instance iterate over it and process the query solutions or, what we
|
stevenh@76
|
216 // do here, just print out the results
|
stevenh@76
|
217 ResultSetFormatter.out(rs);
|
stevenh@76
|
218
|
stevenh@76
|
219 // And an empty line to make it pretty
|
stevenh@76
|
220 System.out.println();
|
stevenh@76
|
221
|
stevenh@76
|
222 qe.close();
|
stevenh@76
|
223 }
|
stevenh@76
|
224
|
stevenh@51
|
225 public static void queryDataset(String query, Dataset dataset) {
|
stevenh@51
|
226
|
stevenh@51
|
227 // Now read the query file into a query object
|
stevenh@51
|
228 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@51
|
229 // if using
|
stevenh@51
|
230 // (e.g.) LET in
|
stevenh@51
|
231 // SPARQL
|
stevenh@51
|
232
|
stevenh@51
|
233 // Create a SPARQL-DL query execution for the given query and
|
stevenh@51
|
234 // ontology model
|
stevenh@51
|
235 QueryExecution qe = QueryExecutionFactory.create(q, dataset);
|
stevenh@51
|
236
|
stevenh@51
|
237 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@51
|
238 ResultSet rs = qe.execSelect();
|
stevenh@51
|
239
|
stevenh@51
|
240 // Print the query for better understanding
|
stevenh@51
|
241 System.out.println(q.toString());
|
stevenh@51
|
242
|
stevenh@51
|
243 // There are different things we can do with the result set, for
|
stevenh@51
|
244 // instance iterate over it and process the query solutions or, what we
|
stevenh@51
|
245 // do here, just print out the results
|
stevenh@51
|
246 ResultSetFormatter.out(rs);
|
stevenh@51
|
247
|
stevenh@51
|
248 // And an empty line to make it pretty
|
stevenh@51
|
249 System.out.println();
|
stevenh@51
|
250
|
stevenh@51
|
251 qe.close();
|
stevenh@51
|
252
|
stevenh@51
|
253 }
|
stevenh@51
|
254
|
stevenh@51
|
255 public static ResultSet querySparqlService(String query, String service) {
|
stevenh@51
|
256
|
stevenh@51
|
257 // Read the query file into a query object
|
stevenh@51
|
258 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@51
|
259 // if using
|
stevenh@51
|
260 // (e.g.) LET in
|
stevenh@51
|
261 // SPARQL
|
stevenh@51
|
262
|
stevenh@51
|
263 // Create a SPARQL-DL query execution for the given query and
|
stevenh@51
|
264 // ontology model
|
stevenh@51
|
265 QueryExecution qe = QueryExecutionFactory.sparqlService(service, q);
|
stevenh@51
|
266
|
stevenh@51
|
267 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@51
|
268 ResultSet rs = qe.execSelect();
|
stevenh@51
|
269
|
stevenh@51
|
270 // Print the query for better understanding
|
stevenh@51
|
271 System.out.println(q.toString());
|
stevenh@51
|
272
|
stevenh@51
|
273 // There are different things we can do with the result set, for
|
stevenh@51
|
274 // instance iterate over it and process the query solutions or, what we
|
stevenh@51
|
275 // do here, just print out the results
|
stevenh@51
|
276 ResultSetFormatter.out(rs);
|
stevenh@51
|
277
|
stevenh@51
|
278 // And an empty line to make it pretty
|
stevenh@51
|
279 System.out.println();
|
stevenh@51
|
280
|
stevenh@51
|
281 qe.close();
|
stevenh@51
|
282
|
stevenh@51
|
283 return rs;
|
stevenh@31
|
284 }
|
stevenh@31
|
285
|
stevenh@31
|
286 /**
|
stevenh@31
|
287 * @param query
|
stevenh@31
|
288 * @param m
|
stevenh@31
|
289 */
|
stevenh@31
|
290 public static void askTheModel(String query, Model m) {
|
stevenh@31
|
291
|
stevenh@31
|
292 // Now read the query file into a query object
|
stevenh@31
|
293 Query q = QueryFactory.read(query);
|
stevenh@31
|
294
|
stevenh@31
|
295 // Create a SPARQL-DL query execution for the given query and
|
stevenh@31
|
296 // ontology model
|
stevenh@51
|
297 QueryExecution qe = QueryExecutionFactory.create(q, m);
|
stevenh@31
|
298
|
stevenh@31
|
299 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@31
|
300 boolean result = qe.execAsk();
|
stevenh@31
|
301
|
stevenh@31
|
302 // Print the query for better understanding
|
stevenh@31
|
303 System.out.println(q.toString());
|
stevenh@31
|
304
|
stevenh@31
|
305 // Print the result
|
stevenh@31
|
306 System.out.println("Result: " + result);
|
stevenh@31
|
307
|
stevenh@31
|
308 // And an empty line to make it pretty
|
stevenh@31
|
309 System.out.println();
|
stevenh@51
|
310
|
stevenh@51
|
311 qe.close();
|
stevenh@31
|
312 }
|
stevenh@31
|
313
|
stevenh@31
|
314 /**
|
stevenh@31
|
315 * @param iter
|
stevenh@31
|
316 */
|
stevenh@31
|
317 private static void printStmts(StmtIterator iter) {
|
stevenh@31
|
318 Statement statement;
|
stevenh@31
|
319
|
stevenh@31
|
320 while (iter.hasNext()) {
|
stevenh@31
|
321 statement = iter.nextStatement();
|
stevenh@31
|
322 System.out.println(" | <" + statement.getSubject() + "> | <"
|
stevenh@31
|
323 + statement.getPredicate() + "> | <"
|
stevenh@31
|
324 + statement.getObject() + "> | ");
|
stevenh@31
|
325 }
|
stevenh@31
|
326
|
stevenh@31
|
327 // And an empty line to make it pretty
|
stevenh@31
|
328 System.out.println();
|
stevenh@31
|
329 }
|
stevenh@31
|
330 }
|