我正在尝试加载一个简单的 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 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;
}
我正在使用以下 GraphML 测试文件:
<?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 抛出异常并提供有用的信息 (e.what()):
parse error: unrecognized type "
问题似乎与 vertex_name 关联有关(我从对 previous question of mine 的评论中得到)。
如果我删除
<data key="d0">A</data>
从节点开始,它起作用了。
但是,我需要能够通过 vertex_name 识别顶点。
我该如何解决这个问题,以便它正确解析 graphml 而不会抛出异常?我做错了什么?
最佳答案
您的代码在我运行时完美运行。
>wilbert.exe
index '0' name 'A'
index '1' name 'B'
这是在 Windows 7 上使用 boost v1.52
关于c++ - 使用 Boost Graph 读取 GraphML 文件时使用 vertex_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16667175/