knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::RedlandModel Class Reference

#include <RedlandModel.h>

Inheritance diagram for knowrob::RedlandModel:
Collaboration diagram for knowrob::RedlandModel:

Public Member Functions

 RedlandModel ()
 
 ~RedlandModel () override
 
void setStorageType (RedlandStorageType storageType)
 
void setStorageHashType (RedlandHashType hashType)
 
void setHost (std::string_view host)
 
void setDatabase (std::string_view database)
 
void setUser (std::string_view user)
 
void setPassword (std::string_view password)
 
void setStorageDirectory (std::string_view dir=".")
 
void setHashesStorage (RedlandHashType hashType, std::string_view dir=".")
 
void setMemoryStorage ()
 
void setRaptorWorld (librdf_world *world)
 
void setOrigin (std::string_view origin)
 
auto storageType () const
 
bool isInitialized () const
 
bool initializeBackend ()
 
bool initializeBackend (const PropertyTree &config) override
 
bool load (const URI &uri, semweb::TripleFormat tripleFormat)
 
bool sparql (std::string_view queryString, const BindingsHandler &callback) const override
 
bool insertOne (const Triple &triple) override
 
bool insertAll (const TripleContainerPtr &triples) override
 
bool removeOne (const Triple &triple) override
 
bool removeAll (const TripleContainerPtr &triples) override
 
bool removeAllWithOrigin (std::string_view origin) override
 
bool isPersistent () const override
 
bool contains (const Triple &triple) override
 
void batch (const TripleHandler &callback) const override
 
void batchOrigin (std::string_view origin, const TripleHandler &callback) override
 
void match (const TriplePattern &query, const TripleVisitor &visitor) override
 
 RedlandModel ()
 
 ~RedlandModel () override
 
void setStorageType (RedlandStorageType storageType)
 
void setStorageHashType (RedlandHashType hashType)
 
void setHost (std::string_view host)
 
void setDatabase (std::string_view database)
 
void setUser (std::string_view user)
 
void setPassword (std::string_view password)
 
void setStorageDirectory (std::string_view dir=".")
 
void setHashesStorage (RedlandHashType hashType, std::string_view dir=".")
 
void setMemoryStorage ()
 
void setRaptorWorld (librdf_world *world)
 
void setOrigin (std::string_view origin)
 
auto storageType () const
 
bool isInitialized () const
 
bool initializeBackend ()
 
bool initializeBackend (const PropertyTree &config) override
 
bool load (const URI &uri, semweb::TripleFormat tripleFormat)
 
bool sparql (std::string_view queryString, const BindingsHandler &callback) const override
 
bool insertOne (const Triple &triple) override
 
bool insertAll (const TripleContainerPtr &triples) override
 
bool removeOne (const Triple &triple) override
 
bool removeAll (const TripleContainerPtr &triples) override
 
bool removeAllWithOrigin (std::string_view origin) override
 
bool isPersistent () const override
 
bool contains (const Triple &triple) override
 
void batch (const TripleHandler &callback) const override
 
void batchOrigin (std::string_view origin, const TripleHandler &callback) override
 
void match (const TriplePattern &query, const TripleVisitor &visitor) override
 
- Public Member Functions inherited from knowrob::SPARQLBackend
 SPARQLBackend (SPARQLFlags flags)
 
bool query (const SPARQLQuery &query, const BindingsHandler &callback) const
 
void query (const GraphQueryPtr &query, const BindingsHandler &callback) override
 
void count (const ResourceCounter &callback) const override
 
 SPARQLBackend (SPARQLFlags flags)
 
bool query (const SPARQLQuery &query, const BindingsHandler &callback) const
 
void query (const GraphQueryPtr &query, const BindingsHandler &callback) override
 
void count (const ResourceCounter &callback) const override
 
- Public Member Functions inherited from knowrob::QueryableStorage
 QueryableStorage (StorageFeatures features=StorageFeature::NothingSpecial)
 
 ~QueryableStorage () override=default
 
virtual void foreach (const TripleVisitor &visitor) const
 
std::vector< VersionedOriginPtrgetOrigins ()
 
std::optional< std::string > getVersionOfOrigin (std::string_view origin)
 
void setVersionOfOrigin (std::string_view origin, std::string_view version)
 
void dropSessionOrigins ()
 
GraphQueryExpansionPtr expand (const GraphQueryPtr &q)
 
 QueryableStorage (StorageFeatures features=StorageFeature::NothingSpecial)
 
 ~QueryableStorage () override=default
 
virtual void foreach (const TripleVisitor &visitor) const
 
std::vector< VersionedOriginPtrgetOrigins ()
 
std::optional< std::string > getVersionOfOrigin (std::string_view origin)
 
void setVersionOfOrigin (std::string_view origin, std::string_view version)
 
void dropSessionOrigins ()
 
GraphQueryExpansionPtr expand (const GraphQueryPtr &q)
 
- Public Member Functions inherited from knowrob::Storage
 Storage (StorageFeatures features=StorageFeature::NothingSpecial)
 
virtual ~Storage ()=default
 
PluginLanguage storageLanguage () const
 
const auto & vocabulary () const
 
void setVocabulary (std::shared_ptr< Vocabulary > vocabulary)
 
bool supports (StorageFeature feature) const
 
std::optional< std::string > getVersionOfOrigin (std::string_view origin) const
 
void setVersionOfOrigin (std::string_view origin, std::optional< std::string_view > version)
 
 Storage (StorageFeatures features=StorageFeature::NothingSpecial)
 
virtual ~Storage ()=default
 
PluginLanguage storageLanguage () const
 
const auto & vocabulary () const
 
void setVocabulary (std::shared_ptr< Vocabulary > vocabulary)
 
bool supports (StorageFeature feature) const
 
std::optional< std::string > getVersionOfOrigin (std::string_view origin) const
 
void setVersionOfOrigin (std::string_view origin, std::optional< std::string_view > version)
 
- Public Member Functions inherited from knowrob::DataSourceHandler
 DataSourceHandler ()=default
 
virtual ~DataSourceHandler ()=default
 
void addDataHandler (const std::string &format, const DataSourceLoader &fn)
 
