#include "elementreduction.h" #include #include #include ElementReduction::ElementReduction(const QList &branchList):branchList(branchList) { } ElementReduction::~ElementReduction() { } void ElementReduction::calibration(const QString &id,QHash &idToBranch,QHash > &linkage) { QString to=idToBranch[id]->toID; if(linkage[to].length()==1) { idToBranch[id]->fromID.swap(idToBranch[id]->toID); } } void ElementReduction::doIt(const QString& rootID) { QHash > linkage;//后面经过merge以后这个表就不能用了!! //先记录元件之间的连接关系 QHash idToBranch; foreach(BranchStruc* v,this->branchList) { idToBranch[v->id]=v; linkage[v->fromID].push_back(v); linkage[v->toID].push_back(v); // std::cout<fromID.toStdString()<<" "<id.toStdString()<<" "<toID.toStdString()<branchList.at(2)->id); QDomElement element=root.createElement(rootID); this->calibration(rootID,idToBranch,linkage); std::cout<<"start "<buildTreeTo(element,root,linkage,idToBranch); // this->visited.remove(this->branchList.at(2)->id); // this->buildTreeFrom(element,root,linkage,idToBranch); this->merge(element,idToBranch); this->reduceSection(); QFile file("1.xml"); if(file.open(QFile::WriteOnly)) { // root.setContent(&file); QTextStream writer(&file); root.save(writer,4); file.close(); } //看看是不是每个都访问到了 foreach(BranchStruc* v,this->branchList) { if(!this->visited.contains(v->id)) { std::cout<<"unvisited "<id.toStdString()<& idToBranch) { // return; QString parentID=idToBranch[element.tagName()]->id; QStringList sep=parentID.split('-'); QString toID=sep.at(1)+sep.at(0); idToBranch[element.tagName()]->toID=toID; element.setAttribute("toID",toID); //顺便编号 //采用广度优先 bool merged=true; while(merged) { merged=false; QDomNodeList list=element.childNodes(); int length=list.length(); for(int i=0;ifromID=toID; node.setAttribute("fromID",toID); if(branch->isZeroBranch) { branch->dispose=true; while(node.childNodes().length()>0) { // std::cout<<"append "<merge((QDomElement &)node,idToBranch); } } void ElementReduction::buildTreeTo(QDomElement &element,QDomDocument &root,QHash >& linkage,QHash& idToBranch) { QString id=element.tagName(); if(this->visited.contains(id)) { // std::cout<<"visited "<id.toStdString()<visited[id]=0; BranchStruc * branch=idToBranch[id]; QString nextTo; nextTo=branch->toID; QVector nextVec; nextVec=linkage[nextTo]; // std::cout<visited.contains(n->id)) { continue; } // std::cout<id.toStdString()<id.toStdString()<id); // std::cout<<"create "<id.toStdString()<buildTreeTo(newEle,root,linkage,idToBranch); } } void ElementReduction::buildTreeFrom(QDomElement &element,QDomDocument &root,QHash >& linkage,QHash& idToBranch) { QString id=element.tagName(); if(this->visited.contains(id)) { std::cout<<"visited "<visited[id]=0; BranchStruc * branch=idToBranch[id]; QString nextFrom=branch->fromID; QVector nextFromVec; nextFromVec=linkage[nextFrom]; foreach(BranchStruc *n,nextFromVec) { if(this->visited.contains(n->id)) { continue; } std::cout<id.toStdString()<id.toStdString()<id); // std::cout<<"create "<id.toStdString()<buildTreeFrom(newEle,root,linkage,idToBranch); } } void ElementReduction::reduceSection() { const QList &branchList=this->branchList; QHash > linkage; foreach(BranchStruc* v,branchList) { linkage[v->fromID].push_back(v); linkage[v->toID].push_back(v); // std::cout<fromID.toStdString()<<" "<id.toStdString()<<" "<toID.toStdString()<dispose || branch->type=="TF") { continue; } std::cout<<"length "<length<<" id "<id.toStdString()<toID; //先找一下没有dispose的 QVector noDispose; // if(linkage[toID].length()==1)//自己就是末端了 // { // branch->dispose=true; // continue; // } std::cout<<"a "<dispose && childBranch->type=="TF") { ignore=true;//只要下面接了TF就不删掉 break; } if(!childBranch->dispose) { noDispose.push_back(childBranch); } } if(ignore) { continue; } std::cout<dispose=true; continue; } if(noDispose.length()==1) { BranchStruc *cb=noDispose.first(); std::cout<id.toStdString() <<"m with"<id.toStdString()<< ",add length"<length<length+=cb->length; branch->toID=cb->toID; cb->dispose=true; // break; reduced=true; } } } }