27 typedef std::map<std::string, std::list<RdfNode> >
ResultMap;
28 typedef std::map<std::string, RdfUri>
PrefixMap;
33 void SetBaseUri(RdfUri uri);
34 RdfUri GetBaseUri()
const;
36 void AddPrefix(std::string prefix, RdfUri uri);
37 PrefixMap GetPrefixes()
const;
41 bool Add(RdfTriple triple);
42 bool Remove(RdfTriple triple);
43 bool Contains(RdfTriple triple);
45 ResultMap ExecuteTupleQuery(
const std::string& query)
const;
46 bool ExecuteBooleanQuery(
const std::string& query)
const;
48 void Save(std::string
filename, std::string format =
"");
49 void Import(std::string url, std::string format =
"");
55 librdf_world* m_World;
56 librdf_storage* m_Storage;
57 librdf_model* m_Model;
59 librdf_statement* RdfTripleToStatement(RdfTriple triple)
const;
60 librdf_node* RdfNodeToLibRdfNode(RdfNode node)
const;
61 librdf_uri* RdfUriToLibRdfUri(RdfUri uri)
const;
63 RdfTriple StatementToRdfTriple(librdf_statement* statement)
const;
64 RdfNode LibRdfNodeToRdfNode(librdf_node* node)
const;
65 RdfUri LibRdfUriToRdfUri(librdf_uri* uri)
const;
67 bool CheckComplete(librdf_statement* statement);
77 std::string PrependPrefixes(
const std::string& query)
const;
85 RdfStorePrivate::RdfStorePrivate()
86 : m_World(0), m_Storage(0), m_Model(0)
89 m_Prefixes[
"rdf"] = RdfUri(
"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
90 m_Prefixes[
"rdfs"] = RdfUri(
"http://www.w3.org/2000/01/rdf-schema#");
91 m_Prefixes[
"xsd"] = RdfUri(
"http://www.w3.org/2001/XMLSchema#");
92 m_Prefixes[
"owl"] = RdfUri(
"http://www.w3.org/2002/07/owl#");
98 RdfStorePrivate::~RdfStorePrivate()
101 librdf_free_model(m_Model);
103 librdf_free_storage(m_Storage);
105 librdf_free_world(m_World);
108 void RdfStorePrivate::SetBaseUri(RdfUri uri)
111 m_Prefixes[
""] = RdfUri(m_BaseUri.ToString());
114 RdfUri RdfStorePrivate::GetBaseUri()
const
119 void RdfStorePrivate::AddPrefix(std::string prefix, RdfUri uri)
121 m_Prefixes[prefix] = uri;
129 void RdfStorePrivate::CleanUp()
133 librdf_free_model(m_Model);
135 librdf_free_storage(m_Storage);
137 librdf_free_world(m_World);
140 m_World = librdf_new_world();
141 librdf_world_open(m_World);
143 m_Storage = librdf_new_storage(m_World,
"memory", 0, 0);
150 m_Model = librdf_new_model(m_World, m_Storage, 0);
158 bool RdfStorePrivate::Add(RdfTriple triple)
160 librdf_statement* statement = RdfTripleToStatement(triple);
162 if (!CheckComplete(statement))
164 librdf_free_statement(statement);
169 if (Contains(triple))
return true;
171 if (librdf_model_add_statement(m_Model, statement) != 0) {
172 librdf_free_statement(statement);
177 librdf_free_statement(statement);
182 bool RdfStorePrivate::Remove(RdfTriple triple)
184 librdf_statement* statement = RdfTripleToStatement(triple);
186 if (!CheckComplete(statement))
188 librdf_free_statement(statement);
193 if (!Contains(triple))
return true;
195 if (librdf_model_remove_statement(m_Model, statement) != 0) {
196 librdf_free_statement(statement);
201 librdf_free_statement(statement);
207 bool RdfStorePrivate::Contains(RdfTriple triple)
209 librdf_statement* statement = RdfTripleToStatement(triple);
212 if (librdf_model_contains_statement(m_Model, statement) == 0) {
213 librdf_free_statement(statement);
218 librdf_free_statement(statement);
228 const std::string completeQuery = this->PrependPrefixes(query);
230 librdf_query* rdfQuery = librdf_new_query(m_World,
"sparql", 0, (
const unsigned char*) completeQuery.c_str(), 0);
232 if (!rdfQuery)
return resultMap;
234 librdf_query_results* results = librdf_query_execute(rdfQuery, m_Model);
238 librdf_free_query(rdfQuery);
242 if (!librdf_query_results_is_bindings(results))
244 librdf_free_query_results(results);
245 librdf_free_query(rdfQuery);
249 while (!librdf_query_results_finished(results))
251 int count = librdf_query_results_get_bindings_count(results);
253 for (
int i = 0; i < count; ++i)
255 const char *name = librdf_query_results_get_binding_name(results, i);
259 std::string key = name;
261 std::list<RdfNode> list;
263 if(!resultMap[key].empty())
265 list = resultMap[key];
268 librdf_node *node = librdf_query_results_get_binding_value(results, i);
270 list.push_back(LibRdfNodeToRdfNode(node));
272 resultMap[key] = list;
275 librdf_query_results_next(results);
278 librdf_free_query_results(results);
279 librdf_free_query(rdfQuery);
284 bool RdfStorePrivate::ExecuteBooleanQuery(
const std::string& query)
const
286 const std::string completeQuery = this->PrependPrefixes(query);
288 librdf_query* rdfQuery = librdf_new_query(m_World,
"sparql", 0, (
const unsigned char*) completeQuery.c_str(), 0);
292 mitkThrow() <<
"failed to create query object";
295 librdf_query_results* results = librdf_query_execute(rdfQuery, m_Model);
299 librdf_free_query(rdfQuery);
303 if (!librdf_query_results_is_boolean(results))
305 librdf_free_query_results(results);
306 librdf_free_query(rdfQuery);
308 mitkThrow() <<
"unexpected result type error: tried to request a boolean result with a non-boolean query";
311 int rawResult = librdf_query_results_get_boolean(results);
317 booleanResult =
true;
319 else if (rawResult == 0)
321 booleanResult =
false;
325 mitkThrow() <<
"error while retrieving result";
328 librdf_free_query_results(results);
329 librdf_free_query(rdfQuery);
331 return booleanResult;
334 void RdfStorePrivate::Save(std::string
filename, std::string format)
336 if (format ==
"") format =
"turtle";
338 librdf_uri* baseUri = RdfUriToLibRdfUri(m_BaseUri);
340 librdf_serializer* s = librdf_new_serializer(m_World, format.c_str(), 0, 0);
347 for (PrefixMap::const_iterator i = m_Prefixes.begin(); i != m_Prefixes.end(); i++)
349 librdf_serializer_set_namespace(s, RdfUriToLibRdfUri(i->second), i->first.c_str());
352 FILE* f = fopen(filename.c_str(),
"w+");
354 librdf_serializer_serialize_model_to_file_handle(s, f, baseUri, m_Model);
356 librdf_free_serializer(s);
357 librdf_free_uri(baseUri);
361 void RdfStorePrivate::Import(std::string url, std::string format)
363 std::string baseUri = m_BaseUri.ToString();
368 SetBaseUri(RdfUri(baseUri));
371 if (format ==
"") format=
"turtle";
374 librdf_uri* uri = librdf_new_uri(m_World, (
const unsigned char*) url.c_str());
376 librdf_uri* libRdfBaseUri = librdf_new_uri(m_World, (
const unsigned char*) baseUri.c_str());
378 librdf_parser* p = librdf_new_parser(m_World, format.c_str(), 0, 0);
385 if (librdf_parser_parse_into_model(p, uri, libRdfBaseUri, m_Model) != 0 )
387 librdf_free_parser(p);
392 int namespaces = librdf_parser_get_namespaces_seen_count(p);
394 for (
int i = 0; i < namespaces; i++) {
395 const char* prefixChar = librdf_parser_get_namespaces_seen_prefix(p, i);
397 if ( !prefixChar )
continue;
399 std::string prefix = prefixChar;
400 RdfUri uri = LibRdfUriToRdfUri(librdf_parser_get_namespaces_seen_uri(p, i));
402 if (uri == RdfUri())
return;
404 RdfStorePrivate::PrefixMap::iterator it = m_Prefixes.find(prefix);
407 if (it == m_Prefixes.end()) {
408 AddPrefix(prefix, uri);
411 librdf_free_parser(p);
414 bool RdfStorePrivate::CheckComplete(librdf_statement* statement)
416 if (librdf_statement_is_complete(statement) != 0)
return true;
420 librdf_statement* RdfStorePrivate::RdfTripleToStatement(RdfTriple triple)
const
422 librdf_node* subject = RdfNodeToLibRdfNode(triple.GetTripleSubject());
423 librdf_node* predicate = RdfNodeToLibRdfNode(triple.GetTriplePredicate());
424 librdf_node*
object = RdfNodeToLibRdfNode(triple.GetTripleObject());
426 librdf_statement* statement = librdf_new_statement_from_nodes(m_World, subject, predicate,
object);
427 if(!statement)
return 0;
431 librdf_node* RdfStorePrivate::RdfNodeToLibRdfNode(RdfNode node)
const
433 librdf_node* newNode = 0;
435 switch (node.GetType())
440 newNode = librdf_new_node_from_blank_identifier(m_World, (
const unsigned char*) node.GetValue().c_str());
444 if (node.GetDatatype() != RdfUri())
446 librdf_uri* typeUri = RdfUriToLibRdfUri(node.GetDatatype());
447 newNode = librdf_new_node_from_typed_literal(m_World, (
const unsigned char*) node.GetValue().c_str(), 0, typeUri);
451 newNode = librdf_new_node_from_literal(m_World, (
const unsigned char*) node.GetValue().c_str(), 0, 0);
456 newNode = librdf_new_node_from_uri( m_World, librdf_new_uri(m_World, (
const unsigned char*) node.GetValue().c_str()) );
464 librdf_uri* RdfStorePrivate::RdfUriToLibRdfUri(RdfUri uri)
const
466 librdf_uri* libUri = librdf_new_uri(m_World, (
const unsigned char*) uri.ToString().c_str());
467 if (!libUri)
return 0;
471 RdfTriple RdfStorePrivate::StatementToRdfTriple(librdf_statement* statement)
const
473 librdf_node *subject = librdf_statement_get_subject(statement);
474 librdf_node *predicate = librdf_statement_get_predicate(statement);
475 librdf_node *
object = librdf_statement_get_object(statement);
477 RdfTriple triple(LibRdfNodeToRdfNode(subject),
478 LibRdfNodeToRdfNode(predicate),
479 LibRdfNodeToRdfNode(
object));
484 RdfNode RdfStorePrivate::LibRdfNodeToRdfNode(librdf_node* node)
const
488 if (!node)
return mitkNode;
490 if (librdf_node_is_resource(node))
493 librdf_uri *uri = librdf_node_get_uri(node);
494 mitkNode.SetValue(LibRdfUriToRdfUri(uri).ToString());
496 else if (librdf_node_is_literal(node))
499 std::string value = (
const char*) librdf_node_get_literal_value(node);
500 if (!value.empty()) mitkNode.SetValue(value);
501 librdf_uri* typeUri = librdf_node_get_literal_value_datatype_uri(node);
502 if (typeUri) mitkNode.SetDatatype(LibRdfUriToRdfUri(typeUri));
504 else if (librdf_node_is_blank(node))
507 std::string str = (
const char*) librdf_node_get_blank_identifier(node);
508 if (!str.empty()) mitkNode.SetValue(str);
513 RdfUri RdfStorePrivate::LibRdfUriToRdfUri(librdf_uri* uri)
const
515 std::string str = (
const char*) librdf_uri_as_string(uri);
516 if (!str.empty())
return RdfUri(str);
521 std::string RdfStorePrivate::PrependPrefixes(
const std::string& query)
const
523 std::string completeQuery;
525 for (PrefixMap::const_iterator i = m_Prefixes.begin(); i != m_Prefixes.end(); i++)
527 completeQuery +=
"PREFIX " + i->first +
": " +
"<" + i->second.ToString() +
"> ";
530 return completeQuery += query;
537 typedef std::map<std::string, std::list<RdfNode> >
ResultMap;
541 :d(new RdfStorePrivate)
557 return d->GetBaseUri();
562 d->AddPrefix(prefix, uri);
567 return d->GetPrefixes();
577 return d->Add(triple);
582 return d->Remove(triple);
587 return d->Contains(triple);
592 return d->ExecuteTupleQuery(query);
597 return d->ExecuteTupleQuery(query);
602 return d->ExecuteBooleanQuery(query);
607 d->Save(filename, format);
612 d->Import(url, format);
bool ExecuteBooleanQuery(const std::string &query) const
ResultMap Query(const std::string &query) const
bool Contains(RdfTriple triple)
void SetBaseUri(RdfUri uri)
DataCollection - Class to facilitate loading/accessing structured data.
void Import(std::string url, std::string format="")
bool Remove(RdfTriple triple)
void AddPrefix(std::string prefix, RdfUri uri)
bool Add(RdfTriple triple)
ResultMap ExecuteTupleQuery(const std::string &query) const
std::map< std::string, std::list< RdfNode > > ResultMap
PrefixMap GetPrefixes() const
static const std::string filename
RdfUri GetBaseUri() const
std::map< std::string, RdfUri > PrefixMap
void Save(std::string filename, std::string format="")