【问题标题】:Not able to read graphML using boost::read_graphml无法使用 boost::read_graphml 读取 graphML
【发布时间】:2018-06-21 12:42:56
【问题描述】:

我正在使用 boost 1.64 版并尝试使用 boost::read_graphml.. 读取一个 graphml 文件。 代码编译成功但运行时抛出异常:

在抛出 'boost::exception_detail::clone_impl >'的实例后调用终止
什么():解析错误:无法识别的类型“”键 中止(核心转储)

test.cpp

#include <boost/graph/graph_utility.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
#include <fstream>

struct GraphData {
    std::string Name;
};
struct VertexProperty
{
    std::string Name ;
};

struct EdgeProperty
{
    std::string Name ;
};

using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, GraphData>;

Graph ReadGraph(std::string const& fileName) {
    Graph graph;
    boost::dynamic_properties dp;
    dp.property("Name", boost::get(&GraphData::Name, graph));

    std::ifstream file(fileName);
    boost::read_graphml(file, graph, dp);

    return graph;
}

int main() {
    Graph g = ReadGraph("111.graphml");
    print_graph(g, get(&GraphData::Name, g));

}

111.graphml

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="key0" for="node" attr.name="Name" attr.type="string" />
  <graph id="G" edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0">
      <data key="key0">A</data>
    </node>
    <node id="n1">
      <data key="key0">D</data>
    </node>
    <node id="n2">
      <data key="key0">B</data>
    </node>
    <node id="n3">
      <data key="key0">C</data>
    </node>
    <edge id="e0" source="n0" target="n1">
    </edge>
    <edge id="e1" source="n2" target="n3">
    </edge>
  </graph>
</graphml>

感谢任何帮助!!!

【问题讨论】:

    标签: c++ boost boost-graph graphml


    【解决方案1】:

    我无法使用显示的代码/xml 重现:

    但我确实注意到您在与顶点属性包对应的位置使用 GraphData

    也许你想要

    using Graph = boost::adjacency_list<boost::setS, boost::vecS, 
       boost::directedS, VertexProperty, EdgeProperty, GraphData>;
    

    然后使用boost::get(&amp;VertexProperty::Name, graph) 代替GraphData 属性。

    更进一步的推测,您可能希望指示 graphml 解析器忽略任何未映射的外部属性:

    boost::dynamic_properties dp(boost::ignore_other_properties);
    

    Live On Coliru

    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/graph_utility.hpp>
    #include <boost/graph/graphml.hpp>
    #include <boost/property_map/dynamic_property_map.hpp>
    #include <boost/property_map/property_map.hpp>
    #include <fstream>
    
    struct GraphData { std::string Name; };
    struct VertexProperty { std::string Name; };
    struct EdgeProperty { std::string Name; };
    
    using Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProperty, EdgeProperty, GraphData>;
    
    Graph ReadGraph(std::istream& is) {
        Graph graph;
        boost::dynamic_properties dp(boost::ignore_other_properties);
        dp.property("Name", boost::get(&VertexProperty::Name, graph));
    
        boost::read_graphml(is, graph, dp);
    
        return graph;
    }
    
    extern std::string const graphml_111;
    
    int main() {
        std::istringstream is(graphml_111);
        Graph g = ReadGraph(is);
        print_graph(g, get(&VertexProperty::Name, g));
    }
    
    std::string const graphml_111 = R"(<?xml version="1.0" encoding="UTF-8"?>
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
      <key id="key0" for="node" attr.name="Name" attr.type="string" />
      <graph id="G" edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
        <node id="n0">
          <data key="key0">A</data>
        </node>
        <node id="n1">
          <data key="key0">D</data>
        </node>
        <node id="n2">
          <data key="key0">B</data>
        </node>
        <node id="n3">
          <data key="key0">C</data>
        </node>
        <edge id="e0" source="n0" target="n1">
        </edge>
        <edge id="e1" source="n2" target="n3">
        </edge>
      </graph>
    </graphml>)";
    

    打印

    A --> D 
    D --> 
    B --> C 
    C -->
    

    【讨论】:

    • 感谢您提供详细信息@sehe ...。我真的不明白我到底出了什么问题,我仍然得到运行上面的测试用例的异常...奇怪...
    • 我的水晶球告诉我您可能正在使用错误的测试文件或与预期不同的可执行文件进行测试。如果仔细检查您的程序在哪个文件夹中运行。也许删除并重建并在 ide 之外启动应用程序
    猜你喜欢
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多