如何将GraphML文件写入具有自定义graph_bundle属性的Boost Graph?

huangapple go评论95阅读模式
英文:

How to write GraphML file to Boost Graph with custom graph_bundle Property?

问题

我无法编译以下代码,我想使用boost图形库的graph_bundle属性。

  1. struct VertexProps
  2. {
  3. std::string VertexName;
  4. };
  5. struct EdgeProps
  6. {
  7. std::string edgeName;
  8. };
  9. struct GraphProps
  10. {
  11. std::string name;
  12. };
  13. boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, GraphProps> g;
  14. g[boost::graph_bundle].name = "myGraphName";
  15. boost::dynamic_properties dp;
  16. dp.property("name", get(&GraphProps::name, g)); // 在这一行出现编译错误
  17. std::ofstream xmlFile("output.graphml");
  18. boost::write_graphml(xmlFile, g, dp, true);
  19. xmlFile.close();

我遇到以下编译错误:

  1. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2646:29: 错误:形成对void的引用
  2. 2646 | typedef value_type&amp; reference;
  3. | ^~~~~~~~~
  4. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2647:35: 错误:形成对void的引用
  5. 2647 | typedef const value_type&amp; const_reference;
  6. | ^~~~~~~~~~~~~~~
  7. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2651:13: 错误:形成对void的引用
  8. 2651 | type;
  9. | ^~~~
  10. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2654:13: 错误:形成对void的引用
  11. 2654 | const_type;
  12. | ^~~~~~~~~~
  13. test.cpp: 在函数‘void writeGraph()’中:
  14. test.cpp:310:49: 错误:没有匹配的函数调用‘get(std::string GraphProps::*, Graph&)
  15. 310 | dp.property("name", get(&GraphProps::name, g));
  16. | ^

似乎boost图形库不支持自定义属性名,可以使用dynamic_properties来将其写入graphml

英文:

I am not able to compile below , where I want to use graph_bundle property of boost graph.

  1. struct VertexProps
  2. {
  3. std::string VertexName;
  4. };
  5. struct EdgeProps
  6. {
  7. std::string edgeName;
  8. };
  9. struct GraphProps
  10. {
  11. std::string name;
  12. };
  13. boost::adjacency_list&lt;boost::vecS, boost::vecS, boost::bidirectionalS, VertexProps, EdgeProps, GraphProps&gt; g;
  14. g[boost::graph_bundle].name = &quot;myGraphName&quot;;
  15. boost::dynamic_properties dp;
  16. dp.property(&quot;name&quot;, get(&amp;GraphProps::name, g)); //compile error on this line
  17. std::ofstream xmlFile(&quot;output.graphml&quot;);
  18. boost::write_graphml(xmlFile, g, dp, true);
  19. xmlFile.close();

I am getting beloe compile error:

  1. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2646:29: error: forming reference to void
  2. 2646 | typedef value_type&amp; reference;
  3. | ^~~~~~~~~
  4. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2647:35: error: forming reference to void
  5. 2647 | typedef const value_type&amp; const_reference;
  6. | ^~~~~~~~~~~~~~~
  7. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2651:13: error: forming reference to void
  8. 2651 | type;
  9. | ^~~~
  10. &lt;&gt;/boost/graph/detail/adjacency_list.hpp:2654:13: error: forming reference to void
  11. 2654 | const_type;
  12. | ^~~~~~~~~~
  13. test.cpp: In function void writeGraph()’:
  14. test.cpp:310:49: error: no matching function for call to get(std::string GraphProps::*, Graph&amp;)’
  15. 310 | dp.property(&quot;name&quot;, get(&amp;GraphProps::name, g));
  16. | ^

It seems , boost graph library can't support custom property name , which can be used dynamic_properties for writing in graphml.

答案1

得分: 1

图属性在属性映射中文档不够详细。公平地说,它们的用途也有限。

您需要创建一个关联属性映射,键类型为 G*(其中 G 表示您的图类型)。

最简单的方法是使用 make_constant_property_map

  1. dp.property("name", boost::make_constant_property<G*>(get_property(g).name));

请参阅 Live On Coliru

  1. #include <boost/graph/adjacency_list.hpp>
  2. #include <boost/graph/graphml.hpp>
  3. struct VertexProps { std::string VertexName; };
  4. struct EdgeProps { std::string edgeName; };
  5. struct GraphProps { std::string name; };
  6. int main() {
  7. using G = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProps,
  8. EdgeProps, GraphProps>;
  9. G g;
  10. auto& gp = get_property(g);
  11. g[boost::graph_bundle].name = "myGraphName"; // or gp.name = "myGraphName"
  12. boost::dynamic_properties dp;
  13. dp.property("name", boost::make_constant_property<G*>(get_property(g).name));
  14. boost::write_graphml(std::cout, g, dp, true);
  15. }

打印

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  3. <key id="key0" for="graph" attr.name="name" attr.type="string" />
  4. <graph id="G" edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
  5. <data key="key0">myGraphName</data>
  6. </graph>
  7. </graphml>
英文:

Graph properties in property maps are poorly documented. In fairness, they're also of limited use.

You need to make an associative property map with key type G* (where G denotes your graph type).

The simplest way would be to use make_constant_property_map:

  1. dp.property(&quot;name&quot;, boost::make_constant_property&lt;G*&gt;(get_property(g).name));

See it Live On Coliru

  1. #include &lt;boost/graph/adjacency_list.hpp&gt;
  2. #include &lt;boost/graph/graphml.hpp&gt;
  3. struct VertexProps { std::string VertexName; };
  4. struct EdgeProps { std::string edgeName; };
  5. struct GraphProps { std::string name; };
  6. int main() {
  7. using G = boost::adjacency_list&lt;boost::vecS, boost::vecS, boost::bidirectionalS, VertexProps,
  8. EdgeProps, GraphProps&gt;;
  9. G g;
  10. auto&amp; gp = get_property(g);
  11. g[boost::graph_bundle].name = &quot;myGraphName&quot;; // or gp.name = &quot;myGraphName&quot;
  12. boost::dynamic_properties dp;
  13. dp.property(&quot;name&quot;, boost::make_constant_property&lt;G*&gt;(get_property(g).name));
  14. boost::write_graphml(std::cout, g, dp, true);
  15. }

Printing

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;graphml xmlns=&quot;http://graphml.graphdrawing.org/xmlns&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd&quot;&gt;
  3. &lt;key id=&quot;key0&quot; for=&quot;graph&quot; attr.name=&quot;name&quot; attr.type=&quot;string&quot; /&gt;
  4. &lt;graph id=&quot;G&quot; edgedefault=&quot;directed&quot; parse.nodeids=&quot;canonical&quot; parse.edgeids=&quot;canonical&quot; parse.order=&quot;nodesfirst&quot;&gt;
  5. &lt;data key=&quot;key0&quot;&gt;myGraphName&lt;/data&gt;
  6. &lt;/graph&gt;
  7. &lt;/graphml&gt;

huangapple
  • 本文由 发表于 2023年2月6日 15:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定