owlcpp  v0.3.3~
C++ library for working with OWL ontologies
 All Classes Namespaces Files Functions Macros Pages
query_node.hpp
Go to the documentation of this file.
1 
6 #ifndef QUERY_NODE_HPP_
7 #define QUERY_NODE_HPP_
8 #include "boost/lexical_cast.hpp"
9 #include "boost/numeric/conversion/cast.hpp"
10 
11 #include "owlcpp/rdf/config.hpp"
12 #include "owlcpp/rdf/exception.hpp"
13 #include "owlcpp/node_id.hpp"
16 
17 namespace owlcpp{
18 
21 inline bool is_literal(const Node_id nid, Triple_store const& ts) {
22  Node const& node = ts[nid];
23  return is_empty(node.ns_id());
24 }
25 
26 inline Node_literal const& to_literal(Node const& node) {
27  try{
28  return dynamic_cast<Node_literal const&>(node);
29  }catch(std::bad_cast&) {
30  BOOST_THROW_EXCEPTION(
31  Rdf_err()
32  << Rdf_err::msg_t("literal node is required")
33  << Rdf_err::str1_t(to_string(node))
34  );
35  }
36 }
37 
38 namespace detail{
39 template<class Out> class Get_value : public Visitor_node {
40  Triple_store const& ts_;
41  Out out_;
42 
43 public:
44  Get_value(Triple_store const& ts): ts_(ts) {}
45 
46  Out operator()() const {return out_;}
47 
48  void visit_impl(Node_iri const& node) {
49  BOOST_THROW_EXCEPTION(
50  Rdf_err()
51  << Rdf_err::msg_t("error converting IRI node to value")
52  << Rdf_err::str1_t(to_string(node))
53  );
54  }
55 
56  void visit_impl(Node_blank const& node) {
57  BOOST_THROW_EXCEPTION(
58  Rdf_err()
59  << Rdf_err::msg_t("error converting blank node to value")
60  << Rdf_err::str1_t(to_string(node))
61  );
62  }
63 
64  void visit_impl(Node_bool const& node) {
65  out_ = boost::numeric_cast<Out>(node.value());
66  }
67 
68  void visit_impl(Node_int const& node) {
69  out_ = boost::numeric_cast<Out>(node.value());
70  }
71 
72  void visit_impl(Node_unsigned const& node) {
73  out_ = boost::numeric_cast<Out>(node.value());
74  }
75 
76  void visit_impl(Node_double const& node) {
77  out_ = boost::numeric_cast<Out>(node.value());
78  }
79 
80  void visit_impl(Node_string const& node) {
81  out_ = boost::lexical_cast<Out>(node.value());
82  }
83 };
84 
85 template<> class Get_value<std::string> : public Visitor_node {
86  Triple_store const& ts_;
87  std::string out_;
88 
89 public:
90  Get_value(Triple_store const& ts): ts_(ts) {}
91 
92  std::string operator()() const {return out_;}
93 
94  void visit_impl(Node_iri const& node) {
95  out_ = to_string_full(node, ts_);
96  }
97 
98  void visit_impl(Node_blank const& node) {
99  out_ = to_string_full(node, ts_);
100  }
101 
102  void visit_impl(Node_bool const& node) {
103  out_ = node.value_str();
104  }
105 
106  void visit_impl(Node_int const& node) {
107  out_ = node.value_str();
108  }
109 
110  void visit_impl(Node_unsigned const& node) {
111  out_ = node.value_str();
112  }
113 
114  void visit_impl(Node_double const& node) {
115  out_ = node.value_str();
116  }
117 
118  void visit_impl(Node_string const& node) {
119  out_ = node.value();
120  }
121 };
122 }//namespace detail
123 
126 template<class Out> inline Out value(const Node_id nid, Triple_store const& ts) {
127  Node const& node = ts[nid];
128  detail::Get_value<Out> gv(ts);
129  try{
130  node.accept(gv);
131  }catch(...) {
132  BOOST_THROW_EXCEPTION(
133  Rdf_err()
134  << Rdf_err::msg_t("conversion error")
135  << Rdf_err::str1_t(to_string(node))
136  << Rdf_err::nested_t(boost::current_exception())
137  );
138  }
139  return gv();
140 }
141 
142 }//namespace owlcpp
143 #endif /* QUERY_NODE_HPP_ */