问题
I'm trying to build a simple GraphML loader using BOOST libraries. I have a GraphML file and I want to load it in a boost adjacency list structure. The graph is directed and the only information that it is stored are the name of the nodes (0,1,2,...) and the edges from one node to another. What I did is:
void loadHierarchy(){
// ...
std::ifstream inFile;
inFile.open("ext.gml", std::ifstream::in);
typedef boost::adjacency_list<> Graph;
Graph g;
boost::read_graphml(inFile, g);
// ...
}
I don't need to use any properties, just to keep the whole graph information in the adjacency list.
The errors that I get are the following:
error: invalid initialization of reference of type
‘boost::mutate_graph&’
from expression of type‘loadHierarchy()::Graph’
/usr/include/boost/graph/graphml.hpp:194: error: in passing argument 2 of
‘void boost::read_graphml(std::istream&, boost::mutate_graph&)’
It should be as simple as that, but apparently it isn't.
回答1:
I think you should use the 3 parameter version of read_graphml(), even if you do not need any properties to be set. The two parameter version that you want to use is an (unfortunately exposed) internal detail of the library.
So, I suggest you to try something like this:
boost::dynamic_properties dp;
boost::read_graphml(inFile, g, dp);
I hope it helped.
回答2:
after a more thorough investigation, i've come to the conclusion it is actually fortunate the 2 param version of boost::read_graphml is exposed. The 3 param one looks like this:
template<typename MutableGraph>
void
read_graphml(std::istream& in, MutableGraph& g, dynamic_properties& dp)
{
mutate_graph_impl<MutableGraph> mg(g,dp);
read_graphml(in, mg);
}
There is a particularly good GraphML editor out there, namely yEd that output a kind of malformed GraphML file, for example it has tags like
<key for="node" id="d6" yfiles.type="nodegraphics"/>
in it. The above key should have a attr.type="string" in it, yet it does not. It instead has a yfiles.type, which seems to be an extension yEd is using (unfortunately). The default mutate_graph_impl can't handle this. mutate_graph_impl will need to be inherited by you, and you will need to directly call the 2 version read_graphml with your own implementation of mutate_graph_impl passed to it. In your own implementation, you will need to override mutate_graph_impl's
virtual void
set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
to handle the key with unspecified attr.type.
来源:https://stackoverflow.com/questions/6877838/boost-read-graphml-example