Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::read_graphviz - how to read out properties?

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?

like image 763
yaqwsx Avatar asked Apr 27 '15 14:04

yaqwsx


1 Answers

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

like image 61
sehe Avatar answered Nov 01 '22 20:11

sehe