knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::AnswerYes Class Reference

#include <AnswerYes.h>

Inheritance diagram for knowrob::AnswerYes:
Collaboration diagram for knowrob::AnswerYes:

Public Member Functions

 AnswerYes ()
 
 AnswerYes (BindingsPtr substitution)
 
 AnswerYes (const AnswerYes &other)
 
bool isRicherThan (const AnswerYes &other) const
 
bool isGenericYes () const
 
auto & substitution () const
 
bool hasGrounding (const Variable &var) const
 
bool addGrounding (const std::shared_ptr< Predicate > &predicate, bool isNegated=false, const GraphSelectorPtr &frame=DefaultGraphSelector())
 
auto & positiveGroundings () const
 
auto & negativeGroundings () const
 
bool mergeWith (const AnswerYes &other, bool ignoreInconsistencies=false)
 
std::string stringFormOfYes () const
 
std::string humanReadableFormOfYes () const
 
 AnswerYes ()
 
 AnswerYes (BindingsPtr substitution)
 
 AnswerYes (const AnswerYes &other)
 
bool isRicherThan (const AnswerYes &other) const
 
bool isGenericYes () const
 
auto & substitution () const
 
bool hasGrounding (const Variable &var) const
 
bool addGrounding (const std::shared_ptr< Predicate > &predicate, bool isNegated=false, const GraphSelectorPtr &frame=DefaultGraphSelector())
 
auto & positiveGroundings () const
 
auto & negativeGroundings () const
 
bool mergeWith (const AnswerYes &other, bool ignoreInconsistencies=false)
 
std::string stringFormOfYes () const
 
std::string humanReadableFormOfYes () const
 
- Public Member Functions inherited from knowrob::Answer
 Answer ()
 
 Answer (const Answer &other)
 
auto & frame () const
 
void setFrame (const std::shared_ptr< GraphSelector > &frame)
 
void applyFrame (const GraphSelector &frame)
 
bool isNegative () const
 
bool isPositive () const
 
bool isUncertain () const
 
bool isCertain () const
 
void setIsUncertain (bool val, std::optional< double > confidence)
 
bool isOccasionallyTrue () const
 
bool isAllwaysTrue () const
 
void setIsOccasionallyTrue (bool val)
 
void setReasonerTerm (const AtomPtr &reasonerTerm)
 
auto & reasonerTerm () const
 
size_t hashOfAnswer () const
 
std::string stringFormOfAnswer () const
 
std::string humanReadableForm () const
 
 Answer ()
 
 Answer (const Answer &other)
 
auto & frame () const
 
void setFrame (const std::shared_ptr< GraphSelector > &frame)
 
void applyFrame (const GraphSelector &frame)
 
bool isNegative () const
 
bool isPositive () const
 
bool isUncertain () const
 
bool isCertain () const
 
void setIsUncertain (bool val, std::optional< double > confidence)
 
bool isOccasionallyTrue () const
 
bool isAllwaysTrue () const
 
void setIsOccasionallyTrue (bool val)
 
void setReasonerTerm (const AtomPtr &reasonerTerm)
 
auto & reasonerTerm () const
 
size_t hashOfAnswer () const
 
std::string stringFormOfAnswer () const
 
std::string humanReadableForm () const
 
- Public Member Functions inherited from knowrob::Token
 Token (TokenType tokenType)
 
virtual ~Token ()=default
 
TokenType tokenType () const
 
size_t hash () const
 
std::string stringForm () const
 
bool isControlToken () const
 
bool isAnswerToken () const
 
bool indicatesEndOfEvaluation () const
 
void write (std::ostream &os) const override
 
 Token (TokenType tokenType)
 
virtual ~Token ()=default
 
TokenType tokenType () const
 
size_t hash () const
 
std::string stringForm () const
 
bool isControlToken () const
 
bool isAnswerToken () const
 
bool indicatesEndOfEvaluation () const
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Printable
virtual ~Printable ()=default
 
virtual std::string format () const
 
virtual ~Printable ()=default
 
virtual std::string format () const
 

Protected Attributes

std::vector< FramedPredicatepositiveGroundings_
 
std::vector< FramedPredicatenegativeGroundings_
 
BindingsPtr substitution_
 
- Protected Attributes inherited from knowrob::Answer
std::shared_ptr< GraphSelectorframe_
 
