Boost: access graph specific properties with read_graphml()

588 views Asked by At

I am trying to read graph related (custom) properties from a .graphml file created with yEd using the Boost Graph library. Reading vertex and edge (dynamic_)properties works but my graph properties are always empty. I've also came across how to read graph-domain attributes with boost::read_graphml? but that solution just produces empty strings (it's in the code below). Apart from that, I was not able to find much information about the problem.

Here's the shortened code (complete working example test.cpp here):

struct VertexProperties { string url, description; };
struct EdgeProperties { string url, description; };
struct GraphProperties { string title; };
// ...
typedef adjacency_list<vecS, vecS, directedS, VertexProperties, EdgeProperties, GraphProperties> DirectedGraph;
typedef dynamic_properties Properties;
DirectedGraph graph(0);

Properties props(ignore_other_properties);
props.property("url", get(&VertexProperties::url, graph));
props.property("description", get(&VertexProperties::description, graph));
props.property("url", get(&EdgeProperties::url, graph));
props.property("description", get(&EdgeProperties::description, graph));
map<string, string> attribute_name2name;
associative_property_map<map<string, string>> graphname_map(attribute_name2name);
props.property("title", graphname_map);
// ...
read_graphml(validated, graph, props);
graph[graph_bundle].title = get(graphname_map, "title");
cout << "\"" << graph[graph_bundle].title << "\"" << endl;

You can compile the full code using g++ test.cpp --std=c++11 -o test -lboost_graph. Running it with ./test simple_graph.graphml produces just "" instead of "foobar" which is the expected output since the graph has the

<data key="d1"><![CDATA[foobar]]></data>

tag which is defined as

<key attr.name="title" attr.type="string" for="graph" id="d1">
  <default/>
</key>

I've uploaded an simple_graph.graphml example file (not enough rep. to post img / more details).

Minor follow-up question: is it possible to load the graph w/o 'fixing' the yEd-exported file (cf. code)? The parser always complains about lines like this (not sure if it's even permitted in the GraphML standard allowed in the standard: "This group consists of the two optional attributes - attr.name (gives the name for the data function) - attr.type ((declares the range of values for the data function)."):

<key for="port" id="d2" yfiles.type="portgraphics"/>

with this error:

parse error: unrecognized type "" for key

Any help/ideas are highly appreciated. Thank you very much!

1

There are 1 answers

0
inzaghi250 On

Try this:

template<typename MutableGraph>
class mutate_graph_impl_yed : public mutate_graph_impl<MutableGraph>
{
public:
    mutate_graph_impl_yed(MutableGraph& g, dynamic_properties& dp)
        : mutate_graph_impl<MutableGraph>(g,dp) { }

    virtual void
        set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
    {
        bool type_found = false;
        try
        {
            mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::vertex_descriptor, value_types>
                (name, m_dp, any_cast<graph_traits<MutableGraph>::vertex_descriptor>(vertex),
                    value, value_type, m_type_names, type_found));
        }
        catch (bad_lexical_cast)
        {
            BOOST_THROW_EXCEPTION(
                parse_error("invalid value \"" + value + "\" for key " +
                    name + " of type " + value_type));
        }
    }

    virtual void
        set_edge_property(const std::string& name, any edge, const std::string& value, const std::string& value_type)
    {
        bool type_found = false;
        try
        {
            mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::edge_descriptor, value_types>
                (name, m_dp, any_cast<graph_traits<MutableGraph>::edge_descriptor>(edge),
                    value, value_type, m_type_names, type_found));
        }
        catch (bad_lexical_cast)
        {
            BOOST_THROW_EXCEPTION(
                parse_error("invalid value \"" + value + "\" for key " +
                    name + " of type " + value_type));
        }
    }
};

Replace your call of read_graphml with:

mutate_graph_impl_yed<Graph> mg(g, dp);
read_graphml(fin, mg, 0);