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

nand and nor gates added

parent 995f8036
No related branches found
No related tags found
No related merge requests found
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
int main(){ int main(){
//System //System
//Automata *amata = new Automata(); /* Automata *amata = new Automata();
//Initial states //Initial states
/* State *state_zero = new State({StateType::Initial}); State *state_zero = new State({StateType::Initial});
//A bunch of automaton //A bunch of automaton
Automaton *amaton_one = new Automaton(state_zero); Automaton *amaton_one = new Automaton(state_zero);
...@@ -18,7 +18,6 @@ int main(){ ...@@ -18,7 +18,6 @@ int main(){
State *state_four = new State({StateType::Normal}); State *state_four = new State({StateType::Normal});
// A bunch of channels // A bunch of channels
Channel *channel_one = new Channel("TF_ok"); Channel *channel_one = new Channel("TF_ok");
Channel *channel_two = new Channel("p_nok"); Channel *channel_two = new Channel("p_nok");
Channel *channel_three = new Channel("TF_nok"); Channel *channel_three = new Channel("TF_nok");
...@@ -60,31 +59,32 @@ int main(){ ...@@ -60,31 +59,32 @@ int main(){
amata->add_channel(channel_six); amata->add_channel(channel_six);
//Adding automaton //Adding automaton
//amata->add_automaton(amaton_one); amata->add_automaton(amaton_one);
cout << amata->get_info(); */ cout << amata->get_info(); */
//Removing... //Removing...
// cout << "Removing..." << endl; // cout << "Removing..." << endl;
//amaton_one->remove_state(state_four); /* amaton_one->remove_state(state_four);
//amaton_one->remove_transition(7); amaton_one->remove_state(state_one);
//amata->remove_automaton(amaton_one); amaton_one->remove_transition(7);
//amata->remove_channel(channel_six); amata->remove_automaton(amaton_one);
// cout << amata->get_info(); amata->remove_channel(channel_six);
cout << amata->get_info(); */
/* StateType stateTest{StateType::Normal}; /* 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; */
Nand* TS = new Nand({NodeType::Attack}, "TS"); /* Nand* TS = new Nand({NodeType::Attack}, "TS");
Sand* TF = new Sand({NodeType::Attack}, "TF"); Sand* TF = new Sand({NodeType::Attack}, "TF");
And* ST = new And({NodeType::Attack}, "ST"); And* ST = new And({NodeType::Attack}, "ST");
Leaf* b = new Leaf({NodeType::Attack}, "b"); Leaf* b = new Leaf({NodeType::Attack}, "b");
Leaf* f = new Leaf({NodeType::Attack}, "f"); Leaf* f = new Leaf({NodeType::Attack}, "f"); */
Or* GA = new Or({NodeType::Attack}, "GA"); Nor* GA = new Nor({NodeType::Attack}, "GA");
Leaf* h = new Leaf({NodeType::Attack}, "h"); Leaf* h = new Leaf({NodeType::Attack}, "h");
Leaf* e = new Leaf({NodeType::Attack}, "e"); Leaf* e = new Leaf({NodeType::Defence}, "e");
Leaf* p = new Leaf({NodeType::Defence}, "p"); // Leaf* p = new Leaf({NodeType::Defence}, "p");
/*Proof if you can add a child to parent which is not in the tree*/ /*Proof if you can add a child to parent which is not in the tree*/
......
...@@ -120,13 +120,9 @@ void Automaton::remove_transition(int transition_id){ ...@@ -120,13 +120,9 @@ void Automaton::remove_transition(int transition_id){
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; more_transitions_initial_state(cur_transition);
int several_states = this->states_.size();
if ((cur_transition->get_source_state() == this->initial_state_ || cur_transition->get_destination_state() == this->initial_state_) && several_states > 2)
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));
/*Deleting transition pointer in transitions vector of the source 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();
for(; it != tmp_transitions.end() && (*it)->get_id() != cur_transition->get_id(); it++){} for(; it != tmp_transitions.end() && (*it)->get_id() != cur_transition->get_id(); it++){}
...@@ -166,13 +162,45 @@ void Automaton::remove_state(State *cur_state){ ...@@ -166,13 +162,45 @@ void Automaton::remove_state(State *cur_state){
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));
for(int i = cur_state->get_transitions().size() - 1; i >= 0; i--){ for(int i = cur_state->get_transitions().size() - 1; i >= 0; i--){
if(i > 0 && cur_state->get_transitions()[i] == cur_state->get_transitions()[i-1]) /* if(i > 0 && cur_state->get_transitions()[i] == cur_state->get_transitions()[i-1])
i--; i--; */
remove_transition(cur_state->get_transitions()[i]); remove_transition(cur_state->get_transitions()[i]);
} }
/*Deleting transition pointers in transitions vector of the automaton which have as destination state this cur_state */
remove_transition_due_removing_state(cur_state);
auto it = this->states_.begin(); auto it = this->states_.begin();
for(; it != this->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->states_.erase(it); it = this->states_.erase(it);
this->state_ids_.erase(cur_state->get_id()); this->state_ids_.erase(cur_state->get_id());
} }
void Automaton::more_transitions_initial_state(Transition *cur_transition){
bool more_transitions_initial_state = 1;
int several_states = this->states_.size();
if ((cur_transition->get_source_state() == this->initial_state_ || cur_transition->get_destination_state() == this->initial_state_) && several_states > 2)
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));
}
void Automaton::remove_transition_due_removing_state(State *cur_state){
for(int i = this->transitions_.size() - 1; i >= 0; i--){
if(this->transitions_[i]->get_destination_state() == cur_state){
Transition *on_transition = this->transitions_[i];
more_transitions_initial_state(on_transition);
vector<Transition*> source_state_transitions = on_transition->get_source_state()->get_transitions();
auto it = source_state_transitions.begin();
for(; it != source_state_transitions.end() && (*it)->get_id() != on_transition->get_id(); it++){}
it = source_state_transitions.erase(it);
on_transition->get_source_state()->set_transitions(source_state_transitions);
it = this->transitions_.begin();
it = this->transitions_.erase(it+i);
this->transition_ids_.erase(on_transition->get_id());
delete on_transition;
}
}
}
...@@ -15,6 +15,9 @@ class Automaton ...@@ -15,6 +15,9 @@ class Automaton
vector<State*> states_; vector<State*> states_;
vector<Transition*> transitions_; vector<Transition*> transitions_;
void remove_transition_due_removing_state(State *cur_state);
void more_transitions_initial_state(Transition *cur_transition);
public: public:
Automaton(State *initial_state); Automaton(State *initial_state);
~Automaton(); ~Automaton();
......
...@@ -12,12 +12,14 @@ Automata* Translator::adt2amas_translate(ADtree *adtree){ ...@@ -12,12 +12,14 @@ Automata* Translator::adt2amas_translate(ADtree *adtree){
Automata* Translator::translation(TreeNode *node, Automata *amata){ Automata* Translator::translation(TreeNode *node, Automata *amata){
if (node->get_node_behaviour() == "Leaf") if (node->get_node_behaviour() == "Leaf")
leaf_to_automaton(node, amata); leaf_to_automaton(node, amata);
else if (node->get_node_behaviour() == "And"){ else if (node->get_node_behaviour() == "And")
and_to_automaton(node, amata); and_to_automaton(node, amata);
} else if (node->get_node_behaviour() == "Or")
else if (node->get_node_behaviour() == "Or"){
or_to_automaton(node, amata); or_to_automaton(node, amata);
} else if (node->get_node_behaviour() == "Nand")
nand_to_automaton(node, amata);
else if (node->get_node_behaviour() == "Nor")
nor_to_automaton(node, amata);
//amata->add_automaton(new_amaton); //amata->add_automaton(new_amaton);
return amata; return amata;
...@@ -59,6 +61,8 @@ void Translator::leaf_to_automaton(TreeNode *node, Automata *amata){ ...@@ -59,6 +61,8 @@ void Translator::leaf_to_automaton(TreeNode *node, Automata *amata){
void Translator::and_to_automaton(TreeNode *node, Automata *amata){ void Translator::and_to_automaton(TreeNode *node, Automata *amata){
State *initial_state = new State({StateType::Initial}); State *initial_state = new State({StateType::Initial});
Automaton *new_amaton = new Automaton(initial_state); Automaton *new_amaton = new Automaton(initial_state);
/* ok automaton path creation */
for(auto& i : node->get_children()){ for(auto& i : node->get_children()){
State *state_nth = new State({StateType::Normal}); State *state_nth = new State({StateType::Normal});
Channel *channel_ok_nth = new Channel(i->get_goal()+"_ok"); Channel *channel_ok_nth = new Channel(i->get_goal()+"_ok");
...@@ -69,9 +73,12 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){ ...@@ -69,9 +73,12 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){
new_amaton->add_transition(*rit, new_amaton->get_states().back(), synchronization_ok_nth); new_amaton->add_transition(*rit, new_amaton->get_states().back(), synchronization_ok_nth);
amata->add_channel(channel_ok_nth); amata->add_channel(channel_ok_nth);
} }
/* Last nodes creation */
State *state_nok_node = new State({StateType::Normal}); State *state_nok_node = new State({StateType::Normal});
State *state_ok_node = new State({StateType::Normal}); State *state_ok_node = new State({StateType::Normal});
/* Respective channels with synchronizations */
Channel *channel_ok_node = new Channel(node->get_goal()+"_ok"); Channel *channel_ok_node = new Channel(node->get_goal()+"_ok");
Channel *channel_nok_node = new Channel(node->get_goal()+"_nok"); Channel *channel_nok_node = new Channel(node->get_goal()+"_nok");
...@@ -80,13 +87,16 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){ ...@@ -80,13 +87,16 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){
Label *unique_label = new Label(node->get_goal()); Label *unique_label = new Label(node->get_goal());
/* Creation of label transition */
new_amaton->add_state(state_ok_node); new_amaton->add_state(state_ok_node);
new_amaton->add_transition(*(++(new_amaton->get_states().rbegin())), state_ok_node, unique_label); new_amaton->add_transition(*(++(new_amaton->get_states().rbegin())), state_ok_node, unique_label);
/* Self-loops */
new_amaton->add_state(state_nok_node); new_amaton->add_state(state_nok_node);
new_amaton->add_transition(state_ok_node, state_ok_node, synchronization_ok_node); new_amaton->add_transition(state_ok_node, state_ok_node, synchronization_ok_node);
new_amaton->add_transition(state_nok_node, state_nok_node, synchronization_nok_node); new_amaton->add_transition(state_nok_node, state_nok_node, synchronization_nok_node);
/* Creation of transitions */
for(auto& i : node->get_children()){ for(auto& i : node->get_children()){
Channel *channel_nok_nth = new Channel(i->get_goal()+"_nok"); Channel *channel_nok_nth = new Channel(i->get_goal()+"_nok");
Synchronization *synchronization_nok_nth = new Synchronization({SynchronizationType::Receive}, channel_nok_nth); Synchronization *synchronization_nok_nth = new Synchronization({SynchronizationType::Receive}, channel_nok_nth);
...@@ -99,7 +109,7 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){ ...@@ -99,7 +109,7 @@ void Translator::and_to_automaton(TreeNode *node, Automata *amata){
amata->add_automaton(new_amaton); amata->add_automaton(new_amaton);
} }
Automaton* Translator::or_to_automaton(TreeNode *node, Automata *amata){ void Translator::or_to_automaton(TreeNode *node, Automata *amata){
State *initial_state = new State({StateType::Initial}); State *initial_state = new State({StateType::Initial});
Automaton *new_amaton = new Automaton(initial_state); Automaton *new_amaton = new Automaton(initial_state);
...@@ -145,21 +155,112 @@ Automaton* Translator::or_to_automaton(TreeNode *node, Automata *amata){ ...@@ -145,21 +155,112 @@ Automaton* Translator::or_to_automaton(TreeNode *node, Automata *amata){
amata->add_automaton(new_amaton); amata->add_automaton(new_amaton);
} }
Automaton* Translator::sand_to_automaton(TreeNode *node, Automata *amata){ void Translator::nand_to_automaton(TreeNode *node, Automata *amata){
State *initial_state = new State({StateType::Initial});
Automaton *new_amaton = new Automaton(initial_state);
State *state_one = new State({StateType::Normal});
State *state_two = new State({StateType::Normal});
State *state_ok_node = new State({StateType::Normal});
State *state_nok_node = new State({StateType::Normal});
vector<TreeNode*> node_children = node->get_children();
auto it = node_children.begin();
Channel *channel_ok_one = new Channel(((*it)->get_goal())+"_ok");
Channel *channel_ok_two = new Channel((*(++it))->get_goal()+"_ok");
Channel *channel_nok_one = new Channel((*(--it))->get_goal()+"_nok");
Channel *channel_nok_two = new Channel((*(++it))->get_goal()+"_nok");
Channel *channel_ok_node = new Channel(node->get_goal()+"_ok");
Channel *channel_nok_node = new Channel(node->get_goal()+"_nok");
Synchronization *synchronization_ok_one = new Synchronization( {SynchronizationType::Receive}, channel_ok_one);
Synchronization *synchronization_ok_two = new Synchronization( {SynchronizationType::Receive}, channel_ok_two);
Synchronization *synchronization_nok_one = new Synchronization( {SynchronizationType::Receive}, channel_nok_one);
Synchronization *synchronization_nok_two = new Synchronization( {SynchronizationType::Receive}, channel_nok_two);
Synchronization *synchronization_ok_node = new Synchronization({SynchronizationType::Send}, channel_ok_node);
Synchronization *synchronization_nok_node = new Synchronization({SynchronizationType::Send}, channel_nok_node);
Label *label_node = new Label(node->get_goal());
new_amaton->add_state(state_one);
new_amaton->add_state(state_two);
new_amaton->add_state(state_ok_node);
new_amaton->add_state(state_nok_node);
new_amaton->add_transition(initial_state, state_one, synchronization_ok_one);
new_amaton->add_transition(state_one, state_two, synchronization_nok_two);
new_amaton->add_transition(state_two, state_ok_node, label_node);
new_amaton->add_transition(state_ok_node, state_ok_node, synchronization_ok_node);
new_amaton->add_transition(initial_state, state_nok_node, synchronization_nok_one);
new_amaton->add_transition(initial_state, state_nok_node, synchronization_ok_two);
new_amaton->add_transition(state_nok_node, state_nok_node, synchronization_nok_node);
amata->add_channel(channel_ok_one);
amata->add_channel(channel_ok_two);
amata->add_channel(channel_nok_one);
amata->add_channel(channel_nok_two);
amata->add_channel(channel_ok_node);
amata->add_channel(channel_nok_node);
amata->add_automaton(new_amaton);
} }
Automaton* Translator::sor_to_automaton(TreeNode *node, Automata *amata){
void Translator::nor_to_automaton(TreeNode *node, Automata *amata){
State *initial_state = new State({StateType::Initial});
Automaton *new_amaton = new Automaton(initial_state);
State *state_one = new State({StateType::Normal});
State *state_two = new State({StateType::Normal});
State *state_ok_node = new State({StateType::Normal});
State *state_nok_node = new State({StateType::Normal});
vector<TreeNode*> node_children = node->get_children();
auto it = node_children.begin();
Channel *channel_ok_one = new Channel(((*it)->get_goal())+"_ok");
Channel *channel_ok_two = new Channel((*(++it))->get_goal()+"_ok");
Channel *channel_nok_one = new Channel((*(--it))->get_goal()+"_nok");
Channel *channel_nok_two = new Channel((*(++it))->get_goal()+"_nok");
Channel *channel_ok_node = new Channel(node->get_goal()+"_ok");
Channel *channel_nok_node = new Channel(node->get_goal()+"_nok");
Synchronization *synchronization_ok_one = new Synchronization( {SynchronizationType::Receive}, channel_ok_one);
Synchronization *synchronization_ok_two = new Synchronization( {SynchronizationType::Receive}, channel_ok_two);
Synchronization *synchronization_nok_one = new Synchronization( {SynchronizationType::Receive}, channel_nok_one);
Synchronization *synchronization_nok_two = new Synchronization( {SynchronizationType::Receive}, channel_nok_two);
Synchronization *synchronization_ok_node = new Synchronization({SynchronizationType::Send}, channel_ok_node);
Synchronization *synchronization_nok_node = new Synchronization({SynchronizationType::Send}, channel_nok_node);
Label *label_node = new Label(node->get_goal());
new_amaton->add_state(state_one);
new_amaton->add_state(state_two);
new_amaton->add_state(state_ok_node);
new_amaton->add_state(state_nok_node);
new_amaton->add_transition(initial_state, state_two, synchronization_nok_one);
new_amaton->add_transition(state_two, state_nok_node, synchronization_ok_two);
new_amaton->add_transition(state_nok_node, state_nok_node, synchronization_nok_node);
new_amaton->add_transition(initial_state, state_one, synchronization_ok_one);
new_amaton->add_transition(initial_state, state_one, synchronization_nok_two);
new_amaton->add_transition(state_one, state_ok_node, label_node);
new_amaton->add_transition(state_ok_node, state_ok_node, synchronization_ok_node);
amata->add_channel(channel_ok_one);
amata->add_channel(channel_ok_two);
amata->add_channel(channel_nok_one);
amata->add_channel(channel_nok_two);
amata->add_channel(channel_ok_node);
amata->add_channel(channel_nok_node);
amata->add_automaton(new_amaton);
} }
Automaton* Translator::nand_to_automaton(TreeNode *node, Automata *amata){ void Translator::sand_to_automaton(TreeNode *node, Automata *amata){
} }
Automaton* Translator::nor_to_automaton(TreeNode *node, Automata *amata){ void Translator::sor_to_automaton(TreeNode *node, Automata *amata){
} }
Automaton* Translator::snand_to_automaton(TreeNode *node, Automata *amata){ void Translator::snand_to_automaton(TreeNode *node, Automata *amata){
} }
Automaton* Translator::snor_to_automaton(TreeNode *node, Automata *amata){ void Translator::snor_to_automaton(TreeNode *node, Automata *amata){
} }
\ No newline at end of file
...@@ -19,13 +19,13 @@ class Translator{ ...@@ -19,13 +19,13 @@ class Translator{
static Automata* translation(TreeNode *node, Automata *amata); static Automata* translation(TreeNode *node, Automata *amata);
static void leaf_to_automaton(TreeNode *node, Automata *amata); static void leaf_to_automaton(TreeNode *node, Automata *amata);
static void and_to_automaton(TreeNode *node, Automata *amata); static void and_to_automaton(TreeNode *node, Automata *amata);
static Automaton* or_to_automaton(TreeNode *node, Automata *amata); static void or_to_automaton(TreeNode *node, Automata *amata);
static Automaton* sand_to_automaton(TreeNode *node, Automata *amata); static void nand_to_automaton(TreeNode *node, Automata *amata);
static Automaton* sor_to_automaton(TreeNode *node, Automata *amata); static void nor_to_automaton(TreeNode *node, Automata *amata);
static Automaton* nand_to_automaton(TreeNode *node, Automata *amata); static void sand_to_automaton(TreeNode *node, Automata *amata);
static Automaton* nor_to_automaton(TreeNode *node, Automata *amata); static void sor_to_automaton(TreeNode *node, Automata *amata);
static Automaton* snand_to_automaton(TreeNode *node, Automata *amata); static void snand_to_automaton(TreeNode *node, Automata *amata);
static Automaton* snor_to_automaton(TreeNode *node, Automata *amata); static void snor_to_automaton(TreeNode *node, Automata *amata);
public: public:
Translator(); Translator();
......
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