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

#include <ConjunctiveBroadcaster.h>

Inheritance diagram for knowrob::ConjunctiveBroadcaster:
Collaboration diagram for knowrob::ConjunctiveBroadcaster:

Public Member Functions

 ConjunctiveBroadcaster (bool ignoreInconsistentAnswers=true)
 
 ConjunctiveBroadcaster (bool ignoreInconsistentAnswers=true)
 
- 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 Types

using AnswerMap = std::map< uint32_t, std::map< size_t, AnswerPtr > >
 
using AnswerMap = std::map< uint32_t, std::map< size_t, AnswerPtr > >
 

Protected Member Functions

void push (Channel &channel, const TokenPtr &tok) override
 
void genCombinations (uint32_t pushedChannelID, AnswerMap::iterator it, AnswerPtr &combinedResult)
 
void push (Channel &channel, const TokenPtr &tok) override
 
void genCombinations (uint32_t pushedChannelID, AnswerMap::iterator it, AnswerPtr &combinedResult)
 
- Protected Member Functions inherited from knowrob::TokenBroadcaster
void push (const TokenPtr &tok) override
 
virtual void pushToBroadcast (const TokenPtr &tok)
 
void push (const TokenPtr &tok) override
 
virtual void pushToBroadcast (const TokenPtr &tok)
 

Protected Attributes

AnswerMap buffer_
 
std::mutex buffer_mutex_
 
bool ignoreInconsistentAnswers_
 
bool hasSolution_
 
unsigned int numClosedChannels_ = 0
 
std::vector< AnswerNoPtrnegativeAnswers_
 
- 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 conjunction. This is intended to be used for parallel evaluation of independent sub-goals within a query.

Definition at line 19 of file ConjunctiveBroadcaster.h.

Member Typedef Documentation

◆ AnswerMap [1/2]

using knowrob::ConjunctiveBroadcaster::AnswerMap = std::map<uint32_t, std::map<size_t, AnswerPtr> >
protected

Definition at line 26 of file ConjunctiveBroadcaster.h.

◆ AnswerMap [2/2]

using knowrob::ConjunctiveBroadcaster::AnswerMap = std::map<uint32_t, std::map<size_t, AnswerPtr> >
protected

Definition at line 26 of file ConjunctiveBroadcaster.h.

Constructor & Destructor Documentation

◆ ConjunctiveBroadcaster() [1/2]

ConjunctiveBroadcaster::ConjunctiveBroadcaster ( bool  ignoreInconsistentAnswers = true)
explicit

◆ ConjunctiveBroadcaster() [2/2]

knowrob::ConjunctiveBroadcaster::ConjunctiveBroadcaster ( bool  ignoreInconsistentAnswers = true)
explicit

Member Function Documentation

◆ genCombinations() [1/2]

void ConjunctiveBroadcaster::genCombinations ( uint32_t  pushedChannelID,
AnswerMap::iterator  it,
AnswerPtr combinedResult 
)
protected

Definition at line 71 of file ConjunctiveBroadcaster.cpp.

72  {
73  if (it == buffer_.end()) {
74  // end reached, push combination
75  TokenBroadcaster::push(combinedResult);
76  hasSolution_ = true;
77  } else if (it->first == pushedChannelID) {
78  // pass through channel from which the new message was pushed
79  auto it1 = it;
80  ++it1;
81  genCombinations(pushedChannelID, it1, combinedResult);
82  } else if (it->second.size() == 1) {
83  // only a single message buffered from this channel
84  auto it1 = it;
85  ++it1;
86  auto merged = mergeAnswers(combinedResult,
87  it->second.begin()->second, ignoreInconsistentAnswers_);
88  if (merged) {
89  genCombinations(pushedChannelID, it1, merged);
90  }
91  } else {
92  // generate a combination for each buffered message
93  // note: the number of possible combinations grows exponentially with number of messages in channels
94  auto it1 = it;
95  ++it1;
96  for (auto &msg: it->second) {
97  auto merged = mergeAnswers(
98  combinedResult, msg.second, ignoreInconsistentAnswers_);
99  if (merged) {
100  genCombinations(pushedChannelID, it1, merged);
101  }
102  }
103  }
104 }
void genCombinations(uint32_t pushedChannelID, AnswerMap::iterator it, AnswerPtr &combinedResult)
void push(const TokenPtr &tok) override
AnswerPtr mergeAnswers(const AnswerPtr &a, const AnswerPtr &b, bool ignoreInconsistencies)
Definition: Answer.cpp:112

