C++ Boost:使用read_graphml()访问特定于图形的属性
我试图从使用Boost图形库创建的.graphml文件中读取与图形相关的(自定义)属性。读取顶点和边(动态)属性可以工作,但我的图形属性始终为空。我也遇到过,但该解决方案只会生成空字符串(在下面的代码中)。除此之外,我找不到关于这个问题的很多信息 以下是缩短的代码(): 定义为C++ Boost:使用read_graphml()访问特定于图形的属性,c++,boost,graph,graphml,dynamic-properties,C++,Boost,Graph,Graphml,Dynamic Properties,我试图从使用Boost图形库创建的.graphml文件中读取与图形相关的(自定义)属性。读取顶点和边(动态)属性可以工作,但我的图形属性始终为空。我也遇到过,但该解决方案只会生成空字符串(在下面的代码中)。除此之外,我找不到关于这个问题的很多信息 以下是缩短的代码(): 定义为 <key attr.name="title" attr.type="string" for="graph" id="d1"> <default/> </key> 我上传了一个
<key attr.name="title" attr.type="string" for="graph" id="d1">
<default/>
</key>
我上传了一个(没有足够的代表发布img/更多详细信息)
次要的后续问题:是否可以加载图形而不“修复”导出的文件(参考代码)?解析器总是抱怨这样的行(不确定是否在GraphML标准中允许,该标准允许:“该组由两个可选属性组成-attr.name(提供数据函数的名称)-attr.type((声明数据函数的值范围)。”):
出现此错误时:
分析错误:无法识别密钥的类型“”
非常感谢您提供的任何帮助/想法。非常感谢!试试这个:
template<typename MutableGraph>
class mutate_graph_impl_yed : public mutate_graph_impl<MutableGraph>
{
public:
mutate_graph_impl_yed(MutableGraph& g, dynamic_properties& dp)
: mutate_graph_impl<MutableGraph>(g,dp) { }
virtual void
set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::vertex_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::vertex_descriptor>(vertex),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
virtual void
set_edge_property(const std::string& name, any edge, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::edge_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::edge_descriptor>(edge),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
};
模板
类mutate\u graph\u impl\ed:公共mutate\u graph\u impl
{
公众:
变异图(可变图&g、动态图&dp)
:mutate_graph_impl(g,dp){}
虚空
设置顶点属性(常量标准::字符串和名称、任意顶点、常量标准::字符串和值、常量标准::字符串和值类型)
{
bool type_found=false;
尝试
{
mpl::for_each(put_属性
(名称、m_dp、任意投射(顶点),
值、值类型、m类型(名称、类型(已找到));
}
捕捉(错误的投射)
{
BOOST\u THROW\u异常(
解析\u错误(“键的无效值\”+值+“\”+
名称+类型+值(类型));
}
}
虚空
set_edge_属性(const std::string&name、任意边、const std::string&value、const std::string&value_类型)
{
bool type_found=false;
尝试
{
mpl::for_each(put_属性
(名称、m_dp、任何铸造(边缘),
值、值类型、m类型(名称、类型(已找到));
}
捕捉(错误的投射)
{
BOOST\u THROW\u异常(
解析\u错误(“键的无效值\”+值+“\”+
名称+类型+值(类型));
}
}
};
将对read_graphml的调用替换为:
mutate_graph_impl_yed<Graph> mg(g, dp);
read_graphml(fin, mg, 0);
mutate\u graph\u impl\u-yed mg(g,dp);
读取图(翅片,毫克,0);
可能是相关的,因为这两个错误都处理空的CDATA解析结果。在标记中有CDATA部分的解决方法吗?谢谢你的回答!这一点很好(并且在我的代码中没有解决方法),但我认为它不相关,因为顶点和边属性被正确解析,并且它们也使用CDATA,例如,(对于顶点)
。为了进行测试,我还删除了图形属性(->foobar
)中的CDATA,但我仍然得到了空的图形属性。还有其他想法吗?
<key for="port" id="d2" yfiles.type="portgraphics"/>
template<typename MutableGraph>
class mutate_graph_impl_yed : public mutate_graph_impl<MutableGraph>
{
public:
mutate_graph_impl_yed(MutableGraph& g, dynamic_properties& dp)
: mutate_graph_impl<MutableGraph>(g,dp) { }
virtual void
set_vertex_property(const std::string& name, any vertex, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::vertex_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::vertex_descriptor>(vertex),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
virtual void
set_edge_property(const std::string& name, any edge, const std::string& value, const std::string& value_type)
{
bool type_found = false;
try
{
mpl::for_each<value_types>(put_property<graph_traits<MutableGraph>::edge_descriptor, value_types>
(name, m_dp, any_cast<graph_traits<MutableGraph>::edge_descriptor>(edge),
value, value_type, m_type_names, type_found));
}
catch (bad_lexical_cast)
{
BOOST_THROW_EXCEPTION(
parse_error("invalid value \"" + value + "\" for key " +
name + " of type " + value_type));
}
}
};
mutate_graph_impl_yed<Graph> mg(g, dp);
read_graphml(fin, mg, 0);