-
Notifications
You must be signed in to change notification settings - Fork 32
/
SPARQL.java
239 lines (211 loc) · 6.47 KB
/
SPARQL.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package org.aksw.qa.commons.sparql;
import java.util.ArrayList;
import java.util.Set;
import org.aksw.jena_sparql_api.cache.extra.CacheFrontend;
import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2;
import org.aksw.jena_sparql_api.core.FluentQueryExecutionFactory;
import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
import org.aksw.jena_sparql_api.http.QueryExecutionFactoryHttp;
import org.aksw.qa.commons.qald.QALD4_EvaluationUtils;
import org.aksw.qa.commons.utils.Results;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.impl.ResourceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.Sets;
/**
* Executes sparql queries.
*/
public class SPARQL {
private Logger log = LoggerFactory.getLogger(SPARQL.class);
public QueryExecutionFactory qef;
private long timeToLive = 360l * 24l * 60l * 60l * 1000l;
/**
* {@link SPARQLEndpoints#DBPEDIA_ORG} as endpoint used.
*/
public SPARQL() {
try {
CacheFrontend cacheFrontend = CacheUtilsH2.createCacheFrontend("./sparql", true, timeToLive);
// AKSW SPARQL API call
qef = FluentQueryExecutionFactory.http(SPARQLEndpoints.DBPEDIA_ORG).config().withCache(cacheFrontend).end().create();
} catch (RuntimeException e) {
log.error("Could not create SPARQL interface! ", e);
}
}
/**
* @param endpoint
* - A sparql endpoint, e.g. {@link SPARQLEndpoints#DBPEDIA_ORG}
*/
public SPARQL(final String endpoint) {
try {
CacheFrontend cacheFrontend = CacheUtilsH2.createCacheFrontend("./sparql", true, timeToLive);
qef = FluentQueryExecutionFactory.http(endpoint).config().withCache(cacheFrontend).end().create();
} catch (RuntimeException e) {
log.error("Could not create SPARQL interface! ", e);
}
}
/**
* Fire a sparql query against endpoint defined in constructor.
* <p>
* <b>NOTE:</b> This will block. To set a maximum execution time, use {@link ThreadedSPARQL}
* <p>
* For string representation of answers, see {@link #extractAnswerStrings(Set)}
*
* @param query
* - a sparql query
* @return
*/
public Set<RDFNode> sparql(final String query) {
Set<RDFNode> set = Sets.newHashSet();
QueryExecution qe = qef.createQueryExecution(query);
if ((qe != null) && (query != null)) {
if (QALD4_EvaluationUtils.isAskType(query)) {
set.add(new ResourceImpl(String.valueOf(qe.execAsk())));
} else {
ResultSet results = qe.execSelect();
String firstVarName = results.getResultVars().get(0);
while (results.hasNext()) {
RDFNode node = results.next().get(firstVarName);
/**
* Instead of returning a set with size 1 and value (null) in it, when no answers are found, this ensures that Set is empty
*/
if (node != null) {
set.add(node);
}
}
}
qe.close();
}
return set;
}
/**
* For use with {@link #sparql(String)} Extracts answer strings. Can be directly set as golden answers in an IQuestion.
*
* @param answers
* @return
*/
public static Set<String> extractAnswerStrings(final Set<RDFNode> answers) {
Set<String> set = Sets.newHashSet();
for (RDFNode answ : answers) {
if (answ.isResource()) {
set.add(answ.asResource().getURI());
} else if (answ.isLiteral()) {
Literal l = (Literal) answ;
try {
set.add(l.getString());
} catch (Exception e) {
e.printStackTrace();
set.add(l.getLexicalForm());
}
} else {
set.add(answ.toString());
}
}
return set;
}
/**
* Executes a select query for the given endpoint and query. Returns the answer as an {@link Results} object.
* @param query
* @param endpoint
* @return
*/
public static Results executeSelect(final String query, final String endpoint) {
QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpoint);
QueryExecution qe = qef.createQueryExecution(query);
ResultSet rs = qe.execSelect();
Results res = new Results();
res.header.addAll(rs.getResultVars());
while(rs.hasNext()) {
QuerySolution sol = rs.nextSolution();
res.table.add(new ArrayList<String>());
for(String head: res.header) {
String answer = "";
if(sol.get(head).isResource()) {
answer = sol.getResource(head).toString();
} else {
String temp = sol.get(head).toString();
if(temp.contains("@")) {
answer = "\"" + temp.substring(0, temp.indexOf("@")) + "\"" + temp.substring(temp.indexOf("@"));
} else if (temp.contains("^^")){
answer = "\"" + temp.substring(0, temp.indexOf("^")) + "\"^^<" + temp.substring(temp.indexOf("^")+2) + ">";
} else {
answer = temp;
}
}
res.table.get(res.table.size()-1).add(answer);
}
}
closeExecFactory(qef);
return res;
}
/**
* Executes an ask query for the given endpoint and query.
* @param query
* @param endpoint
* @return
*/
public static Boolean executeAsk(final String query, final String endpoint) {
QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpoint);
QueryExecution qe = qef.createQueryExecution(query);
closeExecFactory(qef);
return qe.execAsk();
}
/**
* @return - The time to live of frontendCache
*/
public long getCacheTimeToLive() {
return timeToLive;
}
/**
* @return - Tet the time to live for frontendCache
*/
public void setCacheTimeToLive(final long timeToLive) {
this.timeToLive = timeToLive;
}
/**
* Tries to parse query with apache.jena . If fails, returns false.
*
* @param sparql
* @return
*/
public static boolean isValidSparqlQuery(final String sparql) {
try {
QueryFactory.create(sparql);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* Checks if the given endpoint is alive. If fails, returns false.
* @param endpoint
* @return
*/
public static boolean isEndpointAlive(final String endpoint) {
QueryExecutionFactory qef = new QueryExecutionFactoryHttp(endpoint);
try {
QueryExecution qe = qef.createQueryExecution("PREFIX foaf: <http://xmlns.com/foaf/0.1/> ASK { ?x foaf:name \"Alice\" }");
qe.execAsk();
return true;
} catch (Exception e) {
} finally {
closeExecFactory(qef);
}
return false;
}
private static void closeExecFactory(QueryExecutionFactory qef) {
if(qef != null) {
try {
qef.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}