knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
TripleContainer.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_TRIPLE_CONTAINER_H
7 #define KNOWROB_TRIPLE_CONTAINER_H
8 
9 #include <boost/iterator/iterator_facade.hpp>
10 #include <utility>
11 #include <vector>
12 #include <memory>
13 #include <functional>
14 #include "knowrob/semweb/Triple.h"
15 
16 namespace knowrob {
22  class TripleContainer {
23  public:
28 
33  struct iterator : boost::iterator_facade<iterator, const TriplePtr, boost::forward_traversal_tag> {
34  explicit iterator(const ConstGenerator &generator)
35  : generator_(generator), ptr_(generator_()) {
36  }
37 
38  bool equal(iterator const &other) const { return ptr_ == other.ptr_; }
39 
40  const TriplePtr &dereference() const { return *ptr_; }
41 
42  void increment() { ptr_ = generator_(); }
43 
44  private:
45  ConstGenerator generator_;
46  const TriplePtr *ptr_;
47  };
48 
49  virtual ~TripleContainer() = default;
50 
54  iterator begin() const {
55  return iterator(cgenerator());
56  }
57 
61  static iterator end() {
62  return iterator([] { return nullptr; });
63  }
64 
68  bool empty() const {
69  return begin().equal(end());
70  }
71 
75  virtual bool isMutable() const { return false; }
76 
80  virtual ConstGenerator cgenerator() const = 0;
81  };
82 
87  public:
92 
97  struct iterator : boost::iterator_facade<iterator, TriplePtr, boost::forward_traversal_tag> {
99  : generator_(std::move(generator)), ptr_(generator_()) {
100  }
101 
102  bool equal(iterator const &other) const { return ptr_ == other.ptr_; }
103 
104  TriplePtr &dereference() const { return *ptr_; }
105 
106  void increment() { ptr_ = generator_(); }
107 
108  private:
109  MutableGenerator generator_;
110  TriplePtr *ptr_;
111  };
112 
117  return iterator(generator());
118  }
119 
123  static iterator end() {
124  return iterator([] { return nullptr; });
125  }
126 
131 
132  // Override TripleContainer
133  bool isMutable() const override { return true; }
134  };
135 
139  class ProxyTripleContainer : public TripleContainer {
140  public:
141  explicit ProxyTripleContainer(const std::vector<TriplePtr> *triples);
142 
143  explicit ProxyTripleContainer(const std::vector<TriplePtr> &triples);
144 
145  // Override TripleContainer
146  ConstGenerator cgenerator() const override;
147 
148  protected:
149  const std::vector<TriplePtr> *triples_;
150  const std::vector<TriplePtr> triplesData_;
151  };
152 
157  class TripleViewBatch : public MutableTripleContainer {
158  public:
159  explicit TripleViewBatch(uint32_t batchSize);
160 
164  auto size() const { return actualSize_; }
165 
169  void reset() { actualSize_ = 0; }
170 
176  void add(const TriplePtr &triple);
177 
178  // Override TripleContainer
179  ConstGenerator cgenerator() const override;
180 
181  // Override MutableTripleContainer
183 
184  protected:
185  std::vector<TriplePtr> data_;
186  uint32_t batchSize_;
187  std::size_t actualSize_;
188  };
189 
190  using TripleContainerPtr = std::shared_ptr<TripleContainer>;
191  using TripleHandler = std::function<void(const TripleContainerPtr &)>;
192  using TripleVisitor = std::function<void(const TriplePtr &)>;
193 
194  using MutableTripleContainerPtr = std::shared_ptr<MutableTripleContainer>;
196 
197  using TripleFilter = std::function<bool(const Triple &)>;
198 }
199 
200 #endif //KNOWROB_TRIPLE_CONTAINER_H
bool isMutable() const override
virtual MutableGenerator generator()=0
std::function< TriplePtr *()> MutableGenerator
const std::vector< TriplePtr > * triples_
const std::vector< TriplePtr > triplesData_
ConstGenerator cgenerator() const override
ProxyTripleContainer(const std::vector< TriplePtr > *triples)
ProxyTripleContainer(const std::vector< TriplePtr > &triples)
static iterator end()
virtual ~TripleContainer()=default
iterator begin() const
virtual bool isMutable() const
std::function< const TriplePtr *()> ConstGenerator
virtual ConstGenerator cgenerator() const =0
TripleViewBatch(uint32_t batchSize)
std::vector< TriplePtr > data_
void add(const TriplePtr &triple)
ConstGenerator cgenerator() const override
MutableGenerator generator() override
FunctionRule & function()
Definition: terms.cpp:140
std::function< void(const TriplePtr &)> TripleVisitor
std::function< void(const MutableTripleContainerPtr &)> MutableTripleHandler
std::shared_ptr< TripleContainer > TripleContainerPtr
std::function< bool(const Triple &)> TripleFilter
std::shared_ptr< MutableTripleContainer > MutableTripleContainerPtr
std::function< void(const TripleContainerPtr &)> TripleHandler
iterator(MutableGenerator generator)
bool equal(iterator const &other) const
iterator(const ConstGenerator &generator)
const TriplePtr & dereference() const
bool equal(iterator const &other) const