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-30 21:34:31 +08:00
|
|
|
|
#include "element/commontype.h"
|
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-22 11:00:39 +08:00
|
|
|
|
void ElementReduction::calibration(const QString &id,QHash<QString,BranchStruc *> &idToBranch,QHash<QString,QVector<BranchStruc *> > &linkage)
|
|
|
|
|
|
{
|
|
|
|
|
|
QString to=idToBranch[id]->toID;
|
|
|
|
|
|
if(linkage[to].length()==1)
|
|
|
|
|
|
{
|
|
|
|
|
|
idToBranch[id]->fromID.swap(idToBranch[id]->toID);
|
|
|
|
|
|
}
|
2015-03-09 10:31:33 +08:00
|
|
|
|
|
|
|
|
|
|
LineStru *line=static_cast<LineStru *>(idToBranch[id]);
|
|
|
|
|
|
if(0==static_cast<int>(line->length))
|
|
|
|
|
|
{
|
|
|
|
|
|
line->length=10;
|
|
|
|
|
|
line->line->length=10;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-22 11:00:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-22 11:09:20 +08:00
|
|
|
|
void ElementReduction::doIt(const QString& rootID)
|
2015-01-20 21:24:33 +08:00
|
|
|
|
{
|
2015-01-22 21:19:43 +08:00
|
|
|
|
QHash<QString,QVector<BranchStruc *> > linkage;//后面经过merge以后这个表就不能用了!!
|
2015-01-20 22:46:26 +08:00
|
|
|
|
//先记录元件之间的连接关系
|
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-22 15:43:28 +08:00
|
|
|
|
// std::cout<<v->fromID.toStdString()<<" "<<v->id.toStdString()<<" "<<v->toID.toStdString()<<std::endl;
|
2015-01-20 22:46:26 +08:00
|
|
|
|
}
|
2015-01-21 14:03:07 +08:00
|
|
|
|
QDomDocument root;
|
2015-01-22 11:00:39 +08:00
|
|
|
|
|
|
|
|
|
|
// QDomElement element=root.createElement(this->branchList.at(2)->id);
|
2015-01-22 11:09:20 +08:00
|
|
|
|
QDomElement element=root.createElement(rootID);
|
|
|
|
|
|
this->calibration(rootID,idToBranch,linkage);
|
|
|
|
|
|
std::cout<<"start "<<rootID.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
|
root.appendChild(element);
|
2015-01-21 22:05:19 +08:00
|
|
|
|
this->buildTreeTo(element,root,linkage,idToBranch);
|
2015-01-22 11:00:39 +08:00
|
|
|
|
// this->visited.remove(this->branchList.at(2)->id);
|
2015-01-22 15:43:28 +08:00
|
|
|
|
// this->buildTreeFrom(element,root,linkage,idToBranch);
|
2015-02-01 13:52:43 +08:00
|
|
|
|
|
2015-01-21 22:05:19 +08:00
|
|
|
|
this->merge(element,idToBranch);
|
2015-01-22 21:19:43 +08:00
|
|
|
|
this->reduceSection();
|
2015-02-01 13:52:43 +08:00
|
|
|
|
|
2015-01-30 21:34:31 +08:00
|
|
|
|
// QFile file("1.xml");
|
|
|
|
|
|
// if(file.open(QFile::WriteOnly))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// QTextStream writer(&file);
|
|
|
|
|
|
|
|
|
|
|
|
// root.save(writer,4);
|
|
|
|
|
|
// file.close();
|
|
|
|
|
|
// }
|
2015-01-21 14:03:07 +08:00
|
|
|
|
//看看是不是每个都访问到了
|
|
|
|
|
|
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::merge(QDomElement &element,QHash<QString,BranchStruc *>& idToBranch)
|
2015-01-21 14:03:07 +08:00
|
|
|
|
{
|
2015-01-22 14:12:49 +08:00
|
|
|
|
// return;
|
2015-01-21 21:52:18 +08:00
|
|
|
|
QString parentID=idToBranch[element.tagName()]->id;
|
|
|
|
|
|
QStringList sep=parentID.split('-');
|
2015-02-01 13:52:43 +08:00
|
|
|
|
QString toID;
|
|
|
|
|
|
if(sep.length()>1)
|
|
|
|
|
|
{
|
|
|
|
|
|
toID=sep.at(1)+sep.at(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
toID=sep.at(0)+"_"+sep.at(0);
|
|
|
|
|
|
}
|
2015-01-21 21:52:18 +08:00
|
|
|
|
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);
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2015-01-22 15:43:28 +08:00
|
|
|
|
// std::cout<<"append "<<node.firstChild().nodeName().toStdString()<<" to "<<element.tagName().toStdString()<<std::endl;
|
2015-01-21 21:52:18 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-21 22:05:19 +08:00
|
|
|
|
void ElementReduction::buildTreeTo(QDomElement &element,QDomDocument &root,QHash<QString,QVector<BranchStruc *> >& linkage,QHash<QString,BranchStruc *>& idToBranch)
|
2015-01-21 14:03:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
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];
|
2015-01-22 11:09:20 +08:00
|
|
|
|
// std::cout<<nextVec.length()<<" of "<<element.tagName().toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
|
foreach(BranchStruc *n,nextVec)
|
|
|
|
|
|
{
|
2015-01-22 11:00:39 +08:00
|
|
|
|
|
2015-01-21 14:03:07 +08:00
|
|
|
|
if(this->visited.contains(n->id))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2015-01-22 11:00:39 +08:00
|
|
|
|
// std::cout<<element.tagName().toStdString()<<"to can connect to "<<n->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
|
// std::cout<<"add "<<n->id.toStdString()<<std::endl;
|
|
|
|
|
|
QDomElement newEle=root.createElement(n->id);
|
2015-01-21 22:05:19 +08:00
|
|
|
|
// std::cout<<"create "<<n->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
|
element.appendChild(newEle);
|
2015-01-21 22:05:19 +08:00
|
|
|
|
this->buildTreeTo(newEle,root,linkage,idToBranch);
|
2015-01-21 14:03:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-21 22:05:19 +08:00
|
|
|
|
void ElementReduction::buildTreeFrom(QDomElement &element,QDomDocument &root,QHash<QString,QVector<BranchStruc *> >& linkage,QHash<QString,BranchStruc *>& idToBranch)
|
2015-01-21 14:03:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
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];
|
2015-01-22 11:00:39 +08:00
|
|
|
|
|
2015-01-21 14:03:07 +08:00
|
|
|
|
foreach(BranchStruc *n,nextFromVec)
|
|
|
|
|
|
{
|
2015-01-22 11:00:39 +08:00
|
|
|
|
|
2015-01-21 14:03:07 +08:00
|
|
|
|
if(this->visited.contains(n->id))
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2015-01-22 11:00:39 +08:00
|
|
|
|
std::cout<<element.tagName().toStdString()<<"from can connect to "<<n->id.toStdString()<<std::endl;
|
2015-01-21 14:03:07 +08:00
|
|
|
|
// 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);
|
2015-01-21 22:05:19 +08:00
|
|
|
|
this->buildTreeFrom(newEle,root,linkage,idToBranch);
|
2015-01-21 14:03:07 +08:00
|
|
|
|
}
|
2015-01-20 22:46:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-20 21:24:33 +08:00
|
|
|
|
|
2015-01-22 21:19:43 +08:00
|
|
|
|
void ElementReduction::reduceSection()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
const QList<BranchStruc *> &branchList=this->branchList;
|
|
|
|
|
|
QHash<QString,QVector<BranchStruc *> > linkage;
|
|
|
|
|
|
foreach(BranchStruc* v,branchList)
|
|
|
|
|
|
{
|
|
|
|
|
|
linkage[v->fromID].push_back(v);
|
|
|
|
|
|
linkage[v->toID].push_back(v);
|
|
|
|
|
|
// std::cout<<v->fromID.toStdString()<<" "<<v->id.toStdString()<<" "<<v->toID.toStdString()<<std::endl;
|
|
|
|
|
|
}
|
|
|
|
|
|
bool reduced=true;
|
|
|
|
|
|
while(reduced)
|
|
|
|
|
|
{
|
|
|
|
|
|
reduced=false;
|
|
|
|
|
|
foreach(BranchStruc *branch,branchList)
|
|
|
|
|
|
{
|
2015-01-30 21:34:31 +08:00
|
|
|
|
if(branch->dispose || branch->type=="TF"||branch->type=="DG")
|
2015-01-22 21:19:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2015-01-30 21:34:31 +08:00
|
|
|
|
// std::cout<<"length "<<branch->length<<" id "<<branch->id.toStdString()<<std::endl;
|
2015-01-22 21:19:43 +08:00
|
|
|
|
QString toID=branch->toID;
|
|
|
|
|
|
//先找一下没有dispose的
|
|
|
|
|
|
QVector<BranchStruc *> noDispose;
|
|
|
|
|
|
// if(linkage[toID].length()==1)//自己就是末端了
|
|
|
|
|
|
// {
|
|
|
|
|
|
// branch->dispose=true;
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
// }
|
2015-01-30 21:34:31 +08:00
|
|
|
|
// std::cout<<"a "<<linkage[toID].length()<<" "<<toID.toStdString()<<std::endl;
|
2015-01-22 21:19:43 +08:00
|
|
|
|
bool ignore=false;
|
|
|
|
|
|
foreach(BranchStruc *childBranch,linkage[toID])
|
|
|
|
|
|
{
|
|
|
|
|
|
if(childBranch==branch)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2015-01-30 21:34:31 +08:00
|
|
|
|
if(!childBranch->dispose && (childBranch->type=="TF"||childBranch->type=="DG" ) )
|
2015-01-22 21:19:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
ignore=true;//只要下面接了TF就不删掉
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(!childBranch->dispose)
|
|
|
|
|
|
{
|
|
|
|
|
|
noDispose.push_back(childBranch);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if(ignore)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2015-01-30 21:34:31 +08:00
|
|
|
|
// std::cout<<noDispose.length()<<std::endl;
|
2015-01-22 21:19:43 +08:00
|
|
|
|
if(noDispose.length()==0)//自己就是末端了
|
|
|
|
|
|
{
|
|
|
|
|
|
branch->dispose=true;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(noDispose.length()==1)
|
|
|
|
|
|
{
|
|
|
|
|
|
BranchStruc *cb=noDispose.first();
|
2015-01-30 21:34:31 +08:00
|
|
|
|
// std::cout<<branch->id.toStdString() <<"m with"<<cb->id.toStdString()<< ",add length"<<cb->length<<std::endl;
|
2015-01-22 21:19:43 +08:00
|
|
|
|
branch->length+=cb->length;
|
2015-01-30 21:34:31 +08:00
|
|
|
|
LineStru *lineBranch=static_cast<LineStru *>(branch);
|
|
|
|
|
|
if(lineBranch)
|
|
|
|
|
|
{
|
|
|
|
|
|
lineBranch->line->length=branch->length;
|
|
|
|
|
|
}else
|
|
|
|
|
|
{
|
|
|
|
|
|
std::cout<<"can not conver from BranchStruc to LineStru"<<std::endl;
|
|
|
|
|
|
}
|
2015-01-22 21:19:43 +08:00
|
|
|
|
branch->toID=cb->toID;
|
|
|
|
|
|
cb->dispose=true;
|
|
|
|
|
|
// break;
|
|
|
|
|
|
reduced=true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|