Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
execution_pipeline_inputs_management_abstraction.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#ifndef HEDGEHOG_EXECUTION_PIPELINE_INPUTS_MANAGEMENT_ABSTRACTION_H_
20#define HEDGEHOG_EXECUTION_PIPELINE_INPUTS_MANAGEMENT_ABSTRACTION_H_
21
22#include "../../parts/core_switch.h"
23
24#include "../base/input_output/slot_abstraction.h"
25#include "../base/input_output/receiver_abstraction.h"
26
27#include "../../implementors/concrete_implementor/default_slot.h"
28#include "../../implementors/concrete_implementor/queue_receiver.h"
29
31namespace hh {
33namespace core {
35namespace abstraction {
36
39template<class ...Inputs>
41 public SlotAbstraction,
42 public ReceiverAbstraction<Inputs> ... {
43 private:
44 std::unique_ptr<CoreSwitch < Inputs...>> coreSwitch_ = nullptr;
45 protected:
46 using inputs_t = std::tuple<Inputs...>;
47 public:
48
53 template<class ExecutionPipelineImplementation> requires std::is_base_of_v<behavior::MultiSwitchRules<Inputs...>,
54 ExecutionPipelineImplementation>
55 explicit ExecutionPipelineInputsManagementAbstraction(ExecutionPipelineImplementation *const executionPipeline)
56 : SlotAbstraction(std::make_shared<implementor::DefaultSlot>()),
57 ReceiverAbstraction<Inputs>(
58 std::make_shared<implementor::QueueReceiver<Inputs>>(),
60 coreSwitch_(std::make_unique<CoreSwitch < Inputs...>>
61 (static_cast<behavior::MultiSwitchRules<Inputs...> * >(executionPipeline))){}
62
65
66 protected:
69 CoreSwitch<Inputs...> *coreSwitch() const {
70 return coreSwitch_.get();
71 }
72
78 template<tool::ContainsConcept<Inputs...> Input>
79 bool callSendToGraph(std::shared_ptr<Input> &data, size_t const &graphId) {
80 return this->coreSwitch_->callSendToGraph(data, graphId);
81 }
82
85 [[nodiscard]] bool receiversEmpty() const { return (ReceiverAbstraction<Inputs>::empty() && ...); }
86
90 auto switchAsNotifier = static_cast<NotifierAbstraction *>(this->coreSwitch_.get());
91 (connectInputGraphForAType<Inputs>(graph), ...);
92 for (auto slot : std::static_pointer_cast<SlotAbstraction>(graph)->slots()) {
93 switchAsNotifier->addSlot(slot);
94 slot->addNotifier(switchAsNotifier);
95 }
96 }
97
100 auto switchAsNotifier = static_cast<NotifierAbstraction *>(this->coreSwitch_.get());
101 for (auto &slot : switchAsNotifier->connectedSlots()) {
102 for (auto &s : slot->slots()) {
103 s->removeNotifier(switchAsNotifier);
104 s->wakeUp();
105 }
106 }
107 }
108
110 void wakeUp() override { this->slotConditionVariable()->notify_one(); }
111
116 [[nodiscard]] bool canTerminate(bool lock) {
117 bool result;
118 if (lock) { lockSlotMutex(); }
119 result = !this->hasNotifierConnected() && this->receiversEmpty();
120 if (lock) { unlockSlotMutex(); }
121 return result;
122 }
123
126 bool wait() {
127 std::unique_lock<std::mutex> lock(*(this->mutex()));
128 this->slotConditionVariable()->wait(
129 lock,
130 [this]() { return !this->receiversEmpty() || this->canTerminate(false); }
131 );
132
133 return canTerminate(false);
134 }
135
140 }
141
142 private:
146 template<class Input>
148 auto switchAsSender = static_cast<SenderAbstraction<Input> *>(this->coreSwitch_.get());
149 for (auto const receiver : std::static_pointer_cast<ReceiverAbstraction<Input>>(graph)->receivers()) {
150 switchAsSender->addReceiver(receiver);
151 receiver->addSender(switchAsSender);
152 }
153 }
154
155};
156}
157}
158}
159#endif //HEDGEHOG_EXECUTION_PIPELINE_INPUTS_MANAGEMENT_ABSTRACTION_H_
Hedgehog main namespace.
Printer abstraction to get a snapshot of the metrics of the Hedgehog graph.
Definition: printer.h:52
Switch rules behavior abstraction for different types of input.
Core abstraction to notify slots.
void addSlot(SlotAbstraction *const slot)
Add a SlotAbstraction to this notifier.
Core's abstraction to receive a piece of data.
void printEdgeInformation(Printer *printer)
Add to the printer the edge information.
std::set< ReceiverAbstraction * > const & receivers() const
Const accessor to receivers.
Core abstraction to send data.
void addReceiver(ReceiverAbstraction< Output > *const receiver)
Add a ReceiverAbstraction.
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.
std::shared_ptr< std::condition_variable > const & slotConditionVariable() const
Protected accessor to condition variable.
bool wait()
Wait statement when the node is alive and no input data are available.
std::unique_ptr< CoreSwitch< Inputs... > > coreSwitch_
Core switch used to call user-defined rules.
void wakeUp() override
Wake up implementation (notify one node waiting on the condition variable)
void connectGraphToSwitch(std::shared_ptr< GraphInputsManagementAbstraction< Inputs... > > const graph)
Connect a graph to the switch.
ExecutionPipelineInputsManagementAbstraction(ExecutionPipelineImplementation *const executionPipeline)
Construct an Input management abstraction for the execution pipeline from an implementation of MultiS...
bool callSendToGraph(std::shared_ptr< Input > &data, size_t const &graphId)
Interface to the user-defined switch rule.
void connectInputGraphForAType(std::shared_ptr< GraphInputsManagementAbstraction< Inputs... > > const graph)
Connect a graph to the switch for the type Input.
~ExecutionPipelineInputsManagementAbstraction() override=default
Default destructor.
void printEdgesInformation(Printer *printer)
Visitor for the execution pipeline edge.
Switch core.
Definition: core_switch.h:36
Concept verifying that a type is in a variadic.
Definition: concepts.h:86