Awali
Another Weighted Automata library
projection.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 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  }
129 
131  const Aut& in_;
135  std::unordered_map<state_t, state_t> out_state;
136  };
137 
138  template <typename Aut>
139  struct imagers
140  {
147 
148  imagers(const Aut& in)
149  : in_(in)
150  {
151  context_t in_context = in->context();
152  auto in_labelset = in_context.labelset();
153  auto weightset = in_context.weightset();
154  auto out_labelset = select_tail<labelset_t>::get(*in_labelset);
155  out_context_t out_context{out_labelset, *weightset};
156  out_ = make_mutable_automaton(out_context);
157 
158  }
159 
161  {
162  // Copy the states. We cannot iterate on the transitions
163  // only, as we would lose the states without transitions.
164  for (auto s: in_->states())
165  out_state[s] = out_->add_state();
166  out_state[in_->pre()]= out_->pre();
167  out_state[in_->post()]= out_->post();
168 
169  for (auto t : in_->all_transitions())
170  {
171  auto src = out_state.find(in_->src_of(t));
172  auto dst = out_state.find(in_->dst_of(t));
173  if (src != out_state.end() && dst != out_state.end()){
174  out_->new_transition(src->second, dst->second,
175  tuple_tail(in_->label_of(t)), in_->weight_of(t));
176  }
177  }
178  return out_;
179  }
180 
181  void set_history() {
182  auto history = std::make_shared<single_history<Aut>>(in_);
183  out_->set_history(history);
184  for (auto p: in_->all_states())
185  history->add_state(out_state[p], p);
186  }
187 
189  const Aut& in_;
193  std::unordered_map<state_t, state_t> out_state;
194  };
195 
196 
197  template <typename Aut, size_t... I>
198  struct projectors
199  {
206 
207  projectors(const Aut& in)
208  : in_(in)
209  {
210  context_t in_context = in->context();
211  auto in_labelset = in_context.labelset();
212  auto weightset = in_context.weightset();
213  auto out_labelset = selects<labelset_t,I...>::get(*in_labelset);
214  out_context_t out_context{out_labelset, *weightset};
215  out_ = make_mutable_automaton(out_context);
216  }
217 
219  {
220  // Copy the states. We cannot iterate on the transitions
221  // only, as we would lose the states without transitions.
222  for (auto s: in_->states())
223  out_state[s] = out_->add_state();
224  out_state[in_->pre()]= out_->pre();
225  out_state[in_->post()]= out_->post();
226 
227  for (auto t : in_->all_transitions())
228  {
229  auto src = out_state.find(in_->src_of(t));
230  auto dst = out_state.find(in_->dst_of(t));
231  if (src != out_state.end() && dst != out_state.end()){
232  out_->new_transition(src->second, dst->second,
233  sub_tuple<I...>::get(in_->label_of(t)), in_->weight_of(t));
234  }
235  }
236  return out_;
237  }
238 
239  void set_history() {
240  auto history = std::make_shared<single_history<Aut>>(in_);
241  out_->set_history(history);
242  for (auto p: in_->all_states())
243  history->add_state(out_state[p], p);
244  }
245 
247  const Aut& in_;
251  std::unordered_map<state_t, state_t> out_state;
252  };
253  }
254 
263  template <size_t I, typename Tdc>
264  inline
265  auto
266  projection(const Tdc& in, bool keep_history=true) -> typename internal::projector<Tdc, I>::out_automaton_t
267  {
269  auto r= proj();
270  if(keep_history)
271  proj.set_history();
272  return r;
273  }
274 
282  template <typename Tdc>
283  inline
284  auto
285  images(const Tdc& in, bool keep_history=true) -> typename internal::imagers<Tdc>::out_automaton_t
286  {
287  internal::imagers<Tdc> proj(in);
288  auto r= proj();
289  if(keep_history)
290  proj.set_history();
291  return r;
292  }
293 
306  template <size_t... I, typename Tdc>
307  inline
308  auto
309  projections(const Tdc& in, bool keep_history=true) -> typename internal::projectors<Tdc, I...>::out_automaton_t
310  {
311  internal::projectors<Tdc, I...> proj(in);
312  auto r= proj();
313  if(keep_history)
314  proj.set_history();
315  return r;
316  }
317 }}//end of ns awali::stc
318 
319 #endif // !AWALI_ALGOS_PROJECTION_HH
carries the algebraic settings of automata
Definition: context.hh:40
The semiring of floating Numbers.
Definition: r.hh:34
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:309
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:266
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:285
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:911
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:140
context_t_of< Aut > context_t
Definition: projection.hh:141
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:146
out_automaton_t operator()()
Definition: projection.hh:160
void set_history()
Definition: projection.hh:181
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:191
weightset_t_of< context_t > weightset_t
Definition: projection.hh:143
typename select_tail< labelset_t >::labelset_t out_labelset_t
Definition: projection.hh:144
labelset_t_of< context_t > labelset_t
Definition: projection.hh:142
imagers(const Aut &in)
Definition: projection.hh:148
const Aut & in_
Input automaton.
Definition: projection.hh:189
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:193
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:135
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:133
context_t_of< Aut > context_t
Definition: projection.hh:83
const Aut & in_
Input automaton.
Definition: projection.hh:131
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:199
context_t_of< Aut > context_t
Definition: projection.hh:200
out_automaton_t operator()()
Definition: projection.hh:218
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:251
projectors(const Aut &in)
Definition: projection.hh:207
weightset_t_of< context_t > weightset_t
Definition: projection.hh:202
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:205
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:249
void set_history()
Definition: projection.hh:239
labelset_t_of< context_t > labelset_t
Definition: projection.hh:201
const Aut & in_
Input automaton.
Definition: projection.hh:247
typename selects< labelset_t, I... >::labelset_t out_labelset_t
Definition: projection.hh:203
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