我正在尝试使用Boost Graph库从yEd创建的.graphml 文件中读取图形相关(自定义)属性。读取顶点和边缘(dynamic_)属性有效但我的图形属性始终为空。我也遇到了how to read graph-domain attributes with boost::read_graphml?,但该解决方案只生成空字符串(它在下面的代码中)。除此之外,我无法找到有关此问题的更多信息。
以下是缩短的代码(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”,这是预期的输出,因为图表有
<data key="d1"><![CDATA[foobar]]></data>
标签,定义为
<key attr.name="title" attr.type="string" for="graph" id="d1">
<default/>
</key>
我上传了simple_graph.graphml example file (没有足够的代表来发布img /更多详情)。
次要跟进问题:是否可以加载图形,而无需“修复”yEd导出的文件(参见代码)?解析器总是抱怨这样的行(不确定它是否甚至允许在标准中允许的GraphML标准中:“该组由两个可选属性组成 - attr。 name(给出数据函数的名称) - attr.type((声明数据函数的值范围)。“):
<key for="port" id="d2" yfiles.type="portgraphics"/>
出现此错误:
解析错误:键无法识别类型“”
非常感谢任何帮助/想法。非常感谢你!
答案 0 :(得分:0)
尝试一下:
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的调用替换为:
mutate_graph_impl_yed<Graph> mg(g, dp);
read_graphml(fin, mg, 0);