read_graphviz.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ============================
  2. |(logo)|__ ``read_graphviz``
  3. ============================
  4. .. |(logo)| image:: ../../../boost.png
  5. :align: middle
  6. :alt: Boost
  7. .. Copyright (c) 2005-2009 Trustees of Indiana University
  8. Distributed under the Boost Software License, Version 1.0.
  9. (See accompanying file LICENSE_1_0.txt or copy at
  10. http://www.boost.org/LICENSE_1_0.txt)
  11. __ ../../../index.htm
  12. ::
  13. namespace boost {
  14. template <typename MutableGraph>
  15. bool read_graphviz(std::istream& in, MutableGraph& graph,
  16. dynamic_properties& dp,
  17. const std::string& node_id = "node_id");
  18. template <typename MutableGraph>
  19. bool read_graphviz(std::string& str, MutableGraph& graph,
  20. dynamic_properties& dp,
  21. const std::string& node_id = "node_id");
  22. template <typename InputIterator, typename MutableGraph>
  23. bool read_graphviz(InputIterator begin, InputIterator end,
  24. MutableGraph& graph, dynamic_properties& dp,
  25. const std::string& node_id = "node_id");
  26. }
  27. The ``read_graphviz`` function interprets a graph described using the
  28. GraphViz_ DOT language and builds a BGL graph that captures that
  29. description. Using these functions, you can initialize a graph using
  30. data stored as text.
  31. The DOT language can specify both directed and undirected graphs, and
  32. ``read_graphviz`` differentiates between the two. One must pass
  33. ``read_graphviz`` an undirected graph when reading an undirected graph;
  34. the same is true for directed graphs. Furthermore, ``read_graphviz``
  35. will throw an exception if it encounters parallel edges and cannot add
  36. them to the graph.
  37. To handle properties expressed in the DOT language, ``read_graphviz``
  38. takes a dynamic_properties_ object and operates on its collection of
  39. property maps. The reader passes all the properties encountered to
  40. this object, using the GraphViz string keys as the property keys.
  41. Furthermore, ``read_graphviz`` stores node identifier names under the
  42. vertex property map named ``node_id``.
  43. Requirements:
  44. - The type of the graph must model the `Mutable Graph`_ concept.
  45. - The type of the iterator must model the `Input Iterator`_
  46. concept.
  47. - The property map value types must be default-constructible.
  48. .. contents::
  49. Where Defined
  50. -------------
  51. ``<boost/graph/graphviz.hpp>``
  52. Exceptions
  53. ----------
  54. ::
  55. struct graph_exception : public std::exception {
  56. virtual ~graph_exception() throw();
  57. virtual const char* what() const throw() = 0;
  58. };
  59. struct bad_parallel_edge : public graph_exception {
  60. std::string from;
  61. std::string to;
  62. bad_parallel_edge(const std::string&, const std::string&);
  63. virtual ~bad_parallel_edge() throw();
  64. const char* what() const throw();
  65. };
  66. struct directed_graph_error : public graph_exception {
  67. virtual ~directed_graph_error() throw();
  68. virtual const char* what() const throw();
  69. };
  70. struct undirected_graph_error : public graph_exception {
  71. virtual ~undirected_graph_error() throw();
  72. virtual const char* what() const throw();
  73. };
  74. struct bad_graphviz_syntax: public graph_exception {
  75. std::string errmsg;
  76. bad_graphviz_syntax(const std::string&);
  77. virtual ~bad_graphviz_syntax() throw();
  78. virtual const char* what() const throw();
  79. };
  80. Under certain circumstances, ``read_graphviz`` will throw one of the
  81. above exceptions. The three concrete exceptions can all be caught
  82. using the general ``graph_exception`` moniker when greater precision
  83. is not needed. In addition, all of the above exceptions derive from
  84. the standard ``std::exception`` for even more generalized error
  85. handling.
  86. The ``bad_parallel_edge`` exception is thrown when an attempt to add a
  87. parallel edge to the supplied MutableGraph fails. The DOT language
  88. supports parallel edges, but some BGL-compatible graph types do not.
  89. One example of such a graph is ``boost::adjacency_list<setS,vecS>``,
  90. which allows at most one edge can between any two vertices.
  91. The ``directed_graph_error`` exception occurs when an undirected graph
  92. type is passed to ``read_graph`` but the textual representation of the
  93. graph is directed, as indicated by the ``digraph`` keyword in the DOT
  94. language.
  95. The ``undirected_graph_error`` exception occurs when a directed graph
  96. type is passed to ``read_graph`` but the textual representation of the
  97. graph is undirected, as indicated by the ``graph`` keyword in the DOT
  98. language.
  99. The ``bad_graphviz_syntax`` exception occurs when the graph input is not a
  100. valid GraphViz graph.
  101. Example
  102. -------
  103. The following example illustrates a relatively simple use of the
  104. GraphViz reader to populate an ``adjacency_list`` graph
  105. ::
  106. // Vertex properties
  107. typedef property < vertex_name_t, std::string,
  108. property < vertex_color_t, float > > vertex_p;
  109. // Edge properties
  110. typedef property < edge_weight_t, double > edge_p;
  111. // Graph properties
  112. typedef property < graph_name_t, std::string > graph_p;
  113. // adjacency_list-based type
  114. typedef adjacency_list < vecS, vecS, directedS,
  115. vertex_p, edge_p, graph_p > graph_t;
  116. // Construct an empty graph and prepare the dynamic_property_maps.
  117. graph_t graph(0);
  118. dynamic_properties dp;
  119. property_map<graph_t, vertex_name_t>::type name =
  120. get(vertex_name, graph);
  121. dp.property("node_id",name);
  122. property_map<graph_t, vertex_color_t>::type mass =
  123. get(vertex_color, graph);
  124. dp.property("mass",mass);
  125. property_map<graph_t, edge_weight_t>::type weight =
  126. get(edge_weight, graph);
  127. dp.property("weight",weight);
  128. // Use ref_property_map to turn a graph property into a property map
  129. boost::ref_property_map<graph_t*,std::string>
  130. gname(get_property(graph,graph_name));
  131. dp.property("name",gname);
  132. // Sample graph as an std::istream;
  133. std::istringstream
  134. gvgraph("digraph { graph [name=\"graphname\"] a c e [mass = 6.66] }");
  135. bool status = read_graphviz(gvgraph,graph,dp,"node_id");
  136. Building the GraphViz Readers
  137. -----------------------------
  138. To use the GraphViz readers, you will need to build and link against
  139. the "boost_graph" and "boost_regex" libraries. These libraries can be built by following the
  140. `Boost Jam Build Instructions`_ for the subdirectories ``libs/graph/build`` and ``libs/regex/build``.
  141. Notes
  142. -----
  143. - The ``read_graphviz`` function does not use any code from the
  144. GraphViz distribution to interpret the DOT Language. Rather, the
  145. implementation was based on documentation found on the GraphViz web
  146. site, as well as experiments run using the dot application. The
  147. resulting interpretation may be subtly different from dot for some
  148. corner cases that are not well specified.
  149. - On successful reading of a graph, every vertex and edge will have
  150. an associated value for every respective edge and vertex property
  151. encountered while interpreting the graph. These values will be set
  152. using the ``dynamic_properties`` object. Those edges and
  153. vertices that are not explicitly given a value for a property (and that
  154. property has no default) will be
  155. given the default constructed value of the value type. **Be sure
  156. that property map value types are default constructible.**
  157. - ``read_graphviz`` treats subgraphs as syntactic sugar. It does not
  158. reflect subgraphs as actual entities in the BGL. Rather, they are
  159. used to shorten some edge definitions as well as to give a subset
  160. of all nodes or edges certain properties. For example, the
  161. DOT graphs ``digraph { a -> subgraph {b -> c} -> e }`` and
  162. ``digraph { a -> b -> e ; a -> c -> e ; b -> c}`` are equivalent.
  163. - Subgraph IDs refer to subgraphs defined earlier in the graph
  164. description. Undefined subgraphs behave as empty subgraphs
  165. (``{}``). This is the same behavior as GraphViz.
  166. See Also
  167. --------
  168. write_graphviz_
  169. Future Work
  170. -----------
  171. - Passing port information to BGL.
  172. - Expanding escape codes in the same way GraphViz does.
  173. - Support for optional recognition of subgraphs as distinct entities.
  174. .. _GraphViz: http://graphviz.org/
  175. .. _`Mutable Graph`: MutableGraph.html
  176. .. _`Input Iterator`: http://www.boost.org/sgi/stl/InputIterator.html
  177. .. _dynamic_properties: ../../property_map/doc/dynamic_property_map.html
  178. .. _write_graphviz: write-graphviz.html
  179. .. _Boost Jam Build Instructions: ../../../more/getting_started.html#Build_Install