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

时间:2015-04-27 14:13:53

标签: c++ boost graphviz dot

我正在尝试从Graphviz DOT文件中读取图表。我对Vertex的两个属性感兴趣 - 它的id和外围。 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"]
}

当我运行它时,我得到异常“未找到属性:标签”。我做错了什么?

1 个答案:

答案 0 :(得分:7)

您没有为&#34;标签&#34;定义(动态)属性地图。

使用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_propertiesread_graphviz() in Boost::Graph, pass to constructor

的更多说明,请参阅此处