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 "SimpleSPARQLQuery.h"
|
Chris@439
|
17 #include "base/ProgressReporter.h"
|
Chris@439
|
18
|
Chris@440
|
19 #ifdef USE_NEW_RASQAL_API
|
Chris@440
|
20 #include <rasqal/rasqal.h>
|
Chris@440
|
21 #else
|
Chris@439
|
22 #include <rasqal.h>
|
Chris@440
|
23 #endif
|
Chris@439
|
24
|
Chris@439
|
25 #include <iostream>
|
Chris@439
|
26
|
Chris@439
|
27 using std::cerr;
|
Chris@439
|
28 using std::endl;
|
Chris@439
|
29
|
Chris@440
|
30 #ifdef USE_NEW_RASQAL_API
|
Chris@440
|
31 class WrasqalWorldWrapper // wrong but wromantic, etc
|
Chris@440
|
32 {
|
Chris@440
|
33 public:
|
Chris@440
|
34 WrasqalWorldWrapper() : m_world(rasqal_new_world()) { }
|
Chris@440
|
35 ~WrasqalWorldWrapper() { rasqal_free_world(m_world); }
|
Chris@440
|
36
|
Chris@440
|
37 rasqal_world *getWorld() const { return m_world; }
|
Chris@440
|
38
|
Chris@440
|
39 private:
|
Chris@440
|
40 rasqal_world *m_world;
|
Chris@440
|
41 };
|
Chris@440
|
42 #endif
|
Chris@440
|
43
|
Chris@439
|
44 class SimpleSPARQLQuery::Impl
|
Chris@439
|
45 {
|
Chris@439
|
46 public:
|
Chris@439
|
47 Impl(QString query);
|
Chris@439
|
48 ~Impl();
|
Chris@439
|
49
|
Chris@439
|
50 void setProgressReporter(ProgressReporter *reporter) { m_reporter = reporter; }
|
Chris@439
|
51 bool wasCancelled() const { return m_cancelled; }
|
Chris@439
|
52
|
Chris@439
|
53 ResultList execute();
|
Chris@439
|
54
|
Chris@439
|
55 bool isOK() const;
|
Chris@439
|
56 QString getErrorString() const;
|
Chris@439
|
57
|
Chris@439
|
58 protected:
|
Chris@439
|
59 static void errorHandler(void *, raptor_locator *, const char *);
|
Chris@439
|
60
|
Chris@440
|
61 #ifdef USE_NEW_RASQAL_API
|
Chris@440
|
62 static WrasqalWorldWrapper m_www;
|
Chris@440
|
63 #else
|
Chris@439
|
64 static bool m_initialised;
|
Chris@440
|
65 #endif
|
Chris@439
|
66
|
Chris@439
|
67 QString m_query;
|
Chris@439
|
68 QString m_errorString;
|
Chris@439
|
69 ProgressReporter *m_reporter;
|
Chris@439
|
70 bool m_cancelled;
|
Chris@439
|
71 };
|
Chris@439
|
72
|
Chris@440
|
73 #ifdef USE_NEW_RASQAL_API
|
Chris@440
|
74 WrasqalWorldWrapper
|
Chris@440
|
75 SimpleSPARQLQuery::Impl::m_www;
|
Chris@440
|
76 #else
|
Chris@440
|
77 bool
|
Chris@440
|
78 SimpleSPARQLQuery::Impl::m_initialised = false;
|
Chris@440
|
79 #endif
|
Chris@440
|
80
|
Chris@439
|
81 SimpleSPARQLQuery::SimpleSPARQLQuery(QString query) :
|
Chris@439
|
82 m_impl(new Impl(query)) { }
|
Chris@439
|
83
|
Chris@439
|
84 SimpleSPARQLQuery::~SimpleSPARQLQuery()
|
Chris@439
|
85 {
|
Chris@439
|
86 delete m_impl;
|
Chris@439
|
87 }
|
Chris@439
|
88
|
Chris@439
|
89 void
|
Chris@439
|
90 SimpleSPARQLQuery::setProgressReporter(ProgressReporter *reporter)
|
Chris@439
|
91 {
|
Chris@439
|
92 m_impl->setProgressReporter(reporter);
|
Chris@439
|
93 }
|
Chris@439
|
94
|
Chris@439
|
95 bool
|
Chris@439
|
96 SimpleSPARQLQuery::wasCancelled() const
|
Chris@439
|
97 {
|
Chris@439
|
98 return m_impl->wasCancelled();
|
Chris@439
|
99 }
|
Chris@439
|
100
|
Chris@439
|
101 SimpleSPARQLQuery::ResultList
|
Chris@439
|
102 SimpleSPARQLQuery::execute()
|
Chris@439
|
103 {
|
Chris@439
|
104 return m_impl->execute();
|
Chris@439
|
105 }
|
Chris@439
|
106
|
Chris@439
|
107 bool
|
Chris@439
|
108 SimpleSPARQLQuery::isOK() const
|
Chris@439
|
109 {
|
Chris@439
|
110 return m_impl->isOK();
|
Chris@439
|
111 }
|
Chris@439
|
112
|
Chris@439
|
113 QString
|
Chris@439
|
114 SimpleSPARQLQuery::getErrorString() const
|
Chris@439
|
115 {
|
Chris@439
|
116 return m_impl->getErrorString();
|
Chris@439
|
117 }
|
Chris@439
|
118
|
Chris@439
|
119 SimpleSPARQLQuery::Impl::Impl(QString query) :
|
Chris@439
|
120 m_query(query),
|
Chris@439
|
121 m_reporter(0),
|
Chris@439
|
122 m_cancelled(false)
|
Chris@439
|
123 {
|
Chris@439
|
124 }
|
Chris@439
|
125
|
Chris@439
|
126 SimpleSPARQLQuery::Impl::~Impl()
|
Chris@439
|
127 {
|
Chris@439
|
128 }
|
Chris@439
|
129
|
Chris@439
|
130 bool
|
Chris@439
|
131 SimpleSPARQLQuery::Impl::isOK() const
|
Chris@439
|
132 {
|
Chris@439
|
133 return (m_errorString == "");
|
Chris@439
|
134 }
|
Chris@439
|
135
|
Chris@439
|
136 QString
|
Chris@439
|
137 SimpleSPARQLQuery::Impl::getErrorString() const
|
Chris@439
|
138 {
|
Chris@439
|
139 return m_errorString;
|
Chris@439
|
140 }
|
Chris@439
|
141
|
Chris@439
|
142 void
|
Chris@439
|
143 SimpleSPARQLQuery::Impl::errorHandler(void *data,
|
Chris@439
|
144 raptor_locator *locator,
|
Chris@439
|
145 const char *message)
|
Chris@439
|
146 {
|
Chris@439
|
147 SimpleSPARQLQuery::Impl *impl = (SimpleSPARQLQuery::Impl *)data;
|
Chris@439
|
148
|
Chris@439
|
149 // char buffer[256];
|
Chris@439
|
150 // raptor_format_locator(buffer, 255, locator);
|
Chris@439
|
151 // impl->m_errorString = QString("%1 - %2").arg(buffer).arg(message);
|
Chris@439
|
152
|
Chris@439
|
153 impl->m_errorString = message;
|
Chris@439
|
154
|
Chris@439
|
155 cerr << "SimpleSPARQLQuery: ERROR: " << impl->m_errorString.toStdString() << endl;
|
Chris@439
|
156 }
|
Chris@439
|
157
|
Chris@439
|
158 SimpleSPARQLQuery::ResultList
|
Chris@439
|
159 SimpleSPARQLQuery::Impl::execute()
|
Chris@439
|
160 {
|
Chris@439
|
161 ResultList list;
|
Chris@439
|
162
|
Chris@440
|
163 #ifdef USE_NEW_RASQAL_API
|
Chris@440
|
164 rasqal_query *query = rasqal_new_query(m_www.getWorld(), "sparql", NULL);
|
Chris@440
|
165 #else
|
Chris@440
|
166 if (!m_initialised) {
|
Chris@440
|
167 m_initialised = true;
|
Chris@440
|
168 rasqal_init();
|
Chris@440
|
169 }
|
Chris@439
|
170 rasqal_query *query = rasqal_new_query("sparql", NULL);
|
Chris@440
|
171 #endif
|
Chris@439
|
172 if (!query) {
|
Chris@439
|
173 m_errorString = "Failed to construct query";
|
Chris@439
|
174 cerr << "SimpleSPARQLQuery: ERROR: " << m_errorString.toStdString() << endl;
|
Chris@439
|
175 return list;
|
Chris@439
|
176 }
|
Chris@439
|
177
|
Chris@439
|
178 rasqal_query_set_error_handler(query, this, errorHandler);
|
Chris@439
|
179 rasqal_query_set_fatal_error_handler(query, this, errorHandler);
|
Chris@439
|
180
|
Chris@439
|
181 if (rasqal_query_prepare
|
Chris@439
|
182 (query, (const unsigned char *)m_query.toUtf8().data(), NULL)) {
|
Chris@439
|
183 cerr << "SimpleSPARQLQuery: Failed to prepare query" << endl;
|
Chris@439
|
184 rasqal_free_query(query);
|
Chris@439
|
185 return list;
|
Chris@439
|
186 }
|
Chris@439
|
187
|
Chris@439
|
188 rasqal_query_results *results = rasqal_query_execute(query);
|
Chris@439
|
189
|
Chris@439
|
190 // cerr << "Query executed" << endl;
|
Chris@439
|
191
|
Chris@439
|
192 if (!results) {
|
Chris@439
|
193 cerr << "SimpleSPARQLQuery: RASQAL query failed" << endl;
|
Chris@439
|
194 rasqal_free_query(query);
|
Chris@439
|
195 return list;
|
Chris@439
|
196 }
|
Chris@439
|
197
|
Chris@439
|
198 if (!rasqal_query_results_is_bindings(results)) {
|
Chris@439
|
199 cerr << "SimpleSPARQLQuery: RASQAL query has wrong result type (not bindings)" << endl;
|
Chris@439
|
200 rasqal_free_query_results(results);
|
Chris@439
|
201 rasqal_free_query(query);
|
Chris@439
|
202 return list;
|
Chris@439
|
203 }
|
Chris@439
|
204
|
Chris@439
|
205 int resultCount = 0;
|
Chris@439
|
206 int resultTotal = rasqal_query_results_get_count(results); // probably wrong
|
Chris@439
|
207 m_cancelled = false;
|
Chris@439
|
208
|
Chris@439
|
209 while (!rasqal_query_results_finished(results)) {
|
Chris@439
|
210
|
Chris@439
|
211 int count = rasqal_query_results_get_bindings_count(results);
|
Chris@439
|
212
|
Chris@439
|
213 KeyValueMap resultmap;
|
Chris@439
|
214
|
Chris@439
|
215 for (int i = 0; i < count; ++i) {
|
Chris@439
|
216
|
Chris@439
|
217 const unsigned char *name =
|
Chris@439
|
218 rasqal_query_results_get_binding_name(results, i);
|
Chris@439
|
219
|
Chris@439
|
220 rasqal_literal *literal =
|
Chris@439
|
221 rasqal_query_results_get_binding_value(results, i);
|
Chris@439
|
222
|
Chris@439
|
223 QString key = (const char *)name;
|
Chris@439
|
224
|
Chris@439
|
225 if (!literal) {
|
Chris@439
|
226 resultmap[key] = Value();
|
Chris@439
|
227 continue;
|
Chris@439
|
228 }
|
Chris@439
|
229
|
Chris@439
|
230 ValueType type = LiteralValue;
|
Chris@439
|
231 if (literal->type == RASQAL_LITERAL_URI) type = URIValue;
|
Chris@439
|
232 else if (literal->type == RASQAL_LITERAL_BLANK) type = BlankValue;
|
Chris@439
|
233
|
Chris@439
|
234 QString text = (const char *)rasqal_literal_as_string(literal);
|
Chris@439
|
235
|
Chris@439
|
236 resultmap[key] = Value(type, text);
|
Chris@439
|
237 }
|
Chris@439
|
238
|
Chris@439
|
239 list.push_back(resultmap);
|
Chris@439
|
240
|
Chris@439
|
241 rasqal_query_results_next(results);
|
Chris@439
|
242
|
Chris@439
|
243 resultCount++;
|
Chris@439
|
244
|
Chris@439
|
245 if (m_reporter) {
|
Chris@439
|
246 if (resultCount >= resultTotal) {
|
Chris@439
|
247 if (m_reporter->isDefinite()) m_reporter->setDefinite(false);
|
Chris@439
|
248 m_reporter->setProgress(resultCount);
|
Chris@439
|
249 } else {
|
Chris@439
|
250 m_reporter->setProgress((resultCount * 100) / resultTotal);
|
Chris@439
|
251 }
|
Chris@439
|
252
|
Chris@439
|
253 if (m_reporter->wasCancelled()) {
|
Chris@439
|
254 m_cancelled = true;
|
Chris@439
|
255 break;
|
Chris@439
|
256 }
|
Chris@439
|
257 }
|
Chris@439
|
258 }
|
Chris@439
|
259
|
Chris@439
|
260 rasqal_free_query_results(results);
|
Chris@439
|
261 rasqal_free_query(query);
|
Chris@439
|
262
|
Chris@439
|
263 return list;
|
Chris@439
|
264 }
|
Chris@440
|
265
|
Chris@440
|
266 SimpleSPARQLQuery::Value
|
Chris@440
|
267 SimpleSPARQLQuery::singleResultQuery(QString query, QString binding)
|
Chris@440
|
268 {
|
Chris@440
|
269 SimpleSPARQLQuery q(query);
|
Chris@440
|
270 ResultList results = q.execute();
|
Chris@440
|
271 if (!q.isOK()) {
|
Chris@440
|
272 cerr << "SimpleSPARQLQuery::singleResultQuery: ERROR: "
|
Chris@440
|
273 << q.getErrorString().toStdString() << endl;
|
Chris@440
|
274 return Value();
|
Chris@440
|
275 }
|
Chris@440
|
276 if (results.empty()) {
|
Chris@440
|
277 return Value();
|
Chris@440
|
278 }
|
Chris@440
|
279 for (int i = 0; i < results.size(); ++i) {
|
Chris@440
|
280 if (results[i].find(binding) != results[i].end() &&
|
Chris@440
|
281 results[i][binding].type != NoValue) {
|
Chris@440
|
282 return results[i][binding];
|
Chris@440
|
283 }
|
Chris@440
|
284 }
|
Chris@440
|
285 return Value();
|
Chris@440
|
286 }
|
Chris@440
|
287
|
Chris@440
|
288
|
Chris@440
|
289
|