【发布时间】: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