boost::read_graphviz - 如何读出属性?

boost::read_graphviz - how to read out properties?

我正在尝试从 Graphviz DOT 文件中读取图表。我对 Vertex 的两个属性感兴趣——它的 id 和 peripheries。 A 还想加载图形标签。

我的代码如下所示:

struct DotVertex {
    std::string name;
    int peripheries;
};

struct DotEdge {
    std::string label;
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
        DotVertex, DotEdge> graph_t;

    graph_t graphviz;
    boost::dynamic_properties dp;

    dp.property("node_id", boost::get(&DotVertex::name, graphviz));
    dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
    dp.property("edge_id", boost::get(&DotEdge::label, graphviz));

    bool status = boost::read_graphviz(dot, graphviz, dp);

我的示例 DOT 文件如下所示:

digraph G {
  rankdir=LR
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0", peripheries=2]
  0 -> 0 [label="a"]
  0 -> 1 [label="!a"]
  1 [label="1"]
  1 -> 0 [label="a"]
  1 -> 1 [label="!a"]
}

当我 运行 它时,我得到异常 "Property not found: label"。我做错了什么?

您没有为 "label" 定义(动态)属性地图。

要么使用ignore_other_properties,要么定义它:)

在下面的示例中,使用 ignore_other_properties 可以避免需要 rankdir(图形 属性)和 widthstyle(顶点属性):

Live On Coliru

#include <boost/graph/graphviz.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>
#include <iostream>

struct DotVertex {
    std::string name;
    std::string label;
    int peripheries;
};

struct DotEdge {
    std::string label;
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
        DotVertex, DotEdge> graph_t;

int main() {
    graph_t graphviz;
    boost::dynamic_properties dp(boost::ignore_other_properties);

    dp.property("node_id",     boost::get(&DotVertex::name,        graphviz));
    dp.property("label",       boost::get(&DotVertex::label,       graphviz));
    dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
    dp.property("label",       boost::get(&DotEdge::label,         graphviz));

    bool status = boost::read_graphviz(std::cin, graphviz, dp);
    return status? 0 : 255;
}

哪个运行成功

有关使用 dynamic_properties 的更多说明,请参阅此处: