Mercurial > hg > AnnotationManager
comparison src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java @ 34:bd414fe235b5 default tip
now with 100% better sparql.
author | casties |
---|---|
date | Thu, 31 May 2012 21:32:30 +0200 |
parents | e5f5848892a2 |
children |
comparison
equal
deleted
inserted
replaced
33:e5f5848892a2 | 34:bd414fe235b5 |
---|---|
15 public class RDFSearcher { | 15 public class RDFSearcher { |
16 | 16 |
17 private String context; | 17 private String context; |
18 | 18 |
19 private Logger logger = Logger.getRootLogger(); | 19 private Logger logger = Logger.getRootLogger(); |
20 | |
21 static final String baseGraph = " ?annotation <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Annotation>." | |
22 + " ?annotation <" + NS.OAC_NS + "hasTarget> ?target." | |
23 + " ?annotation <" + NS.OAC_NS + "hasBody> ?body." | |
24 + " ?annotation <" + NS.DCTERMS_NS + "creator> ?creator." | |
25 + " ?annotation <" + NS.DCTERMS_NS + "created> ?created." | |
26 + " ?body <" + NS.CNT_NS + "chars> ?text." | |
27 + " ?target <" + NS.DCTERMS_NS + "isPartOf> ?uri."; | |
28 static final String optGraph = " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>}" | |
29 + " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>}" | |
30 + " OPTIONAL { ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>}"; | |
31 static final String nonOptGraph = " ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>." | |
32 + " ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>." | |
33 + " ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>."; | |
34 | |
35 static final String annotGraph = baseGraph + optGraph; | |
20 | 36 |
21 public RDFSearcher(String context) { | 37 public RDFSearcher(String context) { |
22 this.context = context; | 38 this.context = context; |
23 } | 39 } |
24 | 40 |
36 return null; | 52 return null; |
37 } | 53 } |
38 List<Annotation> retAnnots = new ArrayList<Annotation>(); | 54 List<Annotation> retAnnots = new ArrayList<Annotation>(); |
39 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); | 55 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); |
40 String queryString = ""; | 56 String queryString = ""; |
41 // query for tuples with id as subject | |
42 StringBuilder whereString = new StringBuilder(); | 57 StringBuilder whereString = new StringBuilder(); |
43 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id)); | 58 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id)); |
44 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri."); | 59 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri."); |
45 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id)); | 60 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id)); |
46 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText."); | 61 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText."); |
47 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id)); | 62 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id)); |
48 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id)); | 63 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id)); |
49 | 64 |
50 /* | 65 /* |
51 * StringBuilder whereString = new | 66 * StringBuilder whereString = new |
52 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id)); | 67 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id)); |
53 * whereString.append(String | 68 * whereString.append(String |
54 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id)); | 69 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id)); |
111 | 126 |
112 List<Annotation> retAnnots = new ArrayList<Annotation>(); | 127 List<Annotation> retAnnots = new ArrayList<Annotation>(); |
113 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); | 128 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); |
114 String queryString = ""; | 129 String queryString = ""; |
115 | 130 |
116 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>."; | 131 String whereString = annotGraph; |
117 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link."; | |
118 | 132 |
119 if (uri != null && !uri.equals("")) { | 133 if (uri != null && !uri.equals("")) { |
120 whereString = String.format("?target <http://www.purl.org/dc/terms/isPartOf> <%s>.", uri); | 134 whereString += String.format("FILTER (?uri = <%s>)", uri); |
121 } else { | 135 } |
122 whereString = "?target <http://www.purl.org/dc/terms/isPartOf> ?uri."; | |
123 } | |
124 whereString += "?annotation <http://www.openannotation.org/ns/hasTarget> ?target."; | |
125 whereString += "?annotation <http://www.openannotation.org/ns/hasBody> ?body."; | |
126 whereString += "?body <http://www.w3.org/2011/content#chars> ?bodyText."; | |
127 | 136 |
128 if (user != null && !user.equals("")) { | 137 if (user != null && !user.equals("")) { |
129 if (user.startsWith("http")) { | 138 if (user.startsWith("http")) { |
130 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> <%s>", user); | 139 whereString += String.format("FILTER (?creator = <%s>)", user); |
131 } else { | 140 } else { |
132 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> \"%s\".", user); | 141 whereString += String.format("FILTER (?creator = \"%s\")", user); |
133 } | 142 } |
134 } else { | 143 } |
135 whereString += "?annotation <http://www.purl.org/dc/terms/creator> ?userUrl."; | 144 |
136 } | 145 queryString = String.format("SELECT * FROM <%s> WHERE {%s}", context, whereString); |
137 | |
138 whereString += "?annotation <http://www.purl.org/dc/terms/created> ?creationDate"; | |
139 | |
140 queryString = String.format("select * from <%s> where {%s}", context, whereString); | |
141 | 146 |
142 logger.debug("RDFSearcher:" + queryString); | 147 logger.debug("RDFSearcher:" + queryString); |
143 | 148 |
144 try { | 149 try { |
145 TupleQueryResult results = th.querySPARQL(queryString); | 150 TupleQueryResult results = th.querySPARQL(queryString); |
150 if (uri != null && !uri.equals("")) { | 155 if (uri != null && !uri.equals("")) { |
151 annotatedUri = uri; | 156 annotatedUri = uri; |
152 } else { | 157 } else { |
153 annotatedUri = result.getBinding("uri").getValue().stringValue(); | 158 annotatedUri = result.getBinding("uri").getValue().stringValue(); |
154 } | 159 } |
155 | |
156 String annotUser; | 160 String annotUser; |
157 if (user != null && !user.equals("")) { | 161 if (user != null && !user.equals("")) { |
158 annotUser = user; | 162 annotUser = user; |
159 } else { | 163 } else { |
160 annotUser = result.getBinding("userUrl").getValue().stringValue(); | 164 annotUser = result.getBinding("creator").getValue().stringValue(); |
161 } | 165 } |
162 | |
163 String textString = ""; | 166 String textString = ""; |
164 if (result.getBinding("bodyText") != null) { | 167 if (result.getBinding("text") != null) { |
165 textString = result.getBinding("bodyText").getValue().stringValue(); | 168 textString = result.getBinding("text").getValue().stringValue(); |
166 } | 169 } |
167 String xpointer = result.getBinding("target").getValue().stringValue(); | 170 String xpointer = result.getBinding("target").getValue().stringValue(); |
168 String time = result.getBinding("creationDate").getValue().stringValue(); | 171 String time = result.getBinding("created").getValue().stringValue(); |
169 String annotationUri = result.getBinding("annotation").getValue().stringValue(); | 172 String annotationUri = result.getBinding("annotation").getValue().stringValue(); |
170 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri); | 173 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri); |
171 retAnnots.add(annot); | 174 retAnnots.add(annot); |
172 } | 175 } |
173 } catch (Exception e) { | 176 } catch (Exception e) { |
186 // delete triples with id as subject | 189 // delete triples with id as subject |
187 /* | 190 /* |
188 * wish Virtuoso would speak SparQL1.1... String queryString = | 191 * wish Virtuoso would speak SparQL1.1... String queryString = |
189 * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); | 192 * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); |
190 */ | 193 */ |
191 String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); | 194 String whereString = annotGraph + String.format(" FILTER(?annotation = <%s>)", id); |
195 String queryString = String.format("DELETE FROM <%s> {%s} WHERE {%s}", context, baseGraph+nonOptGraph, whereString); | |
192 | 196 |
193 logger.debug("RDFSearcher:" + queryString); | 197 logger.debug("RDFSearcher:" + queryString); |
194 | 198 |
195 try { | 199 try { |
196 th.querySPARQL(queryString); | 200 th.querySPARQL(queryString); |
197 } catch (Exception e) { | 201 } catch (Exception e) { |
198 e.printStackTrace(); | 202 e.printStackTrace(); |
199 throw new TripleStoreSearchError(); | 203 throw new TripleStoreSearchError(); |
200 } | 204 } |
201 } | 205 } |
202 /** | 206 |
203 * Sucht im Triplestore nach Annotationen | |
204 * | |
205 * @param uri | |
206 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource | |
207 * oder NULL oder leer | |
208 * @param user | |
209 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt | |
210 * wurden. | |
211 * @param limit | |
212 * @param offset | |
213 * @return | |
214 * @throws TripleStoreHandlerException | |
215 * @throws TripleStoreSearchError | |
216 */ | |
217 public List<Annotation> searchByUriUserAlt(String uri, String user, String limit, String offset) | |
218 throws TripleStoreHandlerException, TripleStoreSearchError { | |
219 | |
220 List<Annotation> retAnnots = new ArrayList<Annotation>(); | |
221 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); | |
222 String queryString = ""; | |
223 | |
224 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>."; | |
225 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link."; | |
226 | |
227 if (uri != null && !uri.equals("")) { | |
228 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.", | |
229 uri); | |
230 } else { | |
231 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri."); | |
232 } | |
233 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer."); | |
234 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText."); | |
235 | |
236 if (user != null && !user.equals("")) { | |
237 if (user.startsWith("http")) { | |
238 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.", user); | |
239 } else { | |
240 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".", user); | |
241 } | |
242 } else { | |
243 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author."); | |
244 } | |
245 | |
246 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created."); | |
247 | |
248 whereString += String | |
249 .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}"); | |
250 | |
251 queryString = String.format("select distinct * where {%s}", whereString); | |
252 | |
253 logger.debug("RDFSearcher:" + queryString); | |
254 | |
255 try { | |
256 TupleQueryResult results = th.querySPARQL(queryString); | |
257 | |
258 while (results.hasNext()) { | |
259 BindingSet result = results.next(); | |
260 String annotUri; | |
261 if (uri != null && !uri.equals("")) { | |
262 annotUri = uri; | |
263 } else { | |
264 annotUri = result.getBinding("uri").getValue().stringValue(); | |
265 } | |
266 | |
267 String annotUser; | |
268 if (user != null && !user.equals("")) { | |
269 annotUser = user; | |
270 } else { | |
271 annotUser = result.getBinding("author").getValue().stringValue(); | |
272 } | |
273 | |
274 String textString = ""; | |
275 if (result.getBinding("text") != null) { | |
276 textString = result.getBinding("text").getValue().stringValue(); | |
277 } | |
278 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result | |
279 .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s") | |
280 .getValue().stringValue()); | |
281 retAnnots.add(annot); | |
282 } | |
283 } catch (Exception e) { | |
284 e.printStackTrace(); | |
285 throw new TripleStoreSearchError(); | |
286 } | |
287 // TODO Auto-generated method stub | |
288 return retAnnots; | |
289 } | |
290 | 207 |
291 } | 208 } |