knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
DisjunctiveBroadcaster.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/DisjunctiveBroadcaster.h"
7 
8 using namespace knowrob;
9 
11  : TokenBroadcaster(),
12  isCertainlyPositive_(false) {
13 }
14 
16  std::lock_guard<std::mutex> lock(db_mutex_);
18  for (auto &x: deferredPositiveAnswers_) {
20  }
21  } else {
22  // neither a certain positive nor a certain negative answer has been produced.
23  if (deferredPositiveAnswers_.empty()) {
24  if (negativeAnswers_.size() == 1) {
26  } else if (!negativeAnswers_.empty()) {
27  auto no = std::make_shared<AnswerNo>();
28  for (auto &x: negativeAnswers_) {
29  no->mergeWith(*x);
30  }
32  }
33  } else {
34  // only push positive answers if there are any
35  for (auto &x: deferredPositiveAnswers_) {
37  }
38  }
39  }
41  negativeAnswers_.clear();
42 }
43 
45  // note that this stage consolidates results of independent evaluations that
46  // are considered in disjunction. Thus, a positive answer from one evaluation
47  // is sufficient to consider the query as satisfiable.
48  // However, a negative answer from one evaluation is not sufficient to consider
49  // the query as unsatisfiable, as there might be other evaluations that
50  // consider the query as satisfiable.
51 
52  if (answer->isNegative()) {
53  std::lock_guard<std::mutex> lock(db_mutex_);
54  negativeAnswers_.emplace_back(std::static_pointer_cast<const AnswerNo>(answer));
55  } else if (answer->isPositive()) {
56  // a positive answer indicates that a subsystem suggests the input query is satisfiable.
57  // however, the answer can be uncertain, so the "YES" answer is only
58  // pushed directly in case it is a certain answer.
59  // pushing of uncertain answers is delayed until a certain answer has been produced,
60  // or until the end of the stream is reached.
61  auto positiveAnswer = std::static_pointer_cast<const AnswerYes>(answer);
62 
63  if (positiveAnswer->isCertain()) {
64  isCertainlyPositive_ = true;
65  TokenBroadcaster::push(answer);
67  } else {
68  std::unique_lock<std::mutex> lock(db_mutex_);
70  lock.unlock();
71  // push uncertain positive message if a certain positive answer has been produced
72  TokenBroadcaster::push(answer);
73  } else {
74  // else defer pushing uncertain positive answer until a certain answer has been produced, or eof reached
75  deferredPositiveAnswers_.emplace_back(positiveAnswer);
76  }
77  }
78  } else {
79  // neither a positive nor a negative answer, i.e. reasoning system has no clue
80  }
81 }
82 
84  if (tok->indicatesEndOfEvaluation()) {
85  // end of stream message indicates that the input stream has been closed.
88  } else if (tok->tokenType() == TokenType::ANSWER_TOKEN) {
89  pushAnswer(std::static_pointer_cast<const Answer>(tok));
90  } else {
92  }
93 }
std::vector< AnswerNoPtr > negativeAnswers_
void pushAnswer(const AnswerPtr &answer)
std::vector< AnswerYesPtr > deferredPositiveAnswers_
void push(const TokenPtr &msg) override
void push(const TokenPtr &tok) override
std::shared_ptr< const Token > TokenPtr
Definition: Token.h:74
std::shared_ptr< const Answer > AnswerPtr
Definition: Answer.h:129