stevenh@31
|
1 package org.qmul.eecs.c4dm.sparql.utilities;
|
stevenh@31
|
2
|
stevenh@31
|
3 import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
|
stevenh@31
|
4 import com.hp.hpl.jena.query.Query;
|
stevenh@31
|
5 import com.hp.hpl.jena.query.QueryExecution;
|
stevenh@31
|
6 import com.hp.hpl.jena.query.QueryExecutionFactory;
|
stevenh@31
|
7 import com.hp.hpl.jena.query.QueryFactory;
|
stevenh@31
|
8 import com.hp.hpl.jena.query.ResultSet;
|
stevenh@31
|
9 import com.hp.hpl.jena.query.ResultSetFormatter;
|
stevenh@31
|
10 import com.hp.hpl.jena.query.Syntax;
|
stevenh@31
|
11 import com.hp.hpl.jena.rdf.model.Model;
|
stevenh@31
|
12 import com.hp.hpl.jena.rdf.model.Statement;
|
stevenh@31
|
13 import com.hp.hpl.jena.rdf.model.StmtIterator;
|
stevenh@31
|
14
|
stevenh@31
|
15 public class SparqlWrapperMethods {
|
stevenh@31
|
16
|
stevenh@31
|
17 /**
|
stevenh@31
|
18 * @param query
|
stevenh@31
|
19 * @param m
|
stevenh@31
|
20 * @return
|
stevenh@31
|
21 */
|
stevenh@31
|
22 public static Model executeConstructQuery(String query, Model m) {
|
stevenh@31
|
23
|
stevenh@31
|
24 // Now read the query file into a query object
|
stevenh@31
|
25 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@31
|
26 // if using
|
stevenh@31
|
27 // (e.g.) LET in
|
stevenh@31
|
28 // SPARQL
|
stevenh@31
|
29
|
stevenh@31
|
30 QueryExecution qe = QueryExecutionFactory.create(q, m);
|
stevenh@31
|
31
|
stevenh@31
|
32 // We want to execute a CONSTRUCT query, do it, and return the new
|
stevenh@31
|
33 // triples
|
stevenh@31
|
34 Model newModel = qe.execConstruct();
|
stevenh@31
|
35
|
stevenh@31
|
36 // Print the query for better understanding
|
stevenh@31
|
37 System.out.println(q.toString());
|
stevenh@31
|
38
|
stevenh@31
|
39 // Print the new triples
|
stevenh@31
|
40 StmtIterator iter = newModel.listStatements();
|
stevenh@31
|
41 printStmts(iter);
|
stevenh@31
|
42
|
stevenh@31
|
43 return newModel;
|
stevenh@31
|
44 }
|
stevenh@31
|
45
|
stevenh@31
|
46 /**
|
stevenh@31
|
47 * @param query
|
stevenh@31
|
48 * @param m
|
stevenh@31
|
49 */
|
stevenh@31
|
50 public static void queryTheModel(String query, Model m) {
|
stevenh@31
|
51
|
stevenh@31
|
52 // Now read the query file into a query object
|
stevenh@31
|
53 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required
|
stevenh@31
|
54 // if using
|
stevenh@31
|
55 // (e.g.) LET in
|
stevenh@31
|
56 // SPARQL
|
stevenh@31
|
57
|
stevenh@31
|
58 // Create a SPARQL-DL query execution for the given query and
|
stevenh@31
|
59 // ontology model
|
stevenh@31
|
60 QueryExecution qe = SparqlDLExecutionFactory.create(q, m);
|
stevenh@31
|
61
|
stevenh@31
|
62 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@31
|
63 ResultSet rs = qe.execSelect();
|
stevenh@31
|
64
|
stevenh@31
|
65 // Print the query for better understanding
|
stevenh@31
|
66 System.out.println(q.toString());
|
stevenh@31
|
67
|
stevenh@31
|
68 // There are different things we can do with the result set, for
|
stevenh@31
|
69 // instance iterate over it and process the query solutions or, what we
|
stevenh@31
|
70 // do here, just print out the results
|
stevenh@31
|
71 ResultSetFormatter.out(rs);
|
stevenh@31
|
72
|
stevenh@31
|
73 // And an empty line to make it pretty
|
stevenh@31
|
74 System.out.println();
|
stevenh@31
|
75 }
|
stevenh@31
|
76
|
stevenh@31
|
77 /**
|
stevenh@31
|
78 * @param query
|
stevenh@31
|
79 * @param m
|
stevenh@31
|
80 */
|
stevenh@31
|
81 public static void askTheModel(String query, Model m) {
|
stevenh@31
|
82
|
stevenh@31
|
83 // Now read the query file into a query object
|
stevenh@31
|
84 Query q = QueryFactory.read(query);
|
stevenh@31
|
85
|
stevenh@31
|
86 // Create a SPARQL-DL query execution for the given query and
|
stevenh@31
|
87 // ontology model
|
stevenh@31
|
88 QueryExecution qe = SparqlDLExecutionFactory.create(q, m);
|
stevenh@31
|
89
|
stevenh@31
|
90 // We want to execute a SELECT query, do it, and return the result set
|
stevenh@31
|
91 boolean result = qe.execAsk();
|
stevenh@31
|
92
|
stevenh@31
|
93 // Print the query for better understanding
|
stevenh@31
|
94 System.out.println(q.toString());
|
stevenh@31
|
95
|
stevenh@31
|
96 // Print the result
|
stevenh@31
|
97 System.out.println("Result: " + result);
|
stevenh@31
|
98
|
stevenh@31
|
99 // And an empty line to make it pretty
|
stevenh@31
|
100 System.out.println();
|
stevenh@31
|
101 }
|
stevenh@31
|
102
|
stevenh@31
|
103 /**
|
stevenh@31
|
104 * @param iter
|
stevenh@31
|
105 */
|
stevenh@31
|
106 private static void printStmts(StmtIterator iter) {
|
stevenh@31
|
107 Statement statement;
|
stevenh@31
|
108
|
stevenh@31
|
109 while (iter.hasNext()) {
|
stevenh@31
|
110 statement = iter.nextStatement();
|
stevenh@31
|
111 System.out.println(" | <" + statement.getSubject() + "> | <"
|
stevenh@31
|
112 + statement.getPredicate() + "> | <"
|
stevenh@31
|
113 + statement.getObject() + "> | ");
|
stevenh@31
|
114 }
|
stevenh@31
|
115
|
stevenh@31
|
116 // And an empty line to make it pretty
|
stevenh@31
|
117 System.out.println();
|
stevenh@31
|
118 }
|
stevenh@31
|
119 }
|