knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
RedlandModel.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_REDLAND_MODEL_H
7 #define KNOWROB_REDLAND_MODEL_H
8 
9 #include <redland.h>
10 #include "knowrob/semweb/TripleContainer.h"
11 #include "knowrob/URI.h"
12 #include "knowrob/semweb/TripleFormat.h"
13 #include "knowrob/storage/Storage.h"
14 #include "knowrob/semweb/SPARQLQuery.h"
15 #include "RedlandURI.h"
16 #include "knowrob/storage/SPARQLBackend.h"
17 
18 namespace knowrob {
23  enum class RedlandStorageType {
24  // This module is always present (cannot be removed) and provides a simple and fast in-memory store with no persistence.
25  MEMORY,
26  // This module is always present (cannot be removed) and provides indexed storage using Redland Triple stores
27  HASHES,
28  // This module is compiled in when MySQL 3 or 4 is available.
29  MYSQL,
30  // This module is based on the MySQL store and is compiled in when PostgreSQL is available.
31  POSTGRESQL,
32  // This module provides storage via the SQLite relational database when available and supports SQLite V2 and V3.
33  SQLITE
34  };
38  enum class RedlandHashType {
39  // The default hash type.
40  MEMORY,
41  // The Berkeley DB hash type.
42  BDB
43  };
44 
52  class RedlandModel : public SPARQLBackend {
53  public:
54  static constexpr std::string_view QUERY_LANGUAGE_SPARQL = "sparql";
55 
57 
58  ~RedlandModel() override;
59 
65 
71 
75  void setHost(std::string_view host);
76 
80  void setDatabase(std::string_view database);
81 
85  void setUser(std::string_view user);
86 
90  void setPassword(std::string_view password);
91 
95  void setStorageDirectory(std::string_view dir = ".");
96 
103  void setHashesStorage(RedlandHashType hashType, std::string_view dir = ".");
104 
110 
115  void setRaptorWorld(librdf_world *world);
116 
121  void setOrigin(std::string_view origin) { origin_ = origin; }
122 
126  auto storageType() const { return storageType_; }
127 
131  bool isInitialized() const;
132 
138 
139  // override DataBackend
140  bool initializeBackend(const PropertyTree &config) override;
141 
149  bool load(const URI &uri, semweb::TripleFormat tripleFormat);
150 
151  // override SPARQLBackend
152  bool sparql(std::string_view queryString, const BindingsHandler &callback) const override;
153 
154  // override DataBackend
155  bool insertOne(const Triple &triple) override;
156 
157  // override DataBackend
158  bool insertAll(const TripleContainerPtr &triples) override;
159 
160  // override DataBackend
161  bool removeOne(const Triple &triple) override;
162 
163  // override DataBackend
164  bool removeAll(const TripleContainerPtr &triples) override;
165 
166  // override DataBackend
167  bool removeAllWithOrigin(std::string_view origin) override;
168 
169  // Override QueryableBackend
170  bool isPersistent() const override;
171 
172  // Override QueryableBackend
173  bool contains(const Triple &triple) override;
174 
175  // Override QueryableBackend
176  void batch(const TripleHandler &callback) const override;
177 
178  // Override QueryableBackend
179  void batchOrigin(std::string_view origin, const TripleHandler &callback) override;
180 
181  // Override QueryableBackend
182  void match(const TriplePattern &query, const TripleVisitor &visitor) override;
183 
184  protected:
185  librdf_world *ownedWorld_;
186  librdf_world *world_;
187  librdf_model *model_;
188  librdf_storage *storage_;
189 
190  // the uris are created within the context of a librdf_world, so
191  // they cannot be defined statically.
192  RedlandURI xsdURIs_[static_cast<int>(XSDType::LAST)];
193 
195 
196  std::map<std::string, librdf_node *, std::less<>> contextNodes_;
197 
199  // if set when querying, ignore context nodes and use this origin
200  std::optional<std::string> origin_;
201  std::optional<std::string> host_;
202  std::optional<std::string> port_;
203  std::optional<std::string> database_;
204  std::optional<std::string> user_;
205  std::optional<std::string> password_;
206  std::optional<std::string> storageDir_;
207  std::optional<RedlandHashType> hashType_;
210 
212 
213  void knowrobToRaptor(const Triple &triple, raptor_statement *raptorTriple);
214 
215  void knowrobToRaptor(const TriplePattern &pat, raptor_statement *raptorTriple);
216 
217  raptor_term *knowrobToRaptor(const TermPtr &term);
218 
219  librdf_node *getContextNode(std::string_view origin);
220 
221  librdf_node *getContextNode(const Triple &triple);
222 
223  private:
224  void finalize();
225  };
226 
227 } // knowrob
228 
229 #endif //KNOWROB_REDLAND_MODEL_H
auto storageType() const
Definition: RedlandModel.h:126
bool insertOne(const Triple &triple) override
std::optional< std::string > port_
Definition: RedlandModel.h:202
librdf_node * getContextNode(const Triple &triple)
librdf_world * world_
Definition: RedlandModel.h:186
librdf_world * ownedWorld_
Definition: RedlandModel.h:185
void setRaptorWorld(librdf_world *world)
void setUser(std::string_view user)
librdf_model * model_
Definition: RedlandModel.h:187
bool isInitialized() const
std::map< std::string, librdf_node *, std::less<> > contextNodes_
Definition: RedlandModel.h:196
void batchOrigin(std::string_view origin, const TripleHandler &callback) override
void knowrobToRaptor(const Triple &triple, raptor_statement *raptorTriple)
raptor_term * knowrobToRaptor(const TermPtr &term)
void setStorageType(RedlandStorageType storageType)
void batch(const TripleHandler &callback) const override
std::optional< std::string > storageDir_
Definition: RedlandModel.h:206
void match(const TriplePattern &query, const TripleVisitor &visitor) override
bool removeAll(const TripleContainerPtr &triples) override
librdf_storage * storage_
Definition: RedlandModel.h:188
RedlandURI xsdURIs_[static_cast< int >(XSDType::LAST)]
Definition: RedlandModel.h:192
bool load(const URI &uri, semweb::TripleFormat tripleFormat)
bool isPersistent() const override
std::optional< std::string > host_
Definition: RedlandModel.h:201
bool insertAll(const TripleContainerPtr &triples) override
void setStorageHashType(RedlandHashType hashType)
void setPassword(std::string_view password)
bool contains(const Triple &triple) override
std::optional< std::string > password_
Definition: RedlandModel.h:205
void setOrigin(std::string_view origin)
Definition: RedlandModel.h:121
~RedlandModel() override
bool sparql(std::string_view queryString, const BindingsHandler &callback) const override
void setHost(std::string_view host)
std::string getStorageOptions() const
bool removeAllWithOrigin(std::string_view origin) override
bool initializeBackend(const PropertyTree &config) override
std::optional< std::string > origin_
Definition: RedlandModel.h:200
void setDatabase(std::string_view database)
void setHashesStorage(RedlandHashType hashType, std::string_view dir=".")
librdf_node * getContextNode(std::string_view origin)
std::optional< std::string > database_
Definition: RedlandModel.h:203
std::string storageOptions_
Definition: RedlandModel.h:209
RedlandStorageType storageType_
Definition: RedlandModel.h:208
void setStorageDirectory(std::string_view dir=".")
void knowrobToRaptor(const TriplePattern &pat, raptor_statement *raptorTriple)
RedlandURI & xsdURI(XSDType xsdType)
std::optional< std::string > user_
Definition: RedlandModel.h:204
static constexpr std::string_view QUERY_LANGUAGE_SPARQL
Definition: RedlandModel.h:54
std::optional< RedlandHashType > hashType_
Definition: RedlandModel.h:207
bool removeOne(const Triple &triple) override
std::string storageName_
Definition: RedlandModel.h:198
bool query(const SPARQLQuery &query, const BindingsHandler &callback) const
TermRule & string()
Definition: terms.cpp:63
TermRule & term()
Definition: terms.cpp:136
std::function< void(const TriplePtr &)> TripleVisitor
std::shared_ptr< TripleContainer > TripleContainerPtr
RedlandStorageType
Definition: RedlandModel.h:23
std::shared_ptr< Term > TermPtr
Definition: Term.h:117
std::function< void(const BindingsPtr &)> BindingsHandler
Definition: Bindings.h:152
std::function< void(const TripleContainerPtr &)> TripleHandler
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16