如何使用boost::read_graphml读取图域属性

how to read graph-domain attributes with boost::read_graphml?

本文关键字:读取 属性 graphml 何使用 boost read      更新时间:2023-10-16

可能是一个愚蠢的问题,但我在网上找不到任何答案。我的应用程序从自定义文件读取拓扑,并从中构建boost::图。我正在转向更标准的图形表示。我可以使用vertex_descriptor作为键来读/写节点属性,类似地,我可以使用edge_descriptor作为边缘属性,但是图形属性呢?当在graphml文件中读取它们时,它们将与哪个键类型相关联?

为了解释我的疑问,下面是我必须定义图形并读取graphml文件的代码:

struct NetworkNode {
  int ponCustomers;
  int asid;
}; //bundled property map for nodes
struct NetworkEdge {
  int length;
  Capacity maxCapacity;
  Capacity spareCapacity;
  std::set<Flow*> activeFlows;
  Capacity peakCapacity;
}; //bundled property map for edges
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, 
    NetworkNode, NetworkEdge> DGraph;
typedef DGraph::vertex_descriptor Vertex;
[...]
DGraph topology;
boost::dynamic_properties dp;
dp.property("asid", boost::get(&NetworkNode::asid, topology));
dp.property("ponCustomers", boost::get(&NetworkNode::ponCustomers, topology));
dp.property("length", boost::get(&NetworkEdge::length, topology));
dp.property("maxCapacity", boost::get(&NetworkEdge::maxCapacity, topology));
dp.property("spareCapacity", boost::get(&NetworkEdge::spareCapacity, topology));
dp.property("peakCapacity", boost::get(&NetworkEdge::peakCapacity, topology));    
std::map<Vertex, int> avgUsersMap;
boost::associative_property_map<std::map<Vertex, int> >
    avgUsersPMap(avgUsersMap);
dp.property("avgUsers", avgUsersPMap);
[...]
try {
  boost::read_graphml(stream, this->topology, dp);
} catch [...]

请注意我如何创建新的关联映射来存储对图的定义有用的属性(例如,当我构建它时),但不值得在整个图生命周期中存储在每个节点/边缘中。现在,有些性质与整个图有关;例如,我可以在graphml文件中定义像

这样的内容
<key id="name" for="graph" attr.name="graphName" attr.type="string" />

如何定义所需的property_map并将其添加到dp中,以便正确解析该位信息?

您可以为图形设置绑定属性,就像您对顶点和边所做的那样。

像这样:

struct graph_props {
   std::string myName;
...
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, 
    NetworkNode, NetworkEdge, graph_props > DGraph;

为了说服boost::read_graphml保存图形属性,你必须提供一个属性映射(它将只有一个成员)不幸的是,我猜你将不得不提取read_graphml放置到这个映射中的值,并设置绑定的图形属性属性。也许有人能指出一个更简洁的方法。

像这样:

std::map< std::string, std::string > attribute_name2name;
boost::associative_property_map< std::map< std::string, std::string > >
        graphname_map( attribute_name2name );
dp.property("graphname", graphname_map );
boost::read_graphml(stream, this->topology, dp);
topology[boost::graph_bundle].myName = get(graphname_map,"graphname");