gpt4 book ai didi

c++ - Boost:使用 read_graphml() 访问图的特定属性

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:43 26 4
gpt4 key购买 nike

我正在尝试从使用 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"/>

出现此错误:

parse error: unrecognized type "" for key

非常感谢任何帮助/想法。非常感谢!

最佳答案

试试这个:

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

关于c++ - Boost:使用 read_graphml() 访问图的特定属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41317891/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com