• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2004-9 Trustees of Indiana University
2 
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 //
8 // read_graphviz_new.hpp -
9 //   Initialize a model of the BGL's MutableGraph concept and an associated
10 //  collection of property maps using a graph expressed in the GraphViz
11 // DOT Language.
12 //
13 //   Based on the grammar found at:
14 //   https://web.archive.org/web/20041213234742/http://www.graphviz.org/cvs/doc/info/lang.html
15 //
16 //   Jeremiah rewrite used grammar found at:
17 //   http://www.graphviz.org/doc/info/lang.html
18 //   and page 34 or http://www.graphviz.org/pdf/dotguide.pdf
19 //
20 //   See documentation for this code at:
21 //     http://www.boost.org/libs/graph/doc/read_graphviz.html
22 //
23 
24 // Author: Jeremiah Willcock
25 //         Ronald Garcia
26 //
27 
28 #ifndef BOOST_READ_GRAPHVIZ_NEW_HPP
29 #define BOOST_READ_GRAPHVIZ_NEW_HPP
30 
31 #include <boost/ref.hpp>
32 #include <boost/property_map/dynamic_property_map.hpp>
33 #include <boost/graph/graph_traits.hpp>
34 #include <boost/detail/workaround.hpp>
35 #include <algorithm>
36 #include <string>
37 #include <vector>
38 #include <set>
39 #include <utility>
40 #include <map>
41 #include <iostream>
42 #include <cstdlib>
43 
44 namespace boost
45 {
46 
47 namespace read_graphviz_detail
48 {
49     typedef std::string node_name;
50     typedef std::string subgraph_name;
51 
52     typedef std::map< std::string, std::string > properties;
53 
54     struct node_and_port
55     {
56         node_name name;
57         std::string angle; // Or empty if no angle
58         std::vector< std::string > location; // Up to two identifiers
59 
operator ==(const node_and_port & a,const node_and_port & b)60         friend inline bool operator==(
61             const node_and_port& a, const node_and_port& b)
62         {
63             return a.name == b.name && a.angle == b.angle
64                 && a.location == b.location;
65         }
66 
operator <(const node_and_port & a,const node_and_port & b)67         friend inline bool operator<(
68             const node_and_port& a, const node_and_port& b)
69         {
70             if (a.name != b.name)
71                 return a.name < b.name;
72             if (a.angle != b.angle)
73                 return a.angle < b.angle;
74             return a.location < b.location;
75         }
76     };
77 
78     struct edge_info
79     {
80         node_and_port source;
81         node_and_port target;
82         properties props;
83     };
84 
85     struct parser_result
86     {
87         bool graph_is_directed;
88         bool graph_is_strict;
89         std::map< node_name, properties > nodes; // Global set
90         std::vector< edge_info > edges;
91         std::map< subgraph_name, properties > graph_props; // Root and subgraphs
92     };
93 
94     // The actual parser, from libs/graph/src/read_graphviz_new.cpp
95     void parse_graphviz_from_string(
96         const std::string& str, parser_result& result, bool want_directed);
97 
98     // Translate from those results to a graph
99     void translate_results_to_graph(
100         const parser_result& r, ::boost::detail::graph::mutate_graph* mg);
101 
102 } // namespace read_graphviz_detail
103 
104 namespace detail
105 {
106     namespace graph
107     {
108         BOOST_GRAPH_DECL bool read_graphviz_new(
109             const std::string& str, boost::detail::graph::mutate_graph* mg);
110     } // end namespace graph
111 } // end namespace detail
112 
113 template < typename MutableGraph >
read_graphviz_new(const std::string & str,MutableGraph & graph,boost::dynamic_properties & dp,std::string const & node_id="node_id")114 bool read_graphviz_new(const std::string& str, MutableGraph& graph,
115     boost::dynamic_properties& dp, std::string const& node_id = "node_id")
116 {
117     boost::detail::graph::mutate_graph_impl< MutableGraph > mg(
118         graph, dp, node_id);
119     return detail::graph::read_graphviz_new(str, &mg);
120 }
121 
122 } // namespace boost
123 
124 #endif // BOOST_READ_GRAPHVIZ_NEW_HPP
125