]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/graph/example/graph-thingie.cpp
1 // Copyright 2008 Trustees of Indiana University
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)
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.
10 // Author: Ronald Garcia
12 #include <boost/graph/graphviz.hpp>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/foreach.hpp>
18 using namespace boost
;
22 // Create a custom graph properties
23 // (see the documentation for adjacency list)
24 struct graph_identifier_t
26 typedef graph_property_tag kind
;
30 typedef vertex_property_tag kind
;
37 typedef property
< vertex_name_t
, string
,
38 property
< vertex_label_t
, string
, property
< vertex_root_t
, int > > >
41 typedef property
< edge_name_t
, string
> edge_p
;
43 typedef property
< graph_name_t
, string
,
44 property
< graph_identifier_t
, string
> >
46 // adjacency_list-based type
47 typedef adjacency_list
< vecS
, vecS
, directedS
, vertex_p
, edge_p
, graph_p
>
50 // Construct an empty graph and prepare the dynamic_property_maps.
52 dynamic_properties dp
;
54 property_map
< graph_t
, vertex_name_t
>::type vname
55 = get(vertex_name
, graph
);
56 dp
.property("node_id", vname
);
58 property_map
< graph_t
, vertex_label_t
>::type vlabel
59 = get(vertex_label_t(), graph
);
60 dp
.property("label", vlabel
);
62 property_map
< graph_t
, vertex_root_t
>::type root
= get(vertex_root
, graph
);
63 dp
.property("root", root
);
65 property_map
< graph_t
, edge_name_t
>::type elabel
= get(edge_name
, graph
);
66 dp
.property("label", elabel
);
68 // Use ref_property_map to turn a graph property into a property map
69 ref_property_map
< graph_t
*, string
> gname(get_property(graph
, graph_name
));
70 dp
.property("name", gname
);
72 // Use ref_property_map to turn a graph property into a property map
73 ref_property_map
< graph_t
*, string
> gid(
74 get_property(graph
, graph_identifier_t()));
75 dp
.property("identifier", gid
);
76 // Sample graph as an istream;
78 const char* dot
= "digraph \
80 graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
82 a [label=\"NODE_A\", root=\"1\"] \
83 b [label=\"NODE_B\", root=\"0\"] \
85 a -> b [label=\"EDGE_1\"] \
86 b -> c [label=\"EDGE_2\"] \
89 istringstream
gvgraph(dot
);
91 bool status
= read_graphviz(gvgraph
, graph
, dp
, "node_id");
94 cerr
<< "read_graphviz() failed." << endl
;
98 cout
<< "graph " << get("name", dp
, &graph
) << " ("
99 << get("identifier", dp
, &graph
) << ")\n\n";
101 BOOST_FOREACH (graph_t::vertex_descriptor v
, vertices(graph
))
103 cout
<< "vertex " << get("node_id", dp
, v
) << " ("
104 << get("label", dp
, v
) << ")\n";