2015-01-20 20:59:47 +08:00
|
|
|
#include "elementreduction.h"
|
2015-01-20 22:46:26 +08:00
|
|
|
#include <iostream>
|
2015-01-21 14:03:07 +08:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QTextStream>
|
2015-01-20 22:46:26 +08:00
|
|
|
ElementReduction::ElementReduction(const QList<BranchStruc *> &branchList):branchList(branchList)
|
2015-01-20 20:59:47 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ElementReduction::~ElementReduction()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 21:24:33 +08:00
|
|
|
void ElementReduction::doIt()
|
|
|
|
|
{
|
2015-01-20 22:46:26 +08:00
|
|
|
QHash<QString,QVector<BranchStruc *> > linkage;
|
|
|
|
|
//先记录元件之间的连接关系
|
2015-01-21 14:03:07 +08:00
|
|
|
QHash<QString,BranchStruc *> idToBranch;
|
2015-01-20 22:46:26 +08:00
|
|
|
foreach(BranchStruc* v,this->branchList)
|
|
|
|
|
{
|
2015-01-21 14:03:07 +08:00
|
|
|
idToBranch[v->id]=v;
|
2015-01-20 22:46:26 +08:00
|
|
|
linkage[v->fromID].push_back(v);
|
|
|
|
|
linkage[v->toID].push_back(v);
|
|
|
|
|
}
|
2015-01-21 14:03:07 +08:00
|
|
|
QDomDocument root;
|
|
|
|
|
QDomElement element=root.createElement(this->branchList.at(2)->id);
|
|
|
|
|
root.appendChild(element);
|
|
|
|
|
this->buildTree(element,root,linkage,idToBranch);
|
|
|
|
|
this->visited.remove(this->branchList.at(2)->id);
|
|
|
|
|
this->buildTree2(element,root,linkage,idToBranch);
|
2015-01-21 21:52:18 +08:00
|
|
|
this->merge(element,idToBranch);
|
2015-01-21 14:03:07 +08:00
|
|
|
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 "<<v->id.toStdString()<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//合并
|
|
|
|
|
|
2015-01-21 21:52:18 +08:00
|
|
|
//从新给连接关系编号
|
2015-01-21 14:03:07 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 21:52:18 +08:00
|
|
|
|
|
|
|
|
//void ElementReduction::re_establishConnection(QDomElement &element,QHash<QString,BranchStruc *>& idToBranch)
|
|
|
|
|
//{
|
|
|
|
|
// QDomNodeList list=element.childNodes();
|
|
|
|
|
// for(int i=0;i<list.length();i++)
|
|
|
|
|
// {
|
|
|
|
|
// QDomNode node=list.at(i);
|
|
|
|
|
// QString id;
|
|
|
|
|
// id=node.nodeName();
|
|
|
|
|
// BranchStruc *branch=idToBranch[id];
|
|
|
|
|
// if(branch->isZeroBranch)
|
|
|
|
|
// {
|
|
|
|
|
// branch->dispose=true;
|
|
|
|
|
// QDomNodeList child=node.childNodes();
|
|
|
|
|
// for(int j=0;j<child.length();j++)
|
|
|
|
|
// {
|
|
|
|
|
// QDomNode c=child.at(j);
|
|
|
|
|
// element.appendChild(node.removeChild(c));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// this->merge((QDomElement &)node,idToBranch);
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ElementReduction::merge(QDomElement &element,QHash<QString,BranchStruc *>& idToBranch)
|
2015-01-21 14:03:07 +08:00
|
|
|
{
|
2015-01-21 21:52:18 +08:00
|
|
|
QString parentID=idToBranch[element.tagName()]->id;
|
|
|
|
|
QStringList sep=parentID.split('-');
|
|
|
|
|
// QString fromID=sep.at(0)+sep.at(1);
|
|
|
|
|
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;i<length;i++)
|
|
|
|
|
{
|
|
|
|
|
QDomNode nod=list.at(i);
|
|
|
|
|
if(nod.isNull())
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"isNULL"<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
QDomElement &node=(QDomElement &)nod;
|
|
|
|
|
QString id;
|
|
|
|
|
id=node.nodeName();
|
|
|
|
|
BranchStruc *branch=idToBranch[id];
|
|
|
|
|
branch->fromID=toID;
|
|
|
|
|
node.setAttribute("fromID",toID);
|
|
|
|
|
if(branch->isZeroBranch)
|
|
|
|
|
{
|
|
|
|
|
branch->dispose=true;
|
|
|
|
|
while(node.childNodes().length()>0)
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"append "<<node.firstChild().nodeName().toStdString()<<" to "<<element.tagName().toStdString()<<std::endl;
|
|
|
|
|
element.appendChild(node.removeChild(node.firstChild()));
|
|
|
|
|
}
|
|
|
|
|
element.removeChild(node);
|
|
|
|
|
merged=true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
QDomNodeList list=element.childNodes();
|
|
|
|
|
for(int i=0;i<list.length();i++)
|
|
|
|
|
{
|
|
|
|
|
QDomNode nod=list.at(i);
|
|
|
|
|
QDomElement &node=(QDomElement &)nod;
|
|
|
|
|
this->merge((QDomElement &)node,idToBranch);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-21 14:03:07 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ElementReduction::buildTree(QDomElement &element,QDomDocument &root,QHash<QString,QVector<BranchStruc *> >& linkage,QHash<QString,BranchStruc *>& idToBranch)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QString id=element.tagName();
|
|
|
|
|
if(this->visited.contains(id))
|
|
|
|
|
{
|
|
|
|
|
// std::cout<<"visited "<<branch->id.toStdString()<<std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->visited[id]=0;
|
|
|
|
|
BranchStruc * branch=idToBranch[id];
|
|
|
|
|
QString nextTo;
|
|
|
|
|
nextTo=branch->toID;
|
|
|
|
|
QVector<BranchStruc *> nextVec;
|
|
|
|
|
nextVec=linkage[nextTo];
|
|
|
|
|
foreach(BranchStruc *n,nextVec)
|
|
|
|
|
{
|
|
|
|
|
if(this->visited.contains(n->id))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// std::cout<<"add "<<n->id.toStdString()<<std::endl;
|
|
|
|
|
QDomElement newEle=root.createElement(n->id);
|
2015-01-21 21:52:18 +08:00
|
|
|
std::cout<<"create "<<n->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
element.appendChild(newEle);
|
|
|
|
|
this->buildTree(newEle,root,linkage,idToBranch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ElementReduction::buildTree2(QDomElement &element,QDomDocument &root,QHash<QString,QVector<BranchStruc *> >& linkage,QHash<QString,BranchStruc *>& idToBranch)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
QString id=element.tagName();
|
|
|
|
|
if(this->visited.contains(id))
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"visited "<<id.toStdString()<<std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->visited[id]=0;
|
|
|
|
|
BranchStruc * branch=idToBranch[id];
|
|
|
|
|
QString nextFrom=branch->fromID;
|
|
|
|
|
QVector<BranchStruc *> nextFromVec;
|
|
|
|
|
nextFromVec=linkage[nextFrom];
|
|
|
|
|
foreach(BranchStruc *n,nextFromVec)
|
|
|
|
|
{
|
|
|
|
|
if(this->visited.contains(n->id))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// std::cout<<"add "<<n->id.toStdString()<<std::endl;
|
|
|
|
|
QDomElement newEle=root.createElement(n->id);
|
2015-01-21 21:52:18 +08:00
|
|
|
// std::cout<<"create "<<n->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
element.appendChild(newEle);
|
|
|
|
|
this->buildTree2(newEle,root,linkage,idToBranch);
|
|
|
|
|
}
|
2015-01-20 22:46:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ElementReduction::visit(BranchStruc* branch,QHash<QString,QVector<BranchStruc *> >& linkage)
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"visit "<<branch->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
// if(this->visited.contains(branch->id))
|
|
|
|
|
// {
|
|
|
|
|
// std::cout<<"visited "<<branch->id.toStdString()<<std::endl;
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
2015-01-20 22:46:26 +08:00
|
|
|
this->visited[branch->id]=0;
|
|
|
|
|
QString next;
|
|
|
|
|
next=branch->toID;
|
|
|
|
|
QVector<BranchStruc* > nextVec;
|
|
|
|
|
nextVec=linkage[next];
|
2015-01-21 14:03:07 +08:00
|
|
|
// std::cout<<"next length"<<nextVec.length()<<std::endl;
|
2015-01-20 22:46:26 +08:00
|
|
|
foreach(BranchStruc* v,nextVec)
|
2015-01-20 21:24:33 +08:00
|
|
|
{
|
2015-01-20 22:46:26 +08:00
|
|
|
if(v->dispose)
|
2015-01-21 14:03:07 +08:00
|
|
|
{
|
|
|
|
|
// std::cout<<"disposed "<<v->id.toStdString()<<std::endl;
|
2015-01-20 22:46:26 +08:00
|
|
|
continue;//丢弃的元件
|
2015-01-21 14:03:07 +08:00
|
|
|
}
|
2015-01-20 22:46:26 +08:00
|
|
|
if(v->isZeroBranch)//是0阻抗
|
|
|
|
|
{
|
2015-01-21 14:03:07 +08:00
|
|
|
std::cout<<"merge "<<branch->id.toStdString()<<" and "<<v->id.toStdString()<<std::endl;
|
2015-01-20 22:46:26 +08:00
|
|
|
//进行合并
|
|
|
|
|
branch->toID=v->toID;
|
|
|
|
|
v->dispose=true;
|
|
|
|
|
foreach(BranchStruc* change,linkage[branch->toID])
|
|
|
|
|
{
|
|
|
|
|
if(change->id==branch->id)
|
2015-01-21 14:03:07 +08:00
|
|
|
{
|
2015-01-20 22:46:26 +08:00
|
|
|
continue;
|
2015-01-21 14:03:07 +08:00
|
|
|
}
|
2015-01-20 22:46:26 +08:00
|
|
|
change->fromID=branch->toID;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-21 14:03:07 +08:00
|
|
|
// std::cout<<"start to visit next"<<std::endl;
|
|
|
|
|
if(v->id!=branch->id)
|
|
|
|
|
{
|
|
|
|
|
this->visit(v,linkage);//深度优先
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ElementReduction::visit2(BranchStruc* branch,QHash<QString,QVector<BranchStruc *> >& linkage)
|
|
|
|
|
{
|
|
|
|
|
// std::cout<<"visit "<<branch->id.toStdString()<<std::endl;
|
|
|
|
|
if(this->visited.contains(branch->id))
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"visited "<<branch->id.toStdString()<<std::endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this->visited[branch->id]=0;
|
|
|
|
|
QString next;
|
|
|
|
|
next=branch->fromID;
|
|
|
|
|
QVector<BranchStruc* > nextVec;
|
|
|
|
|
nextVec=linkage[next];
|
|
|
|
|
// std::cout<<"next length"<<nextVec.length()<<std::endl;
|
|
|
|
|
foreach(BranchStruc* v,nextVec)
|
|
|
|
|
{
|
|
|
|
|
if(v->dispose)
|
|
|
|
|
{
|
|
|
|
|
// std::cout<<"disposed "<<v->id.toStdString()<<std::endl;
|
|
|
|
|
continue;//丢弃的元件
|
|
|
|
|
}
|
|
|
|
|
if(v->isZeroBranch)//是0阻抗
|
|
|
|
|
{
|
|
|
|
|
std::cout<<"merge "<<branch->id.toStdString()<<" and "<<v->id.toStdString()<<std::endl;
|
|
|
|
|
//进行合并
|
|
|
|
|
branch->fromID=v->fromID;
|
|
|
|
|
v->dispose=true;
|
|
|
|
|
foreach(BranchStruc* change,linkage[branch->fromID])
|
|
|
|
|
{
|
|
|
|
|
if(change->id==branch->id)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
change->toID=branch->fromID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// std::cout<<"start to visit next"<<std::endl;
|
|
|
|
|
this->visit(v,linkage);//深度优先
|
2015-01-20 21:24:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|