1
|
package eu.dnetlib.parthenos.virtuoso;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.util.List;
|
6
|
|
7
|
import org.apache.commons.io.IOUtils;
|
8
|
import org.apache.http.HttpEntity;
|
9
|
import org.apache.http.NameValuePair;
|
10
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
11
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
12
|
import org.apache.http.client.methods.HttpPost;
|
13
|
import org.apache.http.impl.client.CloseableHttpClient;
|
14
|
import org.apache.http.impl.client.HttpClients;
|
15
|
import org.apache.http.message.BasicNameValuePair;
|
16
|
import org.apache.http.util.EntityUtils;
|
17
|
import org.apache.jena.graph.NodeFactory;
|
18
|
import org.apache.jena.graph.Triple;
|
19
|
import org.apache.jena.query.Query;
|
20
|
import org.apache.jena.query.QueryExecutionFactory;
|
21
|
import org.apache.jena.query.QueryFactory;
|
22
|
import org.apache.jena.query.ResultSet;
|
23
|
import org.apache.jena.rdf.model.Model;
|
24
|
import org.apache.jena.rdf.model.Resource;
|
25
|
import org.apache.jena.rdf.model.ResourceFactory;
|
26
|
import org.apache.jena.riot.RDFDataMgr;
|
27
|
import org.apache.jena.riot.RDFFormat;
|
28
|
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;
|
29
|
import org.apache.jena.sparql.engine.http.QueryExceptionHTTP;
|
30
|
import org.apache.jena.util.iterator.ExtendedIterator;
|
31
|
import org.assertj.core.util.Lists;
|
32
|
import org.junit.Assert;
|
33
|
import org.junit.Before;
|
34
|
import org.junit.Ignore;
|
35
|
import org.junit.Test;
|
36
|
import org.junit.runner.RunWith;
|
37
|
import org.junit.runners.JUnit4;
|
38
|
import org.springframework.core.io.ClassPathResource;
|
39
|
import virtuoso.jena.driver.VirtGraph;
|
40
|
import virtuoso.jena.driver.VirtModel;
|
41
|
import virtuoso.jena.driver.VirtuosoQueryExecution;
|
42
|
import virtuoso.jena.driver.VirtuosoQueryExecutionFactory;
|
43
|
|
44
|
import static org.junit.Assert.assertEquals;
|
45
|
import static org.junit.Assert.assertNotNull;
|
46
|
|
47
|
/**
|
48
|
* Created by Alessia Bardi on 11/07/2017.
|
49
|
*
|
50
|
* @author Alessia Bardi
|
51
|
*/
|
52
|
@Ignore
|
53
|
@RunWith(JUnit4.class)
|
54
|
public class VirtuosoTest {
|
55
|
|
56
|
private String testGraph = "virt:test";
|
57
|
private String connectionString = "jdbc:virtuoso://localhost:1111";
|
58
|
private String testUser = "dba";
|
59
|
private String testPwd = "dba";
|
60
|
private String defaultURIBaseURl = "http://test/";
|
61
|
private String sparqlEndpoint = "http://localhost:8890/sparql";
|
62
|
|
63
|
@Before
|
64
|
public void prepare() {
|
65
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
66
|
assertNotNull(graph);
|
67
|
graph.clear();
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* See also: https://stackoverflow.com/questions/24576190/enable-reasoning-for-sparql-request-via-jena
|
72
|
*/
|
73
|
@Test
|
74
|
public void testInference1() {
|
75
|
String q = "DEFINE input:inference 'parthenos_rules' CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}";
|
76
|
String subjectURL = "http://clarin.eu/clarin-eric-datatables/centres";
|
77
|
String query = String.format(q, subjectURL, subjectURL, subjectURL, subjectURL);
|
78
|
System.out.println(query);
|
79
|
final QueryEngineHTTP request = new QueryEngineHTTP(sparqlEndpoint, query);
|
80
|
Model res = request.execConstruct();
|
81
|
RDFDataMgr.write(System.out, res, RDFFormat.RDFXML_PLAIN);
|
82
|
res.close();
|
83
|
request.close();
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
public void testLongQuery() throws IOException {
|
88
|
String q = IOUtils.toString(getResourceAsStream("/eu/dnetlib/parthenos/sparql/longQuery.sparql"));
|
89
|
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
|
90
|
|
91
|
HttpPost httpPost = new HttpPost(sparqlEndpoint);
|
92
|
List<NameValuePair> nvps = Lists.newArrayList();
|
93
|
nvps.add(new BasicNameValuePair("query", q));
|
94
|
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
|
95
|
|
96
|
try (CloseableHttpResponse response2 = httpclient.execute(httpPost)) {
|
97
|
System.out.println(response2.getStatusLine());
|
98
|
HttpEntity entity2 = response2.getEntity();
|
99
|
String s = IOUtils.toString(entity2.getContent());
|
100
|
System.out.println("OUT: " + s);
|
101
|
// do something useful with the response body
|
102
|
// and ensure it is fully consumed
|
103
|
EntityUtils.consume(entity2);
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
|
108
|
@Test(expected = QueryExceptionHTTP.class)
|
109
|
public void testLongQueryFails() throws IOException {
|
110
|
String q = IOUtils.toString(getResourceAsStream("/eu/dnetlib/parthenos/sparql/longQuery.sparql"));
|
111
|
final QueryEngineHTTP request = new QueryEngineHTTP(sparqlEndpoint, q);
|
112
|
Model res = request.execConstruct();
|
113
|
RDFDataMgr.write(System.out, res, RDFFormat.RDFXML_PLAIN);
|
114
|
res.close();
|
115
|
request.close();
|
116
|
}
|
117
|
|
118
|
@Test
|
119
|
public void testRemote() {
|
120
|
String q = "DEFINE input:inference 'parthenos_rules' CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}";
|
121
|
//String q = "CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}";
|
122
|
String subjectURL = "http://clarin.eu/clarin-eric-datatables/centres";
|
123
|
String query = String.format(q, subjectURL, subjectURL, subjectURL, subjectURL);
|
124
|
System.out.println(query);
|
125
|
VirtGraph g = new VirtGraph(connectionString, testUser, testPwd);
|
126
|
g.setReadFromAllGraphs(true);
|
127
|
VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(query, g);
|
128
|
Model resultModel = vqe.execConstruct();
|
129
|
RDFDataMgr.write(System.out, resultModel, RDFFormat.RDFXML_PLAIN);
|
130
|
vqe.close();
|
131
|
g.close();
|
132
|
}
|
133
|
|
134
|
@Test
|
135
|
public void testConnection() {
|
136
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
137
|
assertNotNull(graph);
|
138
|
}
|
139
|
|
140
|
@Test
|
141
|
public void testDescribe() {
|
142
|
//put something in the graph
|
143
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
144
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
145
|
String resURI = "http://www.parthenos-project.eu/ARIADNE/ARIADNECatalog/Dataset/http%3A%2F%2Fregistry.ariadne-infrastructure.eu%2Fdataset%2F25058478";
|
146
|
//now query for the resource URI
|
147
|
String describeQuery = "DESCRIBE <" + resURI + ">";
|
148
|
Query query = QueryFactory.create(describeQuery);
|
149
|
String sparqlUrl = "http://localhost:8890/sparql";
|
150
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
151
|
Model resultModel = serviceRequest.execDescribe();
|
152
|
serviceRequest.close();
|
153
|
resultModel.write(System.out);
|
154
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
155
|
graph.clear();
|
156
|
}
|
157
|
|
158
|
@Ignore
|
159
|
@Test
|
160
|
public void testDescribeRemote() {
|
161
|
String resURI = "http://hdl.handle.net/11858/00-203C-0000-0029-F5E8-C";
|
162
|
//now query for the resource URI
|
163
|
String describeQuery = "DESCRIBE <" + resURI + ">";
|
164
|
Query query = QueryFactory.create(describeQuery);
|
165
|
String sparqlUrl = "https://virtuoso.parthenos.d4science.org/sparql";
|
166
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
167
|
Model resultModel = serviceRequest.execDescribe();
|
168
|
serviceRequest.close();
|
169
|
resultModel.write(System.out);
|
170
|
}
|
171
|
|
172
|
@Test
|
173
|
public void testReadForDescribeResource() {
|
174
|
String sparqlUrl = "http://localhost:8890/sparql";
|
175
|
String queryForGraphs =
|
176
|
"SELECT DISTINCT ?g \n"
|
177
|
+ "WHERE {\n"
|
178
|
+ " { ?g <dnetcollectedFrom> <http://parthenos.d4science.org/handle/api_________::parthenos___::clarin::p_1271859438164> }\n"
|
179
|
+ "}";
|
180
|
Query query = QueryFactory.create(queryForGraphs);
|
181
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
182
|
ResultSet graphs = serviceRequest.execSelect();
|
183
|
String queryForSubjectsTemplate = "SELECT DISTINCT ?s WHERE {GRAPH <%s> {?s ?p ?o}}";
|
184
|
int countGraphs = 0;
|
185
|
while (graphs.hasNext()) {
|
186
|
countGraphs++;
|
187
|
Resource g = graphs.next().getResource("g");
|
188
|
System.out.println("*****GRAPH: " + g.getURI());
|
189
|
final QueryEngineHTTP serviceRequest2 =
|
190
|
QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(String.format(queryForSubjectsTemplate, g.getURI())));
|
191
|
ResultSet subjects = serviceRequest2.execSelect();
|
192
|
int countSubjects = 0;
|
193
|
while (subjects.hasNext()) {
|
194
|
countSubjects++;
|
195
|
Resource s = subjects.next().getResource("s");
|
196
|
String describeQuery = "DESCRIBE <" + s.getURI() + ">";
|
197
|
final QueryEngineHTTP serviceRequest3 = QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(describeQuery));
|
198
|
Model resultModel = serviceRequest3.execDescribe();
|
199
|
serviceRequest3.close();
|
200
|
resultModel.write(System.out);
|
201
|
}
|
202
|
serviceRequest2.close();
|
203
|
System.out.println("Got " + countSubjects + " subjects");
|
204
|
}
|
205
|
serviceRequest.close();
|
206
|
assertEquals("unexpected number of graphs", 4, countGraphs);
|
207
|
|
208
|
}
|
209
|
|
210
|
@Test
|
211
|
public void testClear() {
|
212
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
213
|
assertNotNull(graph);
|
214
|
graph.clear();
|
215
|
Assert.assertTrue(graph.isEmpty());
|
216
|
|
217
|
}
|
218
|
|
219
|
@Test
|
220
|
public void testFeedTriple() {
|
221
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
222
|
assertNotNull(graph);
|
223
|
graph.clear();
|
224
|
Assert.assertTrue(graph.isEmpty());
|
225
|
Triple t = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
226
|
NodeFactory.createURI("http://test/KevinBacon"));
|
227
|
graph.performAdd(t);
|
228
|
assertEquals(1, graph.getCount());
|
229
|
graph.clear();
|
230
|
}
|
231
|
|
232
|
@Test
|
233
|
public void testFeedRDFFile() {
|
234
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
235
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
236
|
long size = md.size();
|
237
|
System.out.println("==Inserted " + size + "triples==");
|
238
|
|
239
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
240
|
assertNotNull(graph);
|
241
|
Assert.assertFalse(graph.isEmpty());
|
242
|
|
243
|
graph.clear();
|
244
|
}
|
245
|
|
246
|
/*
|
247
|
If we want to be able to update, we need to store the triples in different named graph. This way we can delete the old triples and add the new ones.
|
248
|
And then we can add triples about the graph (e.g. provenance, last update time, whatever) in another named graph (e.g. 'provenance'),
|
249
|
where all the info about the named graphs are available.
|
250
|
*/
|
251
|
@Test
|
252
|
public void testUpdate() {
|
253
|
String namedGraph = "virt:test:theObjIdentifier";
|
254
|
Model md = VirtModel.openDatabaseModel(namedGraph, connectionString, testUser, testPwd);
|
255
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
256
|
|
257
|
long size = md.size();
|
258
|
System.out.println("==Inserted " + size + "triples==");
|
259
|
md.removeAll();
|
260
|
Assert.assertTrue(md.isEmpty());
|
261
|
|
262
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test_less_data.rdf"), defaultURIBaseURl);
|
263
|
long size2 = md.size();
|
264
|
System.out.println("==Inserted " + size2 + "triples==");
|
265
|
|
266
|
md.removeAll();
|
267
|
|
268
|
}
|
269
|
|
270
|
@Test
|
271
|
public void testRemoveAllWildcards() {
|
272
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
273
|
assertNotNull(graph);
|
274
|
graph.clear();
|
275
|
Assert.assertTrue(graph.isEmpty());
|
276
|
Triple t1 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
277
|
NodeFactory.createURI("http://test/KevinBacon"));
|
278
|
Triple t2 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
279
|
NodeFactory.createURI("http://test/BonoVox"));
|
280
|
Triple t3 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_enemy_of"),
|
281
|
NodeFactory.createURI("http://test/Spiderman"));
|
282
|
graph.performAdd(t1);
|
283
|
graph.performAdd(t2);
|
284
|
graph.performAdd(t3);
|
285
|
assertEquals(3, graph.getCount());
|
286
|
|
287
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
288
|
assertEquals(3, md.size());
|
289
|
md.removeAll(ResourceFactory.createResource("http://test/1"), ResourceFactory.createProperty("http://test/is_enemy_of"), null);
|
290
|
assertEquals(2, md.size());
|
291
|
}
|
292
|
|
293
|
@Test
|
294
|
public void testSearch() {
|
295
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
296
|
assertNotNull(graph);
|
297
|
graph.clear();
|
298
|
Assert.assertTrue(graph.isEmpty());
|
299
|
Triple t1 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
300
|
NodeFactory.createURI("http://test/KevinBacon"));
|
301
|
Triple t2 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
302
|
NodeFactory.createURI("http://test/BonoVox"));
|
303
|
Triple t3 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_enemy_of"),
|
304
|
NodeFactory.createURI("http://test/Spiderman"));
|
305
|
graph.performAdd(t1);
|
306
|
graph.performAdd(t2);
|
307
|
graph.performAdd(t3);
|
308
|
assertEquals(3, graph.getCount());
|
309
|
|
310
|
final ExtendedIterator<Triple> friends = graph.find(null, NodeFactory.createURI("http://test/is_friend_of"), null);
|
311
|
while (friends.hasNext()) {
|
312
|
System.out.println(friends.next());
|
313
|
}
|
314
|
|
315
|
}
|
316
|
|
317
|
private InputStream getResourceAsStream(final String classpath) {
|
318
|
try {
|
319
|
final ClassPathResource resource = new ClassPathResource(classpath);
|
320
|
return resource.getInputStream();
|
321
|
} catch (IOException e) {
|
322
|
return null;
|
323
|
}
|
324
|
}
|
325
|
|
326
|
}
|