knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Reasoner.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_REASONER_H_
7 #define KNOWROB_REASONER_H_
8 
9 #include <memory>
10 #include "knowrob/PropertyTree.h"
11 #include "knowrob/terms/Atom.h"
12 #include "knowrob/DataSource.h"
13 #include "knowrob/DataSourceHandler.h"
14 #include "knowrob/storage/Storage.h"
15 #include "knowrob/plugins/NamedPlugin.h"
16 #include "ReasonerError.h"
17 
18 namespace knowrob {
19  // forward declaration
20  class ReasonerManager;
21 
28  class Reasoner : public DataSourceHandler {
29  public:
31 
32  virtual ~Reasoner() = default;
33 
37  auto &reasonerName() const { return t_reasonerName_; }
38 
42  auto reasonerLanguage() const { return reasonerLanguage_; }
43 
47  auto storage() const { return storage_; }
48 
52  template<class T> std::shared_ptr<T> getTypedStorage() const {
53  auto typed = std::dynamic_pointer_cast<T>(storage_);
54  if (!typed) throw ReasonerError("Storage is not of the expected type.");
55  return typed;
56  }
57 
62 
67  void pushWork(const std::function<void(void)> &fn);
68 
73  void setStorage(const StoragePtr &storage) { storage_ = storage; }
74 
79  virtual bool initializeReasoner(const PropertyTree &ptree) = 0;
80 
85  virtual void unload() {}
86 
87  private:
88  AtomPtr t_reasonerName_;
89  ReasonerManager *reasonerManager_;
90  PluginLanguage reasonerLanguage_;
91  StoragePtr storage_;
92 
93  void setReasonerManager(ReasonerManager *reasonerManager) { reasonerManager_ = reasonerManager; }
94 
95  void setReasonerName(std::string_view name) { t_reasonerName_ = Atom::Tabled(name); }
96 
97  void setReasonerLanguage(PluginLanguage lang) { reasonerLanguage_ = lang; }
98 
99  friend class ReasonerManager;
100  };
101 
102  using NamedReasoner = NamedPlugin<Reasoner>;
103  using ReasonerFactory = PluginFactory<Reasoner>;
104  using ReasonerPtr = std::shared_ptr<Reasoner>;
105 }
106 
117 #define REASONER_PLUGIN(classType, pluginName) extern "C" { \
118  std::shared_ptr<knowrob::Reasoner> knowrob_createPlugin(std::string_view pluginID) \
119  { return std::make_shared<classType>(pluginID); } \
120  const char* knowrob_getPluginName() { return pluginName; } }
121 
122 #endif //KNOWROB_REASONER_H_
static std::shared_ptr< knowrob::Atom > Tabled(std::string_view stringForm)
Definition: Atom.cpp:40
friend class ReasonerManager
Definition: Reasoner.h:99
ReasonerManager & reasonerManager() const
ReasonerManager & reasonerManager() const
Definition: Reasoner.cpp:24
std::shared_ptr< T > getTypedStorage() const
Definition: Reasoner.h:52
virtual bool initializeReasoner(const PropertyTree &ptree)=0
virtual void unload()
Definition: Reasoner.h:85
void setStorage(const StoragePtr &storage)
Definition: Reasoner.h:73
auto & reasonerName() const
Definition: Reasoner.h:37
auto storage() const
Definition: Reasoner.h:47
void pushWork(const std::function< void(void)> &fn)
auto reasonerLanguage() const
Definition: Reasoner.h:42
virtual ~Reasoner()=default
FunctionRule & function()
Definition: terms.cpp:140
std::shared_ptr< Reasoner > ReasonerPtr
Definition: Reasoner.h:104
PluginLanguage
Definition: NamedPlugin.h:16
PluginFactory< Reasoner > ReasonerFactory
Definition: Reasoner.h:103
std::shared_ptr< Atom > AtomPtr
Definition: Atom.h:69
NamedPlugin< Reasoner > NamedReasoner
Definition: Reasoner.h:102
std::shared_ptr< Storage > StoragePtr
Definition: Storage.h:154