knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::SPARQLBackend Class Referenceabstract

#include <SPARQLBackend.h>

Inheritance diagram for knowrob::SPARQLBackend:
Collaboration diagram for knowrob::SPARQLBackend:

Public Member Functions

 SPARQLBackend (SPARQLFlags flags)
 
virtual bool sparql (std::string_view queryString, const BindingsHandler &callback) const =0
 
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)
 
virtual bool sparql (std::string_view queryString, const BindingsHandler &callback) const =0
 
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 bool isPersistent () const =0
 
virtual void foreach (const TripleVisitor &visitor) const
 
virtual void batch (const TripleHandler &callback) const =0
 
virtual void batchOrigin (std::string_view origin, const TripleHandler &callback)=0
 
virtual bool contains (const Triple &triple)
 
virtual void match (const TriplePattern &query, const TripleVisitor &visitor)
 
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 bool isPersistent () const =0
 
virtual void foreach (const TripleVisitor &visitor) const
 
virtual void batch (const TripleHandler &callback) const =0
 
virtual void batchOrigin (std::string_view origin, const TripleHandler &callback)=0
 
virtual bool contains (const Triple &triple)
 
virtual void match (const TriplePattern &query, const TripleVisitor &visitor)
 
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
 
virtual bool insertOne (const Triple &triple)=0
 
virtual bool insertAll (const TripleContainerPtr &triples)=0
 
virtual bool removeOne (const Triple &triple)=0
 
virtual bool removeAll (const TripleContainerPtr &triples)=0
 
virtual bool removeAllWithOrigin (std::string_view origin)=0
 
virtual bool initializeBackend (const PropertyTree &config)=0
 
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
 
virtual bool insertOne (const Triple &triple)=0
 
virtual bool insertAll (const TripleContainerPtr &triples)=0
 
virtual bool removeOne (const Triple &triple)=0
 
virtual bool removeAll (const TripleContainerPtr &triples)=0
 
virtual bool removeAllWithOrigin (std::string_view origin)=0
 
virtual bool initializeBackend (const PropertyTree &config)=0
 
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
 

Protected Attributes

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)
 
- Static Public Attributes inherited from knowrob::QueryableStorage
static AtomPtr versionProperty = IRIAtom::Tabled("http://knowrob.org/kb/knowrob.owl#hasVersionOfOrigin")
 
- 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 &)
 

Detailed Description

A backend that implements querying via SPARQL.

Definition at line 16 of file SPARQLBackend.h.

Constructor & Destructor Documentation

◆ SPARQLBackend() [1/2]

knowrob::SPARQLBackend::SPARQLBackend ( SPARQLFlags  flags)
inlineexplicit

Definition at line 18 of file SPARQLBackend.h.

18 : sparqlFlags_(flags) {}
SPARQLFlags sparqlFlags_
Definition: SPARQLBackend.h:43

◆ SPARQLBackend() [2/2]

knowrob::SPARQLBackend::SPARQLBackend ( SPARQLFlags  flags)
inlineexplicit

Definition at line 18 of file SPARQLBackend.h.

18 : sparqlFlags_(flags) {}

Member Function Documentation

◆ count() [1/2]

void SPARQLBackend::count ( const ResourceCounter callback) const
overridevirtual
Parameters
callbacka function that is called for each resource and its count.

Implements knowrob::QueryableStorage.

Definition at line 50 of file SPARQLBackend.cpp.

50  {
51  static const char *sparqlString = "SELECT ?resource (COUNT(?s) AS ?count) WHERE "\
52  "{ ?s rdf:type ?resource . } UNION { ?s ?resource ?o . } } "\
53  "GROUP BY ?resource";
54  sparql(sparqlString, [&](const BindingsPtr &bindings) {
55  auto resource = bindings->getAtomic("resource");
56  auto count = bindings->getAtomic("count");
57  if (!resource || !count || !count->isNumeric()) {
58  KB_WARN("Failed to count triples!");
59  return;
60  }
61  callback(resource->stringForm(), std::static_pointer_cast<Numeric>(count)->asLong());
62  });
63 }
void count(const ResourceCounter &callback) const override
virtual bool sparql(std::string_view queryString, const BindingsHandler &callback) const =0
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151

