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

#include <PrologBackend.h>

Inheritance diagram for knowrob::PrologBackend:
Collaboration diagram for knowrob::PrologBackend:

Public Member Functions

 PrologBackend ()
 
 ~PrologBackend () override=default
 
bool initializeBackend ()
 
bool initializeBackend (const PropertyTree &cfg) 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
 
void batch (const TripleHandler &callback) const override
 
void batchOrigin (std::string_view origin, const TripleHandler &callback) override
 
void query (const GraphQueryPtr &query, const BindingsHandler &callback) override
 
void count (const ResourceCounter &callback) const override
 
 PrologBackend ()
 
 ~PrologBackend () override=default
 
bool initializeBackend ()
 
bool initializeBackend (const PropertyTree &cfg) 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
 
void batch (const TripleHandler &callback) const override
 
void batchOrigin (std::string_view origin, const TripleHandler &callback) override
 
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
 
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 void foreach (const TripleVisitor &visitor) const
 
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
 
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 Member Functions

static bool removeAll ()
 
static bool removeAll ()
 
- 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 Protected Member Functions

static PrologTerm transaction (std::string_view rdf_functor, const TripleContainerPtr &triples)
 
static PrologTerm transaction (std::string_view rdf_functor, const TripleContainerPtr &triples)
 

Additional Inherited Members

- 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 &)
 
- 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

The triple store backend for Prolog reasoners. The backend is a singleton, and it is not possible to have multiple instances of it. The reason is that Prolog reasoners do not support multiple EDBs due to limitations of the underlying Prolog "semweb" library which only has a global storage.

Definition at line 19 of file PrologBackend.h.

Constructor & Destructor Documentation

◆ PrologBackend() [1/2]

PrologBackend::PrologBackend ( )

Definition at line 26 of file PrologBackend.cpp.

28 }
QueryableStorage(StorageFeatures features=StorageFeature::NothingSpecial)

◆ ~PrologBackend() [1/2]

knowrob::PrologBackend::~PrologBackend ( )
overridedefault

◆ PrologBackend() [2/2]

knowrob::PrologBackend::PrologBackend ( )

◆ ~PrologBackend() [2/2]

knowrob::PrologBackend::~PrologBackend ( )
overridedefault

Member Function Documentation

◆ batch() [1/2]

void PrologBackend::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 164 of file PrologBackend.cpp.

164  {
165  static groundable<Atom> origin_g(std::make_shared<Variable>("g"));
166  batch_(callback, origin_g);
167 }

◆ batch() [2/2]

void knowrob::PrologBackend::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 PrologBackend::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 169 of file PrologBackend.cpp.

169  {
170  groundable<Atom> origin_g(Atom::Tabled(origin));
171  batch_(callback, groundable<Atom>(origin_g));
172 }
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40

◆ batchOrigin() [2/2]

void knowrob::PrologBackend::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.

◆ count() [1/2]

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

Implements knowrob::QueryableStorage.

Definition at line 178 of file PrologBackend.cpp.

178  {
179  // :- sw_resource_frequency(-Resource, -Frequency)
180  static const auto freq_f = "sw_resource_frequency";
181  static auto var_res = std::make_shared<Variable>("resource");
182  static auto var_freq = std::make_shared<Variable>("frequency");
183 
184  PROLOG_ENGINE_QUERY(PrologTerm(freq_f, var_res, var_freq), [&callback](const BindingsPtr &bindings) {
185  auto val_res = bindings->getAtomic(var_res->name());
186  auto val_freq = bindings->getAtomic(var_freq->name());
187  if (val_res && val_freq && val_freq->isNumeric()) {
188  callback(val_res->stringForm(), std::static_pointer_cast<Numeric>(val_freq)->asInteger());
189  }
190  });
191 }
#define PROLOG_ENGINE_QUERY(term, callback)
Definition: PrologEngine.h:131
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151

◆ count() [2/2]

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

Implements knowrob::QueryableStorage.

◆ initializeBackend() [1/4]

bool PrologBackend::initializeBackend ( )

Definition at line 30 of file PrologBackend.cpp.

30  {
32  auto success = PROLOG_ENGINE_EVAL(
33  PrologTerm("use_module",
34  PrologTerm("library", "semweb/rdf_db"),
35  PrologList({
36  PrologTerm("/", "rdf", std::make_shared<Integer>(4)),
37  PrologTerm("/", "rdf_assert", std::make_shared<Integer>(4)),
38  PrologTerm("/", "rdf_retractall", std::make_shared<Integer>(4)),
39  PrologTerm("/", "rdf_transaction", std::make_shared<Integer>(1)),
40  PrologTerm("/", "rdf_has", std::make_shared<Integer>(3))
41  })));
42  success = success && PROLOG_ENGINE_EVAL(
43  PrologTerm("use_module",
44  PrologTerm("library", "semweb"),
45  PrologList({
46  PrologTerm("/", "sw_literal_compare", std::make_shared<Integer>(3)),
47  PrologTerm("/", "sw_literal_max", std::make_shared<Integer>(3)),
48  PrologTerm("/", "sw_literal_min", std::make_shared<Integer>(3)),
49  PrologTerm("/", "sw_resource_frequency", std::make_shared<Integer>(2)),
50  PrologTerm("/", "sw_class_frequency", std::make_shared<Integer>(2)),
51  PrologTerm("/", "sw_property_frequency", std::make_shared<Integer>(2))
52  })));
53  return success;
54 }
#define PROLOG_ENGINE_EVAL(term)
Definition: PrologEngine.h:128
static void initializeProlog()

