knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
AnswerNo.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/queries/AnswerNo.h"
7 #include "knowrob/knowrob.h"
8 #include "knowrob/integration/python/utils.h"
9 
10 namespace knowrob {
11  const std::shared_ptr<const AnswerNo> &GenericNo() {
12  static const auto instance = std::make_shared<const AnswerNo>();
13  return instance;
14  }
15 } // namespace knowrob
16 
17 using namespace knowrob;
18 
20  : Answer() {
21  setIsNegative(true);
22 }
23 
25  : Answer(other) {
26  setIsNegative(true);
27 }
28 
29 void AnswerNo::addUngrounded(const std::shared_ptr<Predicate> &predicate, bool isNegated) {
30  if (isNegated) {
32  } else {
34  }
35 }
36 
37 bool AnswerNo::mergeWith(const AnswerNo &other) {
38  reasonerTerm_ = {};
39  if (!frame_->mergeWith(*other.frame_)) {
40  // merging frames failed -> results cannot be combined
41  return false;
42  }
43  // insert groundings of other answer
45  other.positiveUngrounded_.begin(), other.positiveUngrounded_.end());
47  other.negativeUngrounded_.begin(), other.negativeUngrounded_.end());
48  return true;
49 }
50 
52  std::stringstream os;
53  if (reasonerTerm_) {
54  os << "[" << *reasonerTerm_ << "] ";
55  }
56  if (isUncertain()) {
57  os << "probably ";
58  }
59  os << "no";
60  if (!positiveUngrounded_.empty() || !negativeUngrounded_.empty()) {
61  os << ", because:\n";
62  for (auto &x: positiveUngrounded_) {
63  os << '\t' << *x.graphSelector() << ' ' << '~' << *x.predicate();
64  if (x.reasonerTerm() && x.reasonerTerm() != reasonerTerm_) {
65  os << ' ' << '[' << *x.reasonerTerm() << "]";
66  }
67  os << '\n';
68  }
69  for (auto &x: negativeUngrounded_) {
70  os << '\t' << *x.graphSelector() << *x.predicate();
71  if (x.reasonerTerm() && x.reasonerTerm() != reasonerTerm_) {
72  os << ' ' << '[' << *x.reasonerTerm() << "]";
73  }
74  os << '\n';
75  }
76  } else {
77  os << '\n';
78  }
79  return os.str();
80 }
81 
83  static const std::string longMsg = "there was evidence supporting the query to be false";
84  return longMsg;
85 }
86 
87 namespace knowrob {
89  auto mergedAnswer = std::make_shared<AnswerNo>(*a);
90  if (mergedAnswer->mergeWith(*b)) {
91  return mergedAnswer;
92  } else {
93  // merging failed
94  return {};
95  }
96  }
97 } // knowrob
98 
99 namespace knowrob::py {
100  template<>
102  using namespace boost::python;
103  class_<AnswerNo, std::shared_ptr<AnswerNo>, bases<Answer>>
104  ("AnswerNo", init<>())
105  .def("addUngrounded", &AnswerNo::addUngrounded)
106  .def("positiveUngrounded", &AnswerNo::positiveUngrounded, return_value_policy<copy_const_reference>())
107  .def("negativeUngrounded", &AnswerNo::negativeUngrounded, return_value_policy<copy_const_reference>())
108  .def("mergeWith", &AnswerNo::mergeWith);
109  // Allow implicit conversion from AnswerNo to const AnswerNo
110  register_ptr_to_python<std::shared_ptr<const AnswerNo> >();
111  implicitly_convertible<std::shared_ptr<AnswerNo>, std::shared_ptr<const AnswerNo> >();
112  }
113 }
AtomPtr reasonerTerm_
Definition: Answer.h:119
bool isUncertain() const
Definition: Answer.cpp:35
void setIsNegative(bool val)
Definition: Answer.h:125
std::shared_ptr< GraphSelector > frame_
Definition: Answer.h:118
std::vector< FramedPredicate > positiveUngrounded_
Definition: AnswerNo.h:69
void addUngrounded(const std::shared_ptr< Predicate > &predicate, bool isNegated=false)
Definition: AnswerNo.cpp:29
std::vector< FramedPredicate > negativeUngrounded_
Definition: AnswerNo.h:70
std::string stringFormOfNo() const
Definition: AnswerNo.cpp:51
bool mergeWith(const AnswerNo &other)
Definition: AnswerNo.cpp:37
std::string humanReadableFormOfNo() const
Definition: AnswerNo.cpp:82
auto & positiveUngrounded() const
Definition: AnswerNo.h:42
auto & negativeUngrounded() const
Definition: AnswerNo.h:49
PredicateRule & predicate()
Definition: formula.cpp:221
TermRule & string()
Definition: terms.cpp:63
void createType< AnswerNo >()
Definition: AnswerNo.cpp:101
GraphSelectorPtr DefaultGraphSelector()
AnswerPtr mergeNegativeAnswers(const AnswerNoPtr &a, const AnswerNoPtr &b)
Definition: AnswerNo.cpp:88
std::shared_ptr< const Answer > AnswerPtr
Definition: Answer.h:129
const std::shared_ptr< const AnswerNo > & GenericNo()
Definition: AnswerNo.cpp:11
std::shared_ptr< const AnswerNo > AnswerNoPtr
Definition: AnswerNo.h:74