如何使用boost :: read_graphml读取图域属性? [英] how to read graph-domain attributes with boost::read_graphml?

查看:276
本文介绍了如何使用boost :: read_graphml读取图域属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Probably a silly question, but I can't find any answer online. My application reads a topology from a custom file and builds a boost::graph out of it. I'm in the process of moving to a more standard graphml representation. I can read/write node properties using a vertex_descriptor as a key, and similarly I can use an edge_descriptor for edge attributes, but what about graph attributes? To which key type will they be associated when they are read in the graphml file?

为解释我的疑问,这是我必须定义图并阅读graphml文件的代码:

To explain my doubt, here's the code I have to define the graph and read the graphml file:

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文件中定义类似

Notice how I create new associative maps to store properties that are useful for the definition of the graph (e.g. when I build it) but not worth storing in every single node/edge for the entire graph lifetime. Now, some of these properties are related to the entire graph; for example I could define in the graphml file something like

<key id="name" for="graph" attr.name="graphName" attr.type="string" />

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

How do I define the required property_map and add it to dp so that this bit of information will be parsed correctly?

推荐答案

您可以设置图形的捆绑属性,就像处理顶点和边一样.

You can setup bundled properties for the graph, just like you did with the vertices and edges.

类似这样的东西:

struct graph_props {
   std::string myName;
...
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, 
    NetworkNode, NetworkEdge, graph_props > DGraph;

为了说服boost :: read_graphml保存图属性,您必须提供一个属性图(该图只有一个成员)不幸的是,AFAIK必须将值read_graphml放置到该图中并设置捆绑图属性属性.也许有人可以指出一种更整洁的方法.

In order to convince boost::read_graphml to save graph properties you have to supply a property map ( which will have just one member ) Unfortunately, AFAIK you will have to extract the values read_graphml places into this map and set the bundled graph property attributes. Maybe someone can point out a neater way to do this.

类似这样的东西:

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");

这篇关于如何使用boost :: read_graphml读取图域属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