bool loadDataSource (const DataSourcePtr &dataSource)
 
bool hasDataHandler (const DataSourcePtr &dataSource) const
 
 DataSourceHandler ()=default
 
virtual ~DataSourceHandler ()=default
 
void addDataHandler (const std::string &format, const DataSourceLoader &fn)
 
bool loadDataSource (const DataSourcePtr &dataSource)
 
bool hasDataHandler (const DataSourcePtr &dataSource) const
 

Static Public Attributes

static constexpr std::string_view QUERY_LANGUAGE_SPARQL = "sparql"
 
- Static Public Attributes inherited from knowrob::QueryableStorage
static AtomPtr versionProperty = IRIAtom::Tabled("http://knowrob.org/kb/knowrob.owl#hasVersionOfOrigin")
 

Protected Member Functions

RedlandURIxsdURI (XSDType xsdType)
 
std::string getStorageOptions () const
 
void knowrobToRaptor (const Triple &triple, raptor_statement *raptorTriple)
 
void knowrobToRaptor (const TriplePattern &pat, raptor_statement *raptorTriple)
 
raptor_term * knowrobToRaptor (const TermPtr &term)
 
librdf_node * getContextNode (std::string_view origin)
 
librdf_node * getContextNode (const Triple &triple)
 
RedlandURIxsdURI (XSDType xsdType)
 
std::string getStorageOptions () const
 
void knowrobToRaptor (const Triple &triple, raptor_statement *raptorTriple)
 
void knowrobToRaptor (const TriplePattern &pat, raptor_statement *raptorTriple)
 
raptor_term * knowrobToRaptor (const TermPtr &term)
 
librdf_node * getContextNode (std::string_view origin)
 
librdf_node * getContextNode (const Triple &triple)
 
- Protected Member Functions inherited from knowrob::Storage
void enableFeature (StorageFeature feature)
 
void setStorageLanguage (PluginLanguage storageLanguage)
 
void enableFeature (StorageFeature feature)
 
void setStorageLanguage (PluginLanguage storageLanguage)
 
- Protected Member Functions inherited from knowrob::DataSourceHandler
virtual bool loadDataSourceWithUnknownFormat (const DataSourcePtr &)
 
virtual bool loadDataSourceWithUnknownFormat (const DataSourcePtr &)
 

Protected Attributes

librdf_world * ownedWorld_
 
librdf_world * world_
 
librdf_model * model_
 
librdf_storage * storage_
 
RedlandURI xsdURIs_ [static_cast< int >(XSDType::LAST)]
 
std::map< std::string, librdf_node *, std::less<> > contextNodes_
 
std::string storageName_
 
std::optional< std::string > origin_
 
std::optional< std::string > host_
 
std::optional< std::string > port_
 
std::optional< std::string > database_
 
std::optional< std::string > user_
 
std::optional< std::string > password_
 
std::optional< std::string > storageDir_
 
std::optional< RedlandHashTypehashType_
 
RedlandStorageType storageType_
 
std::string storageOptions_
 
- Protected Attributes inherited from knowrob::SPARQLBackend
SPARQLFlags sparqlFlags_
 
- Protected Attributes inherited from knowrob::Storage
std::map< std::string, std::string > originVersions_
 
std::shared_ptr< Vocabularyvocabulary_
 
StorageFeatures features_
 
PluginLanguage storageLanguage_
 
- Protected Attributes inherited from knowrob::DataSourceHandler
std::map< std::string, DataSourceLoaderdataSourceHandler_
 

Additional Inherited Members

- Static Public Member Functions inherited from knowrob::QueryableStorage
static std::shared_ptr< AnswerYesyes (const GraphPathQueryPtr &original, const GraphQueryExpansionPtr &expansion, const BindingsPtr &bindings)
 
static std::shared_ptr< AnswerNono (const GraphPathQueryPtr &q)
 
static std::shared_ptr< AnswerYesyes (const GraphPathQueryPtr &original, const GraphQueryExpansionPtr &expansion, const BindingsPtr &bindings)
 
static std::shared_ptr< AnswerNono (const GraphPathQueryPtr &q)
 

Detailed Description

Interface for a Redland model. A redland model is a triple store that can be queried with SPARQL. It can interface with different storage types, such as in-memory, MySQL, PostgreSQL, and SQLite, and can load and save triples in different formats, such as RDF/XML, Turtle, and N-Triples. It can further interface with SPARQL endpoints.

Definition at line 52 of file RedlandModel.h.

Constructor & Destructor Documentation

◆ RedlandModel() [1/2]

RedlandModel::RedlandModel ( )

Definition at line 79 of file RedlandModel.cpp.

81  ownedWorld_(nullptr),
82  world_(nullptr),
83  model_(nullptr),
84  storage_(nullptr),
86 }
librdf_world * world_
Definition: RedlandModel.h:186
librdf_world * ownedWorld_
Definition: RedlandModel.h:185
librdf_model * model_
Definition: RedlandModel.h:187
librdf_storage * storage_
Definition: RedlandModel.h:188
RedlandStorageType storageType_
Definition: RedlandModel.h:208
SPARQLBackend(SPARQLFlags flags)
Definition: SPARQLBackend.h:18

◆ ~RedlandModel() [1/2]

RedlandModel::~RedlandModel ( )
override

Definition at line 88 of file RedlandModel.cpp.

88  {
89  finalize();
90 }

◆ RedlandModel() [2/2]

knowrob::RedlandModel::RedlandModel ( )

◆ ~RedlandModel() [2/2]

knowrob::RedlandModel::~RedlandModel ( )
override

Member Function Documentation

◆ batch() [1/2]

void RedlandModel::batch ( const TripleHandler callback) const
overridevirtual

Iterate over all triples in the model.

Parameters
callbackthe callback to handle the triples.
Returns
true if the iteration was successful.

Implements knowrob::QueryableStorage.

Definition at line 437 of file RedlandModel.cpp.

