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

#include <DisjunctiveBroadcaster.h>

Inheritance diagram for knowrob::DisjunctiveBroadcaster:
Collaboration diagram for knowrob::DisjunctiveBroadcaster:

Public Member Functions

 DisjunctiveBroadcaster ()
 
 DisjunctiveBroadcaster ()
 
- Public Member Functions inherited from knowrob::TokenBroadcaster
 TokenBroadcaster ()
 
 ~TokenBroadcaster () override
 
void addSubscriber (const std::shared_ptr< Channel > &subscriber)
 
void removeSubscriber (const std::shared_ptr< Channel > &subscriber)
 
 TokenBroadcaster ()
 
 ~TokenBroadcaster () override
 
void addSubscriber (const std::shared_ptr< Channel > &subscriber)
 
void removeSubscriber (const std::shared_ptr< Channel > &subscriber)
 
- Public Member Functions inherited from knowrob::TokenStream
 TokenStream ()
 
virtual ~TokenStream ()
 
 TokenStream (const TokenStream &)=delete
 
virtual void close ()
 
bool isOpened () const
 
 TokenStream ()
 
virtual ~TokenStream ()
 
 TokenStream (const TokenStream &)=delete
 
virtual void close ()
 
bool isOpened () const
 

Protected Member Functions

void push (const TokenPtr &msg) override
 
void pushAnswer (const AnswerPtr &answer)
 
void pushDeferredMessages ()
 
void push (const TokenPtr &msg) override
 
void pushAnswer (const AnswerPtr &answer)
 
void pushDeferredMessages ()
 
- Protected Member Functions inherited from knowrob::TokenBroadcaster
virtual void pushToBroadcast (const TokenPtr &tok)
 
void push (const TokenPtr &tok) override
 
virtual void pushToBroadcast (const TokenPtr &tok)
 
- Protected Member Functions inherited from knowrob::TokenStream
virtual void push (Channel &channel, const TokenPtr &tok)
 
virtual void push (Channel &channel, const TokenPtr &tok)
 

Protected Attributes

std::vector< AnswerNoPtrnegativeAnswers_
 
std::vector< AnswerYesPtrdeferredPositiveAnswers_
 
bool isCertainlyPositive_
 
std::mutex db_mutex_
 
- Protected Attributes inherited from knowrob::TokenBroadcaster
std::list< std::shared_ptr< Channel > > subscribers_
 
std::mutex mtx_
 
- Protected Attributes inherited from knowrob::TokenStream
std::list< std::shared_ptr< Channel > > channels_
 
std::atomic< bool > isOpened_
 
std::mutex channel_mutex_
 

Detailed Description

Consolidates answers from multiple sources that are considered in disjunction.

Definition at line 18 of file DisjunctiveBroadcaster.h.

Constructor & Destructor Documentation

◆ DisjunctiveBroadcaster() [1/2]

DisjunctiveBroadcaster::DisjunctiveBroadcaster ( )

◆ DisjunctiveBroadcaster() [2/2]

knowrob::DisjunctiveBroadcaster::DisjunctiveBroadcaster ( )

Member Function Documentation

◆ push() [1/2]

void DisjunctiveBroadcaster::push ( const TokenPtr msg)
overrideprotectedvirtual

Implements knowrob::TokenStream.

Definition at line 83 of file DisjunctiveBroadcaster.cpp.

83  {
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 }
void pushAnswer(const AnswerPtr &answer)
void push(const TokenPtr &tok) override

◆ push() [2/2]

void knowrob::DisjunctiveBroadcaster::push ( const TokenPtr msg)
overrideprotectedvirtual

Reimplemented from knowrob::TokenBroadcaster.

◆ pushAnswer() [1/2]

void DisjunctiveBroadcaster::pushAnswer ( const AnswerPtr answer)
protected

Definition at line 44 of file DisjunctiveBroadcaster.cpp.

44  {
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 }
std::vector< AnswerNoPtr > negativeAnswers_
std::vector< AnswerYesPtr > deferredPositiveAnswers_

◆ pushAnswer() [2/2]

void knowrob::DisjunctiveBroadcaster::pushAnswer ( const AnswerPtr answer)
protected

◆ pushDeferredMessages() [1/2]

void DisjunctiveBroadcaster::pushDeferredMessages ( )
protected

Definition at line 15 of file DisjunctiveBroadcaster.cpp.

15  {
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 }

◆ pushDeferredMessages() [2/2]

void knowrob::DisjunctiveBroadcaster::pushDeferredMessages ( )
protected

Member Data Documentation

◆ db_mutex_

std::mutex knowrob::DisjunctiveBroadcaster::db_mutex_
protected

Definition at line 26 of file DisjunctiveBroadcaster.h.

◆ deferredPositiveAnswers_

std::vector< AnswerYesPtr > knowrob::DisjunctiveBroadcaster::deferredPositiveAnswers_
protected

Definition at line 24 of file DisjunctiveBroadcaster.h.

◆ isCertainlyPositive_

bool knowrob::DisjunctiveBroadcaster::isCertainlyPositive_
protected

Definition at line 25 of file DisjunctiveBroadcaster.h.

◆ negativeAnswers_

std::vector< AnswerNoPtr > knowrob::DisjunctiveBroadcaster::negativeAnswers_
protected

Definition at line 23 of file DisjunctiveBroadcaster.h.


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