owlcpp  v0.3.3~
C++ library for working with OWL ontologies
 All Classes Namespaces Files Functions Macros Pages
copy_triples.hpp
Go to the documentation of this file.
1 
6 #ifndef COPY_TRIPLES_HPP_
7 #define COPY_TRIPLES_HPP_
8 #include "boost/foreach.hpp"
9 #include "boost/unordered_map.hpp"
10 #include "boost/assert.hpp"
11 
12 #include "owlcpp/rdf/triple.hpp"
13 #include "owlcpp/rdf/detail/map_traits.hpp"
16 
17 namespace owlcpp{ namespace detail{
18 
19 template<class Src, class Dest> class Node_copier : public Visitor_node {
20  typedef boost::unordered_map<Node_id,Node_id> node_map_t;
21  typedef node_map_t::const_iterator node_iter_t;
22  typedef boost::unordered_map<Doc_id,Doc_id> doc_map_t;
23  typedef doc_map_t::const_iterator doc_iter_t;
24  typedef boost::unordered_map<Ns_id,Ns_id> ns_map_t;
25  typedef ns_map_t::const_iterator ns_iter_t;
26 
27  typedef detail::Map_traits<Src> traits;
28  typedef typename traits::doc_type doc_type;
29 
30 public:
31  Node_copier(Src const& src, Dest& dest)
32  : src_(src), dest_(dest)
33  {}
34 
35  void operator()(Triple const& t) {
36  const Node_id subj = cp(t.subj_);
37  const Node_id pred = cp(t.pred_);
38  const Node_id obj = cp(t.obj_);
39  const Doc_id doc = cp(t.doc_);
40  dest_.insert_triple(subj, pred, obj, doc);
41  }
42 
43  Node_id cp(const Node_id nid0) {
44  const node_iter_t i = nm_.find(nid0);
45  if( i != nm_.end() ) return i->second;
46  Node const& node = src_[nid0];
47  node.accept(*this);
48  nm_.emplace(nid0, last_inserted_id_);
49  return last_inserted_id_;
50  }
51 
52  Doc_id cp(const Doc_id did0) {
53  const doc_iter_t i = dm_.find(did0);
54  if( i != dm_.end() ) return i->second;
55  doc_type const& doc = src_[did0];
56  const Node_id iri_id = cp(doc.ontology_iri);
57  const Node_id vers_id = cp(doc.version_iri);
58  const std::pair<Doc_id, bool> p =
59  dest_.insert_doc(
60  iri_id,
61  doc.path,
62  vers_id
63  );
64 
65  //Assertion is violated if same Doc info has been inserted into destination
66  //before %this was created.
67  //For example, this will be violated if a document is copied in multiple
68  //operations, with multiple instances of %Node_copier
69  //BOOST_ASSERT(p.second);
70 
71  dm_.emplace(did0, p.first);
72  return p.first;
73  }
74 
75  Ns_id cp(const Ns_id nsid0) {
76  const ns_iter_t i = nsm_.find(nsid0);
77  if( i != nsm_.end() ) return i->second;
78  const Ns_id nsid1 = dest_.insert(src_[nsid0]);
79  std::string const& pref = src_.prefix(nsid0);
80  if(
81  ! pref.empty() &&
82  dest_.prefix(nsid1).empty() &&
83  ! dest_.find_prefix(pref)
84  ) {
85  dest_.insert_prefix(nsid1, pref);
86  }
87  nsm_.emplace(nsid0, nsid1);
88  return nsid1;
89  }
90 
91 private:
92  Src const& src_;
93  Dest& dest_;
94  node_map_t nm_;
95  doc_map_t dm_;
96  ns_map_t nsm_;
97  Node_id last_inserted_id_;
99  void visit_impl(Node_iri const& node) {
100  const Ns_id nsid = cp(node.ns_id());
101  last_inserted_id_ = dest_.insert_node_iri(nsid, node.fragment());
102  }
103 
104  void visit_impl(Node_blank const& node) {
105  const Doc_id did = cp(node.document());
106 
107  //check if blank node is already present
108  //see comment in %cp(const Doc_id)
109  //BOOST_ASSERT( ! dest_.find_blank(node.index(), did));
110 
111  last_inserted_id_ = dest_.insert_blank(node.index(), did);
112  }
113 
114  void visit_impl(Node_bool const& node) {
115  const Node_id dt = cp(node.datatype());
116  last_inserted_id_ = dest_.insert_literal(node.value_str(), dt);
117  }
118 
119  void visit_impl(Node_int const& node) {
120  const Node_id dt = cp(node.datatype());
121  last_inserted_id_ = dest_.insert_literal(node.value_str(), dt);
122  }
123 
124  void visit_impl(Node_unsigned const& node) {
125  const Node_id dt = cp(node.datatype());
126  last_inserted_id_ = dest_.insert_literal(node.value_str(), dt);
127  }
128 
129  void visit_impl(Node_double const& node) {
130  const Node_id dt = cp(node.datatype());
131  last_inserted_id_ = dest_.insert_literal(node.value_str(), dt);
132  }
133 
134  void visit_impl(Node_string const& node) {
135  const Node_id dt = cp(node.datatype());
136  last_inserted_id_ = dest_.insert_literal(
137  node.value(), dt, node.language()
138  );
139  }
140 };
141 
142 }//namespace detail
143 
151 template<class Range, class Src, class Dest> inline void copy_triples(
152  Range const& r,
153  Src const& src,
154  Dest& dest
155 ) {
156  detail::Node_copier<Src, Dest> copier(src, dest);
157  BOOST_FOREACH(Triple const& t, r) copier(t);
158 }
159 
166 template<class Src, class Dest> inline void copy_triples(Src const& src, Dest& dest) {
167  copy_triples(src.map_triple(), src, dest);
168 }
169 
170 }//namespace owlcpp
171 #endif /* COPY_TRIPLES_HPP_ */