◆ initializeBackend() [2/4]

bool knowrob::PrologBackend::initializeBackend ( )

◆ initializeBackend() [3/4]

bool PrologBackend::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 56 of file PrologBackend.cpp.

56  {
57  return initializeBackend();
58 }

◆ initializeBackend() [4/4]

bool knowrob::PrologBackend::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 PrologBackend::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 80 of file PrologBackend.cpp.

80  {
81  // :- rdf_transaction(...).
82  return PROLOG_ENGINE_EVAL(transaction(rdf_assert, triples));
83 }
static PrologTerm transaction(std::string_view rdf_functor, const TripleContainerPtr &triples)

◆ insertAll() [2/2]

bool knowrob::PrologBackend::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 PrologBackend::insertOne ( const Triple triple)
overridevirtual

Add an assertion to this backend.

Parameters
triplea triple.
Returns
true on success

Implements knowrob::Storage.

Definition at line 60 of file PrologBackend.cpp.

60  {
61  // :- rdf_assert($triple.subject, $triple.predicate, $triple.object, $triple.origin).
62  return PROLOG_ENGINE_EVAL(PrologTerm(triple, rdf_assert));
63 }

◆ insertOne() [2/2]

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

Add an assertion to this backend.

Parameters
triplea triple.
Returns
true on success

Implements knowrob::Storage.

◆ isPersistent() [1/2]

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

Implements knowrob::QueryableStorage.

Definition at line 99 of file PrologBackend.cpp.

99  {
100  return false;
101 }

◆ isPersistent() [2/2]

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

Implements knowrob::QueryableStorage.

◆ query() [1/2]

void PrologBackend::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 174 of file PrologBackend.cpp.

174  {
176 }
void query(const GraphQueryPtr &query, const BindingsHandler &callback) override

◆ query() [2/2]

void knowrob::PrologBackend::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.

◆ removeAll() [1/4]

bool PrologBackend::removeAll ( )
static

Remove all triples from the storage.

Returns
true on success.

Definition at line 75 of file PrologBackend.cpp.

75  {
76  // :- rdf_retractall(_, _, _, $origin).
77  return PROLOG_ENGINE_EVAL(PrologTerm(rdf_retractall, PrologTerm(), PrologTerm(), PrologTerm(), PrologTerm()));
78 }

◆ removeAll() [2/4]

static bool knowrob::PrologBackend::removeAll ( )
static

Remove all triples from the storage.

Returns
true on success.

◆ removeAll() [3/4]

bool PrologBackend::removeAll ( const TripleContainerPtr triples)
overridevirtual

Delete all matching statements from this backend.

Parameters
triplesa set of triples.

Implements knowrob::Storage.

Definition at line 85 of file PrologBackend.cpp.

85  {
86  // :- rdf_transaction(...).
87  return PROLOG_ENGINE_EVAL(transaction(rdf_retractall, triples));
88 }

◆ removeAll() [4/4]

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

Delete all matching statements from this backend.

Parameters
triplesa set of triples.

Implements knowrob::Storage.

◆ removeAllWithOrigin() [1/2]

bool PrologBackend::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 70 of file PrologBackend.cpp.

70  {
71  // :- rdf_retractall(_, _, _, $origin).
72  return PROLOG_ENGINE_EVAL(PrologTerm(rdf_retractall, PrologTerm(), PrologTerm(), PrologTerm(), origin));
73 }

◆ removeAllWithOrigin() [2/2]

bool knowrob::PrologBackend::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 PrologBackend::removeOne ( const Triple triple)
overridevirtual

Delete the first matching statement from this backend.

Parameters
triplea triple.

Implements knowrob::Storage.

Definition at line 65 of file PrologBackend.cpp.

65  {
66  // :- rdf_retractall($triple.subject, $triple.predicate, $triple.object, $triple.origin).
67  return PROLOG_ENGINE_EVAL(PrologTerm(triple, rdf_retractall));
68 }

◆ removeOne() [2/2]

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

Delete the first matching statement from this backend.

Parameters
triplea triple.

Implements knowrob::Storage.

◆ transaction() [1/2]

PrologTerm PrologBackend::transaction ( std::string_view  rdf_functor,
const TripleContainerPtr triples 
)
staticprotected

Definition at line 90 of file PrologBackend.cpp.

90  {
91  // transactionTerm = rdf_transaction((rdf_functor(s, p, o, g), ...)).
92  PrologTerm transactionGoal;
93  for (const auto &triple: *triples) {
94  transactionGoal = (transactionGoal & PrologTerm(*triple, rdf_functor));
95  }
96  return PrologTerm(rdf_transaction, transactionGoal);
97 }

◆ transaction() [2/2]

static PrologTerm knowrob::PrologBackend::transaction ( std::string_view  rdf_functor,
const TripleContainerPtr triples 
)
staticprotected

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