Boost:使用read_graphml()访问图形特定的属性 [英] Boost: access graph specific properties with read_graphml()
问题描述
我正在尝试使用Boost Graph库从yEd创建的.graphml 文件中读取与图形相关的(自定义)属性.读取顶点和边(dynamic_)属性有效,但是我的图形属性始终为空.我也遇到过如何使用boost :: read_graphml?,但是该解决方案只会产生空字符串(在下面的代码中).除此之外,我找不到关于该问题的很多信息.
I am trying to read graph related (custom) properties from a .graphml file created with yEd using the Boost Graph library. Reading vertex and edge (dynamic_)properties works but my graph properties are always empty. I've also came across how to read graph-domain attributes with boost::read_graphml? but that solution just produces empty strings (it's in the code below). Apart from that, I was not able to find much information about the problem.
以下是缩短的代码(完整的工作示例test.cpp
此处):
Here's the shortened code (complete working example test.cpp
here):
struct VertexProperties { string url, description; };
struct EdgeProperties { string url, description; };
struct GraphProperties { string title; };
// ...
typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties, GraphProperties> DirectedGraph;
typedef dynamic_properties Properties;
DirectedGraph graph(0);
Properties props(ignore_other_properties);
props.property("url", get(&VertexProperties::url, graph));
props.property("description", get(&VertexProperties::description, graph));
props.property("url", get(&EdgeProperties::url, graph));
props.property("description", get(&EdgeProperties::description, graph));
map<string, string> attribute_name2name;
associative_property_map<map<string, string>> graphname_map(attribute_name2name);
props.property("title", graphname_map);
// ...
read_graphml(validated, graph, props);
graph[graph_bundle].title = get(graphname_map, "title");
cout << "\"" << graph[graph_bundle].title << "\"" << endl;
您可以使用g++ test.cpp --std=c++11 -o test -lboost_graph
编译完整代码.用./test simple_graph.graphml
运行它只会产生"而不是"foobar",这是预期的输出,因为图形具有
You can compile the full code using g++ test.cpp --std=c++11 -o test -lboost_graph
. Running it with ./test simple_graph.graphml
produces just "" instead of "foobar" which is the expected output since the graph has the
<data key="d1"><![CDATA[foobar]]></data>
标签定义为
<key attr.name="title" attr.type="string" for="graph" id="d1">
<default/>
</key>
我上载了 simple_graph.graphml示例文件 (重复次数不足.发布img/更多详细信息).
I've uploaded an simple_graph.graphml example file (not enough rep. to post img / more details).
较小的后续问题:是否可以在不固定" yEd导出文件的情况下加载图形(参见代码)?解析器总是抱怨像这样的行(不确定它是否在GraphML标准中甚至允许在该标准中允许:该组由两个 optional 属性-attr组成.名称(为数据功能命名)-attr.type((声明数据功能的值范围).):
Minor follow-up question: is it possible to load the graph w/o 'fixing' the yEd-exported file (cf. code)? The parser always complains about lines like this (not sure if it's even permitted in the GraphML standard allowed in the standard: "This group consists of the two optional attributes - attr.name (gives the name for the data function) - attr.type ((declares the range of values for the data function)."):
<key for="port" id="d2" yfiles.type="portgraphics"/>
出现此错误:
解析错误:密钥无法识别的类型为"
parse error: unrecognized type "" for key
任何帮助/想法都受到高度赞赏.非常感谢你!
Any help/ideas are highly appreciated. Thank you very much!
推荐答案
尝试一下:
template<typename MutableGraph>
class mutate_graph_impl_yed : public mutate_graph_impl<MutableGraph>
{
public:
mutate_graph_impl_yed(MutableGraph& g, dynamic_properties& dp)
: mutate_graph_impl<MutableGraph>(g,dp) { }
virtual void
set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::vertex_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::vertex_descriptor>(vertex),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
virtual void
set_edge_property(const std::string& name, any edge, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::edge_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::edge_descriptor>(edge),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
};
将您对read_graphml的调用替换为:
Replace your call of read_graphml with:
mutate_graph_impl_yed<Graph> mg(g, dp);
read_graphml(fin, mg, 0);
这篇关于Boost:使用read_graphml()访问图形特定的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!