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_
 
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 65 of file ConjunctiveBroadcaster.cpp.

66  {
67  if (it == buffer_.end()) {
68  // end reached, push combination
69  TokenBroadcaster::push(combinedResult);
70  hasSolution_ = true;
71  } else if (it->first == pushedChannelID) {
72  // pass through channel from which the new message was pushed
73  auto it1 = it;
74  ++it1;
75  genCombinations(pushedChannelID, it1, combinedResult);
76  } else if (it->second.size() == 1) {
77  // only a single message buffered from this channel
78  auto it1 = it;
79  ++it1;
80  auto merged = mergeAnswers(combinedResult,
81  it->second.begin()->second, ignoreInconsistentAnswers_);
82  if (merged) {
83  genCombinations(pushedChannelID, it1, merged);
84  }
85  } else {
86  // generate a combination for each buffered message
87  // note: the number of possible combinations grows exponentially with number of messages in channels
88  auto it1 = it;
89  ++it1;
90  for (auto &msg: it->second) {
91  auto merged = mergeAnswers(
92  combinedResult, msg.second, ignoreInconsistentAnswers_);
93  if (merged) {
94  genCombinations(pushedChannelID, it1, merged);
95  }
96  }
97  }
98 }
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() && !hasSolution_) {
50  if (negativeAnswers_.size() == 1) {
52  } else {
53  auto no = std::make_shared<AnswerNo>();
54  for (auto &x: negativeAnswers_) {
55  no->mergeWith(*x);
56  }
58  }
59  }
60  // pass through non-answer messages
61  TokenStream::push(channel, tok);
62  }
63 }
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 32 of file ConjunctiveBroadcaster.h.


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