Vaucanson  1.4.1
ltl_to_pair.hxx
1 // ltl_to_pair.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2008, 2009 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGORITHMS_LTL_TO_PAIR_HXX
18 # define VCSN_ALGORITHMS_LTL_TO_PAIR_HXX
19 
22 
23 namespace vcsn
24 {
25 // Helper to write lighter code.
26 # define MUTE_TRAITS mute_ltl_to_pair<S, T>
27 
28  template <typename S, typename T>
29  inline typename MUTE_TRAITS::ret_alphabet_t
30  MUTE_TRAITS::
31  cartesian_product(const typename MUTE_TRAITS::first_alphabet_t& A,
32  const typename MUTE_TRAITS::second_alphabet_t& B)
33  {
34  ret_alphabet_t E;
35 
36  for_all_const_(first_alphabet_t, i, A)
37  {
38  for_all_const_(second_alphabet_t, j, B)
39  {
40  E.insert(std::make_pair(*i, *j));
41  }
42  }
43 
44  return E;
45  }
46 
47  template <typename S, typename T>
48  inline typename MUTE_TRAITS::ret
49  MUTE_TRAITS::
50  make_automaton(const Element<S, T>& A)
51  {
52  ret_alphabet_t E = cartesian_product(A.structure().series().monoid().
53  first_monoid().alphabet(),
54  A.structure().series().monoid().
55  second_monoid().alphabet());
56 
57  return make_automaton(E);
58  }
59 
60  template <typename S, typename T>
61  inline typename MUTE_TRAITS::ret::series_set_elt_t
62  MUTE_TRAITS::
63  series_convert(const typename MUTE_TRAITS::ret_series_set_t& series,
64  const typename MUTE_TRAITS::automaton_t::
65  series_set_elt_t& ss)
66  {
67  typename ret::series_set_elt_t R(series);
68 
69  // We assume that the src automaton is an ltl.
70  std::basic_string<ret_letter_t> m;
71  if (!(*(ss.supp().begin())).first.empty())
72  m += std::make_pair(((*(ss.supp().begin())).first)[0],
73  ((*(ss.supp().begin())).second)[0]);
74  R.assoc(m, ss.get(*(ss.supp().begin())));
75 
76  return R;
77  }
78 
79  template <typename S, typename T>
80  void
81  do_ltl_to_pair(const Element<S, T>& src,
82  typename MUTE_TRAITS::ret& res)
83  {
84  BENCH_TASK_SCOPED("ltl_to_pair");
85 
86  // The input FMP transducer must be `letter-to-letter'.
87  precondition(is_ltl(src));
88 
89  // Type helpers.
90  typedef typename MUTE_TRAITS::automaton_t automaton_t;
91  typedef MUTE_TRAITS pair_automaton_traits_t;
92  typedef typename pair_automaton_traits_t::ret res_t;
93  AUTOMATON_TYPES(automaton_t);
94  AUTOMATON_TYPES_(res_t, res_);
95 
96  // Helper map.
97  std::map<hstate_t, res_hstate_t> m;
98 
99  for_all_const_states(p, src)
100  m[*p] = res.add_state();
101 
102  // Setup initial transitions.
103  for_all_const_initial_states(i, src)
104  {
105  res.set_initial(m[*i],
106  pair_automaton_traits_t::
107  series_convert(res.structure().series(),
108  src.get_initial(*i)));
109  }
110 
111  // Setup normal transitions.
112  for_all_const_transitions(e, src)
113  {
114  res.add_series_transition(m[src.src_of(*e)],
115  m[src.dst_of(*e)],
116  pair_automaton_traits_t::
117  series_convert(res.structure().series(),
118  src.series_of(*e)));
119  }
120 
121  // Setup final transitions.
122  for_all_const_final_states(f, src)
123  {
124  res.set_final(m[*f],
125  pair_automaton_traits_t::
126  series_convert(res.structure().series(),
127  src.get_final(*f)));
128  }
129  }
130 
131  //
132  // Facade Functions
133  //
134 
135  template <typename S, typename T>
136  void
137  ltl_to_pair(const Element<S, T>& ltl,
138  typename MUTE_TRAITS::ret& res)
139  {
140  do_ltl_to_pair(ltl, res);
141  }
142 
143  template <typename S, typename T>
144  typename MUTE_TRAITS::ret
145  ltl_to_pair(const Element<S, T>& ltl)
146  {
147  typename MUTE_TRAITS::ret res = MUTE_TRAITS::make_automaton(ltl);
148 
149  do_ltl_to_pair(ltl, res);
150 
151  return res;
152  }
153 
154 # undef MUTE_TRAITS
155 
156 } // ! vcsn
157 
158 #endif // ! VCSN_ALGORITHMS_LTL_TO_PAIR_HXX