knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Goal.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/Goal.h"
7 #include "knowrob/integration/python/utils.h"
8 #include "knowrob/queries/AnswerYes.h"
9 #include "knowrob/reasoner/RDFGoal.h"
10 
11 using namespace knowrob;
12 
13 // make sure bindings only contains variables that are in vars
14 static BindingsPtr includeOnly(const BindingsPtr &bindings, const std::set<std::string_view> &vars) {
15  // test if bindings has variables that are not in vars
16  bool hasExtraVars = bindings->size() > vars.size();
17  if (!hasExtraVars) {
18  for (const auto &var : *bindings) {
19  if (vars.find(var.first) == vars.end()) {
20  hasExtraVars = true;
21  break;
22  }
23  }
24  }
25  if (hasExtraVars) {
26  // bindings has extra variables, filter them
27  auto filtered = std::make_shared<Bindings>();
28  for (const auto &var : vars) {
29  auto it = bindings->find(var);
30  if (it != bindings->end()) {
31  filtered->set(it->second.first, it->second.second);
32  }
33  }
34  return filtered;
35  } else {
36  return bindings;
37  }
38 }
39 
40 static BindingsPtr includeOnly(const BindingsPtr &bindings, const SimpleConjunctionPtr &formula) {
41  std::set<std::string_view> vars;
42  for (const auto &lit : formula->literals()) {
43  for (const auto &var : lit->predicate()->variables()) {
44  vars.insert(var);
45  }
46  }
47  return includeOnly(bindings, vars);
48 }
49 
51  : Query(goal.ctx_),
52  ctx_(goal.ctx_),
53  answerBuffer_(goal.answerBuffer_),
54  outputChannel_(goal.outputChannel_),
55  formula_(std::move(formula)) {}
56 
58  : Query(ctx),
59  ctx_(std::move(ctx)),
60  answerBuffer_(std::make_shared<TokenBuffer>()),
61  outputChannel_(TokenStream::Channel::create(answerBuffer_)),
62  formula_(std::move(formula)) {}
63 
65  : Query(ctx),
66  ctx_(std::move(ctx)),
67  answerBuffer_(std::make_shared<TokenBuffer>()),
68  outputChannel_(TokenStream::Channel::create(answerBuffer_)),
69  formula_(std::make_shared<SimpleConjunction>(literal)) {}
70 
72  outputChannel_->close();
73 }
74 
75 void Goal::push(const AnswerPtr &answer) {
76  outputChannel_->push(answer);
77 }
78 
79 void Goal::push(const BindingsPtr &bindings) {
80  auto filteredBindings = includeOnly(bindings, formula_);
81  auto yes = std::make_shared<AnswerYes>(filteredBindings);
82  for (const auto &lit : formula_->literals()) {
83  auto instance = applyBindings(lit->predicate(), *filteredBindings);
84  yes->addGrounding(std::static_pointer_cast<Predicate>(instance), lit->isNegated());
85  }
86  push(yes);
87 }
88 
89 namespace knowrob::py {
90  template<>
92  using namespace boost::python;
93 
94  using Push1 = void (Goal::*)(const AnswerPtr &);
95  using Push2 = void (Goal::*)(const BindingsPtr &);
96 
97  class_<Goal, std::shared_ptr<Goal>, boost::noncopyable>
98  ("Goal", init<TriplePatternPtr, QueryContextPtr>())
99  .def("formula", &Goal::formula, return_value_policy<copy_const_reference>())
100  .def("answerBuffer", &Goal::answerBuffer, return_value_policy<copy_const_reference>())
101  .def("ctx", &Query::ctx, return_value_policy<copy_const_reference>())
102  .def("push", with<no_gil>(static_cast<Push1>(&Goal::push)))
103  .def("push", with<no_gil>(static_cast<Push2>(&Goal::push)));
105  }
106 }
std::shared_ptr< TokenStream::Channel > outputChannel_
Definition: Goal.h:77
auto & answerBuffer() const
Definition: Goal.h:67
auto & formula() const
Definition: Goal.h:45
void push(const AnswerPtr &answer)
Definition: Goal.cpp:75
~Goal() override
Definition: Goal.cpp:71
Goal(SimpleConjunctionPtr formula, QueryContextPtr ctx=DefaultQueryContext())
Definition: Goal.cpp:57
std::shared_ptr< SimpleConjunction > formula_
Definition: Goal.h:78
auto & ctx() const
Definition: Query.h:41
FormulaRule & formula()
Definition: formula.cpp:283
VariableRule & var()
Definition: terms.cpp:91
void createType< RDFGoal >()
Definition: RDFGoal.cpp:36
void createType< Goal >()
Definition: Goal.cpp:91
std::shared_ptr< SimpleConjunction > SimpleConjunctionPtr
std::shared_ptr< const Bindings > BindingsPtr
Definition: Bindings.h:151
std::shared_ptr< const QueryContext > QueryContextPtr
Definition: QueryContext.h:41
std::shared_ptr< const Answer > AnswerPtr
Definition: Answer.h:129
FirstOrderLiteralPtr applyBindings(const FirstOrderLiteralPtr &lit, const Bindings &bindings)
std::shared_ptr< FirstOrderLiteral > FirstOrderLiteralPtr