使用Boost图形读取GraphML文件时使用vertex_name [英] Using vertex_name when reading a GraphML file with Boost Graph
问题描述
我试图加载一个简单的GraphML文件,使得每个顶点都有一个存储在GraphML中的顶点名称。我可以改变GraphML,重要的是我可以在代码之后访问vertex_name。
以下是我可以提取的最简单的示例,但仍显示问题:
#include< iostream>
#include< string>
#include< fstream>
#include< boost / graph / adjacency_list.hpp>
#include< boost / graph / graphml.hpp>
int main()
{
using namespace boost;
typedef adjacency_list< vecS,vecS,directedS,property< vertex_name_t,std :: string> > BoostGraphType;
typedef dynamic_properties BoostDynamicProperties;
std :: string fn =simple.graphml;
std :: ifstream是(fn.c_str());
if(!is.is_open())
{
std :: cout<< 加载文件<< fn<< 失败了。 <<的std :: ENDL;
抛出无法加载文件。
}
BoostGraphType g;
BoostDynamicProperties dp;
const std :: string vn =vertex_name;
dp.property(vn,get(vertex_name,g));
read_graphml(is,g,dp); (auto vp = vertices(g); vp.first!= vp.second; ++ vp.first)
{
std :: cout<< index<< get(vertex_index,g,* vp.first)<< ';
std :: cout<< name<< get(vertex_name,g,* vp.first)<< '
<<的std :: ENDL;
}
返回0;
}
我使用以下GraphML测试文件:
<?xml version =1.0encoding =UTF-8?>
< graphml xmlns =http://graphml.graphdrawing.org/xmlns
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">
< graph id =Gedgedefault =directed>
< node id =A> < data key =d0> A< / data> < /节点>
< node id =B> < data key =d0> B< / data> < /节点>
< edge id =0source =Atarget =B/>
< /图表>
< / graphml>
read_graphml通过有用的消息(e.what())引发异常:
解析错误:无法识别的类型
'p>看来问题是关系到vertex_name关联(这是我从评论到了一个我的上一个问题)。
如果我删除
< data key =d0> A< / data>
然而,我需要能够通过vertex_name识别顶点。
我该如何解决这个问题,以便正确解析graphml并且不会抛出?我在做什么错了?
代码完美运行时运行它。
> wilbert.exe
索引'0'名称'A'
索引'1'名'B'
这是在Windows 7上使用boost v1.52
I am trying to load a simple GraphML file such that each vertex has a vertex name as stored in the GraphML. I can change the GraphML, the important thing is that I have access to the vertex_name from code afterwards.
Here's the most minimal example that I could extract that still shows the problem:
#include <iostream>
#include <string>
#include <fstream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
int main()
{
using namespace boost;
typedef adjacency_list<vecS, vecS, directedS,property<vertex_name_t,std::string> > BoostGraphType;
typedef dynamic_properties BoostDynamicProperties;
std::string fn = "simple.graphml";
std::ifstream is(fn.c_str());
if (!is.is_open())
{
std::cout << "loading file '" << fn << "'failed." << std::endl;
throw "Could not load file.";
}
BoostGraphType g;
BoostDynamicProperties dp ;
const std::string vn = "vertex_name";
dp.property(vn,get(vertex_name,g));
read_graphml(is, g, dp);
for (auto vp = vertices(g); vp.first != vp.second; ++vp.first)
{
std::cout << "index '" << get(vertex_index,g,*vp.first) << "' ";
std::cout << "name '" << get(vertex_name,g,*vp.first) << "'"
<< std::endl;
}
return 0;
}
I am using the the following GraphML test file:
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="d0" for="node" attr.name="vertex_name" attr.type="string"/>
<graph id="G" edgedefault="directed">
<node id="A"> <data key="d0">A</data> </node>
<node id="B"> <data key="d0">B</data> </node>
<edge id="0" source="A" target="B"/>
</graph>
</graphml>
read_graphml throws an exception with the helpful message (e.what()):
parse error: unrecognized type "
It seems the problem is related to the vertex_name association (which I got from a comment to a previous question of mine).
If I remove
<data key="d0">A</data>
from the node, it works.
However, I need to be able to identify the vertices by vertex_name.
How can I fix this so it properly parses the graphml and does not throw? What am I doing wrong?
Your code works perfectly when I run it.
>wilbert.exe
index '0' name 'A'
index '1' name 'B'
This is using boost v1.52 on windows 7
这篇关于使用Boost图形读取GraphML文件时使用vertex_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!