2

I have already asked a question on how to write dynamic properties to a DOT file. And its works fine. Now when I try to read the DOT file and build my Graph I am getting some exception. Basically I followed a similar way like writing, to read the DOT file. Below is what I have tried :

I have a Map class, that uses the Cell class as vertices and Cell class uses a separate CellProperty class for setting and getting all the Cell properties. Please refer to the linked question to have an idea about the whole class structure.

  boost::dynamic_properties mPropertiesR(boost::ignore_other_properties);
  mPropertiesR.property("ID", boost::get(boost::vertex_index, mGraph));

  auto valueNavigable = boost::make_transform_value_property_map(
  [](Cell &cell) { return cell.GetProperty<bool>("Navigable", false); }, boost::get(boost::vertex_bundle, mGraph));
  mPropertiesR.property("Navigable", valueNavigable);

  std::ifstream fin(mGraphFilePath.c_str());
  std::string const &node_id = "node_id";
  boost::read_graphviz(fin, mGraph, mPropertiesR, "node_id");

  boost::graph_traits<Graph>::vertex_iterator vi, vi_end, next;
  boost::tie(vi, vi_end) = boost::vertices(mGraph);
  for (next = vi; vi != vi_end; vi = next) {
     // cell.GetProperty() is a templated method the takes a default parameter, thus passing "false" bool parameter which returns the "Navigable" cell property
     std::cout<< ": The Navigable property set to  :" << mGraph[*next].GetProperty<bool>("Navigable", false);
     ++next;
  }

The code above compiles but I get an exception :

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::dynamic_const_put_error> >'
what():  Attempt to put a value into a const property map: 
Aborted (core dumped)

The GetProperty() method uses the CellProperty class to get the property values of each cell. Here is how the GetProperty() method looks like:

  template <class T>
  T GetProperty(std::string pPropertyName, T pDefaultValue = T()) {
     try {
        T propertyValue = boost::lexical_cast<T>( mCellProperty.GetPropertyString(pPropertyName, boost::lexical_cast<std::string>(pDefaultValue)));
        return propertyValue;
     } catch (...) {
        std::cout<< ":  Unknown exception thrown while getting cell property value :'" << pPropertyName << "'";
     }
     return pDefaultValue;
  }

What I want is to retrieve the vertex properties in the Graph, which I read from the DOT file, like the "Navigable" property and so on. I cant figure out, what exactly I am doing wrong. Please help me. Thanks in advance !

Community
  • 1
  • 1
soupso
  • 592
  • 1
  • 4
  • 20
  • 1
    If you read the documentation you'll see that you need to return a non-const reference from your GetProperty member function to make the property map writable(Technically the lambda is the one that must return the reference). – llonesmiz Dec 16 '15 at 20:09
  • Thanks @cv_and_he !! I see your point but I am not particularly sure what modifications should be done to my GetProperty() method. It is supposed to return a property value from using CellProperty object. I have edited the question and added the GetProperty() method above. Can you please give me some pointers may be as to what changes has to go to GetProperty() method. Thanks. – soupso Dec 17 '15 at 07:43
  • 1
    You keep omitting the relevant information. We could be much more to-the-point if you showed a minimal example of what you have, or at least the relevant bits. – sehe Dec 17 '15 at 10:31

1 Answers1

1

I am not particularly sure what modifications should be done to my GetProperty() method.

I've said this before:

Also, note how including the stuff in the lambda (cell.GetProperty<bool>("Navigable", false);) was crucial, no one could make that up for you, because the information just wasn't there.

We can't know how to write it because you don't show how the property could be accessed in the first place

The same is true here. I'll give you another pointer instead then.

You need to implement a model of ReadWritePropertyMap or LvaluePropertyMap for a mutable property map.

See this answer for an example of how to implement a read-write property map: look for property_traits<bidi_color_map> and the put and get functions:

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I think [this](http://coliru.stacked-crooked.com/a/86525a2a5c38c279) should work. I don't have time right now to write an answer. Feel free to write one yourself. If nobody does, I'll try tonight if I can. – llonesmiz Dec 17 '15 at 13:40
  • @cv_and_he Yup, that's it basically. I just flat out refuse to invent `Cell` like that. This has not been the first question that lacked that info, and I fear it won't be the last unless we teach the OP to post a valid question. – sehe Dec 17 '15 at 14:31
  • Thanks guys. I will try out and post the answer. Thanks for your efforts ! – soupso Dec 18 '15 at 09:37