comparison rdf/SimpleSPARQLQuery.cpp @ 490:c3fb8258e34d

* Make it possible to import an entire session from an RDF document. However, at the moment the timings of events appear to be constrained by how far the audio decoder has got through its audio file at the time the event is queried -- need to investigate.
author Chris Cannam
date Fri, 21 Nov 2008 18:03:14 +0000
parents 82ab61fa9223
children 23945cdd7161
comparison
equal deleted inserted replaced
489:82ab61fa9223 490:c3fb8258e34d
29 #include <rasqal.h> 29 #include <rasqal.h>
30 #endif 30 #endif
31 31
32 #include <redland.h> 32 #include <redland.h>
33 33
34 //#define DEBUG_SIMPLE_SPARQL_QUERY 1 34 #define DEBUG_SIMPLE_SPARQL_QUERY 1
35 35
36 #include <iostream> 36 #include <iostream>
37 37
38 using std::cerr; 38 using std::cerr;
39 using std::endl; 39 using std::endl;
113 113
114 bool isOK() const { return (m_model != 0); } 114 bool isOK() const { return (m_model != 0); }
115 115
116 librdf_uri *getUri(QString uriString, QString &errorString) 116 librdf_uri *getUri(QString uriString, QString &errorString)
117 { 117 {
118 QMutexLocker locker(&m_mutex);
119
120 if (m_parsedUris.find(uriString) != m_parsedUris.end()) { 118 if (m_parsedUris.find(uriString) != m_parsedUris.end()) {
121 return m_parsedUris[uriString]; 119 return m_parsedUris[uriString];
122 } 120 }
123 121
124 librdf_uri *uri = librdf_new_uri 122 librdf_uri *uri = librdf_new_uri
141 if (librdf_parser_parse_into_model(parser, uri, NULL, m_model)) { 139 if (librdf_parser_parse_into_model(parser, uri, NULL, m_model)) {
142 140
143 errorString = QString("Failed to parse RDF from URI \"%1\"") 141 errorString = QString("Failed to parse RDF from URI \"%1\"")
144 .arg(uriString); 142 .arg(uriString);
145 librdf_free_parser(parser); 143 librdf_free_parser(parser);
146 librdf_free_uri(uri); 144 // librdf_free_uri(uri);
147 return 0; 145 return 0;
148 146
149 } else { 147 } else {
150 148
151 librdf_free_parser(parser); 149 librdf_free_parser(parser);
313 SimpleSPARQLQuery::ResultList 311 SimpleSPARQLQuery::ResultList
314 SimpleSPARQLQuery::Impl::execute() 312 SimpleSPARQLQuery::Impl::execute()
315 { 313 {
316 ResultList list; 314 ResultList list;
317 315
318 m_mutex.lock(); 316 QMutexLocker locker(&m_mutex);
319 317
320 if (m_type == QueryFromModel) { 318 if (m_type == QueryFromModel) {
321 if (!m_redland) { 319 if (!m_redland) {
322 // There can be no results, because no sources have been 320 // There can be no results, because no sources have been
323 // added to the model yet (m_redland is only created when 321 // added to the model yet (m_redland is only created when
324 // addSourceToModel is called) 322 // addSourceToModel is called)
325 cerr << "SimpleSPARQLQuery::execute: NOTE: No sources have been added to data model yet, so no results are possible" << endl; 323 cerr << "SimpleSPARQLQuery::execute: NOTE: No sources have been added to data model yet, so no results are possible" << endl;
326 m_mutex.unlock();
327 return list; 324 return list;
328 } 325 }
329 } 326 }
330 327
331 if (m_type == QueryFromSingleSource) { 328 if (m_type == QueryFromSingleSource) {
334 m_rasqal = new WrasqalWorldWrapper(); 331 m_rasqal = new WrasqalWorldWrapper();
335 if (!m_rasqal->isOK()) { 332 if (!m_rasqal->isOK()) {
336 cerr << "ERROR: SimpleSPARQLQuery::execute: Failed to initialise Rasqal query engine" << endl; 333 cerr << "ERROR: SimpleSPARQLQuery::execute: Failed to initialise Rasqal query engine" << endl;
337 delete m_rasqal; 334 delete m_rasqal;
338 m_rasqal = 0; 335 m_rasqal = 0;
339 m_mutex.unlock();
340 return list; 336 return list;
341 } 337 }
342 } 338 }
343 #else 339 #else
344 if (!m_rasqalInitialised) { 340 if (!m_rasqalInitialised) {
345 rasqal_init(); 341 rasqal_init();
346 m_rasqalInitialised = true; 342 m_rasqalInitialised = true;
347 } 343 }
348 #endif 344 #endif
349 } 345 }
350
351 m_mutex.unlock();
352 346
353 if (m_type == QueryFromSingleSource) { 347 if (m_type == QueryFromSingleSource) {
354 return executeDirectParser(); 348 return executeDirectParser();
355 } else { 349 } else {
356 return executeDatastore(); 350 return executeDatastore();
424 for (int i = 0; i < count; ++i) { 418 for (int i = 0; i < count; ++i) {
425 419
426 const unsigned char *name = 420 const unsigned char *name =
427 rasqal_query_results_get_binding_name(results, i); 421 rasqal_query_results_get_binding_name(results, i);
428 422
423 if (!name) {
424 std::cerr << "WARNING: Result " << i << " of query has no name" << std::endl;
425 continue;
426 }
427
429 rasqal_literal *literal = 428 rasqal_literal *literal =
430 rasqal_query_results_get_binding_value(results, i); 429 rasqal_query_results_get_binding_value(results, i);
431 430
432 QString key = (const char *)name; 431 QString key = (const char *)name;
433 432
435 resultmap[key] = Value(); 434 resultmap[key] = Value();
436 continue; 435 continue;
437 } 436 }
438 437
439 ValueType type = LiteralValue; 438 ValueType type = LiteralValue;
440 if (literal->type == RASQAL_LITERAL_URI) type = URIValue; 439 if (literal->type == RASQAL_LITERAL_BLANK) type = BlankValue;
441 else if (literal->type == RASQAL_LITERAL_BLANK) type = BlankValue; 440 else if (literal->type == RASQAL_LITERAL_URI) type = URIValue;
442 441
443 QString text = (const char *)rasqal_literal_as_string(literal); 442 QString text;
443 const char *lit = (const char *)rasqal_literal_as_string(literal);
444 if (!lit) {
445 std::cerr << "WARNING: Result " << i << " of query has null value" << std::endl;
446 } else {
447 text = lit;
448 }
444 449
445 #ifdef DEBUG_SIMPLE_SPARQL_QUERY 450 #ifdef DEBUG_SIMPLE_SPARQL_QUERY
446 std::cerr << i << ". " << key.toStdString() << " -> " << text.toStdString() << " (type " << type << ")" << std::endl; 451 std::cerr << i << ". " << key.toStdString() << " -> " << text.toStdString() << " (type " << type << ")" << std::endl;
447 #endif 452 #endif
448 453
537 for (int i = 0; i < count; ++i) { 542 for (int i = 0; i < count; ++i) {
538 543
539 const char *name = 544 const char *name =
540 librdf_query_results_get_binding_name(results, i); 545 librdf_query_results_get_binding_name(results, i);
541 546
547 if (!name) {
548 std::cerr << "WARNING: Result " << i << " of query has no name" << std::endl;
549 continue;
550 }
551
542 librdf_node *node = 552 librdf_node *node =
543 librdf_query_results_get_binding_value(results, i); 553 librdf_query_results_get_binding_value(results, i);
544 554
545 QString key = (const char *)name; 555 QString key = (const char *)name;
546 556
554 564
555 if (librdf_node_is_resource(node)) { 565 if (librdf_node_is_resource(node)) {
556 566
557 type = URIValue; 567 type = URIValue;
558 librdf_uri *uri = librdf_node_get_uri(node); 568 librdf_uri *uri = librdf_node_get_uri(node);
559 text = (const char *)librdf_uri_as_string(uri); 569 const char *us = (const char *)librdf_uri_as_string(uri);
570
571 if (!us) {
572 std::cerr << "WARNING: Result " << i << " of query claims URI type, but has null URI" << std::endl;
573 } else {
574 text = us;
575 }
560 576
561 } else if (librdf_node_is_literal(node)) { 577 } else if (librdf_node_is_literal(node)) {
562 578
563 type = LiteralValue; 579 type = LiteralValue;
564 text = (const char *)librdf_node_get_literal_value(node); 580
581 const char *lit = (const char *)librdf_node_get_literal_value(node);
582 if (!lit) {
583 std::cerr << "WARNING: Result " << i << " of query claims literal type, but has no literal" << std::endl;
584 } else {
585 text = lit;
586 }
565 587
566 } else if (librdf_node_is_blank(node)) { 588 } else if (librdf_node_is_blank(node)) {
567 589
568 type = BlankValue; 590 type = BlankValue;
569 591
615 bool 637 bool
616 SimpleSPARQLQuery::Impl::addSourceToModel(QString sourceUri) 638 SimpleSPARQLQuery::Impl::addSourceToModel(QString sourceUri)
617 { 639 {
618 QString err; 640 QString err;
619 641
620 m_mutex.lock(); 642 QMutexLocker locker(&m_mutex);
621 643
622 if (!m_redland) { 644 if (!m_redland) {
623 m_redland = new WredlandWorldWrapper(); 645 m_redland = new WredlandWorldWrapper();
624 if (!m_redland->isOK()) { 646 if (!m_redland->isOK()) {
625 cerr << "ERROR: SimpleSPARQLQuery::addSourceToModel: Failed to initialise Redland datastore" << endl; 647 cerr << "ERROR: SimpleSPARQLQuery::addSourceToModel: Failed to initialise Redland datastore" << endl;
626 delete m_redland; 648 delete m_redland;
627 m_redland = 0; 649 m_redland = 0;
628 m_mutex.unlock();
629 return false; 650 return false;
630 } 651 }
631 } 652 }
632
633 m_mutex.unlock();
634 653
635 librdf_uri *uri = m_redland->getUri(sourceUri, err); 654 librdf_uri *uri = m_redland->getUri(sourceUri, err);
636 655
637 if (!uri) { 656 if (!uri) {
638 std::cerr << "SimpleSPARQLQuery::addSourceToModel: Failed to add source URI \"" << sourceUri.toStdString() << ": " << err.toStdString() << std::endl; 657 std::cerr << "SimpleSPARQLQuery::addSourceToModel: Failed to add source URI \"" << sourceUri.toStdString() << ": " << err.toStdString() << std::endl;