knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
KnowledgeBase.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_KNOWLEDGE_BASE_H
7 #define KNOWROB_KNOWLEDGE_BASE_H
8 
9 #include <memory>
10 #include <boost/property_tree/ptree.hpp>
11 #include <utility>
12 #include "knowrob/queries/QueryContext.h"
13 #include "knowrob/storage/QueryableStorage.h"
14 #include "knowrob/storage/StorageManager.h"
15 #include "knowrob/storage/StorageInterface.h"
16 #include "knowrob/semweb/GraphPathQuery.h"
17 #include "knowrob/semweb/OntologySource.h"
18 #include "knowrob/formulas/SimpleConjunction.h"
19 #include "knowrob/queries/ConjunctiveQuery.h"
20 #include "knowrob/storage/Observer.h"
21 #include "knowrob/storage/ObserverManager.h"
22 
23 namespace knowrob {
24  // forward declaration
25  class ReasonerManager;
26 
32  class KnowledgeBase : public std::enable_shared_from_this<KnowledgeBase> {
33  public:
38  static std::shared_ptr<KnowledgeBase> create(const boost::property_tree::ptree &config);
39 
44  static std::shared_ptr<KnowledgeBase> create(std::string_view config);
45 
49  static std::shared_ptr<KnowledgeBase> create();
50 
52 
53  void init();
54 
55  void loadCommon();
56 
62  bool loadDataSource(const DataSourcePtr &source);
63 
67  auto &vocabulary() const { return vocabulary_; }
68 
72  auto &edb() const { return edb_; }
73 
77  auto &reasonerManager() const { return reasonerManager_; }
78 
82  auto &backendManager() const { return backendManager_; }
83 
88 
96 
105 
114 
121  ObserverPtr observe(const GraphQueryPtr &query, const BindingsHandler &callback);
122 
127 
133  bool insertOne(const Triple &triple);
134 
140  bool insertAll(const TripleContainerPtr &triples);
141 
147  bool insertAll(const std::vector<TriplePtr> &triples);
148 
154  bool removeOne(const Triple &triple);
155 
161  bool removeAll(const TripleContainerPtr &triples);
162 
168  bool removeAll(const std::vector<TriplePtr> &triples);
169 
175  bool removeAllWithOrigin(std::string_view origin);
176 
181  void setDefaultGraph(std::string_view origin);
182 
183  protected:
184  std::shared_ptr<StorageInterface> edb_;
185  std::shared_ptr<ReasonerManager> reasonerManager_;
186  std::shared_ptr<StorageManager> backendManager_;
187  std::shared_ptr<Vocabulary> vocabulary_;
188  std::shared_ptr<ObserverManager> observerManager_;
189  bool isInitialized_;
190 
191  explicit KnowledgeBase(const boost::property_tree::ptree &config);
192 
193  explicit KnowledgeBase(std::string_view config);
194 
195  explicit KnowledgeBase();
196 
197  void configure(const boost::property_tree::ptree &config);
198 
199  static void configurePrefixes(const boost::property_tree::ptree &config);
200 
201  void configureDataSources(const boost::property_tree::ptree &config);
202 
203  void configureBackends(const boost::property_tree::ptree &config);
204 
205  void configureReasoner(const boost::property_tree::ptree &config);
206 
208 
209  void initBackends();
210 
212 
213  std::shared_ptr<NamedBackend> findSourceBackend(const Triple &triple);
214 
216 
217  void stopReasoner();
218 
219  std::vector<std::shared_ptr<NamedBackend>>
220  prepareLoad(std::string_view origin, std::string_view newVersion) const;
221 
222  void
223  finishLoad(const std::shared_ptr<OntologySource> &source, std::string_view origin, std::string_view newVersion);
224 
225  bool loadNonOntologySource(const DataSourcePtr &source) const;
226 
227  bool loadOntologySource(const std::shared_ptr<OntologySource> &source);
228 
229  std::optional<std::string> getVersionOfOrigin(const std::shared_ptr<NamedBackend> &definedBackend,
230  std::string_view origin) const;
231  };
232 
233  using KnowledgeBasePtr = std::shared_ptr<KnowledgeBase>;
234 }
235 
236 #endif //KNOWROB_KNOWLEDGE_BASE_H
KnowledgeBase(std::string_view config)
ObserverPtr observe(const GraphQueryPtr &query, const BindingsHandler &callback)
bool removeOne(const Triple &triple)
bool insertAll(const TripleContainerPtr &triples)
static void configurePrefixes(const boost::property_tree::ptree &config)
bool loadDataSource(const DataSourcePtr &source)
bool loadNonOntologySource(const DataSourcePtr &source) const
auto & backendManager() const
Definition: KnowledgeBase.h:82
KnowledgeBase(const boost::property_tree::ptree &config)
bool removeAllWithOrigin(std::string_view origin)
std::optional< std::string > getVersionOfOrigin(const std::shared_ptr< NamedBackend > &definedBackend, std::string_view origin) const
void finishLoad(const std::shared_ptr< OntologySource > &source, std::string_view origin, std::string_view newVersion)
bool removeAll(const std::vector< TriplePtr > &triples)
bool loadOntologySource(const std::shared_ptr< OntologySource > &source)
std::shared_ptr< StorageManager > backendManager_
bool removeAll(const TripleContainerPtr &triples)
auto & edb() const
Definition: KnowledgeBase.h:72
std::shared_ptr< ReasonerManager > reasonerManager_
QueryableBackendPtr getBackendForQuery() const
static std::shared_ptr< KnowledgeBase > create(const boost::property_tree::ptree &config)
void configureReasoner(const boost::property_tree::ptree &config)
TokenBufferPtr submitQuery(const FirstOrderLiteralPtr &query, const QueryContextPtr &ctx)
std::vector< std::shared_ptr< NamedBackend > > prepareLoad(std::string_view origin, std::string_view newVersion) const
static std::shared_ptr< KnowledgeBase > create()
TokenBufferPtr submitQuery(const ConjunctiveQueryPtr &conjunctiveQuery)
auto & vocabulary() const
Definition: KnowledgeBase.h:67
static std::shared_ptr< KnowledgeBase > create(std::string_view config)
std::shared_ptr< NamedBackend > findSourceBackend(const Triple &triple)
std::shared_ptr< StorageInterface > edb_
std::shared_ptr< Vocabulary > vocabulary_
void configure(const boost::property_tree::ptree &config)
void configureBackends(const boost::property_tree::ptree &config)
auto & reasonerManager() const
Definition: KnowledgeBase.h:77
bool insertOne(const Triple &triple)
void configureDataSources(const boost::property_tree::ptree &config)
void setDefaultGraph(std::string_view origin)
bool insertAll(const std::vector< TriplePtr > &triples)
TokenBufferPtr submitQuery(const FormulaPtr &query, const QueryContextPtr &ctx)
std::shared_ptr< ObserverManager > observerManager_
std::shared_ptr< KnowledgeBase > KnowledgeBasePtr
std::shared_ptr< TripleContainer > TripleContainerPtr
std::shared_ptr< Observer > ObserverPtr
Definition: Observer.h:42
std::shared_ptr< Formula > FormulaPtr
Definition: Formula.h:99
std::shared_ptr< ConjunctiveQuery > ConjunctiveQueryPtr
std::shared_ptr< TokenBuffer > TokenBufferPtr
Definition: TokenBuffer.h:43
std::function< void(const BindingsPtr &)> BindingsHandler
Definition: Bindings.h:152
std::shared_ptr< const QueryContext > QueryContextPtr
Definition: QueryContext.h:41
std::shared_ptr< GraphQuery > GraphQueryPtr
Definition: GraphQuery.h:65
std::shared_ptr< QueryableStorage > QueryableBackendPtr
std::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:107
std::shared_ptr< FirstOrderLiteral > FirstOrderLiteralPtr