RAUL  0.8.0
AtomRDF.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2007-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_ATOM_RDF_HPP
19 #define RAUL_ATOM_RDF_HPP
20 
21 #include <cmath>
22 #include <cstring>
23 #include <sstream>
24 #include <string>
25 #include <utility>
26 
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"
32 
33 #define CUC(x) ((const unsigned char*)(x))
34 
35 namespace Raul {
36 
41 namespace AtomRDF {
42 
44 inline Atom
45 node_to_atom(Redland::Model& model, const Redland::Node& node)
46 {
47  if (node.is_bool()) {
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()) {
56  Atom::DictValue dict;
57  librdf_statement* pattern = librdf_new_statement_from_nodes(
58  model.world().c_obj(),
59  const_cast<librdf_node*>(node.c_obj()),
60  NULL,
61  NULL);
62  librdf_stream* results = librdf_model_find_statements(
63  const_cast<librdf_model*>(model.c_obj()),
64  pattern);
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));
69  dict.insert(std::make_pair(node_to_atom(model, predicate), node_to_atom(model, object)));
70  librdf_stream_next(results);
71  }
72  return Atom(dict);
73  } else {
74  return Atom(node.to_c_string());
75  }
76 }
77 
78 
82 inline Redland::Node
83 atom_to_node(Redland::Model& model, const Atom& atom)
84 {
85  Redland::World& world = model.world();
86 
87  std::ostringstream os;
88  std::string str;
89  librdf_uri* type = NULL;
90  librdf_node* node = NULL;
91 
92  switch (atom.type()) {
93  case Atom::INT:
94  os << atom.get_int32();
95  str = os.str();
96  // xsd:integer -> pretty integer literals in Turtle
97  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#integer"));
98  break;
99  case Atom::FLOAT:
100  if (std::isnan(atom.get_float()) || std::isinf(atom.get_float()))
101  break;
102  os.precision(8);
103  os << atom.get_float();
104  str = os.str();
105  if (str.find(".") == std::string::npos)
106  str += ".0";
107  // xsd:decimal -> pretty decimal (float) literals in Turtle
108  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#decimal"));
109  break;
110  case Atom::BOOL:
111  // xsd:boolean -> pretty boolean literals in Turtle
112  if (atom.get_bool())
113  str = "true";
114  else
115  str = "false";
116  type = librdf_new_uri(world.world(), CUC("http://www.w3.org/2001/XMLSchema#boolean"));
117  break;
118  case Atom::URI:
119  str = atom.get_uri();
120  node = librdf_new_node_from_uri_string(world.world(), CUC(world.expand_uri(str).c_str()));
121  break;
122  case Atom::STRING:
123  str = atom.get_string();
124  break;
125  case Atom::DICT:
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),
130  atom_to_node(model, i->first),
131  atom_to_node(model, i->second));
132  }
133  break;
134  case Atom::BLOB:
135  case Atom::NIL:
136  default:
137  warn << "Unserializable Atom" << std::endl;
138  break;
139  }
140 
141  if (!node && str != "")
142  node = librdf_new_node_from_typed_literal(world.world(), CUC(str.c_str()), NULL, type);
143 
144  return Redland::Node(world, node);
145 }
146 
147 
148 } // namespace AtomRDF
149 } // namespace Raul
150 
151 #endif // RAUL_ATOM_RDF_HPP
152 
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
Definition: Array.hpp:26
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