Boost read_graphml 示例

发布于 2024-11-27 04:23:40 字数 765 浏览 0 评论 0原文

我正在尝试使用 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: 错误:传递 'void boost::read_graphml(std::istream&, boost::mutate_graph&)' 的参数 2

应该就这么简单,但显然事实并非如此。

I'm trying to build a simple GraphML loader using BOOST libraries. I have a GraphML file and I want to load it in a boost adjacency list structure. The graph is directed and the only information that it is stored are the name of the nodes (0,1,2,...) and the edges from one node to another. What I did is:

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

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

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

I don't need to use any properties, just to keep the whole graph information in the adjacency list.

The errors that I get are the following:

error: invalid initialization of reference of type ‘boost::mutate_graph&’ from expression of type ‘loadHierarchy()::Graph’

/usr/include/boost/graph/graphml.hpp:194: error: in passing argument 2 of ‘void boost::read_graphml(std::istream&, boost::mutate_graph&)’

It should be as simple as that, but apparently it isn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-12-04 04:23:41

经过更彻底的调查后,我得出的结论是,boost::read_graphml 的 2 参数版本被暴露实际上是幸运的。第 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"/>

。上面的键中应该有一个 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 的密钥。

after a more thorough investigation, i've come to the conclusion it is actually fortunate the 2 param version of boost::read_graphml is exposed. The 3 param one looks like this:

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);
}

There is a particularly good GraphML editor out there, namely yEd that output a kind of malformed GraphML file, for example it has tags like

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

in it. The above key should have a attr.type="string" in it, yet it does not. It instead has a yfiles.type, which seems to be an extension yEd is using (unfortunately). The default mutate_graph_impl can't handle this. mutate_graph_impl will need to be inherited by you, and you will need to directly call the 2 version read_graphml with your own implementation of mutate_graph_impl passed to it.
In your own implementation, you will need to override mutate_graph_impl's

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

to handle the key with unspecified attr.type.

埋情葬爱 2024-12-04 04:23:40

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

所以,我建议你尝试这样的事情:

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

我希望它有所帮助。

I think you should use the 3 parameter version of read_graphml(), even if you do not need any properties to be set. The two parameter version that you want to use is an (unfortunately exposed) internal detail of the library.

So, I suggest you to try something like this:

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

I hope it helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文