comparison rdf/PluginRDFIndexer.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
13 COPYING included with this distribution for more information. 13 COPYING included with this distribution for more information.
14 */ 14 */
15 15
16 #include "PluginRDFIndexer.h" 16 #include "PluginRDFIndexer.h"
17 17
18 #include "SimpleSPARQLQuery.h"
19
20 #include "data/fileio/CachedFile.h" 18 #include "data/fileio/CachedFile.h"
21 #include "data/fileio/FileSource.h" 19 #include "data/fileio/FileSource.h"
22 #include "data/fileio/PlaylistFileReader.h" 20 #include "data/fileio/PlaylistFileReader.h"
23 #include "plugin/PluginIdentifier.h" 21 #include "plugin/PluginIdentifier.h"
24 22
25 #include "base/Profiler.h" 23 #include "base/Profiler.h"
26 24
27 #include <vamp-hostsdk/PluginHostAdapter.h> 25 #include <vamp-hostsdk/PluginHostAdapter.h>
26
27 #include <dataquay/BasicStore.h>
28 #include <dataquay/RDFException.h>
28 29
29 #include <QFileInfo> 30 #include <QFileInfo>
30 #include <QDir> 31 #include <QDir>
31 #include <QUrl> 32 #include <QUrl>
32 #include <QDateTime> 33 #include <QDateTime>
38 using std::endl; 39 using std::endl;
39 using std::vector; 40 using std::vector;
40 using std::string; 41 using std::string;
41 using Vamp::PluginHostAdapter; 42 using Vamp::PluginHostAdapter;
42 43
44 using Dataquay::Uri;
45 using Dataquay::Node;
46 using Dataquay::Nodes;
47 using Dataquay::Triple;
48 using Dataquay::Triples;
49 using Dataquay::BasicStore;
50 using Dataquay::RDFException;
51 using Dataquay::RDFDuplicateImportException;
52
43 PluginRDFIndexer * 53 PluginRDFIndexer *
44 PluginRDFIndexer::m_instance = 0; 54 PluginRDFIndexer::m_instance = 0;
45 55
46 bool
47 PluginRDFIndexer::m_prefixesLoaded = false;
48
49 PluginRDFIndexer * 56 PluginRDFIndexer *
50 PluginRDFIndexer::getInstance() 57 PluginRDFIndexer::getInstance()
51 { 58 {
52 if (!m_instance) m_instance = new PluginRDFIndexer(); 59 if (!m_instance) m_instance = new PluginRDFIndexer();
53 return m_instance; 60 return m_instance;
54 } 61 }
55 62
56 PluginRDFIndexer::PluginRDFIndexer() 63 PluginRDFIndexer::PluginRDFIndexer() :
57 { 64 m_index(new Dataquay::BasicStore)
65 {
66 m_index->addPrefix("vamp", Uri("http://purl.org/ontology/vamp/"));
67 m_index->addPrefix("foaf", Uri("http://xmlns.com/foaf/0.1/"));
68 m_index->addPrefix("dc", Uri("http://purl.org/dc/elements/1.1/"));
58 indexInstalledURLs(); 69 indexInstalledURLs();
70 }
71
72 const BasicStore *
73 PluginRDFIndexer::getIndex()
74 {
75 return m_index;
59 } 76 }
60 77
61 PluginRDFIndexer::~PluginRDFIndexer() 78 PluginRDFIndexer::~PluginRDFIndexer()
62 { 79 {
63 QMutexLocker locker(&m_mutex); 80 QMutexLocker locker(&m_mutex);
239 bool 256 bool
240 PluginRDFIndexer::pullURL(QString urlString) 257 PluginRDFIndexer::pullURL(QString urlString)
241 { 258 {
242 Profiler profiler("PluginRDFIndexer::indexURL"); 259 Profiler profiler("PluginRDFIndexer::indexURL");
243 260
244 loadPrefixes();
245
246 // SVDEBUG << "PluginRDFIndexer::indexURL(" << urlString << ")" << endl; 261 // SVDEBUG << "PluginRDFIndexer::indexURL(" << urlString << ")" << endl;
247 262
248 QMutexLocker locker(&m_mutex); 263 QMutexLocker locker(&m_mutex);
249 264
250 QString localString = urlString; 265 QUrl local = urlString;
251 266
252 if (FileSource::isRemote(urlString) && 267 if (FileSource::isRemote(urlString) &&
253 FileSource::canHandleScheme(urlString)) { 268 FileSource::canHandleScheme(urlString)) {
254 269
255 CachedFile cf(urlString, 0, "application/rdf+xml"); 270 CachedFile cf(urlString, 0, "application/rdf+xml");
256 if (!cf.isOK()) { 271 if (!cf.isOK()) {
257 return false; 272 return false;
258 } 273 }
259 274
260 localString = QUrl::fromLocalFile(cf.getLocalFilename()).toString(); 275 local = QUrl::fromLocalFile(cf.getLocalFilename());
261 } 276
262 277 } else {
263 return SimpleSPARQLQuery::addSourceToModel(localString); 278
279 local = QUrl::fromLocalFile(urlString);
280 }
281
282 try {
283 m_index->import(local, BasicStore::ImportFailOnDuplicates);
284 } catch (RDFDuplicateImportException &e) {
285 cerr << "PluginRDFIndexer::pullURL: Document at " << urlString
286 << " duplicates triples found in earlier loaded document" << endl;
287 return false;
288 } catch (RDFException &e) {
289 cerr << "PluginRDFIndexer::pullURL: Failed to import document from "
290 << urlString << ": " << e.what() << endl;
291 return false;
292 }
293 return true;
264 } 294 }
265 295
266 bool 296 bool
267 PluginRDFIndexer::reindex() 297 PluginRDFIndexer::reindex()
268 { 298 {
269 SimpleSPARQLQuery::QueryType m = SimpleSPARQLQuery::QueryFromModel; 299 Triples tt = m_index->match
270 300 (Triple(Node(), "a", m_index->expand("vamp:Plugin")));
271 SimpleSPARQLQuery query 301 Nodes plugins = tt.a();
272 (m,
273 QString
274 (
275 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
276
277 " SELECT ?plugin ?library ?plugin_id "
278
279 " WHERE { "
280 " ?plugin a vamp:Plugin . "
281 " ?plugin vamp:identifier ?plugin_id . "
282
283 " OPTIONAL { "
284 " ?library vamp:available_plugin ?plugin "
285 " } "
286 " } "
287 ));
288
289 SimpleSPARQLQuery::ResultList results = query.execute();
290
291 if (!query.isOK()) {
292 cerr << "ERROR: PluginRDFIndexer::reindex: ERROR: Failed to query plugins from model: "
293 << query.getErrorString() << endl;
294 return false;
295 }
296
297 if (results.empty()) {
298 cerr << "PluginRDFIndexer::reindex: NOTE: no vamp:Plugin resources found in indexed documents" << endl;
299 return false;
300 }
301 302
302 bool foundSomething = false; 303 bool foundSomething = false;
303 bool addedSomething = false; 304 bool addedSomething = false;
304 305
305 for (SimpleSPARQLQuery::ResultList::iterator i = results.begin(); 306 foreach (Node plugin, plugins) {
306 i != results.end(); ++i) { 307
307 308 if (plugin.type != Node::URI) {
308 QString pluginUri = (*i)["plugin"].value; 309 cerr << "PluginRDFIndexer::reindex: Plugin has no URI: node is "
309 QString soUri = (*i)["library"].value; 310 << plugin << endl;
310 QString identifier = (*i)["plugin_id"].value; 311 continue;
311 312 }
312 if (identifier == "") { 313
313 cerr << "PluginRDFIndexer::reindex: NOTE: No vamp:identifier for plugin <" 314 Triple idt = m_index->matchFirst
314 << pluginUri << ">" 315 (Triple(plugin, "vamp:identifier", Node()));
315 << endl; 316
316 continue; 317 if (idt.c.type != Node::Literal) {
317 } 318 cerr << "PluginRDFIndexer::reindex: Plugin " << plugin
318 if (soUri == "") { 319 << " lacks vamp:identifier literal" << endl;
319 cerr << "PluginRDFIndexer::reindex: NOTE: No implementation library for plugin <" 320 continue;
320 << pluginUri << ">" 321 }
321 << endl; 322
322 continue; 323 Triple libt = m_index->matchFirst
323 } 324 (Triple(Node(), "vamp:available_plugin", plugin));
324 325
325 QString sonameQuery = 326 if (libt.a.type != Node::URI) {
326 QString( 327 cerr << "PluginRDFIndexer::reindex: Plugin " << plugin
327 " PREFIX vamp: <http://purl.org/ontology/vamp/> " 328 << " is not vamp:available_plugin in any library" << endl;
328 " SELECT ?library_id " 329 continue;
329 " WHERE { " 330 }
330 " <%1> vamp:identifier ?library_id " 331
331 " } " 332 Triple sot = m_index->matchFirst
332 ) 333 (Triple(libt.a, "vamp:identifier", Node()));
333 .arg(soUri); 334
334 335 if (sot.c.type != Node::Literal) {
335 SimpleSPARQLQuery::Value sonameValue = 336 cerr << "PluginRDFIndexer::reindex: Library " << libt.a
336 SimpleSPARQLQuery::singleResultQuery(m, sonameQuery, "library_id"); 337 << " lacks vamp:identifier for soname" << endl;
337 QString soname = sonameValue.value; 338 continue;
338 if (soname == "") { 339 }
339 cerr << "PluginRDFIndexer::reindex: NOTE: No identifier for library <" 340
340 << soUri << ">" 341 QString pluginUri = plugin.value;
341 << endl; 342 QString identifier = idt.c.value;
342 continue; 343 QString soname = sot.c.value;
343 }
344 344
345 QString pluginId = PluginIdentifier::createIdentifier 345 QString pluginId = PluginIdentifier::createIdentifier
346 ("vamp", soname, identifier); 346 ("vamp", soname, identifier);
347 347
348 foundSomething = true; 348 foundSomething = true;
371 cerr << "PluginRDFIndexer::reindex: NOTE: Plugins found, but none sufficiently described" << endl; 371 cerr << "PluginRDFIndexer::reindex: NOTE: Plugins found, but none sufficiently described" << endl;
372 } 372 }
373 373
374 return addedSomething; 374 return addedSomething;
375 } 375 }
376
377 void
378 PluginRDFIndexer::loadPrefixes()
379 {
380 return;
381 //!!!
382 if (m_prefixesLoaded) return;
383 const char *prefixes[] = {
384 "http://purl.org/ontology/vamp/"
385 };
386 for (size_t i = 0; i < sizeof(prefixes)/sizeof(prefixes[0]); ++i) {
387 CachedFile cf(prefixes[i], 0, "application/rdf+xml");
388 if (!cf.isOK()) continue;
389 SimpleSPARQLQuery::addSourceToModel
390 (QUrl::fromLocalFile(cf.getLocalFilename()).toString());
391 }
392 m_prefixesLoaded = true;
393 }
394
395