comparison rdf/PluginRDFDescription.cpp @ 739:51d3b8e816b8

Merge from branch "dataquay". Next release will require Dataquay v0.9
author Chris Cannam
date Fri, 25 May 2012 14:58:21 +0100
parents 87a9168efd71
children e802e550a1f2
comparison
equal deleted inserted replaced
735:66c3f4e060e9 739:51d3b8e816b8
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2008 QMUL. 7 This file copyright 2008-2012 QMUL.
8 8
9 This program is free software; you can redistribute it and/or 9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as 10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the 11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
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->complete
180 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " 188 (Triple(plugin, index->expand("vamp:name"), Node()));
181 " PREFIX dc: <http://purl.org/dc/elements/1.1/> " 189
182 " SELECT ?%3 " 190 if (n.type == Node::Literal && n.value != "") {
183 " WHERE { " 191 m_pluginName = n.value;
184 " <%1> %2 ?%3 . " 192 }
185 " }") 193
186 .arg(m_pluginUri); 194 n = index->complete
187 195 (Triple(plugin, index->expand("dc:description"), Node()));
188 SimpleSPARQLQuery::Value v; 196
189 197 if (n.type == Node::Literal && n.value != "") {
190 v = SimpleSPARQLQuery::singleResultQuery 198 m_pluginDescription = n.value;
191 (m, queryTemplate.arg("vamp:name").arg("name"), "name"); 199 }
192 200
193 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { 201 n = index->complete
194 m_pluginName = v.value; 202 (Triple(plugin, index->expand("foaf:maker"), Node()));
195 } 203
196 204 if (n.type == Node::URI || n.type == Node::Blank) {
197 v = SimpleSPARQLQuery::singleResultQuery 205 n = index->complete(Triple(n, index->expand("foaf:name"), Node()));
198 (m, queryTemplate.arg("dc:description").arg("description"), "description"); 206 if (n.type == Node::Literal && n.value != "") {
199 207 m_pluginMaker = n.value;
200 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { 208 }
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 } 209 }
220 210
221 // If we have a more-information URL for this plugin, then we take 211 // If we have a more-information URL for this plugin, then we take
222 // that. Otherwise, a more-information URL for the plugin 212 // that. Otherwise, a more-information URL for the plugin library
223 // library would do nicely. Failing that, we could perhaps use 213 // would do nicely.
224 // any foaf:page URL at all that appears in the file -- but 214
225 // perhaps that would be unwise 215 n = index->complete
226 216 (Triple(plugin, index->expand("foaf:page"), Node()));
227 v = SimpleSPARQLQuery::singleResultQuery 217
228 (m, 218 if (n.type == Node::URI && n.value != "") {
229 QString( 219 m_pluginInfoURL = n.value;
230 " PREFIX vamp: <http://purl.org/ontology/vamp/> " 220 }
231 " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " 221
232 " SELECT ?page " 222 n = index->complete
233 " WHERE { " 223 (Triple(Node(), index->expand("vamp:available_plugin"), plugin));
234 " <%1> foaf:page ?page . " 224
235 " }") 225 if (n.value != "") {
236 .arg(m_pluginUri), 226 n = index->complete(Triple(n, index->expand("foaf:page"), Node()));
237 "page"); 227 if (n.type == Node::URI && n.value != "") {
238 228 m_pluginInfoURL = n.value;
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 } 229 }
263 } 230 }
264 231
265 return true; 232 return true;
266 } 233 }
268 bool 235 bool
269 PluginRDFDescription::indexOutputs() 236 PluginRDFDescription::indexOutputs()
270 { 237 {
271 Profiler profiler("PluginRDFDescription::indexOutputs"); 238 Profiler profiler("PluginRDFDescription::indexOutputs");
272 239
273 SimpleSPARQLQuery::QueryType m = SimpleSPARQLQuery::QueryFromModel; 240 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance();
274 241 const BasicStore *index = indexer->getIndex();
275 SimpleSPARQLQuery query 242 Uri plugin(m_pluginUri);
276 (m, 243
277 QString 244 Nodes outputs = index->match
278 ( 245 (Triple(plugin, index->expand("vamp:output"), Node())).objects();
279 " PREFIX vamp: <http://purl.org/ontology/vamp/> " 246
280 247 if (outputs.empty()) {
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 <" 248 cerr << "ERROR: PluginRDFDescription::indexURL: NOTE: No outputs defined for <"
300 << m_pluginUri << ">" << endl; 249 << m_pluginUri << ">" << endl;
301 return false; 250 return false;
302 } 251 }
303 252
304 // Note that an output may appear more than once, if it inherits 253 foreach (Node output, outputs) {
305 // more than one type (e.g. DenseOutput and QuantizedOutput). So 254
306 // these results must accumulate 255 if ((output.type != Node::URI && output.type != Node::Blank) ||
307 256 output.value == "") {
308 for (int i = 0; i < results.size(); ++i) { 257 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; 258 return false;
314 } 259 }
315 260
316 QString outputUri = results[i]["output"].value; 261 Node n = index->complete(Triple(output, index->expand("vamp:identifier"), Node()));
317 262 if (n.type != Node::Literal || n.value == "") {
318 SimpleSPARQLQuery::Value v; 263 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; 264 return false;
330 } 265 }
331 QString outputId = v.value; 266 QString outputId = n.value;
332 267
333 v = SimpleSPARQLQuery::singleResultQuery 268 m_outputUriMap[outputId] = output.value;
334 (m, 269
335 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " 270 n = index->complete(Triple(output, Uri("a"), Node()));
336 " SELECT ?output_type "
337 " WHERE { <%1> a ?output_type } ")
338 .arg(outputUri), "output_type");
339
340 QString outputType; 271 QString outputType;
341 if (v.type == SimpleSPARQLQuery::URIValue) outputType = v.value; 272 if (n.type == Node::URI) outputType = n.value;
342 273
343 v = SimpleSPARQLQuery::singleResultQuery 274 n = index->complete(Triple(output, index->expand("vamp:unit"), Node()));
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; 275 QString outputUnit;
351 if (v.type == SimpleSPARQLQuery::LiteralValue) outputUnit = v.value; 276 if (n.type == Node::Literal) outputUnit = n.value;
352
353 m_outputUriMap[outputId] = outputUri;
354 277
355 if (outputType.contains("DenseOutput")) { 278 if (outputType.contains("DenseOutput")) {
356 m_outputDispositions[outputId] = OutputDense; 279 m_outputDispositions[outputId] = OutputDense;
357 } else if (outputType.contains("SparseOutput")) { 280 } else if (outputType.contains("SparseOutput")) {
358 m_outputDispositions[outputId] = OutputSparse; 281 m_outputDispositions[outputId] = OutputSparse;
359 } else if (outputType.contains("TrackLevelOutput")) { 282 } else if (outputType.contains("TrackLevelOutput")) {
360 m_outputDispositions[outputId] = OutputTrackLevel; 283 m_outputDispositions[outputId] = OutputTrackLevel;
361 } else { 284 } else {
362 m_outputDispositions[outputId] = OutputDispositionUnknown; 285 m_outputDispositions[outputId] = OutputDispositionUnknown;
363 } 286 }
287 // cerr << "output " << output << " -> id " << outputId << ", type " << outputType << ", unit "
288 // << outputUnit << ", disposition " << m_outputDispositions[outputId] << endl;
364 289
365 if (outputUnit != "") { 290 if (outputUnit != "") {
366 m_outputUnitMap[outputId] = outputUnit; 291 m_outputUnitMap[outputId] = outputUnit;
367 } 292 }
368 293
369 v = SimpleSPARQLQuery::singleResultQuery 294 n = index->complete(Triple(output, index->expand("dc:title"), Node()));
370 (m, 295 if (n.type == Node::Literal && n.value != "") {
371 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " 296 m_outputNames[outputId] = n.value;
372 " PREFIX dc: <http://purl.org/dc/elements/1.1/> " 297 }
373 " SELECT ?title " 298
374 " WHERE { <%1> dc:title ?title } ") 299 n = index->complete(Triple(output, index->expand("vamp:computes_event_type"), Node()));
375 .arg(outputUri), "title"); 300 // cerr << output << " -> computes_event_type " << n << endl;
376 301 if (n.type == Node::URI && n.value != "") {
377 if (v.type == SimpleSPARQLQuery::LiteralValue && v.value != "") { 302 m_outputEventTypeURIMap[outputId] = n.value;
378 m_outputNames[outputId] = v.value; 303 }
379 } 304
380 305 n = index->complete(Triple(output, index->expand("vamp:computes_feature"), Node()));
381 QString queryTemplate = 306 if (n.type == Node::URI && n.value != "") {
382 QString(" PREFIX vamp: <http://purl.org/ontology/vamp/> " 307 m_outputFeatureAttributeURIMap[outputId] = n.value;
383 " SELECT ?%2 " 308 }
384 " WHERE { <%1> vamp:computes_%2 ?%2 } ") 309
385 .arg(outputUri); 310 n = index->complete(Triple(output, index->expand("vamp:computes_signal_type"), Node()));
386 311 if (n.type == Node::URI && n.value != "") {
387 v = SimpleSPARQLQuery::singleResultQuery 312 m_outputSignalTypeURIMap[outputId] = n.value;
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 } 313 }
407 } 314 }
408 315
409 return true; 316 return true;
410 } 317 }