Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Dec 17, 2024
1 parent 9e1b6ed commit b941c4b
Show file tree
Hide file tree
Showing 26 changed files with 249 additions and 198 deletions.
6 changes: 3 additions & 3 deletions examples/count/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using namespace reactor;
using namespace std::chrono_literals;

class Count : public Reactor {
class Count final : public Reactor {
private:
// actions
Timer timer{"timer", this};
LogicalAction<int> counter{"counter", this};

// reactions_
Reaction r_init{"r_init", 1, false, this, [this]() { this->init(); }};
Reaction r_counter{"r_counter", 2, false, this, [this]() { this->print_count(); }};
Reaction r_init{"r_init", 1, this, [this]() { this->init(); }};
Reaction r_counter{"r_counter", 2, this, [this]() { this->print_count(); }};

public:
explicit Count(Environment* env)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace reactor;
using namespace std::chrono_literals;

class Hello : public Reactor {
class Hello final : public Reactor {
private:
// actions
Timer timer{"timer", this, 1s, 2s};
Expand Down
37 changes: 18 additions & 19 deletions examples/multiport_mutation/consumer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>& in) {
std::cout << "consumer: " << index_ << " received value:" << *in.get() << std::endl;
void reaction_1(const Input<unsigned>& 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<unsigned> in{"in", this};

void assemble() override {
handle.declare_trigger(&in);
}
void assemble() override { handle.declare_trigger(&in); }
};


#endif //CONSUMER_HH
#endif // CONSUMER_HH
31 changes: 18 additions & 13 deletions examples/multiport_mutation/load_balancer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>& inbound, LogicalAction<unsigned>& scale_action, Multiport<Output<unsigned>>& outbound) {
void reaction_1(const Input<unsigned>& inbound, LogicalAction<unsigned>& scale_action,
Multiport<Output<unsigned>>& outbound) {
if (rand() % 30 == 0) {
scale_action.schedule(rand() % 20 + 1);
}
Expand All @@ -29,13 +31,15 @@ private:
outbound[sel].set(inbound.get());
}

void reaction_2(ModifableMultiport<Output<unsigned>>&outbound, [[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
void reaction_2(ModifableMultiport<Output<unsigned>>& outbound,
[[maybe_unused]] const LogicalAction<unsigned>& scale, Output<unsigned>& scale_bank) {
ModifableMultiport<Output<unsigned>>* temp = &outbound;
std::size_t new_size = *scale.get();

auto antideps = (outbound[0]).anti_dependencies();

auto change_size = std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);
auto change_size =
std::make_shared<MutationChangeOutputMultiportSize<unsigned>>(temp, this->reactor_, antideps, new_size);

add_to_transaction(change_size);

Expand All @@ -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);
}
}
Expand All @@ -78,6 +85,4 @@ public:
}
};



#endif //LOAD_BALANCER_HH
#endif // LOAD_BALANCER_HH
71 changes: 34 additions & 37 deletions examples/multiport_mutation/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,89 @@
#include <reactor-cpp/mutations/bank.hh>
#include <reactor-cpp/mutations/connection.hh>

#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 <reactor-cpp/reactor-cpp.hh>

class Deployment : public Reactor {
class Deployment final : public Reactor {
std::unique_ptr<Producer> producer_;
std::unique_ptr<LoadBalancer> load_balancer_;
std::vector<std::unique_ptr<Consumer>> 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<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank, ModifableMultiport<Output<unsigned>>& load_balancer) {
public:
Inner(Reactor* reactor)
: MutableScope(reactor) {}
void reaction_1(const Input<unsigned>& scale, std::vector<std::unique_ptr<Consumer>>& reactor_bank,
ModifableMultiport<Output<unsigned>>& load_balancer) {
std::size_t new_size = *scale.get();
std::size_t old_size = reactor_bank.size();

std::function<std::unique_ptr<Consumer>(Reactor*, std::size_t)> lambda = [](Reactor* reactor, std::size_t index) {
std::string __lf_inst_name = "consumer_" + std::to_string(index);
return std::make_unique<Consumer>(__lf_inst_name, reactor->environment(), index);
std::function lambda = [](Reactor* reactor, std::size_t index) {
std::string _lf_inst_name = "consumer_" + std::to_string(index);
return std::make_unique<Consumer>(_lf_inst_name, reactor->environment(), index);
};
auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(&reactor_bank, this->reactor_, new_size, lambda);

auto change_size = std::make_shared<MutationChangeBankSize<std::unique_ptr<Consumer>>>(
&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<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(&load_balancer[i], &reactor_bank[i].get()->in, reactor_);
add_to_transaction(add_conn);
auto add_conn = std::make_shared<MutationAddConnection<Output<unsigned>, Input<unsigned>>>(
&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;
};

Inner __inner;

Deployment(const std::string& name, Environment* env) : Reactor(name, env), __inner(this),
producer_(std::make_unique<Producer>("producer", environment())),
load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
std::cout << "creating instance of deployment" << std::endl;
Deployment(const std::string& name, Environment* env)
: Reactor(name, env)
, __inner(this)
, producer_(std::make_unique<Producer>("producer", environment()))
, load_balancer_(std::make_unique<LoadBalancer>("load_balancer", environment())) {
std::cout << "creating instance of deployment" << '\n';
consumers_.reserve(4);
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
std::string __lf_inst_name = "consumer_" + std::to_string(__lf_idx);
consumers_.push_back(std::make_unique<Consumer>(__lf_inst_name, environment(), __lf_idx));
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
std::string _lf_inst_name = "consumer_" + std::to_string(_lf_idx);
consumers_.push_back(std::make_unique<Consumer>(_lf_inst_name, environment(), _lf_idx));
}
}
~Deployment() override = default;

Input<unsigned> scale{"scale", this};

void assemble() override {
for (size_t __lf_idx = 0; __lf_idx < 4; __lf_idx++) {
environment()->draw_connection(load_balancer_->out[__lf_idx], consumers_[__lf_idx]->in, ConnectionProperties{});
for (size_t _lf_idx = 0; _lf_idx < 4; _lf_idx++) {
environment()->draw_connection(load_balancer_->out[_lf_idx], consumers_[_lf_idx]->in, ConnectionProperties{});
environment()->draw_connection(producer_->value, load_balancer_->inbound, ConnectionProperties{});
}
environment()->draw_connection(load_balancer_->scale_bank, scale, ConnectionProperties{});
scale_bank.declare_trigger(&this->scale);
}
};


auto main() -> int {
//srand(time(nullptr));
Environment env{4, true};
auto deployment = std::make_unique<Deployment>("c1", &env);
env.optimize();
Expand Down
24 changes: 15 additions & 9 deletions examples/multiport_mutation/producer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>& 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;
Expand All @@ -43,4 +49,4 @@ public:
}
};

#endif //PRODUCER_HH
#endif // PRODUCER_HH
8 changes: 4 additions & 4 deletions examples/ports/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using namespace reactor;
using namespace std::chrono_literals;

class Trigger : public Reactor {
class Trigger final : public Reactor {
private:
Timer timer;
Reaction r_timer{"r_timer", 1, this, [this]() { on_timer(); }};
Expand All @@ -25,7 +25,7 @@ class Trigger : public Reactor {
void on_timer() { trigger.set(); }
};

class Counter : public Reactor {
class Counter final : public Reactor {
private:
int value_{0};
Reaction r_trigger{"r_trigger", 1, this, [this]() { on_trigger(); }};
Expand All @@ -49,7 +49,7 @@ class Counter : public Reactor {
}
};

class Printer : public Reactor {
class Printer final : public Reactor {
private:
Reaction r_value{"r_value", 1, this, [this]() { this->on_value(); }};

Expand All @@ -67,7 +67,7 @@ class Printer : public Reactor {
void on_value() { std::cout << this->name() << ": " << *value.get() << '\n'; }
};

class Adder : public Reactor {
class Adder final : public Reactor {
private:
Reaction r_add{"r_add", 1, this, [this]() { this->add(); }};

Expand Down
Loading

0 comments on commit b941c4b

Please sign in to comment.