如何使用boost::read_graphml读取图域属性
how to read graph-domain attributes with boost::read_graphml?
可能是一个愚蠢的问题,但我在网上找不到任何答案。我的应用程序从自定义文件读取拓扑,并从中构建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");
相关文章:
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 从C++代码中直接读取成员属性
- C 从文件中读取对象具有数组属性
- 从C 读取QML数组属性
- 如何从C 中的HDF5文件中读取属性名称和数据集名称,在这里我不知道HDF5文件中存在什么属性
- 我如何使用GO语言读取可能是两种不同数据类型之一的HDF5属性
- 如何从提升属性树中读取字段类型
- 声明仅在C 中使用读取属性的接口
- MFC SDI在更改属性时从INI读取并更新视图字符串
- 组合框初始化错误:无法读取未定义的属性'constructor'
- 运行时未读取log4cplus属性文件更改
- Boost属性树Json读取包含LPWSTR的文件
- 我正在尝试通过 Boost 图形库从 Graphviz DOT 文件中读取图形.如何读取存储在数组中的未知数量的属性
- 如何从 TMXObjectGroup 读取对象定制属性
- 使用派生类"istream"读取基类的属性
- 读取配置属性的简单方法
- 通过boost::read_graphviz()读取boost动态属性时发生异常
- 在c++中使用xerces读取属性名
- 用c++从QML单例中读取属性
- HDF5 C++读取属性的内容