Skip to content
Snippets Groups Projects
Commit eef0e9c1 authored by Giann Karlo Aguirre Samboní's avatar Giann Karlo Aguirre Samboní
Browse files

Adding and removing states and transitions implemented

parent 798ad577
No related branches found
No related tags found
No related merge requests found
...@@ -43,9 +43,7 @@ int main(){ ...@@ -43,9 +43,7 @@ int main(){
Label *label_one = new Label("TS"); Label *label_one = new Label("TS");
//Adding states //Adding states
amaton_one->add_state(state_zero);
amaton_one->add_state(state_one); amaton_one->add_state(state_one);
amaton_one->add_state(state_two); amaton_one->add_state(state_two);
amaton_one->add_state(state_three); amaton_one->add_state(state_three);
...@@ -58,15 +56,33 @@ int main(){ ...@@ -58,15 +56,33 @@ int main(){
amaton_one->add_transition(state_two, state_three, label_one); 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_three, state_three, synchronization_five);
amaton_one->add_transition(state_zero, state_four, synchronization_three); 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_zero, state_four, synchronization_four);
amaton_one->add_transition(state_four, state_four, synchronization_six); amaton_one->add_transition(state_four, state_four, synchronization_six);
//Adding channels
amata->add_channel(channel_one);
amata->add_channel(channel_two);
amata->add_channel(channel_three);
amata->add_channel(channel_four);
amata->add_channel(channel_five);
amata->add_channel(channel_six);
//Adding automaton
amata->add_automaton(amaton_one);
cout << amata->get_info();
StateType stateTest{StateType::Normal}; //Removing...
cout << "Removing..." << endl;
amaton_one->remove_state(state_four);
//amaton_one->remove_transition(7);
cout << amata->get_info();
/* StateType stateTest{StateType::Normal};
cout << "state type = " << static_cast<int>(stateTest) << endl; cout << "state type = " << static_cast<int>(stateTest) << endl;
SynchronizationType synchronizationTest{SynchronizationType::Receive}; SynchronizationType synchronizationTest{SynchronizationType::Receive};
cout << "synchronization type = " << static_cast<int>(synchronizationTest) << endl; cout << "synchronization type = " << static_cast<int>(synchronizationTest) << endl; */
return 0; return 0;
} }
...@@ -10,6 +10,8 @@ class Action ...@@ -10,6 +10,8 @@ class Action
public: public:
Action(); Action();
~Action(); ~Action();
virtual string get_info() = 0;
}; };
#endif #endif
\ No newline at end of file
...@@ -14,7 +14,18 @@ set<int> Automata::get_automaton_ids(){ ...@@ -14,7 +14,18 @@ set<int> Automata::get_automaton_ids(){
} }
vector<Channel*> Automata::get_channels(){ vector<Channel*> Automata::get_channels(){
return this->vector_channels_; return this->channels_;
}
string Automata::get_info(){
string info_amata = "";
for(auto& i : this->vector_automaton_){
info_amata += i->get_info() + "\n";
}
for(auto& i : this->channels_){
info_amata += i->get_info() + "\n";
}
return info_amata;
} }
/* Sets */ /* Sets */
...@@ -26,8 +37,8 @@ void Automata::set_automaton_ids(set<int> automaton_ids){ ...@@ -26,8 +37,8 @@ void Automata::set_automaton_ids(set<int> automaton_ids){
this->automaton_ids_ = automaton_ids; this->automaton_ids_ = automaton_ids;
} }
void Automata::set_channels(vector<Channel*> vector_channels){ void Automata::set_channels(vector<Channel*> channels){
this->vector_channels_ = vector_channels; this->channels_ = channels;
} }
/* Other methods */ /* Other methods */
...@@ -43,7 +54,7 @@ void Automata::add_channel(Channel *new_channel){ ...@@ -43,7 +54,7 @@ void Automata::add_channel(Channel *new_channel){
auto channel_in_automata = this->channel_ids_.find(new_channel->get_id()) == this->channel_ids_.end(); 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)); assert(("channel can't be in the automata to allow their addition", channel_in_automata));
this->channel_ids_.insert(new_channel->get_id()); this->channel_ids_.insert(new_channel->get_id());
this->vector_channels_.push_back(new_channel); this->channels_.push_back(new_channel);
} }
// Removing... // Removing...
...@@ -11,7 +11,7 @@ class Automata ...@@ -11,7 +11,7 @@ class Automata
set<int> automaton_ids_; set<int> automaton_ids_;
set<int> channel_ids_; set<int> channel_ids_;
vector<Automaton*> vector_automaton_; vector<Automaton*> vector_automaton_;
vector<Channel*> vector_channels_; vector<Channel*> channels_;
public: public:
Automata(); Automata();
~Automata(); ~Automata();
...@@ -20,11 +20,12 @@ class Automata ...@@ -20,11 +20,12 @@ class Automata
vector<Automaton*> get_vector_automaton(); vector<Automaton*> get_vector_automaton();
set<int> get_automaton_ids(); set<int> get_automaton_ids();
vector<Channel*> get_channels(); vector<Channel*> get_channels();
string get_info();
/* Sets */ /* Sets */
void set_vector_automaton(vector<Automaton*> vector_automaton); void set_vector_automaton(vector<Automaton*> vector_automaton);
void set_automaton_ids(set<int> automaton_ids); void set_automaton_ids(set<int> automaton_ids);
void set_channels(vector<Channel*> vector_channels); void set_channels(vector<Channel*> channels);
/*Other methods */ /*Other methods */
void add_automaton(Automaton *new_automaton); void add_automaton(Automaton *new_automaton);
......
#include "automaton.hpp" #include "automaton.hpp"
Automaton::Automaton(State* initial_state): initial_state_(initial_state){ Automaton::Automaton(State* initial_state): initial_state_(initial_state){
auto correct_type = initial_state->get_state_type() == StateType::Initial;
assert(("Incorrect type for the parameter initial_state, must be Initial type", correct_type));
++current_id; ++current_id;
id = current_id; id = current_id;
vector_states_.push_back(initial_state); this->state_ids_.insert(initial_state->get_id());
this->states_.push_back(initial_state);
} }
Automaton::~Automaton(){} Automaton::~Automaton(){}
...@@ -27,11 +30,20 @@ set<int> Automaton::get_state_ids(){ ...@@ -27,11 +30,20 @@ set<int> Automaton::get_state_ids(){
} }
vector<State*> Automaton::get_states(){ vector<State*> Automaton::get_states(){
return this->vector_states_; return this->states_;
} }
vector<Transition*> Automaton::get_transitions(){ vector<Transition*> Automaton::get_transitions(){
return this->vector_transitions_; return this->transitions_;
}
string Automaton::get_info(){
string info_amaton = "Automaton " + to_string(this->id) + ":\n";
for(auto& i : this->states_){
info_amaton += "\t" + i->get_info(2);
info_amaton += i != this->states_.back() ? "\n" : "";
}
return info_amaton;
} }
/* Sets */ /* Sets */
...@@ -40,8 +52,10 @@ void Automaton::set_initial_state(State* initial_state){ ...@@ -40,8 +52,10 @@ void Automaton::set_initial_state(State* initial_state){
State *tmp_state = new State; State *tmp_state = new State;
tmp_state = this->initial_state_; tmp_state = this->initial_state_;
this->initial_state_ = initial_state; this->initial_state_ = initial_state;
remove_state(tmp_state); tmp_state->set_state_type(StateType::Normal);
vector_states_.push_back(initial_state); this->initial_state_->set_state_type(StateType::Initial);
this->state_ids_.insert(initial_state->get_id());
this->states_.push_back(initial_state);
} }
void Automaton::set_transition_ids(set<int> transition_ids){ void Automaton::set_transition_ids(set<int> transition_ids){
...@@ -53,11 +67,11 @@ void Automaton::set_state_ids(set<int> state_ids){ ...@@ -53,11 +67,11 @@ void Automaton::set_state_ids(set<int> state_ids){
} }
void Automaton::set_states(vector<State*> vector_states){ void Automaton::set_states(vector<State*> vector_states){
this->vector_states_ = vector_states; this->states_ = vector_states;
} }
void Automaton::set_transitions(vector<Transition*> vector_transitions){ void Automaton::set_transitions(vector<Transition*> vector_transitions){
this->vector_transitions_ = vector_transitions; this->transitions_ = vector_transitions;
} }
int Automaton::current_id = 0; int Automaton::current_id = 0;
...@@ -82,21 +96,32 @@ void Automaton::add_transition(State *source_state, State *destination_state, Ac ...@@ -82,21 +96,32 @@ void Automaton::add_transition(State *source_state, State *destination_state, Ac
destination_state->set_transitions(tmp_transitions); destination_state->set_transitions(tmp_transitions);
this->transition_ids_.insert(new_transition->get_id()); this->transition_ids_.insert(new_transition->get_id());
this->vector_transitions_.push_back(new_transition); this->transitions_.push_back(new_transition);
} }
void Automaton::add_state(State *new_state){ void Automaton::add_state(State *new_state){
auto state_in_automaton = this->state_ids_.find(new_state->get_id()) == this->state_ids_.end(); auto state_in_automaton = this->state_ids_.find(new_state->get_id()) == this->state_ids_.end();
assert(("state can't be added to the automaton because it exists already", state_in_automaton)); assert(("state can't be added to the automaton because it exists already", state_in_automaton));
this->state_ids_.insert(new_state->get_id()); this->state_ids_.insert(new_state->get_id());
this->vector_states_.push_back(new_state); this->states_.push_back(new_state);
} }
// Removing... // Removing...
void Automaton::remove_transition(int transition_id){
auto transition_in_automaton = this->transition_ids_.find(transition_id) != this->transition_ids_.end();
assert(("transition can't be removed since it doesn't belong to this automaton", transition_in_automaton));
auto it = this->transitions_.begin();
for(; it != this->transitions_.end() && (*it)->get_id() != transition_id; it++){}
remove_transition(*it);
}
void Automaton::remove_transition(Transition *cur_transition){ void Automaton::remove_transition(Transition *cur_transition){
auto transition_in_automaton = this->transition_ids_.find(cur_transition->get_id()) != this->transition_ids_.end(); auto transition_in_automaton = this->transition_ids_.find(cur_transition->get_id()) != this->transition_ids_.end();
assert(("transition can't be removed since it doesn't belong to this automaton", transition_in_automaton)); assert(("transition can't be removed since it doesn't belong to this automaton", transition_in_automaton));
bool more_transitions_initial_state = 1;
if (cur_transition->get_source_state() == this->initial_state_ || cur_transition->get_destination_state() == this->initial_state_)
more_transitions_initial_state = this->initial_state_->get_transitions().size() >= 1;
assert(("You can't remove this transition since it is the unique transition associated with the initial state", more_transitions_initial_state));
vector<Transition*> tmp_transitions = cur_transition->get_source_state()->get_transitions(); vector<Transition*> tmp_transitions = cur_transition->get_source_state()->get_transitions();
auto it = tmp_transitions.begin(); auto it = tmp_transitions.begin();
...@@ -110,11 +135,10 @@ void Automaton::remove_transition(Transition *cur_transition){ ...@@ -110,11 +135,10 @@ void Automaton::remove_transition(Transition *cur_transition){
it = tmp_transitions.erase(it); it = tmp_transitions.erase(it);
cur_transition->get_destination_state()->set_transitions(tmp_transitions); cur_transition->get_destination_state()->set_transitions(tmp_transitions);
it = this->vector_transitions_.begin(); it = this->transitions_.begin();
for(; it != this->vector_transitions_.end() && (*it)->get_id() != cur_transition->get_id(); it++){} for(; it != this->transitions_.end() && (*it)->get_id() != cur_transition->get_id(); it++){}
it = this->vector_transitions_.erase(it); it = this->transitions_.erase(it);
this->transition_ids_.erase(cur_transition->get_id()); this->transition_ids_.erase(cur_transition->get_id());
delete cur_transition; delete cur_transition;
} }
...@@ -124,12 +148,13 @@ void Automaton::remove_state(State *cur_state){ ...@@ -124,12 +148,13 @@ void Automaton::remove_state(State *cur_state){
auto no_initial_state = cur_state->get_id() != this->initial_state_->get_id(); 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)); 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(); for(int i = 0; i < cur_state->get_transitions().size(); i++){
cur_state->set_transitions(tmp_transitions); remove_transition(cur_state->get_transitions()[i]);
}
auto it = this->vector_states_.begin(); auto it = this->states_.begin();
for(; it != this->vector_states_.end() && (*it)->get_id() != cur_state->get_id(); it++){} for(; it != this->states_.end() && (*it)->get_id() != cur_state->get_id(); it++){}
it = this->vector_states_.erase(it); it = this->states_.erase(it);
this->state_ids_.erase(cur_state->get_id()); this->state_ids_.erase(cur_state->get_id());
} }
...@@ -12,8 +12,8 @@ class Automaton ...@@ -12,8 +12,8 @@ class Automaton
State *initial_state_; State *initial_state_;
set<int> state_ids_; set<int> state_ids_;
set<int> transition_ids_; set<int> transition_ids_;
vector<State*> vector_states_; vector<State*> states_;
vector<Transition*> vector_transitions_; vector<Transition*> transitions_;
public: public:
Automaton(State *initial_state); Automaton(State *initial_state);
...@@ -28,7 +28,6 @@ class Automaton ...@@ -28,7 +28,6 @@ class Automaton
vector<Transition*> get_transitions(); vector<Transition*> get_transitions();
string get_info(); string get_info();
/* Sets */ /* Sets */
void set_initial_state(State *initial_state); void set_initial_state(State *initial_state);
void set_state_ids(set<int> state_ids); void set_state_ids(set<int> state_ids);
...@@ -43,6 +42,7 @@ class Automaton ...@@ -43,6 +42,7 @@ class Automaton
void add_state(State *new_state); void add_state(State *new_state);
// Removing... // Removing...
void remove_transition(Transition *cur_transition); void remove_transition(Transition *cur_transition);
void remove_transition(int transition_id);
void remove_state(State *cur_state); void remove_state(State *cur_state);
}; };
......
#include "channel.hpp" #include "channel.hpp"
Channel::Channel(string name): name_(name){} Channel::Channel(string name): name_(name){
++current_id;
id = current_id;
}
Channel::~Channel(){} Channel::~Channel(){}
...@@ -14,6 +17,10 @@ string Channel::get_name(){ ...@@ -14,6 +17,10 @@ string Channel::get_name(){
return this->name_; return this->name_;
} }
string Channel::get_info(){
string info_ch = "Channel " + to_string(this->id) + ": " + this->name_;
return info_ch;
}
/* Sets */ /* Sets */
void Channel::set_name(string name){ void Channel::set_name(string name){
......
...@@ -19,7 +19,8 @@ class Channel ...@@ -19,7 +19,8 @@ class Channel
int get_id(); int get_id();
string get_name(); string get_name();
string get_info();
/* Sets */ /* Sets */
void set_name(string name); void set_name(string name);
......
...@@ -10,6 +10,10 @@ string Label::get_name(){ ...@@ -10,6 +10,10 @@ string Label::get_name(){
return this->name_; return this->name_;
} }
string Label::get_info(){
return this->name_;
}
/* Sets */ /* Sets */
void Label::set_name(string name){ void Label::set_name(string name){
......
...@@ -14,6 +14,7 @@ class Label: public Action ...@@ -14,6 +14,7 @@ class Label: public Action
/* Gets */ /* Gets */
string get_name(); string get_name();
string get_info();
/* Sets */ /* Sets */
......
...@@ -22,9 +22,33 @@ vector<Transition*> State::get_transitions(){ ...@@ -22,9 +22,33 @@ vector<Transition*> State::get_transitions(){
return this->transitions_; return this->transitions_;
} }
string State::get_info(int tabs){
string tmp_info_transition;
string info_state = "State " + to_string(this->id) + " (" + to_string(this->state_type_) + "):\n";
tmp_info_transition.insert(0, tabs, '\t');
int j = 1;
for(auto& i : this->transitions_){
info_state += tmp_info_transition + i->get_info();
if (j == this->transitions_.size())
info_state += "";
else
info_state += "\n";
j++;
}
return info_state;
}
string State::get_info(){ string State::get_info(){
string info_state = to_string(this->id); string info_state = "State " + to_string(this->id) + " (" + to_string(this->state_type_) + "):\n";
info_state += "(" + to_string(this->state_type_) + ")"; int j = 1;
for(auto& i : this->transitions_){
info_state += "\t" + i->get_info();
if (j == this->transitions_.size())
info_state += "";
else
info_state += "\n";
j++;
}
return info_state; return info_state;
} }
......
...@@ -26,6 +26,7 @@ class State ...@@ -26,6 +26,7 @@ class State
StateType get_state_type(); StateType get_state_type();
vector<Transition*> get_transitions(); vector<Transition*> get_transitions();
string get_info(int tabs);
string get_info(); string get_info();
/* Sets */ /* Sets */
......
...@@ -14,6 +14,10 @@ Channel* Synchronization::get_channel(){ ...@@ -14,6 +14,10 @@ Channel* Synchronization::get_channel(){
return this->channel_; return this->channel_;
} }
string Synchronization::get_info(){
string info_sync = to_string(this->synchronization_type_) + this->channel_->get_name();
return info_sync;
}
/* Sets */ /* Sets */
void Synchronization::set_synchronization_type(SynchronizationType synchronization_type){ void Synchronization::set_synchronization_type(SynchronizationType synchronization_type){
......
...@@ -21,6 +21,7 @@ class Synchronization: public Action ...@@ -21,6 +21,7 @@ class Synchronization: public Action
int get_synchronization_type(); int get_synchronization_type();
Channel* get_channel(); Channel* get_channel();
string get_info();
/* Sets */ /* Sets */
......
...@@ -6,10 +6,10 @@ string to_string(SynchronizationType sh){ ...@@ -6,10 +6,10 @@ string to_string(SynchronizationType sh){
switch (sh) switch (sh)
{ {
case SynchronizationType::Send: case SynchronizationType::Send:
sh_ = "Send"; sh_ = "!";
break; break;
case SynchronizationType::Receive: case SynchronizationType::Receive:
sh_ = "Receive"; sh_ = "?";
break; break;
default: default:
break; break;
......
...@@ -7,8 +7,6 @@ Transition::Transition(State *source_state, State *destination_state, Action *ac ...@@ -7,8 +7,6 @@ Transition::Transition(State *source_state, State *destination_state, Action *ac
} }
Transition::~Transition(){ Transition::~Transition(){
delete source_state_;
delete destination_state_;
delete action_; delete action_;
} }
...@@ -30,6 +28,11 @@ Action* Transition::get_action(){ ...@@ -30,6 +28,11 @@ Action* Transition::get_action(){
return this->action_; return this->action_;
} }
string Transition::get_info(){
string info_tran = "Transition " + to_string(this->get_id()) + " " + this->action_->get_info();
return info_tran;
}
/* Sets */ /* Sets */
void Transition::set_source_state(State *source_state){ void Transition::set_source_state(State *source_state){
......
...@@ -30,6 +30,7 @@ class Transition ...@@ -30,6 +30,7 @@ class Transition
State* get_source_state(); State* get_source_state();
State* get_destination_state(); State* get_destination_state();
Action* get_action(); Action* get_action();
string get_info();
/* Sets */ /* Sets */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment