1 // Copyright 2008 Trustees of Indiana University
2
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6
7 // An example of using read_graphviz to load a GraphViz Dot textual
8 // graph into a BGL adjacency_list graph that has custom properties.
9
10 // Author: Ronald Garcia
11
12
13
14 #include <boost/graph/graphviz.hpp>
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/foreach.hpp>
17 #include <string>
18 #include <sstream>
19
20 using namespace boost;
21 using namespace std;
22
23
24 //
25 // Create a custom graph properties
26 // (see the documentation for adjacency list)
27 struct graph_identifier_t { typedef graph_property_tag kind; };
28 struct vertex_label_t { typedef vertex_property_tag kind; };
29
main()30 int main() {
31
32 // Vertex properties
33 typedef property < vertex_name_t, string,
34 property < vertex_label_t, string,
35 property < vertex_root_t, int > > > vertex_p;
36 // Edge properties
37 typedef property < edge_name_t, string > edge_p;
38 // Graph properties
39 typedef property < graph_name_t, string,
40 property < graph_identifier_t, string > > graph_p;
41 // adjacency_list-based type
42 typedef adjacency_list < vecS, vecS, directedS,
43 vertex_p, edge_p, graph_p > graph_t;
44
45 // Construct an empty graph and prepare the dynamic_property_maps.
46 graph_t graph(0);
47 dynamic_properties dp;
48
49 property_map<graph_t, vertex_name_t>::type vname =
50 get(vertex_name, graph);
51 dp.property("node_id",vname);
52
53 property_map<graph_t, vertex_label_t>::type vlabel =
54 get(vertex_label_t(), graph);
55 dp.property("label",vlabel);
56
57 property_map<graph_t, vertex_root_t>::type root =
58 get(vertex_root, graph);
59 dp.property("root",root);
60
61 property_map<graph_t, edge_name_t>::type elabel =
62 get(edge_name, graph);
63 dp.property("label",elabel);
64
65 // Use ref_property_map to turn a graph property into a property map
66 ref_property_map<graph_t*,string>
67 gname(get_property(graph,graph_name));
68 dp.property("name",gname);
69
70 // Use ref_property_map to turn a graph property into a property map
71 ref_property_map<graph_t*,string>
72 gid(get_property(graph,graph_identifier_t()));
73 dp.property("identifier",gid);
74 // Sample graph as an istream;
75
76 const char* dot =
77 "digraph \
78 { \
79 graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
80 \
81 a [label=\"NODE_A\", root=\"1\"] \
82 b [label=\"NODE_B\", root=\"0\"] \
83 \
84 a -> b [label=\"EDGE_1\"] \
85 b -> c [label=\"EDGE_2\"] \
86 }";
87
88
89 istringstream gvgraph(dot);
90
91 bool status = read_graphviz(gvgraph,graph,dp,"node_id");
92
93 cout << "graph " << get("name",dp,&graph) <<
94 " (" << get("identifier",dp,&graph) << ")\n\n";
95
96 BOOST_FOREACH( graph_t::vertex_descriptor v, vertices(graph) ) {
97 cout << "vertex " << get("node_id",dp,v) <<
98 " (" << get("label",dp,v) << ")\n";
99 }
100
101 return 0;
102 }
103