gpt4 book ai didi

c++ - 无法使用 boost::read_graphml 读取 graphML

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:00 25 4
gpt4 key购买 nike

我正在使用 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>

感谢对此的任何帮助!!!

最佳答案

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

但是我确实注意到您在对应于 Vertex 属性包的位置使用 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 -->

关于c++ - 无法使用 boost::read_graphml 读取 graphML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50968873/

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