437  {
438  auto triples = std::make_shared<RaptorContainer>(GlobalSettings::batchSize());
439  auto contexts = librdf_model_get_contexts(model_);
440  while (!librdf_iterator_end(contexts)) {
441  batch_(model_, (librdf_node *) librdf_iterator_get_object(contexts), triples, callback);
442  librdf_iterator_next(contexts);
443  }
444  if (triples->size() > 0) {
445  triples->shrink();
446  callback(triples);
447  }
448  librdf_free_iterator(contexts);
449 }
static uint32_t batchSize()
Definition: knowrob.h:68

◆ batch() [2/2]

void knowrob::RedlandModel::batch ( const TripleHandler callback) const
overridevirtual

Iterate over all triples in the model.

Parameters
callbackthe callback to handle the triples.
Returns
true if the iteration was successful.

Implements knowrob::QueryableStorage.

◆ batchOrigin() [1/2]

void RedlandModel::batchOrigin ( std::string_view  origin,
const TripleHandler callback 
)
overridevirtual

Iterate over all triples in the model that have a given origin.

Parameters
originthe origin of the triples.
callbackthe callback to handle the triples.
Returns
true if the iteration was successful.

Implements knowrob::QueryableStorage.

Definition at line 451 of file RedlandModel.cpp.

451  {
452  auto triples = std::make_shared<RaptorContainer>(GlobalSettings::batchSize());
453  batch_(model_, getContextNode(origin), triples, callback);
454  if (triples->size() > 0) {
455  triples->shrink();
456  callback(triples);
457  }
458 }
librdf_node * getContextNode(std::string_view origin)

◆ batchOrigin() [2/2]

void knowrob::RedlandModel::batchOrigin ( std::string_view  origin,
const TripleHandler callback 
)
overridevirtual

Iterate over all triples in the model that have a given origin.

Parameters
originthe origin of the triples.
callbackthe callback to handle the triples.
Returns
true if the iteration was successful.

Implements knowrob::QueryableStorage.

◆ contains() [1/2]

bool RedlandModel::contains ( const Triple triple)
overridevirtual
Parameters
triplea framed triple.
Returns
true if the model contains the triple.

Reimplemented from knowrob::QueryableStorage.

Definition at line 460 of file RedlandModel.cpp.

460  {
461  auto raptorTriple = librdf_new_statement(world_);
462  knowrobToRaptor(triple, raptorTriple);
463  auto result = librdf_model_contains_statement(model_, raptorTriple);
464  librdf_free_statement(raptorTriple);
465  return result;
466 }
void knowrobToRaptor(const Triple &triple, raptor_statement *raptorTriple)

◆ contains() [2/2]

bool knowrob::RedlandModel::contains ( const Triple triple)
overridevirtual
Parameters
triplea framed triple.
Returns
true if the model contains the triple.

Reimplemented from knowrob::QueryableStorage.

◆ getContextNode() [1/4]

librdf_node * RedlandModel::getContextNode ( const Triple triple)
protected

Definition at line 557 of file RedlandModel.cpp.