◆ genCombinations() [2/2]

void knowrob::ConjunctiveBroadcaster::genCombinations ( uint32_t  pushedChannelID,
AnswerMap::iterator  it,
AnswerPtr combinedResult 
)
protected

◆ push() [1/2]

void ConjunctiveBroadcaster::push ( Channel channel,
const TokenPtr tok 
)
overrideprotectedvirtual

Reimplemented from knowrob::TokenStream.

Definition at line 20 of file ConjunctiveBroadcaster.cpp.

20  {
21  if (tok->tokenType() == TokenType::ANSWER_TOKEN) {
22  auto answer = std::static_pointer_cast<const Answer>(tok);
23  if (answer->isPositive()) {
24  const uint32_t channelID = channel.id();
25  // need to lock the whole push as genCombinations uses an iterator over the buffer.
26  std::lock_guard<std::mutex> lock(buffer_mutex_);
27 
28  // add to the buffer for later combinations
29  // replace other answer with same hash if present
30  buffer_[channelID][answer->hashOfAnswer()] = answer;
31 
32  // generate combinations with other channels if each channel
33  // buffer has some content.
34  if (buffer_.size() == channels_.size()) {
35  if (channels_.size() == 1) {
36  // not needed to generate combinations
38  } else {
39  // generate all combinations and push combined messages
40  genCombinations(channelID, buffer_.begin(), answer);
41  }
42  }
43  } else if (answer->isNegative()) {
44  // do not combine negative answers like the positive ones above.
45  // only push "no" when receiving EOF while no positive answer has been produced.
46  negativeAnswers_.emplace_back(std::static_pointer_cast<const AnswerNo>(answer));
47  }
48  } else {
49  if (tok->indicatesEndOfEvaluation()) {
51  if (numClosedChannels_ == channels_.size() && !hasSolution_) {
52  if (negativeAnswers_.size() == 1) {
54  } else {
55  auto no = std::make_shared<AnswerNo>();
56  for (auto &x: negativeAnswers_) {
57  no->mergeWith(*x);
58  }
60  }
61  } else {
62  // do not pass on the EOF token until all channels have finished
63  return;
64  }
65  }
66  // pass through non-answer messages
67  TokenStream::push(channel, tok);
68  }
69 }
std::vector< AnswerNoPtr > negativeAnswers_
virtual void push(Channel &channel, const TokenPtr &tok)
Definition: TokenStream.cpp:48
std::list< std::shared_ptr< Channel > > channels_
Definition: TokenStream.h:118

◆ push() [2/2]

void knowrob::ConjunctiveBroadcaster::push ( Channel channel,
const TokenPtr tok 
)
overrideprotectedvirtual

Reimplemented from knowrob::TokenStream.

Member Data Documentation

◆ buffer_

AnswerMap knowrob::ConjunctiveBroadcaster::buffer_
protected

Definition at line 28 of file ConjunctiveBroadcaster.h.

◆ buffer_mutex_

std::mutex knowrob::ConjunctiveBroadcaster::buffer_mutex_
protected

Definition at line 29 of file ConjunctiveBroadcaster.h.

◆ hasSolution_

bool knowrob::ConjunctiveBroadcaster::hasSolution_
protected

Definition at line 31 of file ConjunctiveBroadcaster.h.

◆ ignoreInconsistentAnswers_

bool knowrob::ConjunctiveBroadcaster::ignoreInconsistentAnswers_
protected

Definition at line 30 of file ConjunctiveBroadcaster.h.

◆ negativeAnswers_

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

Definition at line 33 of file ConjunctiveBroadcaster.h.

◆ numClosedChannels_

unsigned int knowrob::ConjunctiveBroadcaster::numClosedChannels_ = 0
protected

Definition at line 32 of file ConjunctiveBroadcaster.h.


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