【问题标题】:Boost: access graph specific properties with read_graphml()Boost:使用 read_graphml() 访问图的特定属性
【发布时间】:2016-12-25 00:31:03
【问题描述】:

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

出现此错误:

解析错误:键的类型“”无法识别

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

【问题讨论】:

  • this issue 可能相关,因为这两个错误都处理空 CDATA 解析结果。标记中的 CDATA 部分是否有解决方法?
  • 感谢您的回复!好点(我的代码中没有解决方法),但我认为它不相关,因为顶点和边属性被正确解析并且它们也在使用 CDATA,例如,(对于顶点)&lt;data key="d6"&gt;&lt;![CDATA[just a test]]&gt;&lt;/data&gt;。为了测试,我还删除了图形属性 (-> &lt;data key="d1"&gt;foobar&lt;/data&gt;) 中的 CDATA,但我仍然有空的图形属性。更多想法?

标签: c++ boost graph graphml dynamic-properties


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多