【问题标题】:Exception while reading boost dynamic properties through boost::read_graphviz()通过 boost::read_graphviz() 读取 boost 动态属性时出现异常
【发布时间】:2015-12-16 14:25:16
【问题描述】:

我已经向question 询问了如何将动态属性写入 DOT 文件。它的工作正常。现在,当我尝试读取 DOT 文件并构建我的图表时,我遇到了一些异常。基本上我遵循类似写作的方式来读取 DOT 文件。以下是我尝试过的:

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

  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;
  }

上面的代码可以编译,但我得到一个异常:

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() 方法的样子:

  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 文件中读取的,例如“可导航”属性等。我无法弄清楚,我到底做错了什么。请帮我。提前致谢!

【问题讨论】:

  • 如果您阅读文档,您会发现您需要从 GetProperty 成员函数返回一个非常量引用以使属性映射可写(从技术上讲,lambda 是必须返回引用的那个)。
  • 谢谢@cv_and_he !!我明白你的意思,但我不确定应该对我的 GetProperty() 方法进行哪些修改。它应该使用 CellProperty 对象返回一个属性值。我已经编辑了问题并在上面添加了 GetProperty() 方法。您能否给我一些指示,可能是关于 GetProperty() 方法必须进行哪些更改。谢谢。
  • 您一直忽略相关信息。如果你展示了一个关于你所拥有的东西的最小例子,或者至少是相关的部分,我们可能会更中肯。

标签: c++ boost graph graphviz boost-graph


【解决方案1】:

我不太确定应该对我的 GetProperty() 方法进行哪些修改。

我有said this before:

另外,请注意在 lambda (cell.GetProperty&lt;bool&gt;("Navigable", false);) 中包含这些内容是多么重要,没有人可以为您弥补这一点,因为信息不存在。

我们不知道如何编写它,因为您一开始没有说明如何访问该属性

这里也是如此。那我再给你一个指针。

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

有关如何实现读写属性映射的示例,请参阅此答案:查找 property_traits&lt;bidi_color_map&gt; 以及 putget 函数:

【讨论】:

  • 我认为this 应该可以工作。我现在没有时间写答案。随意自己写一个。如果没有人这样做,如果可以的话,我今晚会尝试。
  • @cv_and_he 是的,基本上就是这样。我完全拒绝像那样发明Cell。这不是第一个缺少该信息的问题,我担心它不会是最后一个,除非我们教 OP 发布一个有效的问题。
  • 谢谢大家。我会尝试并发布答案。感谢您的努力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 2012-05-13
相关资源
最近更新 更多