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