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

#include <QueryableStorage.h>

Inheritance diagram for knowrob::QueryableStorage:
Collaboration diagram for knowrob::QueryableStorage:

Public Member Functions

 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)
 
virtual void query (const GraphQueryPtr &query, const BindingsHandler &callback)=0
 
virtual void count (const ResourceCounter &callback) const =0
 
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)
 
virtual void query (const GraphQueryPtr &query, const BindingsHandler &callback)=0
 
virtual void count (const ResourceCounter &callback) const =0
 
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
 

Static Public Member Functions

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

static AtomPtr versionProperty = IRIAtom::Tabled("http://knowrob.org/kb/knowrob.owl#hasVersionOfOrigin")
 

Additional Inherited Members

- 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 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_
 

Detailed Description

A backend that can be queried.

Definition at line 25 of file QueryableStorage.h.

Constructor & Destructor Documentation

◆ QueryableStorage() [1/2]

QueryableStorage::QueryableStorage ( StorageFeatures  features = StorageFeature::NothingSpecial)
explicit

Definition at line 20 of file QueryableStorage.cpp.

21  : Storage(features) {
22 }
Storage(StorageFeatures features=StorageFeature::NothingSpecial)
Definition: Storage.h:61

◆ ~QueryableStorage() [1/2]

knowrob::QueryableStorage::~QueryableStorage ( )
overridedefault

◆ QueryableStorage() [2/2]

knowrob::QueryableStorage::QueryableStorage ( StorageFeatures  features = StorageFeature::NothingSpecial)
explicit

◆ ~QueryableStorage() [2/2]

knowrob::QueryableStorage::~QueryableStorage ( )
overridedefault

Member Function Documentation

◆ batch() [1/2]

virtual void knowrob::QueryableStorage::batch ( const TripleHandler callback) const
pure virtual

Iterate over all triples in the model.

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

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

◆ batch() [2/2]

virtual void knowrob::QueryableStorage::batch ( const TripleHandler callback) const
pure virtual

Iterate over all triples in the model.

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

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

◆ batchOrigin() [1/2]

virtual void knowrob::QueryableStorage::batchOrigin ( std::string_view  origin,
const TripleHandler callback 
)
pure virtual

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.

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

◆ batchOrigin() [2/2]

virtual void knowrob::QueryableStorage::batchOrigin ( std::string_view  origin,
const TripleHandler callback 
)
pure virtual

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.

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

◆ contains() [1/2]

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

Reimplemented in knowrob::RedlandModel, and knowrob::RedlandModel.

Definition at line 83 of file QueryableStorage.cpp.

83  {
84  bool hasTriple = false;
85  match(TriplePattern(triple), [&hasTriple](const TriplePtr &) {
86  hasTriple = true;
87  });
88  return hasTriple;
89 }
virtual void match(const TriplePattern &query, const TripleVisitor &visitor)

◆ contains() [2/2]

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

Reimplemented in knowrob::RedlandModel, and knowrob::RedlandModel.

◆ count() [1/2]

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

Implemented in knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, knowrob::PrologBackend, knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, and knowrob::PrologBackend.

◆ count() [2/2]

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

Implemented in knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, knowrob::PrologBackend, knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, and knowrob::PrologBackend.

◆ dropSessionOrigins() [1/2]

void QueryableStorage::dropSessionOrigins ( )

Delete triples that have been asserted from a "session" origin.

Definition at line 52 of file QueryableStorage.cpp.

52  {
56 }
static constexpr std::string_view ORIGIN_SESSION
static constexpr std::string_view ORIGIN_USER
static constexpr std::string_view ORIGIN_REASONER
virtual bool removeAllWithOrigin(std::string_view origin)=0

◆ dropSessionOrigins() [2/2]

void knowrob::QueryableStorage::dropSessionOrigins ( )

Delete triples that have been asserted from a "session" origin.

◆ expand() [1/2]

