knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Reasoner.cpp
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 #include "knowrob/Logger.h"
7 #include "knowrob/reasoner/Reasoner.h"
8 #include "knowrob/reasoner/ReasonerManager.h"
9 #include "knowrob/reasoner/ReasonerError.h"
10 #include "knowrob/reasoner/GoalDrivenReasoner.h"
11 #include "knowrob/reasoner/DataDrivenReasoner.h"
12 #include "knowrob/integration/python/utils.h"
13 
14 using namespace knowrob;
15 
17  reasonerManager_(nullptr),
18  reasonerLanguage_(PluginLanguage::CPP)
19 {
20  static const auto undefinedName = std::make_shared<Atom>("undefined");
21  t_reasonerName_ = undefinedName;
22 }
23 
25  if (reasonerManager_) {
26  return *reasonerManager_;
27  } else {
28  throw ReasonerError("No ReasonerManager has been assigned to the reasoner.");
29  }
30 }
31 
32 namespace knowrob {
33  class ReasonerTask : public ThreadPool::Runner {
34  public:
35  explicit ReasonerTask(const std::function<void()> &fn) : fn_(fn) {}
36 
37  void run() override { fn_(); }
38 
39  protected:
40  std::function<void()> fn_;
41  };
42 }
43 
44 void Reasoner::pushWork(const std::function<void(void)> &fn) {
45  auto runner = std::make_shared<ReasonerTask>(fn);
46  DefaultThreadPool()->pushWork(runner, [](const std::exception &e) {
47  KB_ERROR("Error in reasoner worker thread: {}", e.what());
48  });
49 }
50 
51 namespace knowrob::py {
52  // this struct is needed because Reasoner has pure virtual methods
53  struct ReasonerWrap : public Reasoner, boost::python::wrapper<Reasoner> {
54  explicit ReasonerWrap(PyObject *p) : Reasoner(), self(p) {}
55 
56  bool initializeReasoner(const PropertyTree &config) override {
57  return call_method<bool>(self, "initializeReasoner", config);
58  }
59 
60  private:
61  PyObject *self;
62  };
63 
64  template<>
66  using namespace boost::python;
67  class_<Reasoner, std::shared_ptr<ReasonerWrap>, bases<DataSourceHandler>, boost::noncopyable>
68  ("Reasoner", init<>())
69  .def("pushWork", +[](Reasoner &x, object &fn) {
70  auto fn_wrap = std::make_shared<PyObj_wrap>(fn);
71  x.pushWork([fn_wrap] {
72  (*fn_wrap)();
73  });
74  })
75  .def("reasonerLanguage", &Reasoner::reasonerLanguage)
76  .def("storage", &Reasoner::storage)
77  // methods that must be implemented by reasoner plugins
78  .def("initializeReasoner", &ReasonerWrap::initializeReasoner);
81  }
82 }
#define KB_ERROR
Definition: Logger.h:28
void pushWork(const std::function< void(void)> &fn)
Definition: Reasoner.cpp:44
ReasonerManager & reasonerManager() const
Definition: Reasoner.cpp:24
auto storage() const
Definition: Reasoner.h:47
auto reasonerLanguage() const
Definition: Reasoner.h:42
FunctionRule & function()
Definition: terms.cpp:140
void createType< GoalDrivenReasoner >()
void createType< Reasoner >()
Definition: Reasoner.cpp:65
void createType< DataDrivenReasoner >()
PluginLanguage
Definition: NamedPlugin.h:16
std::shared_ptr< ThreadPool > DefaultThreadPool()
Definition: ThreadPool.cpp:19
int run(int argc, char **argv)
Definition: terminal.cpp:608