annotate rdf/RDFTransformFactory.cpp @ 727:211efc770335 dataquay

Tiny bit of work on the RDFTransformFactory
author Chris Cannam
date Fri, 18 May 2012 21:23:12 +0100
parents 1424aa29ae95
children f2de9a42830e
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@727 7 This file copyright 2008-2012 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 "RDFTransformFactory.h"
Chris@439 17
Chris@439 18 #include <map>
Chris@439 19 #include <vector>
Chris@439 20
Chris@494 21 #include <QTextStream>
Chris@592 22 #include <QUrl>
Chris@494 23
Chris@439 24 #include <iostream>
Chris@439 25 #include <cmath>
Chris@439 26
Chris@439 27 #include "PluginRDFIndexer.h"
Chris@494 28 #include "PluginRDFDescription.h"
Chris@439 29 #include "base/ProgressReporter.h"
Chris@503 30 #include "plugin/PluginIdentifier.h"
Chris@439 31
Chris@439 32 #include "transform/TransformFactory.h"
Chris@439 33
Chris@727 34 #include <dataquay/BasicStore.h>
Chris@727 35 #include <dataquay/PropertyObject.h>
Chris@727 36
Chris@439 37 using std::cerr;
Chris@439 38 using std::endl;
Chris@439 39
Chris@727 40 using Dataquay::Uri;
Chris@727 41 using Dataquay::Node;
Chris@727 42 using Dataquay::Nodes;
Chris@727 43 using Dataquay::Triple;
Chris@727 44 using Dataquay::Triples;
Chris@727 45 using Dataquay::BasicStore;
Chris@727 46 using Dataquay::PropertyObject;
Chris@439 47
Chris@439 48
Chris@439 49 class RDFTransformFactoryImpl
Chris@439 50 {
Chris@439 51 public:
Chris@439 52 RDFTransformFactoryImpl(QString url);
Chris@439 53 virtual ~RDFTransformFactoryImpl();
Chris@439 54
Chris@493 55 bool isRDF();
Chris@439 56 bool isOK();
Chris@439 57 QString getErrorString() const;
Chris@439 58
Chris@439 59 std::vector<Transform> getTransforms(ProgressReporter *);
Chris@439 60
Chris@494 61 static QString writeTransformToRDF(const Transform &, QString);
Chris@494 62
Chris@439 63 protected:
Chris@727 64 BasicStore *m_store;
Chris@439 65 QString m_urlString;
Chris@439 66 QString m_errorString;
Chris@493 67 bool m_isRDF;
Chris@489 68 bool setOutput(Transform &, QString);
Chris@489 69 bool setParameters(Transform &, QString);
Chris@439 70 };
Chris@439 71
Chris@439 72
Chris@439 73 QString
Chris@439 74 RDFTransformFactory::getKnownExtensions()
Chris@439 75 {
Chris@439 76 return "*.rdf *.n3 *.ttl";
Chris@439 77 }
Chris@439 78
Chris@439 79 RDFTransformFactory::RDFTransformFactory(QString url) :
Chris@439 80 m_d(new RDFTransformFactoryImpl(url))
Chris@439 81 {
Chris@439 82 }
Chris@439 83
Chris@439 84 RDFTransformFactory::~RDFTransformFactory()
Chris@439 85 {
Chris@439 86 delete m_d;
Chris@439 87 }
Chris@439 88
Chris@439 89 bool
Chris@493 90 RDFTransformFactory::isRDF()
Chris@493 91 {
Chris@493 92 return m_d->isRDF();
Chris@493 93 }
Chris@493 94
Chris@493 95 bool
Chris@439 96 RDFTransformFactory::isOK()
Chris@439 97 {
Chris@439 98 return m_d->isOK();
Chris@439 99 }
Chris@439 100
Chris@439 101 QString
Chris@439 102 RDFTransformFactory::getErrorString() const
Chris@439 103 {
Chris@439 104 return m_d->getErrorString();
Chris@439 105 }
Chris@439 106
Chris@439 107 std::vector<Transform>
Chris@439 108 RDFTransformFactory::getTransforms(ProgressReporter *r)
Chris@439 109 {
Chris@439 110 return m_d->getTransforms(r);
Chris@439 111 }
Chris@439 112
Chris@494 113 QString
Chris@494 114 RDFTransformFactory::writeTransformToRDF(const Transform &t, QString f)
Chris@494 115 {
Chris@494 116 return RDFTransformFactoryImpl::writeTransformToRDF(t, f);
Chris@494 117 }
Chris@494 118
Chris@439 119 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) :
Chris@727 120 m_store(new BasicStore),
Chris@493 121 m_urlString(url),
Chris@493 122 m_isRDF(false)
Chris@439 123 {
Chris@727 124 //!!! retrieve data if remote... then
Chris@727 125 m_store->addPrefix("vamp", Uri("http://purl.org/ontology/vamp/"));
Chris@727 126 try {
Chris@727 127 m_store->import(QUrl::fromLocalFile(url), BasicStore::ImportIgnoreDuplicates);
Chris@727 128 m_isRDF = true;
Chris@727 129 } catch (...) { }
Chris@439 130 }
Chris@439 131
Chris@439 132 RDFTransformFactoryImpl::~RDFTransformFactoryImpl()
Chris@439 133 {
Chris@727 134 delete m_store;
Chris@439 135 }
Chris@439 136
Chris@439 137 bool
Chris@493 138 RDFTransformFactoryImpl::isRDF()
Chris@493 139 {
Chris@493 140 return m_isRDF;
Chris@493 141 }
Chris@493 142
Chris@493 143 bool
Chris@439 144 RDFTransformFactoryImpl::isOK()
Chris@439 145 {
Chris@439 146 return (m_errorString == "");
Chris@439 147 }
Chris@439 148
Chris@439 149 QString
Chris@439 150 RDFTransformFactoryImpl::getErrorString() const
Chris@439 151 {
Chris@439 152 return m_errorString;
Chris@439 153 }
Chris@439 154
Chris@439 155 std::vector<Transform>
Chris@439 156 RDFTransformFactoryImpl::getTransforms(ProgressReporter *reporter)
Chris@439 157 {
Chris@439 158 std::vector<Transform> transforms;
Chris@439 159
Chris@440 160 std::map<QString, Transform> uriTransformMap;
Chris@439 161
Chris@727 162 Nodes tnodes = m_store->match
Chris@727 163 (Triple(Node(), "a", m_store->expand("vamp:Transform"))).a();
Chris@439 164
Chris@727 165 foreach (Node tnode, tnodes) {
Chris@439 166
Chris@727 167 Node pnode = m_store->matchFirst
Chris@727 168 (Triple(tnode, "vamp:plugin", Node())).c;
Chris@440 169
Chris@727 170 // There are various queries we need to make that might
Chris@727 171 // include data from either the transform RDF or the model
Chris@727 172 // accumulated from plugin descriptions. For example, the
Chris@727 173 // transform RDF may specify the output's true URI, or it
Chris@727 174 // might have a blank node or some other URI with the
Chris@727 175 // appropriate vamp:identifier included in the file. To cover
Chris@727 176 // both cases, we need to add the file itself into the model
Chris@727 177 // and always query the model using the transform URI rather
Chris@727 178 // than querying the file itself subsequently.
Chris@440 179
Chris@727 180 //!!! ^^^ what does this mean for us with Dataquay? do we need to cross-check outputs against the indexer?
Chris@489 181
Chris@489 182 SimpleSPARQLQuery::addSourceToModel(m_urlString);
Chris@489 183
Chris@439 184 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance();
Chris@439 185
Chris@440 186 for (int i = 0; i < transformResults.size(); ++i) {
Chris@439 187
Chris@440 188 SimpleSPARQLQuery::KeyValueMap &result = transformResults[i];
Chris@439 189
Chris@439 190 QString transformUri = result["transform"].value;
Chris@439 191 QString pluginUri = result["plugin"].value;
Chris@439 192
Chris@439 193 QString pluginId = indexer->getIdForPluginURI(pluginUri);
Chris@439 194 if (pluginId == "") {
Chris@439 195 cerr << "RDFTransformFactory: WARNING: Unknown plugin <"
Chris@686 196 << pluginUri << "> for transform <"
Chris@686 197 << transformUri << ">, skipping this transform"
Chris@440 198 << endl;
Chris@440 199 continue;
Chris@440 200 }
Chris@440 201
Chris@439 202 Transform transform;
Chris@439 203 transform.setPluginIdentifier(pluginId);
Chris@439 204
Chris@489 205 if (!setOutput(transform, transformUri)) {
Chris@439 206 return transforms;
Chris@439 207 }
Chris@439 208
Chris@489 209 if (!setParameters(transform, transformUri)) {
Chris@439 210 return transforms;
Chris@439 211 }
Chris@439 212
Chris@440 213 uriTransformMap[transformUri] = transform;
Chris@439 214
Chris@489 215 // We have to do this a very long way round, to work around
Chris@489 216 // rasqal's current inability to handle correctly more than one
Chris@489 217 // OPTIONAL graph in a query
Chris@439 218
Chris@489 219 static const char *optionals[] = {
Chris@489 220 "output",
Chris@489 221 "program",
Chris@508 222 "summary_type",
Chris@489 223 "step_size",
Chris@489 224 "block_size",
Chris@489 225 "window_type",
Chris@489 226 "sample_rate",
Chris@489 227 "start",
Chris@489 228 "duration"
Chris@489 229 };
Chris@489 230
Chris@489 231 for (int j = 0; j < sizeof(optionals)/sizeof(optionals[0]); ++j) {
Chris@439 232
Chris@489 233 QString optional = optionals[j];
Chris@489 234
Chris@489 235 QString queryTemplate =
Chris@489 236 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
Chris@489 237
Chris@489 238 " SELECT ?%1 "
Chris@489 239
Chris@489 240 " WHERE { "
Chris@489 241 " <%2> vamp:%1 ?%1 "
Chris@489 242 " } ";
Chris@489 243
Chris@489 244 SimpleSPARQLQuery query
Chris@489 245 (SimpleSPARQLQuery::QueryFromModel,
Chris@489 246 queryTemplate.arg(optional).arg(transformUri));
Chris@440 247
Chris@489 248 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@440 249
Chris@489 250 if (!query.isOK()) {
Chris@489 251 m_errorString = query.getErrorString();
Chris@489 252 return transforms;
Chris@440 253 }
Chris@440 254
Chris@489 255 if (results.empty()) continue;
Chris@440 256
Chris@489 257 for (int k = 0; k < results.size(); ++k) {
Chris@489 258
Chris@489 259 const SimpleSPARQLQuery::Value &v = results[k][optional];
Chris@489 260
Chris@489 261 if (v.type == SimpleSPARQLQuery::LiteralValue) {
Chris@440 262
Chris@489 263 if (optional == "program") {
Chris@489 264 transform.setProgram(v.value);
Chris@508 265 } else if (optional == "summary_type") {
Chris@508 266 transform.setSummaryType
Chris@508 267 (transform.stringToSummaryType(v.value));
Chris@489 268 } else if (optional == "step_size") {
Chris@489 269 transform.setStepSize(v.value.toUInt());
Chris@489 270 } else if (optional == "block_size") {
Chris@489 271 transform.setBlockSize(v.value.toUInt());
Chris@489 272 } else if (optional == "window_type") {
Chris@688 273 transform.setWindowType
Chris@688 274 (Window<float>::getTypeForName
Chris@688 275 (v.value.toLower().toStdString()));
Chris@489 276 } else if (optional == "sample_rate") {
Chris@489 277 transform.setSampleRate(v.value.toFloat());
Chris@489 278 } else if (optional == "start") {
Chris@489 279 transform.setStartTime
Chris@489 280 (RealTime::fromXsdDuration(v.value.toStdString()));
Chris@489 281 } else if (optional == "duration") {
Chris@489 282 transform.setDuration
Chris@489 283 (RealTime::fromXsdDuration(v.value.toStdString()));
Chris@489 284 } else {
Chris@686 285 cerr << "RDFTransformFactory: ERROR: Inconsistent optionals lists (unexpected optional \"" << optional << "\"" << endl;
Chris@489 286 }
Chris@440 287 }
Chris@440 288 }
Chris@440 289 }
Chris@440 290
Chris@690 291 SVDEBUG << "RDFTransformFactory: NOTE: Transform is: " << endl;
Chris@686 292 cerr << transform.toXmlString() << endl;
Chris@439 293
Chris@439 294 transforms.push_back(transform);
Chris@439 295 }
Chris@439 296
Chris@439 297 return transforms;
Chris@439 298 }
Chris@439 299
Chris@440 300 bool
Chris@440 301 RDFTransformFactoryImpl::setOutput(Transform &transform,
Chris@489 302 QString transformUri)
Chris@440 303 {
Chris@489 304 SimpleSPARQLQuery::Value outputValue =
Chris@489 305 SimpleSPARQLQuery::singleResultQuery
Chris@489 306 (SimpleSPARQLQuery::QueryFromModel,
Chris@489 307 QString
Chris@489 308 (
Chris@489 309 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
Chris@489 310
Chris@494 311 " SELECT ?output_id "
Chris@489 312
Chris@489 313 " WHERE { "
Chris@489 314 " <%1> vamp:output ?output . "
Chris@494 315 " ?output vamp:identifier ?output_id "
Chris@489 316 " } "
Chris@489 317 )
Chris@489 318 .arg(transformUri),
Chris@494 319 "output_id");
Chris@489 320
Chris@489 321 if (outputValue.type == SimpleSPARQLQuery::NoValue) {
Chris@489 322 return true;
Chris@489 323 }
Chris@494 324
Chris@494 325 if (outputValue.type != SimpleSPARQLQuery::LiteralValue) {
Chris@494 326 m_errorString = QString("No vamp:identifier found for output of transform <%1>, or vamp:identifier is not a literal").arg(transformUri);
Chris@489 327 return false;
Chris@489 328 }
Chris@489 329
Chris@494 330 transform.setOutput(outputValue.value);
Chris@440 331
Chris@440 332 return true;
Chris@440 333 }
Chris@440 334
Chris@440 335
Chris@440 336 bool
Chris@440 337 RDFTransformFactoryImpl::setParameters(Transform &transform,
Chris@489 338 QString transformUri)
Chris@440 339 {
Chris@440 340 SimpleSPARQLQuery paramQuery
Chris@489 341 (SimpleSPARQLQuery::QueryFromModel,
Chris@480 342 QString
Chris@440 343 (
Chris@440 344 " PREFIX vamp: <http://purl.org/ontology/vamp/> "
Chris@440 345
Chris@440 346 " SELECT ?param_id ?param_value "
Chris@440 347
Chris@440 348 " WHERE { "
Chris@489 349 " <%1> vamp:parameter_binding ?binding . "
Chris@440 350 " ?binding vamp:parameter ?param ; "
Chris@440 351 " vamp:value ?param_value . "
Chris@440 352 " ?param vamp:identifier ?param_id "
Chris@440 353 " } "
Chris@440 354 )
Chris@440 355 .arg(transformUri));
Chris@440 356
Chris@440 357 SimpleSPARQLQuery::ResultList paramResults = paramQuery.execute();
Chris@440 358
Chris@440 359 if (!paramQuery.isOK()) {
Chris@440 360 m_errorString = paramQuery.getErrorString();
Chris@440 361 return false;
Chris@440 362 }
Chris@440 363
Chris@440 364 if (paramQuery.wasCancelled()) {
Chris@440 365 m_errorString = "Query cancelled";
Chris@440 366 return false;
Chris@440 367 }
Chris@440 368
Chris@440 369 for (int j = 0; j < paramResults.size(); ++j) {
Chris@440 370
Chris@440 371 QString paramId = paramResults[j]["param_id"].value;
Chris@440 372 QString paramValue = paramResults[j]["param_value"].value;
Chris@440 373
Chris@440 374 if (paramId == "" || paramValue == "") continue;
Chris@440 375
Chris@440 376 transform.setParameter(paramId, paramValue.toFloat());
Chris@440 377 }
Chris@440 378
Chris@440 379 return true;
Chris@440 380 }
Chris@440 381
Chris@494 382 QString
Chris@494 383 RDFTransformFactoryImpl::writeTransformToRDF(const Transform &transform,
Chris@494 384 QString uri)
Chris@494 385 {
Chris@494 386 QString str;
Chris@494 387 QTextStream s(&str);
Chris@494 388
Chris@503 389 // assumes the usual prefixes are available; requires that uri be
Chris@503 390 // a local fragment (e.g. ":transform") rather than a uri enclosed
Chris@503 391 // in <>, so that we can suffix it if need be
Chris@494 392
Chris@494 393 QString pluginId = transform.getPluginIdentifier();
Chris@494 394 QString pluginUri = PluginRDFIndexer::getInstance()->getURIForPluginId(pluginId);
Chris@494 395
Chris@503 396 if (pluginUri != "") {
Chris@503 397 s << uri << " a vamp:Transform ;" << endl;
Chris@592 398 s << " vamp:plugin <" << QUrl(pluginUri).toEncoded().data() << "> ;" << endl;
Chris@503 399 } else {
Chris@686 400 std::cerr << "WARNING: RDFTransformFactory::writeTransformToRDF: No plugin URI available for plugin id \"" << pluginId << "\", writing synthetic plugin and library resources" << std::endl;
Chris@503 401 QString type, soname, label;
Chris@503 402 PluginIdentifier::parseIdentifier(pluginId, type, soname, label);
Chris@503 403 s << uri << "_plugin a vamp:Plugin ;" << endl;
Chris@503 404 s << " vamp:identifier \"" << label << "\" .\n" << endl;
Chris@503 405 s << uri << "_library a vamp:PluginLibrary ;" << endl;
Chris@503 406 s << " vamp:identifier \"" << soname << "\" ;" << endl;
Chris@503 407 s << " vamp:available_plugin " << uri << "_plugin .\n" << endl;
Chris@503 408 s << uri << " a vamp:Transform ;" << endl;
Chris@503 409 s << " vamp:plugin " << uri << "_plugin ;" << endl;
Chris@503 410 }
Chris@503 411
Chris@494 412 PluginRDFDescription description(pluginId);
Chris@503 413 QString outputId = transform.getOutput();
Chris@503 414 QString outputUri = description.getOutputUri(outputId);
Chris@494 415
Chris@494 416 if (transform.getOutput() != "" && outputUri == "") {
Chris@686 417 std::cerr << "WARNING: RDFTransformFactory::writeTransformToRDF: No output URI available for transform output id \"" << transform.getOutput() << "\", writing a synthetic output resource" << std::endl;
Chris@494 418 }
Chris@494 419
Chris@494 420 if (transform.getStepSize() != 0) {
Chris@494 421 s << " vamp:step_size \"" << transform.getStepSize() << "\"^^xsd:int ; " << endl;
Chris@494 422 }
Chris@494 423 if (transform.getBlockSize() != 0) {
Chris@494 424 s << " vamp:block_size \"" << transform.getBlockSize() << "\"^^xsd:int ; " << endl;
Chris@494 425 }
Chris@494 426 if (transform.getStartTime() != RealTime::zeroTime) {
Chris@494 427 s << " vamp:start \"" << transform.getStartTime().toXsdDuration().c_str() << "\"^^xsd:duration ; " << endl;
Chris@494 428 }
Chris@494 429 if (transform.getDuration() != RealTime::zeroTime) {
Chris@494 430 s << " vamp:duration \"" << transform.getDuration().toXsdDuration().c_str() << "\"^^xsd:duration ; " << endl;
Chris@494 431 }
Chris@494 432 if (transform.getSampleRate() != 0) {
Chris@494 433 s << " vamp:sample_rate \"" << transform.getSampleRate() << "\"^^xsd:float ; " << endl;
Chris@494 434 }
Chris@494 435
Chris@494 436 QString program = transform.getProgram();
Chris@494 437 if (program != "") {
Chris@494 438 s << " vamp:program \"\"\"" << program << "\"\"\" ;" << endl;
Chris@494 439 }
Chris@494 440
Chris@508 441 QString summary = transform.summaryTypeToString(transform.getSummaryType());
Chris@508 442 if (summary != "") {
Chris@508 443 s << " vamp:summary_type \"" << summary << "\" ;" << endl;
Chris@508 444 }
Chris@508 445
Chris@494 446 Transform::ParameterMap parameters = transform.getParameters();
Chris@494 447 for (Transform::ParameterMap::const_iterator i = parameters.begin();
Chris@494 448 i != parameters.end(); ++i) {
Chris@494 449 QString name = i->first;
Chris@494 450 float value = i->second;
Chris@494 451 s << " vamp:parameter_binding [" << endl;
Chris@494 452 s << " vamp:parameter [ vamp:identifier \"" << name << "\" ] ;" << endl;
Chris@494 453 s << " vamp:value \"" << value << "\"^^xsd:float ;" << endl;
Chris@494 454 s << " ] ;" << endl;
Chris@494 455 }
Chris@494 456
Chris@494 457 if (outputUri != "") {
Chris@592 458 s << " vamp:output <" << QUrl(outputUri).toEncoded().data() << "> ." << endl;
Chris@503 459 } else if (outputId != "") {
Chris@503 460 s << " vamp:output [ vamp:identifier \"" << outputId << "\" ] ." << endl;
Chris@494 461 } else {
Chris@494 462 s << " ." << endl;
Chris@494 463 }
Chris@494 464
Chris@494 465 return str;
Chris@494 466 }
Chris@494 467