comparison 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
comparison
equal deleted inserted replaced
726:1bfb40549003 727:211efc770335
2 2
3 /* 3 /*
4 Sonic Visualiser 4 Sonic Visualiser
5 An audio file viewer and annotation editor. 5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London. 6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2008 QMUL. 7 This file copyright 2008-2012 QMUL.
8 8
9 This program is free software; you can redistribute it and/or 9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as 10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the 11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file 12 License, or (at your option) any later version. See the file
22 #include <QUrl> 22 #include <QUrl>
23 23
24 #include <iostream> 24 #include <iostream>
25 #include <cmath> 25 #include <cmath>
26 26
27 #include "SimpleSPARQLQuery.h"
28 #include "PluginRDFIndexer.h" 27 #include "PluginRDFIndexer.h"
29 #include "PluginRDFDescription.h" 28 #include "PluginRDFDescription.h"
30 #include "base/ProgressReporter.h" 29 #include "base/ProgressReporter.h"
31 #include "plugin/PluginIdentifier.h" 30 #include "plugin/PluginIdentifier.h"
32 31
33 #include "transform/TransformFactory.h" 32 #include "transform/TransformFactory.h"
34 33
34 #include <dataquay/BasicStore.h>
35 #include <dataquay/PropertyObject.h>
36
35 using std::cerr; 37 using std::cerr;
36 using std::endl; 38 using std::endl;
37 39
38 typedef const unsigned char *STR; // redland's expected string type 40 using Dataquay::Uri;
41 using Dataquay::Node;
42 using Dataquay::Nodes;
43 using Dataquay::Triple;
44 using Dataquay::Triples;
45 using Dataquay::BasicStore;
46 using Dataquay::PropertyObject;
39 47
40 48
41 class RDFTransformFactoryImpl 49 class RDFTransformFactoryImpl
42 { 50 {
43 public: 51 public:
51 std::vector<Transform> getTransforms(ProgressReporter *); 59 std::vector<Transform> getTransforms(ProgressReporter *);
52 60
53 static QString writeTransformToRDF(const Transform &, QString); 61 static QString writeTransformToRDF(const Transform &, QString);
54 62
55 protected: 63 protected:
64 BasicStore *m_store;
56 QString m_urlString; 65 QString m_urlString;
57 QString m_errorString; 66 QString m_errorString;
58 bool m_isRDF; 67 bool m_isRDF;
59 bool setOutput(Transform &, QString); 68 bool setOutput(Transform &, QString);
60 bool setParameters(Transform &, QString); 69 bool setParameters(Transform &, QString);
106 { 115 {
107 return RDFTransformFactoryImpl::writeTransformToRDF(t, f); 116 return RDFTransformFactoryImpl::writeTransformToRDF(t, f);
108 } 117 }
109 118
110 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) : 119 RDFTransformFactoryImpl::RDFTransformFactoryImpl(QString url) :
120 m_store(new BasicStore),
111 m_urlString(url), 121 m_urlString(url),
112 m_isRDF(false) 122 m_isRDF(false)
113 { 123 {
124 //!!! retrieve data if remote... then
125 m_store->addPrefix("vamp", Uri("http://purl.org/ontology/vamp/"));
126 try {
127 m_store->import(QUrl::fromLocalFile(url), BasicStore::ImportIgnoreDuplicates);
128 m_isRDF = true;
129 } catch (...) { }
114 } 130 }
115 131
116 RDFTransformFactoryImpl::~RDFTransformFactoryImpl() 132 RDFTransformFactoryImpl::~RDFTransformFactoryImpl()
117 { 133 {
118 SimpleSPARQLQuery::closeSingleSource(m_urlString); 134 delete m_store;
119 } 135 }
120 136
121 bool 137 bool
122 RDFTransformFactoryImpl::isRDF() 138 RDFTransformFactoryImpl::isRDF()
123 { 139 {
141 { 157 {
142 std::vector<Transform> transforms; 158 std::vector<Transform> transforms;
143 159
144 std::map<QString, Transform> uriTransformMap; 160 std::map<QString, Transform> uriTransformMap;
145 161
146 QString query = 162 Nodes tnodes = m_store->match
147 " PREFIX vamp: <http://purl.org/ontology/vamp/> " 163 (Triple(Node(), "a", m_store->expand("vamp:Transform"))).a();
148 164
149 " SELECT ?transform ?plugin " 165 foreach (Node tnode, tnodes) {
150 166
151 " FROM <%2> " 167 Node pnode = m_store->matchFirst
152 168 (Triple(tnode, "vamp:plugin", Node())).c;
153 " WHERE { " 169
154 " ?transform a vamp:Transform ; " 170 // There are various queries we need to make that might
155 " vamp:plugin ?plugin . " 171 // include data from either the transform RDF or the model
156 " } "; 172 // accumulated from plugin descriptions. For example, the
157 173 // transform RDF may specify the output's true URI, or it
158 SimpleSPARQLQuery transformsQuery 174 // might have a blank node or some other URI with the
159 (SimpleSPARQLQuery::QueryFromSingleSource, query.arg(m_urlString)); 175 // appropriate vamp:identifier included in the file. To cover
160 176 // both cases, we need to add the file itself into the model
161 SimpleSPARQLQuery::ResultList transformResults = transformsQuery.execute(); 177 // and always query the model using the transform URI rather
162 178 // than querying the file itself subsequently.
163 if (!transformsQuery.isOK()) { 179
164 m_errorString = transformsQuery.getErrorString(); 180 //!!! ^^^ what does this mean for us with Dataquay? do we need to cross-check outputs against the indexer?
165 return transforms;
166 }
167
168 m_isRDF = true;
169
170 if (transformResults.empty()) {
171 SVDEBUG << "RDFTransformFactory: NOTE: No RDF/TTL transform descriptions found in document at <" << m_urlString << ">" << endl;
172 return transforms;
173 }
174
175 // There are various queries we need to make that might include
176 // data from either the transform RDF or the model accumulated
177 // from plugin descriptions. For example, the transform RDF may
178 // specify the output's true URI, or it might have a blank node or
179 // some other URI with the appropriate vamp:identifier included in
180 // the file. To cover both cases, we need to add the file itself
181 // into the model and always query the model using the transform
182 // URI rather than querying the file itself subsequently.
183 181
184 SimpleSPARQLQuery::addSourceToModel(m_urlString); 182 SimpleSPARQLQuery::addSourceToModel(m_urlString);
185 183
186 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance(); 184 PluginRDFIndexer *indexer = PluginRDFIndexer::getInstance();
187 185