annotate rdf/PluginRDFIndexer.cpp @ 473:47b41ec34407

* Avoid waiting on uninstalled transforms mutex in TransformFactory:: getTransformInstallStatus -- we can return TransformUnknown * Don't follow link in SelectableLabel when clicking to select it * Restore lost version number in plugin param dialog
author Chris Cannam
date Tue, 28 Oct 2008 18:06:06 +0000
parents 0dc158c1a6c9
children a70dcfed59c1
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@439 7 This file copyright 2008 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@439 18 #include "SimpleSPARQLQuery.h"
Chris@439 19
Chris@467 20 #include "data/fileio/CachedFile.h"
Chris@471 21 #include "data/fileio/FileSource.h"
Chris@461 22 #include "data/fileio/PlaylistFileReader.h"
Chris@439 23 #include "plugin/PluginIdentifier.h"
Chris@439 24
Chris@457 25 #include "base/Profiler.h"
Chris@457 26
Chris@439 27 #include <vamp-sdk/PluginHostAdapter.h>
Chris@439 28
Chris@439 29 #include <QFileInfo>
Chris@439 30 #include <QDir>
Chris@439 31 #include <QUrl>
Chris@461 32 #include <QDateTime>
Chris@461 33 #include <QSettings>
Chris@461 34 #include <QFile>
Chris@439 35
Chris@439 36 #include <iostream>
Chris@439 37 using std::cerr;
Chris@439 38 using std::endl;
Chris@439 39 using std::vector;
Chris@439 40 using std::string;
Chris@439 41 using Vamp::PluginHostAdapter;
Chris@439 42
Chris@439 43 PluginRDFIndexer *
Chris@439 44 PluginRDFIndexer::m_instance = 0;
Chris@439 45
Chris@439 46 PluginRDFIndexer *
Chris@439 47 PluginRDFIndexer::getInstance()
Chris@439 48 {
Chris@439 49 if (!m_instance) m_instance = new PluginRDFIndexer();
Chris@439 50 return m_instance;
Chris@439 51 }
Chris@439 52
Chris@439 53 PluginRDFIndexer::PluginRDFIndexer()
Chris@439 54 {
Chris@439 55 vector<string> paths = PluginHostAdapter::getPluginPath();
Chris@439 56
Chris@439 57 QStringList filters;
Chris@439 58 filters << "*.n3";
Chris@439 59 filters << "*.N3";
Chris@439 60 filters << "*.rdf";
Chris@439 61 filters << "*.RDF";
Chris@439 62
Chris@439 63 // Search each Vamp plugin path for a .rdf file that either has
Chris@439 64 // name "soname", "soname:label" or "soname/label" plus RDF
Chris@439 65 // extension. Use that order of preference, and prefer n3 over
Chris@439 66 // rdf extension.
Chris@439 67
Chris@439 68 for (vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
Chris@439 69
Chris@439 70 QDir dir(i->c_str());
Chris@439 71 if (!dir.exists()) continue;
Chris@439 72
Chris@439 73 QStringList entries = dir.entryList
Chris@439 74 (filters, QDir::Files | QDir::Readable);
Chris@439 75
Chris@439 76 for (QStringList::const_iterator j = entries.begin();
Chris@439 77 j != entries.end(); ++j) {
Chris@439 78 QFileInfo fi(dir.filePath(*j));
Chris@439 79 indexFile(fi.absoluteFilePath());
Chris@439 80 }
Chris@439 81
Chris@439 82 QStringList subdirs = dir.entryList
Chris@439 83 (QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable);
Chris@439 84
Chris@439 85 for (QStringList::const_iterator j = subdirs.begin();
Chris@439 86 j != subdirs.end(); ++j) {
Chris@439 87 QDir subdir(dir.filePath(*j));
Chris@439 88 if (subdir.exists()) {
Chris@439 89 entries = subdir.entryList
Chris@439 90 (filters, QDir::Files | QDir::Readable);
Chris@439 91 for (QStringList::const_iterator k = entries.begin();
Chris@439 92 k != entries.end(); ++k) {
Chris@439 93 QFileInfo fi(subdir.filePath(*k));
Chris@439 94 indexFile(fi.absoluteFilePath());
Chris@439 95 }
Chris@439 96 }
Chris@439 97 }
Chris@439 98 }
Chris@439 99 }
Chris@439 100
Chris@439 101 PluginRDFIndexer::~PluginRDFIndexer()
Chris@439 102 {
Chris@461 103 QMutexLocker locker(&m_mutex);
Chris@439 104 }
Chris@439 105
Chris@461 106 bool
Chris@461 107 PluginRDFIndexer::indexConfiguredURLs()
Chris@461 108 {
Chris@461 109 std::cerr << "PluginRDFIndexer::indexConfiguredURLs" << std::endl;
Chris@461 110
Chris@461 111 QSettings settings;
Chris@461 112 settings.beginGroup("RDF");
Chris@461 113
Chris@461 114 QString indexKey("rdf-indices");
Chris@461 115 QStringList indices = settings.value(indexKey).toStringList();
Chris@461 116
Chris@461 117 for (int i = 0; i < indices.size(); ++i) {
Chris@461 118
Chris@461 119 QString index = indices[i];
Chris@461 120
Chris@461 121 std::cerr << "PluginRDFIndexer::indexConfiguredURLs: index url is "
Chris@461 122 << index.toStdString() << std::endl;
Chris@461 123
Chris@467 124 CachedFile cf(index);
Chris@467 125 if (!cf.isOK()) continue;
Chris@467 126
Chris@467 127 FileSource indexSource(cf.getLocalFilename());
Chris@461 128
Chris@461 129 PlaylistFileReader reader(indexSource);
Chris@461 130 if (!reader.isOK()) continue;
Chris@461 131
Chris@461 132 PlaylistFileReader::Playlist list = reader.load();
Chris@461 133 for (PlaylistFileReader::Playlist::const_iterator j = list.begin();
Chris@461 134 j != list.end(); ++j) {
Chris@461 135 std::cerr << "PluginRDFIndexer::indexConfiguredURLs: url is "
Chris@461 136 << j->toStdString() << std::endl;
Chris@461 137 indexURL(*j);
Chris@461 138 }
Chris@461 139 }
Chris@461 140
Chris@461 141 QString urlListKey("rdf-urls");
Chris@461 142 QStringList urls = settings.value(urlListKey).toStringList();
Chris@461 143
Chris@461 144 for (int i = 0; i < urls.size(); ++i) {
Chris@461 145 indexURL(urls[i]);
Chris@461 146 }
Chris@461 147
Chris@461 148 settings.endGroup();
Chris@461 149 return true;
Chris@461 150 }
Chris@461 151
Chris@439 152 QString
Chris@439 153 PluginRDFIndexer::getURIForPluginId(QString pluginId)
Chris@439 154 {
Chris@461 155 QMutexLocker locker(&m_mutex);
Chris@461 156
Chris@439 157 if (m_idToUriMap.find(pluginId) == m_idToUriMap.end()) return "";
Chris@439 158 return m_idToUriMap[pluginId];
Chris@439 159 }
Chris@439 160
Chris@439 161 QString
Chris@439 162 PluginRDFIndexer::getIdForPluginURI(QString uri)
Chris@439 163 {
Chris@461 164 QMutexLocker locker(&m_mutex);
Chris@461 165
Chris@439 166 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
Chris@439 167
Chris@439 168 // Haven't found this uri referenced in any document on the
Chris@439 169 // local filesystem; try resolving the pre-fragment part of
Chris@439 170 // the uri as a document URL and reading that if possible.
Chris@439 171
Chris@439 172 // Because we may want to refer to this document again, we
Chris@439 173 // cache it locally if it turns out to exist.
Chris@439 174
Chris@439 175 cerr << "PluginRDFIndexer::getIdForPluginURI: NOTE: Failed to find a local RDF document describing plugin <" << uri.toStdString() << ">: attempting to retrieve one remotely by guesswork" << endl;
Chris@439 176
Chris@439 177 QString baseUrl = QUrl(uri).toString(QUrl::RemoveFragment);
Chris@439 178
Chris@457 179 indexURL(baseUrl);
Chris@439 180
Chris@439 181 if (m_uriToIdMap.find(uri) == m_uriToIdMap.end()) {
Chris@439 182 m_uriToIdMap[uri] = "";
Chris@439 183 }
Chris@439 184 }
Chris@439 185
Chris@439 186 return m_uriToIdMap[uri];
Chris@439 187 }
Chris@439 188
Chris@439 189 QString
Chris@439 190 PluginRDFIndexer::getDescriptionURLForPluginId(QString pluginId)
Chris@439 191 {
Chris@461 192 QMutexLocker locker(&m_mutex);
Chris@461 193
Chris@439 194 if (m_idToDescriptionMap.find(pluginId) == m_idToDescriptionMap.end()) return "";
Chris@439 195 return m_idToDescriptionMap[pluginId];
Chris@439 196 }
Chris@439 197
Chris@439 198 QString
Chris@439 199 PluginRDFIndexer::getDescriptionURLForPluginURI(QString uri)
Chris@439 200 {
Chris@461 201 QMutexLocker locker(&m_mutex);
Chris@461 202
Chris@439 203 QString id = getIdForPluginURI(uri);
Chris@439 204 if (id == "") return "";
Chris@439 205 return getDescriptionURLForPluginId(id);
Chris@439 206 }
Chris@439 207
Chris@456 208 QStringList
Chris@456 209 PluginRDFIndexer::getIndexedPluginIds()
Chris@456 210 {
Chris@461 211 QMutexLocker locker(&m_mutex);
Chris@461 212
Chris@456 213 QStringList ids;
Chris@456 214 for (StringMap::const_iterator i = m_idToDescriptionMap.begin();
Chris@456 215 i != m_idToDescriptionMap.end(); ++i) {
Chris@456 216 ids.push_back(i->first);
Chris@456 217 }
Chris@456 218 return ids;
Chris@456 219 }
Chris@456 220
Chris@439 221 bool
Chris@439 222 PluginRDFIndexer::indexFile(QString filepath)
Chris@439 223 {
Chris@439 224 QUrl url = QUrl::fromLocalFile(filepath);
Chris@439 225 QString urlString = url.toString();
Chris@439 226 return indexURL(urlString);
Chris@439 227 }
Chris@461 228
Chris@439 229 bool
Chris@439 230 PluginRDFIndexer::indexURL(QString urlString)
Chris@439 231 {
Chris@457 232 Profiler profiler("PluginRDFIndexer::indexURL");
Chris@457 233
Chris@461 234 std::cerr << "PluginRDFIndexer::indexURL(" << urlString.toStdString() << ")" << std::endl;
Chris@461 235
Chris@461 236 QMutexLocker locker(&m_mutex);
Chris@461 237
Chris@457 238 QString localString = urlString;
Chris@457 239
Chris@457 240 if (FileSource::isRemote(urlString) &&
Chris@457 241 FileSource::canHandleScheme(urlString)) {
Chris@457 242
Chris@467 243 CachedFile cf(urlString);
Chris@467 244 if (!cf.isOK()) {
Chris@467 245 return false;
Chris@467 246 }
Chris@467 247
Chris@467 248 localString = cf.getLocalFilename();
Chris@457 249 }
Chris@457 250
Chris@439 251 // cerr << "PluginRDFIndexer::indexURL: url = <" << urlString.toStdString() << ">" << endl;
Chris@439 252
Chris@439 253 SimpleSPARQLQuery query
Chris@439 254 (QString
Chris@439 255 (
Chris@439 256 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
Chris@439 257
Chris@439 258 " SELECT ?plugin ?library_id ?plugin_id "
Chris@439 259 " FROM <%1> "
Chris@439 260
Chris@439 261 " WHERE { "
Chris@439 262 " ?plugin a vamp:Plugin . "
Chris@439 263
Chris@439 264 // Make the identifier and library parts optional, so
Chris@439 265 // that we can check and report helpfully if one or both
Chris@439 266 // is absent instead of just getting no results
Chris@439 267
Chris@440 268 //!!! No -- because of rasqal's inability to correctly
Chris@440 269 // handle more than one OPTIONAL graph in a query, let's
Chris@440 270 // make identifier compulsory after all
Chris@440 271 //" OPTIONAL { ?plugin vamp:identifier ?plugin_id } . "
Chris@440 272
Chris@440 273 " ?plugin vamp:identifier ?plugin_id . "
Chris@439 274
Chris@439 275 " OPTIONAL { "
Chris@439 276 " ?library a vamp:PluginLibrary ; "
Chris@439 277 " vamp:available_plugin ?plugin ; "
Chris@439 278 " vamp:identifier ?library_id "
Chris@439 279 " } "
Chris@439 280 " } "
Chris@439 281 )
Chris@457 282 .arg(localString));
Chris@439 283
Chris@439 284 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@439 285
Chris@439 286 if (!query.isOK()) {
Chris@439 287 cerr << "ERROR: PluginRDFIndexer::indexURL: ERROR: Failed to index document at <"
Chris@439 288 << urlString.toStdString() << ">: "
Chris@439 289 << query.getErrorString().toStdString() << endl;
Chris@439 290 return false;
Chris@439 291 }
Chris@439 292
Chris@439 293 if (results.empty()) {
Chris@439 294 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
Chris@439 295 << urlString.toStdString()
Chris@439 296 << "> does not describe any vamp:Plugin resources" << endl;
Chris@439 297 return false;
Chris@439 298 }
Chris@439 299
Chris@439 300 bool foundSomething = false;
Chris@439 301 bool addedSomething = false;
Chris@439 302
Chris@439 303 for (SimpleSPARQLQuery::ResultList::iterator i = results.begin();
Chris@439 304 i != results.end(); ++i) {
Chris@439 305
Chris@439 306 QString pluginUri = (*i)["plugin"].value;
Chris@439 307 QString soname = (*i)["library_id"].value;
Chris@439 308 QString identifier = (*i)["plugin_id"].value;
Chris@439 309
Chris@439 310 if (identifier == "") {
Chris@439 311 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
Chris@439 312 << urlString.toStdString()
Chris@439 313 << "> fails to define any vamp:identifier for plugin <"
Chris@439 314 << pluginUri.toStdString() << ">"
Chris@439 315 << endl;
Chris@439 316 continue;
Chris@439 317 }
Chris@439 318 if (soname == "") {
Chris@439 319 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
Chris@439 320 << urlString.toStdString() << "> does not associate plugin <"
Chris@439 321 << pluginUri.toStdString() << "> with any implementation library"
Chris@439 322 << endl;
Chris@439 323 continue;
Chris@439 324 }
Chris@439 325 /*
Chris@439 326 cerr << "PluginRDFIndexer::indexURL: Document for plugin \""
Chris@439 327 << soname.toStdString() << ":" << identifier.toStdString()
Chris@439 328 << "\" (uri <" << pluginUri.toStdString() << ">) is at url <"
Chris@439 329 << urlString.toStdString() << ">" << endl;
Chris@439 330 */
Chris@439 331 QString pluginId = PluginIdentifier::createIdentifier
Chris@439 332 ("vamp", soname, identifier);
Chris@439 333
Chris@439 334 foundSomething = true;
Chris@439 335
Chris@439 336 if (m_idToDescriptionMap.find(pluginId) != m_idToDescriptionMap.end()) {
Chris@439 337 cerr << "PluginRDFIndexer::indexURL: NOTE: Plugin id \""
Chris@439 338 << pluginId.toStdString() << "\", described in document at <"
Chris@439 339 << urlString.toStdString()
Chris@439 340 << ">, has already been described in document <"
Chris@439 341 << m_idToDescriptionMap[pluginId].toStdString()
Chris@439 342 << ">: ignoring this new description" << endl;
Chris@439 343 continue;
Chris@439 344 }
Chris@439 345
Chris@439 346 m_idToDescriptionMap[pluginId] = urlString;
Chris@439 347 m_idToUriMap[pluginId] = pluginUri;
Chris@439 348
Chris@439 349 addedSomething = true;
Chris@439 350
Chris@439 351 if (pluginUri != "") {
Chris@439 352 if (m_uriToIdMap.find(pluginUri) != m_uriToIdMap.end()) {
Chris@439 353 cerr << "PluginRDFIndexer::indexURL: WARNING: Found multiple plugins with the same URI:" << endl;
Chris@439 354 cerr << " 1. Plugin id \"" << m_uriToIdMap[pluginUri].toStdString() << "\"" << endl;
Chris@439 355 cerr << " described in <" << m_idToDescriptionMap[m_uriToIdMap[pluginUri]].toStdString() << ">" << endl;
Chris@439 356 cerr << " 2. Plugin id \"" << pluginId.toStdString() << "\"" << endl;
Chris@439 357 cerr << " described in <" << urlString.toStdString() << ">" << endl;
Chris@439 358 cerr << "both claim URI <" << pluginUri.toStdString() << ">" << endl;
Chris@439 359 } else {
Chris@439 360 m_uriToIdMap[pluginUri] = pluginId;
Chris@439 361 }
Chris@439 362 }
Chris@439 363 }
Chris@439 364
Chris@439 365 if (!foundSomething) {
Chris@439 366 cerr << "PluginRDFIndexer::indexURL: NOTE: Document at <"
Chris@439 367 << urlString.toStdString()
Chris@439 368 << "> does not sufficiently describe any plugins" << endl;
Chris@439 369 }
Chris@439 370
Chris@439 371 return addedSomething;
Chris@439 372 }
Chris@439 373
Chris@439 374
Chris@439 375