1
|
package eu.dnetlib.parthenos.virtuoso;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
|
6
|
import org.apache.jena.graph.NodeFactory;
|
7
|
import org.apache.jena.graph.Triple;
|
8
|
import org.apache.jena.query.*;
|
9
|
import org.apache.jena.rdf.model.Model;
|
10
|
import org.apache.jena.rdf.model.ResIterator;
|
11
|
import org.apache.jena.rdf.model.Resource;
|
12
|
import org.apache.jena.rdf.model.ResourceFactory;
|
13
|
import org.apache.jena.riot.RDFDataMgr;
|
14
|
import org.apache.jena.riot.RDFFormat;
|
15
|
import org.apache.jena.sparql.engine.http.QueryEngineHTTP;
|
16
|
import org.apache.jena.util.iterator.ExtendedIterator;
|
17
|
import org.junit.Assert;
|
18
|
import org.junit.Before;
|
19
|
import org.junit.Ignore;
|
20
|
import org.junit.Test;
|
21
|
import org.springframework.core.io.ClassPathResource;
|
22
|
import virtuoso.jena.driver.VirtGraph;
|
23
|
import virtuoso.jena.driver.VirtModel;
|
24
|
|
25
|
import static org.junit.Assert.assertEquals;
|
26
|
import static org.junit.Assert.assertNotNull;
|
27
|
|
28
|
/**
|
29
|
* Created by Alessia Bardi on 11/07/2017.
|
30
|
*
|
31
|
* @author Alessia Bardi
|
32
|
*/
|
33
|
@Ignore
|
34
|
public class VirtuosoTest {
|
35
|
|
36
|
private String testGraph = "virt:test";
|
37
|
private String connectionString = "jdbc:virtuoso://localhost:1111";
|
38
|
private String testUser = "dba";
|
39
|
private String testPwd = "dba";
|
40
|
private String defaultURIBaseURl = "http://test/";
|
41
|
|
42
|
@Before
|
43
|
public void prepare() {
|
44
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
45
|
assertNotNull(graph);
|
46
|
graph.clear();
|
47
|
}
|
48
|
|
49
|
@Test
|
50
|
public void testConnection() {
|
51
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
52
|
assertNotNull(graph);
|
53
|
}
|
54
|
|
55
|
@Ignore
|
56
|
@Test
|
57
|
public void testDescribe(){
|
58
|
//put something in the graph
|
59
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
60
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
61
|
String resURI = "http://www.parthenos-project.eu/ARIADNE/ARIADNECatalog/Dataset/http%3A%2F%2Fregistry.ariadne-infrastructure.eu%2Fdataset%2F25058478";
|
62
|
//now query for the resource URI
|
63
|
String describeQuery = "DESCRIBE <"+resURI+">";
|
64
|
Query query = QueryFactory.create(describeQuery) ;
|
65
|
String sparqlUrl = "http://localhost:8890/sparql";
|
66
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
67
|
Model resultModel = serviceRequest.execDescribe();
|
68
|
serviceRequest.close();
|
69
|
resultModel.write(System.out);
|
70
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
71
|
graph.clear();
|
72
|
}
|
73
|
|
74
|
|
75
|
@Ignore
|
76
|
@Test
|
77
|
public void testDescribeRemote(){
|
78
|
String resURI = "http://hdl.handle.net/11858/00-203C-0000-0029-F5E8-C";
|
79
|
//now query for the resource URI
|
80
|
String describeQuery = "DESCRIBE <"+resURI+">";
|
81
|
Query query = QueryFactory.create(describeQuery) ;
|
82
|
String sparqlUrl = "https://virtuoso.parthenos.d4science.org/sparql";
|
83
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
84
|
Model resultModel = serviceRequest.execDescribe();
|
85
|
serviceRequest.close();
|
86
|
resultModel.write(System.out);
|
87
|
}
|
88
|
|
89
|
@Test
|
90
|
public void testReadForDescribeResource() {
|
91
|
String sparqlUrl = "http://localhost:8890/sparql";
|
92
|
String queryForGraphs =
|
93
|
"SELECT DISTINCT ?g \n"
|
94
|
+ "WHERE {\n"
|
95
|
+ " { ?g <dnetcollectedFrom> <http://parthenos.d4science.org/handle/api_________::parthenos___::clarin::p_1271859438164> }\n"
|
96
|
+ "}";
|
97
|
Query query = QueryFactory.create(queryForGraphs);
|
98
|
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query);
|
99
|
ResultSet graphs = serviceRequest.execSelect();
|
100
|
String queryForSubjectsTemplate = "SELECT DISTINCT ?s WHERE {GRAPH <%s> {?s ?p ?o}}";
|
101
|
int countGraphs = 0;
|
102
|
while (graphs.hasNext()) {
|
103
|
countGraphs++;
|
104
|
Resource g = graphs.next().getResource("g");
|
105
|
System.out.println("*****GRAPH: "+g.getURI());
|
106
|
final QueryEngineHTTP serviceRequest2 =
|
107
|
QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(String.format(queryForSubjectsTemplate, g.getURI())));
|
108
|
ResultSet subjects = serviceRequest2.execSelect();
|
109
|
int countSubjects = 0;
|
110
|
while (subjects.hasNext()) {
|
111
|
countSubjects++;
|
112
|
Resource s = subjects.next().getResource("s");
|
113
|
String describeQuery = "DESCRIBE <" + s.getURI() + ">";
|
114
|
final QueryEngineHTTP serviceRequest3 = QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(describeQuery));
|
115
|
Model resultModel = serviceRequest3.execDescribe();
|
116
|
serviceRequest3.close();
|
117
|
resultModel.write(System.out);
|
118
|
}
|
119
|
serviceRequest2.close();
|
120
|
System.out.println("Got "+countSubjects+" subjects");
|
121
|
}
|
122
|
serviceRequest.close();
|
123
|
assertEquals("unexpected number of graphs", 4, countGraphs);
|
124
|
|
125
|
}
|
126
|
|
127
|
@Test
|
128
|
public void testClear() {
|
129
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
130
|
assertNotNull(graph);
|
131
|
graph.clear();
|
132
|
Assert.assertTrue(graph.isEmpty());
|
133
|
|
134
|
}
|
135
|
|
136
|
@Test
|
137
|
public void testFeedTriple() {
|
138
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
139
|
assertNotNull(graph);
|
140
|
graph.clear();
|
141
|
Assert.assertTrue(graph.isEmpty());
|
142
|
Triple t = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
143
|
NodeFactory.createURI("http://test/KevinBacon"));
|
144
|
graph.performAdd(t);
|
145
|
assertEquals(1, graph.getCount());
|
146
|
graph.clear();
|
147
|
}
|
148
|
|
149
|
@Test
|
150
|
public void testFeedRDFFile() {
|
151
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
152
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
153
|
long size = md.size();
|
154
|
System.out.println("==Inserted " + size + "triples==");
|
155
|
|
156
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
157
|
assertNotNull(graph);
|
158
|
Assert.assertFalse(graph.isEmpty());
|
159
|
|
160
|
graph.clear();
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
public void testFeedAndDump() {
|
165
|
Model md = VirtModel.openDatabaseModel("obj1", connectionString, testUser, testPwd);
|
166
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
167
|
md.close();
|
168
|
|
169
|
Model md2 = VirtModel.openDatabaseModel("obj1", connectionString, testUser, testPwd);
|
170
|
final ResIterator resIterator = md2.listSubjects();
|
171
|
while(resIterator.hasNext()){
|
172
|
Resource r = resIterator.nextResource();
|
173
|
System.out.println(r.getURI()+"*************");
|
174
|
Dataset d = RDFDataMgr.loadDataset(r.getURI());
|
175
|
RDFDataMgr.write(System.out, d, RDFFormat.RDFXML_PLAIN);
|
176
|
System.out.println("**********************");
|
177
|
}
|
178
|
System.out.println("END");
|
179
|
md2.removeAll();
|
180
|
md2.close();
|
181
|
|
182
|
}
|
183
|
|
184
|
/*
|
185
|
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.
|
186
|
And then we can add triples about the graph (e.g. provenance, last update time, whatever) in another named graph (e.g. 'provenance'),
|
187
|
where all the info about the named graphs are available.
|
188
|
*/
|
189
|
@Test
|
190
|
public void testUpdate() {
|
191
|
String namedGraph = "virt:test:theObjIdentifier";
|
192
|
Model md = VirtModel.openDatabaseModel(namedGraph, connectionString, testUser, testPwd);
|
193
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl);
|
194
|
|
195
|
long size = md.size();
|
196
|
System.out.println("==Inserted " + size + "triples==");
|
197
|
md.removeAll();
|
198
|
Assert.assertTrue(md.isEmpty());
|
199
|
|
200
|
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test_less_data.rdf"), defaultURIBaseURl);
|
201
|
long size2 = md.size();
|
202
|
System.out.println("==Inserted " + size2 + "triples==");
|
203
|
|
204
|
md.removeAll();
|
205
|
|
206
|
}
|
207
|
|
208
|
|
209
|
@Test
|
210
|
public void testRemoveAllWildcards() {
|
211
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
212
|
assertNotNull(graph);
|
213
|
graph.clear();
|
214
|
Assert.assertTrue(graph.isEmpty());
|
215
|
Triple t1 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
216
|
NodeFactory.createURI("http://test/KevinBacon"));
|
217
|
Triple t2 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
218
|
NodeFactory.createURI("http://test/BonoVox"));
|
219
|
Triple t3 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_enemy_of"),
|
220
|
NodeFactory.createURI("http://test/Spiderman"));
|
221
|
graph.performAdd(t1);
|
222
|
graph.performAdd(t2);
|
223
|
graph.performAdd(t3);
|
224
|
assertEquals(3, graph.getCount());
|
225
|
|
226
|
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd);
|
227
|
assertEquals(3, md.size());
|
228
|
md.removeAll(ResourceFactory.createResource("http://test/1"), ResourceFactory.createProperty("http://test/is_enemy_of"), null);
|
229
|
assertEquals(2, md.size());
|
230
|
}
|
231
|
|
232
|
@Test
|
233
|
public void testSearch(){
|
234
|
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd);
|
235
|
assertNotNull(graph);
|
236
|
graph.clear();
|
237
|
Assert.assertTrue(graph.isEmpty());
|
238
|
Triple t1 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
239
|
NodeFactory.createURI("http://test/KevinBacon"));
|
240
|
Triple t2 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_friend_of"),
|
241
|
NodeFactory.createURI("http://test/BonoVox"));
|
242
|
Triple t3 = new Triple(NodeFactory.createURI("http://test/1"), NodeFactory.createURI("http://test/is_enemy_of"),
|
243
|
NodeFactory.createURI("http://test/Spiderman"));
|
244
|
graph.performAdd(t1);
|
245
|
graph.performAdd(t2);
|
246
|
graph.performAdd(t3);
|
247
|
assertEquals(3, graph.getCount());
|
248
|
|
249
|
final ExtendedIterator<Triple> friends = graph.find(null, NodeFactory.createURI("http://test/is_friend_of"), null);
|
250
|
while(friends.hasNext()){
|
251
|
System.out.println(friends.next());
|
252
|
}
|
253
|
|
254
|
}
|
255
|
|
256
|
|
257
|
|
258
|
|
259
|
private InputStream getResourceAsStream(final String classpath) {
|
260
|
try {
|
261
|
final ClassPathResource resource = new ClassPathResource(classpath);
|
262
|
return resource.getInputStream();
|
263
|
} catch (IOException e) {
|
264
|
return null;
|
265
|
}
|
266
|
}
|
267
|
|
268
|
}
|