Revision 50690
Added by Alessia Bardi about 6 years ago
modules/dnet-parthenos-publisher/trunk/test/main/java/eu/dnetlib/parthenos/virtuoso/VirtuosoTest.java | ||
---|---|---|
8 | 8 |
import org.apache.jena.query.Query; |
9 | 9 |
import org.apache.jena.query.QueryExecutionFactory; |
10 | 10 |
import org.apache.jena.query.QueryFactory; |
11 |
import org.apache.jena.query.ResultSet; |
|
11 | 12 |
import org.apache.jena.rdf.model.Model; |
13 |
import org.apache.jena.rdf.model.Resource; |
|
12 | 14 |
import org.apache.jena.rdf.model.ResourceFactory; |
13 | 15 |
import org.apache.jena.sparql.engine.http.QueryEngineHTTP; |
14 | 16 |
import org.apache.jena.util.iterator.ExtendedIterator; |
... | ... | |
50 | 52 |
assertNotNull(graph); |
51 | 53 |
} |
52 | 54 |
|
55 |
@Ignore |
|
53 | 56 |
@Test |
54 | 57 |
public void testDescribe(){ |
55 | 58 |
//put something in the graph |
... | ... | |
59 | 62 |
//now query for the resource URI |
60 | 63 |
String describeQuery = "DESCRIBE <"+resURI+">"; |
61 | 64 |
Query query = QueryFactory.create(describeQuery) ; |
62 |
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest("http://localhost:8890/sparql", query); |
|
65 |
String sparqlUrl = "http://localhost:8890/sparql"; |
|
66 |
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query); |
|
63 | 67 |
Model resultModel = serviceRequest.execDescribe(); |
64 | 68 |
serviceRequest.close(); |
65 | 69 |
resultModel.write(System.out); |
... | ... | |
67 | 71 |
graph.clear(); |
68 | 72 |
} |
69 | 73 |
|
74 |
|
|
75 |
@Ignore |
|
70 | 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 |
|
71 | 128 |
public void testClear() { |
72 | 129 |
VirtGraph graph = new VirtGraph(testGraph, connectionString, testUser, testPwd); |
73 | 130 |
assertNotNull(graph); |
74 | 131 |
graph.clear(); |
75 | 132 |
Assert.assertTrue(graph.isEmpty()); |
133 |
|
|
76 | 134 |
} |
77 | 135 |
|
78 | 136 |
@Test |
modules/dnet-parthenos-publisher/trunk/src/main/java/eu/dnetlib/parthenos/virtuoso/VirtuosoReadAPI.java | ||
---|---|---|
1 |
package eu.dnetlib.parthenos.virtuoso; |
|
2 |
|
|
3 |
import java.io.OutputStream; |
|
4 |
import java.util.Iterator; |
|
5 |
import java.util.List; |
|
6 |
import javax.annotation.Nullable; |
|
7 |
|
|
8 |
import com.google.common.base.Function; |
|
9 |
import com.google.common.collect.Iterators; |
|
10 |
import com.google.common.collect.Lists; |
|
11 |
import org.apache.commons.logging.Log; |
|
12 |
import org.apache.commons.logging.LogFactory; |
|
13 |
import org.apache.jena.query.*; |
|
14 |
import org.apache.jena.rdf.model.Model; |
|
15 |
import org.apache.jena.sparql.engine.http.QueryEngineHTTP; |
|
16 |
import org.springframework.beans.factory.annotation.Value; |
|
17 |
import org.springframework.web.bind.annotation.*; |
|
18 |
|
|
19 |
/** |
|
20 |
* Created by Alessia Bardi on 31/01/2018. |
|
21 |
* Read-only API for virtuoso. |
|
22 |
* |
|
23 |
* //TODO: pagination |
|
24 |
* //TODO swagger documentation? |
|
25 |
* |
|
26 |
* @author Alessia Bardi |
|
27 |
*/ |
|
28 |
@RestController |
|
29 |
public class VirtuosoReadAPI { |
|
30 |
|
|
31 |
private static final Log log = LogFactory.getLog(VirtuosoReadAPI.class); |
|
32 |
|
|
33 |
@Value("${virtuoso.sparqlurl}") |
|
34 |
private String sparqlUrl; |
|
35 |
@Value("${virtuoso.pwd}") |
|
36 |
private String username; |
|
37 |
@Value("${virtuoso.pwd}") |
|
38 |
private String password; |
|
39 |
@Value("${virtuoso.uri.base.default}") |
|
40 |
private String defaultBaseURI; |
|
41 |
|
|
42 |
@RequestMapping(value = "/virtuoso/graphs", produces = { "application/json" }, method = RequestMethod.GET) |
|
43 |
public List<String> getGraphURLs(@RequestParam final String api) { |
|
44 |
String queryForGraphs = |
|
45 |
"SELECT DISTINCT ?g WHERE { { ?g <dnetcollectedFrom> <"+defaultBaseURI+api+"> }}"; |
|
46 |
|
|
47 |
log.debug(queryForGraphs); |
|
48 |
Query query = QueryFactory.create(queryForGraphs); |
|
49 |
final QueryEngineHTTP serviceRequest = QueryExecutionFactory.createServiceRequest(sparqlUrl, query); |
|
50 |
ResultSet graphs = serviceRequest.execSelect(); |
|
51 |
Iterator<String> s = Iterators.transform(graphs, new Function<QuerySolution, String>() { |
|
52 |
@Nullable |
|
53 |
@Override |
|
54 |
public String apply(@Nullable final QuerySolution qs) { |
|
55 |
return qs.getResource("g").getURI(); |
|
56 |
} |
|
57 |
}); |
|
58 |
List<String> res = Lists.newArrayList(s); |
|
59 |
serviceRequest.close(); |
|
60 |
return res; |
|
61 |
} |
|
62 |
|
|
63 |
@RequestMapping(value = "/virtuoso/subjects", produces = { "application/json" }, method = RequestMethod.GET) |
|
64 |
public List<String> getSubjectsForGraph(@RequestParam final String graph) { |
|
65 |
String queryForSubjectsTemplate = "SELECT DISTINCT ?s WHERE {GRAPH <%s> {?s ?p ?o . FILTER regex(str(?s),'^http')}}"; |
|
66 |
|
|
67 |
final QueryEngineHTTP serviceRequest = |
|
68 |
QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(String.format(queryForSubjectsTemplate, graph))); |
|
69 |
ResultSet subjects = serviceRequest.execSelect(); |
|
70 |
Iterator<String> s = Iterators.transform(subjects, new Function<QuerySolution, String>() { |
|
71 |
@Nullable |
|
72 |
@Override |
|
73 |
public String apply(@Nullable final QuerySolution qs) { |
|
74 |
return qs.getResource("s").getURI(); |
|
75 |
} |
|
76 |
}); |
|
77 |
List<String> res = Lists.newArrayList(s); |
|
78 |
serviceRequest.close(); |
|
79 |
return res; |
|
80 |
} |
|
81 |
|
|
82 |
@RequestMapping(value = "/virtuoso/subjects/{subject}", produces = { "application/rdf+xml" }, method = RequestMethod.GET) |
|
83 |
public void getSubjectsForGraph(@PathVariable final String subject, final OutputStream responseStream) { |
|
84 |
String describeQuery = "DESCRIBE <" + subject + ">"; |
|
85 |
|
|
86 |
final QueryEngineHTTP serviceRequest = |
|
87 |
QueryExecutionFactory.createServiceRequest(sparqlUrl, QueryFactory.create(describeQuery)); |
|
88 |
Model resultModel = serviceRequest.execDescribe(); |
|
89 |
resultModel.write(responseStream); |
|
90 |
serviceRequest.close(); |
|
91 |
} |
|
92 |
|
|
93 |
|
|
94 |
} |
Also available in: Unified diff
API that read from Virtuoso