AtomPtr reasonerTerm_
 
bool isPositive_ = false
 
bool isNegative_ = false
 
- Protected Attributes inherited from knowrob::Token
TokenType tokenType_
 
bool isTerminalToken_ = false
 

Additional Inherited Members

- Protected Member Functions inherited from knowrob::Answer
void setIsPositive (bool val)
 
void setIsNegative (bool val)
 
void setIsPositive (bool val)
 
void setIsNegative (bool val)
 

Detailed Description

A positive answer indicates that a querying component has evidence for the input query being true for some instances of the query.

Definition at line 18 of file AnswerYes.h.

Constructor & Destructor Documentation

◆ AnswerYes() [1/6]

AnswerYes::AnswerYes ( )

Default constructor.

Definition at line 16 of file AnswerYes.cpp.

17  : Answer(),
18  substitution_(std::make_shared<Bindings>()) {
19  setIsPositive(true);
20 }
void setIsPositive(bool val)
Definition: Answer.h:123
BindingsPtr substitution_
Definition: AnswerYes.h:104

◆ AnswerYes() [2/6]

AnswerYes::AnswerYes ( BindingsPtr  substitution)
explicit

Definition at line 22 of file AnswerYes.cpp.

23  : Answer(),
24  substitution_(std::move(substitution)) {
25  setIsPositive(true);
26 }
auto & substitution() const
Definition: AnswerYes.h:50

◆ AnswerYes() [3/6]

AnswerYes::AnswerYes ( const AnswerYes other)

Copy constructor.

Parameters
otheranother answer.

Definition at line 28 of file AnswerYes.cpp.

29  : Answer(other),
32  substitution_(std::make_shared<Bindings>(*other.substitution_)) {
33  setIsPositive(true);
34 }
std::vector< FramedPredicate > positiveGroundings_
Definition: AnswerYes.h:102
std::vector< FramedPredicate > negativeGroundings_
Definition: AnswerYes.h:103

◆ AnswerYes() [4/6]

knowrob::AnswerYes::AnswerYes ( )

Default constructor.

◆ AnswerYes() [5/6]

knowrob::AnswerYes::AnswerYes ( BindingsPtr  substitution)
explicit

◆ AnswerYes() [6/6]

knowrob::AnswerYes::AnswerYes ( const AnswerYes other)

Copy constructor.

Parameters
otheranother answer.

Member Function Documentation

◆ addGrounding() [1/2]

bool AnswerYes::addGrounding ( const std::shared_ptr< Predicate > &  predicate,
bool  isNegated = false,
const GraphSelectorPtr frame = DefaultGraphSelector() 
)

Add a grounded literal to the answer. Positive literals may not contain variables.t

Parameters
predicatea predicate.
framea graph selector.
isNegatedtrue if the negation of the predicate is grounded.

Definition at line 58 of file AnswerYes.cpp.

60  {
61  if (!frame_->mergeWith(*frame)) {
62  KB_WARN("Failed to frame \"{}\" with \"{}\".", *frame_, *frame);
63  return false;
64  }
65  if (isNegated) {
67  } else {
69  }
70  return true;
71 }
#define KB_WARN
Definition: Logger.h:27
AtomPtr reasonerTerm_
Definition: Answer.h:119
auto & frame() const
Definition: Answer.h:35
std::shared_ptr< GraphSelector > frame_
Definition: Answer.h:118
PredicateRule & predicate()
Definition: formula.cpp:221

◆ addGrounding() [2/2]

bool knowrob::AnswerYes::addGrounding ( const std::shared_ptr< Predicate > &  predicate,
bool  isNegated = false,
const GraphSelectorPtr frame = DefaultGraphSelector() 
)

Add a grounded literal to the answer. Positive literals may not contain variables.t

Parameters
predicatea predicate.
framea graph selector.
isNegatedtrue if the negation of the predicate is grounded.

◆ hasGrounding() [1/2]

bool knowrob::AnswerYes::hasGrounding ( const Variable var) const
inline
Parameters
vara variable.
Returns
true if the variable is mapped to a term.

Definition at line 56 of file AnswerYes.h.

56 { return substitution_->contains(var.name()); }
VariableRule & var()
Definition: terms.cpp:91

◆ hasGrounding() [2/2]

bool knowrob::AnswerYes::hasGrounding ( const Variable var) const
inline
Parameters
vara variable.
Returns
true if the variable is mapped to a term.

