boost::read_graphviz - how to read out properties?
Asked Answered
C

1

6

I am trying to read a graph from Graphviz DOT file. I am interested in two properties for Vertex - its id and peripheries. A also want to load graph labels.

My code looks like this:

struct DotVertex {
    std::string name;
    int peripheries;
};

struct DotEdge {
    std::string label;
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
        DotVertex, DotEdge> graph_t;

    graph_t graphviz;
    boost::dynamic_properties dp;

    dp.property("node_id", boost::get(&DotVertex::name, graphviz));
    dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
    dp.property("edge_id", boost::get(&DotEdge::label, graphviz));

    bool status = boost::read_graphviz(dot, graphviz, dp);

My sample DOT file looks like this:

digraph G {
  rankdir=LR
  I [label="", style=invis, width=0]
  I -> 0
  0 [label="0", peripheries=2]
  0 -> 0 [label="a"]
  0 -> 1 [label="!a"]
  1 [label="1"]
  1 -> 0 [label="a"]
  1 -> 1 [label="!a"]
}

When I run it, I get exception "Property not found: label". What am I doing wrong?

Condemn answered 27/4, 2015 at 14:13 Comment(0)
G
9

You didn't define the (dynamic) propertymap for "label".

Either use ignore_other_properties or define it :)

In the sample below, using ignore_other_properties prevents requiring rankdir (graph property) and width, style (vertex properties):

Live On Coliru

#include <boost/graph/graphviz.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>
#include <iostream>

struct DotVertex {
    std::string name;
    std::string label;
    int peripheries;
};

struct DotEdge {
    std::string label;
};

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS,
        DotVertex, DotEdge> graph_t;

int main() {
    graph_t graphviz;
    boost::dynamic_properties dp(boost::ignore_other_properties);

    dp.property("node_id",     boost::get(&DotVertex::name,        graphviz));
    dp.property("label",       boost::get(&DotVertex::label,       graphviz));
    dp.property("peripheries", boost::get(&DotVertex::peripheries, graphviz));
    dp.property("label",       boost::get(&DotEdge::label,         graphviz));

    bool status = boost::read_graphviz(std::cin, graphviz, dp);
    return status? 0 : 255;
}

Which runs successfully

See here for more explanations about using dynamic_properties: read_graphviz() in Boost::Graph, pass to constructor

Gunnar answered 27/4, 2015 at 19:28 Comment(1)
Thank a lot! I didn't undestood the usage of the properties from the documentation.Condemn

© 2022 - 2024 — McMap. All rights reserved.