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

Generated on Thu Oct 9 20:22:34 2008 for Vaucanson by  doxygen 1.5.1