4

I'm trying to generate a .dot which displays an horizontal graph with Boost Graph Library.

My code when creating my graph looks like this:

struct VertexP {
    std::string tag;
};

struct EdgeP {
    std::string symbol;
};

struct GraphP{
    std::string orientation;
};

typedef boost::adjacency_list<boost::vecS,
        boost::vecS, boost::directedS,
        VertexP, EdgeP, GraphP> Graph;

GraphP property;
property.orientation = "LR";
Graph graph(property);
// Then fill the graph

The code I'm using to generate the .dot file is something like this:

Graph g = creator.AutomatonToGraph(&automaton);
ofstream dot_file("automaton.dot");
dynamic_properties dp;
dp.property("node_id", get(&VertexP::tag, g));
dp.property("label", get(&VertexP::tag, g));
dp.property("label", get(&EdgeP::symbol, g));

write_graphviz_dp(dot_file, g, dp);

This writes the .dot file perfectly with nodes and edges labels, but my problem is that I want to add the rankdir=LR graph property to the output file. I tried with:

Graph g = creator.AutomatonToGraph(&automaton);
ofstream dot_file("automaton.dot");
dynamic_properties dp;
dp.property("node_id", get(&VertexP::tag, g));
dp.property("label", get(&VertexP::tag, g));
dp.property("label", get(&EdgeP::symbol, g));
dp.property("rankdir", get(&GraphP::orientation, g));

write_graphviz_dp(dot_file, g, dp);

But I get a big chunk of errors starting by this:

/src/lab2.cc:48:55:   required from here
/usr/include/boost/graph/detail/adjacency_list.hpp:2585:29: error: forming reference to void
         typedef value_type& reference;
                             ^~~~~~~~~

I'm really new using BGL, what I'm doing wrong?

Chromz
  • 183
  • 3
  • 11

1 Answers1

1

Reading into the implementation of dynamic_graph_properties_writer, I figured out you should probably just do

    dp.property("rankdir", boost::make_constant_property<Graph*>(std::string("LR")));

For dynamic retrieval you can use a function property map: (map set/get requests into C++ class/structure changes):

#include <boost/property_map/function_property_map.hpp>

    dp.property("rankdir", boost::make_function_property_map<Graph*>([](Graph const* g) { return g->m_property->orientation; }));

Live Demo

See it Live On Wandbox

#include <boost/graph/adj_list_serialize.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/function_property_map.hpp>
#include <fstream>
using namespace boost;

struct VertexP { std::string tag; };
struct EdgeP { std::string symbol; };
struct GraphP { std::string orientation; };

typedef adjacency_list<vecS, vecS, directedS, VertexP, EdgeP, GraphP> Graph;

int main() {
    Graph g(GraphP{"LR"});
    // Then fill the graph
    add_edge(
        add_vertex(VertexP{ "tag1" }, g),
        add_vertex(VertexP{ "tag2" }, g),
        EdgeP{ "symbol" }, g
    );

    {
        std::ofstream dot_file("automaton.dot");
        dynamic_properties dp;
        dp.property("node_id", get(&VertexP::tag, g));
        dp.property("label", get(&VertexP::tag, g));
        dp.property("label", get(&EdgeP::symbol, g));
        dp.property("rankdir", boost::make_constant_property<Graph*>(std::string("LR")));
        dp.property("dummy", boost::make_function_property_map<Graph*>([](Graph const* g) { return g->m_property->orientation; }));

        write_graphviz_dp(dot_file, g, dp);
    }
}

Which writes

digraph G {
dummy=LR;
rankdir=LR;
tag2 [label=tag2];
tag1 [label=tag1];
tag1->tag2  [label=symbol];
}
sehe
  • 374,641
  • 47
  • 450
  • 633