annotate rdf/PluginRDFDescription.cpp @ 439:beb2948baa77

* Merge revisions 1041 to 1130 from sv-rdf-import branch
author Chris Cannam
date Thu, 18 Sep 2008 12:09:32 +0000
parents
children 5746c559af15
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 "PluginRDFDescription.h"
Chris@439 17
Chris@439 18 #include "PluginRDFIndexer.h"
Chris@439 19 #include "SimpleSPARQLQuery.h"
Chris@439 20
Chris@439 21 #include "plugin/PluginIdentifier.h"
Chris@439 22
Chris@439 23 #include <iostream>
Chris@439 24 using std::cerr;
Chris@439 25 using std::endl;
Chris@439 26
Chris@439 27 PluginRDFDescription::PluginRDFDescription(QString pluginId) :
Chris@439 28 m_pluginId(pluginId),
Chris@439 29 m_haveDescription(false)
Chris@439 30 {
Chris@439 31 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance();
Chris@439 32 QString url = indexer->getDescriptionURLForPluginId(pluginId);
Chris@439 33 if (url == "") {
Chris@439 34 cerr << "PluginRDFDescription: WARNING: No RDF description available for plugin ID \""
Chris@439 35 << pluginId.toStdString() << "\"" << endl;
Chris@439 36 } else {
Chris@439 37 if (!indexURL(url)) {
Chris@439 38 cerr << "PluginRDFDescription: ERROR: Failed to query RDF description for plugin ID \""
Chris@439 39 << pluginId.toStdString() << "\"" << endl;
Chris@439 40 } else {
Chris@439 41 m_haveDescription = true;
Chris@439 42 }
Chris@439 43 }
Chris@439 44 }
Chris@439 45
Chris@439 46 PluginRDFDescription::~PluginRDFDescription()
Chris@439 47 {
Chris@439 48 }
Chris@439 49
Chris@439 50 bool
Chris@439 51 PluginRDFDescription::haveDescription() const
Chris@439 52 {
Chris@439 53 return m_haveDescription;
Chris@439 54 }
Chris@439 55
Chris@439 56 PluginRDFDescription::OutputType
Chris@439 57 PluginRDFDescription::getOutputType(QString outputId) const
Chris@439 58 {
Chris@439 59 if (m_outputTypes.find(outputId) == m_outputTypes.end()) {
Chris@439 60 return OutputTypeUnknown;
Chris@439 61 }
Chris@439 62 return m_outputTypes.find(outputId)->second;
Chris@439 63 }
Chris@439 64
Chris@439 65 PluginRDFDescription::OutputDisposition
Chris@439 66 PluginRDFDescription::getOutputDisposition(QString outputId) const
Chris@439 67 {
Chris@439 68 if (m_outputDispositions.find(outputId) == m_outputDispositions.end()) {
Chris@439 69 return OutputDispositionUnknown;
Chris@439 70 }
Chris@439 71 return m_outputDispositions.find(outputId)->second;
Chris@439 72 }
Chris@439 73
Chris@439 74 QString
Chris@439 75 PluginRDFDescription::getOutputFeatureTypeURI(QString outputId) const
Chris@439 76 {
Chris@439 77 if (m_outputFeatureTypeURIMap.find(outputId) ==
Chris@439 78 m_outputFeatureTypeURIMap.end()) {
Chris@439 79 return "";
Chris@439 80 }
Chris@439 81 return m_outputFeatureTypeURIMap.find(outputId)->second;
Chris@439 82 }
Chris@439 83
Chris@439 84 QString
Chris@439 85 PluginRDFDescription::getOutputEventTypeURI(QString outputId) const
Chris@439 86 {
Chris@439 87 if (m_outputEventTypeURIMap.find(outputId) ==
Chris@439 88 m_outputEventTypeURIMap.end()) {
Chris@439 89 return "";
Chris@439 90 }
Chris@439 91 return m_outputEventTypeURIMap.find(outputId)->second;
Chris@439 92 }
Chris@439 93
Chris@439 94 QString
Chris@439 95 PluginRDFDescription::getOutputUnit(QString outputId) const
Chris@439 96 {
Chris@439 97 if (m_outputUnitMap.find(outputId) == m_outputUnitMap.end()) {
Chris@439 98 return "";
Chris@439 99 }
Chris@439 100 return m_outputUnitMap.find(outputId)->second;
Chris@439 101 }
Chris@439 102
Chris@439 103 bool
Chris@439 104 PluginRDFDescription::indexURL(QString url)
Chris@439 105 {
Chris@439 106 QString type, soname, label;
Chris@439 107 PluginIdentifier::parseIdentifier(m_pluginId, type, soname, label);
Chris@439 108
Chris@439 109 SimpleSPARQLQuery query
Chris@439 110 (QString
Chris@439 111 (
Chris@439 112 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
Chris@439 113
Chris@439 114 " SELECT ?output_id ?output_type ?feature_type ?event_type ?unit "
Chris@439 115 " FROM <%1> "
Chris@439 116
Chris@439 117 " WHERE { "
Chris@439 118
Chris@439 119 " ?plugin a vamp:Plugin ; "
Chris@439 120 " vamp:identifier \"%2\" ; "
Chris@439 121 " vamp:output_descriptor ?output . "
Chris@439 122
Chris@439 123 " ?output vamp:identifier ?output_id ; "
Chris@439 124 " a ?output_type . "
Chris@439 125
Chris@439 126 " OPTIONAL { "
Chris@439 127 " ?output vamp:computes_feature_type ?feature_type "
Chris@439 128 " } . "
Chris@439 129
Chris@439 130 " OPTIONAL { "
Chris@439 131 " ?output vamp:computes_event_type ?event_type "
Chris@439 132 " } . "
Chris@439 133
Chris@439 134 " OPTIONAL { "
Chris@439 135 " ?output vamp:unit ?unit "
Chris@439 136 " } . "
Chris@439 137
Chris@439 138 " } "
Chris@439 139 )
Chris@439 140 .arg(url)
Chris@439 141 .arg(label));
Chris@439 142
Chris@439 143 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@439 144
Chris@439 145 if (!query.isOK()) {
Chris@439 146 cerr << "ERROR: PluginRDFDescription::indexURL: ERROR: Failed to query document at <"
Chris@439 147 << url.toStdString() << ">: "
Chris@439 148 << query.getErrorString().toStdString() << endl;
Chris@439 149 return false;
Chris@439 150 }
Chris@439 151
Chris@439 152 if (results.empty()) {
Chris@439 153 cerr << "ERROR: PluginRDFDescription::indexURL: NOTE: Document at <"
Chris@439 154 << url.toStdString()
Chris@439 155 << "> does not appear to describe any plugin outputs" << endl;
Chris@439 156 return false;
Chris@439 157 }
Chris@439 158
Chris@439 159 // Note that an output may appear more than once, if it inherits
Chris@439 160 // more than one type (e.g. DenseOutput and QuantizedOutput). So
Chris@439 161 // these results must accumulate
Chris@439 162
Chris@439 163 for (int i = 0; i < results.size(); ++i) {
Chris@439 164
Chris@439 165 QString outputId = results[i]["output_id"].value;
Chris@439 166
Chris@439 167 if (m_outputTypes.find(outputId) == m_outputTypes.end()) {
Chris@439 168 m_outputTypes[outputId] = OutputTypeUnknown;
Chris@439 169 }
Chris@439 170
Chris@439 171 QString outputType = results[i]["output_type"].value;
Chris@439 172
Chris@439 173 if (outputType.contains("DenseOutput")) {
Chris@439 174 m_outputDispositions[outputId] = OutputDense;
Chris@439 175 } else if (outputType.contains("SparseOutput")) {
Chris@439 176 m_outputDispositions[outputId] = OutputSparse;
Chris@439 177 } else if (outputType.contains("TrackLevelOutput")) {
Chris@439 178 m_outputDispositions[outputId] = OutputTrackLevel;
Chris@439 179 }
Chris@439 180
Chris@439 181 if (results[i]["feature_type"].type == SimpleSPARQLQuery::URIValue) {
Chris@439 182
Chris@439 183 QString featureType = results[i]["feature_type"].value;
Chris@439 184
Chris@439 185 if (featureType != "") {
Chris@439 186 if (m_outputTypes[outputId] == OutputEvents) {
Chris@439 187 m_outputTypes[outputId] = OutputFeaturesAndEvents;
Chris@439 188 } else {
Chris@439 189 m_outputTypes[outputId] = OutputFeatures;
Chris@439 190 }
Chris@439 191 m_outputFeatureTypeURIMap[outputId] = featureType;
Chris@439 192 }
Chris@439 193 }
Chris@439 194
Chris@439 195 if (results[i]["event_type"].type == SimpleSPARQLQuery::URIValue) {
Chris@439 196
Chris@439 197 QString eventType = results[i]["event_type"].value;
Chris@439 198
Chris@439 199 if (eventType != "") {
Chris@439 200 if (m_outputTypes[outputId] == OutputFeatures) {
Chris@439 201 m_outputTypes[outputId] = OutputFeaturesAndEvents;
Chris@439 202 } else {
Chris@439 203 m_outputTypes[outputId] = OutputEvents;
Chris@439 204 }
Chris@439 205 m_outputEventTypeURIMap[outputId] = eventType;
Chris@439 206 }
Chris@439 207 }
Chris@439 208
Chris@439 209 if (results[i]["unit"].type == SimpleSPARQLQuery::LiteralValue) {
Chris@439 210
Chris@439 211 QString unit = results[i]["unit"].value;
Chris@439 212
Chris@439 213 if (unit != "") {
Chris@439 214 m_outputUnitMap[outputId] = unit;
Chris@439 215 }
Chris@439 216 }
Chris@439 217 }
Chris@439 218
Chris@439 219 return true;
Chris@439 220 }
Chris@439 221