write_graphml.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ============================
  2. |(logo)|__ ``write_graphml``
  3. ============================
  4. .. Copyright (C) 2006 Tiago de Paula Peixoto <tiago@forked.de>
  5. Distributed under the Boost Software License, Version 1.0. (See
  6. accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. Authors: Tiago de Paula Peixoto
  9. .. |(logo)| image:: ../../../boost.png
  10. :align: middle
  11. :alt: Boost
  12. __ ../../../index.htm
  13. ::
  14. template<typename Graph>
  15. void
  16. write_graphml(std::ostream& out, const Graph& g, const dynamic_properties& dp,
  17. bool ordered_vertices=false);
  18. template<typename Graph, typename VertexIndexMap>
  19. void
  20. write_graphml(std::ostream& out, const Graph& g, VertexIndexMap vertex_index,
  21. const dynamic_properties& dp, bool ordered_vertices=false);
  22. This is to write a BGL graph object into an output stream in the
  23. GraphML_ format. Both overloads of ``write_graphml`` will emit all of
  24. the properties stored in the dynamic_properties_ object, thereby
  25. retaining the properties that have been read in through the dual
  26. function read_graphml_. The second overload must be used when the
  27. graph doesn't have an internal vertex index map, which must then be
  28. supplied with the appropriate parameter.
  29. .. contents::
  30. Where Defined
  31. -------------
  32. ``<boost/graph/graphml.hpp>``
  33. Parameters
  34. ----------
  35. OUT: ``std::ostream& out``
  36. A standard ``std::ostream`` object.
  37. IN: ``VertexListGraph& g``
  38. A directed or undirected graph. The
  39. graph's type must be a model of VertexListGraph_. If the graph
  40. doesn't have an internal ``vertex_index`` property map, one
  41. must be supplied with the vertex_index parameter.
  42. IN: ``VertexIndexMap vertex_index``
  43. A vertex property map containing the indexes in the range
  44. [0,num_vertices(g)].
  45. IN: ``dynamic_properties& dp``
  46. Contains all of the vertex, edge, and graph properties that should be
  47. emitted by the GraphML writer.
  48. IN: ``bool ordered_vertices``
  49. This tells whether or not the order of the vertices from vertices(g)
  50. matches the order of the indexes. If ``true``, the ``parse.nodeids``
  51. graph attribute will be set to ``canonical``. Otherwise it will be
  52. set to ``free``.
  53. Example
  54. -------
  55. This example demonstrates using BGL-GraphML interface to write
  56. a BGL graph into a GraphML format file.
  57. ::
  58. enum files_e { dax_h, yow_h, boz_h, zow_h, foo_cpp,
  59. foo_o, bar_cpp, bar_o, libfoobar_a,
  60. zig_cpp, zig_o, zag_cpp, zag_o,
  61. libzigzag_a, killerapp, N };
  62. const char* name[] = { "dax.h", "yow.h", "boz.h", "zow.h", "foo.cpp",
  63. "foo.o", "bar.cpp", "bar.o", "libfoobar.a",
  64. "zig.cpp", "zig.o", "zag.cpp", "zag.o",
  65. "libzigzag.a", "killerapp" };
  66. int main(int,char*[])
  67. {
  68. typedef pair<int,int> Edge;
  69. Edge used_by[] = {
  70. Edge(dax_h, foo_cpp), Edge(dax_h, bar_cpp), Edge(dax_h, yow_h),
  71. Edge(yow_h, bar_cpp), Edge(yow_h, zag_cpp),
  72. Edge(boz_h, bar_cpp), Edge(boz_h, zig_cpp), Edge(boz_h, zag_cpp),
  73. Edge(zow_h, foo_cpp),
  74. Edge(foo_cpp, foo_o),
  75. Edge(foo_o, libfoobar_a),
  76. Edge(bar_cpp, bar_o),
  77. Edge(bar_o, libfoobar_a),
  78. Edge(libfoobar_a, libzigzag_a),
  79. Edge(zig_cpp, zig_o),
  80. Edge(zig_o, libzigzag_a),
  81. Edge(zag_cpp, zag_o),
  82. Edge(zag_o, libzigzag_a),
  83. Edge(libzigzag_a, killerapp)
  84. };
  85. const int nedges = sizeof(used_by)/sizeof(Edge);
  86. typedef adjacency_list< vecS, vecS, directedS,
  87. property< vertex_color_t, string >,
  88. property< edge_weight_t, int >
  89. > Graph;
  90. Graph g(used_by, used_by + nedges, N);
  91. graph_traits<Graph>::vertex_iterator v, v_end;
  92. for (tie(v,v_end) = vertices(g); v != v_end; ++v)
  93. put(vertex_color_t(), g, *v, name[*v]);
  94. graph_traits<Graph>::edge_iterator e, e_end;
  95. for (tie(e,e_end) = edges(g); e != e_end; ++e)
  96. put(edge_weight_t(), g, *e, 3);
  97. dynamic_properties dp;
  98. dp.property("name", get(vertex_color_t(), g));
  99. dp.property("weight", get(edge_weight_t(), g));
  100. write_graphml(std::cout, g, dp, true);
  101. }
  102. The output will be:
  103. ::
  104. <?xml version="1.0" encoding="UTF-8"?>
  105. <graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml http://graphml.graphdrawing.org/xmlns/graphml/graphml-attributes-1.0rc.xsd">
  106. <key id="key0" for="node" attr.name="name" attr.type="string" />
  107. <key id="key1" for="edge" attr.name="weight" attr.type="int" />
  108. <graph id="G" edgedefault="directed" parse.nodeids="canonical" parse.edgeids="canonical" parse.order="nodesfirst">
  109. <node id="n0">
  110. <data key="key0">dax.h</data>
  111. </node>
  112. <node id="n1">
  113. <data key="key0">yow.h</data>
  114. </node>
  115. <node id="n2">
  116. <data key="key0">boz.h</data>
  117. </node>
  118. <node id="n3">
  119. <data key="key0">zow.h</data>
  120. </node>
  121. <node id="n4">
  122. <data key="key0">foo.cpp</data>
  123. </node>
  124. <node id="n5">
  125. <data key="key0">foo.o</data>
  126. </node>
  127. <node id="n6">
  128. <data key="key0">bar.cpp</data>
  129. </node>
  130. <node id="n7">
  131. <data key="key0">bar.o</data>
  132. </node>
  133. <node id="n8">
  134. <data key="key0">libfoobar.a</data>
  135. </node>
  136. <node id="n9">
  137. <data key="key0">zig.cpp</data>
  138. </node>
  139. <node id="n10">
  140. <data key="key0">zig.o</data>
  141. </node>
  142. <node id="n11">
  143. <data key="key0">zag.cpp</data>
  144. </node>
  145. <node id="n12">
  146. <data key="key0">zag.o</data>
  147. </node>
  148. <node id="n13">
  149. <data key="key0">libzigzag.a</data>
  150. </node>
  151. <node id="n14">
  152. <data key="key0">killerapp</data>
  153. </node>
  154. <edge id="e0" source="n0" target="n4">
  155. <data key="key1">3</data>
  156. </edge>
  157. <edge id="e1" source="n0" target="n6">
  158. <data key="key1">3</data>
  159. </edge>
  160. <edge id="e2" source="n0" target="n1">
  161. <data key="key1">3</data>
  162. </edge>
  163. <edge id="e3" source="n1" target="n6">
  164. <data key="key1">3</data>
  165. </edge>
  166. <edge id="e4" source="n1" target="n11">
  167. <data key="key1">3</data>
  168. </edge>
  169. <edge id="e5" source="n2" target="n6">
  170. <data key="key1">3</data>
  171. </edge>
  172. <edge id="e6" source="n2" target="n9">
  173. <data key="key1">3</data>
  174. </edge>
  175. <edge id="e7" source="n2" target="n11">
  176. <data key="key1">3</data>
  177. </edge>
  178. <edge id="e8" source="n3" target="n4">
  179. <data key="key1">3</data>
  180. </edge>
  181. <edge id="e9" source="n4" target="n5">
  182. <data key="key1">3</data>
  183. </edge>
  184. <edge id="e10" source="n5" target="n8">
  185. <data key="key1">3</data>
  186. </edge>
  187. <edge id="e11" source="n6" target="n7">
  188. <data key="key1">3</data>
  189. </edge>
  190. <edge id="e12" source="n7" target="n8">
  191. <data key="key1">3</data>
  192. </edge>
  193. <edge id="e13" source="n8" target="n13">
  194. <data key="key1">3</data>
  195. </edge>
  196. <edge id="e14" source="n9" target="n10">
  197. <data key="key1">3</data>
  198. </edge>
  199. <edge id="e15" source="n10" target="n13">
  200. <data key="key1">3</data>
  201. </edge>
  202. <edge id="e16" source="n11" target="n12">
  203. <data key="key1">3</data>
  204. </edge>
  205. <edge id="e17" source="n12" target="n13">
  206. <data key="key1">3</data>
  207. </edge>
  208. <edge id="e18" source="n13" target="n14">
  209. <data key="key1">3</data>
  210. </edge>
  211. </graph>
  212. </graphml>
  213. See Also
  214. --------
  215. _read_graphml
  216. Notes
  217. -----
  218. - Note that you can use GraphML file write facilities without linking
  219. against the ``boost_graph`` library.
  220. .. _GraphML: http://graphml.graphdrawing.org/
  221. .. _dynamic_properties: ../../property_map/doc/dynamic_property_map.html
  222. .. _read_graphml: read_graphml.html
  223. .. _VertexListGraph: VertexListGraph.html