00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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/misc/escaper.hh>
00024 # include <vaucanson/tools/dot_format.hh>
00025 # include <vaucanson/automata/concept/handlers.hh>
00026 # include <vaucanson/misc/usual_macros.hh>
00027
00028 namespace vcsn
00029 {
00030 namespace io
00031 {
00032 inline dot::dot(const std::string& name)
00033 {
00034 std::ostringstream os;
00035 std::set<char> to_escape;
00036 to_escape.insert('"');
00037 (misc::setesc(to_escape))(os) << misc::escaper<std::string>(name);
00038 name_ = std::string("\"") + os.str();
00039 }
00040
00041 template<typename Saver, typename Conv>
00042 void dot::operator()(std::ostream& out, const Saver& s,
00043 const Conv& conv) const
00044 {
00045 typedef typename Saver::automaton_t auto_t;
00046 const auto_t& a = s.automaton();
00047 unsigned count = 0;
00048 std::map<hstate_t, unsigned> state_map;
00049
00050 out << "digraph vcsn {" << std::endl
00051 << "label=\"" << name_.c_str() + 1 << ' ' << a << "\";" << std::endl
00052 << "node [shape=circle];" << std::endl;
00053
00054 for (typename auto_t::state_iterator i = a.states().begin();
00055 i != a.states().end();
00056 ++i)
00057 {
00058 unsigned c = state_map[*i] = count++;
00059 if (a.is_initial(*i))
00060 {
00061 out << name_ << count
00062 << "\" [style=invis,label=\"\",width=.01,height=.01];"
00063 << std::endl
00064 << name_ << count << "\" -> " << name_ << c
00065 << "\" [label=\"" << conv(a.structure(), a.get_initial(*i))
00066 << "\"];" << std::endl;
00067 ++count;
00068 }
00069 if (a.is_final(*i))
00070 {
00071 out << name_ << count
00072 << "\" [style=invis,label=\"\",width=.01,height=.01];"
00073 << std::endl
00074 << name_ << c << "\" -> " << name_ << count
00075 << "\" [label=\""<< conv(a.structure(), a.get_final(*i))
00076 <<"\"];" << std::endl;
00077 ++count;
00078 }
00079 out << name_ << c << "\" [label=\"" << *i << "\"];" << std::endl;
00080 }
00081 for (typename auto_t::transition_iterator i = a.transitions().begin();
00082 i != a.transitions().end();
00083 ++i)
00084 {
00085 out << name_ << state_map[a.src_of(*i)]
00086 << "\" -> "
00087 << name_ << state_map[a.dst_of(*i)];
00088 out << "\" [label=\"" << conv(a.structure(), a.series_of(*i))
00089 << "\"];" << std::endl;
00090 }
00091 out << "}" << std::endl;
00092 }
00093
00094 inline transducer_dot::transducer_dot(const std::string& name)
00095 {
00096 std::ostringstream os;
00097 std::set<char> to_escape;
00098 to_escape.insert('"');
00099 (misc::setesc(to_escape))(os) << misc::escaper<std::string>(name);
00100 name_ = std::string("\"") + os.str();
00101 }
00102
00103 template<typename Saver, typename Conv>
00104 void transducer_dot::operator()(std::ostream& out, const Saver& s,
00105 const Conv& conv) const
00106 {
00107 typedef typename Saver::automaton_t auto_t;
00108 AUTOMATON_TYPES(auto_t);
00109 const auto_t& a = s.automaton();
00110 unsigned count = 0;
00111 std::map<hstate_t, unsigned> state_map;
00112
00113 out << "digraph vcsn {" << std::endl
00114 << "label=\"" << name_.c_str() + 1 << ' ' << a << "\";" << std::endl
00115 << "node [shape=circle];" << std::endl;
00116
00117 for (typename auto_t::state_iterator i = a.states().begin();
00118 i != a.states().end();
00119 ++i)
00120 {
00121 unsigned c = state_map[*i] = count++;
00122 if (a.is_initial(*i))
00123 {
00124 out << name_ << count
00125 << "\" [style=invis,label=\"\",width=.01,height=.01];"
00126 << std::endl
00127 << name_ << count << "\" -> " << name_ << c << '"';
00128 std::ostringstream o;
00129 series_set_elt_t ss = a.get_initial(*i);
00130 for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00131 o << conv(a.structure(), *s) << "|"
00132 << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00133 << " ";
00134 out << "[label=\"" << o.str() << "\"];"
00135 << std::endl;
00136 ++count;
00137 }
00138 if (a.is_final(*i))
00139 {
00140 out << name_ << count
00141 << "\" [style=invis,label=\"\",width=.01,height=.01];"
00142 << std::endl
00143 << name_ << c << "\" -> " << name_ << count << '"';
00144 std::ostringstream o;
00145 series_set_elt_t ss = a.get_final(*i);
00146 for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00147 o << conv(a.structure(), *s) << "|"
00148 << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00149 << " ";
00150 out << "[label=\"" << o.str() << "\"];"
00151 << std::endl;
00152 ++count;
00153 }
00154 out << name_ << c << "\" [label=\"" << *i << "\"];" << std::endl;
00155 }
00156 for (typename auto_t::transition_iterator i = a.transitions().begin();
00157 i != a.transitions().end();
00158 ++i)
00159 {
00160 out << name_ << state_map[a.src_of(*i)]
00161 << "\" -> "
00162 << name_ << state_map[a.dst_of(*i)] << '"';
00163 std::ostringstream o;
00164 series_set_elt_t ss = a.series_of(*i);
00165 for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00166 o << conv(a.structure(), *s) << "|"
00167 << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00168 << " ";
00169 out << "[label=\"" << o.str() << "\"];"
00170 << std::endl;
00171 }
00172 out << "}" << std::endl;
00173 }
00174
00175 }
00176 }
00177
00178 #endif // ! VCSN_TOOLS_DOT_FORMAT_HXX