◆ count() [2/2]

void knowrob::SPARQLBackend::count ( const ResourceCounter callback) const
overridevirtual
Parameters
callbacka function that is called for each resource and its count.

Implements knowrob::QueryableStorage.

◆ query() [1/4]

void SPARQLBackend::query ( const GraphQueryPtr query,
const BindingsHandler callback 
)
overridevirtual

Submits a graph query to this storage.

Parameters
querya graph query
callbacka function that is called for each answer to the query.

Implements knowrob::QueryableStorage.

Definition at line 14 of file SPARQLBackend.cpp.

14  { // NOLINT
15  std::shared_ptr<GraphPattern> negatedPattern;
16  if (q->term()->termType() == GraphTermType::Pattern) {
17  // Currently we cannot support negations where the object is grounded.
18  // This is because the SPARQL query would need to use NOT-EXISTS or MINUS, which is not supported by rasqal.
19  // Instead, we use OPTIONAL and !BOUND to simulate negation which requires that the object is not grounded
20  // when entering the negated pattern.
21  // But, on the other hand negations are handled separately by the KnowledgeBase anyway.
22  // So KnowRob will at the moment only call this interface with single negated patterns
23  // while evaluating sequences of negations in parallel and after any positive pattern that
24  // appears in a query.
25  auto pat = std::static_pointer_cast<GraphPattern>(q->term());
26  if (pat->value()->isNegated()) {
27  negatedPattern = pat;
28  }
29  }
30 
31  if (negatedPattern) {
32  // negation-as-failure: Try positive query, if it has no solution, then the negated pattern is true.
33  auto positivePat = std::make_shared<TriplePattern>(*negatedPattern->value());
34  positivePat->setIsNegated(false);
35  auto positiveQuery = std::make_shared<GraphQuery>(
36  std::make_shared<GraphPattern>(positivePat),
38  bool hasSolution = false;
39  query(positiveQuery, [&](const BindingsPtr& /*bindings*/) {
40  hasSolution = true;
41  });
42  if (!hasSolution) {
43  callback(Bindings::emptyBindings());
44  }
45  } else {
46  sparql(SPARQLQuery(q, sparqlFlags_)(), callback);
47  }
48 }
static std::shared_ptr< const Bindings > emptyBindings()
Definition: Bindings.cpp:99
bool query(const SPARQLQuery &query, const BindingsHandler &callback) const
QueryContextPtr OneSolutionContext()
Definition: Query.cpp:15

◆ query() [2/4]

void knowrob::SPARQLBackend::query ( const GraphQueryPtr query,
const BindingsHandler callback 
)
overridevirtual

Submits a graph query to this storage.

Parameters
querya graph query
callbacka function that is called for each answer to the query.

Implements knowrob::QueryableStorage.

◆ query() [3/4]

bool SPARQLBackend::query ( const SPARQLQuery query,
const BindingsHandler callback 
) const

Run a SPARQL query on the model.

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

Definition at line 10 of file SPARQLBackend.cpp.

10  {
11  return sparql(q(), callback);
12 }

◆ query() [4/4]

bool knowrob::SPARQLBackend::query ( const SPARQLQuery query,
const BindingsHandler callback 
) const

Run a SPARQL query on the model.

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

◆ sparql() [1/2]

virtual bool knowrob::SPARQLBackend::sparql ( std::string_view  queryString,
const BindingsHandler callback 
) const
pure virtual

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.

Implemented in knowrob::RedlandModel, and knowrob::RedlandModel.

◆ sparql() [2/2]

virtual bool knowrob::SPARQLBackend::sparql ( std::string_view  queryString,
const BindingsHandler callback 
) const
pure virtual

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.

Implemented in knowrob::RedlandModel, and knowrob::RedlandModel.

Member Data Documentation

◆ sparqlFlags_

SPARQLFlags knowrob::SPARQLBackend::sparqlFlags_
protected

Definition at line 43 of file SPARQLBackend.h.


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