Revision 51265
Added by Alessia Bardi about 6 years ago
VirtuosoTest.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.io.InputStream; |
5 |
import java.util.List; |
|
5 | 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; |
|
6 | 17 |
import org.apache.jena.graph.NodeFactory; |
7 | 18 |
import org.apache.jena.graph.Triple; |
8 | 19 |
import org.apache.jena.query.*; |
... | ... | |
13 | 24 |
import org.apache.jena.riot.RDFDataMgr; |
14 | 25 |
import org.apache.jena.riot.RDFFormat; |
15 | 26 |
import org.apache.jena.sparql.engine.http.QueryEngineHTTP; |
27 |
import org.apache.jena.sparql.engine.http.QueryExceptionHTTP; |
|
16 | 28 |
import org.apache.jena.util.iterator.ExtendedIterator; |
29 |
import org.assertj.core.util.Lists; |
|
17 | 30 |
import org.junit.Assert; |
18 | 31 |
import org.junit.Before; |
19 | 32 |
import org.junit.Ignore; |
... | ... | |
53 | 66 |
* See also: https://stackoverflow.com/questions/24576190/enable-reasoning-for-sparql-request-via-jena |
54 | 67 |
*/ |
55 | 68 |
@Test |
56 |
public void testInference1(){ |
|
69 |
public void testInference1() {
|
|
57 | 70 |
String q = "DEFINE input:inference 'parthenos_rules' CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}"; |
58 | 71 |
String subjectURL = "http://clarin.eu/clarin-eric-datatables/centres"; |
59 | 72 |
String query = String.format(q, subjectURL, subjectURL, subjectURL, subjectURL); |
60 | 73 |
System.out.println(query); |
61 |
final QueryEngineHTTP request = new QueryEngineHTTP (sparqlEndpoint, query);
|
|
74 |
final QueryEngineHTTP request = new QueryEngineHTTP(sparqlEndpoint, query); |
|
62 | 75 |
Model res = request.execConstruct(); |
63 | 76 |
RDFDataMgr.write(System.out, res, RDFFormat.RDFXML_PLAIN); |
64 | 77 |
res.close(); |
65 | 78 |
request.close(); |
66 | 79 |
} |
67 | 80 |
|
81 |
@Test |
|
82 |
public void testLongQuery() throws IOException { |
|
83 |
String q = IOUtils.toString(getResourceAsStream("/eu/dnetlib/parthenos/sparql/longQuery.sparql")); |
|
84 |
try (CloseableHttpClient httpclient = HttpClients.createDefault()) { |
|
68 | 85 |
|
86 |
HttpPost httpPost = new HttpPost(sparqlEndpoint); |
|
87 |
List<NameValuePair> nvps = Lists.newArrayList(); |
|
88 |
nvps.add(new BasicNameValuePair("query", q)); |
|
89 |
httpPost.setEntity(new UrlEncodedFormEntity(nvps)); |
|
69 | 90 |
|
91 |
try (CloseableHttpResponse response2 = httpclient.execute(httpPost)) { |
|
92 |
System.out.println(response2.getStatusLine()); |
|
93 |
HttpEntity entity2 = response2.getEntity(); |
|
94 |
String s = IOUtils.toString(entity2.getContent()); |
|
95 |
System.out.println("OUT: " + s); |
|
96 |
// do something useful with the response body |
|
97 |
// and ensure it is fully consumed |
|
98 |
EntityUtils.consume(entity2); |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
@Test(expected = QueryExceptionHTTP.class) |
|
104 |
public void testLongQueryFails() throws IOException { |
|
105 |
String q = IOUtils.toString(getResourceAsStream("/eu/dnetlib/parthenos/sparql/longQuery.sparql")); |
|
106 |
final QueryEngineHTTP request = new QueryEngineHTTP(sparqlEndpoint, q); |
|
107 |
Model res = request.execConstruct(); |
|
108 |
RDFDataMgr.write(System.out, res, RDFFormat.RDFXML_PLAIN); |
|
109 |
res.close(); |
|
110 |
request.close(); |
|
111 |
} |
|
112 |
|
|
70 | 113 |
@Test |
71 |
public void testRemote(){ |
|
114 |
public void testRemote() {
|
|
72 | 115 |
String q = "DEFINE input:inference 'parthenos_rules' CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}"; |
73 | 116 |
//String q = "CONSTRUCT {<%s> ?p ?o . } WHERE { <%s> ?p ?o .}"; |
74 | 117 |
String subjectURL = "http://clarin.eu/clarin-eric-datatables/centres"; |
... | ... | |
91 | 134 |
|
92 | 135 |
@Ignore |
93 | 136 |
@Test |
94 |
public void testDescribe(){ |
|
137 |
public void testDescribe() {
|
|
95 | 138 |
//put something in the graph |
96 | 139 |
Model md = VirtModel.openDatabaseModel(testGraph, connectionString, testUser, testPwd); |
97 | 140 |
md.read(getResourceAsStream("eu/dnetlib/parthenos/virtuoso/test.rdf"), defaultURIBaseURl); |
98 | 141 |
String resURI = "http://www.parthenos-project.eu/ARIADNE/ARIADNECatalog/Dataset/http%3A%2F%2Fregistry.ariadne-infrastructure.eu%2Fdataset%2F25058478"; |
99 | 142 |
//now query for the resource URI |
100 |
String describeQuery = "DESCRIBE <"+resURI+">";
|
|
101 |
Query query = QueryFactory.create(describeQuery) ;
|
|
143 |
String describeQuery = "DESCRIBE <" + resURI + ">";
|
|
144 |
Query query = QueryFactory.create(describeQuery); |
|
102 | 145 |
String sparqlUrl = "http://localhost:8890/sparql"; |
103 | 146 |
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query); |
104 | 147 |
Model resultModel = serviceRequest.execDescribe(); |
... | ... | |
108 | 151 |
graph.clear(); |
109 | 152 |
} |
110 | 153 |
|
111 |
|
|
112 | 154 |
@Ignore |
113 | 155 |
@Test |
114 |
public void testDescribeRemote(){ |
|
156 |
public void testDescribeRemote() {
|
|
115 | 157 |
String resURI = "http://hdl.handle.net/11858/00-203C-0000-0029-F5E8-C"; |
116 | 158 |
//now query for the resource URI |
117 |
String describeQuery = "DESCRIBE <"+resURI+">";
|
|
118 |
Query query = QueryFactory.create(describeQuery) ;
|
|
159 |
String describeQuery = "DESCRIBE <" + resURI + ">";
|
|
160 |
Query query = QueryFactory.create(describeQuery); |
|
119 | 161 |
String sparqlUrl = "https://virtuoso.parthenos.d4science.org/sparql"; |
120 | 162 |
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query); |
121 | 163 |
Model resultModel = serviceRequest.execDescribe(); |
... | ... | |
139 | 181 |
while (graphs.hasNext()) { |
140 | 182 |
countGraphs++; |
141 | 183 |
Resource g = graphs.next().getResource("g"); |
142 |
System.out.println("*****GRAPH: "+g.getURI());
|
|
184 |
System.out.println("*****GRAPH: " + g.getURI());
|
|
143 | 185 |
final QueryEngineHTTP serviceRequest2 = |
144 | 186 |
QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(String.format(queryForSubjectsTemplate, g.getURI()))); |
145 | 187 |
ResultSet subjects = serviceRequest2.execSelect(); |
... | ... | |
154 | 196 |
resultModel.write(System.out); |
155 | 197 |
} |
156 | 198 |
serviceRequest2.close(); |
157 |
System.out.println("Got "+countSubjects+" subjects");
|
|
199 |
System.out.println("Got " + countSubjects + " subjects");
|
|
158 | 200 |
} |
159 | 201 |
serviceRequest.close(); |
160 | 202 |
assertEquals("unexpected number of graphs", 4, countGraphs); |
... | ... | |
205 | 247 |
|
206 | 248 |
Model md2 = VirtModel.openDatabaseModel("obj1", connectionString, testUser, testPwd); |
207 | 249 |
final ResIterator resIterator = md2.listSubjects(); |
208 |
while(resIterator.hasNext()){
|
|
250 |
while (resIterator.hasNext()) {
|
|
209 | 251 |
Resource r = resIterator.nextResource(); |
210 |
System.out.println(r.getURI()+"*************");
|
|
252 |
System.out.println(r.getURI() + "*************");
|
|
211 | 253 |
Dataset d = RDFDataMgr.loadDataset(r.getURI()); |
212 | 254 |
RDFDataMgr.write(System.out, d, RDFFormat.RDFXML_PLAIN); |
213 | 255 |
System.out.println("**********************"); |
... | ... | |
242 | 284 |
|
243 | 285 |
} |
244 | 286 |
|
245 |
|
|
246 | 287 |
@Test |
247 | 288 |
public void testRemoveAllWildcards() { |
248 | 289 |
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd); |
... | ... | |
267 | 308 |
} |
268 | 309 |
|
269 | 310 |
@Test |
270 |
public void testSearch(){ |
|
311 |
public void testSearch() {
|
|
271 | 312 |
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd); |
272 | 313 |
assertNotNull(graph); |
273 | 314 |
graph.clear(); |
... | ... | |
284 | 325 |
assertEquals(3, graph.getCount()); |
285 | 326 |
|
286 | 327 |
final ExtendedIterator<Triple> friends = graph.find(null, NodeFactory.createURI("http://test/is_friend_of"), null); |
287 |
while(friends.hasNext()){
|
|
328 |
while (friends.hasNext()) {
|
|
288 | 329 |
System.out.println(friends.next()); |
289 | 330 |
} |
290 | 331 |
|
291 | 332 |
} |
292 | 333 |
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 | 334 |
private InputStream getResourceAsStream(final String classpath) { |
298 | 335 |
try { |
299 | 336 |
final ClassPathResource resource = new ClassPathResource(classpath); |
Also available in: Unified diff
Can't use jena Query because does not support inference rule, can't use jena HttpEngine because it does not support long queryif we do not use Query: hence let's query with handmade post request to the sparql endpoint.