comparison rdf/RDFTransformFactory.cpp @ 494:81963c51b488

* Add ability to export a transform structure as RDF -- for use when exporting data from runner, so as to refer back to the generating transform from the audio features rdf * some improvements to pane & layer management when importing rdf, but it's all still a big hack here
author Chris Cannam
date Tue, 25 Nov 2008 17:46:02 +0000
parents 3931711b5671
children 3176aade1a03
comparison
equal deleted inserted replaced
493:3931711b5671 494:81963c51b488
16 #include "RDFTransformFactory.h" 16 #include "RDFTransformFactory.h"
17 17
18 #include <map> 18 #include <map>
19 #include <vector> 19 #include <vector>
20 20
21 #include <QTextStream>
22
21 #include <iostream> 23 #include <iostream>
22 #include <cmath> 24 #include <cmath>
23 25
24 #include "SimpleSPARQLQuery.h" 26 #include "SimpleSPARQLQuery.h"
25 #include "PluginRDFIndexer.h" 27 #include "PluginRDFIndexer.h"
28 #include "PluginRDFDescription.h"
26 #include "base/ProgressReporter.h" 29 #include "base/ProgressReporter.h"
27 30
28 #include "transform/TransformFactory.h" 31 #include "transform/TransformFactory.h"
29 32
30 using std::cerr; 33 using std::cerr;
42 bool isRDF(); 45 bool isRDF();
43 bool isOK(); 46 bool isOK();
44 QString getErrorString() const; 47 QString getErrorString() const;
45 48
46 std::vector<Transform> getTransforms(ProgressReporter *); 49 std::vector<Transform> getTransforms(ProgressReporter *);
50
51 static QString writeTransformToRDF(const Transform &, QString);
47 52
48 protected: 53 protected:
49 QString m_urlString; 54 QString m_urlString;
50 QString m_errorString; 55 QString m_errorString;
51 bool m_isRDF; 56 bool m_isRDF;
92 RDFTransformFactory::getTransforms(ProgressReporter *r) 97 RDFTransformFactory::getTransforms(ProgressReporter *r)
93 { 98 {
94 return m_d->getTransforms(r); 99 return m_d->getTransforms(r);
95 } 100 }
96 101
102 QString
103 RDFTransformFactory::writeTransformToRDF(const Transform &t, QString f)
104 {
105 return RDFTransformFactoryImpl::writeTransformToRDF(t, f);
106 }
107
97 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) : 108 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) :
98 m_urlString(url), 109 m_urlString(url),
99 m_isRDF(false) 110 m_isRDF(false)
100 { 111 {
101 } 112 }
158 cerr << "RDFTransformFactory: NOTE: No RDF/TTL transform descriptions found in document at <" << m_urlString.toStdString() << ">" << endl; 169 cerr << "RDFTransformFactory: NOTE: No RDF/TTL transform descriptions found in document at <" << m_urlString.toStdString() << ">" << endl;
159 return transforms; 170 return transforms;
160 } 171 }
161 172
162 // There are various queries we need to make that might include 173 // There are various queries we need to make that might include
163 // data from iether the transform RDF or the model accumulated 174 // data from either the transform RDF or the model accumulated
164 // from plugin descriptions. For example, the transform RDF may 175 // from plugin descriptions. For example, the transform RDF may
165 // specify the output's true URI, or it might have a blank node or 176 // specify the output's true URI, or it might have a blank node or
166 // some other URI with the appropriate vamp:identifier included in 177 // some other URI with the appropriate vamp:identifier included in
167 // the file. To cover both cases, we need to add the file itself 178 // the file. To cover both cases, we need to add the file itself
168 // into the model and always query the model using the transform 179 // into the model and always query the model using the transform
290 (SimpleSPARQLQuery::QueryFromModel, 301 (SimpleSPARQLQuery::QueryFromModel,
291 QString 302 QString
292 ( 303 (
293 " PREFIX vamp: <http://purl.org/ontology/vamp/> " 304 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
294 305
295 " SELECT ?output " 306 " SELECT ?output_id "
296 307
297 " WHERE { " 308 " WHERE { "
298 " <%1> vamp:output ?output . " 309 " <%1> vamp:output ?output . "
310 " ?output vamp:identifier ?output_id "
299 " } " 311 " } "
300 ) 312 )
301 .arg(transformUri), 313 .arg(transformUri),
302 "output"); 314 "output_id");
303 315
304 if (outputValue.type == SimpleSPARQLQuery::NoValue) { 316 if (outputValue.type == SimpleSPARQLQuery::NoValue) {
305 return true; 317 return true;
306 } 318 }
307 319
308 if (outputValue.type != SimpleSPARQLQuery::URIValue) { 320 if (outputValue.type != SimpleSPARQLQuery::LiteralValue) {
309 m_errorString = QString("vamp:output given for transform <%1> is not a URI").arg(transformUri); 321 m_errorString = QString("No vamp:identifier found for output of transform <%1>, or vamp:identifier is not a literal").arg(transformUri);
310 return false; 322 return false;
311 } 323 }
312 324
313 SimpleSPARQLQuery::Value outputIdValue = 325 transform.setOutput(outputValue.value);
314 SimpleSPARQLQuery::singleResultQuery
315 (SimpleSPARQLQuery::QueryFromModel,
316 QString
317 (
318 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
319
320 " SELECT ?output_id "
321
322 " WHERE { "
323 " <%1> vamp:identifier ?output_id "
324 " } "
325 )
326 .arg(outputValue.value),
327 "output_id");
328
329 if (outputIdValue.type != SimpleSPARQLQuery::LiteralValue) {
330 m_errorString = QString("No vamp:identifier found for output <%1>, or vamp:identifier is not a literal").arg(outputValue.value);
331 return false;
332 }
333
334 transform.setOutput(outputIdValue.value);
335 326
336 return true; 327 return true;
337 } 328 }
338 329
339 330
381 } 372 }
382 373
383 return true; 374 return true;
384 } 375 }
385 376
377 QString
378 RDFTransformFactoryImpl::writeTransformToRDF(const Transform &transform,
379 QString uri)
380 {
381 QString str;
382 QTextStream s(&str);
383
384 // assumes the usual prefixes are available
385
386 s << uri << " a vamp:Transform ;" << endl;
387
388 QString pluginId = transform.getPluginIdentifier();
389 QString pluginUri = PluginRDFIndexer::getInstance()->getURIForPluginId(pluginId);
390
391 PluginRDFDescription description(pluginId);
392 QString outputUri = description.getOutputUri(transform.getOutput());
393
394 if (transform.getOutput() != "" && outputUri == "") {
395 std::cerr << "WARNING: RDFTransformFactory::writeTransformToRDF: No output URI available for transform output id \"" << transform.getOutput().toStdString() << "\"" << std::endl;
396 }
397
398 s << " vamp:plugin <" << pluginUri << "> ;" << endl;
399
400 if (transform.getStepSize() != 0) {
401 s << " vamp:step_size \"" << transform.getStepSize() << "\"^^xsd:int ; " << endl;
402 }
403 if (transform.getBlockSize() != 0) {
404 s << " vamp:block_size \"" << transform.getBlockSize() << "\"^^xsd:int ; " << endl;
405 }
406 if (transform.getStartTime() != RealTime::zeroTime) {
407 s << " vamp:start \"" << transform.getStartTime().toXsdDuration().c_str() << "\"^^xsd:duration ; " << endl;
408 }
409 if (transform.getDuration() != RealTime::zeroTime) {
410 s << " vamp:duration \"" << transform.getDuration().toXsdDuration().c_str() << "\"^^xsd:duration ; " << endl;
411 }
412 if (transform.getSampleRate() != 0) {
413 s << " vamp:sample_rate \"" << transform.getSampleRate() << "\"^^xsd:float ; " << endl;
414 }
415
416 QString program = transform.getProgram();
417
418 if (program != "") {
419 s << " vamp:program \"\"\"" << program << "\"\"\" ;" << endl;
420 }
421
422 Transform::ParameterMap parameters = transform.getParameters();
423 for (Transform::ParameterMap::const_iterator i = parameters.begin();
424 i != parameters.end(); ++i) {
425 QString name = i->first;
426 float value = i->second;
427 s << " vamp:parameter_binding [" << endl;
428 s << " vamp:parameter [ vamp:identifier \"" << name << "\" ] ;" << endl;
429 s << " vamp:value \"" << value << "\"^^xsd:float ;" << endl;
430 s << " ] ;" << endl;
431 }
432
433 if (outputUri != "") {
434 s << " vamp:output <" << outputUri << "> ." << endl;
435 } else {
436 s << " ." << endl;
437 }
438
439 return str;
440 }
441