diff --git a/examples/multiport_mutation/consumer.hh b/examples/multiport_mutation/consumer.hh index b2ac7324..e984517a 100644 --- a/examples/multiport_mutation/consumer.hh +++ b/examples/multiport_mutation/consumer.hh @@ -11,36 +11,35 @@ using namespace reactor; using namespace std::chrono_literals; class Consumer : public Reactor { -private: - class Inner: public Scope { - Inner(Reactor* reactor, std::size_t index) : Scope(reactor), index_(index) {} - std::size_t index_; + class Inner : public Scope { + Inner(Reactor* reactor, std::size_t index) + : Scope(reactor) + , index_(index) {} + std::size_t index_ = 0; - [[maybe_unused]] const Inner& __lf_inner = *this; + const Inner& _lf_inner = *this; - void reaction_1([[maybe_unused]] const Input& in) { - std::cout << "consumer: " << index_ << " received value:" << *in.get() << std::endl; + void reaction_1(const Input& in) const { + std::cout << "consumer: " << index_ << " received value:" << *in.get() << '\n'; } friend Consumer; }; - Inner __lf_inner; - Reaction handle{"handle", 1, this, [this]() { __lf_inner.reaction_1(this->in); }}; + Inner _lf_inner; + Reaction handle{"handle", 1, this, [this]() { _lf_inner.reaction_1(this->in); }}; + public: - Consumer(const std::string& name, Environment* env, std::size_t index) : Reactor(name, env), __lf_inner(this, index) { - std::cout << "creating instance of consumer" << std::endl; + Consumer(const std::string& name, Environment* env, std::size_t index) + : Reactor(name, env) + , _lf_inner(this, index) { + std::cout << "creating instance of consumer" << '\n'; } - ~Consumer() override { - std::cout << "Consumer Object is deleted" << std::endl; - }; + ~Consumer() override { std::cout << "Consumer Object is deleted" << '\n'; }; Input in{"in", this}; - void assemble() override { - handle.declare_trigger(&in); - } + void assemble() override { handle.declare_trigger(&in); } }; - -#endif //CONSUMER_HH +#endif // CONSUMER_HH diff --git a/examples/multiport_mutation/load_balancer.hh b/examples/multiport_mutation/load_balancer.hh index 8e744279..d13a01c0 100644 --- a/examples/multiport_mutation/load_balancer.hh +++ b/examples/multiport_mutation/load_balancer.hh @@ -15,12 +15,14 @@ using namespace std::chrono_literals; class LoadBalancer : public Reactor { private: - class Inner: public MutableScope { - Inner(Reactor* reactor) : MutableScope(reactor) {} - [[maybe_unused]] const Inner& __lf_inner = *this; + class Inner : public MutableScope { + Inner(Reactor* reactor) + : MutableScope(reactor) {} + const Inner& __lf_inner = *this; // reaction bodies - void reaction_1(const Input& inbound, LogicalAction& scale_action, Multiport>& outbound) { + void reaction_1(const Input& inbound, LogicalAction& scale_action, + Multiport>& outbound) { if (rand() % 30 == 0) { scale_action.schedule(rand() % 20 + 1); } @@ -29,13 +31,15 @@ private: outbound[sel].set(inbound.get()); } - void reaction_2(ModifableMultiport>&outbound, [[maybe_unused]] const LogicalAction& scale, Output& scale_bank) { + void reaction_2(ModifableMultiport>& outbound, + [[maybe_unused]] const LogicalAction& scale, Output& scale_bank) { ModifableMultiport>* temp = &outbound; std::size_t new_size = *scale.get(); auto antideps = (outbound[0]).anti_dependencies(); - auto change_size = std::make_shared>(temp, this->reactor_, antideps, new_size); + auto change_size = + std::make_shared>(temp, this->reactor_, antideps, new_size); add_to_transaction(change_size); @@ -48,16 +52,19 @@ private: }; Inner __lf_inner; - Reaction process{"process", 2, this, [this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }}; - Reaction scale{"scale", 1, this, [this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }}; + Reaction process{"process", 2, this, + [this]() { __lf_inner.reaction_1(this->inbound, this->scale_action, this->out); }}; + Reaction scale{"scale", 1, this, + [this]() { __lf_inner.reaction_2(this->out, this->scale_action, this->scale_bank); }}; public: LoadBalancer(const std::string& name, Environment* env) - : Reactor(name, env), __lf_inner(this) { + : Reactor(name, env) + , __lf_inner(this) { std::cout << "creating instance of load balancer" << std::endl; out.reserve(4); for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { - std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx); + std::string _lf_port_name = out.name() + "_" + std::to_string(_lf_idx); out.emplace_back(_lf_port_name, this); } } @@ -78,6 +85,4 @@ public: } }; - - -#endif //LOAD_BALANCER_HH +#endif // LOAD_BALANCER_HH diff --git a/examples/multiport_mutation/main.cc b/examples/multiport_mutation/main.cc index 3956b8e6..cf255fc0 100644 --- a/examples/multiport_mutation/main.cc +++ b/examples/multiport_mutation/main.cc @@ -3,11 +3,11 @@ #include #include +#include "../../lib/mutation/bank.cc" +#include "../../lib/mutation/connection.cc" #include "./consumer.hh" #include "./load_balancer.hh" #include "./producer.hh" -#include "../../lib/mutation/bank.cc" -#include "../../lib/mutation/connection.cc" #include class Deployment : public Reactor { @@ -15,46 +15,43 @@ class Deployment : public Reactor { std::unique_ptr load_balancer_; std::vector> consumers_; - Reaction scale_bank{"scale_bank", 1, this, [this](){this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out);}}; + Reaction scale_bank{"scale_bank", 1, this, + [this]() { this->__inner.reaction_1(this->scale, this->consumers_, load_balancer_->out); }}; public: - -class Inner: public MutableScope { + class Inner : public MutableScope { int state = 0; - [[maybe_unused]] const Inner& __lf_inner = *this; -public: + const Inner& __lf_inner = *this; - Inner(Reactor* reactor) : MutableScope(reactor) {} - void reaction_1(const Input& scale, std::vector>& reactor_bank, ModifableMultiport>& load_balancer) { + public: + Inner(Reactor* reactor) + : MutableScope(reactor) {} + void reaction_1(const Input& scale, std::vector>& reactor_bank, + ModifableMultiport>& load_balancer) { std::size_t new_size = *scale.get(); std::size_t old_size = reactor_bank.size(); - std::function(Reactor*, std::size_t)> lambda = [](Reactor* reactor, std::size_t index) { + std::function lambda = [](Reactor* reactor, std::size_t index) { std::string __lf_inst_name = "consumer_" + std::to_string(index); return std::make_unique(__lf_inst_name, reactor->environment(), index); }; - auto change_size = std::make_shared>>(&reactor_bank, this->reactor_, new_size, lambda); + + auto change_size = std::make_shared>>( + &reactor_bank, this->reactor_, new_size, lambda); add_to_transaction(change_size); - // old topology commit_transaction(); - // new topology - - if (old_size > new_size) { - for (auto i = 0; i < old_size - new_size; i++) { - } - } else { - std::cout << "load_balancer size:" << load_balancer.size() << " bank size: " << reactor_bank.size() << std::endl; + if (old_size < new_size) { for (auto i = 0; i < new_size; i++) { - auto add_conn = std::make_shared, Input>>(&load_balancer[i], &reactor_bank[i].get()->in, reactor_); - add_to_transaction(add_conn); + auto add_conn = std::make_shared, Input>>( + &load_balancer[i], &reactor_bank[i].get()->in, reactor_); + add_to_transaction(add_conn); } - commit_transaction(true); } - std::cout << "new bank size:" << reactor_bank.size() << std::endl; + commit_transaction(true); } friend LoadBalancer; @@ -62,9 +59,11 @@ class Inner: public MutableScope { Inner __inner; - Deployment(const std::string& name, Environment* env) : Reactor(name, env), __inner(this), - producer_(std::make_unique("producer", environment())), - load_balancer_(std::make_unique("load_balancer", environment())) { + Deployment(const std::string& name, Environment* env) + : Reactor(name, env) + , __inner(this) + , producer_(std::make_unique("producer", environment())) + , load_balancer_(std::make_unique("load_balancer", environment())) { std::cout << "creating instance of deployment" << std::endl; consumers_.reserve(4); for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) { @@ -86,9 +85,8 @@ class Inner: public MutableScope { } }; - auto main() -> int { - //srand(time(nullptr)); + // srand(time(nullptr)); Environment env{4, true}; auto deployment = std::make_unique("c1", &env); env.optimize(); diff --git a/examples/multiport_mutation/producer.hh b/examples/multiport_mutation/producer.hh index 7a998cbb..9b445e8b 100644 --- a/examples/multiport_mutation/producer.hh +++ b/examples/multiport_mutation/producer.hh @@ -13,23 +13,29 @@ using namespace std::chrono_literals; class Producer : public Reactor { private: Timer timer{"timer", this, 1s, 1s}; - Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value);}}; + Reaction r_timer{"r_timer", 1, this, [this]() { __lf_inner.reaction_1(this->value); }}; + + class Inner : public Scope { + unsigned int counter_ = 0; + const Inner& __lf_inner = *this; - class Inner: public Scope { - unsigned itr = 0; - [[maybe_unused]] const Inner& __lf_inner = *this; void reaction_1([[maybe_unused]] Output& out) { - std::cout << "producing value:" << itr << std::endl; - out.set(itr++); + std::cout << "producing value:" << counter_ << std::endl; + out.set(counter_++); } - explicit Inner(Reactor* reactor) : Scope(reactor) {} + + explicit Inner(Reactor* reactor) + : Scope(reactor) {} friend Producer; }; Inner __lf_inner; + public: - Producer(const std::string& name, Environment* env) : Reactor(name, env), __lf_inner(this) { + Producer(const std::string& name, Environment* env) + : Reactor(name, env) + , __lf_inner(this) { std::cout << "creating instance of producer" << std::endl; } Producer() = delete; @@ -43,4 +49,4 @@ public: } }; -#endif //PRODUCER_HH +#endif // PRODUCER_HH diff --git a/examples/power_train/main.cc b/examples/power_train/main.cc index 155c3501..819c1ed0 100644 --- a/examples/power_train/main.cc +++ b/examples/power_train/main.cc @@ -141,6 +141,7 @@ class Engine : public Reactor { // ports Input torque{"torque", this}; // NOLINT Multiport control_input; + private: // reactions_ Reaction r1{"1", 1, this, [this]() { reaction_1(); }}; diff --git a/examples/unit_tests_mutations/main.cc b/examples/unit_tests_mutations/main.cc index 0066d705..78a1b754 100644 --- a/examples/unit_tests_mutations/main.cc +++ b/examples/unit_tests_mutations/main.cc @@ -6,22 +6,25 @@ using namespace std::chrono_literals; -class TestMultiport: public reactor::Reactor { +class TestMultiport : public reactor::Reactor { reactor::ModifableMultiport> test_multiport_{"modifable_multiports_"}; reactor::Timer timer_{"timer", this, 1s}; reactor::LogicalAction scale{"scale", this}; - reactor::Reaction trigger_reaction_{"trigger_reaction", 1, this, [this](){this->__inner.reaction_1(scale);}}; - reactor::Reaction test_reaction_{"test_reaction", 2, this, [this](){this->__inner.reaction_2(test_multiport_, scale);}}; - reactor::Reaction validate_reaction_{"validate_reaction", 3, this, [this](){this->__inner.reaction_3(test_multiport_);}}; -public: + reactor::Reaction trigger_reaction_{"trigger_reaction", 1, this, [this]() { this->__inner.reaction_1(scale); }}; + reactor::Reaction test_reaction_{"test_reaction", 2, this, + [this]() { this->__inner.reaction_2(test_multiport_, scale); }}; + reactor::Reaction validate_reaction_{"validate_reaction", 3, this, + [this]() { this->__inner.reaction_3(test_multiport_); }}; -class Inner: public reactor::MutableScope { +public: + class Inner : public reactor::MutableScope { int state = 0; std::vector sizes = {4, 5, 6, 5, 4, 3}; [[maybe_unused]] const Inner& __lf_inner = *this; -public: - Inner(Reactor* reactor) : MutableScope(reactor) {} + public: + Inner(Reactor* reactor) + : MutableScope(reactor) {} void reaction_1(reactor::LogicalAction& scale) { int size = sizes[state]; @@ -30,7 +33,8 @@ class Inner: public reactor::MutableScope { scale.schedule(size); } - void reaction_2(reactor::ModifableMultiport>& test_multiport, reactor::LogicalAction& scale) { + void reaction_2(reactor::ModifableMultiport>& test_multiport, + reactor::LogicalAction& scale) { reactor::ModifableMultiport>* temp = &test_multiport; std::size_t new_size = *scale.get(); @@ -50,12 +54,14 @@ class Inner: public reactor::MutableScope { Inner __inner; - TestMultiport(const std::string& name, reactor::Environment* env) : Reactor(name, env), __inner(this) { + TestMultiport(const std::string& name, reactor::Environment* env) + : Reactor(name, env) + , __inner(this) { std::cout << "creating instance of deployment" << std::endl; test_multiport_.reserve(4); for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) { - std::string _lf_port_name = test_multiport_.name() + "_" + std::to_string(_lf_idx); + std::string _lf_port_name = test_multiport_.name() + "_" + std::to_string(_lf_idx); test_multiport_.emplace_back(_lf_port_name, this); } } @@ -65,11 +71,9 @@ class Inner: public reactor::MutableScope { trigger_reaction_.declare_trigger(&timer_); trigger_reaction_.declare_schedulable_action(&scale); test_reaction_.declare_trigger(&scale); - } }; - auto main() -> int { // srand(time(nullptr)); reactor::Environment env{4};