diff options
Diffstat (limited to 'raul/AtomRDF.hpp')
-rw-r--r-- | raul/AtomRDF.hpp | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/raul/AtomRDF.hpp b/raul/AtomRDF.hpp index 0b961d2..2a15fe3 100644 --- a/raul/AtomRDF.hpp +++ b/raul/AtomRDF.hpp @@ -22,10 +22,12 @@ #include <string> #include <sstream> #include <cmath> +#include <utility> #include "raul/log.hpp" #include "raul/Atom.hpp" #include "redlandmm/Node.hpp" #include "redlandmm/World.hpp" +#include "redlandmm/Model.hpp" #define CUC(x) ((const unsigned char*)(x)) @@ -39,18 +41,37 @@ namespace AtomRDF { /** Convert a Redland::Node to a Raul::Atom */ inline Atom -node_to_atom(const Redland::Node& node) +node_to_atom(Redland::Model& model, const Redland::Node& node) { - if (node.is_bool()) + if (node.is_bool()) { return Atom(bool(node.to_bool())); - else if (node.is_resource()) - return Atom(Atom::URI, node.world()->qualify(node.to_c_string())); - else if (node.is_float()) + } else if (node.is_resource()) { + return Atom(Atom::URI, node.to_c_string()); + } else if (node.is_float()) { return Atom(node.to_float()); - else if (node.is_int()) + } else if (node.is_int()) { return Atom(node.to_int()); - else + } else if (node.is_blank()) { + Atom::DictValue dict; + librdf_statement* pattern = librdf_new_statement_from_nodes( + model.world().c_obj(), + const_cast<librdf_node*>(node.c_obj()), + NULL, + NULL); + librdf_stream* results = librdf_model_find_statements( + const_cast<librdf_model*>(model.c_obj()), + pattern); + while (!librdf_stream_end(results)) { + librdf_statement* s = librdf_stream_get_object(results); + Redland::Node predicate(model.world(), librdf_statement_get_predicate(s)); + Redland::Node object(model.world(), librdf_statement_get_object(s)); + dict.insert(std::make_pair(node_to_atom(model, predicate), node_to_atom(model, object))); + librdf_stream_next(results); + } + return Atom(dict); + } else { return Atom(node.to_c_string()); + } } @@ -58,8 +79,10 @@ node_to_atom(const Redland::Node& node) * Note that not all Atoms are serialisable, the returned node should * be checked (can be treated as a bool) before use. */ inline Redland::Node -atom_to_node(Redland::World& world, const Atom& atom) +atom_to_node(Redland::Model& model, const Atom& atom) { + Redland::World& world = model.world(); + std::ostringstream os; std::string str; librdf_uri* type = NULL; @@ -98,6 +121,15 @@ atom_to_node(Redland::World& world, const Atom& atom) case Atom::STRING: str = atom.get_string(); break; + case Atom::DICT: + node = librdf_new_node(world.world()); + for (Atom::DictValue::const_iterator i = atom.get_dict().begin(); + i != atom.get_dict().end(); ++i) { + model.add_statement(Redland::Node(world, node), + atom_to_node(model, i->first), + atom_to_node(model, i->second)); + } + break; case Atom::BLOB: case Atom::NIL: default: |