soupso
soupso

Reputation: 642

Exception while reading boost dynamic properties through boost::read_graphviz()

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 !

Upvotes: 2

Views: 412

Answers (1)

sehe
sehe

Reputation: 393829

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:

Upvotes: 1

Related Questions