Boost read_graphml 示例

3

我正在尝试使用BOOST库构建一个简单的GraphML加载器。我有一个GraphML文件,想要将其加载到一个boost邻接列表结构中。该图是有向的,存储的唯一信息是节点的名称(0,1,2...)以及从一个节点到另一个节点的边缘。我的做法如下:

void loadHierarchy(){
    // ...
    std::ifstream inFile;
    inFile.open("ext.gml", std::ifstream::in);

    typedef boost::adjacency_list<> Graph;
    Graph g;

    boost::read_graphml(inFile, g);
    // ...
}

我不需要使用任何属性,只需将整个图形信息保存在邻接表中。
我得到的错误如下:
错误:从类型为‘loadHierarchy()::Graph’的表达式初始化引用类型‘boost::mutate_graph&’无效 /usr/include/boost/graph/graphml.hpp:194:错误:传递参数2时‘void boost::read_graphml(std::istream&, boost::mutate_graph&)’出现问题
这应该很简单,但显然并非如此。
2个回答

2

我认为即使你不需要设置任何属性,也应该使用read_graphml()的三个参数版本。你想要使用的两个参数版本是库的一个(不幸地)暴露的内部细节。

所以,我建议你尝试像这样做:

boost::dynamic_properties dp;
boost::read_graphml(inFile, g, dp);

我希望您能从中受益。

2

经过更彻底的调查,我得出结论,暴露2个参数版本的boost::read_graphml实际上是幸运的。3个参数版本如下:

template<typename MutableGraph>
void
read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp)
{
    mutate_graph_impl<MutableGraph> mg(g,dp);
    read_graphml(in, mg);
}

有一个非常好的GraphML编辑器,名为yEd,但它输出的GraphML文件格式有些不规范,例如它的标签可能会像这样:


<key for="node" id="d6" yfiles.type="nodegraphics"/>

在 IT 技术中,上述密钥应该具有 attr.type="string" 属性,但它并没有。相反,它具有 yfiles.type,这似乎是 yEd 使用的扩展(不幸的是)。默认的 mutate_graph_impl 无法处理此情况。您需要继承 mutate_graph_impl,并直接调用 2 版本的 read_graphml,并将自己的 mutate_graph_impl 实现传递给它。

在您自己的实现中,您需要覆盖 mutate_graph_impl 的内容。

virtual void
    set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)

处理未指定attr.type的键。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接