3

I have already asked a question here about using Boost Graph Library and writing graph into file. Due to change in my requirements, I need to write dynamic graph properties into a DOT file. After some look up, I managed to come up with some code but it does not work. Below is what I have done so far:

Map class uses the Cell class as vertices and Cell class uses a separate CellProperty class for setting and getting all the Cell properties.

And finally Map class where I build the graph and try to write the graph into a DOT file.

Map.h

class Map {
 public:
  typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS, Cell> Graph;
  typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;

  explicit Map(std::string pGraphFilePath);
  virtual ~Map();
  void LoadGraph();
 private:
  Graph mGraph;
  std::vector<std::vector<Vertex>> mGrid;
};

Map.cpp

const unsigned int RowNum = 3;
const unsigned int ColumnNum = 4;

Map::Map(std::string pGraphFilePath) : mGraph(), mGrid() {}
Map::~Map() {}

void Map::LoadGraph() {
  int dummyID = 1;
  for (unsigned int row = 0; row < RowNum; row++) {
    mGrid.resize(RowNum);
    for (unsigned int col = 0; col < ColumnNum; col++) {
      mGrid[row].resize(ColumnNum);

      Vertex vID = boost::add_vertex(mGraph);
      mGraph[vID].SetProperty<unsigned int>("ID", dummyID);
      mGraph[vID].SetProperty<bool>("Navigable", true);
      mGrid[row][col] = vID;
      dummyID++;
      // add the edges for the contained cells in the grid
      if (col > 0) { boost::add_edge(mGrid[row][col - 1], mGrid[row][col], mGraph); }
      if (row > 0) { boost::add_edge(mGrid[row - 1][col], mGrid[row][col], mGraph); }
    }
  }

  // write cell properties
  boost::dynamic_properties propertiesOutPut;

  propertiesOutPut.property("ID", boost::get(boost::vertex_index, mGraph));

  // As Navigable is an external property, it need to be mapped with the internal graph property
  // the lines below are the update after I got the answers and link for my query
  // cell.GetProperty() is a templated method the takes a default parameter, thus passing "false" bool parameter which returns the "Navigable" cell property
  auto valueNavigable = boost::make_transform_value_property_map([](Cell &cell) { return cell.GetProperty<bool>("Navigable", false); }, boost::get(boost::vertex_bundle, mGraph));
  propertiesOutPut.property("Navigable", valueNavigable);

  std::ofstream fout("MyGraph.dot");
  boost::write_graphviz_dp(fout, mGraph, propertiesOutPut, std::string("ID"));
}

The problem I am getting is with the propertiesOutPut.property() method for boost::get(). I can not figure out the correct parameters for boost::get(). Please help me out. Thanks !!

Community
  • 1
  • 1
soupso
  • 592
  • 1
  • 4
  • 20
  • 1
    If this is already a minimal example then good luck with out a clear question statement. – luk32 Dec 15 '15 at 15:19

1 Answers1

4

You could use a transform_value_property_map on top of the propertymap that contains the vertex properties struct. (You didn't show it).

I have a number of answers showing how to do that, although these are all using internal properties, there is no big difference because anu property map can be transformed in the same way, regardless of whether the property map is internal or external (that's the whole purpose of property maps: decoupling the way properties are accessed).

Most relevant:

Other:

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks Sehe. So if I understood you correctly then the external property somehow has to be mapped onto the internal property. Looked into one of the link you have posted, I changed the my Graph and added an internal property. please have a look at the edited code above. I still get loads of error because of this : boost::property_map::type vertex_label = boost::get(boost::vertex_name, mGraph); – soupso Dec 15 '15 at 18:31
  • Hi again Sehe. You saved me !! One of your link has the key to my problem. I used a lambda using make_transform_value_property_map and that did work. I will update the above code now, so who ever needs it can use it. Thank you so much for your constant support. – soupso Dec 15 '15 at 19:04
  • Cheers! I think it's slightly confusing to edit the question with the solution (unless clearly marked as an update). Also, note how including the stuff in the lambda (`cell.GetProperty("Navigable", false);`) was crucial, no one could make that up for you, because the information just wasn't there. – sehe Dec 15 '15 at 22:05
  • Hi @sehe. I am here again for some problem with reading a DOT file. I have posted a separate question. http://bit.ly/1P7fT0u Please help me on this. Thank you so much ! – soupso Dec 16 '15 at 17:41