Mercurial > hg > svcore
comparison rdf/RDFTransformFactory.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 "RDFTransformFactory.h" | |
17 | |
18 #include <map> | |
19 #include <vector> | |
20 | |
21 #include <redland.h> | |
22 #include <rasqal.h> | |
23 | |
24 #include <iostream> | |
25 #include <cmath> | |
26 | |
27 #include "SimpleSPARQLQuery.h" | |
28 #include "PluginRDFIndexer.h" | |
29 #include "base/ProgressReporter.h" | |
30 | |
31 #include "transform/TransformFactory.h" | |
32 | |
33 using std::cerr; | |
34 using std::endl; | |
35 | |
36 typedef const unsigned char *STR; // redland's expected string type | |
37 | |
38 | |
39 class RDFTransformFactoryImpl | |
40 { | |
41 public: | |
42 RDFTransformFactoryImpl(QString url); | |
43 virtual ~RDFTransformFactoryImpl(); | |
44 | |
45 bool isOK(); | |
46 QString getErrorString() const; | |
47 | |
48 std::vector<Transform> getTransforms(ProgressReporter *); | |
49 | |
50 protected: | |
51 QString m_urlString; | |
52 QString m_errorString; | |
53 }; | |
54 | |
55 | |
56 QString | |
57 RDFTransformFactory::getKnownExtensions() | |
58 { | |
59 return "*.rdf *.n3 *.ttl"; | |
60 } | |
61 | |
62 RDFTransformFactory::RDFTransformFactory(QString url) : | |
63 m_d(new RDFTransformFactoryImpl(url)) | |
64 { | |
65 } | |
66 | |
67 RDFTransformFactory::~RDFTransformFactory() | |
68 { | |
69 delete m_d; | |
70 } | |
71 | |
72 bool | |
73 RDFTransformFactory::isOK() | |
74 { | |
75 return m_d->isOK(); | |
76 } | |
77 | |
78 QString | |
79 RDFTransformFactory::getErrorString() const | |
80 { | |
81 return m_d->getErrorString(); | |
82 } | |
83 | |
84 std::vector<Transform> | |
85 RDFTransformFactory::getTransforms(ProgressReporter *r) | |
86 { | |
87 return m_d->getTransforms(r); | |
88 } | |
89 | |
90 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) : | |
91 m_urlString(url) | |
92 { | |
93 } | |
94 | |
95 RDFTransformFactoryImpl::~RDFTransformFactoryImpl() | |
96 { | |
97 } | |
98 | |
99 bool | |
100 RDFTransformFactoryImpl::isOK() | |
101 { | |
102 return (m_errorString == ""); | |
103 } | |
104 | |
105 QString | |
106 RDFTransformFactoryImpl::getErrorString() const | |
107 { | |
108 return m_errorString; | |
109 } | |
110 | |
111 std::vector<Transform> | |
112 RDFTransformFactoryImpl::getTransforms(ProgressReporter *reporter) | |
113 { | |
114 std::vector<Transform> transforms; | |
115 | |
116 SimpleSPARQLQuery query | |
117 (QString | |
118 ( | |
119 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
120 | |
121 " SELECT ?transform ?plugin ?output ?program " | |
122 " ?step_size ?block_size ?window_type " | |
123 " ?sample_rate ?start ?duration " | |
124 | |
125 " FROM <%1> " | |
126 | |
127 " WHERE { " | |
128 " ?transform a vamp:Transform ; " | |
129 " vamp:plugin ?plugin . " | |
130 " OPTIONAL { ?transform vamp:output ?output } . " | |
131 " OPTIONAL { ?transform vamp:program ?program } . " | |
132 " OPTIONAL { ?transform vamp:step_size ?step_size } . " | |
133 " OPTIONAL { ?transform vamp:block_size ?block_size } . " | |
134 " OPTIONAL { ?transform vamp:window_type ?window_type } . " | |
135 " OPTIONAL { ?transform vamp:sample_rate ?sample_rate } . " | |
136 " OPTIONAL { ?transform vamp:start ?start } . " | |
137 " OPTIONAL { ?transform vamp:duration ?duration } " | |
138 " } " | |
139 ) | |
140 .arg(m_urlString)); | |
141 | |
142 SimpleSPARQLQuery::ResultList results = query.execute(); | |
143 | |
144 if (!query.isOK()) { | |
145 m_errorString = query.getErrorString(); | |
146 return transforms; | |
147 } | |
148 | |
149 if (query.wasCancelled()) { | |
150 m_errorString = "Query cancelled"; | |
151 return transforms; | |
152 } | |
153 | |
154 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); | |
155 | |
156 for (int i = 0; i < results.size(); ++i) { | |
157 | |
158 SimpleSPARQLQuery::KeyValueMap &result = results[i]; | |
159 | |
160 QString transformUri = result["transform"].value; | |
161 QString pluginUri = result["plugin"].value; | |
162 | |
163 QString pluginId = indexer->getIdForPluginURI(pluginUri); | |
164 | |
165 if (pluginId == "") { | |
166 cerr << "RDFTransformFactory: WARNING: Unknown plugin <" | |
167 << pluginUri.toStdString() << "> for transform <" | |
168 << transformUri.toStdString() << ">" << endl; | |
169 continue; | |
170 } | |
171 | |
172 Transform transform; | |
173 transform.setPluginIdentifier(pluginId); | |
174 | |
175 if (result["output"].type == SimpleSPARQLQuery::LiteralValue) { | |
176 transform.setOutput(result["output"].value); | |
177 } | |
178 | |
179 if (result["program"].type == SimpleSPARQLQuery::LiteralValue) { | |
180 transform.setProgram(result["program"].value); | |
181 } | |
182 | |
183 if (result["step_size"].type == SimpleSPARQLQuery::LiteralValue) { | |
184 transform.setStepSize(result["step_size"].value.toUInt()); | |
185 } | |
186 | |
187 if (result["block_size"].type == SimpleSPARQLQuery::LiteralValue) { | |
188 transform.setBlockSize(result["block_size"].value.toUInt()); | |
189 } | |
190 | |
191 if (result["window_type"].type == SimpleSPARQLQuery::LiteralValue) { | |
192 cerr << "NOTE: can't handle window type yet (value is \"" | |
193 << result["window_type"].value.toStdString() << "\")" << endl; | |
194 } | |
195 | |
196 if (result["sample_rate"].type == SimpleSPARQLQuery::LiteralValue) { | |
197 transform.setStepSize(result["sample_rate"].value.toFloat()); | |
198 } | |
199 | |
200 if (result["start"].type == SimpleSPARQLQuery::LiteralValue) { | |
201 transform.setStartTime(RealTime::fromXsdDuration | |
202 (result["start"].value.toStdString())); | |
203 } | |
204 | |
205 if (result["duration"].type == SimpleSPARQLQuery::LiteralValue) { | |
206 transform.setDuration(RealTime::fromXsdDuration | |
207 (result["duration"].value.toStdString())); | |
208 } | |
209 | |
210 SimpleSPARQLQuery paramQuery | |
211 (QString | |
212 ( | |
213 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | |
214 | |
215 " SELECT ?param_id ?param_value " | |
216 | |
217 " FROM <%1> " | |
218 | |
219 " WHERE { " | |
220 " <%2> vamp:parameter ?param . " | |
221 " ?param vamp:identifier ?param_id ; " | |
222 " vamp:value ?param_value " | |
223 " } " | |
224 ) | |
225 .arg(m_urlString) | |
226 .arg(transformUri)); | |
227 | |
228 SimpleSPARQLQuery::ResultList paramResults = paramQuery.execute(); | |
229 | |
230 if (!paramQuery.isOK()) { | |
231 m_errorString = paramQuery.getErrorString(); | |
232 return transforms; | |
233 } | |
234 | |
235 if (paramQuery.wasCancelled()) { | |
236 m_errorString = "Query cancelled"; | |
237 return transforms; | |
238 } | |
239 | |
240 for (int j = 0; j < paramResults.size(); ++j) { | |
241 | |
242 QString paramId = paramResults[j]["param_id"].value; | |
243 QString paramValue = paramResults[j]["param_value"].value; | |
244 | |
245 if (paramId == "" || paramValue == "") continue; | |
246 | |
247 transform.setParameter(paramId, paramValue.toFloat()); | |
248 } | |
249 | |
250 cerr << "RDFTransformFactory: NOTE: Transform is: " << endl; | |
251 cerr << transform.toXmlString().toStdString() << endl; | |
252 | |
253 transforms.push_back(transform); | |
254 } | |
255 | |
256 return transforms; | |
257 } | |
258 |