Awali
Another Weighted Automata library
projection.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_PROJECTION_HH
18 # define AWALI_ALGOS_PROJECTION_HH
19 
20 # include <unordered_map>
21 
23 #include <awali/sttc/misc/set.hh>
27 
28 namespace awali { namespace sttc {
29 
30  /*---------------------------.
31  | projection(transducteur). |
32  `---------------------------*/
33 
34  namespace internal
35  {
36 
37  template <typename Labelset, size_t I> struct select;
38  template <typename Labelset, size_t... I> struct selects;
39  template <typename Labelset, size_t... I> struct select_tail;
40 
41  template <typename... T, size_t I>
42  struct select<tupleset<T...>,I>{
43  using tp_t = tupleset<T...>;
44  using labelset_t = typename tp_t::template valueset_t<I>;
45 
46  static
47  labelset_t get(const tp_t& t) {
48  return std::get<I>(t.sets());
49  }
50  };
51 
52  template<typename T> struct tuple_to_tupleset;
53  template<typename... T>
54  struct tuple_to_tupleset<std::tuple<T...>> {
55  using type = tupleset<T...>;
56  };
57 
58  template <typename... T, size_t... I>
59  struct selects<tupleset<T...>,I...>{
60  using tp_t = tupleset<T...>;
61  using labelset_t = typename tuple_to_tupleset<typename sub_tuple<I...>::template type<std::tuple<T...>>>::type;
62 
63  static
64  labelset_t get(const tp_t& t) {
65  return {sub_tuple<I...>::get(t.sets())};
66  }
67  };
68 
69  template <typename... T>
70  struct select_tail<tupleset<T...>>{
71  using tp_t = tupleset<T...>;
72  using labelset_t = typename tuple_to_tupleset<typename tail_tuple<std::tuple<T...>>::type>::type;
73 
74  static
75  labelset_t get(const tp_t& t) {
76  return {tuple_tail(t.sets())};
77  }
78  };
79 
80  template <typename Aut, size_t I>
81  struct projector
82  {
89 
90  projector(const Aut& in)
91  : in_(in)
92  {
93  context_t in_context = in->context();
94  auto in_labelset = in_context.labelset();
95  auto weightset = in_context.weightset();
96  auto out_labelset = select<labelset_t,I>::get(*in_labelset);
97  out_context_t out_context{out_labelset, *weightset};
98  out_ = make_mutable_automaton(out_context);
99 
100  }
101 
103  {
104  // Copy the states. We cannot iterate on the transitions
105  // only, as we would lose the states without transitions.
106  for (auto s: in_->states())
107  out_state[s] = out_->add_state();
108  out_state[in_->pre()]= out_->pre();
109  out_state[in_->post()]= out_->post();
110 
111  for (auto t : in_->all_transitions())
112  {
113  auto src = out_state.find(in_->src_of(t));
114  auto dst = out_state.find(in_->dst_of(t));
115  if (src != out_state.end() && dst != out_state.end()){
116  out_->new_transition(src->second, dst->second,
117  std::get<I>(in_->label_of(t)), in_->weight_of(t));
118  }
119  }
120  return out_;
121  }
122 
123  void set_history() {
124  auto history = std::make_shared<single_history<Aut>>(in_);
125  out_->set_history(history);
126  for (auto p: in_->all_states()) {
127  history->add_state(out_state[p], p);
128  if(in_->has_name(p)) {
129  out_->set_state_name(out_state[p], in_->get_state_name(p));
130  }
131  }
132  }
133 
135  const Aut& in_;
139  std::unordered_map<state_t, state_t> out_state;
140  };
141 
142  template <typename Aut>
143  struct imagers
144  {
151 
152  imagers(const Aut& in)
153  : in_(in)
154  {
155  context_t in_context = in->context();
156  auto in_labelset = in_context.labelset();
157  auto weightset = in_context.weightset();
158  auto out_labelset = select_tail<labelset_t>::get(*in_labelset);
159  out_context_t out_context{out_labelset, *weightset};
160  out_ = make_mutable_automaton(out_context);
161 
162  }
163 
165  {
166  // Copy the states. We cannot iterate on the transitions
167  // only, as we would lose the states without transitions.
168  for (auto s: in_->states())
169  out_state[s] = out_->add_state();
170  out_state[in_->pre()]= out_->pre();
171  out_state[in_->post()]= out_->post();
172 
173  for (auto t : in_->all_transitions())
174  {
175  auto src = out_state.find(in_->src_of(t));
176  auto dst = out_state.find(in_->dst_of(t));
177  if (src != out_state.end() && dst != out_state.end()){
178  out_->new_transition(src->second, dst->second,
179  tuple_tail(in_->label_of(t)), in_->weight_of(t));
180  }
181  }
182  return out_;
183  }
184 
185  void set_history() {
186  auto history = std::make_shared<single_history<Aut>>(in_);
187  out_->set_history(history);
188  for (auto p: in_->all_states())
189  history->add_state(out_state[p], p);
190  }
191 
193  const Aut& in_;
197  std::unordered_map<state_t, state_t> out_state;
198  };
199 
200 
201  template <typename Aut, size_t... I>
202  struct projectors
203  {
210 
211  projectors(const Aut& in)
212  : in_(in)
213  {
214  context_t in_context = in->context();
215  auto in_labelset = in_context.labelset();
216  auto weightset = in_context.weightset();
217  auto out_labelset = selects<labelset_t,I...>::get(*in_labelset);
218  out_context_t out_context{out_labelset, *weightset};
219  out_ = make_mutable_automaton(out_context);
220  }
221 
223  {
224  // Copy the states. We cannot iterate on the transitions
225  // only, as we would lose the states without transitions.
226  for (auto s: in_->states())
227  out_state[s] = out_->add_state();
228  out_state[in_->pre()]= out_->pre();
229  out_state[in_->post()]= out_->post();
230 
231  for (auto t : in_->all_transitions())
232  {
233  auto src = out_state.find(in_->src_of(t));
234  auto dst = out_state.find(in_->dst_of(t));
235  if (src != out_state.end() && dst != out_state.end()){
236  out_->new_transition(src->second, dst->second,
237  sub_tuple<I...>::get(in_->label_of(t)), in_->weight_of(t));
238  }
239  }
240  return out_;
241  }
242 
243  void set_history() {
244  auto history = std::make_shared<single_history<Aut>>(in_);
245  out_->set_history(history);
246  for (auto p: in_->all_states())
247  history->add_state(out_state[p], p);
248  }
249 
251  const Aut& in_;
255  std::unordered_map<state_t, state_t> out_state;
256  };
257  }
258 
267  template <size_t I, typename Tdc>
268  inline
269  auto
270  projection(const Tdc& in, bool keep_history=true) -> typename internal::projector<Tdc, I>::out_automaton_t
271  {
273  auto r= proj();
274  if(keep_history)
275  proj.set_history();
276  return r;
277  }
278 
286  template <typename Tdc>
287  inline
288  auto
289  images(const Tdc& in, bool keep_history=true) -> typename internal::imagers<Tdc>::out_automaton_t
290  {
291  internal::imagers<Tdc> proj(in);
292  auto r= proj();
293  if(keep_history)
294  proj.set_history();
295  return r;
296  }
297 
310  template <size_t... I, typename Tdc>
311  inline
312  auto
313  projections(const Tdc& in, bool keep_history=true) -> typename internal::projectors<Tdc, I...>::out_automaton_t
314  {
315  internal::projectors<Tdc, I...> proj(in);
316  auto r= proj();
317  if(keep_history)
318  proj.set_history();
319  return r;
320  }
321 }}//end of ns awali::stc
322 
323 #endif // !AWALI_ALGOS_PROJECTION_HH
carries the algebraic settings of automata
Definition: context.hh:40
The semiring of floating Numbers.
Definition: r.hh:35
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
const valuesets_t & sets() const
The componants valuesets, as a tuple.
Definition: tupleset.hh:152
weightset_description weightset(const std::string &k)
auto tuple_tail(const Tuple &t) -> typename tail_tuple< Tuple >::type
Definition: sub_tuple.hh:96
Definition: projection.hh:39
Definition: projection.hh:38
Definition: sub_tuple.hh:65
Definition: projection.hh:52
auto projections(const Tdc &in, bool keep_history=true) -> typename internal::projectors< Tdc, I... >::out_automaton_t
Projection and/or permutation of tapes of a transducer.
Definition: projection.hh:313
auto projection(const Tdc &in, bool keep_history=true) -> typename internal::projector< Tdc, I >::out_automaton_t
Projection of one tape of a transducer.
Definition: projection.hh:270
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
auto images(const Tdc &in, bool keep_history=true) -> typename internal::imagers< Tdc >::out_automaton_t
Projection of last tapes of a transducer.
Definition: projection.hh:289
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
Definition: projection.hh:144
context_t_of< Aut > context_t
Definition: projection.hh:145
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:150
out_automaton_t operator()()
Definition: projection.hh:164
void set_history()
Definition: projection.hh:185
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:195
weightset_t_of< context_t > weightset_t
Definition: projection.hh:147
typename select_tail< labelset_t >::labelset_t out_labelset_t
Definition: projection.hh:148
labelset_t_of< context_t > labelset_t
Definition: projection.hh:146
imagers(const Aut &in)
Definition: projection.hh:152
const Aut & in_
Input automaton.
Definition: projection.hh:193
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:197
Definition: projection.hh:82
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:88
void set_history()
Definition: projection.hh:123
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:139
labelset_t_of< context_t > labelset_t
Definition: projection.hh:84
projector(const Aut &in)
Definition: projection.hh:90
weightset_t_of< context_t > weightset_t
Definition: projection.hh:85
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:137
context_t_of< Aut > context_t
Definition: projection.hh:83
const Aut & in_
Input automaton.
Definition: projection.hh:135
out_automaton_t operator()()
Definition: projection.hh:102
typename select< labelset_t, I >::labelset_t out_labelset_t
Definition: projection.hh:86
Definition: projection.hh:203
context_t_of< Aut > context_t
Definition: projection.hh:204
out_automaton_t operator()()
Definition: projection.hh:222
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:255
projectors(const Aut &in)
Definition: projection.hh:211
weightset_t_of< context_t > weightset_t
Definition: projection.hh:206
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:209
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:253
void set_history()
Definition: projection.hh:243
labelset_t_of< context_t > labelset_t
Definition: projection.hh:205
const Aut & in_
Input automaton.
Definition: projection.hh:251
typename selects< labelset_t, I... >::labelset_t out_labelset_t
Definition: projection.hh:207
static labelset_t get(const tp_t &t)
Definition: projection.hh:47
typename tp_t::template valueset_t< I > labelset_t
Definition: projection.hh:44
typename tuple_to_tupleset< typename tail_tuple< std::tuple< T... > >::type >::type labelset_t
Definition: projection.hh:72
static labelset_t get(const tp_t &t)
Definition: projection.hh:75
Definition: reduce.hh:103
typename tuple_to_tupleset< typename sub_tuple< I... >::template type< std::tuple< T... > >>::type labelset_t
Definition: projection.hh:61
static labelset_t get(const tp_t &t)
Definition: projection.hh:64
Definition: sub_tuple.hh:34
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:40