knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Storage.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_STORAGE_H
7 #define KNOWROB_STORAGE_H
8 
9 #include <boost/property_tree/ptree.hpp>
10 #include "knowrob/semweb/TriplePattern.h"
11 #include "knowrob/semweb/Triple.h"
12 #include "knowrob/semweb/TripleContainer.h"
13 #include "knowrob/semweb/Vocabulary.h"
14 #include "knowrob/PropertyTree.h"
15 #include "knowrob/ThreadPool.h"
16 #include "knowrob/DataSourceHandler.h"
17 #include "knowrob/plugins/PluginFactory.h"
18 #include "knowrob/plugins/TypedPluginFactory.h"
19 
20 namespace knowrob {
24  enum class StorageFeature : std::uint8_t {
25  // A flag that indicates that nothing special is supported.
26  NothingSpecial = 1ul << 0,
27  // A flag that indicates that re-assignment of variables within query pipelines is supported.
28  ReAssignment = 1ul << 2,
29  // A flag that indicates that triple context is supported.
30  // If this flag is not set, statements with additional context information first must be reified
31  // before they can be handled by this backend.
32  TripleContext = 1ul << 3
33  };
35 
43 
51 
59  class Storage : public DataSourceHandler {
60  public:
62 
63  virtual ~Storage() = default;
64 
69 
73  const auto &vocabulary() const { return vocabulary_; }
74 
79  void setVocabulary(std::shared_ptr<Vocabulary> vocabulary) { vocabulary_ = std::move(vocabulary); }
80 
84  bool supports(StorageFeature feature) const { return features_ & feature; }
85 
91  virtual bool insertOne(const Triple &triple) = 0;
92 
98  virtual bool insertAll(const TripleContainerPtr &triples) = 0;
99 
104  virtual bool removeOne(const Triple &triple) = 0;
105 
110  virtual bool removeAll(const TripleContainerPtr &triples) = 0;
111 
116  virtual bool removeAllWithOrigin(std::string_view origin) = 0;
117 
123  virtual bool initializeBackend(const PropertyTree &config) = 0;
124 
125  std::optional<std::string> getVersionOfOrigin(std::string_view origin) const {
126  auto it = originVersions_.find(origin.data());
127  if (it != originVersions_.end()) {
128  return it->second;
129  }
130  return std::nullopt;
131  }
132 
133  void setVersionOfOrigin(std::string_view origin, std::optional<std::string_view> version) {
134  if (version) {
135  originVersions_[origin.data()] = version.value().data();
136  } else {
137  originVersions_.erase(origin.data());
138  }
139  }
140 
141  protected:
142  std::map<std::string, std::string> originVersions_;
143  std::shared_ptr<Vocabulary> vocabulary_;
146 
147  void enableFeature(StorageFeature feature) { features_ = features_ | feature; }
148 
150 
151  friend class StorageManager;
152  };
153 
154  using StoragePtr = std::shared_ptr<Storage>;
155  using BackendFactory = PluginFactory<Storage>;
156  using NamedBackend = NamedPlugin<Storage>;
157 
158 } // knowrob
159 
171 #define KNOWROB_STORAGE_PLUGIN(classType, pluginName) extern "C" { \
172  std::shared_ptr<knowrob::DataBackend> knowrob_createPlugin(const std::string &pluginID) \
173  { return std::make_shared<classType>(); } \
174  const char* knowrob_getPluginName() { return pluginName; } }
175 
176 #endif //KNOWROB_STORAGE_H
bool supports(StorageFeature feature) const
Definition: Storage.h:84
void setVersionOfOrigin(std::string_view origin, std::optional< std::string_view > version)
Definition: Storage.h:133
void enableFeature(StorageFeature feature)
Definition: Storage.h:147
std::optional< std::string > getVersionOfOrigin(std::string_view origin) const
Definition: Storage.h:125
const auto & vocabulary() const
Definition: Storage.h:73
std::shared_ptr< Vocabulary > vocabulary_
Definition: Storage.h:143
PluginLanguage storageLanguage_
Definition: Storage.h:145
void setVocabulary(std::shared_ptr< Vocabulary > vocabulary)
Definition: Storage.h:79
virtual bool initializeBackend(const PropertyTree &config)=0
virtual bool insertAll(const TripleContainerPtr &triples)=0
virtual ~Storage()=default
virtual bool removeAll(const TripleContainerPtr &triples)=0
virtual bool removeAllWithOrigin(std::string_view origin)=0
Storage(StorageFeatures features=StorageFeature::NothingSpecial)
Definition: Storage.h:61
std::map< std::string, std::string > originVersions_
Definition: Storage.h:142
void setStorageLanguage(PluginLanguage storageLanguage)
Definition: Storage.h:149
StorageFeatures features_
Definition: Storage.h:144
PluginLanguage storageLanguage() const
Definition: Storage.h:68
virtual bool removeOne(const Triple &triple)=0
virtual bool insertOne(const Triple &triple)=0
StorageFeature
Definition: Storage.h:24
FormulaPtr operator|(const FormulaPtr &phi, const FormulaPtr &psi)
Definition: Disjunction.cpp:55
std::shared_ptr< TripleContainer > TripleContainerPtr
PluginLanguage
Definition: NamedPlugin.h:16
NamedPlugin< Storage > NamedBackend
Definition: Storage.h:156
StorageFeature StorageFeatures
Definition: Storage.h:34
std::shared_ptr< Storage > StoragePtr
Definition: Storage.h:154
PluginFactory< Storage > BackendFactory
Definition: Storage.h:155
FormulaPtr operator&(const FormulaPtr &phi, const FormulaPtr &psi)
Definition: Conjunction.cpp:34