annotate rdf/PluginRDFIndexer.cpp @ 730:27c861cce97b dataquay

Further fixes for Dataquay compatibility
author Chris Cannam
date Mon, 21 May 2012 14:33:35 +0100
parents 211efc770335
children 760feced8380
rev   line source
Chris@439 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@439 2
Chris@439 3 /*
Chris@439 4 Sonic Visualiser
Chris@439 5 An audio file viewer and annotation editor.
Chris@439 6 Centre for Digital Music, Queen Mary, University of London.
Chris@727 7 This file copyright 2008-2012 QMUL.
Chris@439 8
Chris@439 9 This program is free software; you can redistribute it and/or
Chris@439 10 modify it under the terms of the GNU General Public License as
Chris@439 11 published by the Free Software Foundation; either version 2 of the
Chris@439 12 License, or (at your option) any later version. See the file
Chris@439 13 COPYING included with this distribution for more information.
Chris@439 14 */
Chris@439 15
Chris@439 16 #include "PluginRDFIndexer.h"
Chris@439 17
Chris@467 18 #include "data/fileio/CachedFile.h"
Chris@471 19 #include "data/fileio/FileSource.h"
Chris@461 20 #include "data/fileio/PlaylistFileReader.h"
Chris@439 21 #include "plugin/PluginIdentifier.h"
Chris@439 22
Chris@457 23 #include "base/Profiler.h"
Chris@457 24
Chris@475 25 #include <vamp-hostsdk/PluginHostAdapter.h>
Chris@439 26
Chris@725 27 #include <dataquay/BasicStore.h>
Chris@725 28 #include <dataquay/RDFException.h>
Chris@725 29
Chris@439 30 #include <QFileInfo>
Chris@439 31 #include <QDir>
Chris@439 32 #include <QUrl>
Chris@461 33 #include <QDateTime>
Chris@461 34 #include <QSettings>
Chris@461 35 #include <QFile>
Chris@439 36
Chris@439 37 #include <iostream>
Chris@439 38 using std::cerr;
Chris@439 39 using std::endl;
Chris@439 40 using std::vector;
Chris@439 41 using std::string;
Chris@439 42 using Vamp::PluginHostAdapter;
Chris@439 43
Chris@725 44 using Dataquay::Uri;
Chris@725 45 using Dataquay::Node;
Chris@725 46 using Dataquay::Nodes;
Chris@725 47 using Dataquay::Triple;
Chris@725 48 using Dataquay::Triples;
Chris@725 49 using Dataquay::BasicStore;
Chris@725 50 using Dataquay::RDFException;
Chris@725 51 using Dataquay::RDFDuplicateImportException;
Chris@725 52
Chris@439 53 PluginRDFIndexer *
Chris@439 54 PluginRDFIndexer::m_instance = 0;
Chris@439 55
Chris@439 56 PluginRDFIndexer *
Chris@439 57 PluginRDFIndexer::getInstance()
Chris@439 58 {
Chris@439 59 if (!m_instance) m_instance = new PluginRDFIndexer();
Chris@439 60 return m_instance;
Chris@439 61 }
Chris@439 62
Chris@725 63 PluginRDFIndexer::PluginRDFIndexer() :
Chris@725 64 m_index(new Dataquay::BasicStore)
Chris@439 65 {
Chris@725 66 m_index->addPrefix("vamp", Uri("http://purl.org/ontology/vamp/"));
Chris@725 67 m_index->addPrefix("foaf", Uri("http://xmlns.com/foaf/0.1/"));
Chris@725 68 m_index->addPrefix("dc", Uri("http://purl.org/dc/elements/1.1/"));
Chris@477 69 indexInstalledURLs();
Chris@477 70 }
Chris@477 71
Chris@725 72 const BasicStore *
Chris@725 73 PluginRDFIndexer::getIndex()
Chris@725 74 {
Chris@725 75 return m_index;
Chris@725 76 }
Chris@725 77
Chris@477 78 PluginRDFIndexer::~PluginRDFIndexer()
Chris@477 79 {
Chris@477 80 QMutexLocker locker(&m_mutex);
Chris@477 81 }
Chris@477 82
Chris@477 83 void
Chris@477 84 PluginRDFIndexer::indexInstalledURLs()
Chris@477 85 {
Chris@439 86 vector<string> paths = PluginHostAdapter::getPluginPath();
Chris@439 87
Chris@730 88 // std::cerr << "\nPluginRDFIndexer::indexInstalledURLs: pid is " << getpid() << std::endl;
Chris@730 89
Chris@439 90 QStringList filters;
Chris@439 91 filters << "*.n3";
Chris@439 92 filters << "*.N3";
Chris@439 93 filters << "*.rdf";
Chris@439 94 filters << "*.RDF";
Chris@439 95
Chris@439 96 // Search each Vamp plugin path for a .rdf file that either has
Chris@439 97 // name "soname", "soname:label" or "soname/label" plus RDF
Chris@439 98 // extension. Use that order of preference, and prefer n3 over
Chris@439 99 // rdf extension.
Chris@439 100
Chris@439 101 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
Chris@718 102
Chris@439 103 QDir dir(i->c_str());
Chris@439 104 if (!dir.exists()) continue;
Chris@439 105
Chris@439 106 QStringList entries = dir.entryList
Chris@439 107 (filters, QDir::Files | QDir::Readable);
Chris@439 108
Chris@439 109 for (QStringList::const_iterator j = entries.begin();
Chris@439 110 j != entries.end(); ++j) {
Chris@718 111
Chris@439 112 QFileInfo fi(dir.filePath(*j));
Chris@489 113 pullFile(fi.absoluteFilePath());
Chris@439 114 }
Chris@439 115
Chris@439 116 QStringList subdirs = dir.entryList
Chris@439 117 (QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable);
Chris@439 118
Chris@439 119 for (QStringList::const_iterator j = subdirs.begin();
Chris@439 120 j != subdirs.end(); ++j) {
Chris@718 121
Chris@439 122 QDir subdir(dir.filePath(*j));
Chris@439 123 if (subdir.exists()) {
Chris@439 124 entries = subdir.entryList
Chris@439 125 (filters, QDir::Files | QDir::Readable);
Chris@439 126 for (QStringList::const_iterator k = entries.begin();
Chris@439 127 k != entries.end(); ++k) {
Chris@439 128 QFileInfo fi(subdir.filePath(*k));
Chris@489 129 pullFile(fi.absoluteFilePath());
Chris@439 130 }
Chris@439 131 }
Chris@439 132 }
Chris@439 133 }
Chris@489 134
Chris@489 135 reindex();
Chris@439 136 }
Chris@439 137
Chris@461 138 bool
Chris@461 139 PluginRDFIndexer::indexConfiguredURLs()
Chris@461 140 {
Chris@690 141 SVDEBUG << "PluginRDFIndexer::indexConfiguredURLs" << endl;
Chris@461 142
Chris@461 143 QSettings settings;
Chris@461 144 settings.beginGroup("RDF");
Chris@461 145
Chris@461 146 QString indexKey("rdf-indices");
Chris@461 147 QStringList indices = settings.value(indexKey).toStringList();
Chris@461 148
Chris@461 149 for (int i = 0; i < indices.size(); ++i) {
Chris@461 150
Chris@461 151 QString index = indices[i];
Chris@461 152
Chris@690 153 SVDEBUG << "PluginRDFIndexer::indexConfiguredURLs: index url is "
Chris@687 154 << index << endl;
Chris@461 155
Chris@467 156 CachedFile cf(index);
Chris@467 157 if (!cf.isOK()) continue;
Chris@467 158
Chris@467 159 FileSource indexSource(cf.getLocalFilename());
Chris@461 160
Chris@461 161 PlaylistFileReader reader(indexSource);
Chris@461 162 if (!reader.isOK()) continue;
Chris@461 163
Chris@461 164 PlaylistFileReader::Playlist list = reader.load();
Chris@461 165 for (PlaylistFileReader::Playlist::const_iterator j = list.begin();
Chris@461 166 j != list.end(); ++j) {
Chris@690 167 SVDEBUG << "PluginRDFIndexer::indexConfiguredURLs: url is "
Chris@687 168 << j->toStdString() << endl;
Chris@489 169 pullURL(*j);
Chris@461 170 }
Chris@461 171 }
Chris@461 172
Chris@461 173 QString urlListKey("rdf-urls");
Chris@461 174 QStringList urls = settings.value(urlListKey).toStringList();
Chris@461 175
Chris@461 176 for (int i = 0; i < urls.size(); ++i) {
Chris@489 177 pullURL(urls[i]);
Chris@461 178 }
Chris@461 179
Chris@461 180 settings.endGroup();
Chris@489 181 reindex();
Chris@461 182 return true;
Chris@461 183 }
Chris@461 184
Chris@439 185 QString
Chris@439 186 PluginRDFIndexer::getURIForPluginId(QString pluginId)
Chris@439 187 {
Chris@461 188 QMutexLocker locker(&m_mutex);
Chris@461 189
Chris@439 190 if (m_idToUriMap.find(pluginId) == m_idToUriMap.end()) return "";
Chris@439 191 return m_idToUriMap[pluginId];
Chris@439 192 }
Chris@439 193
Chris@439 194 QString
Chris@439 195 PluginRDFIndexer::getIdForPluginURI(QString uri)
Chris@439 196 {
Chris@476 197 m_mutex.lock();
Chris@461 198
Chris@439 199 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
Chris@439 200
Chris@476 201 m_mutex.unlock();
Chris@476 202
Chris@439 203 // Haven't found this uri referenced in any document on the
Chris@439 204 // local filesystem; try resolving the pre-fragment part of
Chris@439 205 // the uri as a document URL and reading that if possible.
Chris@439 206
Chris@439 207 // Because we may want to refer to this document again, we
Chris@439 208 // cache it locally if it turns out to exist.
Chris@439 209
Chris@686 210 cerr << "PluginRDFIndexer::getIdForPluginURI: NOTE: Failed to find a local RDF document describing plugin <" << uri << ">: attempting to retrieve one remotely by guesswork" << endl;
Chris@439 211
Chris@439 212 QString baseUrl = QUrl(uri).toString(QUrl::RemoveFragment);
Chris@439 213
Chris@457 214 indexURL(baseUrl);
Chris@439 215
Chris@476 216 m_mutex.lock();
Chris@476 217
Chris@439 218 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
Chris@439 219 m_uriToIdMap[uri] = "";
Chris@439 220 }
Chris@439 221 }
Chris@439 222
Chris@476 223 QString id = m_uriToIdMap[uri];
Chris@476 224 m_mutex.unlock();
Chris@476 225 return id;
Chris@439 226 }
Chris@439 227
Chris@456 228 QStringList
Chris@456 229 PluginRDFIndexer::getIndexedPluginIds()
Chris@456 230 {
Chris@461 231 QMutexLocker locker(&m_mutex);
Chris@461 232
Chris@456 233 QStringList ids;
Chris@489 234 for (StringMap::const_iterator i = m_idToUriMap.begin();
Chris@489 235 i != m_idToUriMap.end(); ++i) {
Chris@456 236 ids.push_back(i->first);
Chris@456 237 }
Chris@456 238 return ids;
Chris@456 239 }
Chris@456 240
Chris@439 241 bool
Chris@489 242 PluginRDFIndexer::pullFile(QString filepath)
Chris@439 243 {
Chris@439 244 QUrl url = QUrl::fromLocalFile(filepath);
Chris@439 245 QString urlString = url.toString();
Chris@489 246 return pullURL(urlString);
Chris@439 247 }
Chris@461 248
Chris@439 249 bool
Chris@439 250 PluginRDFIndexer::indexURL(QString urlString)
Chris@439 251 {
Chris@489 252 bool pulled = pullURL(urlString);
Chris@489 253 if (!pulled) return false;
Chris@489 254 reindex();
Chris@489 255 return true;
Chris@489 256 }
Chris@489 257
Chris@489 258 bool
Chris@489 259 PluginRDFIndexer::pullURL(QString urlString)
Chris@489 260 {
Chris@457 261 Profiler profiler("PluginRDFIndexer::indexURL");
Chris@457 262
Chris@730 263 // std::cerr << "PluginRDFIndexer::indexURL(" << urlString.toStdString() << ")" << std::endl;
Chris@461 264
Chris@461 265 QMutexLocker locker(&m_mutex);
Chris@461 266
Chris@725 267 QUrl local = urlString;
Chris@457 268
Chris@457 269 if (FileSource::isRemote(urlString) &&
Chris@457 270 FileSource::canHandleScheme(urlString)) {
Chris@457 271
Chris@520 272 CachedFile cf(urlString, 0, "application/rdf+xml");
Chris@467 273 if (!cf.isOK()) {
Chris@467 274 return false;
Chris@467 275 }
Chris@467 276
Chris@725 277 local = QUrl::fromLocalFile(cf.getLocalFilename());
Chris@725 278
Chris@730 279 } else if (urlString.startsWith("file:")) {
Chris@730 280
Chris@730 281 local = QUrl(urlString);
Chris@730 282
Chris@725 283 } else {
Chris@725 284
Chris@725 285 local = QUrl::fromLocalFile(urlString);
Chris@457 286 }
Chris@457 287
Chris@725 288 try {
Chris@725 289 m_index->import(local, BasicStore::ImportFailOnDuplicates);
Chris@725 290 } catch (RDFDuplicateImportException &e) {
Chris@730 291 cerr << e.what() << endl;
Chris@725 292 cerr << "PluginRDFIndexer::pullURL: Document at " << urlString
Chris@730 293 << " duplicates triples found in earlier loaded document -- skipping it" << endl;
Chris@725 294 return false;
Chris@725 295 } catch (RDFException &e) {
Chris@730 296 cerr << e.what() << endl;
Chris@725 297 cerr << "PluginRDFIndexer::pullURL: Failed to import document from "
Chris@725 298 << urlString << ": " << e.what() << endl;
Chris@725 299 return false;
Chris@725 300 }
Chris@725 301 return true;
Chris@489 302 }
Chris@489 303
Chris@489 304 bool
Chris@489 305 PluginRDFIndexer::reindex()
Chris@489 306 {
Chris@725 307 Triples tt = m_index->match
Chris@730 308 (Triple(Node(), Uri("a"), m_index->expand("vamp:Plugin")));
Chris@730 309 Nodes plugins = tt.subjects();
Chris@439 310
Chris@439 311 bool foundSomething = false;
Chris@439 312 bool addedSomething = false;
Chris@439 313
Chris@725 314 foreach (Node plugin, plugins) {
Chris@725 315
Chris@725 316 if (plugin.type != Node::URI) {
Chris@725 317 cerr << "PluginRDFIndexer::reindex: Plugin has no URI: node is "
Chris@725 318 << plugin << endl;
Chris@439 319 continue;
Chris@439 320 }
Chris@725 321
Chris@730 322 Node idn = m_index->complete
Chris@730 323 (Triple(plugin, m_index->expand("vamp:identifier"), Node()));
Chris@730 324
Chris@730 325 if (idn.type != Node::Literal) {
Chris@725 326 cerr << "PluginRDFIndexer::reindex: Plugin " << plugin
Chris@725 327 << " lacks vamp:identifier literal" << endl;
Chris@439 328 continue;
Chris@439 329 }
Chris@481 330
Chris@730 331 Node libn = m_index->complete
Chris@730 332 (Triple(Node(), m_index->expand("vamp:available_plugin"), plugin));
Chris@481 333
Chris@730 334 if (libn.type != Node::URI) {
Chris@725 335 cerr << "PluginRDFIndexer::reindex: Plugin " << plugin
Chris@725 336 << " is not vamp:available_plugin in any library" << endl;
Chris@481 337 continue;
Chris@481 338 }
Chris@481 339
Chris@730 340 Node son = m_index->complete
Chris@730 341 (Triple(libn, m_index->expand("vamp:identifier"), Node()));
Chris@725 342
Chris@730 343 if (son.type != Node::Literal) {
Chris@730 344 cerr << "PluginRDFIndexer::reindex: Library " << libn
Chris@725 345 << " lacks vamp:identifier for soname" << endl;
Chris@725 346 continue;
Chris@725 347 }
Chris@725 348
Chris@725 349 QString pluginUri = plugin.value;
Chris@730 350 QString identifier = idn.value;
Chris@730 351 QString soname = son.value;
Chris@725 352
Chris@439 353 QString pluginId = PluginIdentifier::createIdentifier
Chris@439 354 ("vamp", soname, identifier);
Chris@439 355
Chris@439 356 foundSomething = true;
Chris@439 357
Chris@489 358 if (m_idToUriMap.find(pluginId) != m_idToUriMap.end()) {
Chris@439 359 continue;
Chris@439 360 }
Chris@439 361
Chris@439 362 m_idToUriMap[pluginId] = pluginUri;
Chris@439 363
Chris@439 364 addedSomething = true;
Chris@439 365
Chris@439 366 if (pluginUri != "") {
Chris@439 367 if (m_uriToIdMap.find(pluginUri) != m_uriToIdMap.end()) {
Chris@718 368 cerr << "PluginRDFIndexer::reindex: WARNING: Found multiple plugins with the same URI:" << endl;
Chris@686 369 cerr << " 1. Plugin id \"" << m_uriToIdMap[pluginUri] << "\"" << endl;
Chris@686 370 cerr << " 2. Plugin id \"" << pluginId << "\"" << endl;
Chris@686 371 cerr << "both claim URI <" << pluginUri << ">" << endl;
Chris@439 372 } else {
Chris@439 373 m_uriToIdMap[pluginUri] = pluginId;
Chris@439 374 }
Chris@439 375 }
Chris@439 376 }
Chris@439 377
Chris@439 378 if (!foundSomething) {
Chris@718 379 cerr << "PluginRDFIndexer::reindex: NOTE: Plugins found, but none sufficiently described" << endl;
Chris@439 380 }
Chris@439 381
Chris@439 382 return addedSomething;
Chris@439 383 }