knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
RDFGoalReasoner.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/reasoner/RDFGoalReasoner.h>
7 #include "knowrob/integration/python/utils.h"
8 
9 using namespace knowrob;
10 
12  std::vector<TriplePatternPtr> rdfLiterals;
13  for (auto &lit : query->formula()->literals()) {
14  auto rdfLit = std::dynamic_pointer_cast<TriplePattern>(lit);
15  if (rdfLit) {
16  rdfLiterals.push_back(rdfLit);
17  } else {
18  rdfLiterals.push_back(std::make_shared<TriplePattern>(lit->predicate(), lit->isNegated()));
19  }
20  }
21  auto rdfGoal = std::make_shared<RDFGoal>(rdfLiterals, *query);
22  return evaluate(rdfGoal);
23 }
24 
25 namespace knowrob::py {
26 
27  // this struct is needed because Reasoner has pure virtual methods
28  struct RDFGoalReasonerWrap : public RDFGoalReasoner, boost::python::wrapper<RDFGoalReasoner> {
29  explicit RDFGoalReasonerWrap(PyObject *p) : RDFGoalReasoner(), self(p) {}
30 
31  bool initializeReasoner(const PropertyTree &config) override {
32  return call_method<bool>(self, "initializeReasoner", config);
33  }
34 
35  bool evaluate(RDFGoalPtr query) override {
36  return call_method<bool>(self, "evaluate", query);
37  }
38 
39  private:
40  PyObject *self;
41  };
42 
43  template<>
45  using namespace boost::python;
46 
47  // export the GoalDrivenReasoner class
48  class_<RDFGoalReasoner, std::shared_ptr<RDFGoalReasonerWrap>, bases<GoalDrivenReasoner>, boost::noncopyable>
49  ("RDFGoalReasoner", init<>())
50  // methods that must be implemented by reasoner plugins
51  .def("evaluate", &RDFGoalReasonerWrap::evaluate);
52  }
53 }
virtual bool evaluate(RDFGoalPtr query)=0
bool evaluate(GoalPtr query) override
void createType< RDFGoalReasoner >()
std::shared_ptr< Goal > GoalPtr
Definition: Goal.h:84
std::shared_ptr< RDFGoal > RDFGoalPtr
Definition: RDFGoal.h:42