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