annotate rdf/SimpleSPARQLQuery.cpp @ 473:47b41ec34407

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