read_graphviz.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
  7. <title>Boost read_graphviz</title>
  8. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  9. </head>
  10. <body>
  11. <div class="document" id="logo-read-graphviz">
  12. <h1 class="title"><a class="reference external" href="../../../index.htm"><img align="middle" alt="Boost" class="align-middle" src="../../../boost.png" /></a> <tt class="docutils literal"><span class="pre">read_graphviz</span></tt></h1>
  13. <!-- Copyright (c) 2005-2009 Trustees of Indiana University
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file LICENSE_1_0.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt) -->
  17. <pre class="literal-block">
  18. namespace boost {
  19. template &lt;typename MutableGraph&gt;
  20. bool read_graphviz(std::istream&amp; in, MutableGraph&amp; graph,
  21. dynamic_properties&amp; dp,
  22. const std::string&amp; node_id = &quot;node_id&quot;);
  23. template &lt;typename MutableGraph&gt;
  24. bool read_graphviz(std::string&amp; str, MutableGraph&amp; graph,
  25. dynamic_properties&amp; dp,
  26. const std::string&amp; node_id = &quot;node_id&quot;);
  27. template &lt;typename InputIterator, typename MutableGraph&gt;
  28. bool read_graphviz(InputIterator begin, InputIterator end,
  29. MutableGraph&amp; graph, dynamic_properties&amp; dp,
  30. const std::string&amp; node_id = &quot;node_id&quot;);
  31. }
  32. </pre>
  33. <p>The <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> function interprets a graph described using the
  34. <a class="reference external" href="http://graphviz.org/">GraphViz</a> DOT language and builds a BGL graph that captures that
  35. description. Using these functions, you can initialize a graph using
  36. data stored as text.</p>
  37. <p>The DOT language can specify both directed and undirected graphs, and
  38. <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> differentiates between the two. One must pass
  39. <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> an undirected graph when reading an undirected graph;
  40. the same is true for directed graphs. Furthermore, <tt class="docutils literal"><span class="pre">read_graphviz</span></tt>
  41. will throw an exception if it encounters parallel edges and cannot add
  42. them to the graph.</p>
  43. <p>To handle properties expressed in the DOT language, <tt class="docutils literal"><span class="pre">read_graphviz</span></tt>
  44. takes a <a class="reference external" href="../../property_map/doc/dynamic_property_map.html">dynamic_properties</a> object and operates on its collection of
  45. property maps. The reader passes all the properties encountered to
  46. this object, using the GraphViz string keys as the property keys.
  47. Furthermore, <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> stores node identifier names under the
  48. vertex property map named <tt class="docutils literal"><span class="pre">node_id</span></tt>.</p>
  49. <dl class="docutils">
  50. <dt>Requirements:</dt>
  51. <dd><ul class="first last simple">
  52. <li>The type of the graph must model the <a class="reference external" href="MutableGraph.html">Mutable Graph</a> concept.</li>
  53. <li>The type of the iterator must model the <a class="reference external" href="http://www.boost.org/sgi/stl/InputIterator.html">Input Iterator</a>
  54. concept.</li>
  55. <li>The property map value types must be default-constructible.</li>
  56. </ul>
  57. </dd>
  58. </dl>
  59. <div class="contents topic" id="contents">
  60. <p class="topic-title first">Contents</p>
  61. <ul class="simple">
  62. <li><a class="reference internal" href="#where-defined" id="id2">Where Defined</a></li>
  63. <li><a class="reference internal" href="#exceptions" id="id3">Exceptions</a></li>
  64. <li><a class="reference internal" href="#example" id="id4">Example</a></li>
  65. <li><a class="reference internal" href="#building-the-graphviz-readers" id="id5">Building the GraphViz Readers</a></li>
  66. <li><a class="reference internal" href="#notes" id="id6">Notes</a></li>
  67. <li><a class="reference internal" href="#see-also" id="id7">See Also</a></li>
  68. <li><a class="reference internal" href="#future-work" id="id8">Future Work</a></li>
  69. </ul>
  70. </div>
  71. <div class="section" id="where-defined">
  72. <h1><a class="toc-backref" href="#id2">Where Defined</a></h1>
  73. <p><tt class="docutils literal"><span class="pre">&lt;boost/graph/graphviz.hpp&gt;</span></tt></p>
  74. </div>
  75. <div class="section" id="exceptions">
  76. <h1><a class="toc-backref" href="#id3">Exceptions</a></h1>
  77. <pre class="literal-block">
  78. struct graph_exception : public std::exception {
  79. virtual ~graph_exception() throw();
  80. virtual const char* what() const throw() = 0;
  81. };
  82. struct bad_parallel_edge : public graph_exception {
  83. std::string from;
  84. std::string to;
  85. bad_parallel_edge(const std::string&amp;, const std::string&amp;);
  86. virtual ~bad_parallel_edge() throw();
  87. const char* what() const throw();
  88. };
  89. struct directed_graph_error : public graph_exception {
  90. virtual ~directed_graph_error() throw();
  91. virtual const char* what() const throw();
  92. };
  93. struct undirected_graph_error : public graph_exception {
  94. virtual ~undirected_graph_error() throw();
  95. virtual const char* what() const throw();
  96. };
  97. struct bad_graphviz_syntax: public graph_exception {
  98. std::string errmsg;
  99. bad_graphviz_syntax(const std::string&amp;);
  100. virtual ~bad_graphviz_syntax() throw();
  101. virtual const char* what() const throw();
  102. };
  103. </pre>
  104. <p>Under certain circumstances, <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> will throw one of the
  105. above exceptions. The three concrete exceptions can all be caught
  106. using the general <tt class="docutils literal"><span class="pre">graph_exception</span></tt> moniker when greater precision
  107. is not needed. In addition, all of the above exceptions derive from
  108. the standard <tt class="docutils literal"><span class="pre">std::exception</span></tt> for even more generalized error
  109. handling.</p>
  110. <p>The <tt class="docutils literal"><span class="pre">bad_parallel_edge</span></tt> exception is thrown when an attempt to add a
  111. parallel edge to the supplied MutableGraph fails. The DOT language
  112. supports parallel edges, but some BGL-compatible graph types do not.
  113. One example of such a graph is <tt class="docutils literal"><span class="pre">boost::adjacency_list&lt;setS,vecS&gt;</span></tt>,
  114. which allows at most one edge can between any two vertices.</p>
  115. <p>The <tt class="docutils literal"><span class="pre">directed_graph_error</span></tt> exception occurs when an undirected graph
  116. type is passed to <tt class="docutils literal"><span class="pre">read_graph</span></tt> but the textual representation of the
  117. graph is directed, as indicated by the <tt class="docutils literal"><span class="pre">digraph</span></tt> keyword in the DOT
  118. language.</p>
  119. <p>The <tt class="docutils literal"><span class="pre">undirected_graph_error</span></tt> exception occurs when a directed graph
  120. type is passed to <tt class="docutils literal"><span class="pre">read_graph</span></tt> but the textual representation of the
  121. graph is undirected, as indicated by the <tt class="docutils literal"><span class="pre">graph</span></tt> keyword in the DOT
  122. language.</p>
  123. <p>The <tt class="docutils literal"><span class="pre">bad_graphviz_syntax</span></tt> exception occurs when the graph input is not a
  124. valid GraphViz graph.</p>
  125. </div>
  126. <div class="section" id="example">
  127. <h1><a class="toc-backref" href="#id4">Example</a></h1>
  128. <p>The following example illustrates a relatively simple use of the
  129. GraphViz reader to populate an <tt class="docutils literal"><span class="pre">adjacency_list</span></tt> graph</p>
  130. <pre class="literal-block">
  131. // Vertex properties
  132. typedef property &lt; vertex_name_t, std::string,
  133. property &lt; vertex_color_t, float &gt; &gt; vertex_p;
  134. // Edge properties
  135. typedef property &lt; edge_weight_t, double &gt; edge_p;
  136. // Graph properties
  137. typedef property &lt; graph_name_t, std::string &gt; graph_p;
  138. // adjacency_list-based type
  139. typedef adjacency_list &lt; vecS, vecS, directedS,
  140. vertex_p, edge_p, graph_p &gt; graph_t;
  141. // Construct an empty graph and prepare the dynamic_property_maps.
  142. graph_t graph(0);
  143. dynamic_properties dp;
  144. property_map&lt;graph_t, vertex_name_t&gt;::type name =
  145. get(vertex_name, graph);
  146. dp.property(&quot;node_id&quot;,name);
  147. property_map&lt;graph_t, vertex_color_t&gt;::type mass =
  148. get(vertex_color, graph);
  149. dp.property(&quot;mass&quot;,mass);
  150. property_map&lt;graph_t, edge_weight_t&gt;::type weight =
  151. get(edge_weight, graph);
  152. dp.property(&quot;weight&quot;,weight);
  153. // Use ref_property_map to turn a graph property into a property map
  154. boost::ref_property_map&lt;graph_t*,std::string&gt;
  155. gname(get_property(graph,graph_name));
  156. dp.property(&quot;name&quot;,gname);
  157. // Sample graph as an std::istream;
  158. std::istringstream
  159. gvgraph(&quot;digraph { graph [name=\&quot;graphname\&quot;] a c e [mass = 6.66] }&quot;);
  160. bool status = read_graphviz(gvgraph,graph,dp,&quot;node_id&quot;);
  161. </pre>
  162. </div>
  163. <div class="section" id="building-the-graphviz-readers">
  164. <h1><a class="toc-backref" href="#id5">Building the GraphViz Readers</a></h1>
  165. <p>To use the GraphViz readers, you will need to build and link against
  166. the &quot;boost_graph&quot; and &quot;boost_regex&quot; libraries. These libraries can be built by following the
  167. <a class="reference external" href="../../../more/getting_started.html#Build_Install">Boost Jam Build Instructions</a> for the subdirectories <tt class="docutils literal"><span class="pre">libs/graph/build</span></tt> and <tt class="docutils literal"><span class="pre">libs/regex/build</span></tt>.</p>
  168. </div>
  169. <div class="section" id="notes">
  170. <h1><a class="toc-backref" href="#id6">Notes</a></h1>
  171. <blockquote>
  172. <ul class="simple">
  173. <li>The <tt class="docutils literal"><span class="pre">read_graphviz</span></tt> function does not use any code from the
  174. GraphViz distribution to interpret the DOT Language. Rather, the
  175. implementation was based on documentation found on the GraphViz web
  176. site, as well as experiments run using the dot application. The
  177. resulting interpretation may be subtly different from dot for some
  178. corner cases that are not well specified.</li>
  179. <li>On successful reading of a graph, every vertex and edge will have
  180. an associated value for every respective edge and vertex property
  181. encountered while interpreting the graph. These values will be set
  182. using the <tt class="docutils literal"><span class="pre">dynamic_properties</span></tt> object. Those edges and
  183. vertices that are not explicitly given a value for a property (and that
  184. property has no default) will be
  185. given the default constructed value of the value type. <strong>Be sure
  186. that property map value types are default constructible.</strong></li>
  187. <li><tt class="docutils literal"><span class="pre">read_graphviz</span></tt> treats subgraphs as syntactic sugar. It does not
  188. reflect subgraphs as actual entities in the BGL. Rather, they are
  189. used to shorten some edge definitions as well as to give a subset
  190. of all nodes or edges certain properties. For example, the
  191. DOT graphs <tt class="docutils literal"><span class="pre">digraph</span> <span class="pre">{</span> <span class="pre">a</span> <span class="pre">-&gt;</span> <span class="pre">subgraph</span> <span class="pre">{b</span> <span class="pre">-&gt;</span> <span class="pre">c}</span> <span class="pre">-&gt;</span> <span class="pre">e</span> <span class="pre">}</span></tt> and
  192. <tt class="docutils literal"><span class="pre">digraph</span> <span class="pre">{</span> <span class="pre">a</span> <span class="pre">-&gt;</span> <span class="pre">b</span> <span class="pre">-&gt;</span> <span class="pre">e</span> <span class="pre">;</span> <span class="pre">a</span> <span class="pre">-&gt;</span> <span class="pre">c</span> <span class="pre">-&gt;</span> <span class="pre">e</span> <span class="pre">;</span> <span class="pre">b</span> <span class="pre">-&gt;</span> <span class="pre">c}</span></tt> are equivalent.</li>
  193. <li>Subgraph IDs refer to subgraphs defined earlier in the graph
  194. description. Undefined subgraphs behave as empty subgraphs
  195. (<tt class="docutils literal"><span class="pre">{}</span></tt>). This is the same behavior as GraphViz.</li>
  196. </ul>
  197. </blockquote>
  198. </div>
  199. <div class="section" id="see-also">
  200. <h1><a class="toc-backref" href="#id7">See Also</a></h1>
  201. <p><a class="reference external" href="write-graphviz.html">write_graphviz</a></p>
  202. </div>
  203. <div class="section" id="future-work">
  204. <h1><a class="toc-backref" href="#id8">Future Work</a></h1>
  205. <blockquote>
  206. <ul class="simple">
  207. <li>Passing port information to BGL.</li>
  208. <li>Expanding escape codes in the same way GraphViz does.</li>
  209. <li>Support for optional recognition of subgraphs as distinct entities.</li>
  210. </ul>
  211. </blockquote>
  212. </div>
  213. </div>
  214. <div class="footer">
  215. <hr class="footer" />
  216. Generated on: 2009-06-12 00:41 UTC.
  217. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  218. </div>
  219. </body>
  220. </html>