dot_format.hxx

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/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           if (ss.supp().begin() == ss.supp().end())
00131             o << "0";
00132           for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00133             o << conv(a.structure(), *s) << "|"
00134               << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00135               << " ";
00136           out << "[label=\"" << o.str() << "\"];"
00137               << std::endl;
00138           ++count;
00139         }
00140         if (a.is_final(*i))
00141         {
00142           out << name_ << count
00143               << "\" [style=invis,label=\"\",width=.01,height=.01];"
00144               << std::endl
00145               << name_ << c << "\" -> " << name_ << count << '"';
00146           std::ostringstream o;
00147           series_set_elt_t ss = a.get_final(*i);
00148           if (ss.supp().begin() == ss.supp().end())
00149             o << "0";
00150           for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00151             o << conv(a.structure(), *s) << "|"
00152               << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00153               << " ";
00154           out << "[label=\"" << o.str() << "\"];"
00155               << std::endl;
00156           ++count;
00157         }
00158         out << name_ << c << "\" [label=\"" << *i << "\"];" << std::endl;
00159       }
00160       for (typename auto_t::transition_iterator i = a.transitions().begin();
00161            i != a.transitions().end();
00162            ++i)
00163       {
00164         out << name_ << state_map[a.src_of(*i)]
00165             << "\" -> "
00166             << name_ << state_map[a.dst_of(*i)] << '"';
00167         std::ostringstream o;
00168         series_set_elt_t ss = a.series_of(*i);
00169         if (ss.supp().begin() == ss.supp().end())
00170           o << "0";
00171         for_all_const_(series_set_elt_t::support_t, s, ss.supp())
00172           o << conv(a.structure(), *s) << "|"
00173             << ss.get(monoid_elt_t (a.structure().series().monoid(), *s))
00174             << " ";
00175         out << "[label=\"" << o.str() << "\"];"
00176             << std::endl;
00177       }
00178       out << "}" << std::endl;
00179     }
00180 
00181   } // tools
00182 } // vcsn
00183 
00184 #endif // ! VCSN_TOOLS_DOT_FORMAT_HXX

Generated on Wed Jun 13 17:00:21 2007 for Vaucanson by  doxygen 1.5.1