18 #ifndef RAUL_ATOM_RDF_HPP
19 #define RAUL_ATOM_RDF_HPP
27 #include "raul/Atom.hpp"
28 #include "raul/log.hpp"
29 #include "redlandmm/Model.hpp"
30 #include "redlandmm/Node.hpp"
31 #include "redlandmm/World.hpp"
33 #define CUC(x) ((const unsigned char*)(x))
48 return Atom(
bool(node.to_bool()));
49 }
else if (node.is_resource()) {
50 return Atom(Atom::URI, node.to_c_string());
51 }
else if (node.is_float()) {
52 return Atom(node.to_float());
53 }
else if (node.is_int()) {
54 return Atom(node.to_int());
55 }
else if (node.is_blank()) {
57 librdf_statement* pattern = librdf_new_statement_from_nodes(
58 model.world().c_obj(),
59 const_cast<librdf_node*
>(node.c_obj()),
62 librdf_stream* results = librdf_model_find_statements(
63 const_cast<librdf_model*>(model.c_obj()),
65 while (!librdf_stream_end(results)) {
66 librdf_statement* s = librdf_stream_get_object(results);
67 Redland::Node predicate(model.world(), librdf_statement_get_predicate(s));
68 Redland::Node object(model.world(), librdf_statement_get_object(s));
70 librdf_stream_next(results);
74 return Atom(node.to_c_string());
85 Redland::World& world = model.world();
87 std::ostringstream os;
89 librdf_uri* type = NULL;
90 librdf_node* node = NULL;
92 switch (atom.
type()) {
94 os << atom.get_int32();
97 type = librdf_new_uri(world.world(), CUC(
"http://www.w3.org/2001/XMLSchema#integer"));
100 if (std::isnan(atom.get_float()) || std::isinf(atom.get_float()))
103 os << atom.get_float();
105 if (str.find(
".") == std::string::npos)
108 type = librdf_new_uri(world.world(), CUC(
"http://www.w3.org/2001/XMLSchema#decimal"));
116 type = librdf_new_uri(world.world(), CUC(
"http://www.w3.org/2001/XMLSchema#boolean"));
119 str = atom.get_uri();
120 node = librdf_new_node_from_uri_string(world.world(), CUC(world.expand_uri(str).c_str()));
123 str = atom.get_string();
126 node = librdf_new_node(world.world());
127 for (Atom::DictValue::const_iterator i = atom.get_dict().begin();
128 i != atom.get_dict().end(); ++i) {
129 model.add_statement(Redland::Node(world, node),
137 warn <<
"Unserializable Atom" << std::endl;
141 if (!node && str !=
"")
142 node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
144 return Redland::Node(world, node);
151 #endif // RAUL_ATOM_RDF_HPP
A piece of data with some type.
Definition: Atom.hpp:43
Atom node_to_atom(Redland::Model &model, const Redland::Node &node)
Convert a Redland::Node to a Raul::Atom.
Definition: AtomRDF.hpp:45
Type type() const
Type of this atom.
Definition: Atom.hpp:165
Redland::Node atom_to_node(Redland::Model &model, const Atom &atom)
Convert a Raul::Atom to a Redland::Node Note that not all Atoms are serialisable, the returned node s...
Definition: AtomRDF.hpp:83