2

I am using boost version 1.64 and trying to read a graphml file using boost::read_graphml.. The code compiles but throws an exception when run:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
what(): parse error: unrecognized type "" for key Abort (core dumped)

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>

Appreciate any help on this !!!

Nishant Sharma
  • 341
  • 2
  • 5
  • 17

1 Answers1

2

I can't repro that using the code/xml shown:

However I do note that you're using GraphData in the position that corresponds tot the Vertex properties bundle.

Perhaps you wanted

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

And then use boost::get(&VertexProperty::Name, graph) instead of the GraphData property.

Even more conjecturing, you might want to instruct the graphml parser to ignore any extaneous properties not mapped:

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

Prints

A --> D 
D --> 
B --> C 
C -->
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for the details@sehe ... .I really do not understand whats wrong at my end that I still get that exception running the testcase above... weird... – Nishant Sharma Jun 22 '18 at 06:50
  • My crystal ball tells me you might be testing using the wrong test file or a different executable than intended. If double check which folder your program runs in. Perhaps deleting and rebuilding and starting the application outside the ide – sehe Jun 22 '18 at 08:15