无法使用boost :: read_graphml读取graphML

时间:2018-06-21 12:42:56

标签: c++ boost boost-graph graphml

我正在使用Boost版本1.64,并尝试使用boost :: read_graphml读取graphml文件。 代码可以编译,但在运行时会引发异常:

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

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>

对此表示感谢!

1 个答案:

答案 0 :(得分:1)

我无法使用所示的代码/ xml对此进行复制:

但是,我确实注意到您在与“顶点属性”捆绑包相对应的位置使用GraphData

也许你想要

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

然后使用boost::get(&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 -->