Vaucanson 1.4
|
00001 // dot_format.hxx: this file is part of the Vaucanson project. 00002 // 00003 // Vaucanson, a generic library for finite state machines. 00004 // 00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group. 00006 // 00007 // This program is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 00010 // of the License, or (at your option) any later version. 00011 // 00012 // The complete GNU General Public Licence Notice can be found as the 00013 // `COPYING' file in the root directory. 00014 // 00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file. 00016 // 00017 #ifndef VCSN_TOOLS_DOT_FORMAT_HXX 00018 # define VCSN_TOOLS_DOT_FORMAT_HXX 00019 00020 # include <sstream> 00021 # include <map> 00022 00023 # include <vaucanson/tools/dot_format.hh> 00024 # include <vaucanson/automata/concept/handlers.hh> 00025 # include <vaucanson/misc/usual_macros.hh> 00026 00027 namespace vcsn 00028 { 00029 namespace tools 00030 { 00031 void 00032 name_escaper(std::ostream& out, const std::string& name) 00033 { 00034 for (std::string::const_iterator i = name.begin(); i != name.end(); ++i) 00035 { 00036 if (*i == '"') 00037 out << "\\"; 00038 out << *i; 00039 } 00040 } 00041 00042 inline dot::dot(const std::string& name) 00043 { 00044 std::ostringstream os; 00045 name_escaper(os, name); 00046 name_ = std::string("\"") + os.str(); 00047 } 00048 00049 template<typename Saver, typename Conv> 00050 void dot::operator()(std::ostream& out, const Saver& s, 00051 const Conv& conv) const 00052 { 00053 typedef typename Saver::automaton_t auto_t; 00054 typedef typename auto_t::hstate_t hstate_t; 00055 00056 const auto_t& a = s.automaton(); 00057 unsigned count = 0; 00058 std::map<hstate_t, unsigned> state_map; 00059 00060 out << "digraph vcsn {" << std::endl 00061 << "label=\"" << name_.c_str() + 1 << ' ' << a << "\";" << std::endl 00062 << "node [shape=circle];" << std::endl; 00063 00064 for (typename auto_t::state_iterator i = a.states().begin(); 00065 i != a.states().end(); 00066 ++i) 00067 { 00068 unsigned c = state_map[*i] = count++; 00069 if (a.is_initial(*i)) 00070 { 00071 out << name_ << count 00072 << "\" [style=invis,label=\"\",width=.01,height=.01];" 00073 << std::endl 00074 << name_ << count << "\" -> " << name_ << c 00075 << "\" [label=\"" << conv(a.structure(), a.get_initial(*i)) 00076 << "\"];" << std::endl; 00077 ++count; 00078 } 00079 if (a.is_final(*i)) 00080 { 00081 out << name_ << count 00082 << "\" [style=invis,label=\"\",width=.01,height=.01];" 00083 << std::endl 00084 << name_ << c << "\" -> " << name_ << count 00085 << "\" [label=\""<< conv(a.structure(), a.get_final(*i)) 00086 <<"\"];" << std::endl; 00087 ++count; 00088 } 00089 out << name_ << c << "\" [label=\"" << *i << "\"];" << std::endl; 00090 } 00091 for (typename auto_t::transition_iterator i = a.transitions().begin(); 00092 i != a.transitions().end(); 00093 ++i) 00094 { 00095 out << name_ << state_map[a.src_of(*i)] 00096 << "\" -> " 00097 << name_ << state_map[a.dst_of(*i)]; 00098 out << "\" [label=\"" << conv(a.structure(), a.series_of(*i)) 00099 << "\"];" << std::endl; 00100 } 00101 out << "}" << std::endl; 00102 } 00103 00104 inline transducer_dot::transducer_dot(const std::string& name) 00105 { 00106 std::ostringstream os; 00107 name_escaper(os, name); 00108 name_ = std::string("\"") + os.str(); 00109 } 00110 00111 template<typename Saver, typename Conv> 00112 void transducer_dot::operator()(std::ostream& out, const Saver& s, 00113 const Conv& conv) const 00114 { 00115 typedef typename Saver::automaton_t auto_t; 00116 AUTOMATON_TYPES(auto_t); 00117 const auto_t& a = s.automaton(); 00118 unsigned count = 0; 00119 std::map<hstate_t, unsigned> state_map; 00120 00121 out << "digraph vcsn {" << std::endl 00122 << "label=\"" << name_.c_str() + 1 << ' ' << a << "\";" << std::endl 00123 << "node [shape=circle];" << std::endl; 00124 00125 for (typename auto_t::state_iterator i = a.states().begin(); 00126 i != a.states().end(); 00127 ++i) 00128 { 00129 unsigned c = state_map[*i] = count++; 00130 if (a.is_initial(*i)) 00131 { 00132 out << name_ << count 00133 << "\" [style=invis,label=\"\",width=.01,height=.01];" 00134 << std::endl 00135 << name_ << count << "\" -> " << name_ << c << '"'; 00136 std::ostringstream o; 00137 series_set_elt_t ss = a.get_initial(*i); 00138 if (ss.supp().begin() == ss.supp().end()) 00139 o << ss; 00140 for_all_const_(series_set_elt_t::support_t, s, ss.supp()) 00141 { 00142 monoid_elt_t x(a.structure().series().monoid(), *s); 00143 o << conv(a.structure(), x) << "|" << ss.get(x) << " "; 00144 } 00145 out << "[label=\"" << o.str() << "\"];" 00146 << std::endl; 00147 ++count; 00148 } 00149 if (a.is_final(*i)) 00150 { 00151 out << name_ << count 00152 << "\" [style=invis,label=\"\",width=.01,height=.01];" 00153 << std::endl 00154 << name_ << c << "\" -> " << name_ << count << '"'; 00155 std::ostringstream o; 00156 series_set_elt_t ss = a.get_final(*i); 00157 if (ss.supp().begin() == ss.supp().end()) 00158 o << ss; 00159 for_all_const_(series_set_elt_t::support_t, s, ss.supp()) 00160 { 00161 monoid_elt_t x(a.structure().series().monoid(), *s); 00162 o << conv(a.structure(), x) << "|" << ss.get(x) << " "; 00163 } 00164 out << "[label=\"" << o.str() << "\"];" 00165 << std::endl; 00166 ++count; 00167 } 00168 out << name_ << c << "\" [label=\"" << *i << "\"];" << std::endl; 00169 } 00170 for (typename auto_t::transition_iterator i = a.transitions().begin(); 00171 i != a.transitions().end(); 00172 ++i) 00173 { 00174 out << name_ << state_map[a.src_of(*i)] 00175 << "\" -> " 00176 << name_ << state_map[a.dst_of(*i)] << '"'; 00177 std::ostringstream o; 00178 series_set_elt_t ss = a.series_of(*i); 00179 if (ss.supp().begin() == ss.supp().end()) 00180 o << ss; 00181 for_all_const_(series_set_elt_t::support_t, s, ss.supp()) 00182 { 00183 monoid_elt_t x(a.structure().series().monoid(), *s); 00184 o << conv(a.structure(), x) << "|" << ss.get(x) << " "; 00185 } 00186 out << "[label=\"" << o.str() << "\"];" 00187 << std::endl; 00188 } 00189 out << "}" << std::endl; 00190 } 00191 00192 } // ! tools 00193 00194 } // ! vcsn 00195 00196 #endif // ! VCSN_TOOLS_DOT_FORMAT_HXX