GraphQueryExpansionPtr QueryableStorage::expand ( const GraphQueryPtr q)

Compute the expansion of a graph path query.

Parameters
qa graph path query.
Returns
the expansion of the query.

Definition at line 362 of file QueryableStorage.cpp.

362  {
363  // Expand the query. Currently, this is mainly used to compute the answer frame here.
364  // But also to insert some builtins for occasional triples.
365  auto exp_ctx = std::make_shared<GraphQueryExpansion>();
366  exp_ctx->query_ctx = q->ctx();
367  exp_ctx->with_reassignment = supports(StorageFeature::ReAssignment);
368  exp_ctx->expanded = expand_query(q, *exp_ctx);
369  return exp_ctx;
370 }
bool supports(StorageFeature feature) const
Definition: Storage.h:84

◆ expand() [2/2]

GraphQueryExpansionPtr knowrob::QueryableStorage::expand ( const GraphQueryPtr q)

Compute the expansion of a graph path query.

Parameters
qa graph path query.
Returns
the expansion of the query.

◆ foreach() [1/2]

void QueryableStorage::foreach ( const TripleVisitor visitor) const
virtual

Iterate over all triples in the model.

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

Reimplemented in knowrob::MongoKnowledgeGraph, and knowrob::MongoKnowledgeGraph.

Definition at line 91 of file QueryableStorage.cpp.

91  {
92  batch([&](const TripleContainerPtr &container) {
93  for (auto &triple: *container) {
94  visitor(triple);
95  }
96  });
97 }
virtual void batch(const TripleHandler &callback) const =0
std::shared_ptr< TripleContainer > TripleContainerPtr

◆ foreach() [2/2]

virtual void knowrob::QueryableStorage::foreach ( const TripleVisitor visitor) const
virtual

Iterate over all triples in the model.

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

Reimplemented in knowrob::MongoKnowledgeGraph, and knowrob::MongoKnowledgeGraph.

◆ getOrigins() [1/2]

std::vector< VersionedOriginPtr > QueryableStorage::getOrigins ( )
Returns
a list of all origins that have been asserted.

Definition at line 24 of file QueryableStorage.cpp.

24  {
25  static auto v_origin = std::make_shared<Variable>("Origin");
26  static auto v_version = std::make_shared<Variable>("Version");
27  std::vector<VersionedOriginPtr> origins;
28  match(TriplePattern(v_origin, versionProperty, v_version),
29  [&](const TriplePtr &triple) {
30  origins.push_back(std::make_shared<VersionedOrigin>(triple->subject(), triple->valueAsString()));
31  });
32  return origins;
33 }
static AtomPtr versionProperty
virtual std::string_view valueAsString() const =0
virtual std::string_view subject() const =0

◆ getOrigins() [2/2]

std::vector<VersionedOriginPtr> knowrob::QueryableStorage::getOrigins ( )
Returns
a list of all origins that have been asserted.

◆ getVersionOfOrigin() [1/2]

std::optional< std::string > QueryableStorage::getVersionOfOrigin ( std::string_view  origin)
Parameters
originan origin string.
Returns
the version of the origin, or an empty optional if the origin is unknown.

Definition at line 44 of file QueryableStorage.cpp.

44  {
45  static auto v_version = std::make_shared<Variable>("Version");
46  std::optional<std::string> version;
47  match(TriplePattern(std::make_shared<Atom>(origin), versionProperty, v_version),
48  [&](const TriplePtr &triple) { version = triple->createStringValue(); });
49  return version;
50 }
std::string createStringValue() const
Definition: Triple.cpp:71

◆ getVersionOfOrigin() [2/2]

std::optional<std::string> knowrob::QueryableStorage::getVersionOfOrigin ( std::string_view  origin)
Parameters
originan origin string.
Returns
the version of the origin, or an empty optional if the origin is unknown.

◆ isPersistent() [1/2]

