Vaucanson 1.4
ltl_to_pair.hxx
00001 // ltl_to_pair.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2008, 2009 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_ALGORITHMS_LTL_TO_PAIR_HXX
00018 # define VCSN_ALGORITHMS_LTL_TO_PAIR_HXX
00019 
00020 # include <vaucanson/algorithms/ltl_to_pair.hh>
00021 # include <vaucanson/algorithms/is_ltl.hh>
00022 
00023 namespace vcsn
00024 {
00025 // Helper to write lighter code.
00026 # define MUTE_TRAITS mute_ltl_to_pair<S, T>
00027 
00028   template <typename S, typename T>
00029   inline typename MUTE_TRAITS::ret_alphabet_t
00030   MUTE_TRAITS::
00031   cartesian_product(const typename MUTE_TRAITS::first_alphabet_t& A,
00032                     const typename MUTE_TRAITS::second_alphabet_t& B)
00033   {
00034     ret_alphabet_t E;
00035 
00036     for_all_const_(first_alphabet_t, i, A)
00037     {
00038       for_all_const_(second_alphabet_t, j, B)
00039       {
00040         E.insert(std::make_pair(*i, *j));
00041       }
00042     }
00043 
00044     return E;
00045   }
00046 
00047   template <typename S, typename T>
00048   inline typename MUTE_TRAITS::ret
00049   MUTE_TRAITS::
00050   make_automaton(const Element<S, T>& A)
00051   {
00052     ret_alphabet_t E = cartesian_product(A.structure().series().monoid().
00053                                          first_monoid().alphabet(),
00054                                          A.structure().series().monoid().
00055                                          second_monoid().alphabet());
00056 
00057     return make_automaton(E);
00058   }
00059 
00060   template <typename S, typename T>
00061   inline typename MUTE_TRAITS::ret::series_set_elt_t
00062   MUTE_TRAITS::
00063   series_convert(const typename MUTE_TRAITS::ret_series_set_t& series,
00064                  const typename MUTE_TRAITS::automaton_t::
00065                  series_set_elt_t& ss)
00066   {
00067     typename ret::series_set_elt_t R(series);
00068 
00069     // We assume that the src automaton is an ltl.
00070     std::basic_string<ret_letter_t> m;
00071     if (!(*(ss.supp().begin())).first.empty())
00072       m += std::make_pair(((*(ss.supp().begin())).first)[0],
00073                           ((*(ss.supp().begin())).second)[0]);
00074     R.assoc(m, ss.get(*(ss.supp().begin())));
00075 
00076     return R;
00077   }
00078 
00079   template <typename S, typename T>
00080   void
00081   do_ltl_to_pair(const Element<S, T>& src,
00082                  typename MUTE_TRAITS::ret& res)
00083   {
00084     BENCH_TASK_SCOPED("ltl_to_pair");
00085 
00086     // The input FMP transducer must be `letter-to-letter'.
00087     precondition(is_ltl(src));
00088 
00089     // Type helpers.
00090     typedef typename MUTE_TRAITS::automaton_t automaton_t;
00091     typedef MUTE_TRAITS pair_automaton_traits_t;
00092     typedef typename pair_automaton_traits_t::ret res_t;
00093     AUTOMATON_TYPES(automaton_t);
00094     AUTOMATON_TYPES_(res_t, res_);
00095 
00096     // Helper map.
00097     std::map<hstate_t, res_hstate_t> m;
00098 
00099     for_all_const_states(p, src)
00100       m[*p] = res.add_state();
00101 
00102     // Setup initial transitions.
00103     for_all_const_initial_states(i, src)
00104     {
00105       res.set_initial(m[*i],
00106                       pair_automaton_traits_t::
00107                       series_convert(res.structure().series(),
00108                                      src.get_initial(*i)));
00109     }
00110 
00111     // Setup normal transitions.
00112     for_all_const_transitions(e, src)
00113     {
00114       res.add_series_transition(m[src.src_of(*e)],
00115                                 m[src.dst_of(*e)],
00116                                 pair_automaton_traits_t::
00117                                 series_convert(res.structure().series(),
00118                                                src.series_of(*e)));
00119     }
00120 
00121     // Setup final transitions.
00122     for_all_const_final_states(f, src)
00123     {
00124       res.set_final(m[*f],
00125                     pair_automaton_traits_t::
00126                     series_convert(res.structure().series(),
00127                                    src.get_final(*f)));
00128     }
00129   }
00130 
00131   //
00132   // Facade Functions
00133   //
00134 
00135   template <typename S, typename T>
00136   void
00137   ltl_to_pair(const Element<S, T>& ltl,
00138               typename MUTE_TRAITS::ret& res)
00139   {
00140     do_ltl_to_pair(ltl, res);
00141   }
00142 
00143   template <typename S, typename T>
00144   typename MUTE_TRAITS::ret
00145   ltl_to_pair(const Element<S, T>& ltl)
00146   {
00147     typename MUTE_TRAITS::ret res = MUTE_TRAITS::make_automaton(ltl);
00148 
00149     do_ltl_to_pair(ltl, res);
00150 
00151     return res;
00152   }
00153 
00154 # undef MUTE_TRAITS
00155 
00156 } // ! vcsn
00157 
00158 #endif // ! VCSN_ALGORITHMS_LTL_TO_PAIR_HXX