Definition at line 56 of file AnswerYes.h.

56 { return substitution_->contains(var.name()); }

◆ humanReadableFormOfYes() [1/2]

std::string AnswerYes::humanReadableFormOfYes ( ) const
Returns
the human readable form of this answer.

Definition at line 133 of file AnswerYes.cpp.

133  {
134  std::stringstream os;
135  os << std::setprecision(4);
136  os << "the query is ";
137  if (isUncertain()) {
138  if (frame_->confidence.has_value()) {
139  os << "true with a confidence of " << std::setprecision(4) << frame_->confidence.value();
140  } else {
141  os << "probably true";
142  }
143  } else {
144  os << "true";
145  }
146  if (frame_->occasional) {
147  os << " at some time ";
148  }
149  if (frame_->begin.has_value() && frame_->end.has_value()) {
150  os << " during the time points " << frame_->begin.value() << " and " << frame_->end.value();
151  } else if (frame_->begin.has_value()) {
152  os << " since " << frame_->begin.value();
153  } else if (frame_->end.has_value()) {
154  os << " until " << frame_->end.value();
155  }
156  os << ".";
157  return os.str();
158 }
bool isUncertain() const
Definition: Answer.cpp:35

◆ humanReadableFormOfYes() [2/2]

std::string knowrob::AnswerYes::humanReadableFormOfYes ( ) const
Returns
the human readable form of this answer.

◆ isGenericYes() [1/2]

bool AnswerYes::isGenericYes ( ) const
Returns
true if this answer is a generic "yes" answer without any groundings.

Definition at line 54 of file AnswerYes.cpp.

54  {
55  return substitution_->empty();
56 }

◆ isGenericYes() [2/2]

bool knowrob::AnswerYes::isGenericYes ( ) const
Returns
true if this answer is a generic "yes" answer without any groundings.

◆ isRicherThan() [1/2]

bool AnswerYes::isRicherThan ( const AnswerYes other) const
Parameters
otheranother answer.
Returns
true if this answer has more information than the other answer.

Definition at line 36 of file AnswerYes.cpp.

36  {
37  if (isUncertain() != other.isUncertain()) {
38  // certain answer is richer than uncertain answer
39  return isUncertain() < other.isUncertain();
40  } else if (substitution_->size() != other.substitution_->size()) {
41  // answer with more substitutions is richer
42  return substitution_->size() > other.substitution_->size();
43  } else if (positiveGroundings_.size() != other.positiveGroundings_.size()) {
44  // answer with more positive groundings is richer
45  return positiveGroundings_.size() > other.positiveGroundings_.size();
46  } else if (negativeGroundings_.size() != other.negativeGroundings_.size()) {
47  // answer with more negative groundings is richer
48  return negativeGroundings_.size() > other.negativeGroundings_.size();
49  } else {
50  return false;
51  }
52 }

◆ isRicherThan() [2/2]

bool knowrob::AnswerYes::isRicherThan ( const AnswerYes other) const
Parameters
otheranother answer.
Returns
true if this answer has more information than the other answer.

◆ mergeWith() [1/2]

bool AnswerYes::mergeWith ( const AnswerYes other,
bool  ignoreInconsistencies = false 
)

Merge this answer with another answer.

Parameters
otheranother answer.
ignoreInconsistenciesif true, inconsistencies are ignored.
Returns
true if the merge was successful.

Definition at line 73 of file AnswerYes.cpp.

73  {
74  auto mergedBindings = std::make_shared<Bindings>(*substitution_);
75  if (ignoreInconsistencies) {
76  // insert all substitutions of other answer, possibly overwriting existing ones
77  *mergedBindings += *other.substitution_;
78  } else {
79  // unify substitutions
80  if (!mergedBindings->unifyWith(*other.substitution_)) {
81  // unification failed -> results cannot be combined
82  return false;
83  }
84  }
85  substitution_ = mergedBindings;
86 
87  if (!frame_->mergeWith(*other.frame_)) {
88  // merging frames failed -> results cannot be combined
89  return false;
90  }
91 
92  reasonerTerm_ = {};
93  // insert groundings of other answer
95  other.positiveGroundings_.begin(), other.positiveGroundings_.end());
97  other.negativeGroundings_.begin(), other.negativeGroundings_.end());
98 
99  return true;
100 }

