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