557  {
558  return getContextNode(
559  triple.graph() ? triple.graph().value() :
560  origin_.has_value() ? origin_.value() :
562 }
static constexpr std::string_view ORIGIN_USER
std::optional< std::string > origin_
Definition: RedlandModel.h:200
virtual std::optional< std::string_view > graph() const =0

◆ getContextNode() [2/4]

librdf_node* knowrob::RedlandModel::getContextNode ( const Triple triple)
protected

◆ getContextNode() [3/4]

librdf_node * RedlandModel::getContextNode ( std::string_view  origin)
protected

Definition at line 545 of file RedlandModel.cpp.

545  {
546  auto it = contextNodes_.find(origin);
547  if (it != contextNodes_.end()) {
548  return it->second;
549  }
550  auto contextNode = librdf_new_node_from_uri_string(
551  world_,
552  (const unsigned char *) origin.data());
553  contextNodes_[std::string(origin)] = contextNode;
554  return contextNode;
555 }
std::map< std::string, librdf_node *, std::less<> > contextNodes_
Definition: RedlandModel.h:196
TermRule & string()
Definition: terms.cpp:63

◆ getContextNode() [4/4]

librdf_node* knowrob::RedlandModel::getContextNode ( std::string_view  origin)
protected

◆ getStorageOptions() [1/2]

std::string RedlandModel::getStorageOptions ( ) const
protected

Definition at line 259 of file RedlandModel.cpp.

259  {
260  using OptPair_o = std::pair<std::string, std::optional<std::string>>;
261  std::vector<std::pair<std::string, std::string>> opts;
262  for (auto &option: {
263  OptPair_o{"host", host_},
264  OptPair_o{"port", port_},
265  OptPair_o{"database", database_},
266  OptPair_o{"user", user_},
267  OptPair_o{"password", password_},
268  OptPair_o{"dir", storageDir_}}) {
269  if (option.second.has_value()) {
270  opts.emplace_back(option.first, option.second.value());
271  }
272  }
273  // "hash-type" is required by "hashes" storage type
274  if (hashType_.has_value()) {
275  switch (hashType_.value()) {
277  opts.emplace_back("hash-type", "memory");
278  break;
280  opts.emplace_back("hash-type", "bdb");
281  break;
282  }
283  }
284  // context support currently is required which limits the storage types we can use.
285  // but "memory" and "hashes" can do it.
286  opts.emplace_back("contexts", "yes");
287  std::stringstream ss;
288  for (std::size_t i = 0; i < opts.size(); i++) {
289  ss << opts[i].first << "='" << opts[i].second << '\'';
290  if (i < opts.size() - 1) {
291  ss << ",";
292  }
293  }
294  return ss.str();
295 }
std::optional< std::string > port_
Definition: RedlandModel.h:202
std::optional< std::string > storageDir_
Definition: RedlandModel.h:206
std::optional< std::string > host_
Definition: RedlandModel.h:201
std::optional< std::string > password_
Definition: RedlandModel.h:205
std::optional< std::string > database_
Definition: RedlandModel.h:203
std::optional< std::string > user_
Definition: RedlandModel.h:204
std::optional< RedlandHashType > hashType_
Definition: RedlandModel.h:207
TermRule & option()
Definition: terms.cpp:110

◆ getStorageOptions() [2/2]

std::string knowrob::RedlandModel::getStorageOptions ( ) const
protected

◆ initializeBackend() [1/4]

bool RedlandModel::initializeBackend ( )

Initialize the backend.

Returns
true if the backend was initialized successfully.

Definition at line 174 of file RedlandModel.cpp.

174  {
175  if (isInitialized()) {
176  KB_WARN("RedlandModel already initialized.");
177  return false;
178  }
179 
180  // initialize the world if it hasn't been set before
181  if (!world_) {
182  ownedWorld_ = world_ = librdf_new_world();
183  if (!world_) {
184  throw StorageError("Failed to create Redland world.");
185  }
186  librdf_world_open(world_);
187  librdf_world_set_logger(world_, nullptr, logRedlandMessage);
188  }
189 
190  // initialize the storage
192  const char *storageOptions = storageOptions_.empty() ? nullptr : storageOptions_.c_str();
193  const char *storageType = getStorageTypeString(storageType_);
194  const char *storageName = origin_.has_value() ? origin_.value().c_str() : "knowrob";
195  KB_INFO("[redland] using storage of type \"{}\" with name \"{}\" and options \"{}\".",
196  storageType, storageName, storageOptions);
197  storage_ = librdf_new_storage(world_,
198  storageType,
199  storageName,
200  storageOptions);
201  if (!storage_) {
202  throw StorageError("Failed to create Redland storage for type \"{}\".", storageType);
203  }
204 
205  // initialize the model
206  static const char *modelOptions = nullptr;
207  model_ = librdf_new_model(world_, storage_, modelOptions);
208  if (!model_) {
209  librdf_free_storage(storage_);
210  storage_ = nullptr;
211  throw StorageError("Failed to create Redland model for storage type \"{}\".", storageType);
212  }
213 
214  // initialize URIs
215  for (int i = 0; i != static_cast<int>(XSDType::LAST); i++) {
217  }
218 
219  return true;
220 }
#define KB_INFO
Definition: Logger.h:26
#define KB_WARN
Definition: Logger.h:27
auto storageType() const
Definition: RedlandModel.h:126
RedlandURI xsdURIs_[static_cast< int >(XSDType::LAST)]
Definition: RedlandModel.h:192
bool isInitialized() const
std::string getStorageOptions() const
std::string storageOptions_
Definition: RedlandModel.h:209
void set(librdf_world *world, std::string_view uri)
Definition: RedlandURI.h:34
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16

◆ initializeBackend() [2/4]

bool knowrob::RedlandModel::initializeBackend ( )

Initialize the backend.

Returns
true if the backend was initialized successfully.

◆ initializeBackend() [3/4]

bool RedlandModel::initializeBackend ( const PropertyTree config)
overridevirtual

Initialize this backend from a property tree.

Parameters
configa property tree.
Returns
true on success

Implements knowrob::Storage.

Definition at line 222 of file RedlandModel.cpp.

222  {
223  if (isInitialized()) {
224  KB_WARN("RedlandModel already initialized.");
225  return false;
226  }
227  auto o_host = ptree->get_optional<std::string>(REDLAND_SETTING_HOST);
228  auto o_port = ptree->get_optional<std::string>(REDLAND_SETTING_PORT);
229  auto o_user = ptree->get_optional<std::string>(REDLAND_SETTING_USER);
230  auto o_password = ptree->get_optional<std::string>(REDLAND_SETTING_PASSWORD);
231  auto o_db = ptree->get_optional<std::string>(REDLAND_SETTING_DB);
232  auto o_origin = ptree->get_optional<std::string>(REDLAND_SETTING_ORIGIN);
233  auto o_storage = ptree->get_optional<std::string>(REDLAND_SETTING_STORAGE);
234 
235  if (o_host) host_ = o_host.value();
236  if (o_port) port_ = o_port.value();
237  if (o_user) user_ = o_user.value();
238  if (o_password) password_ = o_password.value();
239  if (o_db) database_ = o_db.value();
240  if (o_origin) origin_ = o_origin.value();
241  if (o_storage) {
243  if (o_storage.value() == "hashes") {
245  } else if (o_storage.value() == "mysql") {
247  } else if (o_storage.value() == "postgresql") {
249  } else if (o_storage.value() == "sqlite") {
251  } else {
252  KB_WARN("Unknown storage type \"{}\", falling back to memory.", o_storage.value());
253  }
254  }
255  // finally call initializeBackend()
256  return initializeBackend();
257 }
#define REDLAND_SETTING_DB
#define REDLAND_SETTING_USER
#define REDLAND_SETTING_PORT
#define REDLAND_SETTING_ORIGIN
#define REDLAND_SETTING_PASSWORD
#define REDLAND_SETTING_HOST
#define REDLAND_SETTING_STORAGE

◆ initializeBackend() [4/4]

bool knowrob::RedlandModel::initializeBackend ( const PropertyTree config)
overridevirtual

Initialize this backend from a property tree.

Parameters
configa property tree.
Returns
true on success

Implements knowrob::Storage.

◆ insertAll() [1/2]

bool RedlandModel::insertAll ( const TripleContainerPtr triples)
overridevirtual

Add assertions to this backend.

Parameters
triplesa set of triples.
Returns
true on success

Implements knowrob::Storage.

Definition at line 362 of file RedlandModel.cpp.

362  {
363  // insert all triples into an in-memory model.
364  // only after all triples are inserted, the model is transformed and then pushed to the next stage.
365  for (auto &knowrobTriple: *triples) {
366  auto raptorTriple = librdf_new_statement(world_);
367  // map the knowrob triple into a raptor triple
368  knowrobToRaptor(*knowrobTriple, raptorTriple);
369  librdf_model_context_add_statement(model_, getContextNode(*knowrobTriple), raptorTriple);
370  librdf_free_statement(raptorTriple);
371  }
372  return true;
373 }

◆ insertAll() [2/2]

bool knowrob::RedlandModel::insertAll ( const TripleContainerPtr triples)
overridevirtual

Add assertions to this backend.

Parameters
triplesa set of triples.
Returns
true on success

Implements knowrob::Storage.

◆ insertOne() [1/2]

bool RedlandModel::insertOne ( const Triple triple)
overridevirtual

Add an assertion to this backend.

Parameters
triplea triple.
Returns
true on success

Implements knowrob::Storage.

Definition at line 352 of file RedlandModel.cpp.

352  {
353  auto raptorTriple = librdf_new_statement(world_);
354  // map the knowrob triple into a raptor triple
355  knowrobToRaptor(knowrobTriple, raptorTriple);
356  // add the triple together with a context node holding the origin literal
357  librdf_model_context_add_statement(model_, getContextNode(knowrobTriple), raptorTriple);
358  librdf_free_statement(raptorTriple);
359  return true;
360 }

◆ insertOne() [2/2]

bool knowrob::RedlandModel::insertOne ( const Triple triple)
overridevirtual

Add an assertion to this backend.

Parameters
triplea triple.
Returns
true on success

Implements knowrob::Storage.

◆ isInitialized() [1/2]

bool RedlandModel::isInitialized ( ) const
Returns
true if the model is initialized.

Definition at line 117 of file RedlandModel.cpp.

117  {
118  return model_ != nullptr;
119 }

◆ isInitialized() [2/2]

bool knowrob::RedlandModel::isInitialized ( ) const
Returns
true if the model is initialized.

◆ isPersistent() [1/2]

bool RedlandModel::isPersistent ( ) const
overridevirtual
Returns
true if the backend is persistent.

Implements knowrob::QueryableStorage.

Definition at line 121 of file RedlandModel.cpp.

◆ isPersistent() [2/2]

bool knowrob::RedlandModel::isPersistent ( ) const
overridevirtual
Returns
true if the backend is persistent.

Implements knowrob::QueryableStorage.

◆ knowrobToRaptor() [1/6]

raptor_term * RedlandModel::knowrobToRaptor ( const TermPtr term)
protected

Definition at line 568 of file RedlandModel.cpp.

568  {
569  if (!term || !term->isAtomic()) {
570  return nullptr;
571  }
572  auto atomic = std::static_pointer_cast<Atomic>(term);
573  if (term->isIRI()) {
574  return KNOWROB_RDF_NEW_URI(atomic->stringForm());
575  } else if (term->isBlank()) {
576  return KNOWROB_RDF_NEW_BLANK(atomic->stringForm());
577  } else if (term->isString()) {
578  return KNOWROB_RDF_NEW_LITERAL(atomic->stringForm().data(), xsdURI(XSDType::STRING));
579  } else if (term->isNumeric()) {
580  auto numeric = std::static_pointer_cast<Numeric>(atomic);
581  return KNOWROB_RDF_NEW_LITERAL(atomic->stringForm().data(), xsdURI(numeric->xsdType()));
582  }
583  KB_WARN("Failed to convert term {} to raptor term.", *term);
584  return nullptr;
585 }
#define KNOWROB_RDF_NEW_BLANK(val)
#define KNOWROB_RDF_NEW_LITERAL(val, xsdType)
#define KNOWROB_RDF_NEW_URI(val)
RedlandURI & xsdURI(XSDType xsdType)
TermRule & atomic()
Definition: terms.cpp:79
TermRule & term()
Definition: terms.cpp:136

◆ knowrobToRaptor() [2/6]

raptor_term* knowrob::RedlandModel::knowrobToRaptor ( const TermPtr term)
protected

◆ knowrobToRaptor() [3/6]

void RedlandModel::knowrobToRaptor ( const Triple triple,
raptor_statement *  raptorTriple 
)
protected

Definition at line 603 of file RedlandModel.cpp.

603  {
604  raptor_term *subject, *predicate, *object;
606  if (triple.isSubjectBlank()) {
607  subject = KNOWROB_RDF_NEW_BLANK(triple.subject());
608  } else {
609  subject = KNOWROB_RDF_NEW_URI(triple.subject());
610  }
611  if (triple.isObjectBlank()) {
612  object = KNOWROB_RDF_NEW_BLANK(triple.valueAsString());
613  } else if (triple.isObjectIRI()) {
614  object = KNOWROB_RDF_NEW_URI(triple.valueAsString());
615  } else if (triple.xsdType()) {
616  switch (triple.xsdType().value()) {
617  case XSDType::STRING:
618  object = KNOWROB_RDF_NEW_LITERAL(triple.valueAsString().data(), xsdURI(triple.xsdType().value()));
619  break;
620  default:
621  object = KNOWROB_RDF_NEW_LITERAL(triple.createStringValue().c_str(), xsdURI(triple.xsdType().value()));
622  break;
623  }
624  } else {
626  }
627  librdf_statement_set_subject(raptorTriple, subject);
628  librdf_statement_set_predicate(raptorTriple, predicate);
629  librdf_statement_set_object(raptorTriple, object);
630 }
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
virtual std::string_view subject() const =0
bool isSubjectBlank() const
Definition: Triple.h:44
virtual std::string_view predicate() const =0
std::string createStringValue() const
Definition: Triple.cpp:71
bool isObjectIRI() const
Definition: Triple.h:54
bool isObjectBlank() const
Definition: Triple.h:49
PredicateRule & predicate()
Definition: formula.cpp:221

◆ knowrobToRaptor() [4/6]

void knowrob::RedlandModel::knowrobToRaptor ( const Triple triple,
raptor_statement *  raptorTriple 
)
protected

◆ knowrobToRaptor() [5/6]

void RedlandModel::knowrobToRaptor ( const TriplePattern pat,
raptor_statement *  raptorTriple 
)
protected

Definition at line 587 of file RedlandModel.cpp.

587  {
588  raptor_term *subject, *predicate, *object;
589  if (pat.subjectTerm()) {
590  subject = knowrobToRaptor(pat.subjectTerm());
591  librdf_statement_set_subject(raptorTriple, subject);
592  }
593  if (pat.propertyTerm()) {
595  librdf_statement_set_predicate(raptorTriple, predicate);
596  }
597  if (pat.objectTerm() && pat.objectOperator() == FilterType::EQ) {
598  object = knowrobToRaptor(pat.objectTerm());
599  librdf_statement_set_object(raptorTriple, object);
600  }
601 }
auto objectOperator() const
auto & propertyTerm() const
Definition: TriplePattern.h:92
auto & subjectTerm() const
Definition: TriplePattern.h:81
auto & objectTerm() const
Definition: TriplePattern.h:97

◆ knowrobToRaptor() [6/6]

void knowrob::RedlandModel::knowrobToRaptor ( const TriplePattern pat,
raptor_statement *  raptorTriple 
)
protected

◆ load() [1/2]

bool RedlandModel::load ( const URI uri,
semweb::TripleFormat  tripleFormat 
)

Load an URI into the model. Note that in case of SPARQL endpoints, this does not mean al data is copied into the model.

Parameters
urithe uri to load.
tripleFormatthe format of the triples.
Returns
true if the uri was loaded successfully.

Definition at line 297 of file RedlandModel.cpp.

297  {
298  static const char *modelName = nullptr;
299  auto rdf_uri = librdf_new_uri(
300  world_,
301  (const unsigned char *) uri().c_str());
302  int returnCode = librdf_model_load(
303  model_,
304  rdf_uri,
305  modelName,
306  tripleFormatMimeType(tripleFormat).data(),
307  nullptr);
308  librdf_free_uri(rdf_uri);
309  return returnCode == 0;
310 }
std::string_view tripleFormatMimeType(TripleFormat format)

◆ load() [2/2]

bool knowrob::RedlandModel::load ( const URI uri,
semweb::TripleFormat  tripleFormat 
)

Load an URI into the model. Note that in case of SPARQL endpoints, this does not mean al data is copied into the model.

Parameters
urithe uri to load.
tripleFormatthe format of the triples.
Returns
true if the uri was loaded successfully.

◆ match() [1/2]

void RedlandModel::match ( const TriplePattern query,
const TripleVisitor visitor 
)
overridevirtual
Parameters
querya framed triple pattern.
visitora function that is called for each matching framed triple.

Reimplemented from knowrob::QueryableStorage.

Definition at line 468 of file RedlandModel.cpp.

468  {
469  auto triples = std::make_shared<RaptorContainer>(1);
470  auto rdf_query = librdf_new_statement(world_);
471  knowrobToRaptor(query, rdf_query);
472 
473  auto stream = librdf_model_find_statements(model_, rdf_query);
474  while (!librdf_stream_end(stream)) {
475  auto rdf_statement = librdf_stream_get_object(stream);
476  triples->add(rdf_statement->subject, rdf_statement->predicate, rdf_statement->object);
477  auto &triple = *triples->begin();
478 
479  if (query.objectOperator() != FilterType::EQ) {
480  if (query.filter(*triple)) visitor(triple);
481  } else {
482  visitor(triple);
483  }
484  triples->reset();
485  librdf_stream_next(stream);
486  }
487  librdf_free_stream(stream);
488 }
bool query(const SPARQLQuery &query, const BindingsHandler &callback) const

◆ match() [2/2]

void knowrob::RedlandModel::match ( const TriplePattern query,
const TripleVisitor visitor 
)
overridevirtual
Parameters
querya framed triple pattern.
visitora function that is called for each matching framed triple.

Reimplemented from knowrob::QueryableStorage.

◆ removeAll() [1/2]

bool RedlandModel::removeAll ( const TripleContainerPtr triples)
overridevirtual

Delete all matching statements from this backend.

Parameters
triplesa set of triples.

Implements knowrob::Storage.

Definition at line 384 of file RedlandModel.cpp.

384  {
385  bool allRemoved = true;
386  for (auto &knowrobTriple: *triples) {
387  // map the knowrob triple into a raptor triple
388  auto raptorTriple = librdf_new_statement(world_);
389  knowrobToRaptor(*knowrobTriple, raptorTriple);
390  auto ret = librdf_model_context_remove_statement(model_, getContextNode(*knowrobTriple), raptorTriple);
391  if (ret != 0) {
392  allRemoved = false;
393  }
394  librdf_free_statement(raptorTriple);
395  }
396  return allRemoved;
397 }

◆ removeAll() [2/2]

bool knowrob::RedlandModel::removeAll ( const TripleContainerPtr triples)
overridevirtual

Delete all matching statements from this backend.

Parameters
triplesa set of triples.

Implements knowrob::Storage.

◆ removeAllWithOrigin() [1/2]

bool RedlandModel::removeAllWithOrigin ( std::string_view  origin)
overridevirtual

Delete all statements with a given origin from this backend.

Parameters
originthe origin of the statements to be deleted.

Implements knowrob::Storage.

Definition at line 399 of file RedlandModel.cpp.

399  {
400  auto stream = librdf_model_find_statements_in_context(
401  model_, nullptr, getContextNode(origin));
402  if (!stream) return false;
403  // collect matching statements
404  std::vector<librdf_statement *> toRemove;
405  while (!librdf_stream_end(stream)) {
406  auto next = librdf_stream_get_object(stream);
407  toRemove.push_back(librdf_new_statement_from_statement(next));
408  librdf_stream_next(stream);
409  }
410  librdf_free_stream(stream);
411  // finally remove matching statements
412  for (auto statement: toRemove) {
413  librdf_model_remove_statement(model_, statement);
414  librdf_free_statement(statement);
415  }
416  return true;
417 }

◆ removeAllWithOrigin() [2/2]

bool knowrob::RedlandModel::removeAllWithOrigin ( std::string_view  origin)
overridevirtual

Delete all statements with a given origin from this backend.

Parameters
originthe origin of the statements to be deleted.

Implements knowrob::Storage.

◆ removeOne() [1/2]

bool RedlandModel::removeOne ( const Triple triple)
overridevirtual

Delete the first matching statement from this backend.

Parameters
triplea triple.

Implements knowrob::Storage.

Definition at line 375 of file RedlandModel.cpp.

375  {
376  auto raptorTriple = librdf_new_statement(world_);
377  // map the knowrob triple into a raptor triple
378  knowrobToRaptor(knowrobTriple, raptorTriple);
379  auto ret = librdf_model_context_remove_statement(model_, getContextNode(knowrobTriple), raptorTriple);
380  librdf_free_statement(raptorTriple);
381  return ret == 0;
382 }

◆ removeOne() [2/2]

bool knowrob::RedlandModel::removeOne ( const Triple triple)
overridevirtual

Delete the first matching statement from this backend.

Parameters
triplea triple.

Implements knowrob::Storage.

◆ setDatabase() [1/2]

void RedlandModel::setDatabase ( std::string_view  database)
Parameters
databasethe database name.

Definition at line 142 of file RedlandModel.cpp.

142  {
143  REDLAND_REQUIRE_UNINITIALIZED("database", database_, database);
144 }
#define REDLAND_REQUIRE_UNINITIALIZED(name, field, val)

◆ setDatabase() [2/2]

void knowrob::RedlandModel::setDatabase ( std::string_view  database)
Parameters
databasethe database name.

◆ setHashesStorage() [1/2]

void RedlandModel::setHashesStorage ( RedlandHashType  hashType,
std::string_view  dir = "." 
)

Set the storage type to "hashes".

Parameters
hashTypethe hash type.
dirthe directory for the hashes storage.
See also
https://librdf.org/docs/api/redland-storage-module-hashes.html

Definition at line 162 of file RedlandModel.cpp.

162  {
164  setStorageHashType(hashType);
165  if (hashType == RedlandHashType::BDB) {
166  setStorageDirectory(dir);
167  }
168 }
void setStorageHashType(RedlandHashType hashType)
void setStorageType(RedlandStorageType storageType)
void setStorageDirectory(std::string_view dir=".")

◆ setHashesStorage() [2/2]

void knowrob::RedlandModel::setHashesStorage ( RedlandHashType  hashType,
std::string_view  dir = "." 
)

Set the storage type to "hashes".

Parameters
hashTypethe hash type.
dirthe directory for the hashes storage.
See also
https://librdf.org/docs/api/redland-storage-module-hashes.html

◆ setHost() [1/2]

void RedlandModel::setHost ( std::string_view  host)
Parameters
hostthe host of the database.

Definition at line 138 of file RedlandModel.cpp.

138  {
139  REDLAND_REQUIRE_UNINITIALIZED("host", host_, host);
140 }

◆ setHost() [2/2]

void knowrob::RedlandModel::setHost ( std::string_view  host)
Parameters
hostthe host of the database.

◆ setMemoryStorage() [1/2]

void RedlandModel::setMemoryStorage ( )

Set the storage type to "memory".

See also
https://librdf.org/docs/api/redland-storage-module-memory.html

Definition at line 170 of file RedlandModel.cpp.

◆ setMemoryStorage() [2/2]

void knowrob::RedlandModel::setMemoryStorage ( )

Set the storage type to "memory".

See also
https://librdf.org/docs/api/redland-storage-module-memory.html

◆ setOrigin() [1/2]

void knowrob::RedlandModel::setOrigin ( std::string_view  origin)
inline

If set assume triples in this backend have this as a default origin.

Parameters
originthe origin of the triples.

Definition at line 121 of file RedlandModel.h.

121 { origin_ = origin; }

◆ setOrigin() [2/2]

void knowrob::RedlandModel::setOrigin ( std::string_view  origin)
inline

If set assume triples in this backend have this as a default origin.

Parameters
originthe origin of the triples.

Definition at line 121 of file RedlandModel.h.

121 { origin_ = origin; }

◆ setPassword() [1/2]

void RedlandModel::setPassword ( std::string_view  password)
Parameters
passwordthe password for the database.

Definition at line 150 of file RedlandModel.cpp.

150  {
151  REDLAND_REQUIRE_UNINITIALIZED("password", password_, password);
152 }

◆ setPassword() [2/2]

void knowrob::RedlandModel::setPassword ( std::string_view  password)
Parameters
passwordthe password for the database.

◆ setRaptorWorld() [1/2]

void RedlandModel::setRaptorWorld ( librdf_world *  world)

If not set, the model will create its own.

Parameters
worlda redland world.

Definition at line 158 of file RedlandModel.cpp.

158  {
159  REDLAND_REQUIRE_UNINITIALIZED("raptor-world", world_, world);
160 }

◆ setRaptorWorld() [2/2]

void knowrob::RedlandModel::setRaptorWorld ( librdf_world *  world)

If not set, the model will create its own.

Parameters
worlda redland world.

◆ setStorageDirectory() [1/2]

void RedlandModel::setStorageDirectory ( std::string_view  dir = ".")
Parameters
dirthe directory for the storage.

Definition at line 154 of file RedlandModel.cpp.

154  {
156 }

◆ setStorageDirectory() [2/2]

void knowrob::RedlandModel::setStorageDirectory ( std::string_view  dir = ".")
Parameters
dirthe directory for the storage.

◆ setStorageHashType() [1/2]

void RedlandModel::setStorageHashType ( RedlandHashType  hashType)

Set the "hash-type" parameter of the storage.

Parameters
hashTypethe hash type.

Definition at line 134 of file RedlandModel.cpp.

134  {
135  REDLAND_REQUIRE_UNINITIALIZED("hash-type", hashType_, hashType);
136 }

◆ setStorageHashType() [2/2]

void knowrob::RedlandModel::setStorageHashType ( RedlandHashType  hashType)

Set the "hash-type" parameter of the storage.

Parameters
hashTypethe hash type.

◆ setStorageType() [1/2]

void RedlandModel::setStorageType ( RedlandStorageType  storageType)

Set the storage type.

Parameters
storageTypethe storage type.

Definition at line 130 of file RedlandModel.cpp.

130  {
132 }

◆ setStorageType() [2/2]

void knowrob::RedlandModel::setStorageType ( RedlandStorageType  storageType)

Set the storage type.

Parameters
storageTypethe storage type.

◆ setUser() [1/2]

void RedlandModel::setUser ( std::string_view  user)
Parameters
userthe user name for the database.

Definition at line 146 of file RedlandModel.cpp.

146  {
147  REDLAND_REQUIRE_UNINITIALIZED("user", user_, user);
148 }

◆ setUser() [2/2]

void knowrob::RedlandModel::setUser ( std::string_view  user)
Parameters
userthe user name for the database.

◆ sparql() [1/2]

bool RedlandModel::sparql ( std::string_view  queryString,
const BindingsHandler callback 
) const
overridevirtual

Run a SPARQL query on the model.

Parameters
queryStringthe query to run.
callbackthe callback to handle the results.
Returns
true if the query was successful.

Implements knowrob::SPARQLBackend.

Definition at line 490 of file RedlandModel.cpp.

490  {
491  auto queryObj = librdf_new_query(
492  world_,
493  QUERY_LANGUAGE_SPARQL.data(),
494  nullptr,
495  (unsigned char *) queryString.data(),
496  nullptr);
497  if (!queryObj) {
498  KB_WARN("Failed to create query `{}` for model \"{}\".", queryString, storageName_);
499  return false;
500  }
501  auto results = librdf_query_execute(queryObj, model_);
502  if (!results) {
503  KB_WARN("Failed to execute query `{}` for model \"{}\".", queryString, storageName_);
504  librdf_free_query(queryObj);
505  return false;
506  }
507  while (!librdf_query_results_finished(results)) {
508  auto bindings = std::make_shared<Bindings>();
509 
510  // read bindings
511  int bindings_count = librdf_query_results_get_bindings_count(results);
512  for (int i = 0; i < bindings_count; ++i) {
513  auto name = std::string_view(librdf_query_results_get_binding_name(results, i));
514  if (name.empty() || name[0] == '_') {
515  // skip variables that start with '_'.
516  // afaik, there is no way to prevent these from being returned when "*" is
517  // used in select pattern.
518  continue;
519  }
520 
521  auto node = librdf_query_results_get_binding_value(results, i);
522  if (!node) {
523  // this indicates that a variable appears in select pattern, but it was not grounded
524  // in the query. this is not an error, but rather means the grounding was OPTIONAL.
525  // we can just skip this variable.
526  continue;
527  }
528 
529  auto knowrobTerm = termFromNode(node);
530  if (knowrobTerm) {
531  bindings->set(std::make_shared<Variable>(name), knowrobTerm);
532  } else {
533  KB_WARN("Failed to process binding for variable \"{}\".", name);
534  }
535  librdf_free_node(node);
536  }
537  callback(bindings);
538  librdf_query_results_next(results);
539  }
540  librdf_free_query_results(results);
541  librdf_free_query(queryObj);
542  return true;
543 }
static constexpr std::string_view QUERY_LANGUAGE_SPARQL
Definition: RedlandModel.h:54
std::string storageName_
Definition: RedlandModel.h:198

◆ sparql() [2/2]

bool knowrob::RedlandModel::sparql ( std::string_view  queryString,
const BindingsHandler callback 
) const
overridevirtual

Run a SPARQL query on the model.

Parameters
queryStringthe query to run.
callbackthe callback to handle the results.
Returns
true if the query was successful.

Implements knowrob::SPARQLBackend.

◆ storageType() [1/2]

auto knowrob::RedlandModel::storageType ( ) const
inline
Returns
the redland storage type of the model.

Definition at line 126 of file RedlandModel.h.

126 { return storageType_; }

◆ storageType() [2/2]

auto knowrob::RedlandModel::storageType ( ) const
inline
Returns
the redland storage type of the model.

Definition at line 126 of file RedlandModel.h.

126 { return storageType_; }

◆ xsdURI() [1/2]

RedlandURI & RedlandModel::xsdURI ( XSDType  xsdType)
protected

Definition at line 564 of file RedlandModel.cpp.

564  {
565  return xsdURIs_[static_cast<int>(xsdType)];
566 }

◆ xsdURI() [2/2]

RedlandURI& knowrob::RedlandModel::xsdURI ( XSDType  xsdType)
protected

Member Data Documentation

◆ contextNodes_

std::map< std::string, librdf_node *, std::less<> > knowrob::RedlandModel::contextNodes_
protected

Definition at line 196 of file RedlandModel.h.

◆ database_

std::optional< std::string > knowrob::RedlandModel::database_
protected

Definition at line 203 of file RedlandModel.h.

◆ hashType_

std::optional< RedlandHashType > knowrob::RedlandModel::hashType_
protected

Definition at line 207 of file RedlandModel.h.

◆ host_

std::optional< std::string > knowrob::RedlandModel::host_
protected

Definition at line 201 of file RedlandModel.h.

◆ model_

librdf_model * knowrob::RedlandModel::model_
protected

Definition at line 187 of file RedlandModel.h.

◆ origin_

std::optional< std::string > knowrob::RedlandModel::origin_
protected

Definition at line 200 of file RedlandModel.h.

◆ ownedWorld_

librdf_world * knowrob::RedlandModel::ownedWorld_
protected

Definition at line 185 of file RedlandModel.h.

◆ password_

std::optional< std::string > knowrob::RedlandModel::password_
protected

Definition at line 205 of file RedlandModel.h.

◆ port_

std::optional< std::string > knowrob::RedlandModel::port_
protected

Definition at line 202 of file RedlandModel.h.

◆ QUERY_LANGUAGE_SPARQL

static constexpr std::string_view knowrob::RedlandModel::QUERY_LANGUAGE_SPARQL = "sparql"
staticconstexpr

Definition at line 54 of file RedlandModel.h.

◆ storage_

librdf_storage * knowrob::RedlandModel::storage_
protected

Definition at line 188 of file RedlandModel.h.

◆ storageDir_

std::optional< std::string > knowrob::RedlandModel::storageDir_
protected

Definition at line 206 of file RedlandModel.h.

◆ storageName_

std::string knowrob::RedlandModel::storageName_
protected

Definition at line 198 of file RedlandModel.h.

◆ storageOptions_

std::string knowrob::RedlandModel::storageOptions_
protected

Definition at line 209 of file RedlandModel.h.

◆ storageType_

RedlandStorageType knowrob::RedlandModel::storageType_
protected

Definition at line 208 of file RedlandModel.h.

◆ user_

std::optional< std::string > knowrob::RedlandModel::user_
protected

Definition at line 204 of file RedlandModel.h.

◆ world_

librdf_world * knowrob::RedlandModel::world_
protected

Definition at line 186 of file RedlandModel.h.

◆ xsdURIs_

RedlandURI knowrob::RedlandModel::xsdURIs_
protected

Definition at line 192 of file RedlandModel.h.


The documentation for this class was generated from the following files: