annotate rdf/RDFImporter.cpp @ 504:3376dc26dece

* Add FeatureWriter method to set basic track metadata; write it out from RDFFeatureWriter if present
author Chris Cannam
date Fri, 05 Dec 2008 11:09:44 +0000
parents 83eae5239db6
children 1b8c748fd7ea
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@439 7 This file copyright 2008 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 "RDFImporter.h"
Chris@439 17
Chris@439 18 #include <map>
Chris@439 19 #include <vector>
Chris@439 20
Chris@439 21 #include <iostream>
Chris@439 22 #include <cmath>
Chris@439 23
Chris@439 24 #include "SimpleSPARQLQuery.h"
Chris@439 25
Chris@439 26 #include "base/ProgressReporter.h"
Chris@439 27 #include "base/RealTime.h"
Chris@439 28
Chris@439 29 #include "data/model/SparseOneDimensionalModel.h"
Chris@439 30 #include "data/model/SparseTimeValueModel.h"
Chris@439 31 #include "data/model/EditableDenseThreeDimensionalModel.h"
Chris@449 32 #include "data/model/NoteModel.h"
Chris@449 33 #include "data/model/RegionModel.h"
Chris@499 34 #include "data/model/WaveFileModel.h"
Chris@499 35
Chris@499 36 #include "data/fileio/FileSource.h"
Chris@439 37
Chris@439 38 using std::cerr;
Chris@439 39 using std::endl;
Chris@439 40
Chris@439 41 class RDFImporterImpl
Chris@439 42 {
Chris@439 43 public:
Chris@439 44 RDFImporterImpl(QString url, int sampleRate);
Chris@439 45 virtual ~RDFImporterImpl();
Chris@490 46
Chris@490 47 void setSampleRate(int sampleRate) { m_sampleRate = sampleRate; }
Chris@439 48
Chris@439 49 bool isOK();
Chris@439 50 QString getErrorString() const;
Chris@439 51
Chris@439 52 std::vector<Model *> getDataModels(ProgressReporter *);
Chris@439 53
Chris@439 54 protected:
Chris@439 55 QString m_uristring;
Chris@439 56 QString m_errorString;
Chris@499 57 std::map<QString, Model *> m_audioModelMap;
Chris@439 58 int m_sampleRate;
Chris@439 59
Chris@499 60 void getDataModelsAudio(std::vector<Model *> &, ProgressReporter *);
Chris@440 61 void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
Chris@440 62 void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
Chris@440 63
Chris@493 64 void getDenseModelTitle(Model *, QString, QString);
Chris@493 65
Chris@440 66 void getDenseFeatureProperties(QString featureUri,
Chris@440 67 int &sampleRate, int &windowLength,
Chris@440 68 int &hopSize, int &width, int &height);
Chris@440 69
Chris@449 70 void fillModel(Model *, long, long, bool, std::vector<float> &, QString);
Chris@439 71 };
Chris@439 72
Chris@439 73
Chris@439 74 QString
Chris@439 75 RDFImporter::getKnownExtensions()
Chris@439 76 {
Chris@439 77 return "*.rdf *.n3 *.ttl";
Chris@439 78 }
Chris@439 79
Chris@439 80 RDFImporter::RDFImporter(QString url, int sampleRate) :
Chris@439 81 m_d(new RDFImporterImpl(url, sampleRate))
Chris@439 82 {
Chris@439 83 }
Chris@439 84
Chris@439 85 RDFImporter::~RDFImporter()
Chris@439 86 {
Chris@439 87 delete m_d;
Chris@439 88 }
Chris@439 89
Chris@490 90 void
Chris@490 91 RDFImporter::setSampleRate(int sampleRate)
Chris@490 92 {
Chris@490 93 m_d->setSampleRate(sampleRate);
Chris@490 94 }
Chris@490 95
Chris@439 96 bool
Chris@439 97 RDFImporter::isOK()
Chris@439 98 {
Chris@439 99 return m_d->isOK();
Chris@439 100 }
Chris@439 101
Chris@439 102 QString
Chris@439 103 RDFImporter::getErrorString() const
Chris@439 104 {
Chris@439 105 return m_d->getErrorString();
Chris@439 106 }
Chris@439 107
Chris@439 108 std::vector<Model *>
Chris@439 109 RDFImporter::getDataModels(ProgressReporter *r)
Chris@439 110 {
Chris@439 111 return m_d->getDataModels(r);
Chris@439 112 }
Chris@439 113
Chris@439 114 RDFImporterImpl::RDFImporterImpl(QString uri, int sampleRate) :
Chris@439 115 m_uristring(uri),
Chris@439 116 m_sampleRate(sampleRate)
Chris@439 117 {
Chris@439 118 }
Chris@439 119
Chris@439 120 RDFImporterImpl::~RDFImporterImpl()
Chris@439 121 {
Chris@492 122 SimpleSPARQLQuery::closeSingleSource(m_uristring);
Chris@439 123 }
Chris@439 124
Chris@439 125 bool
Chris@439 126 RDFImporterImpl::isOK()
Chris@439 127 {
Chris@439 128 return (m_errorString == "");
Chris@439 129 }
Chris@439 130
Chris@439 131 QString
Chris@439 132 RDFImporterImpl::getErrorString() const
Chris@439 133 {
Chris@439 134 return m_errorString;
Chris@439 135 }
Chris@439 136
Chris@439 137 std::vector<Model *>
Chris@439 138 RDFImporterImpl::getDataModels(ProgressReporter *reporter)
Chris@439 139 {
Chris@439 140 std::vector<Model *> models;
Chris@439 141
Chris@499 142 getDataModelsAudio(models, reporter);
Chris@499 143
Chris@490 144 if (m_sampleRate == 0) {
Chris@490 145 std::cerr << "RDFImporter::getDataModels: invalid sample rate" << std::endl;
Chris@490 146 return models;
Chris@490 147 }
Chris@490 148
Chris@440 149 getDataModelsDense(models, reporter);
Chris@440 150
Chris@440 151 QString error;
Chris@440 152 if (!isOK()) error = m_errorString;
Chris@440 153 m_errorString = "";
Chris@440 154
Chris@440 155 getDataModelsSparse(models, reporter);
Chris@440 156
Chris@440 157 if (isOK()) m_errorString = error;
Chris@440 158
Chris@440 159 return models;
Chris@440 160 }
Chris@440 161
Chris@440 162 void
Chris@499 163 RDFImporterImpl::getDataModelsAudio(std::vector<Model *> &models,
Chris@499 164 ProgressReporter *reporter)
Chris@499 165 {
Chris@499 166 SimpleSPARQLQuery query = SimpleSPARQLQuery
Chris@499 167 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@499 168 QString
Chris@499 169 (
Chris@499 170 " PREFIX mo: <http://purl.org/ontology/mo/> "
Chris@499 171 " SELECT ?signal ?source FROM <%1> "
Chris@499 172 " WHERE { ?signal a mo:Signal ; mo:available_as ?source } "
Chris@499 173 )
Chris@499 174 .arg(m_uristring));
Chris@499 175
Chris@499 176 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@499 177
Chris@499 178 for (int i = 0; i < results.size(); ++i) {
Chris@499 179
Chris@499 180 QString signal = results[i]["signal"].value;
Chris@499 181 QString source = results[i]["source"].value;
Chris@499 182
Chris@499 183 FileSource fs(source, reporter);
Chris@499 184 if (fs.isAvailable()) {
Chris@499 185 if (reporter) {
Chris@499 186 reporter->setMessage(RDFImporter::tr("Importing audio referenced in RDF..."));
Chris@499 187 }
Chris@499 188 fs.waitForData();
Chris@499 189 WaveFileModel *newModel = new WaveFileModel(fs, m_sampleRate);
Chris@499 190 if (newModel->isOK()) {
Chris@499 191 std::cerr << "Successfully created wave file model from source at \"" << source.toStdString() << "\"" << std::endl;
Chris@499 192 models.push_back(newModel);
Chris@499 193 m_audioModelMap[signal] = newModel;
Chris@499 194 if (m_sampleRate == 0) {
Chris@499 195 m_sampleRate = newModel->getSampleRate();
Chris@499 196 }
Chris@499 197 } else {
Chris@499 198 std::cerr << "Failed to create wave file model from source at \"" << source.toStdString() << "\"" << std::endl;
Chris@499 199 delete newModel;
Chris@499 200 }
Chris@499 201 }
Chris@499 202 }
Chris@499 203 }
Chris@499 204
Chris@499 205 void
Chris@440 206 RDFImporterImpl::getDataModelsDense(std::vector<Model *> &models,
Chris@440 207 ProgressReporter *reporter)
Chris@440 208 {
Chris@499 209 if (reporter) {
Chris@499 210 reporter->setMessage(RDFImporter::tr("Importing dense signal data from RDF..."));
Chris@499 211 }
Chris@499 212
Chris@440 213 SimpleSPARQLQuery query = SimpleSPARQLQuery
Chris@489 214 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@480 215 QString
Chris@440 216 (
Chris@440 217 " PREFIX mo: <http://purl.org/ontology/mo/>"
Chris@440 218 " PREFIX af: <http://purl.org/ontology/af/>"
Chris@440 219
Chris@493 220 " SELECT ?feature ?feature_signal_type ?value "
Chris@440 221 " FROM <%1> "
Chris@440 222
Chris@440 223 " WHERE { "
Chris@440 224
Chris@493 225 " ?signal af:signal_feature ?feature . "
Chris@440 226
Chris@440 227 " ?feature a ?feature_signal_type ; "
Chris@440 228 " af:value ?value . "
Chris@440 229
Chris@440 230 " } "
Chris@440 231 )
Chris@440 232 .arg(m_uristring));
Chris@440 233
Chris@440 234 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@440 235
Chris@440 236 if (!query.isOK()) {
Chris@440 237 m_errorString = query.getErrorString();
Chris@440 238 return;
Chris@440 239 }
Chris@440 240
Chris@440 241 if (query.wasCancelled()) {
Chris@440 242 m_errorString = "Query cancelled";
Chris@440 243 return;
Chris@440 244 }
Chris@440 245
Chris@440 246 for (int i = 0; i < results.size(); ++i) {
Chris@440 247
Chris@440 248 QString feature = results[i]["feature"].value;
Chris@440 249 QString type = results[i]["feature_signal_type"].value;
Chris@440 250 QString value = results[i]["value"].value;
Chris@440 251
Chris@440 252 int sampleRate = 0;
Chris@440 253 int windowLength = 0;
Chris@440 254 int hopSize = 0;
Chris@440 255 int width = 0;
Chris@440 256 int height = 0;
Chris@440 257 getDenseFeatureProperties
Chris@440 258 (feature, sampleRate, windowLength, hopSize, width, height);
Chris@440 259
Chris@440 260 if (sampleRate != 0 && sampleRate != m_sampleRate) {
Chris@440 261 cerr << "WARNING: Sample rate in dense feature description does not match our underlying rate -- using rate from feature description" << endl;
Chris@440 262 }
Chris@440 263 if (sampleRate == 0) sampleRate = m_sampleRate;
Chris@440 264
Chris@440 265 if (hopSize == 0) {
Chris@440 266 cerr << "WARNING: Dense feature description does not specify a hop size -- assuming 1" << endl;
Chris@440 267 hopSize = 1;
Chris@440 268 }
Chris@440 269
Chris@440 270 if (height == 0) {
Chris@440 271 cerr << "WARNING: Dense feature description does not specify feature signal dimensions -- assuming one-dimensional (height = 1)" << endl;
Chris@440 272 height = 1;
Chris@440 273 }
Chris@440 274
Chris@440 275 QStringList values = value.split(' ', QString::SkipEmptyParts);
Chris@440 276
Chris@440 277 if (values.empty()) {
Chris@440 278 cerr << "WARNING: Dense feature description does not specify any values!" << endl;
Chris@440 279 continue;
Chris@440 280 }
Chris@440 281
Chris@440 282 if (height == 1) {
Chris@440 283
Chris@440 284 SparseTimeValueModel *m = new SparseTimeValueModel
Chris@440 285 (sampleRate, hopSize, false);
Chris@440 286
Chris@440 287 for (int j = 0; j < values.size(); ++j) {
Chris@440 288 float f = values[j].toFloat();
Chris@440 289 SparseTimeValueModel::Point point(j * hopSize, f, "");
Chris@440 290 m->addPoint(point);
Chris@440 291 }
Chris@493 292
Chris@493 293 getDenseModelTitle(m, feature, type);
Chris@440 294
Chris@440 295 models.push_back(m);
Chris@440 296
Chris@440 297 } else {
Chris@440 298
Chris@440 299 EditableDenseThreeDimensionalModel *m =
Chris@440 300 new EditableDenseThreeDimensionalModel(sampleRate, hopSize,
Chris@440 301 height, false);
Chris@440 302
Chris@440 303 EditableDenseThreeDimensionalModel::Column column;
Chris@440 304
Chris@440 305 int x = 0;
Chris@440 306
Chris@440 307 for (int j = 0; j < values.size(); ++j) {
Chris@440 308 if (j % height == 0 && !column.empty()) {
Chris@440 309 m->setColumn(x++, column);
Chris@440 310 column.clear();
Chris@440 311 }
Chris@440 312 column.push_back(values[j].toFloat());
Chris@440 313 }
Chris@440 314
Chris@440 315 if (!column.empty()) {
Chris@440 316 m->setColumn(x++, column);
Chris@440 317 }
Chris@440 318
Chris@493 319 getDenseModelTitle(m, feature, type);
Chris@493 320
Chris@440 321 models.push_back(m);
Chris@440 322 }
Chris@440 323 }
Chris@440 324 }
Chris@440 325
Chris@440 326 void
Chris@493 327 RDFImporterImpl::getDenseModelTitle(Model *m,
Chris@493 328 QString featureUri,
Chris@493 329 QString featureTypeUri)
Chris@493 330 {
Chris@493 331 QString titleQuery = QString
Chris@493 332 (
Chris@493 333 " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
Chris@493 334 " SELECT ?title "
Chris@493 335 " FROM <%1> "
Chris@493 336 " WHERE { "
Chris@493 337 " <%2> dc:title ?title . "
Chris@493 338 " } "
Chris@493 339 ).arg(m_uristring);
Chris@493 340
Chris@493 341 SimpleSPARQLQuery::Value v;
Chris@493 342
Chris@493 343 v = SimpleSPARQLQuery::singleResultQuery
Chris@493 344 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@493 345 titleQuery.arg(featureUri),
Chris@493 346 "title");
Chris@493 347
Chris@493 348 if (v.value != "") {
Chris@493 349 std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal) \"" << v.value.toStdString() << "\"" << std::endl;
Chris@493 350 m->setObjectName(v.value);
Chris@493 351 return;
Chris@493 352 }
Chris@493 353
Chris@493 354 v = SimpleSPARQLQuery::singleResultQuery
Chris@493 355 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@493 356 titleQuery.arg(featureTypeUri),
Chris@493 357 "title");
Chris@493 358
Chris@493 359 if (v.value != "") {
Chris@493 360 std::cerr << "RDFImporterImpl::getDenseModelTitle: Title (from signal type) \"" << v.value.toStdString() << "\"" << std::endl;
Chris@493 361 m->setObjectName(v.value);
Chris@493 362 return;
Chris@493 363 }
Chris@493 364
Chris@493 365 std::cerr << "RDFImporterImpl::getDenseModelTitle: No title available for feature <" << featureUri.toStdString() << ">" << std::endl;
Chris@493 366 }
Chris@493 367
Chris@493 368 void
Chris@440 369 RDFImporterImpl::getDenseFeatureProperties(QString featureUri,
Chris@440 370 int &sampleRate, int &windowLength,
Chris@440 371 int &hopSize, int &width, int &height)
Chris@440 372 {
Chris@489 373 SimpleSPARQLQuery::QueryType s = SimpleSPARQLQuery::QueryFromSingleSource;
Chris@489 374
Chris@440 375 QString dimensionsQuery
Chris@440 376 (
Chris@440 377 " PREFIX mo: <http://purl.org/ontology/mo/>"
Chris@440 378 " PREFIX af: <http://purl.org/ontology/af/>"
Chris@440 379
Chris@440 380 " SELECT ?dimensions "
Chris@440 381 " FROM <%1> "
Chris@440 382
Chris@440 383 " WHERE { "
Chris@440 384
Chris@440 385 " <%2> af:dimensions ?dimensions . "
Chris@440 386
Chris@440 387 " } "
Chris@440 388 );
Chris@440 389
Chris@440 390 SimpleSPARQLQuery::Value dimensionsValue =
Chris@489 391 SimpleSPARQLQuery::singleResultQuery
Chris@489 392 (s, dimensionsQuery.arg(m_uristring).arg(featureUri), "dimensions");
Chris@440 393
Chris@440 394 cerr << "Dimensions = \"" << dimensionsValue.value.toStdString() << "\""
Chris@440 395 << endl;
Chris@440 396
Chris@440 397 if (dimensionsValue.value != "") {
Chris@440 398 QStringList dl = dimensionsValue.value.split(" ");
Chris@440 399 if (dl.empty()) dl.push_back(dimensionsValue.value);
Chris@440 400 if (dl.size() > 0) height = dl[0].toInt();
Chris@440 401 if (dl.size() > 1) width = dl[1].toInt();
Chris@440 402 }
Chris@440 403
Chris@440 404 QString queryTemplate
Chris@440 405 (
Chris@440 406 " PREFIX mo: <http://purl.org/ontology/mo/>"
Chris@440 407 " PREFIX af: <http://purl.org/ontology/af/>"
Chris@440 408 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
Chris@440 409
Chris@440 410 " SELECT ?%3 "
Chris@440 411 " FROM <%1> "
Chris@440 412
Chris@440 413 " WHERE { "
Chris@440 414
Chris@440 415 " <%2> mo:time ?time . "
Chris@440 416
Chris@440 417 " ?time a tl:Interval ; "
Chris@440 418 " tl:onTimeLine ?timeline . "
Chris@440 419
Chris@440 420 " ?map tl:rangeTimeLine ?timeline . "
Chris@440 421
Chris@440 422 " ?map tl:%3 ?%3 . "
Chris@440 423
Chris@440 424 " } "
Chris@440 425 );
Chris@440 426
Chris@440 427 // Another laborious workaround for rasqal's failure to handle
Chris@440 428 // multiple optionals properly
Chris@440 429
Chris@440 430 SimpleSPARQLQuery::Value srValue =
Chris@489 431 SimpleSPARQLQuery::singleResultQuery(s,
Chris@480 432 queryTemplate
Chris@440 433 .arg(m_uristring).arg(featureUri)
Chris@440 434 .arg("sampleRate"),
Chris@440 435 "sampleRate");
Chris@440 436 if (srValue.value != "") {
Chris@440 437 sampleRate = srValue.value.toInt();
Chris@440 438 }
Chris@440 439
Chris@440 440 SimpleSPARQLQuery::Value hopValue =
Chris@489 441 SimpleSPARQLQuery::singleResultQuery(s,
Chris@480 442 queryTemplate
Chris@440 443 .arg(m_uristring).arg(featureUri)
Chris@440 444 .arg("hopSize"),
Chris@440 445 "hopSize");
Chris@440 446 if (srValue.value != "") {
Chris@440 447 hopSize = hopValue.value.toInt();
Chris@440 448 }
Chris@440 449
Chris@440 450 SimpleSPARQLQuery::Value winValue =
Chris@489 451 SimpleSPARQLQuery::singleResultQuery(s,
Chris@480 452 queryTemplate
Chris@440 453 .arg(m_uristring).arg(featureUri)
Chris@440 454 .arg("windowLength"),
Chris@440 455 "windowLength");
Chris@440 456 if (winValue.value != "") {
Chris@440 457 windowLength = winValue.value.toInt();
Chris@440 458 }
Chris@440 459
Chris@440 460 cerr << "sr = " << sampleRate << ", hop = " << hopSize << ", win = " << windowLength << endl;
Chris@440 461 }
Chris@440 462
Chris@440 463 void
Chris@440 464 RDFImporterImpl::getDataModelsSparse(std::vector<Model *> &models,
Chris@440 465 ProgressReporter *reporter)
Chris@440 466 {
Chris@499 467 if (reporter) {
Chris@499 468 reporter->setMessage(RDFImporter::tr("Importing event data from RDF..."));
Chris@499 469 }
Chris@499 470
Chris@489 471 SimpleSPARQLQuery::QueryType s = SimpleSPARQLQuery::QueryFromSingleSource;
Chris@489 472
Chris@439 473 // Our query is intended to retrieve every thing that has a time,
Chris@439 474 // and every feature type and value associated with a thing that
Chris@439 475 // has a time.
Chris@439 476
Chris@439 477 // We will then need to refine this big bag of results into a set
Chris@439 478 // of data models.
Chris@439 479
Chris@439 480 // Results that have different source signals should go into
Chris@439 481 // different models.
Chris@439 482
Chris@439 483 // Results that have different feature types should go into
Chris@439 484 // different models.
Chris@439 485
Chris@439 486 // Results that are sparse should go into different models from
Chris@439 487 // those that are dense (we need to examine the timestamps to
Chris@439 488 // establish this -- if the timestamps are regular, the results
Chris@439 489 // are dense -- so we can't do it as we go along, only after
Chris@439 490 // collecting all results).
Chris@439 491
Chris@439 492 // Timed things that have features associated with them should not
Chris@439 493 // appear directly in any model -- their features should appear
Chris@439 494 // instead -- and these should be different models from those used
Chris@439 495 // for timed things that do not have features.
Chris@439 496
Chris@439 497 // As we load the results, we'll push them into a partially
Chris@439 498 // structured container that maps from source signal (URI as
Chris@439 499 // string) -> feature type (likewise) -> time -> list of values.
Chris@439 500 // If the source signal or feature type is unavailable, the empty
Chris@439 501 // string will do.
Chris@439 502
Chris@449 503 QString prefixes = QString(
Chris@439 504 " PREFIX event: <http://purl.org/NET/c4dm/event.owl#>"
Chris@449 505 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
Chris@439 506 " PREFIX mo: <http://purl.org/ontology/mo/>"
Chris@439 507 " PREFIX af: <http://purl.org/ontology/af/>"
Chris@449 508 " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
Chris@449 509 );
Chris@439 510
Chris@449 511 QString queryString = prefixes + QString(
Chris@449 512
Chris@499 513 " SELECT ?signal ?timed_thing ?event_type ?value"
Chris@439 514 " FROM <%1>"
Chris@439 515
Chris@439 516 " WHERE {"
Chris@440 517
Chris@440 518 " ?signal a mo:Signal ."
Chris@440 519
Chris@439 520 " ?signal mo:time ?interval ."
Chris@449 521 " ?interval tl:onTimeLine ?tl ."
Chris@449 522 " ?time tl:onTimeLine ?tl ."
Chris@449 523 " ?timed_thing event:time ?time ."
Chris@440 524 " ?timed_thing a ?event_type ."
Chris@440 525
Chris@439 526 " OPTIONAL {"
Chris@440 527 " ?timed_thing af:feature ?value"
Chris@439 528 " }"
Chris@439 529 " }"
Chris@439 530
Chris@439 531 ).arg(m_uristring);
Chris@439 532
Chris@449 533 QString timeQueryString = prefixes + QString(
Chris@449 534
Chris@449 535 " SELECT ?time FROM <%1> "
Chris@449 536 " WHERE { "
Chris@449 537 " <%2> event:time ?t . "
Chris@449 538 " ?t tl:at ?time . "
Chris@449 539 " } "
Chris@449 540
Chris@449 541 ).arg(m_uristring);
Chris@449 542
Chris@449 543 QString rangeQueryString = prefixes + QString(
Chris@449 544
Chris@449 545 " SELECT ?time ?duration FROM <%1> "
Chris@449 546 " WHERE { "
Chris@449 547 " <%2> event:time ?t . "
Chris@449 548 " ?t tl:beginsAt ?time . "
Chris@449 549 " ?t tl:duration ?duration . "
Chris@449 550 " } "
Chris@449 551
Chris@449 552 ).arg(m_uristring);
Chris@449 553
Chris@449 554 QString labelQueryString = prefixes + QString(
Chris@449 555
Chris@449 556 " SELECT ?label FROM <%1> "
Chris@449 557 " WHERE { "
Chris@449 558 " <%2> rdfs:label ?label . "
Chris@449 559 " } "
Chris@449 560
Chris@449 561 ).arg(m_uristring);
Chris@449 562
Chris@489 563 SimpleSPARQLQuery query(s, queryString);
Chris@439 564 query.setProgressReporter(reporter);
Chris@439 565
Chris@500 566 // cerr << "Query will be: " << queryString.toStdString() << endl;
Chris@439 567
Chris@439 568 SimpleSPARQLQuery::ResultList results = query.execute();
Chris@439 569
Chris@439 570 if (!query.isOK()) {
Chris@439 571 m_errorString = query.getErrorString();
Chris@440 572 return;
Chris@439 573 }
Chris@439 574
Chris@439 575 if (query.wasCancelled()) {
Chris@439 576 m_errorString = "Query cancelled";
Chris@440 577 return;
Chris@439 578 }
Chris@439 579
Chris@449 580 /*
Chris@449 581 This function is now only used for sparse data (for dense data
Chris@449 582 we would be in getDataModelsDense instead).
Chris@449 583
Chris@449 584 For sparse data, the determining factors in deciding what model
Chris@449 585 to use are: Do the features have values? and Do the features
Chris@449 586 have duration?
Chris@449 587
Chris@449 588 We can run through the results and check off whether we find
Chris@449 589 values and duration for each of the source+type keys, and then
Chris@449 590 run through the source+type keys pushing each of the results
Chris@449 591 into a suitable model.
Chris@449 592
Chris@449 593 Unfortunately, at this point we do not yet have any actual
Chris@449 594 timing data (time/duration) -- just the time URI.
Chris@449 595
Chris@449 596 What we _could_ do is to create one of each type of model at the
Chris@449 597 start, for each of the source+type keys, and then push each
Chris@449 598 feature into the relevant model depending on what we find out
Chris@449 599 about it. Then return only non-empty models.
Chris@449 600 */
Chris@449 601
Chris@449 602 // Map from signal source to event type to dimensionality to
Chris@449 603 // presence of duration to model ptr. Whee!
Chris@449 604 std::map<QString, std::map<QString, std::map<int, std::map<bool, Model *> > > >
Chris@449 605 modelMap;
Chris@449 606
Chris@439 607 for (int i = 0; i < results.size(); ++i) {
Chris@439 608
Chris@499 609 if (i % 4 == 0) {
Chris@499 610 if (reporter) reporter->setProgress(i/4);
Chris@499 611 }
Chris@499 612
Chris@499 613 QString source = results[i]["signal"].value;
Chris@449 614 QString type = results[i]["event_type"].value;
Chris@449 615 QString thinguri = results[i]["timed_thing"].value;
Chris@449 616
Chris@449 617 RealTime time;
Chris@449 618 RealTime duration;
Chris@439 619
Chris@449 620 bool haveTime = false;
Chris@449 621 bool haveDuration = false;
Chris@439 622
Chris@449 623 QString label = SimpleSPARQLQuery::singleResultQuery
Chris@489 624 (s, labelQueryString.arg(thinguri), "label").value;
Chris@449 625
Chris@489 626 SimpleSPARQLQuery rangeQuery(s, rangeQueryString.arg(thinguri));
Chris@450 627 SimpleSPARQLQuery::ResultList rangeResults = rangeQuery.execute();
Chris@450 628 if (!rangeResults.empty()) {
Chris@450 629 // std::cerr << rangeResults.size() << " range results" << std::endl;
Chris@450 630 time = RealTime::fromXsdDuration
Chris@450 631 (rangeResults[0]["time"].value.toStdString());
Chris@450 632 duration = RealTime::fromXsdDuration
Chris@450 633 (rangeResults[0]["duration"].value.toStdString());
Chris@450 634 // std::cerr << "duration string " << rangeResults[0]["duration"].value.toStdString() << std::endl;
Chris@449 635 haveTime = true;
Chris@450 636 haveDuration = true;
Chris@449 637 } else {
Chris@450 638 QString timestring = SimpleSPARQLQuery::singleResultQuery
Chris@489 639 (s, timeQueryString.arg(thinguri), "time").value;
Chris@450 640 if (timestring != "") {
Chris@450 641 time = RealTime::fromXsdDuration(timestring.toStdString());
Chris@449 642 haveTime = true;
Chris@449 643 }
Chris@449 644 }
Chris@439 645
Chris@439 646 QString valuestring = results[i]["value"].value;
Chris@449 647 std::vector<float> values;
Chris@449 648
Chris@439 649 if (valuestring != "") {
Chris@449 650 QStringList vsl = valuestring.split(" ", QString::SkipEmptyParts);
Chris@449 651 for (int j = 0; j < vsl.size(); ++j) {
Chris@449 652 bool success = false;
Chris@449 653 float v = vsl[j].toFloat(&success);
Chris@449 654 if (success) values.push_back(v);
Chris@449 655 }
Chris@439 656 }
Chris@439 657
Chris@449 658 int dimensions = 1;
Chris@449 659 if (values.size() == 1) dimensions = 2;
Chris@449 660 else if (values.size() > 1) dimensions = 3;
Chris@449 661
Chris@449 662 Model *model = 0;
Chris@449 663
Chris@449 664 if (modelMap[source][type][dimensions].find(haveDuration) ==
Chris@449 665 modelMap[source][type][dimensions].end()) {
Chris@449 666
Chris@449 667 /*
Chris@449 668 std::cerr << "Creating new model: source = " << source.toStdString()
Chris@449 669 << ", type = " << type.toStdString() << ", dimensions = "
Chris@449 670 << dimensions << ", haveDuration = " << haveDuration
Chris@449 671 << ", time = " << time << ", duration = " << duration
Chris@449 672 << std::endl;
Chris@449 673 */
Chris@449 674
Chris@449 675 if (!haveDuration) {
Chris@449 676
Chris@449 677 if (dimensions == 1) {
Chris@449 678
Chris@449 679 // std::cerr << "SparseOneDimensionalModel" << std::endl;
Chris@449 680 model = new SparseOneDimensionalModel(m_sampleRate, 1, false);
Chris@449 681
Chris@449 682 } else if (dimensions == 2) {
Chris@449 683
Chris@449 684 // std::cerr << "SparseTimeValueModel" << std::endl;
Chris@449 685 model = new SparseTimeValueModel(m_sampleRate, 1, false);
Chris@449 686
Chris@449 687 } else {
Chris@449 688
Chris@449 689 // We don't have a three-dimensional sparse model,
Chris@449 690 // so use a note model. We do have some logic (in
Chris@449 691 // extractStructure below) for guessing whether
Chris@449 692 // this should after all have been a dense model,
Chris@449 693 // but it's hard to apply it because we don't have
Chris@449 694 // all the necessary timing data yet... hmm
Chris@449 695
Chris@449 696 // std::cerr << "NoteModel" << std::endl;
Chris@449 697 model = new NoteModel(m_sampleRate, 1, false);
Chris@449 698 }
Chris@449 699
Chris@449 700 } else { // haveDuration
Chris@449 701
Chris@449 702 if (dimensions == 1 || dimensions == 2) {
Chris@449 703
Chris@449 704 // If our units are frequency or midi pitch, we
Chris@449 705 // should be using a note model... hm
Chris@449 706
Chris@449 707 // std::cerr << "RegionModel" << std::endl;
Chris@449 708 model = new RegionModel(m_sampleRate, 1, false);
Chris@449 709
Chris@449 710 } else {
Chris@449 711
Chris@449 712 // We don't have a three-dimensional sparse model,
Chris@449 713 // so use a note model. We do have some logic (in
Chris@449 714 // extractStructure below) for guessing whether
Chris@449 715 // this should after all have been a dense model,
Chris@449 716 // but it's hard to apply it because we don't have
Chris@449 717 // all the necessary timing data yet... hmm
Chris@449 718
Chris@449 719 // std::cerr << "NoteModel" << std::endl;
Chris@449 720 model = new NoteModel(m_sampleRate, 1, false);
Chris@449 721 }
Chris@449 722 }
Chris@449 723
Chris@499 724 if (m_audioModelMap.find(source) != m_audioModelMap.end()) {
Chris@499 725 std::cerr << "source model for " << model << " is " << m_audioModelMap[source] << std::endl;
Chris@499 726 model->setSourceModel(m_audioModelMap[source]);
Chris@499 727 }
Chris@499 728
Chris@493 729 QString titleQuery = QString
Chris@493 730 (
Chris@493 731 " PREFIX dc: <http://purl.org/dc/elements/1.1/> "
Chris@493 732 " SELECT ?title "
Chris@493 733 " FROM <%1> "
Chris@493 734 " WHERE { "
Chris@493 735 " <%2> dc:title ?title . "
Chris@493 736 " } "
Chris@493 737 ).arg(m_uristring).arg(type);
Chris@493 738 QString title = SimpleSPARQLQuery::singleResultQuery
Chris@493 739 (s, titleQuery, "title").value;
Chris@493 740 if (title != "") model->setObjectName(title);
Chris@493 741
Chris@449 742 modelMap[source][type][dimensions][haveDuration] = model;
Chris@449 743 models.push_back(model);
Chris@449 744 }
Chris@449 745
Chris@449 746 model = modelMap[source][type][dimensions][haveDuration];
Chris@449 747
Chris@449 748 if (model) {
Chris@449 749 long ftime = RealTime::realTime2Frame(time, m_sampleRate);
Chris@449 750 long fduration = RealTime::realTime2Frame(duration, m_sampleRate);
Chris@449 751 fillModel(model, ftime, fduration, haveDuration, values, label);
Chris@439 752 }
Chris@439 753 }
Chris@439 754 }
Chris@439 755
Chris@439 756 void
Chris@449 757 RDFImporterImpl::fillModel(Model *model,
Chris@449 758 long ftime,
Chris@449 759 long fduration,
Chris@449 760 bool haveDuration,
Chris@449 761 std::vector<float> &values,
Chris@449 762 QString label)
Chris@449 763 {
Chris@493 764 // std::cerr << "RDFImporterImpl::fillModel: adding point at frame " << ftime << std::endl;
Chris@492 765
Chris@449 766 SparseOneDimensionalModel *sodm =
Chris@449 767 dynamic_cast<SparseOneDimensionalModel *>(model);
Chris@449 768 if (sodm) {
Chris@449 769 SparseOneDimensionalModel::Point point(ftime, label);
Chris@449 770 sodm->addPoint(point);
Chris@449 771 return;
Chris@449 772 }
Chris@449 773
Chris@449 774 SparseTimeValueModel *stvm =
Chris@449 775 dynamic_cast<SparseTimeValueModel *>(model);
Chris@449 776 if (stvm) {
Chris@449 777 SparseTimeValueModel::Point point
Chris@449 778 (ftime, values.empty() ? 0.f : values[0], label);
Chris@449 779 stvm->addPoint(point);
Chris@449 780 return;
Chris@449 781 }
Chris@449 782
Chris@449 783 NoteModel *nm =
Chris@449 784 dynamic_cast<NoteModel *>(model);
Chris@449 785 if (nm) {
Chris@449 786 if (haveDuration) {
Chris@449 787 float value = 0.f, level = 1.f;
Chris@449 788 if (!values.empty()) {
Chris@449 789 value = values[0];
Chris@449 790 if (values.size() > 1) {
Chris@449 791 level = values[1];
Chris@449 792 }
Chris@449 793 }
Chris@449 794 NoteModel::Point point(ftime, value, fduration, level, label);
Chris@449 795 nm->addPoint(point);
Chris@449 796 } else {
Chris@449 797 float value = 0.f, duration = 1.f, level = 1.f;
Chris@449 798 if (!values.empty()) {
Chris@449 799 value = values[0];
Chris@449 800 if (values.size() > 1) {
Chris@449 801 duration = values[1];
Chris@449 802 if (values.size() > 2) {
Chris@449 803 level = values[2];
Chris@449 804 }
Chris@449 805 }
Chris@449 806 }
Chris@449 807 NoteModel::Point point(ftime, value, duration, level, label);
Chris@449 808 nm->addPoint(point);
Chris@449 809 }
Chris@449 810 return;
Chris@449 811 }
Chris@449 812
Chris@449 813 RegionModel *rm =
Chris@449 814 dynamic_cast<RegionModel *>(model);
Chris@449 815 if (rm) {
Chris@449 816 if (haveDuration) {
Chris@449 817 RegionModel::Point point
Chris@449 818 (ftime, values.empty() ? 0.f : values[0], fduration, label);
Chris@449 819 rm->addPoint(point);
Chris@449 820 } else {
Chris@449 821 // This won't actually happen -- we only create region models
Chris@449 822 // if we do have duration -- but just for completeness
Chris@449 823 float value = 0.f, duration = 1.f;
Chris@449 824 if (!values.empty()) {
Chris@449 825 value = values[0];
Chris@449 826 if (values.size() > 1) {
Chris@449 827 duration = values[1];
Chris@449 828 }
Chris@449 829 }
Chris@449 830 RegionModel::Point point(ftime, value, duration, label);
Chris@449 831 rm->addPoint(point);
Chris@449 832 }
Chris@449 833 return;
Chris@449 834 }
Chris@449 835
Chris@449 836 std::cerr << "WARNING: RDFImporterImpl::fillModel: Unknown or unexpected model type" << std::endl;
Chris@449 837 return;
Chris@449 838 }
Chris@449 839
Chris@490 840 RDFImporter::RDFDocumentType
Chris@490 841 RDFImporter::identifyDocumentType(QString url)
Chris@490 842 {
Chris@490 843 bool haveAudio = false;
Chris@490 844 bool haveAnnotations = false;
Chris@449 845
Chris@499 846 // This query is not expected to return any values, but if it
Chris@499 847 // executes successfully (leaving no error in the error string)
Chris@499 848 // then we know we have RDF
Chris@499 849 SimpleSPARQLQuery q(SimpleSPARQLQuery::QueryFromSingleSource,
Chris@499 850 QString(" SELECT ?x FROM <%1> WHERE { ?x <y> <z> } ")
Chris@499 851 .arg(url));
Chris@499 852
Chris@499 853 SimpleSPARQLQuery::ResultList r = q.execute();
Chris@499 854 if (!q.isOK()) {
Chris@499 855 return NotRDF;
Chris@499 856 }
Chris@499 857
Chris@490 858 SimpleSPARQLQuery::Value value =
Chris@490 859 SimpleSPARQLQuery::singleResultQuery
Chris@490 860 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@490 861 QString
Chris@490 862 (" PREFIX mo: <http://purl.org/ontology/mo/> "
Chris@490 863 " SELECT ?url FROM <%1> "
Chris@490 864 " WHERE { ?signal a mo:Signal ; mo:available_as ?url } "
Chris@490 865 ).arg(url),
Chris@490 866 "url");
Chris@490 867
Chris@490 868 if (value.type == SimpleSPARQLQuery::URIValue) {
Chris@490 869 haveAudio = true;
Chris@490 870 }
Chris@490 871
Chris@490 872 value =
Chris@490 873 SimpleSPARQLQuery::singleResultQuery
Chris@490 874 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@490 875 QString
Chris@490 876 (" PREFIX event: <http://purl.org/NET/c4dm/event.owl#> "
Chris@490 877 " SELECT ?thing FROM <%1> "
Chris@490 878 " WHERE { ?thing event:time ?time } "
Chris@490 879 ).arg(url),
Chris@490 880 "thing");
Chris@490 881
Chris@490 882 if (value.type == SimpleSPARQLQuery::URIValue) {
Chris@490 883 haveAnnotations = true;
Chris@490 884 }
Chris@490 885
Chris@490 886 if (!haveAnnotations) {
Chris@490 887
Chris@490 888 value =
Chris@490 889 SimpleSPARQLQuery::singleResultQuery
Chris@490 890 (SimpleSPARQLQuery::QueryFromSingleSource,
Chris@490 891 QString
Chris@490 892 (" PREFIX af: <http://purl.org/ontology/af/> "
Chris@490 893 " SELECT ?thing FROM <%1> "
Chris@490 894 " WHERE { ?signal af:signal_feature ?thing } "
Chris@490 895 ).arg(url),
Chris@490 896 "thing");
Chris@490 897
Chris@490 898 if (value.type == SimpleSPARQLQuery::URIValue) {
Chris@490 899 haveAnnotations = true;
Chris@490 900 }
Chris@490 901 }
Chris@490 902
Chris@490 903 if (haveAudio) {
Chris@490 904 if (haveAnnotations) {
Chris@490 905 return AudioRefAndAnnotations;
Chris@490 906 } else {
Chris@490 907 return AudioRef;
Chris@490 908 }
Chris@490 909 } else {
Chris@490 910 if (haveAnnotations) {
Chris@490 911 return Annotations;
Chris@490 912 } else {
Chris@499 913 return OtherRDFDocument;
Chris@490 914 }
Chris@490 915 }
Chris@492 916
Chris@492 917 SimpleSPARQLQuery::closeSingleSource(url);
Chris@490 918 }
Chris@490 919
Chris@490 920