virtual bool knowrob::QueryableStorage::isPersistent ( ) const
pure virtual

◆ isPersistent() [2/2]

virtual bool knowrob::QueryableStorage::isPersistent ( ) const
pure virtual

◆ match() [1/2]

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

Reimplemented in knowrob::RedlandModel, knowrob::MongoKnowledgeGraph, knowrob::RedlandModel, and knowrob::MongoKnowledgeGraph.

Definition at line 68 of file QueryableStorage.cpp.

68  {
69  auto graph_query = std::make_shared<GraphQuery>(
70  std::make_shared<GraphPattern>(std::make_shared<TriplePattern>(q)),
72  query(graph_query, [&](const BindingsPtr &bindings) {
73  TriplePtr triplePtr;
74  // create a triple view that holds a reference to the bindings.
75  // this is done to allow the visitor to take over the ownership of the triple.
76  triplePtr.ptr = new TripleView_withBindings(bindings);
77  triplePtr.owned = true;
78  q.instantiateInto(*triplePtr, bindings);
79  visitor(triplePtr);
80  });
81 }
virtual void query(const GraphQueryPtr &query, const BindingsHandler &callback)=0
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151
QueryContextPtr DefaultQueryContext()
Definition: Query.cpp:11
Triple * ptr
Definition: Triple.h:590

◆ match() [2/2]

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

Reimplemented in knowrob::RedlandModel, knowrob::MongoKnowledgeGraph, knowrob::RedlandModel, and knowrob::MongoKnowledgeGraph.

◆ no() [1/2]

std::shared_ptr< AnswerNo > QueryableStorage::no ( const GraphPathQueryPtr q)
static

Generates a negative answer to a query.

Parameters
qa graph path query.
Returns
a negative answer to the query.

Definition at line 137 of file QueryableStorage.cpp.

137  {
138  static const auto edbTerm = Atom::Tabled("EDB");
139 
140  // send one negative answer if no positive answer was found
141  auto negativeAnswer = std::make_shared<AnswerNo>();
142  negativeAnswer->setReasonerTerm(edbTerm);
143  // Apply query context "origin" and "perspective" to the answer if any
144  negativeAnswer->applyFrame(q->ctx()->selector);
145  // the answer is uncertain as we only were not able to obtain a positive answer
146  // which does not mean that there is no positive answer.
147  negativeAnswer->setIsUncertain(true, std::nullopt);
148 
149  // Add ungrounded literals to negative answer.
150  // But at the moment the information is not provided by EDBs, would be difficult to implement e.g. in MongoDB.
151  // Well at least we know if the query is only a single triple pattern.
152  if (q->path().size() == 1) {
153  negativeAnswer->addUngrounded(q->path().front()->predicate(),
154  q->path().front()->isNegated());
155  }
156  return negativeAnswer;
157 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40

◆ no() [2/2]

static std::shared_ptr<AnswerNo> knowrob::QueryableStorage::no ( const GraphPathQueryPtr q)
static

Generates a negative answer to a query.

Parameters
qa graph path query.
Returns
a negative answer to the query.

◆ query() [1/2]

virtual void knowrob::QueryableStorage::query ( const GraphQueryPtr query,
const BindingsHandler callback 
)
pure virtual

Submits a graph query to this storage.

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

Implemented in knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, knowrob::PrologBackend, knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, and knowrob::PrologBackend.

◆ query() [2/2]

virtual void knowrob::QueryableStorage::query ( const GraphQueryPtr query,
const BindingsHandler callback 
)
pure virtual

Submits a graph query to this storage.

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

Implemented in knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, knowrob::PrologBackend, knowrob::SPARQLBackend, knowrob::MongoKnowledgeGraph, and knowrob::PrologBackend.

◆ setVersionOfOrigin() [1/2]

void QueryableStorage::setVersionOfOrigin ( std::string_view  origin,
std::string_view  version 
)

Set the version of an origin.

Parameters
originan origin string.
versiona version string.

Definition at line 35 of file QueryableStorage.cpp.

35  {
36  TripleView triple;
37  triple.setSubject(origin);
38  triple.setPredicate(versionProperty->stringForm());
39  triple.setStringValue(version);
40  triple.setGraph(origin);
41  insertOne(triple);
42 }
virtual bool insertOne(const Triple &triple)=0
void setStringValue(std::string_view v) override
Definition: Triple.h:464
void setPredicate(std::string_view predicate) override
Definition: Triple.h:449
void setGraph(std::string_view graph) override
Definition: Triple.h:497
void setSubject(std::string_view subject) override
Definition: Triple.h:446

◆ setVersionOfOrigin() [2/2]

void knowrob::QueryableStorage::setVersionOfOrigin ( std::string_view  origin,
std::string_view  version 
)

Set the version of an origin.

Parameters
originan origin string.
versiona version string.

◆ yes() [1/2]

std::shared_ptr< AnswerYes > QueryableStorage::yes ( const GraphPathQueryPtr original,
const GraphQueryExpansionPtr expansion,
const BindingsPtr bindings 
)
static

Generates a positive answer to a query.

Parameters
originala graph path query.
expansiona graph query expansion.
bindingsa set of bindings.
Returns
a positive answer to the query.

Definition at line 99 of file QueryableStorage.cpp.

101  {
102  static const auto edbTerm = Atom::Tabled("EDB");
103 
104  auto positiveAnswer = std::make_shared<AnswerYes>(bindings);
105  // Indicate that EDB has computed the grounding.
106  positiveAnswer->setReasonerTerm(edbTerm);
107  // Apply query context to the answer for some parameters.
108  positiveAnswer->applyFrame(original->ctx()->selector);
109 
110  // Add predicate groundings to the answer
111  for (auto &rdfLiteral: original->path()) {
112  auto p = rdfLiteral->predicate();
113  auto p_instance = applyBindings(p, *positiveAnswer->substitution());
114  positiveAnswer->addGrounding(
115  std::static_pointer_cast<Predicate>(p_instance),
116  rdfLiteral->isNegated(),
117  positiveAnswer->frame());
118  }
119 
120  // The answer is uncertain if any of the groundings is uncertain.
121  positiveAnswer->setIsUncertain(
122  std::any_of(expanded->u_vars.begin(), expanded->u_vars.end(), [&](const VariablePtr &v) {
123  auto &u_v = bindings->get(v->name());
124  return (u_v && u_v->isNumeric() && std::static_pointer_cast<Numeric>(u_v)->asBoolean());
125  }), std::nullopt);
126 
127  // The answer is occasional if any of the groundings has occasional=true flag
128  positiveAnswer->setIsOccasionallyTrue(
129  std::any_of(expanded->o_vars.begin(), expanded->o_vars.end(), [&](const VariablePtr &v) {
130  auto &o_v = bindings->get(v->name());
131  return (o_v && o_v->isNumeric() && std::static_pointer_cast<Numeric>(o_v)->asBoolean());
132  }));
133 
134  return positiveAnswer;
135 }
std::shared_ptr< Variable > VariablePtr
Definition: Variable.h:60
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)

◆ yes() [2/2]

static std::shared_ptr<AnswerYes> knowrob::QueryableStorage::yes ( const GraphPathQueryPtr original,
const GraphQueryExpansionPtr expansion,
const BindingsPtr bindings 
)
static

Generates a positive answer to a query.

Parameters
originala graph path query.
expansiona graph query expansion.
bindingsa set of bindings.
Returns
a positive answer to the query.

Member Data Documentation

◆ versionProperty

AtomPtr QueryableStorage::versionProperty = IRIAtom::Tabled("http://knowrob.org/kb/knowrob.owl#hasVersionOfOrigin")
static

Definition at line 27 of file QueryableStorage.h.


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