Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Lenovo on 3/15/2018.
- //
- #include "dfaGenerator.h"
- dfaGenerator::dfaGenerator(node* s)
- {
- vector<node*> temp;
- temp.push_back(s);
- tableRow* x = new tableRow(temp);
- states.push_back(x);
- // cout << "First node before Eclosure " << states[0]->getArrayList().size() << endl;
- states[0] = Eclosure(states[0]);
- //cout << states[0]->getArrayList()[0]->get
- // cout <<"First node After Eclosure " << states[0]->getArrayList().size()<<" name: " << states[0]->getArrayList()[0]->getNodeCount()<<" "
- // << " name: "<< states[0]->getArrayList()[1]->getNodeCount()<<endl;
- if(s->isFinal())
- {
- states[0]->setFinal(true, s->getFinalType());
- }
- tableGenerator();
- // cout<<"here"<<endl;
- // cout << "Mimization starts here ******************************" << endl;
- cout << "Size before minimization " << states.size()<<endl;
- //Minimize();
- //cout << "Size after minimization " << states.size()<<endl;
- }
- tableRow* dfaGenerator::Eclosure(tableRow *row)
- {
- stack<node*> Stack;
- vector<node*> temp;
- tableRow *newRow = new tableRow(temp);
- for(int i = 0 ; i < row->getArrayList().size() ; i++)
- {
- Stack.push(row->getArrayList()[i]);
- newRow->addToArrayList(row->getArrayList()[i]);
- if(row->getArrayList()[i]->isFinal())
- {
- row->setFinal(true, row->getArrayList()[i]->getFinalType());
- }
- }
- while(!Stack.empty())
- {
- node* n = Stack.top();
- Stack.pop();
- // cout<<"Node going out of stack > going out edges count: "<<n->getEdgeOutList().size()<<" Name: " << n->getNodeCount()<<endl;
- vector<Edge*> edges = n->getEdgeOutList();
- for(int i = 0 ; i< edges.size() ; i++)
- {
- Edge* e = edges[i];
- // cout<< "Edge value " <<e->getValue() << "Node Name: "<<e->getToNode()->getNodeCount()<<endl;
- // cout <<"Node of Node going out edges count: "<<e->getToNode()->getEdgeOutList().size()<<endl;
- if(e->getValue()== "\\L" &&
- !(std::find(newRow->getArrayList().begin(), newRow->getArrayList().end(), e->getToNode()) == newRow->getArrayList().end()))
- {
- newRow->addToArrayList(e->getToNode());
- Stack.push(e->getToNode());
- // cout<<"We pushed " << e->getToNode()->getNodeCount()<< " into the stack, with out list = "<<e->getToNode()->getEdgeOutList().size() << "FINAL? "<<e->getToNode()->isFinal()<<endl;
- if(e->getToNode()->isFinal())
- {
- newRow->setFinal(true, e->getToNode()->getFinalType());
- }
- }
- }
- }
- return newRow;
- }
- int dfaGenerator:: countSame(vector<node*> one, vector<node*> two)
- {
- int counter = 0 ;
- for (int i = 0 ; i < one.size() ; i ++)
- {
- if(std::find(two.begin(), two.end(), one[i]) != two.end())
- {
- counter++;
- }
- else
- {
- counter = -1 ;
- break;
- }
- }
- return counter;
- }
- void dfaGenerator::tableGenerator()
- {
- for(int i = 0 ; i < states.size(); i++)
- {
- cout<<"Value of i: "<<i<<" size of states: "<<states.size()<<endl;
- tableRow *row = states[i];
- cout<<"sizee of arr " << row->getArrayList().size()<<" "<<row->getArrayList()[0]->getNodeCount()<<endl;
- vector<node*> state = row->getArrayList();
- for (int j = 0 ; j < state.size() ; j++ )
- {
- //cout<<"here"<<endl;
- // cout << " ***************" << endl;
- node* n = state[j];
- cout << "value of j = " << j << " name of node: "<< n->getNodeCount()<<endl;
- vector<Edge*> edgeList = n->getEdgeOutList();
- cout<<"edge list size "<< edgeList.size()<<endl;
- for(int k = 0 ; k < edgeList.size() ; k++ )
- {
- // cout << "value of k = " << k << endl;
- Edge* e = edgeList[k];
- string val = e->getValue();
- // cout<<"value "<< val<<endl;
- // node* target = e->getToNode();
- map<string,tableRow*> temp = row->getMap();
- if(val != "\\L")
- {
- if(temp[val] == NULL)
- {
- cout<<" here is my target node " << e->getToNode()->getNodeCount()<< e->getValue()<<endl;
- vector<node*> t;
- t.push_back(e->getToNode());
- tableRow* tempr = new tableRow(t);
- row->addToHash(val,tempr);
- }
- else
- {
- row->editMap(val,e->getToNode());
- cout << row ->getMap()[val]<<endl;
- }
- }
- }
- }
- int coun = 0 ;
- int x = row->getMap().size();
- for(map<string,tableRow*>::iterator itr = row->getMap().begin(); itr != row->getMap().end()
- && coun < x ; ++itr)
- {
- //cout<<x<<endl;
- // cout << "here"<<(coun)<<endl;
- string key = itr->first;
- coun++;
- cout<< key;
- cout<< "before " <<itr->second->getArrayList().size()<<endl;
- tableRow *r = Eclosure(itr->second);
- cout<< "after "<<r->getArrayList().size()<<endl;
- cout<< "key >>>>>>>>>>>>>>>>>>>>>>>> "<<key<< "Size after closure of itr->second: "<< r->getArrayList().size()<< endl;
- row->addToHash(key, r );
- cout << "more after "<<row->getMap()[itr->first]->getArrayList().size()<<endl;
- bool stateFound = false;
- for(int k = 0 ; k <states.size(); k++)
- {
- if(countSame(states[k]->getArrayList(), r->getArrayList()) == r->getArrayList().size())
- {
- stateFound = true;
- row->addToHash(key,states[k]);
- break;
- }
- }
- if(!stateFound)
- states.push_back(row->getMap()[key]);
- cout<<row->getMap()["l"]->getArrayList()[0]->getNodeCount()<<endl;
- cout<<"Size of states after adding : " <<states.size()<<endl;
- }
- }
- }
- void dfaGenerator::Minimize()
- {
- for(int i = 0 ; i < states.size()-1; i++)
- {
- //cout<<"aa fen"<<endl;
- bool found =false;
- tableRow* Row1 = states[i];
- cout << "value of i= " << i<<endl;
- for(int j =i+1 ; j < states.size() ; j++)
- {
- cout << "value of j= " << j <<endl;
- tableRow* Row2 = states[j];
- int counter1 , counter2;
- if(Row1->getMap().size()==Row2->getMap().size() && Row1->isFinal()==Row2->isFinal() )
- {
- cout << "Maps of 2 states are equal in length and both are Final"<<endl;
- counter1 = 0;
- for(map<string,tableRow*>::iterator it = Row1->getMap().begin(); counter1 < Row1->getMap().size() && it != Row1->getMap().end(); ++it)
- {
- found = false;
- cout << "Iterate over map 1 with key = "<< it->first <<endl;
- counter2 =0;
- for (map<string,tableRow*>::iterator it2 = Row2->getMap().begin(); counter2 < Row2->getMap().size() &&it2 != Row2->getMap().end(); ++it2)
- {
- //cout << "here" <<endl;
- cout << "Iterate over map 2 with key = "<< it2->first<<endl;
- if (it->first == it2->first && it->second == it2->second)
- {
- //cout << found << endl;
- cout << "found " << it->first << " in map1 equal "<< it2->first <<" in map 2 " << endl;
- found = true;
- }
- counter2++;
- }
- if(found == false)
- {
- cout << "Didn't Find Equality & Breaking" << endl;
- break;
- }
- counter1++;
- }
- if(found==true) // 2 identical states
- {
- cout << "FOUND Total Equality"<<endl;
- for(int k = 0 ; k < states.size()-1; k++)
- {
- tableRow* ro = states[k];
- int counter3 = 0;
- for (map<string,tableRow*>::iterator iter = ro->getMap().begin(); counter3 < ro->getMap().size()&&iter != ro->getMap().end(); ++iter)
- {
- if ((iter->second)->getArrayList() == Row2->getArrayList())
- {
- ro->addToHash(iter->first, Row1);
- }
- counter3++;
- }
- }
- states.erase(states.begin() + j);
- cout << ">>>>>>>>> Removing state: " << j << endl;
- cout << states.size()<< endl;
- j--;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement