diff --git a/src/adt2amas.cpp b/src/adt2amas.cpp index 0c7efd9f84cd9124c9618df0a047b05793b284c4..14888affed1f19c32e87a21ae2d21ffdf180537d 100644 --- a/src/adt2amas.cpp +++ b/src/adt2amas.cpp @@ -4,8 +4,11 @@ int main(){ //System Automata *amata = new Automata(); + //Initial states + State *state_zero = new State({StateType::Initial}); + //A bunch of automaton - Automaton *amaton_one = new Automaton(); + Automaton *amaton_one = new Automaton(state_zero); /* Automaton *amaton_two = new Automaton(); Automaton *amaton_three = new Automaton(); Automaton *amaton_four = new Automaton(); @@ -16,19 +19,27 @@ int main(){ Automaton *amaton_nine = new Automaton(); */ //A bunch of states - State *state_zero = new State({StateType::Initial}); State *state_one = new State({StateType::Normal}); State *state_two = new State({StateType::Normal}); State *state_three = new State({StateType::Normal}); State *state_four = new State({StateType::Normal}); + // A bunch of channels + + Channel *channel_one = new Channel("TF_ok"); + Channel *channel_two = new Channel("p_nok"); + Channel *channel_three = new Channel("TF_nok"); + Channel *channel_four = new Channel("p_ok"); + Channel *channel_five = new Channel("TS_ok"); + Channel *channel_six = new Channel("TS_nok"); + //A bunch of actions - Channel *channel_one = new Channel("TF_ok", {ChannelType::Receive}); - Channel *channel_two = new Channel("p_nok", {ChannelType::Receive}); - Channel *channel_three = new Channel("TF_nok", {ChannelType::Receive}); - Channel *channel_four = new Channel("p_ok", {ChannelType::Receive}); - Channel *channel_five = new Channel("TS_ok", {ChannelType::Send}); - Channel *channel_six = new Channel("TS_nok", {ChannelType::Send}); + Synchronization *synchronization_one = new Synchronization( {SynchronizationType::Receive}, channel_one); + Synchronization *synchronization_two = new Synchronization( {SynchronizationType::Receive}, channel_two); + Synchronization *synchronization_three = new Synchronization( {SynchronizationType::Receive}, channel_three); + Synchronization *synchronization_four = new Synchronization( {SynchronizationType::Receive}, channel_four); + Synchronization *synchronization_five = new Synchronization( {SynchronizationType::Send}, channel_five); + Synchronization *synchronization_six = new Synchronization( {SynchronizationType::Send}, channel_six); Label *label_one = new Label("TS"); @@ -42,20 +53,20 @@ int main(){ //Adding transitions - amaton_one->add_transition("", state_zero, state_one, channel_one); - amaton_one->add_transition("", state_one, state_two, channel_two); - amaton_one->add_transition("", state_two, state_three, label_one); - amaton_one->add_transition("", state_three, state_three, channel_five); - amaton_one->add_transition("", state_zero, state_four, channel_three); - amaton_one->add_transition("", state_zero, state_three, channel_four); - amaton_one->add_transition("", state_four, state_four, channel_six); + amaton_one->add_transition(state_zero, state_one, synchronization_one); + amaton_one->add_transition(state_one, state_two, synchronization_two); + amaton_one->add_transition(state_two, state_three, label_one); + amaton_one->add_transition(state_three, state_three, synchronization_five); + amaton_one->add_transition(state_zero, state_four, synchronization_three); + amaton_one->add_transition(state_zero, state_three, synchronization_four); + amaton_one->add_transition(state_four, state_four, synchronization_six); StateType stateTest{StateType::Normal}; cout << "state type = " << static_cast<int>(stateTest) << endl; - ChannelType channelTest{ChannelType::Receive}; - cout << "channel type = " << static_cast<int>(channelTest) << endl; + SynchronizationType synchronizationTest{SynchronizationType::Receive}; + cout << "synchronization type = " << static_cast<int>(synchronizationTest) << endl; return 0; } diff --git a/src/amas/CMakeLists.txt b/src/amas/CMakeLists.txt index 004a8c239de068bc3892f6eb890c8dbb53a902f0..62d9d987756b3255fb8e9ab5f28770a83d0380e6 100644 --- a/src/amas/CMakeLists.txt +++ b/src/amas/CMakeLists.txt @@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.0.0) # Asynchronous Multi-Agent System Library -add_library(amas_lib action.cpp channel.cpp channel_type.cpp label.cpp transition.cpp state.cpp state_type.cpp automata.cpp automaton.cpp) +add_library(amas_lib action.cpp channel.cpp synchronization.cpp synchronization_type.cpp label.cpp transition.cpp state.cpp state_type.cpp automata.cpp automaton.cpp) target_include_directories(amas_lib PUBLIC ${CMAKE_CURRENT_LIST_DIR}) diff --git a/src/amas/action.cpp b/src/amas/action.cpp index 193ba9b6d4115dab1fd23dc23ae8c54f9d3dbb37..12103ae45cceea507f4ed57f5fbbb8494f422dd3 100644 --- a/src/amas/action.cpp +++ b/src/amas/action.cpp @@ -1,17 +1,5 @@ #include "action.hpp" -Action::Action(string name): name_(name){} +Action::Action(){} Action::~Action(){} - -/* Gets */ - -string Action::get_name(){ - return this->name_; -} - -/* Sets */ - -void Action::set_name(string name){ - this->name_ = name; -} \ No newline at end of file diff --git a/src/amas/action.hpp b/src/amas/action.hpp index 5b570c40455056ebec08e8a7b7241e4f42f6ca32..24b0447c54041b876a1d19381c7a65919fd9aa8c 100644 --- a/src/amas/action.hpp +++ b/src/amas/action.hpp @@ -7,20 +7,9 @@ using namespace std; class Action { - private: - string name_; - public: - Action(string name = ""); + Action(); ~Action(); - - /* Gets */ - - string get_name(); - - /* Sets */ - - void set_name(string name); - }; + #endif \ No newline at end of file diff --git a/src/amas/automata.cpp b/src/amas/automata.cpp index 71a2beb0806ea993909a3831f95d27d67065faf3..fc1bbbc27a54b2ae5fb170e0ddc5158c48be4d24 100644 --- a/src/amas/automata.cpp +++ b/src/amas/automata.cpp @@ -1,8 +1,6 @@ #include "automata.hpp" -Automata::Automata(vector<Automaton*> vector_automaton, set<int> automaton_ids) : vector_automaton_(vector_automaton), - automaton_ids_(automaton_ids) - {} +Automata::Automata(){} Automata::~Automata(){} @@ -15,6 +13,10 @@ set<int> Automata::get_automaton_ids(){ return this->automaton_ids_; } +vector<Channel*> Automata::get_channels(){ + return this->vector_channels_; +} + /* Sets */ void Automata::set_vector_automaton(vector<Automaton*> vector_automaton){ this->vector_automaton_ = vector_automaton; @@ -23,11 +25,25 @@ void Automata::set_vector_automaton(vector<Automaton*> vector_automaton){ void Automata::set_automaton_ids(set<int> automaton_ids){ this->automaton_ids_ = automaton_ids; } + +void Automata::set_channels(vector<Channel*> vector_channels){ + this->vector_channels_ = vector_channels; +} /* Other methods */ +// Adding... void Automata::add_automaton(Automaton *new_automaton){ auto automaton_in_automata = this->automaton_ids_.find(new_automaton->get_id()) == this->automaton_ids_.end(); assert(("automaton can't be in the automata to allow their addition", automaton_in_automata)); this->automaton_ids_.insert(new_automaton->get_id()); this->vector_automaton_.push_back(new_automaton); -} \ No newline at end of file +} + +void Automata::add_channel(Channel *new_channel){ + auto channel_in_automata = this->channel_ids_.find(new_channel->get_id()) == this->channel_ids_.end(); + assert(("channel can't be in the automata to allow their addition", channel_in_automata)); + this->channel_ids_.insert(new_channel->get_id()); + this->vector_channels_.push_back(new_channel); +} + +// Removing... diff --git a/src/amas/automata.hpp b/src/amas/automata.hpp index dc71e76da8c0ebe8760a2ec55988e6ea48b00348..6609f7596497161fe8fddbfb5addeec20716070d 100644 --- a/src/amas/automata.hpp +++ b/src/amas/automata.hpp @@ -2,27 +2,33 @@ #define AUTOMATA_HPP #include "automaton.hpp" -#include "transition.hpp" +#include "channel.hpp" + class Automata { private: set<int> automaton_ids_; + set<int> channel_ids_; vector<Automaton*> vector_automaton_; + vector<Channel*> vector_channels_; public: - Automata(vector<Automaton*> vector_automaton = {}, set<int> automaton_ids = {}); + Automata(); ~Automata(); /* Gets */ vector<Automaton*> get_vector_automaton(); set<int> get_automaton_ids(); + vector<Channel*> get_channels(); /* Sets */ void set_vector_automaton(vector<Automaton*> vector_automaton); void set_automaton_ids(set<int> automaton_ids); + void set_channels(vector<Channel*> vector_channels); /*Other methods */ void add_automaton(Automaton *new_automaton); + void add_channel(Channel *new_channel); }; diff --git a/src/amas/automaton.cpp b/src/amas/automaton.cpp index be6631d007dace56146762aeaced4f0c3c200c74..4d481bd5670872c6a6d291ea40b212ab4017407a 100644 --- a/src/amas/automaton.cpp +++ b/src/amas/automaton.cpp @@ -3,6 +3,7 @@ Automaton::Automaton(State* initial_state): initial_state_(initial_state){ ++current_id; id = current_id; + vector_states_.push_back(initial_state); } Automaton::~Automaton(){} @@ -36,7 +37,11 @@ vector<Transition*> Automaton::get_transitions(){ /* Sets */ void Automaton::set_initial_state(State* initial_state){ + State *tmp_state = new State; + tmp_state = this->initial_state_; this->initial_state_ = initial_state; + remove_state(tmp_state); + vector_states_.push_back(initial_state); } void Automaton::set_transition_ids(set<int> transition_ids){ @@ -61,12 +66,12 @@ int Automaton::current_id = 0; // Adding... -void Automaton::add_transition(string goal, State *source_state, State *destination_state, Action *action){ +void Automaton::add_transition(State *source_state, State *destination_state, Action *action){ auto source_state_in_automaton = this->state_ids_.find(source_state->get_id()) != this->state_ids_.end(); auto dest_state_in_automaton = this->state_ids_.find(destination_state->get_id()) != this->state_ids_.end(); assert(("both states have to exist already in the automaton", source_state_in_automaton && dest_state_in_automaton)); - Transition *new_transition = new Transition(goal, source_state, destination_state, action); + Transition *new_transition = new Transition(source_state, destination_state, action); vector<Transition*> tmp_transitions = source_state->get_transitions(); tmp_transitions.push_back(new_transition); @@ -116,6 +121,8 @@ void Automaton::remove_transition(Transition *cur_transition){ void Automaton::remove_state(State *cur_state){ auto state_in_automaton = this->state_ids_.find(cur_state->get_id()) != this->state_ids_.end(); assert(("state can't be removed since it doesn't belong to this automaton", state_in_automaton)); + auto no_initial_state = cur_state->get_id() != this->initial_state_->get_id(); + assert(("you can't remove their automaton initial state before replace it", no_initial_state)); vector<Transition*> tmp_transitions = cur_state->get_transitions(); tmp_transitions.clear(); diff --git a/src/amas/automaton.hpp b/src/amas/automaton.hpp index c1ec97a6c4e7460a9e9b863ffd7ed08d8bca2157..fd823ff4797226d4474c6a8b530b4f9172c41295 100644 --- a/src/amas/automaton.hpp +++ b/src/amas/automaton.hpp @@ -2,6 +2,7 @@ #define AUTOMATON_HPP #include "transition.hpp" +#include "state.hpp" class Automaton { @@ -15,7 +16,7 @@ class Automaton vector<Transition*> vector_transitions_; public: - Automaton(State *initial_state = nullptr); + Automaton(State *initial_state); ~Automaton(); /* Gets */ @@ -25,6 +26,8 @@ class Automaton set<int> get_transition_ids(); vector<State*> get_states(); vector<Transition*> get_transitions(); + string get_info(); + /* Sets */ void set_initial_state(State *initial_state); @@ -32,12 +35,13 @@ class Automaton void set_transition_ids(set<int> transition_ids); void set_states(vector<State*> vector_states); void set_transitions(vector<Transition*> vector_transitions); + /* Other methods */ // Adding... - void add_transition(string goal, State *source_state, State *destination_state, Action *action); + void add_transition(State *source_state, State *destination_state, Action *action); void add_state(State *new_state); - // Remving... + // Removing... void remove_transition(Transition *cur_transition); void remove_state(State *cur_state); diff --git a/src/amas/channel.cpp b/src/amas/channel.cpp index 7b738125fe4846a6f01c265021537d153f7dc84d..f57c3b868fc8b0b5b1dae03c8ee609ec73f8d5c6 100644 --- a/src/amas/channel.cpp +++ b/src/amas/channel.cpp @@ -1,18 +1,23 @@ #include "channel.hpp" -Channel::Channel(string name, ChannelType channel_type) : channel_type_(channel_type), - Action(name){} +Channel::Channel(string name): name_(name){} Channel::~Channel(){} /* Gets */ -int Channel::get_channel_type(){ - return static_cast<int>(this->channel_type_); +int Channel::get_id(){ + return this->id; +} + +string Channel::get_name(){ + return this->name_; } /* Sets */ -void Channel::set_channel_type(ChannelType channel_type){ - this->channel_type_ = channel_type; -} \ No newline at end of file +void Channel::set_name(string name){ + this->name_ = name; +} + +int Channel::current_id = 0; \ No newline at end of file diff --git a/src/amas/channel.hpp b/src/amas/channel.hpp index 04f15c246412c537198d0adb2c4233d988b63f89..e5ec8fdb4e39f4634448eb0cc38ccf963f749b1a 100644 --- a/src/amas/channel.hpp +++ b/src/amas/channel.hpp @@ -1,30 +1,28 @@ #ifndef CHANNEL_HPP #define CHANNEL_HPP -#include "action.hpp" -#include "channel_type.hpp" +#include <iostream> -class Channel: public Action +using namespace std; + +class Channel { private: - - ChannelType channel_type_; - + int id; + static int current_id; + string name_; public: - - Channel(string name = "", ChannelType channel_type = static_cast<ChannelType>(0)); + Channel(string name = ""); ~Channel(); /* Gets */ - int get_channel_type(); + int get_id(); + string get_name(); /* Sets */ - void set_channel_type(ChannelType channel_type); - - /* Others methods*/ - + void set_name(string name); + }; - #endif \ No newline at end of file diff --git a/src/amas/channel_type.cpp b/src/amas/channel_type.cpp deleted file mode 100644 index c8f2ac82fb6f4e34d68163c3d1f183db43a05913..0000000000000000000000000000000000000000 --- a/src/amas/channel_type.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "channel_type.hpp" - -string to_string(ChannelType ch){ - - string _ch = ""; - switch (ch) - { - case ChannelType::Send: - _ch = "Send"; - break; - case ChannelType::Receive: - _ch = "Receive"; - break; - default: - break; - } - - return _ch; -} \ No newline at end of file diff --git a/src/amas/channel_type.hpp b/src/amas/channel_type.hpp deleted file mode 100644 index e77c92da969f6d7d063133dc8bb9ae2515884155..0000000000000000000000000000000000000000 --- a/src/amas/channel_type.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef CHANNEL_TYPE_HPP -#define CHANNEL_TYPE_HPP - -#include <iostream> - -using namespace std; - -enum class ChannelType -{ - Send, // 0 - Receive // 1 -}; - -// methods - -string to_string(ChannelType ch); - -#endif \ No newline at end of file diff --git a/src/amas/label.cpp b/src/amas/label.cpp index baedb3d1d6715dbee91dc8cb3bf46e60eef6fcff..8d8459f9210e84ba2a8a105b392428c25914c6c2 100644 --- a/src/amas/label.cpp +++ b/src/amas/label.cpp @@ -1,5 +1,17 @@ #include "label.hpp" -Label::Label(string name) : Action(name){} +Label::Label(string name) : name_(name){} -Label::~Label(){} \ No newline at end of file +Label::~Label(){} + +/* Gets */ + +string Label::get_name(){ + return this->name_; +} + +/* Sets */ + +void Label::set_name(string name){ + this->name_ = name; +} \ No newline at end of file diff --git a/src/amas/label.hpp b/src/amas/label.hpp index 2537d5bc91b2688185ab7631414d575d9ee882f4..fd2e3f1ef064e7c3ea4498e490475fd4ede253e7 100644 --- a/src/amas/label.hpp +++ b/src/amas/label.hpp @@ -5,9 +5,19 @@ class Label: public Action { + private: + string name_; public: - Label(string name); + Label(string name = ""); ~Label(); + + /* Gets */ + + string get_name(); + + /* Sets */ + + void set_name(string name); }; #endif \ No newline at end of file diff --git a/src/amas/state.cpp b/src/amas/state.cpp index 7cc8dddab3cc67d18fa5bf0ffee4d1f1d374038e..f368dc52a3e9c134ed5203e311b17464ce6f521f 100644 --- a/src/amas/state.cpp +++ b/src/amas/state.cpp @@ -22,6 +22,12 @@ vector<Transition*> State::get_transitions(){ return this->transitions_; } +string State::get_info(){ + string info_state = to_string(this->id); + info_state += "(" + to_string(this->state_type_) + ")"; + return info_state; +} + /* Sets */ void State::set_state_type(StateType state_type){ diff --git a/src/amas/state.hpp b/src/amas/state.hpp index f5ca7cc4265630683ee808589017e1f97b022705..78b0a54d3249b8eee39e1250192a1dd469495388 100644 --- a/src/amas/state.hpp +++ b/src/amas/state.hpp @@ -26,6 +26,8 @@ class State StateType get_state_type(); vector<Transition*> get_transitions(); + string get_info(); + /* Sets */ void set_state_type(StateType state_type); diff --git a/src/amas/synchronization.cpp b/src/amas/synchronization.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed2cfd06e4bc1f051c7b9cef6436377a7fc4fdd3 --- /dev/null +++ b/src/amas/synchronization.cpp @@ -0,0 +1,25 @@ +#include "synchronization.hpp" + +Synchronization::Synchronization(SynchronizationType synchronization_type, Channel *channel) : synchronization_type_(synchronization_type), channel_(channel){} + +Synchronization::~Synchronization(){} + +/* Gets */ + +int Synchronization::get_synchronization_type(){ + return static_cast<int>(this->synchronization_type_); +} + +Channel* Synchronization::get_channel(){ + return this->channel_; +} + +/* Sets */ + +void Synchronization::set_synchronization_type(SynchronizationType synchronization_type){ + this->synchronization_type_ = synchronization_type; +} + +void Synchronization::set_channel(Channel *channel){ + this->channel_ = channel; +} diff --git a/src/amas/synchronization.hpp b/src/amas/synchronization.hpp new file mode 100644 index 0000000000000000000000000000000000000000..726d9f2363f6f8d7d3f574e58b5ba0969cf1f0e6 --- /dev/null +++ b/src/amas/synchronization.hpp @@ -0,0 +1,34 @@ +#ifndef SYNCHRONIZATION_HPP +#define SYNCHRONIZATION_HPP + +#include "action.hpp" +#include "synchronization_type.hpp" +#include "channel.hpp" + +class Synchronization: public Action +{ + private: + + SynchronizationType synchronization_type_; + Channel *channel_; + + public: + + Synchronization(SynchronizationType synchronization_type = static_cast<SynchronizationType>(0), Channel *channel = nullptr); + ~Synchronization(); + + /* Gets */ + + int get_synchronization_type(); + Channel* get_channel(); + + /* Sets */ + + void set_synchronization_type(SynchronizationType synchronization_type); + void set_channel(Channel *channel); + + /* Others methods*/ + +}; + +#endif \ No newline at end of file diff --git a/src/amas/synchronization_type.cpp b/src/amas/synchronization_type.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67935dd96d1972f9d75be57559e56ac3a3cc8343 --- /dev/null +++ b/src/amas/synchronization_type.cpp @@ -0,0 +1,19 @@ +#include "synchronization_type.hpp" + +string to_string(SynchronizationType sh){ + + string sh_ = ""; + switch (sh) + { + case SynchronizationType::Send: + sh_ = "Send"; + break; + case SynchronizationType::Receive: + sh_ = "Receive"; + break; + default: + break; + } + + return sh_; +} \ No newline at end of file diff --git a/src/amas/synchronization_type.hpp b/src/amas/synchronization_type.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f530b2e9406000a554201279e802ef23dd289cb1 --- /dev/null +++ b/src/amas/synchronization_type.hpp @@ -0,0 +1,18 @@ +#ifndef SYNCHRONIZATION_TYPE_HPP +#define SYNCHRONIZATION_TYPE_HPP + +#include <iostream> + +using namespace std; + +enum class SynchronizationType +{ + Send, // 0 + Receive // 1 +}; + +// methods + +string to_string(SynchronizationType sh); + +#endif \ No newline at end of file diff --git a/src/amas/transition.cpp b/src/amas/transition.cpp index 43e06f16ae8732637b49c34ec56c5944eed673e1..ac3072e69d341991e5c6f2df6694f4721fa9e746 100644 --- a/src/amas/transition.cpp +++ b/src/amas/transition.cpp @@ -1,7 +1,7 @@ #include "transition.hpp" -Transition::Transition(string goal, State *source_state, State *destination_state, Action *action): - goal_(goal), source_state_(source_state), destination_state_(destination_state), action_(action){ +Transition::Transition(State *source_state, State *destination_state, Action *action): + source_state_(source_state), destination_state_(destination_state), action_(action){ ++current_id; id = current_id; } @@ -18,10 +18,6 @@ int Transition::get_id(){ return this->id; } -string Transition::get_goal(){ - return this->goal_; -} - State* Transition::get_source_state(){ return this->source_state_; } @@ -36,10 +32,6 @@ Action* Transition::get_action(){ /* Sets */ -void Transition::set_goal(string goal){ - this->goal_ = goal; -} - void Transition::set_source_state(State *source_state){ this->source_state_ = source_state; } diff --git a/src/amas/transition.hpp b/src/amas/transition.hpp index 6db947060a06b3e7145b05ed26569ce9ead354df..37974079c55cee7d4ae86fdf80e88dd16174f554 100644 --- a/src/amas/transition.hpp +++ b/src/amas/transition.hpp @@ -3,8 +3,7 @@ #include <bits/stdc++.h> // is it good idea to include this header? #include "state.hpp" -#include "action.hpp" -#include "channel.hpp" +#include "synchronization.hpp" #include "label.hpp" using namespace std; @@ -16,20 +15,18 @@ class Transition int id; static int current_id; - string goal_; State *source_state_; State *destination_state_; Action *action_; public: - Transition(string goal = "", State *source_state = nullptr, State *destination_state = nullptr, Action *action = nullptr); + Transition(State *source_state = nullptr, State *destination_state = nullptr, Action *action = nullptr); ~Transition(); /* Gets */ int get_id(); - string get_goal(); State* get_source_state(); State* get_destination_state(); Action* get_action(); @@ -37,7 +34,6 @@ class Transition /* Sets */ /*void set_id();*/ - void set_goal(string goal); void set_source_state(State *source_state); void set_destination_state(State *destination_state); void set_action(Action *action);