graphviz.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. //=======================================================================
  2. // Copyright 2001 University of Notre Dame.
  3. // Copyright 2003 Jeremy Siek
  4. // Authors: Lie-Quan Lee, Jeremy Siek, and Douglas Gregor
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. #ifndef BOOST_GRAPHVIZ_HPP
  11. #define BOOST_GRAPHVIZ_HPP
  12. #include <boost/config.hpp>
  13. #include <string>
  14. #include <map>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <stdio.h> // for FILE
  18. #include <boost/property_map/property_map.hpp>
  19. #include <boost/tuple/tuple.hpp>
  20. #include <boost/graph/graph_traits.hpp>
  21. #include <boost/graph/properties.hpp>
  22. #include <boost/graph/subgraph.hpp>
  23. #include <boost/graph/adjacency_list.hpp>
  24. #include <boost/property_map/dynamic_property_map.hpp>
  25. #include <boost/graph/overloading.hpp>
  26. #include <boost/graph/dll_import_export.hpp>
  27. #include <boost/spirit/include/classic_multi_pass.hpp>
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/algorithm/string/replace.hpp>
  30. #include <boost/xpressive/xpressive_static.hpp>
  31. namespace boost {
  32. template <typename directed_category>
  33. struct graphviz_io_traits {
  34. static std::string name() {
  35. return "digraph";
  36. }
  37. static std::string delimiter() {
  38. return "->";
  39. } };
  40. template <>
  41. struct graphviz_io_traits <undirected_tag> {
  42. static std::string name() {
  43. return "graph";
  44. }
  45. static std::string delimiter() {
  46. return "--";
  47. }
  48. };
  49. struct default_writer {
  50. void operator()(std::ostream&) const {
  51. }
  52. template <class VorE>
  53. void operator()(std::ostream&, const VorE&) const {
  54. }
  55. };
  56. template <typename T>
  57. inline std::string escape_dot_string(const T& obj) {
  58. using namespace boost::xpressive;
  59. static sregex valid_unquoted_id = (((alpha | '_') >> *_w) | (!as_xpr('-') >> (('.' >> *_d) | (+_d >> !('.' >> *_d)))));
  60. std::string s(boost::lexical_cast<std::string>(obj));
  61. if (regex_match(s, valid_unquoted_id)) {
  62. return s;
  63. } else {
  64. return "\"" + regex_replace(s, sregex(as_xpr('"')), "\\\"") + "\"";
  65. }
  66. }
  67. template <class Name>
  68. class label_writer {
  69. public:
  70. label_writer(Name _name) : name(_name) {}
  71. template <class VertexOrEdge>
  72. void operator()(std::ostream& out, const VertexOrEdge& v) const {
  73. out << "[label=" << escape_dot_string(get(name, v)) << "]";
  74. }
  75. private:
  76. Name name;
  77. };
  78. template <class Name>
  79. inline label_writer<Name>
  80. make_label_writer(Name n) {
  81. return label_writer<Name>(n);
  82. }
  83. enum edge_attribute_t { edge_attribute = 1111 };
  84. enum vertex_attribute_t { vertex_attribute = 2222 };
  85. enum graph_graph_attribute_t { graph_graph_attribute = 3333 };
  86. enum graph_vertex_attribute_t { graph_vertex_attribute = 4444 };
  87. enum graph_edge_attribute_t { graph_edge_attribute = 5555 };
  88. BOOST_INSTALL_PROPERTY(edge, attribute);
  89. BOOST_INSTALL_PROPERTY(vertex, attribute);
  90. BOOST_INSTALL_PROPERTY(graph, graph_attribute);
  91. BOOST_INSTALL_PROPERTY(graph, vertex_attribute);
  92. BOOST_INSTALL_PROPERTY(graph, edge_attribute);
  93. template <class Attribute>
  94. inline void write_attributes(const Attribute& attr, std::ostream& out) {
  95. typename Attribute::const_iterator i, iend;
  96. i = attr.begin();
  97. iend = attr.end();
  98. while ( i != iend ) {
  99. out << i->first << "=" << escape_dot_string(i->second);
  100. ++i;
  101. if ( i != iend )
  102. out << ", ";
  103. }
  104. }
  105. template<typename Attributes>
  106. inline void write_all_attributes(Attributes attributes,
  107. const std::string& name,
  108. std::ostream& out)
  109. {
  110. typename Attributes::const_iterator i = attributes.begin(),
  111. end = attributes.end();
  112. if (i != end) {
  113. out << name << " [\n";
  114. write_attributes(attributes, out);
  115. out << "];\n";
  116. }
  117. }
  118. inline void write_all_attributes(detail::error_property_not_found,
  119. const std::string&,
  120. std::ostream&)
  121. {
  122. // Do nothing - no attributes exist
  123. }
  124. template <typename GraphGraphAttributes,
  125. typename GraphNodeAttributes,
  126. typename GraphEdgeAttributes>
  127. struct graph_attributes_writer
  128. {
  129. graph_attributes_writer(GraphGraphAttributes gg,
  130. GraphNodeAttributes gn,
  131. GraphEdgeAttributes ge)
  132. : g_attributes(gg), n_attributes(gn), e_attributes(ge) { }
  133. void operator()(std::ostream& out) const {
  134. write_all_attributes(g_attributes, "graph", out);
  135. write_all_attributes(n_attributes, "node", out);
  136. write_all_attributes(e_attributes, "edge", out);
  137. }
  138. GraphGraphAttributes g_attributes;
  139. GraphNodeAttributes n_attributes;
  140. GraphEdgeAttributes e_attributes;
  141. };
  142. template <typename GAttrMap, typename NAttrMap, typename EAttrMap>
  143. graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap>
  144. make_graph_attributes_writer(const GAttrMap& g_attr, const NAttrMap& n_attr,
  145. const EAttrMap& e_attr) {
  146. return graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap>
  147. (g_attr, n_attr, e_attr);
  148. }
  149. template <typename Graph>
  150. graph_attributes_writer
  151. <typename graph_property<Graph, graph_graph_attribute_t>::type,
  152. typename graph_property<Graph, graph_vertex_attribute_t>::type,
  153. typename graph_property<Graph, graph_edge_attribute_t>::type>
  154. make_graph_attributes_writer(const Graph& g)
  155. {
  156. typedef typename graph_property<Graph, graph_graph_attribute_t>::type
  157. GAttrMap;
  158. typedef typename graph_property<Graph, graph_vertex_attribute_t>::type
  159. NAttrMap;
  160. typedef typename graph_property<Graph, graph_edge_attribute_t>::type
  161. EAttrMap;
  162. GAttrMap gam = get_property(g, graph_graph_attribute);
  163. NAttrMap nam = get_property(g, graph_vertex_attribute);
  164. EAttrMap eam = get_property(g, graph_edge_attribute);
  165. graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap> writer(gam, nam, eam);
  166. return writer;
  167. }
  168. template <typename AttributeMap>
  169. struct attributes_writer {
  170. attributes_writer(AttributeMap attr)
  171. : attributes(attr) { }
  172. template <class VorE>
  173. void operator()(std::ostream& out, const VorE& e) const {
  174. this->write_attribute(out, attributes[e]);
  175. }
  176. private:
  177. template<typename AttributeSequence>
  178. void write_attribute(std::ostream& out,
  179. const AttributeSequence& seq) const
  180. {
  181. if (!seq.empty()) {
  182. out << "[";
  183. write_attributes(seq, out);
  184. out << "]";
  185. }
  186. }
  187. void write_attribute(std::ostream&,
  188. detail::error_property_not_found) const
  189. {
  190. }
  191. AttributeMap attributes;
  192. };
  193. template <typename Graph>
  194. attributes_writer
  195. <typename property_map<Graph, edge_attribute_t>::const_type>
  196. make_edge_attributes_writer(const Graph& g)
  197. {
  198. typedef typename property_map<Graph, edge_attribute_t>::const_type
  199. EdgeAttributeMap;
  200. return attributes_writer<EdgeAttributeMap>(get(edge_attribute, g));
  201. }
  202. template <typename Graph>
  203. attributes_writer
  204. <typename property_map<Graph, vertex_attribute_t>::const_type>
  205. make_vertex_attributes_writer(const Graph& g)
  206. {
  207. typedef typename property_map<Graph, vertex_attribute_t>::const_type
  208. VertexAttributeMap;
  209. return attributes_writer<VertexAttributeMap>(get(vertex_attribute, g));
  210. }
  211. template <typename Graph, typename VertexPropertiesWriter,
  212. typename EdgePropertiesWriter, typename GraphPropertiesWriter,
  213. typename VertexID>
  214. inline void
  215. write_graphviz
  216. (std::ostream& out, const Graph& g,
  217. VertexPropertiesWriter vpw,
  218. EdgePropertiesWriter epw,
  219. GraphPropertiesWriter gpw,
  220. VertexID vertex_id
  221. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  222. {
  223. BOOST_CONCEPT_ASSERT((EdgeListGraphConcept<Graph>));
  224. typedef typename graph_traits<Graph>::directed_category cat_type;
  225. typedef graphviz_io_traits<cat_type> Traits;
  226. std::string name = "G";
  227. out << Traits::name() << " " << escape_dot_string(name) << " {" << std::endl;
  228. gpw(out); //print graph properties
  229. typename graph_traits<Graph>::vertex_iterator i, end;
  230. for(boost::tie(i,end) = vertices(g); i != end; ++i) {
  231. out << escape_dot_string(get(vertex_id, *i));
  232. vpw(out, *i); //print vertex attributes
  233. out << ";" << std::endl;
  234. }
  235. typename graph_traits<Graph>::edge_iterator ei, edge_end;
  236. for(boost::tie(ei, edge_end) = edges(g); ei != edge_end; ++ei) {
  237. out << escape_dot_string(get(vertex_id, source(*ei, g))) << Traits::delimiter() << escape_dot_string(get(vertex_id, target(*ei, g))) << " ";
  238. epw(out, *ei); //print edge attributes
  239. out << ";" << std::endl;
  240. }
  241. out << "}" << std::endl;
  242. }
  243. template <typename Graph, typename VertexPropertiesWriter,
  244. typename EdgePropertiesWriter, typename GraphPropertiesWriter>
  245. inline void
  246. write_graphviz(std::ostream& out, const Graph& g,
  247. VertexPropertiesWriter vpw,
  248. EdgePropertiesWriter epw,
  249. GraphPropertiesWriter gpw
  250. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  251. { write_graphviz(out, g, vpw, epw, gpw, get(vertex_index, g)); }
  252. #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
  253. // ambiguous overload problem with VC++
  254. template <typename Graph>
  255. inline void
  256. write_graphviz(std::ostream& out, const Graph& g
  257. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  258. {
  259. default_writer dw;
  260. default_writer gw;
  261. write_graphviz(out, g, dw, dw, gw);
  262. }
  263. #endif
  264. template <typename Graph, typename VertexWriter>
  265. inline void
  266. write_graphviz(std::ostream& out, const Graph& g, VertexWriter vw
  267. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  268. {
  269. default_writer dw;
  270. default_writer gw;
  271. write_graphviz(out, g, vw, dw, gw);
  272. }
  273. template <typename Graph, typename VertexWriter, typename EdgeWriter>
  274. inline void
  275. write_graphviz(std::ostream& out, const Graph& g,
  276. VertexWriter vw, EdgeWriter ew
  277. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  278. {
  279. default_writer gw;
  280. write_graphviz(out, g, vw, ew, gw);
  281. }
  282. namespace detail {
  283. template <class Graph_, class RandomAccessIterator, class VertexID>
  284. void write_graphviz_subgraph (std::ostream& out,
  285. const subgraph<Graph_>& g,
  286. RandomAccessIterator vertex_marker,
  287. RandomAccessIterator edge_marker,
  288. VertexID vertex_id)
  289. {
  290. typedef subgraph<Graph_> Graph;
  291. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  292. typedef typename graph_traits<Graph>::directed_category cat_type;
  293. typedef graphviz_io_traits<cat_type> Traits;
  294. typedef typename graph_property<Graph, graph_name_t>::type NameType;
  295. const NameType& g_name = get_property(g, graph_name);
  296. if ( g.is_root() )
  297. out << Traits::name() ;
  298. else
  299. out << "subgraph";
  300. out << " " << escape_dot_string(g_name) << " {" << std::endl;
  301. typename Graph::const_children_iterator i_child, j_child;
  302. //print graph/node/edge attributes
  303. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  304. typedef typename graph_property<Graph, graph_graph_attribute_t>::type
  305. GAttrMap;
  306. typedef typename graph_property<Graph, graph_vertex_attribute_t>::type
  307. NAttrMap;
  308. typedef typename graph_property<Graph, graph_edge_attribute_t>::type
  309. EAttrMap;
  310. GAttrMap gam = get_property(g, graph_graph_attribute);
  311. NAttrMap nam = get_property(g, graph_vertex_attribute);
  312. EAttrMap eam = get_property(g, graph_edge_attribute);
  313. graph_attributes_writer<GAttrMap, NAttrMap, EAttrMap> writer(gam, nam, eam);
  314. writer(out);
  315. #else
  316. make_graph_attributes_writer(g)(out);
  317. #endif
  318. //print subgraph
  319. for ( boost::tie(i_child,j_child) = g.children();
  320. i_child != j_child; ++i_child )
  321. write_graphviz_subgraph(out, *i_child, vertex_marker, edge_marker,
  322. vertex_id);
  323. // Print out vertices and edges not in the subgraphs.
  324. typename graph_traits<Graph>::vertex_iterator i, end;
  325. typename graph_traits<Graph>::edge_iterator ei, edge_end;
  326. for(boost::tie(i,end) = vertices(g); i != end; ++i) {
  327. Vertex v = g.local_to_global(*i);
  328. int pos = get(vertex_id, v);
  329. if ( vertex_marker[pos] ) {
  330. vertex_marker[pos] = false;
  331. out << escape_dot_string(pos);
  332. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  333. typedef typename property_map<Graph, vertex_attribute_t>::const_type
  334. VertexAttributeMap;
  335. attributes_writer<VertexAttributeMap> vawriter(get(vertex_attribute,
  336. g.root()));
  337. vawriter(out, v);
  338. #else
  339. make_vertex_attributes_writer(g.root())(out, v);
  340. #endif
  341. out << ";" << std::endl;
  342. }
  343. }
  344. for (boost::tie(ei, edge_end) = edges(g); ei != edge_end; ++ei) {
  345. Vertex u = g.local_to_global(source(*ei,g)),
  346. v = g.local_to_global(target(*ei, g));
  347. int pos = get(get(edge_index, g.root()), g.local_to_global(*ei));
  348. if ( edge_marker[pos] ) {
  349. edge_marker[pos] = false;
  350. out << escape_dot_string(get(vertex_id, u)) << " " << Traits::delimiter()
  351. << " " << escape_dot_string(get(vertex_id, v));
  352. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  353. typedef typename property_map<Graph, edge_attribute_t>::const_type
  354. EdgeAttributeMap;
  355. attributes_writer<EdgeAttributeMap> eawriter(get(edge_attribute, g));
  356. eawriter(out, *ei);
  357. #else
  358. make_edge_attributes_writer(g)(out, *ei); //print edge properties
  359. #endif
  360. out << ";" << std::endl;
  361. }
  362. }
  363. out << "}" << std::endl;
  364. }
  365. } // namespace detail
  366. // requires graph_name graph property
  367. template <typename Graph>
  368. void write_graphviz(std::ostream& out, const subgraph<Graph>& g) {
  369. std::vector<bool> edge_marker(num_edges(g), true);
  370. std::vector<bool> vertex_marker(num_vertices(g), true);
  371. detail::write_graphviz_subgraph(out, g,
  372. vertex_marker.begin(),
  373. edge_marker.begin(),
  374. get(vertex_index, g));
  375. }
  376. template <typename Graph>
  377. void write_graphviz(const std::string& filename, const subgraph<Graph>& g) {
  378. std::ofstream out(filename.c_str());
  379. std::vector<bool> edge_marker(num_edges(g), true);
  380. std::vector<bool> vertex_marker(num_vertices(g), true);
  381. detail::write_graphviz_subgraph(out, g,
  382. vertex_marker.begin(),
  383. edge_marker.begin(),
  384. get(vertex_index, g));
  385. }
  386. template <typename Graph, typename VertexID>
  387. void write_graphviz(std::ostream& out, const subgraph<Graph>& g,
  388. VertexID vertex_id)
  389. {
  390. std::vector<bool> edge_marker(num_edges(g), true);
  391. std::vector<bool> vertex_marker(num_vertices(g), true);
  392. detail::write_graphviz_subgraph(out, g,
  393. vertex_marker.begin(),
  394. edge_marker.begin(),
  395. vertex_id);
  396. }
  397. template <typename Graph, typename VertexID>
  398. void write_graphviz(const std::string& filename, const subgraph<Graph>& g,
  399. VertexID vertex_id)
  400. {
  401. std::ofstream out(filename.c_str());
  402. std::vector<bool> edge_marker(num_edges(g), true);
  403. std::vector<bool> vertex_marker(num_vertices(g), true);
  404. detail::write_graphviz_subgraph(out, g,
  405. vertex_marker.begin(),
  406. edge_marker.begin(),
  407. vertex_id);
  408. }
  409. #if 0
  410. // This interface has not worked for a long time
  411. typedef std::map<std::string, std::string> GraphvizAttrList;
  412. typedef property<vertex_attribute_t, GraphvizAttrList>
  413. GraphvizVertexProperty;
  414. typedef property<edge_attribute_t, GraphvizAttrList,
  415. property<edge_index_t, int> >
  416. GraphvizEdgeProperty;
  417. typedef property<graph_graph_attribute_t, GraphvizAttrList,
  418. property<graph_vertex_attribute_t, GraphvizAttrList,
  419. property<graph_edge_attribute_t, GraphvizAttrList,
  420. property<graph_name_t, std::string> > > >
  421. GraphvizGraphProperty;
  422. typedef subgraph<adjacency_list<vecS,
  423. vecS, directedS,
  424. GraphvizVertexProperty,
  425. GraphvizEdgeProperty,
  426. GraphvizGraphProperty> >
  427. GraphvizDigraph;
  428. typedef subgraph<adjacency_list<vecS,
  429. vecS, undirectedS,
  430. GraphvizVertexProperty,
  431. GraphvizEdgeProperty,
  432. GraphvizGraphProperty> >
  433. GraphvizGraph;
  434. // These four require linking the BGL-Graphviz library: libbgl-viz.a
  435. // from the /src directory.
  436. // Library has not existed for a while
  437. extern void read_graphviz(const std::string& file, GraphvizDigraph& g);
  438. extern void read_graphviz(FILE* file, GraphvizDigraph& g);
  439. extern void read_graphviz(const std::string& file, GraphvizGraph& g);
  440. extern void read_graphviz(FILE* file, GraphvizGraph& g);
  441. #endif
  442. class dynamic_properties_writer
  443. {
  444. public:
  445. dynamic_properties_writer(const dynamic_properties& dp) : dp(&dp) { }
  446. template<typename Descriptor>
  447. void operator()(std::ostream& out, Descriptor key) const
  448. {
  449. bool first = true;
  450. for (dynamic_properties::const_iterator i = dp->begin();
  451. i != dp->end(); ++i) {
  452. if (typeid(key) == i->second->key()) {
  453. if (first) out << " [";
  454. else out << ", ";
  455. first = false;
  456. out << i->first << "=" << escape_dot_string(i->second->get_string(key));
  457. }
  458. }
  459. if (!first) out << "]";
  460. }
  461. private:
  462. const dynamic_properties* dp;
  463. };
  464. class dynamic_vertex_properties_writer
  465. {
  466. public:
  467. dynamic_vertex_properties_writer(const dynamic_properties& dp,
  468. const std::string& node_id)
  469. : dp(&dp), node_id(&node_id) { }
  470. template<typename Descriptor>
  471. void operator()(std::ostream& out, Descriptor key) const
  472. {
  473. bool first = true;
  474. for (dynamic_properties::const_iterator i = dp->begin();
  475. i != dp->end(); ++i) {
  476. if (typeid(key) == i->second->key()
  477. && i->first != *node_id) {
  478. if (first) out << " [";
  479. else out << ", ";
  480. first = false;
  481. out << i->first << "=" << escape_dot_string(i->second->get_string(key));
  482. }
  483. }
  484. if (!first) out << "]";
  485. }
  486. private:
  487. const dynamic_properties* dp;
  488. const std::string* node_id;
  489. };
  490. namespace graph { namespace detail {
  491. template<typename Vertex>
  492. struct node_id_property_map
  493. {
  494. typedef std::string value_type;
  495. typedef value_type reference;
  496. typedef Vertex key_type;
  497. typedef readable_property_map_tag category;
  498. node_id_property_map() {}
  499. node_id_property_map(const dynamic_properties& dp,
  500. const std::string& node_id)
  501. : dp(&dp), node_id(&node_id) { }
  502. const dynamic_properties* dp;
  503. const std::string* node_id;
  504. };
  505. template<typename Vertex>
  506. inline std::string
  507. get(node_id_property_map<Vertex> pm,
  508. typename node_id_property_map<Vertex>::key_type v)
  509. { return get(*pm.node_id, *pm.dp, v); }
  510. } } // end namespace graph::detail
  511. template<typename Graph>
  512. inline void
  513. write_graphviz_dp(std::ostream& out, const Graph& g,
  514. const dynamic_properties& dp,
  515. const std::string& node_id = "node_id"
  516. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  517. {
  518. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  519. write_graphviz_dp(out, g, dp, node_id,
  520. graph::detail::node_id_property_map<Vertex>(dp, node_id));
  521. }
  522. template<typename Graph, typename VertexID>
  523. void
  524. write_graphviz_dp(std::ostream& out, const Graph& g,
  525. const dynamic_properties& dp, const std::string& node_id,
  526. VertexID id
  527. BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph,vertex_list_graph_tag))
  528. {
  529. write_graphviz
  530. (out, g,
  531. /*vertex_writer=*/dynamic_vertex_properties_writer(dp, node_id),
  532. /*edge_writer=*/dynamic_properties_writer(dp),
  533. /*graph_writer=*/default_writer(),
  534. id);
  535. }
  536. /////////////////////////////////////////////////////////////////////////////
  537. // Graph reader exceptions
  538. /////////////////////////////////////////////////////////////////////////////
  539. struct graph_exception : public std::exception {
  540. virtual ~graph_exception() throw() {}
  541. virtual const char* what() const throw() = 0;
  542. };
  543. struct bad_parallel_edge : public graph_exception {
  544. std::string from;
  545. std::string to;
  546. mutable std::string statement;
  547. bad_parallel_edge(const std::string& i, const std::string& j) :
  548. from(i), to(j) {}
  549. virtual ~bad_parallel_edge() throw() {}
  550. const char* what() const throw() {
  551. if(statement.empty())
  552. statement =
  553. std::string("Failed to add parallel edge: (")
  554. + from + "," + to + ")\n";
  555. return statement.c_str();
  556. }
  557. };
  558. struct directed_graph_error : public graph_exception {
  559. virtual ~directed_graph_error() throw() {}
  560. virtual const char* what() const throw() {
  561. return
  562. "read_graphviz: "
  563. "Tried to read a directed graph into an undirected graph.";
  564. }
  565. };
  566. struct undirected_graph_error : public graph_exception {
  567. virtual ~undirected_graph_error() throw() {}
  568. virtual const char* what() const throw() {
  569. return
  570. "read_graphviz: "
  571. "Tried to read an undirected graph into a directed graph.";
  572. }
  573. };
  574. struct bad_graphviz_syntax: public graph_exception {
  575. std::string errmsg;
  576. bad_graphviz_syntax(const std::string& errmsg)
  577. : errmsg(errmsg) {}
  578. const char* what() const throw () {return errmsg.c_str();}
  579. ~bad_graphviz_syntax() throw () {};
  580. };
  581. namespace detail { namespace graph {
  582. typedef std::string id_t;
  583. typedef id_t node_t;
  584. // edges are not uniquely determined by adjacent nodes
  585. class edge_t {
  586. int idx_;
  587. explicit edge_t(int i) : idx_(i) {}
  588. public:
  589. static edge_t new_edge() {
  590. static int idx = 0;
  591. return edge_t(idx++);
  592. };
  593. bool operator==(const edge_t& rhs) const {
  594. return idx_ == rhs.idx_;
  595. }
  596. bool operator<(const edge_t& rhs) const {
  597. return idx_ < rhs.idx_;
  598. }
  599. };
  600. class mutate_graph
  601. {
  602. public:
  603. virtual ~mutate_graph() {}
  604. virtual bool is_directed() const = 0;
  605. virtual void do_add_vertex(const node_t& node) = 0;
  606. virtual void
  607. do_add_edge(const edge_t& edge, const node_t& source, const node_t& target)
  608. = 0;
  609. virtual void
  610. set_node_property(const id_t& key, const node_t& node, const id_t& value) = 0;
  611. virtual void
  612. set_edge_property(const id_t& key, const edge_t& edge, const id_t& value) = 0;
  613. virtual void // RG: need new second parameter to support BGL subgraphs
  614. set_graph_property(const id_t& key, const id_t& value) = 0;
  615. };
  616. template<typename MutableGraph>
  617. class mutate_graph_impl : public mutate_graph
  618. {
  619. typedef typename graph_traits<MutableGraph>::vertex_descriptor bgl_vertex_t;
  620. typedef typename graph_traits<MutableGraph>::edge_descriptor bgl_edge_t;
  621. public:
  622. mutate_graph_impl(MutableGraph& graph, dynamic_properties& dp,
  623. std::string node_id_prop)
  624. : graph_(graph), dp_(dp), node_id_prop_(node_id_prop) { }
  625. ~mutate_graph_impl() {}
  626. bool is_directed() const
  627. {
  628. return
  629. boost::is_convertible<
  630. typename boost::graph_traits<MutableGraph>::directed_category,
  631. boost::directed_tag>::value;
  632. }
  633. virtual void do_add_vertex(const node_t& node)
  634. {
  635. // Add the node to the graph.
  636. bgl_vertex_t v = add_vertex(graph_);
  637. // Set up a mapping from name to BGL vertex.
  638. bgl_nodes.insert(std::make_pair(node, v));
  639. // node_id_prop_ allows the caller to see the real id names for nodes.
  640. put(node_id_prop_, dp_, v, node);
  641. }
  642. void
  643. do_add_edge(const edge_t& edge, const node_t& source, const node_t& target)
  644. {
  645. std::pair<bgl_edge_t, bool> result =
  646. add_edge(bgl_nodes[source], bgl_nodes[target], graph_);
  647. if(!result.second) {
  648. // In the case of no parallel edges allowed
  649. boost::throw_exception(bad_parallel_edge(source, target));
  650. } else {
  651. bgl_edges.insert(std::make_pair(edge, result.first));
  652. }
  653. }
  654. void
  655. set_node_property(const id_t& key, const node_t& node, const id_t& value)
  656. {
  657. put(key, dp_, bgl_nodes[node], value);
  658. }
  659. void
  660. set_edge_property(const id_t& key, const edge_t& edge, const id_t& value)
  661. {
  662. put(key, dp_, bgl_edges[edge], value);
  663. }
  664. void
  665. set_graph_property(const id_t& key, const id_t& value)
  666. {
  667. /* RG: pointer to graph prevents copying */
  668. put(key, dp_, &graph_, value);
  669. }
  670. protected:
  671. MutableGraph& graph_;
  672. dynamic_properties& dp_;
  673. std::string node_id_prop_;
  674. std::map<node_t, bgl_vertex_t> bgl_nodes;
  675. std::map<edge_t, bgl_edge_t> bgl_edges;
  676. };
  677. } } } // end namespace boost::detail::graph
  678. #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER
  679. # ifndef BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS
  680. # define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS
  681. # endif
  682. # include <boost/graph/detail/read_graphviz_spirit.hpp>
  683. #else // New default parser
  684. # include <boost/graph/detail/read_graphviz_new.hpp>
  685. #endif // BOOST_GRAPH_USE_SPIRIT_PARSER
  686. namespace boost {
  687. // Parse the passed string as a GraphViz dot file.
  688. template <typename MutableGraph>
  689. bool read_graphviz(const std::string& data,
  690. MutableGraph& graph,
  691. dynamic_properties& dp,
  692. std::string const& node_id = "node_id") {
  693. #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER
  694. return read_graphviz_spirit(data.begin(), data.end(), graph, dp, node_id);
  695. #else // Non-Spirit parser
  696. return read_graphviz_new(data,graph,dp,node_id);
  697. #endif
  698. }
  699. // Parse the passed iterator range as a GraphViz dot file.
  700. template <typename InputIterator, typename MutableGraph>
  701. bool read_graphviz(InputIterator user_first,
  702. InputIterator user_last,
  703. MutableGraph& graph,
  704. dynamic_properties& dp,
  705. std::string const& node_id = "node_id") {
  706. #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER
  707. typedef InputIterator is_t;
  708. typedef boost::spirit::classic::multi_pass<is_t> iterator_t;
  709. iterator_t first(boost::spirit::classic::make_multi_pass(user_first));
  710. iterator_t last(boost::spirit::classic::make_multi_pass(user_last));
  711. return read_graphviz_spirit(first, last, graph, dp, node_id);
  712. #else // Non-Spirit parser
  713. return read_graphviz_new(std::string(user_first, user_last), graph, dp, node_id);
  714. #endif
  715. }
  716. // Parse the passed stream as a GraphViz dot file.
  717. template <typename MutableGraph>
  718. bool read_graphviz(std::istream& in, MutableGraph& graph,
  719. dynamic_properties& dp,
  720. std::string const& node_id = "node_id")
  721. {
  722. typedef std::istream_iterator<char> is_t;
  723. in >> std::noskipws;
  724. return read_graphviz(is_t(in), is_t(), graph, dp, node_id);
  725. }
  726. } // namespace boost
  727. #ifdef BOOST_GRAPH_USE_MPI
  728. # include <boost/graph/distributed/graphviz.hpp>
  729. #endif
  730. #endif // BOOST_GRAPHVIZ_HPP