◆ mergeWith() [2/2]

bool knowrob::AnswerYes::mergeWith ( const AnswerYes other,
bool  ignoreInconsistencies = false 
)

Merge this answer with another answer.

Parameters
otheranother answer.
ignoreInconsistenciesif true, inconsistencies are ignored.
Returns
true if the merge was successful.

◆ negativeGroundings() [1/2]

auto& knowrob::AnswerYes::negativeGroundings ( ) const
inline

Part of the answer is that certain literals that appear negated in the query are not true. This is a list of such literals.

Returns
a list of negative groundings.

Definition at line 81 of file AnswerYes.h.

81 { return negativeGroundings_; }

◆ negativeGroundings() [2/2]

auto& knowrob::AnswerYes::negativeGroundings ( ) const
inline

Part of the answer is that certain literals that appear negated in the query are not true. This is a list of such literals.

Returns
a list of negative groundings.

Definition at line 81 of file AnswerYes.h.

81 { return negativeGroundings_; }

◆ positiveGroundings() [1/2]

auto& knowrob::AnswerYes::positiveGroundings ( ) const
inline

Part of the answer is that certain literals that appear positive in the query are true. This is a list of such literals.

Returns
a list of positive groundings.

Definition at line 74 of file AnswerYes.h.

74 { return positiveGroundings_; }

◆ positiveGroundings() [2/2]

auto& knowrob::AnswerYes::positiveGroundings ( ) const
inline

Part of the answer is that certain literals that appear positive in the query are true. This is a list of such literals.

Returns
a list of positive groundings.

Definition at line 74 of file AnswerYes.h.

74 { return positiveGroundings_; }

◆ stringFormOfYes() [1/2]

std::string AnswerYes::stringFormOfYes ( ) const
Returns
the string form of this answer.

Definition at line 102 of file AnswerYes.cpp.

102  {
103  std::stringstream os;
104  if (reasonerTerm_) {
105  os << "[" << *reasonerTerm_ << "] ";
106  }
107  if (isUncertain()) {
108  os << "probably ";
109  }
110  os << "yes";
111  if (!positiveGroundings_.empty() || !negativeGroundings_.empty()) {
112  os << ", because:\n";
113  for (auto &x: positiveGroundings_) {
114  os << '\t' << *x.graphSelector() << ' ' << *x.predicate();
115  if (x.reasonerTerm() && x.reasonerTerm() != reasonerTerm_) {
116  os << ' ' << '[' << *x.reasonerTerm() << "]";
117  }
118  os << '\n';
119  }
120  for (auto &x: negativeGroundings_) {
121  os << '\t' << *x.graphSelector() << '~' << *x.predicate();
122  if (x.reasonerTerm() && x.reasonerTerm() != reasonerTerm_) {
123  os << ' ' << '[' << *x.reasonerTerm() << "]";
124  }
125  os << '\n';
126  }
127  } else {
128  os << '\n';
129  }
130  return os.str();
131 }

◆ stringFormOfYes() [2/2]

std::string knowrob::AnswerYes::stringFormOfYes ( ) const
Returns
the string form of this answer.

◆ substitution() [1/2]

auto& knowrob::AnswerYes::substitution ( ) const
inline

Each positive answer is associated with a substitution that maps variables to terms. This substitution can be used to instantiate the query.

Returns
a mapping from variables to terms.

Definition at line 50 of file AnswerYes.h.

50 { return substitution_; }

◆ substitution() [2/2]

auto& knowrob::AnswerYes::substitution ( ) const
inline

Each positive answer is associated with a substitution that maps variables to terms. This substitution can be used to instantiate the query.

Returns
a mapping from variables to terms.

Definition at line 50 of file AnswerYes.h.

50 { return substitution_; }

Member Data Documentation

◆ negativeGroundings_

std::vector< FramedPredicate > knowrob::AnswerYes::negativeGroundings_
protected

Definition at line 103 of file AnswerYes.h.

◆ positiveGroundings_

std::vector< FramedPredicate > knowrob::AnswerYes::positiveGroundings_
protected

Definition at line 102 of file AnswerYes.h.

◆ substitution_

BindingsPtr knowrob::AnswerYes::substitution_
protected

Definition at line 104 of file AnswerYes.h.


The documentation for this class was generated from the following files: