fmp_to_realtime.hxx

00001 // fmp_to_realtime.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005 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_FMP_TO_REALTIME_HXX
00018 # define VCSN_ALGORITHMS_FMP_TO_REALTIME_HXX
00019 
00020 # include <vaucanson/automata/concept/automata.hh>
00021 # include <vaucanson/automata/concept/transducer.hh>
00022 # include <vaucanson/algebra/concept/freemonoid_product.hh>
00023 
00024 # include <map>
00025 
00026 namespace vcsn
00027 {
00028 
00029   template<typename S, typename T,
00030            typename SS, typename TT,
00031            typename Self>
00032   void
00033   do_fmp_to_realtime(const vcsn::AutomataBase<S>&,
00034                      const vcsn::TransducerBase<SS>&,
00035                      const vcsn::algebra::FreeMonoidProductBase<Self>&,
00036                      const vcsn::Element<S, T>& fmp,
00037                      vcsn::Element<SS, TT>& res)
00038   {
00039     // Map source automaton's states with result's states
00040     std::map<vcsn::hstate_t, vcsn::hstate_t> m;
00041 
00042     // Input FMP type
00043     typedef vcsn::Element<S, T> FMP_t;
00044 
00045     // Output transducer type
00046     typedef vcsn::Element<SS, TT> Trans_t;
00047 
00048     /*-------------------------.
00049     | Creating the transducer. |
00050     `-------------------------*/
00051 
00052     // Adding states
00053     for (typename FMP_t::state_iterator St = fmp.states().begin();
00054          St != fmp.states().end();
00055          ++St)
00056       m[*St] = res.add_state();
00057 
00058 
00059     /*-------------------------.
00060     | Setting initial states.  |
00061     `-------------------------*/
00062 
00063     for (typename FMP_t::initial_iterator St = fmp.initial().begin();
00064          St != fmp.initial().end();
00065          ++St)
00066     {
00067       //Series to be created
00068       typename Trans_t::series_set_elt_t s(res.structure().series());
00069 
00070       typename FMP_t::series_set_elt_t s_elt = fmp.get_initial(*St);
00071       for_all_const_(FMP_t::series_set_elt_t::support_t, i, s_elt.supp())
00072       {
00073         typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00074           output_monoid_value;
00075         typename Trans_t::semiring_elt_t::semiring_elt_t weight;
00076 
00077         typename Trans_t::monoid_elt_value_t
00078           input_monoid_value = (*i).first;
00079         typename Trans_t::monoid_elt_t
00080           input_monoid(res.structure().series().monoid(),
00081                        input_monoid_value);
00082 
00083         typename Trans_t::semiring_elt_t::monoid_elt_t
00084           output_monoid(res.structure().series().semiring().monoid(),
00085                         (*i).second);
00086         weight = s_elt.get(*i);
00087 
00088         //Creating the element multiplicity
00089         typename Trans_t::semiring_elt_t
00090           out_mult(res.structure().series().semiring());
00091         out_mult.assoc(output_monoid, weight);
00092 
00093         //Associating it to the input monoid
00094         s.assoc(input_monoid, out_mult);
00095       }
00096       res.set_initial(m[*St], s);
00097     }
00098 
00099 
00100     /*------------------------.
00101     | Setting final states.   |
00102     `------------------------*/
00103 
00104     for (typename FMP_t::final_iterator St = fmp.final().begin();
00105          St != fmp.final().end();
00106          ++St)
00107     {
00108       //Series to be created
00109       typename Trans_t::series_set_elt_t s(res.structure().series());
00110 
00111       typename FMP_t::series_set_elt_t s_elt = fmp.get_final(*St);
00112       for_all_const_(FMP_t::series_set_elt_t::support_t, i, s_elt.supp())
00113       {
00114         typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00115           output_monoid_value;
00116         typename Trans_t::semiring_elt_t::semiring_elt_t weight;
00117 
00118         typename Trans_t::monoid_elt_value_t input_monoid_value =
00119           (*i).first;
00120         typename Trans_t::monoid_elt_t
00121           input_monoid(res.structure().series().monoid(),
00122                        input_monoid_value);
00123         typename Trans_t::semiring_elt_t::monoid_elt_t
00124           output_monoid(res.structure().series().semiring().monoid(),
00125                         (*i).second);
00126         weight = s_elt.get(*i);
00127 
00128         //Creating the element multiplicity
00129         typename Trans_t::semiring_elt_t
00130           out_mult(res.structure().series().semiring());
00131         out_mult.assoc(output_monoid, weight);
00132 
00133         //Associating it to the input monoid
00134         s.assoc(input_monoid, out_mult);
00135       }
00136       res.set_final(m[*St], s);
00137     }
00138 
00139 
00140     /*-----------------------.
00141     | Creating transitions.  |
00142     `-----------------------*/
00143 
00144     for (typename FMP_t::transition_iterator Ed = fmp.transitions().begin();
00145          Ed != fmp.transitions().end();
00146          ++Ed)
00147     {
00148       // FIXME
00149       // No Special Treatment is done for 0 weighted
00150       typename Trans_t::series_set_elt_t
00151         transition_value(res.structure().series());
00152 
00153       typename Trans_t::monoid_elt_value_t input_monoid_value;
00154       typename Trans_t::semiring_elt_value_t::monoid_elt_value_t
00155         output_monoid_value;
00156 
00157       typename FMP_t::series_set_elt_t series_fmp(fmp.structure().series());
00158       typename Trans_t::semiring_elt_t
00159         out_mult(res.structure().series().semiring());
00160       series_fmp = fmp.series_of(*Ed);
00161 
00162       for_all_const_(FMP_t::series_set_elt_t::support_t,
00163                      i,
00164                      series_fmp.supp())
00165       {
00166         input_monoid_value = (*i).first;
00167         output_monoid_value = (*i).second;
00168 
00169         //Creating the transition's semiring value
00170         typename Trans_t::semiring_elt_t::semiring_elt_t
00171           transition_weight(res.structure().series().semiring().semiring(),
00172                             series_fmp.get(*i));
00173         typename Trans_t::semiring_elt_t
00174           out_mult(res.structure().series().semiring());
00175         out_mult.assoc(typename Trans_t::semiring_elt_t::monoid_elt_t
00176                        (res.structure().series().semiring().monoid(),
00177                         output_monoid_value),
00178                        transition_weight);
00179 
00180         //Creating the transition's monoid value
00181         typename Trans_t::monoid_elt_t
00182           input(res.structure().series().monoid(),
00183                 input_monoid_value);
00184         transition_value.assoc(input, out_mult);
00185         res.add_series_transition(m[fmp.src_of(*Ed)],
00186                                   m[fmp.dst_of(*Ed)],
00187                                   transition_value);
00188       }
00189     }
00190   }
00191 
00192   template<typename S, typename T,
00193            typename SS, typename TT>
00194   vcsn::Element<SS, TT>&
00195   fmp_to_realtime(const vcsn::Element<S, T>& fmp,
00196                   vcsn::Element<SS, TT>& res)
00197   {
00198     do_fmp_to_realtime(fmp.structure(), res.structure(),
00199                        fmp.structure().series().monoid(),
00200                        fmp, res);
00201     return res;
00202   }
00203 }
00204 #endif // ! VCSN_ALGORITHMS_FMP_TO_REALTIME_HXX

Generated on Sat Jul 29 17:12:59 2006 for Vaucanson by  doxygen 1.4.6