Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
graph_sink.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19
20
21#ifndef HEDGEHOG_GRAPH_SINK_H
22#define HEDGEHOG_GRAPH_SINK_H
23
24#include <variant>
25#include "../abstractions/base/input_output/slot_abstraction.h"
26#include "../abstractions/base/node/node_abstraction.h"
27#include "../abstractions/base/input_output/receiver_abstraction.h"
28
29#include "../implementors/concrete_implementor/default_slot.h"
30#include "../implementors/concrete_implementor/queue_receiver.h"
31
33namespace hh {
35namespace core {
36
39template<class ...Outputs>
40class GraphSink :
43 public abstraction::ReceiverAbstraction<Outputs> ... {
44 public:
45 using ResultType_t = std::shared_ptr<std::variant<std::shared_ptr<Outputs>...>>;
46
49 NodeAbstraction("Sink"),
50 SlotAbstraction(std::static_pointer_cast<implementor::ImplementorSlot>(std::make_shared<implementor::DefaultSlot>())),
51 abstraction::ReceiverAbstraction<Outputs>(
52 std::make_shared<implementor::QueueReceiver < Outputs>>(),
53 SlotAbstraction::mutex())... {}
54
56 ~GraphSink() override = default;
57
59 void wakeUp() override { this->slotConditionVariable()->notify_one(); }
60
64 std::unique_lock<std::mutex> lock(*(this->mutex()));
65
66 this->slotConditionVariable()->wait(
67 lock,
68 [this]() { return !this->receiversEmpty() || !this->hasNotifierConnected(); }
69 );
70
71 ResultType_t res = nullptr;
72 if (!receiversEmpty()) {
73 res = std::make_shared<std::variant<std::shared_ptr<Outputs>...>>();
74 bool outputFound = false;
75 (getOneAvailableResultForAType<Outputs>(outputFound, res), ...);
76 }
77 return res;
78 }
79
80
83 void print(Printer *printer){
84 printer->printSink(this);
86 }
87
90 [[nodiscard]] std::vector<std::pair<std::string const, std::string const>> ids() const override {
91 return {{this->id(), this->id()}};
92 }
93
94 private:
101 template<class Output>
102 void getOneAvailableResultForAType(bool &outputFound, ResultType_t &res) {
103 if (!outputFound) {
105 outputFound = true;
107 }
108 }
109 }
110
114
118 [[nodiscard]] behavior::Node *node() const override {
119 throw std::runtime_error("Try to get a node out of a core switch while there is none.");
120 }
121};
122}
123}
124#endif //HEDGEHOG_GRAPH_SINK_H
Hedgehog main namespace.
Printer abstraction to get a snapshot of the metrics of the Hedgehog graph.
Definition: printer.h:52
virtual void printSink(core::abstraction::NodeAbstraction const *sink)=0
Print outer graph sink.
Behavior abstraction for the base node.
Definition: node.h:32
Core's abstraction to receive a piece of data.
std::shared_ptr< Input > getInputData()
Get an input data from the concrete receiver implementation.
void printEdgeInformation(Printer *printer)
Add to the printer the edge information.
ReceiverAbstraction(std::shared_ptr< implementor::ImplementorReceiver< Outputs > > concreteReceiver, std::shared_ptr< std::mutex > slotMutex)
Constructor using a concrete implementation of a receiver implementor, and the mutex from the slot.
Core's abstraction to receive a signal.
std::shared_ptr< std::mutex > const & mutex() const
Protected accessor to mutex.
bool hasNotifierConnected() const
Test if there is at least one notifier connected.
SlotAbstraction(std::shared_ptr< implementor::ImplementorSlot > concreteSlot)
Constructor using a concrete slot implementation.
std::shared_ptr< std::condition_variable > const & slotConditionVariable() const
Protected accessor to condition variable.
virtual std::string id() const
Core's id ('x' + address of abstraction) as string.
NodeAbstraction(std::string name)
Core node constructor using the core's name.
Sink of the graph, only used in an outer graph.
Definition: graph_sink.h:43
void wakeUp() override
Wake up implementation (notify one node waiting on the condition variable)
Definition: graph_sink.h:59
behavior::Node * node() const override
Getter to the node counterpart.
Definition: graph_sink.h:118
void print(Printer *printer)
Gather sink information.
Definition: graph_sink.h:83
void getOneAvailableResultForAType(bool &outputFound, ResultType_t &res)
Test if there is an available output data for a data.
Definition: graph_sink.h:102
ResultType_t getBlockingResult()
Get a result from the sink, if none is available wait for it (block the current thread)
Definition: graph_sink.h:63
~GraphSink() override=default
Default destructor.
GraphSink()
Default constructor.
Definition: graph_sink.h:48
bool receiversEmpty()
Test if the receivers for all types are empty.
Definition: graph_sink.h:113
std::vector< std::pair< std::string const, std::string const > > ids() const override
Node ids [nodeId, nodeGroupId] accessor.
Definition: graph_sink.h:90
std::shared_ptr< std::variant< std::shared_ptr< Outputs >... > > ResultType_t
Accessor to the output types.
Definition: graph_sink.h:45