Mercurial > hg > svcore
comparison rdf/RDFTransformFactory.cpp @ 728:f2de9a42830e dataquay
Convert a bit more of RDFTransformFactory to Dataquay
author | Chris Cannam |
---|---|
date | Sat, 19 May 2012 12:50:36 +0100 |
parents | 211efc770335 |
children | 11289d40a57a |
comparison
equal
deleted
inserted
replaced
727:211efc770335 | 728:f2de9a42830e |
---|---|
160 std::map<QString, Transform> uriTransformMap; | 160 std::map<QString, Transform> uriTransformMap; |
161 | 161 |
162 Nodes tnodes = m_store->match | 162 Nodes tnodes = m_store->match |
163 (Triple(Node(), "a", m_store->expand("vamp:Transform"))).a(); | 163 (Triple(Node(), "a", m_store->expand("vamp:Transform"))).a(); |
164 | 164 |
165 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); | |
166 | |
165 foreach (Node tnode, tnodes) { | 167 foreach (Node tnode, tnodes) { |
166 | 168 |
167 Node pnode = m_store->matchFirst | 169 Node pnode = m_store->matchFirst |
168 (Triple(tnode, "vamp:plugin", Node())).c; | 170 (Triple(tnode, "vamp:plugin", Node())).c; |
169 | 171 |
170 // There are various queries we need to make that might | 172 if (pnode == Node()) { |
171 // include data from either the transform RDF or the model | 173 cerr << "RDFTransformFactory: WARNING: No vamp:plugin for " |
172 // accumulated from plugin descriptions. For example, the | 174 << "vamp:Transform node " << tnode |
173 // transform RDF may specify the output's true URI, or it | 175 << ", skipping this transform" << endl; |
174 // might have a blank node or some other URI with the | 176 continue; |
175 // appropriate vamp:identifier included in the file. To cover | 177 } |
176 // both cases, we need to add the file itself into the model | 178 |
177 // and always query the model using the transform URI rather | 179 QString transformUri = tnode.value; |
178 // than querying the file itself subsequently. | 180 QString pluginUri = pnode.value; |
179 | |
180 //!!! ^^^ what does this mean for us with Dataquay? do we need to cross-check outputs against the indexer? | |
181 | |
182 SimpleSPARQLQuery::addSourceToModel(m_urlString); | |
183 | |
184 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); | |
185 | |
186 for (int i = 0; i < transformResults.size(); ++i) { | |
187 | |
188 SimpleSPARQLQuery::KeyValueMap &result = transformResults[i]; | |
189 | |
190 QString transformUri = result["transform"].value; | |
191 QString pluginUri = result["plugin"].value; | |
192 | 181 |
193 QString pluginId = indexer->getIdForPluginURI(pluginUri); | 182 QString pluginId = indexer->getIdForPluginURI(pluginUri); |
194 if (pluginId == "") { | 183 if (pluginId == "") { |
195 cerr << "RDFTransformFactory: WARNING: Unknown plugin <" | 184 cerr << "RDFTransformFactory: WARNING: Unknown plugin <" |
196 << pluginUri << "> for transform <" | 185 << pluginUri << "> for transform <" |
210 return transforms; | 199 return transforms; |
211 } | 200 } |
212 | 201 |
213 uriTransformMap[transformUri] = transform; | 202 uriTransformMap[transformUri] = transform; |
214 | 203 |
215 // We have to do this a very long way round, to work around | |
216 // rasqal's current inability to handle correctly more than one | |
217 // OPTIONAL graph in a query | |
218 | |
219 static const char *optionals[] = { | 204 static const char *optionals[] = { |
220 "output", | |
221 "program", | 205 "program", |
222 "summary_type", | 206 "summary_type", |
223 "step_size", | 207 "step_size", |
224 "block_size", | 208 "block_size", |
225 "window_type", | 209 "window_type", |
230 | 214 |
231 for (int j = 0; j < sizeof(optionals)/sizeof(optionals[0]); ++j) { | 215 for (int j = 0; j < sizeof(optionals)/sizeof(optionals[0]); ++j) { |
232 | 216 |
233 QString optional = optionals[j]; | 217 QString optional = optionals[j]; |
234 | 218 |
235 QString queryTemplate = | 219 Node onode = m_store->matchFirst |
236 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | 220 (Triple(Uri(transformUri), optional, Node())).c; |
237 | 221 |
238 " SELECT ?%1 " | 222 if (onode.type != Node::Literal) continue; |
239 | 223 |
240 " WHERE { " | 224 if (optional == "program") { |
241 " <%2> vamp:%1 ?%1 " | 225 transform.setProgram(onode.value); |
242 " } "; | 226 } else if (optional == "summary_type") { |
243 | 227 transform.setSummaryType |
244 SimpleSPARQLQuery query | 228 (transform.stringToSummaryType(onode.value)); |
245 (SimpleSPARQLQuery::QueryFromModel, | 229 } else if (optional == "step_size") { |
246 queryTemplate.arg(optional).arg(transformUri)); | 230 transform.setStepSize(onode.value.toUInt()); |
247 | 231 } else if (optional == "block_size") { |
248 SimpleSPARQLQuery::ResultList results = query.execute(); | 232 transform.setBlockSize(onode.value.toUInt()); |
249 | 233 } else if (optional == "window_type") { |
250 if (!query.isOK()) { | 234 transform.setWindowType |
251 m_errorString = query.getErrorString(); | 235 (Window<float>::getTypeForName |
252 return transforms; | 236 (onode.value.toLower().toStdString())); |
253 } | 237 } else if (optional == "sample_rate") { |
254 | 238 transform.setSampleRate(onode.value.toFloat()); |
255 if (results.empty()) continue; | 239 } else if (optional == "start") { |
256 | 240 transform.setStartTime |
257 for (int k = 0; k < results.size(); ++k) { | 241 (RealTime::fromXsdDuration(onode.value.toStdString())); |
258 | 242 } else if (optional == "duration") { |
259 const SimpleSPARQLQuery::Value &v = results[k][optional]; | 243 transform.setDuration |
260 | 244 (RealTime::fromXsdDuration(onode.value.toStdString())); |
261 if (v.type == SimpleSPARQLQuery::LiteralValue) { | 245 } else { |
262 | 246 cerr << "RDFTransformFactory: ERROR: Inconsistent optionals lists (unexpected optional \"" << optional << "\"" << endl; |
263 if (optional == "program") { | |
264 transform.setProgram(v.value); | |
265 } else if (optional == "summary_type") { | |
266 transform.setSummaryType | |
267 (transform.stringToSummaryType(v.value)); | |
268 } else if (optional == "step_size") { | |
269 transform.setStepSize(v.value.toUInt()); | |
270 } else if (optional == "block_size") { | |
271 transform.setBlockSize(v.value.toUInt()); | |
272 } else if (optional == "window_type") { | |
273 transform.setWindowType | |
274 (Window<float>::getTypeForName | |
275 (v.value.toLower().toStdString())); | |
276 } else if (optional == "sample_rate") { | |
277 transform.setSampleRate(v.value.toFloat()); | |
278 } else if (optional == "start") { | |
279 transform.setStartTime | |
280 (RealTime::fromXsdDuration(v.value.toStdString())); | |
281 } else if (optional == "duration") { | |
282 transform.setDuration | |
283 (RealTime::fromXsdDuration(v.value.toStdString())); | |
284 } else { | |
285 cerr << "RDFTransformFactory: ERROR: Inconsistent optionals lists (unexpected optional \"" << optional << "\"" << endl; | |
286 } | |
287 } | |
288 } | 247 } |
289 } | 248 } |
290 | 249 |
291 SVDEBUG << "RDFTransformFactory: NOTE: Transform is: " << endl; | 250 SVDEBUG << "RDFTransformFactory: NOTE: Transform is: " << endl; |
292 cerr << transform.toXmlString() << endl; | 251 cerr << transform.toXmlString() << endl; |
299 | 258 |
300 bool | 259 bool |
301 RDFTransformFactoryImpl::setOutput(Transform &transform, | 260 RDFTransformFactoryImpl::setOutput(Transform &transform, |
302 QString transformUri) | 261 QString transformUri) |
303 { | 262 { |
304 SimpleSPARQLQuery::Value outputValue = | 263 Node outputNode = m_store->matchFirst |
305 SimpleSPARQLQuery::singleResultQuery | 264 (Triple(Uri(transformUri), "vamp:output", Node())).c; |
306 (SimpleSPARQLQuery::QueryFromModel, | 265 |
307 QString | 266 if (outputNode == Node()) return true; |
308 ( | 267 |
309 " PREFIX vamp: <http://purl.org/ontology/vamp/> " | 268 if (outputNode.type != Node::URI && outputNode.type != Node::Blank) { |
310 | 269 m_errorString = QString("vamp:output for output of transform <%1> is not a URI or blank node").arg(transformUri); |
311 " SELECT ?output_id " | 270 return false; |
312 | 271 } |
313 " WHERE { " | 272 |
314 " <%1> vamp:output ?output . " | 273 // Now, outputNode might be the subject of a triple within m_store |
315 " ?output vamp:identifier ?output_id " | 274 // that tells us the vamp:identifier, or it might be the subject |
316 " } " | 275 // of a triple within the indexer that tells us it |
317 ) | 276 |
318 .arg(transformUri), | 277 Node identNode = m_store->matchFirst |
319 "output_id"); | 278 (Triple(outputNode, "vamp:identifier", Node())).c; |
320 | 279 |
321 if (outputValue.type == SimpleSPARQLQuery::NoValue) { | 280 if (identNode == Node()) { |
322 return true; | 281 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); |
323 } | 282 const BasicStore *index = indexer->getIndex(); |
324 | 283 identNode = index->matchFirst |
325 if (outputValue.type != SimpleSPARQLQuery::LiteralValue) { | 284 (Triple(outputNode, "vamp:identifier", Node())).c; |
285 } | |
286 | |
287 if (identNode == Node() || identNode.type != Node::Literal) { | |
326 m_errorString = QString("No vamp:identifier found for output of transform <%1>, or vamp:identifier is not a literal").arg(transformUri); | 288 m_errorString = QString("No vamp:identifier found for output of transform <%1>, or vamp:identifier is not a literal").arg(transformUri); |
327 return false; | 289 return false; |
328 } | 290 } |
329 | 291 |
330 transform.setOutput(outputValue.value); | 292 transform.setOutput(identNode.value); |
331 | 293 |
332 return true; | 294 return true; |
333 } | 295 } |
334 | 296 |
335 | 297 |