boost::read_graphviz - 如何读出属性

boost::read_graphviz - how to read out properties?

本文关键字:何读出 属性 graphviz read boost      更新时间:2023-10-16

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

当我运行它时,我收到异常"找不到属性:标签"。我做错了什么?

您没有为"label"定义(动态)属性映射。

使用ignore_other_properties或定义它:)

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

住在科里鲁

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

哪个成功运行

有关在 Boost::Graph 中使用 dynamic_properties : read_graphviz() 的更多说明,请参阅此处,传递给构造函数