read_graphviztemplate <typename MutableGraph> bool read_graphviz(std::istream& in, MutableGraph& graph, dynamic_properties& dp, const std::string& node_id = "node_id"); // Only available if BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS is defined template <typename MultiPassIterator, typename MutableGraph> bool read_graphviz(MultiPassIterator begin, MultiPassIterator end, MutableGraph& graph, dynamic_properties& dp, const std::string& node_id = "node_id"); // Deprecated GraphViz readers void read_graphviz(const std::string& file, GraphvizDigraph& g); void read_graphviz(FILE* file, GraphvizDigraph& g); void read_graphviz(const std::string& file, GraphvizGraph& g); void read_graphviz(FILE* file, GraphvizGraph& g); The read_graphviz function interprets a graph described using the GraphViz DOT language and builds a BGL graph that captures that description. Using this function, you can initialize a graph using data stored as text. To use the iterator version of read_graphviz, you will need to define the macro BOOST_GRAPH_READ_GRAPHVIZ_ITERATORS before including the header <boost/graph/graphviz.hpp>. Doing so may greatly increase the amount of time required to compile the GraphViz reader. The DOT language can specify both directed and undirected graphs, and read_graphviz differentiates between the two. One must pass read_graphviz an undirected graph when reading an undirected graph; the same is true for directed graphs. Furthermore, read_graphviz will throw an exception if it encounters parallel edges and cannot add them to the graph. To handle properties expressed in the DOT language, read_graphviz takes a dynamic_properties object and operates on its collection of property maps. The reader passes all the properties encountered to this object, using the GraphViz string keys as the property keys. Furthermore, read_graphviz stores node identifier names under the vertex property map named node_id.
Where Defined<boost/graph/graphviz.hpp> Exceptionsstruct graph_exception : public std::exception { virtual ~graph_exception() throw(); virtual const char* what() const throw() = 0; }; struct bad_parallel_edge : public graph_exception { std::string from; std::string to; bad_parallel_edge(const std::string&, const std::string&); virtual ~bad_parallel_edge() throw(); const char* what() const throw(); }; struct directed_graph_error : public graph_exception { virtual ~directed_graph_error() throw(); virtual const char* what() const throw(); }; struct undirected_graph_error : public graph_exception { virtual ~undirected_graph_error() throw(); virtual const char* what() const throw(); }; Under certain circumstances, read_graphviz will throw one of the above exceptions. The three concrete exceptions can all be caught using the general graph_exception moniker when greater precision is not needed. In addition, all of the above exceptions derive from the standard std::exception for even more generalized error handling. The bad_parallel_edge exception is thrown when an attempt to add a parallel edge to the supplied MutableGraph fails. The DOT language supports parallel edges, but some BGL-compatible graph types do not. One example of such a graph is boost::adjacency_list<setS,vecS>, which allows at most one edge can between any two vertices. The directed_graph_error exception occurs when an undirected graph type is passed to read_graph but the textual representation of the graph is directed, as indicated by the digraph keyword in the DOT language. The undirected_graph_error exception occurs when a directed graph type is passed to read_graph but the textual representation of the graph is undirected, as indicated by the graph keyword in the DOT language. ExampleThe following example illustrates a relatively simple use of the GraphViz reader to populate an adjacency_list graph // Vertex properties typedef property < vertex_name_t, std::string, property < vertex_color_t, float > > vertex_p; // Edge properties typedef property < edge_weight_t, double > edge_p; // Graph properties typedef property < graph_name_t, std::string > graph_p; // adjacency_list-based type typedef adjacency_list < vecS, vecS, directedS, vertex_p, edge_p, graph_p > graph_t; // Construct an empty graph and prepare the dynamic_property_maps. graph_t graph(0); dynamic_properties dp; property_map<graph_t, vertex_name_t>::type name = get(vertex_name, graph); dp.property("node_id",name); property_map<graph_t, vertex_color_t>::type mass = get(vertex_color, graph); dp.property("mass",mass); property_map<graph_t, edge_weight_t>::type weight = get(edge_weight, graph); dp.property("weight",weight); // Use ref_property_map to turn a graph property into a property map boost::ref_property_map<graph_t*,std::string> gname(get_property(graph,graph_name)); dp.property("name",gname); // Sample graph as an std::istream; std::istringstream gvgraph("digraph { graph [name=\"graphname\"] a c e [mass = 6.66] }"); bool status = read_graphviz(gvgraph,graph,dp,"node_id"); Building the GraphViz ReadersTo use the GraphViz readers, you will need to build and link against the "boost_graph" library. The library can be built by following the Boost Jam Build Instructions for the subdirectory libs/graph/build. Deprecated ReadersThe deprecated readers do not provide exceptions on error (they abort), they require the use of one of the predefined graph types (GraphvizDigraph or GraphvizGraph), and they do not support arbitrary properties. They will be removed in a future Boost version. Notes
Future Work
|