/src/boost/boost/graph/graphviz.hpp
Line | Count | Source |
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 | | |
13 | | #include <boost/config.hpp> |
14 | | #include <cstdio> // for FILE |
15 | | #include <fstream> |
16 | | #include <iostream> |
17 | | #include <map> |
18 | | #include <string> |
19 | | #include <boost/property_map/property_map.hpp> |
20 | | #include <boost/tuple/tuple.hpp> |
21 | | #include <boost/graph/exception.hpp> |
22 | | #include <boost/graph/graph_traits.hpp> |
23 | | #include <boost/graph/properties.hpp> |
24 | | #include <boost/graph/subgraph.hpp> |
25 | | #include <boost/graph/adjacency_list.hpp> |
26 | | #include <boost/property_map/dynamic_property_map.hpp> |
27 | | #include <boost/graph/overloading.hpp> |
28 | | #include <boost/graph/dll_import_export.hpp> |
29 | | #include <boost/graph/compressed_sparse_row_graph.hpp> |
30 | | #include <boost/graph/iteration_macros.hpp> |
31 | | #include <boost/graph/detail/mpi_include.hpp> |
32 | | #include <boost/spirit/include/classic_multi_pass.hpp> |
33 | | #include <boost/lexical_cast.hpp> |
34 | | #include <boost/static_assert.hpp> |
35 | | #include <boost/algorithm/string/replace.hpp> |
36 | | #include <boost/xpressive/xpressive_static.hpp> |
37 | | #include <boost/foreach.hpp> |
38 | | |
39 | | namespace boost |
40 | | { |
41 | | |
42 | | template < typename directed_category > struct graphviz_io_traits |
43 | | { |
44 | | static std::string name() { return "digraph"; } |
45 | | static std::string delimiter() { return "->"; } |
46 | | }; |
47 | | |
48 | | template <> struct graphviz_io_traits< undirected_tag > |
49 | | { |
50 | 0 | static std::string name() { return "graph"; } |
51 | 0 | static std::string delimiter() { return "--"; } |
52 | | }; |
53 | | |
54 | | struct default_writer |
55 | | { |
56 | 0 | void operator()(std::ostream&) const {} |
57 | | template < class VorE > void operator()(std::ostream&, const VorE&) const {} |
58 | | }; |
59 | | |
60 | | template < typename T > inline std::string escape_dot_string(const T& obj) |
61 | | { |
62 | | using namespace boost::xpressive; |
63 | | static sregex valid_unquoted_id = (((alpha | '_') >> *_w) |
64 | | | (!as_xpr('-') >> (('.' >> *_d) | (+_d >> !('.' >> *_d))))); |
65 | | std::string s(boost::lexical_cast< std::string >(obj)); |
66 | | if (regex_match(s, valid_unquoted_id)) |
67 | | { |
68 | | return s; |
69 | | } |
70 | | else |
71 | | { |
72 | | boost::algorithm::replace_all(s, "\"", "\\\""); |
73 | | return "\"" + s + "\""; |
74 | | } |
75 | | } |
76 | | |
77 | | template < class Name > class label_writer |
78 | | { |
79 | | public: |
80 | | label_writer(Name _name) : name(_name) {} |
81 | | template < class VertexOrEdge > |
82 | | void operator()(std::ostream& out, const VertexOrEdge& v) const |
83 | | { |
84 | | out << "[label=" << escape_dot_string(get(name, v)) << "]"; |
85 | | } |
86 | | |
87 | | private: |
88 | | Name name; |
89 | | }; |
90 | | template < class Name > inline label_writer< Name > make_label_writer(Name n) |
91 | | { |
92 | | return label_writer< Name >(n); |
93 | | } |
94 | | |
95 | | enum edge_attribute_t |
96 | | { |
97 | | edge_attribute = 1111 |
98 | | }; |
99 | | enum vertex_attribute_t |
100 | | { |
101 | | vertex_attribute = 2222 |
102 | | }; |
103 | | enum graph_graph_attribute_t |
104 | | { |
105 | | graph_graph_attribute = 3333 |
106 | | }; |
107 | | enum graph_vertex_attribute_t |
108 | | { |
109 | | graph_vertex_attribute = 4444 |
110 | | }; |
111 | | enum graph_edge_attribute_t |
112 | | { |
113 | | graph_edge_attribute = 5555 |
114 | | }; |
115 | | |
116 | | BOOST_INSTALL_PROPERTY(edge, attribute); |
117 | | BOOST_INSTALL_PROPERTY(vertex, attribute); |
118 | | BOOST_INSTALL_PROPERTY(graph, graph_attribute); |
119 | | BOOST_INSTALL_PROPERTY(graph, vertex_attribute); |
120 | | BOOST_INSTALL_PROPERTY(graph, edge_attribute); |
121 | | |
122 | | template < class Attribute > |
123 | | inline void write_attributes(const Attribute& attr, std::ostream& out) |
124 | | { |
125 | | typename Attribute::const_iterator i, iend; |
126 | | i = attr.begin(); |
127 | | iend = attr.end(); |
128 | | |
129 | | while (i != iend) |
130 | | { |
131 | | out << i->first << "=" << escape_dot_string(i->second); |
132 | | ++i; |
133 | | if (i != iend) |
134 | | out << ", "; |
135 | | } |
136 | | } |
137 | | |
138 | | template < typename Attributes > |
139 | | inline void write_all_attributes( |
140 | | Attributes attributes, const std::string& name, std::ostream& out) |
141 | | { |
142 | | typename Attributes::const_iterator i = attributes.begin(), |
143 | | end = attributes.end(); |
144 | | if (i != end) |
145 | | { |
146 | | out << name << " [\n"; |
147 | | write_attributes(attributes, out); |
148 | | out << "];\n"; |
149 | | } |
150 | | } |
151 | | |
152 | | inline void write_all_attributes( |
153 | | detail::error_property_not_found, const std::string&, std::ostream&) |
154 | 0 | { |
155 | 0 | // Do nothing - no attributes exist |
156 | 0 | } |
157 | | |
158 | | template < typename GraphGraphAttributes, typename GraphNodeAttributes, |
159 | | typename GraphEdgeAttributes > |
160 | | struct graph_attributes_writer |
161 | | { |
162 | | graph_attributes_writer( |
163 | | GraphGraphAttributes gg, GraphNodeAttributes gn, GraphEdgeAttributes ge) |
164 | | : g_attributes(gg), n_attributes(gn), e_attributes(ge) |
165 | | { |
166 | | } |
167 | | |
168 | | void operator()(std::ostream& out) const |
169 | | { |
170 | | write_all_attributes(g_attributes, "graph", out); |
171 | | write_all_attributes(n_attributes, "node", out); |
172 | | write_all_attributes(e_attributes, "edge", out); |
173 | | } |
174 | | GraphGraphAttributes g_attributes; |
175 | | GraphNodeAttributes n_attributes; |
176 | | GraphEdgeAttributes e_attributes; |
177 | | }; |
178 | | |
179 | | template < typename GAttrMap, typename NAttrMap, typename EAttrMap > |
180 | | graph_attributes_writer< GAttrMap, NAttrMap, EAttrMap > |
181 | | make_graph_attributes_writer( |
182 | | const GAttrMap& g_attr, const NAttrMap& n_attr, const EAttrMap& e_attr) |
183 | | { |
184 | | return graph_attributes_writer< GAttrMap, NAttrMap, EAttrMap >( |
185 | | g_attr, n_attr, e_attr); |
186 | | } |
187 | | |
188 | | template < typename Graph > |
189 | | graph_attributes_writer< |
190 | | typename graph_property< Graph, graph_graph_attribute_t >::type, |
191 | | typename graph_property< Graph, graph_vertex_attribute_t >::type, |
192 | | typename graph_property< Graph, graph_edge_attribute_t >::type > |
193 | | make_graph_attributes_writer(const Graph& g) |
194 | | { |
195 | | typedef typename graph_property< Graph, graph_graph_attribute_t >::type |
196 | | GAttrMap; |
197 | | typedef typename graph_property< Graph, graph_vertex_attribute_t >::type |
198 | | NAttrMap; |
199 | | typedef |
200 | | typename graph_property< Graph, graph_edge_attribute_t >::type EAttrMap; |
201 | | GAttrMap gam = get_property(g, graph_graph_attribute); |
202 | | NAttrMap nam = get_property(g, graph_vertex_attribute); |
203 | | EAttrMap eam = get_property(g, graph_edge_attribute); |
204 | | graph_attributes_writer< GAttrMap, NAttrMap, EAttrMap > writer( |
205 | | gam, nam, eam); |
206 | | return writer; |
207 | | } |
208 | | |
209 | | template < typename AttributeMap > struct attributes_writer |
210 | | { |
211 | | attributes_writer(AttributeMap attr) : attributes(attr) {} |
212 | | |
213 | | template < class VorE > |
214 | | void operator()(std::ostream& out, const VorE& e) const |
215 | | { |
216 | | this->write_attribute(out, attributes[e]); |
217 | | } |
218 | | |
219 | | private: |
220 | | template < typename AttributeSequence > |
221 | | void write_attribute(std::ostream& out, const AttributeSequence& seq) const |
222 | | { |
223 | | if (!seq.empty()) |
224 | | { |
225 | | out << "["; |
226 | | write_attributes(seq, out); |
227 | | out << "]"; |
228 | | } |
229 | | } |
230 | | |
231 | | void write_attribute(std::ostream&, detail::error_property_not_found) const |
232 | | { |
233 | | } |
234 | | AttributeMap attributes; |
235 | | }; |
236 | | |
237 | | template < typename Graph > |
238 | | attributes_writer< |
239 | | typename property_map< Graph, edge_attribute_t >::const_type > |
240 | | make_edge_attributes_writer(const Graph& g) |
241 | | { |
242 | | typedef typename property_map< Graph, edge_attribute_t >::const_type |
243 | | EdgeAttributeMap; |
244 | | return attributes_writer< EdgeAttributeMap >(get(edge_attribute, g)); |
245 | | } |
246 | | |
247 | | template < typename Graph > |
248 | | attributes_writer< |
249 | | typename property_map< Graph, vertex_attribute_t >::const_type > |
250 | | make_vertex_attributes_writer(const Graph& g) |
251 | | { |
252 | | typedef typename property_map< Graph, vertex_attribute_t >::const_type |
253 | | VertexAttributeMap; |
254 | | return attributes_writer< VertexAttributeMap >(get(vertex_attribute, g)); |
255 | | } |
256 | | |
257 | | template < typename Graph, typename VertexPropertiesWriter, |
258 | | typename EdgePropertiesWriter, typename GraphPropertiesWriter, |
259 | | typename VertexID > |
260 | | inline void write_graphviz(std::ostream& out, const Graph& g, |
261 | | VertexPropertiesWriter vpw, EdgePropertiesWriter epw, |
262 | | GraphPropertiesWriter gpw, |
263 | | VertexID vertex_id BOOST_GRAPH_ENABLE_IF_MODELS_PARM( |
264 | | Graph, vertex_list_graph_tag)) |
265 | | { |
266 | | BOOST_CONCEPT_ASSERT((EdgeListGraphConcept< Graph >)); |
267 | | |
268 | | typedef typename graph_traits< Graph >::directed_category cat_type; |
269 | | typedef graphviz_io_traits< cat_type > Traits; |
270 | | std::string name = "G"; |
271 | | out << Traits::name() << " " << escape_dot_string(name) << " {" |
272 | | << std::endl; |
273 | | |
274 | | gpw(out); // print graph properties |
275 | | |
276 | | typename graph_traits< Graph >::vertex_iterator i, end; |
277 | | |
278 | | for (boost::tie(i, end) = vertices(g); i != end; ++i) |
279 | | { |
280 | | out << escape_dot_string(get(vertex_id, *i)); |
281 | | vpw(out, *i); // print vertex attributes |
282 | | out << ";" << std::endl; |
283 | | } |
284 | | typename graph_traits< Graph >::edge_iterator ei, edge_end; |
285 | | for (boost::tie(ei, edge_end) = edges(g); ei != edge_end; ++ei) |
286 | | { |
287 | | out << escape_dot_string(get(vertex_id, source(*ei, g))) |
288 | | << Traits::delimiter() |
289 | | << escape_dot_string(get(vertex_id, target(*ei, g))) << " "; |
290 | | epw(out, *ei); // print edge attributes |
291 | | out << ";" << std::endl; |
292 | | } |
293 | | out << "}" << std::endl; |
294 | | } |
295 | | |
296 | | template < typename Graph, typename VertexPropertiesWriter, |
297 | | typename EdgePropertiesWriter, typename GraphPropertiesWriter > |
298 | | inline void write_graphviz(std::ostream& out, const Graph& g, |
299 | | VertexPropertiesWriter vpw, EdgePropertiesWriter epw, |
300 | | GraphPropertiesWriter gpw BOOST_GRAPH_ENABLE_IF_MODELS_PARM( |
301 | | Graph, vertex_list_graph_tag)) |
302 | | { |
303 | | write_graphviz(out, g, vpw, epw, gpw, get(vertex_index, g)); |
304 | | } |
305 | | |
306 | | template < typename Graph > |
307 | | inline void write_graphviz(std::ostream& out, |
308 | | const Graph& g BOOST_GRAPH_ENABLE_IF_MODELS_PARM( |
309 | | Graph, vertex_list_graph_tag)) |
310 | | { |
311 | | default_writer dw; |
312 | | default_writer gw; |
313 | | write_graphviz(out, g, dw, dw, gw); |
314 | | } |
315 | | |
316 | | template < typename Graph, typename VertexWriter > |
317 | | inline void write_graphviz(std::ostream& out, const Graph& g, |
318 | | VertexWriter vw BOOST_GRAPH_ENABLE_IF_MODELS_PARM( |
319 | | Graph, vertex_list_graph_tag)) |
320 | | { |
321 | | default_writer dw; |
322 | | default_writer gw; |
323 | | write_graphviz(out, g, vw, dw, gw); |
324 | | } |
325 | | |
326 | | template < typename Graph, typename VertexWriter, typename EdgeWriter > |
327 | | inline void write_graphviz(std::ostream& out, const Graph& g, VertexWriter vw, |
328 | | EdgeWriter ew BOOST_GRAPH_ENABLE_IF_MODELS_PARM( |
329 | | Graph, vertex_list_graph_tag)) |
330 | | { |
331 | | default_writer gw; |
332 | | write_graphviz(out, g, vw, ew, gw); |
333 | | } |
334 | | |
335 | | namespace detail |
336 | | { |
337 | | |
338 | | template < class Graph_, class RandomAccessIterator, class VertexID > |
339 | | void write_graphviz_subgraph(std::ostream& out, const subgraph< Graph_ >& g, |
340 | | RandomAccessIterator vertex_marker, RandomAccessIterator edge_marker, |
341 | | VertexID vertex_id) |
342 | | { |
343 | | typedef subgraph< Graph_ > Graph; |
344 | | typedef typename graph_traits< Graph >::vertex_descriptor Vertex; |
345 | | typedef typename graph_traits< Graph >::directed_category cat_type; |
346 | | typedef graphviz_io_traits< cat_type > Traits; |
347 | | |
348 | | typedef typename graph_property< Graph, graph_name_t >::type NameType; |
349 | | const NameType& g_name = get_property(g, graph_name); |
350 | | |
351 | | if (g.is_root()) |
352 | | out << Traits::name(); |
353 | | else |
354 | | out << "subgraph"; |
355 | | |
356 | | out << " " << escape_dot_string(g_name) << " {" << std::endl; |
357 | | |
358 | | typename Graph::const_children_iterator i_child, j_child; |
359 | | |
360 | | // print graph/node/edge attributes |
361 | | make_graph_attributes_writer(g)(out); |
362 | | |
363 | | // print subgraph |
364 | | for (boost::tie(i_child, j_child) = g.children(); i_child != j_child; |
365 | | ++i_child) |
366 | | write_graphviz_subgraph( |
367 | | out, *i_child, vertex_marker, edge_marker, vertex_id); |
368 | | |
369 | | // Print out vertices and edges not in the subgraphs. |
370 | | |
371 | | typename graph_traits< Graph >::vertex_iterator i, end; |
372 | | typename graph_traits< Graph >::edge_iterator ei, edge_end; |
373 | | |
374 | | for (boost::tie(i, end) = vertices(g); i != end; ++i) |
375 | | { |
376 | | Vertex v = g.local_to_global(*i); |
377 | | int pos = get(vertex_id, v); |
378 | | if (vertex_marker[pos]) |
379 | | { |
380 | | vertex_marker[pos] = false; |
381 | | out << escape_dot_string(pos); |
382 | | make_vertex_attributes_writer(g.root())(out, v); |
383 | | out << ";" << std::endl; |
384 | | } |
385 | | } |
386 | | |
387 | | for (boost::tie(ei, edge_end) = edges(g); ei != edge_end; ++ei) |
388 | | { |
389 | | Vertex u = g.local_to_global(source(*ei, g)), |
390 | | v = g.local_to_global(target(*ei, g)); |
391 | | int pos = get(get(edge_index, g.root()), g.local_to_global(*ei)); |
392 | | if (edge_marker[pos]) |
393 | | { |
394 | | edge_marker[pos] = false; |
395 | | out << escape_dot_string(get(vertex_id, u)) << " " |
396 | | << Traits::delimiter() << " " |
397 | | << escape_dot_string(get(vertex_id, v)); |
398 | | make_edge_attributes_writer(g)( |
399 | | out, *ei); // print edge properties |
400 | | out << ";" << std::endl; |
401 | | } |
402 | | } |
403 | | out << "}" << std::endl; |
404 | | } |
405 | | } // namespace detail |
406 | | |
407 | | // requires graph_name graph property |
408 | | template < typename Graph > |
409 | | void write_graphviz(std::ostream& out, const subgraph< Graph >& g) |
410 | | { |
411 | | std::vector< bool > edge_marker(num_edges(g), true); |
412 | | std::vector< bool > vertex_marker(num_vertices(g), true); |
413 | | |
414 | | detail::write_graphviz_subgraph(out, g, vertex_marker.begin(), |
415 | | edge_marker.begin(), get(vertex_index, g)); |
416 | | } |
417 | | |
418 | | template < typename Graph > |
419 | | void write_graphviz(const std::string& filename, const subgraph< Graph >& g) |
420 | | { |
421 | | std::ofstream out(filename.c_str()); |
422 | | std::vector< bool > edge_marker(num_edges(g), true); |
423 | | std::vector< bool > vertex_marker(num_vertices(g), true); |
424 | | |
425 | | detail::write_graphviz_subgraph(out, g, vertex_marker.begin(), |
426 | | edge_marker.begin(), get(vertex_index, g)); |
427 | | } |
428 | | |
429 | | template < typename Graph, typename VertexID > |
430 | | void write_graphviz( |
431 | | std::ostream& out, const subgraph< Graph >& g, VertexID vertex_id) |
432 | | { |
433 | | std::vector< bool > edge_marker(num_edges(g), true); |
434 | | std::vector< bool > vertex_marker(num_vertices(g), true); |
435 | | |
436 | | detail::write_graphviz_subgraph( |
437 | | out, g, vertex_marker.begin(), edge_marker.begin(), vertex_id); |
438 | | } |
439 | | |
440 | | template < typename Graph, typename VertexID > |
441 | | void write_graphviz( |
442 | | const std::string& filename, const subgraph< Graph >& g, VertexID vertex_id) |
443 | | { |
444 | | std::ofstream out(filename.c_str()); |
445 | | std::vector< bool > edge_marker(num_edges(g), true); |
446 | | std::vector< bool > vertex_marker(num_vertices(g), true); |
447 | | |
448 | | detail::write_graphviz_subgraph( |
449 | | out, g, vertex_marker.begin(), edge_marker.begin(), vertex_id); |
450 | | } |
451 | | |
452 | | #if 0 |
453 | | // This interface has not worked for a long time |
454 | | typedef std::map<std::string, std::string> GraphvizAttrList; |
455 | | |
456 | | typedef property<vertex_attribute_t, GraphvizAttrList> |
457 | | GraphvizVertexProperty; |
458 | | |
459 | | typedef property<edge_attribute_t, GraphvizAttrList, |
460 | | property<edge_index_t, int> > |
461 | | GraphvizEdgeProperty; |
462 | | |
463 | | typedef property<graph_graph_attribute_t, GraphvizAttrList, |
464 | | property<graph_vertex_attribute_t, GraphvizAttrList, |
465 | | property<graph_edge_attribute_t, GraphvizAttrList, |
466 | | property<graph_name_t, std::string> > > > |
467 | | GraphvizGraphProperty; |
468 | | |
469 | | typedef subgraph<adjacency_list<vecS, |
470 | | vecS, directedS, |
471 | | GraphvizVertexProperty, |
472 | | GraphvizEdgeProperty, |
473 | | GraphvizGraphProperty> > |
474 | | GraphvizDigraph; |
475 | | |
476 | | typedef subgraph<adjacency_list<vecS, |
477 | | vecS, undirectedS, |
478 | | GraphvizVertexProperty, |
479 | | GraphvizEdgeProperty, |
480 | | GraphvizGraphProperty> > |
481 | | GraphvizGraph; |
482 | | |
483 | | // These four require linking the BGL-Graphviz library: libbgl-viz.a |
484 | | // from the /src directory. |
485 | | // Library has not existed for a while |
486 | | extern void read_graphviz(const std::string& file, GraphvizDigraph& g); |
487 | | extern void read_graphviz(FILE* file, GraphvizDigraph& g); |
488 | | |
489 | | extern void read_graphviz(const std::string& file, GraphvizGraph& g); |
490 | | extern void read_graphviz(FILE* file, GraphvizGraph& g); |
491 | | #endif |
492 | | |
493 | | class dynamic_properties_writer |
494 | | { |
495 | | public: |
496 | 0 | dynamic_properties_writer(const dynamic_properties& dp) : dp(&dp) {} |
497 | | |
498 | | template < typename Descriptor > |
499 | | void operator()(std::ostream& out, Descriptor key) const |
500 | | { |
501 | | bool first = true; |
502 | | for (dynamic_properties::const_iterator i = dp->begin(); i != dp->end(); |
503 | | ++i) |
504 | | { |
505 | | if (typeid(key) == i->second->key()) |
506 | | { |
507 | | if (first) |
508 | | out << " ["; |
509 | | else |
510 | | out << ", "; |
511 | | first = false; |
512 | | |
513 | | out << i->first << "=" |
514 | | << escape_dot_string(i->second->get_string(key)); |
515 | | } |
516 | | } |
517 | | |
518 | | if (!first) |
519 | | out << "]"; |
520 | | } |
521 | | |
522 | | private: |
523 | | const dynamic_properties* dp; |
524 | | }; |
525 | | |
526 | | class dynamic_vertex_properties_writer |
527 | | { |
528 | | public: |
529 | | dynamic_vertex_properties_writer( |
530 | | const dynamic_properties& dp, const std::string& node_id) |
531 | | : dp(&dp), node_id(&node_id) |
532 | 0 | { |
533 | 0 | } |
534 | | |
535 | | template < typename Descriptor > |
536 | | void operator()(std::ostream& out, Descriptor key) const |
537 | | { |
538 | | bool first = true; |
539 | | for (dynamic_properties::const_iterator i = dp->begin(); i != dp->end(); |
540 | | ++i) |
541 | | { |
542 | | if (typeid(key) == i->second->key() && i->first != *node_id) |
543 | | { |
544 | | if (first) |
545 | | out << " ["; |
546 | | else |
547 | | out << ", "; |
548 | | first = false; |
549 | | |
550 | | out << i->first << "=" |
551 | | << escape_dot_string(i->second->get_string(key)); |
552 | | } |
553 | | } |
554 | | |
555 | | if (!first) |
556 | | out << "]"; |
557 | | } |
558 | | |
559 | | private: |
560 | | const dynamic_properties* dp; |
561 | | const std::string* node_id; |
562 | | }; |
563 | | |
564 | | template < typename Graph > class dynamic_graph_properties_writer |
565 | | { |
566 | | public: |
567 | | dynamic_graph_properties_writer( |
568 | | const dynamic_properties& dp, const Graph& g) |
569 | | : g(&g), dp(&dp) |
570 | | { |
571 | | } |
572 | | |
573 | | void operator()(std::ostream& out) const |
574 | | { |
575 | | for (dynamic_properties::const_iterator i = dp->begin(); i != dp->end(); |
576 | | ++i) |
577 | | { |
578 | | if (typeid(Graph*) == i->second->key()) |
579 | | { |
580 | | // const_cast here is to match interface used in read_graphviz |
581 | | out << i->first << "=" |
582 | | << escape_dot_string( |
583 | | i->second->get_string(const_cast< Graph* >(g))) |
584 | | << ";\n"; |
585 | | } |
586 | | } |
587 | | } |
588 | | |
589 | | private: |
590 | | const Graph* g; |
591 | | const dynamic_properties* dp; |
592 | | }; |
593 | | |
594 | | namespace graph |
595 | | { |
596 | | namespace detail |
597 | | { |
598 | | |
599 | | template < typename Vertex > struct node_id_property_map |
600 | | { |
601 | | typedef std::string value_type; |
602 | | typedef value_type reference; |
603 | | typedef Vertex key_type; |
604 | | typedef readable_property_map_tag category; |
605 | | |
606 | | node_id_property_map() {} |
607 | | |
608 | | node_id_property_map( |
609 | | const dynamic_properties& dp, const std::string& node_id) |
610 | | : dp(&dp), node_id(&node_id) |
611 | | { |
612 | | } |
613 | | |
614 | | const dynamic_properties* dp; |
615 | | const std::string* node_id; |
616 | | }; |
617 | | |
618 | | template < typename Vertex > |
619 | | inline std::string get(node_id_property_map< Vertex > pm, |
620 | | typename node_id_property_map< Vertex >::key_type v) |
621 | | { |
622 | | return get(*pm.node_id, *pm.dp, v); |
623 | | } |
624 | | |
625 | | } |
626 | | } // end namespace graph::detail |
627 | | |
628 | | template < typename Graph > |
629 | | inline void write_graphviz_dp(std::ostream& out, const Graph& g, |
630 | | const dynamic_properties& dp, |
631 | | const std::string& node_id |
632 | | = "node_id" BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag)) |
633 | | { |
634 | | typedef typename graph_traits< Graph >::vertex_descriptor Vertex; |
635 | | write_graphviz_dp(out, g, dp, node_id, |
636 | | graph::detail::node_id_property_map< Vertex >(dp, node_id)); |
637 | | } |
638 | | |
639 | | template < typename Graph, typename VertexID > |
640 | | void write_graphviz_dp(std::ostream& out, const Graph& g, |
641 | | const dynamic_properties& dp, const std::string& node_id, |
642 | | VertexID id BOOST_GRAPH_ENABLE_IF_MODELS_PARM(Graph, vertex_list_graph_tag)) |
643 | | { |
644 | | write_graphviz(out, g, |
645 | | /*vertex_writer=*/dynamic_vertex_properties_writer(dp, node_id), |
646 | | /*edge_writer=*/dynamic_properties_writer(dp), |
647 | | /*graph_writer=*/dynamic_graph_properties_writer< Graph >(dp, g), id); |
648 | | } |
649 | | |
650 | | ///////////////////////////////////////////////////////////////////////////// |
651 | | // Graph reader exceptions |
652 | | ///////////////////////////////////////////////////////////////////////////// |
653 | | struct BOOST_SYMBOL_VISIBLE bad_graphviz_syntax : public graph_exception |
654 | | { |
655 | | std::string errmsg; |
656 | 4.28k | bad_graphviz_syntax(const std::string& errmsg) : errmsg(errmsg) {} |
657 | 0 | const char* what() const throw() BOOST_OVERRIDE { return errmsg.c_str(); } |
658 | 8.56k | ~bad_graphviz_syntax() throw() BOOST_OVERRIDE {} |
659 | | }; |
660 | | |
661 | | namespace detail |
662 | | { |
663 | | namespace graph |
664 | | { |
665 | | |
666 | | typedef std::string id_t; |
667 | | typedef id_t node_t; |
668 | | |
669 | | // edges are not uniquely determined by adjacent nodes |
670 | | class edge_t |
671 | | { |
672 | | int idx_; |
673 | 6.16M | explicit edge_t(int i) : idx_(i) {} |
674 | | |
675 | | public: |
676 | | static edge_t new_edge() |
677 | 6.16M | { |
678 | 6.16M | static int idx = 0; |
679 | 6.16M | return edge_t(idx++); |
680 | 6.16M | } |
681 | | |
682 | | bool operator==(const edge_t& rhs) const |
683 | 0 | { |
684 | 0 | return idx_ == rhs.idx_; |
685 | 0 | } |
686 | 431M | bool operator<(const edge_t& rhs) const { return idx_ < rhs.idx_; } |
687 | | }; |
688 | | |
689 | | class mutate_graph |
690 | | { |
691 | | public: |
692 | 5.31k | virtual ~mutate_graph() {} |
693 | | virtual bool is_directed() const = 0; |
694 | | virtual void do_add_vertex(const node_t& node) = 0; |
695 | | |
696 | | virtual void do_add_edge( |
697 | | const edge_t& edge, const node_t& source, const node_t& target) |
698 | | = 0; |
699 | | |
700 | | virtual void set_node_property( |
701 | | const id_t& key, const node_t& node, const id_t& value) |
702 | | = 0; |
703 | | |
704 | | virtual void set_edge_property( |
705 | | const id_t& key, const edge_t& edge, const id_t& value) |
706 | | = 0; |
707 | | |
708 | | virtual void // RG: need new second parameter to support BGL |
709 | | // subgraphs |
710 | | set_graph_property(const id_t& key, const id_t& value) |
711 | | = 0; |
712 | | |
713 | | virtual void finish_building_graph() = 0; |
714 | | }; |
715 | | |
716 | | template < typename MutableGraph > |
717 | | class mutate_graph_impl : public mutate_graph |
718 | | { |
719 | | typedef typename graph_traits< MutableGraph >::vertex_descriptor |
720 | | bgl_vertex_t; |
721 | | typedef typename graph_traits< MutableGraph >::edge_descriptor |
722 | | bgl_edge_t; |
723 | | |
724 | | public: |
725 | | mutate_graph_impl(MutableGraph& graph, dynamic_properties& dp, |
726 | | std::string node_id_prop) |
727 | 5.31k | : graph_(graph), dp_(dp), node_id_prop_(node_id_prop) |
728 | 5.31k | { |
729 | 5.31k | } |
730 | | |
731 | 5.31k | ~mutate_graph_impl() BOOST_OVERRIDE {} |
732 | | |
733 | | bool is_directed() const BOOST_OVERRIDE |
734 | 5.31k | { |
735 | 5.31k | return boost::is_convertible< |
736 | 5.31k | typename boost::graph_traits< |
737 | 5.31k | MutableGraph >::directed_category, |
738 | 5.31k | boost::directed_tag >::value; |
739 | 5.31k | } |
740 | | |
741 | | void do_add_vertex(const node_t& node) BOOST_OVERRIDE |
742 | 10.0k | { |
743 | | // Add the node to the graph. |
744 | 10.0k | bgl_vertex_t v = add_vertex(graph_); |
745 | | |
746 | | // Set up a mapping from name to BGL vertex. |
747 | 10.0k | bgl_nodes.insert(std::make_pair(node, v)); |
748 | | |
749 | | // node_id_prop_ allows the caller to see the real id names for |
750 | | // nodes. |
751 | 10.0k | put(node_id_prop_, dp_, v, node); |
752 | 10.0k | } |
753 | | |
754 | | void do_add_edge(const edge_t& edge, const node_t& source, |
755 | | const node_t& target) BOOST_OVERRIDE |
756 | 6.16M | { |
757 | 6.16M | std::pair< bgl_edge_t, bool > result |
758 | 6.16M | = add_edge(bgl_nodes[source], bgl_nodes[target], graph_); |
759 | | |
760 | 6.16M | if (!result.second) |
761 | 0 | { |
762 | | // In the case of no parallel edges allowed |
763 | 0 | boost::throw_exception(bad_parallel_edge(source, target)); |
764 | 0 | } |
765 | 6.16M | else |
766 | 6.16M | { |
767 | 6.16M | bgl_edges.insert(std::make_pair(edge, result.first)); |
768 | 6.16M | } |
769 | 6.16M | } |
770 | | |
771 | | void set_node_property(const id_t& key, const node_t& node, |
772 | | const id_t& value) BOOST_OVERRIDE |
773 | 7.31k | { |
774 | 7.31k | put(key, dp_, bgl_nodes[node], value); |
775 | 7.31k | } |
776 | | |
777 | | void set_edge_property(const id_t& key, const edge_t& edge, |
778 | | const id_t& value) BOOST_OVERRIDE |
779 | 219k | { |
780 | 219k | put(key, dp_, bgl_edges[edge], value); |
781 | 219k | } |
782 | | |
783 | | void set_graph_property(const id_t& key, |
784 | | const id_t& value) BOOST_OVERRIDE |
785 | 3.09k | { |
786 | | /* RG: pointer to graph prevents copying */ |
787 | 3.09k | put(key, dp_, &graph_, value); |
788 | 3.09k | } |
789 | | |
790 | 1.01k | void finish_building_graph() BOOST_OVERRIDE {} |
791 | | |
792 | | protected: |
793 | | MutableGraph& graph_; |
794 | | dynamic_properties& dp_; |
795 | | std::string node_id_prop_; |
796 | | std::map< node_t, bgl_vertex_t > bgl_nodes; |
797 | | std::map< edge_t, bgl_edge_t > bgl_edges; |
798 | | }; |
799 | | |
800 | | template < typename Directed, typename VertexProperty, |
801 | | typename EdgeProperty, typename GraphProperty, typename Vertex, |
802 | | typename EdgeIndex > |
803 | | class mutate_graph_impl< compressed_sparse_row_graph< Directed, |
804 | | VertexProperty, EdgeProperty, GraphProperty, Vertex, EdgeIndex > > |
805 | | : public mutate_graph |
806 | | { |
807 | | typedef compressed_sparse_row_graph< Directed, VertexProperty, |
808 | | EdgeProperty, GraphProperty, Vertex, EdgeIndex > |
809 | | CSRGraph; |
810 | | typedef typename graph_traits< CSRGraph >::vertices_size_type |
811 | | bgl_vertex_t; |
812 | | typedef |
813 | | typename graph_traits< CSRGraph >::edges_size_type bgl_edge_t; |
814 | | typedef typename graph_traits< CSRGraph >::edge_descriptor |
815 | | edge_descriptor; |
816 | | |
817 | | public: |
818 | | mutate_graph_impl(CSRGraph& graph, dynamic_properties& dp, |
819 | | std::string node_id_prop) |
820 | | : graph_(graph) |
821 | | , dp_(dp) |
822 | | , vertex_count(0) |
823 | | , node_id_prop_(node_id_prop) |
824 | | { |
825 | | } |
826 | | |
827 | | ~mutate_graph_impl() BOOST_OVERRIDE {} |
828 | | |
829 | | void finish_building_graph() BOOST_OVERRIDE |
830 | | { |
831 | | typedef compressed_sparse_row_graph< directedS, no_property, |
832 | | bgl_edge_t, GraphProperty, Vertex, EdgeIndex > |
833 | | TempCSRGraph; |
834 | | TempCSRGraph temp(edges_are_unsorted_multi_pass, |
835 | | edges_to_add.begin(), edges_to_add.end(), |
836 | | counting_iterator< bgl_edge_t >(0), vertex_count); |
837 | | set_property(temp, graph_all, get_property(graph_, graph_all)); |
838 | | graph_.assign(temp); // Copies structure, not properties |
839 | | std::vector< edge_descriptor > edge_permutation_from_sorting( |
840 | | num_edges(temp)); |
841 | | BGL_FORALL_EDGES_T(e, temp, TempCSRGraph) |
842 | | { |
843 | | edge_permutation_from_sorting[temp[e]] = e; |
844 | | } |
845 | | typedef boost::tuple< id_t, bgl_vertex_t, id_t > v_prop; |
846 | | BOOST_FOREACH (const v_prop& t, vertex_props) |
847 | | { |
848 | | put(boost::get< 0 >(t), dp_, boost::get< 1 >(t), |
849 | | boost::get< 2 >(t)); |
850 | | } |
851 | | typedef boost::tuple< id_t, bgl_edge_t, id_t > e_prop; |
852 | | BOOST_FOREACH (const e_prop& t, edge_props) |
853 | | { |
854 | | put(boost::get< 0 >(t), dp_, |
855 | | edge_permutation_from_sorting[boost::get< 1 >(t)], |
856 | | boost::get< 2 >(t)); |
857 | | } |
858 | | } |
859 | | |
860 | | bool is_directed() const BOOST_OVERRIDE |
861 | | { |
862 | | return boost::is_convertible< |
863 | | typename boost::graph_traits< CSRGraph >::directed_category, |
864 | | boost::directed_tag >::value; |
865 | | } |
866 | | |
867 | | void do_add_vertex(const node_t& node) BOOST_OVERRIDE |
868 | | { |
869 | | // Add the node to the graph. |
870 | | bgl_vertex_t v = vertex_count++; |
871 | | |
872 | | // Set up a mapping from name to BGL vertex. |
873 | | bgl_nodes.insert(std::make_pair(node, v)); |
874 | | |
875 | | // node_id_prop_ allows the caller to see the real id names for |
876 | | // nodes. |
877 | | vertex_props.push_back( |
878 | | boost::make_tuple(node_id_prop_, v, node)); |
879 | | } |
880 | | |
881 | | void do_add_edge(const edge_t& edge, const node_t& source, |
882 | | const node_t& target) BOOST_OVERRIDE |
883 | | { |
884 | | bgl_edge_t result = edges_to_add.size(); |
885 | | edges_to_add.push_back( |
886 | | std::make_pair(bgl_nodes[source], bgl_nodes[target])); |
887 | | bgl_edges.insert(std::make_pair(edge, result)); |
888 | | } |
889 | | |
890 | | void set_node_property(const id_t& key, const node_t& node, |
891 | | const id_t& value) BOOST_OVERRIDE |
892 | | { |
893 | | vertex_props.push_back( |
894 | | boost::make_tuple(key, bgl_nodes[node], value)); |
895 | | } |
896 | | |
897 | | void set_edge_property(const id_t& key, const edge_t& edge, |
898 | | const id_t& value) BOOST_OVERRIDE |
899 | | { |
900 | | edge_props.push_back( |
901 | | boost::make_tuple(key, bgl_edges[edge], value)); |
902 | | } |
903 | | |
904 | | void set_graph_property(const id_t& key, |
905 | | const id_t& value) BOOST_OVERRIDE |
906 | | { |
907 | | /* RG: pointer to graph prevents copying */ |
908 | | put(key, dp_, &graph_, value); |
909 | | } |
910 | | |
911 | | protected: |
912 | | CSRGraph& graph_; |
913 | | dynamic_properties& dp_; |
914 | | bgl_vertex_t vertex_count; |
915 | | std::string node_id_prop_; |
916 | | std::vector< boost::tuple< id_t, bgl_vertex_t, id_t > > |
917 | | vertex_props; |
918 | | std::vector< boost::tuple< id_t, bgl_edge_t, id_t > > edge_props; |
919 | | std::vector< std::pair< bgl_vertex_t, bgl_vertex_t > > edges_to_add; |
920 | | std::map< node_t, bgl_vertex_t > bgl_nodes; |
921 | | std::map< edge_t, bgl_edge_t > bgl_edges; |
922 | | }; |
923 | | |
924 | | } |
925 | | } |
926 | | } // end namespace boost::detail::graph |
927 | | |
928 | | #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER |
929 | | #ifndef BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS |
930 | | #define BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS |
931 | | #endif |
932 | | #include <boost/graph/detail/read_graphviz_spirit.hpp> |
933 | | #else // New default parser |
934 | | #include <boost/graph/detail/read_graphviz_new.hpp> |
935 | | #endif // BOOST_GRAPH_USE_SPIRIT_PARSER |
936 | | |
937 | | namespace boost |
938 | | { |
939 | | |
940 | | // Parse the passed string as a GraphViz dot file. |
941 | | template < typename MutableGraph > |
942 | | bool read_graphviz(const std::string& data, MutableGraph& graph, |
943 | | dynamic_properties& dp, std::string const& node_id = "node_id") |
944 | 5.31k | { |
945 | | #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER |
946 | | return read_graphviz_spirit(data.begin(), data.end(), graph, dp, node_id); |
947 | | #else // Non-Spirit parser |
948 | 5.31k | return read_graphviz_new(data, graph, dp, node_id); |
949 | 5.31k | #endif |
950 | 5.31k | } |
951 | | |
952 | | // Parse the passed iterator range as a GraphViz dot file. |
953 | | template < typename InputIterator, typename MutableGraph > |
954 | | bool read_graphviz(InputIterator user_first, InputIterator user_last, |
955 | | MutableGraph& graph, dynamic_properties& dp, |
956 | | std::string const& node_id = "node_id") |
957 | | { |
958 | | #ifdef BOOST_GRAPH_USE_SPIRIT_PARSER |
959 | | typedef InputIterator is_t; |
960 | | typedef boost::spirit::classic::multi_pass< is_t > iterator_t; |
961 | | |
962 | | iterator_t first(boost::spirit::classic::make_multi_pass(user_first)); |
963 | | iterator_t last(boost::spirit::classic::make_multi_pass(user_last)); |
964 | | |
965 | | return read_graphviz_spirit(first, last, graph, dp, node_id); |
966 | | #else // Non-Spirit parser |
967 | | return read_graphviz_new( |
968 | | std::string(user_first, user_last), graph, dp, node_id); |
969 | | #endif |
970 | | } |
971 | | |
972 | | // Parse the passed stream as a GraphViz dot file. |
973 | | template < typename MutableGraph > |
974 | | bool read_graphviz(std::istream& in, MutableGraph& graph, |
975 | | dynamic_properties& dp, std::string const& node_id = "node_id") |
976 | | { |
977 | | typedef std::istream_iterator< char > is_t; |
978 | | in >> std::noskipws; |
979 | | return read_graphviz(is_t(in), is_t(), graph, dp, node_id); |
980 | | } |
981 | | |
982 | | } // namespace boost |
983 | | |
984 | | #include BOOST_GRAPH_MPI_INCLUDE(<boost/graph/distributed/graphviz.hpp>) |
985 | | |
986 | | #endif // BOOST_GRAPHVIZ_HPP |