如何使用Boost图形库获取边缘的端口标识符?

4

使用Boost Graph Library,是否可以获取边的端口标识符?

例如:调用read_graphviz后,我可以遍历此图的边缘并打印它们的node_id - 我得到"A -> B,A -> B"。如何打印类似于"A:p0 -> B:p1,A:p0 -> B:p2"的内容?

digraph G {
    A [label="A|<p0>p0"];
    B [label="B|<p1>p1|<p2>p2"];

    A:p0 -> B:p1;
    A:p0 -> B:p2;
}

Rendering of digraph G

1个回答

4

read_graphviz_new.hpp源代码中:

struct edge_info {
  node_and_port source;
  node_and_port target;
  properties props;
};

node_and_port 的格式如下:

struct node_and_port {
  node_name name;
  std::string angle; // Or empty if no angle
  std::vector<std::string> location; // Up to two identifiers
  // ...
}

我认为(但尚未验证),如果您直接使用解析器调用以下命令,则可以获得这些结果:

 void parse_graphviz_from_string(const std::string& str, parser_result& result, bool want_directed);

在命名空间boost::read_graphviz_detail中。如果您直接使用read_graphviz,它也可能在dynamic_property_map中可用;它内部引用read_graphviz_new
注意:graphviz.hpp中,根据#ifdef选择了两个graphviz解析器之一:
#ifdef BOOST_GRAPH_USE_SPIRIT_PARSER
  return read_graphviz_spirit(data.begin(), data.end(), graph, dp, node_id);
#else // Non-Spirit parser
  return read_graphviz_new(data,graph,dp,node_id);
#endif

如果我理解正确的话,那么您需要非Spirit解析器;基于Spirit的解析器似乎忽略了端口。不管怎样,这只是基于对Boost v.1.44源代码的快速查看;对我而言,感兴趣的代码位于/usr/include/boost/graph/detail/read_graphviz_new.hpp。我没有测试过,但看起来所有的工具都在那里。

谢谢 - 它起作用了。为了澄清,您将一个 parser_result 传递给 parse_graphviz_from_string,对于其 edges 向量中的每个 edge_info,您查看每个 node_and_portlocation 字段。 - jlstrecker
澄清一下,在read_graphviz中我们无法获取端口信息。在translate_results_to_graph函数中,只有ei.source.nameei.target.name被复制到返回的图形中。 - chain ro

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