关于C#:Exception,同时通过boost :: read_graphviz()读取boost动态属性

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

我已经问了一个有关如何将动态属性写入DOT文件的问题。和它的作品。现在,当我尝试读取DOT文件并构建我的图时,出现了一些异常。基本上,我按照类似的方式(如写入)读取DOT文件。以下是我尝试过的内容:

我有一个Map类,它使用Cell类作为顶点,而Cell类使用单独的CellProperty类来设置和获取所有Cell属性。请参考链接的问题以了解整个类的结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  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;
  }

上面的代码可以编译,但是出现异常:

1
2
3
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)

GetProperty()方法使用CellProperty类来获取每个单元格的属性值。这是GetProperty()方法的样子:

1
2
3
4
5
6
7
8
9
10
  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;
  }

我想要的是检索从DOT文件读取的图形中的顶点属性,例如" Navigable"属性等。我不知道我到底在做什么错。请帮我。在此先感谢!


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

我之前说过这个话:

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.

我们不知道如何编写它,因为您没有展示如何首先访问该属性

这里也是一样。我会给你另一个指针。

您需要为可变属性映射实现ReadWritePropertyMapLvaluePropertyMap模型。

有关如何实现读写属性映射的示例,请参见以下答案:查找property_traits<bidi_color_map>以及putget函数:

  • 切割图形集,Boost图形库