Mercurial > hg > svcore
comparison rdf/PluginRDFDescription.cpp @ 725:c789deb83bd4 dataquay
Convert PluginRDFDescription and PluginRDFIndexer to use Dataquay
author | Chris Cannam |
---|---|
date | Fri, 18 May 2012 14:45:15 +0100 |
parents | f3fd2988fc9b |
children | 211efc770335 |
comparison
equal
deleted
inserted
replaced
724:7eb389da7976 | 725:c789deb83bd4 |
---|---|
14 */ | 14 */ |
15 | 15 |
16 #include "PluginRDFDescription.h" | 16 #include "PluginRDFDescription.h" |
17 | 17 |
18 #include "PluginRDFIndexer.h" | 18 #include "PluginRDFIndexer.h" |
19 #include "SimpleSPARQLQuery.h" | |
20 | 19 |
21 #include "base/Profiler.h" | 20 #include "base/Profiler.h" |
22 | 21 |
23 #include "plugin/PluginIdentifier.h" | 22 #include "plugin/PluginIdentifier.h" |
23 | |
24 #include <dataquay/BasicStore.h> | |
24 | 25 |
25 #include <iostream> | 26 #include <iostream> |
26 using std::cerr; | 27 using std::cerr; |
27 using std::endl; | 28 using std::endl; |
29 | |
30 using Dataquay::Uri; | |
31 using Dataquay::Node; | |
32 using Dataquay::Nodes; | |
33 using Dataquay::Triple; | |
34 using Dataquay::Triples; | |
35 using Dataquay::BasicStore; | |
28 | 36 |
29 PluginRDFDescription::PluginRDFDescription(QString pluginId) : | 37 PluginRDFDescription::PluginRDFDescription(QString pluginId) : |
30 m_pluginId(pluginId), | 38 m_pluginId(pluginId), |
31 m_haveDescription(false) | 39 m_haveDescription(false) |
32 { | 40 { |
170 bool | 178 bool |
171 PluginRDFDescription::indexMetadata() | 179 PluginRDFDescription::indexMetadata() |
172 { | 180 { |
173 Profiler profiler("PluginRDFDescription::index"); | 181 Profiler profiler("PluginRDFDescription::index"); |
174 | 182 |
175 SimpleSPARQLQuery::QueryType m = SimpleSPARQLQuery::QueryFromModel; | 183 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); |
176 | 184 const BasicStore *index = indexer->getIndex(); |
177 QString queryTemplate = | 185 Uri plugin(m_pluginUri); |
178 QString( | 186 |
179 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | 187 Node n = index->matchFirst(Triple(plugin, "vamp:name", Node())).c; |
180 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " | 188 if (n.type == Node::Literal && n.value != "") { |
181 " PREFIX dc: <http://purl.org/dc/elements/1.1/> " | 189 m_pluginName = n.value; |
182 " SELECT ?%3 " | 190 } |
183 " WHERE { " | 191 |
184 " <%1> %2 ?%3 . " | 192 n = index->matchFirst(Triple(plugin, "dc:description", Node())).c; |
185 " }") | 193 if (n.type == Node::Literal && n.value != "") { |
186 .arg(m_pluginUri); | 194 m_pluginDescription = n.value; |
187 | 195 } |
188 SimpleSPARQLQuery::Value v; | 196 |
189 | 197 n = index->matchFirst(Triple(plugin, "foaf:maker", Node())).c; |
190 v = SimpleSPARQLQuery::singleResultQuery | 198 if (n.type == Node::URI || n.type == Node::Blank) { |
191 (m, queryTemplate.arg("vamp:name").arg("name"), "name"); | 199 n = index->matchFirst(Triple(n, "foaf:name", Node())).c; |
192 | 200 if (n.type == Node::Literal && n.value != "") { |
193 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { | 201 m_pluginMaker = n.value; |
194 m_pluginName = v.value; | 202 } |
195 } | |
196 | |
197 v = SimpleSPARQLQuery::singleResultQuery | |
198 (m, queryTemplate.arg("dc:description").arg("description"), "description"); | |
199 | |
200 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { | |
201 m_pluginDescription = v.value; | |
202 } | |
203 | |
204 v = SimpleSPARQLQuery::singleResultQuery | |
205 (m, | |
206 QString( | |
207 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
208 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " | |
209 " SELECT ?name " | |
210 " WHERE { " | |
211 " <%1> foaf:maker ?maker . " | |
212 " ?maker foaf:name ?name . " | |
213 " }") | |
214 .arg(m_pluginUri), | |
215 "name"); | |
216 | |
217 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { | |
218 m_pluginMaker = v.value; | |
219 } | 203 } |
220 | 204 |
221 // If we have a more-information URL for this plugin, then we take | 205 // If we have a more-information URL for this plugin, then we take |
222 // that. Otherwise, a more-information URL for the plugin | 206 // that. Otherwise, a more-information URL for the plugin library |
223 // library would do nicely. Failing that, we could perhaps use | 207 // would do nicely. |
224 // any foaf:page URL at all that appears in the file -- but | 208 |
225 // perhaps that would be unwise | 209 n = index->matchFirst(Triple(plugin, "foaf:page", Node())).c; |
226 | 210 if (n.type == Node::URI && n.value != "") { |
227 v = SimpleSPARQLQuery::singleResultQuery | 211 m_pluginInfoURL = n.value; |
228 (m, | 212 } |
229 QString( | 213 |
230 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | 214 n = index->matchFirst(Triple(Node(), "vamp:available_plugin", plugin)).a; |
231 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " | 215 if (n.value != "") { |
232 " SELECT ?page " | 216 n = index->matchFirst(Triple(n, "foaf:page", Node())).c; |
233 " WHERE { " | 217 if (n.type == Node::URI && n.value != "") { |
234 " <%1> foaf:page ?page . " | 218 m_pluginInfoURL = n.value; |
235 " }") | |
236 .arg(m_pluginUri), | |
237 "page"); | |
238 | |
239 if (v.type == SimpleSPARQLQuery::URIValue && v.value != "") { | |
240 | |
241 m_pluginInfoURL = v.value; | |
242 | |
243 } else { | |
244 | |
245 v = SimpleSPARQLQuery::singleResultQuery | |
246 (m, | |
247 QString( | |
248 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
249 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " | |
250 " SELECT ?page " | |
251 " WHERE { " | |
252 " ?library vamp:available_plugin <%1> ; " | |
253 " a vamp:PluginLibrary ; " | |
254 " foaf:page ?page . " | |
255 " }") | |
256 .arg(m_pluginUri), | |
257 "page"); | |
258 | |
259 if (v.type == SimpleSPARQLQuery::URIValue && v.value != "") { | |
260 | |
261 m_pluginInfoURL = v.value; | |
262 } | 219 } |
263 } | 220 } |
264 | 221 |
265 return true; | 222 return true; |
266 } | 223 } |
268 bool | 225 bool |
269 PluginRDFDescription::indexOutputs() | 226 PluginRDFDescription::indexOutputs() |
270 { | 227 { |
271 Profiler profiler("PluginRDFDescription::indexOutputs"); | 228 Profiler profiler("PluginRDFDescription::indexOutputs"); |
272 | 229 |
273 SimpleSPARQLQuery::QueryType m = SimpleSPARQLQuery::QueryFromModel; | 230 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); |
274 | 231 const BasicStore *index = indexer->getIndex(); |
275 SimpleSPARQLQuery query | 232 Uri plugin(m_pluginUri); |
276 (m, | 233 |
277 QString | 234 Nodes outputs = index->match(Triple(plugin, "vamp:output", Node())).a(); |
278 ( | 235 |
279 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | 236 if (outputs.empty()) { |
280 | |
281 " SELECT ?output " | |
282 | |
283 " WHERE { " | |
284 " <%1> vamp:output ?output . " | |
285 " } " | |
286 ) | |
287 .arg(m_pluginUri)); | |
288 | |
289 SimpleSPARQLQuery::ResultList results = query.execute(); | |
290 | |
291 if (!query.isOK()) { | |
292 cerr << "ERROR: PluginRDFDescription::index: ERROR: Failed to query outputs for <" | |
293 << m_pluginUri << ">: " | |
294 << query.getErrorString() << endl; | |
295 return false; | |
296 } | |
297 | |
298 if (results.empty()) { | |
299 cerr << "ERROR: PluginRDFDescription::indexURL: NOTE: No outputs defined for <" | 237 cerr << "ERROR: PluginRDFDescription::indexURL: NOTE: No outputs defined for <" |
300 << m_pluginUri << ">" << endl; | 238 << m_pluginUri << ">" << endl; |
301 return false; | 239 return false; |
302 } | 240 } |
303 | 241 |
304 // Note that an output may appear more than once, if it inherits | 242 foreach (Node output, outputs) { |
305 // more than one type (e.g. DenseOutput and QuantizedOutput). So | 243 |
306 // these results must accumulate | 244 if ((output.type != Node::URI && output.type != Node::Blank) || |
307 | 245 output.value == "") { |
308 for (int i = 0; i < results.size(); ++i) { | 246 cerr << "ERROR: PluginRDFDescription::indexURL: No valid URI for output " << output << " of plugin <" << m_pluginUri << ">" << endl; |
309 | |
310 if (results[i]["output"].type != SimpleSPARQLQuery::URIValue || | |
311 results[i]["output"].value == "") { | |
312 cerr << "ERROR: PluginRDFDescription::indexURL: No valid URI for output " << i << " of plugin <" << m_pluginUri << ">" << endl; | |
313 return false; | 247 return false; |
314 } | 248 } |
315 | 249 |
316 QString outputUri = results[i]["output"].value; | 250 Node n = index->matchFirst(Triple(output, "vamp:identifier", Node())).c; |
317 | 251 if (n.type != Node::Literal || n.value == "") { |
318 SimpleSPARQLQuery::Value v; | 252 cerr << "ERROR: PluginRDFDescription::indexURL: No vamp:identifier for output <" << output << ">" << endl; |
319 | |
320 v = SimpleSPARQLQuery::singleResultQuery | |
321 (m, | |
322 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
323 " SELECT ?output_id " | |
324 " WHERE { <%1> vamp:identifier ?output_id } ") | |
325 .arg(outputUri), "output_id"); | |
326 | |
327 if (v.type != SimpleSPARQLQuery::LiteralValue || v.value == "") { | |
328 cerr << "ERROR: PluginRDFDescription::indexURL: No identifier for output <" << outputUri << ">" << endl; | |
329 return false; | 253 return false; |
330 } | 254 } |
331 QString outputId = v.value; | 255 QString outputId = n.value; |
332 | 256 |
333 v = SimpleSPARQLQuery::singleResultQuery | 257 n = index->matchFirst(Triple(output, "a", Node())).c; |
334 (m, | |
335 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
336 " SELECT ?output_type " | |
337 " WHERE { <%1> a ?output_type } ") | |
338 .arg(outputUri), "output_type"); | |
339 | |
340 QString outputType; | 258 QString outputType; |
341 if (v.type == SimpleSPARQLQuery::URIValue) outputType = v.value; | 259 if (n.type == Node::URI) outputType = n.value; |
342 | 260 |
343 v = SimpleSPARQLQuery::singleResultQuery | 261 n = index->matchFirst(Triple(output, "vamp:unit", Node())).c; |
344 (m, | |
345 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
346 " SELECT ?unit " | |
347 " WHERE { <%1> vamp:unit ?unit } ") | |
348 .arg(outputUri), "unit"); | |
349 | |
350 QString outputUnit; | 262 QString outputUnit; |
351 if (v.type == SimpleSPARQLQuery::LiteralValue) outputUnit = v.value; | 263 if (n.type == Node::Literal) outputUnit = n.value; |
352 | 264 |
353 m_outputUriMap[outputId] = outputUri; | 265 m_outputUriMap[outputId] = output.value; |
354 | 266 |
355 if (outputType.contains("DenseOutput")) { | 267 if (outputType.contains("DenseOutput")) { |
356 m_outputDispositions[outputId] = OutputDense; | 268 m_outputDispositions[outputId] = OutputDense; |
357 } else if (outputType.contains("SparseOutput")) { | 269 } else if (outputType.contains("SparseOutput")) { |
358 m_outputDispositions[outputId] = OutputSparse; | 270 m_outputDispositions[outputId] = OutputSparse; |
364 | 276 |
365 if (outputUnit != "") { | 277 if (outputUnit != "") { |
366 m_outputUnitMap[outputId] = outputUnit; | 278 m_outputUnitMap[outputId] = outputUnit; |
367 } | 279 } |
368 | 280 |
369 v = SimpleSPARQLQuery::singleResultQuery | 281 n = index->matchFirst(Triple(output, "dc:title", Node())).c; |
370 (m, | 282 if (n.type == Node::Literal && n.value != "") { |
371 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " | 283 m_outputNames[outputId] = n.value; |
372 " PREFIX dc: <http://purl.org/dc/elements/1.1/> " | 284 } |
373 " SELECT ?title " | 285 |
374 " WHERE { <%1> dc:title ?title } ") | 286 n = index->matchFirst(Triple(output, "vamp:computes_event_type", Node())).c; |
375 .arg(outputUri), "title"); | 287 if (n.type == Node::URI && n.value != "") { |
376 | 288 m_outputEventTypeURIMap[outputId] = n.value; |
377 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { | 289 } |
378 m_outputNames[outputId] = v.value; | 290 |
379 } | 291 n = index->matchFirst(Triple(output, "vamp:computes_feature", Node())).c; |
380 | 292 if (n.type == Node::URI && n.value != "") { |
381 QString queryTemplate = | 293 m_outputFeatureAttributeURIMap[outputId] = n.value; |
382 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " | 294 } |
383 " SELECT ?%2 " | 295 |
384 " WHERE { <%1> vamp:computes_%2 ?%2 } ") | 296 n = index->matchFirst(Triple(output, "vamp:computes_signal_type", Node())).c; |
385 .arg(outputUri); | 297 if (n.type == Node::URI && n.value != "") { |
386 | 298 m_outputSignalTypeURIMap[outputId] = n.value; |
387 v = SimpleSPARQLQuery::singleResultQuery | |
388 (m, queryTemplate.arg("event_type"), "event_type"); | |
389 | |
390 if (v.type == SimpleSPARQLQuery::URIValue && v.value != "") { | |
391 m_outputEventTypeURIMap[outputId] = v.value; | |
392 } | |
393 | |
394 v = SimpleSPARQLQuery::singleResultQuery | |
395 (m, queryTemplate.arg("feature"), "feature"); | |
396 | |
397 if (v.type == SimpleSPARQLQuery::URIValue && v.value != "") { | |
398 m_outputFeatureAttributeURIMap[outputId] = v.value; | |
399 } | |
400 | |
401 v = SimpleSPARQLQuery::singleResultQuery | |
402 (m, queryTemplate.arg("signal_type"), "signal_type"); | |
403 | |
404 if (v.type == SimpleSPARQLQuery::URIValue && v.value != "") { | |
405 m_outputSignalTypeURIMap[outputId] = v.value; | |
406 } | 299 } |
407 } | 300 } |
408 | 301 |
409 return true; | 302 return true; |
410 } | 303 } |