Awali
Another Weighted Automata library
projection.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2023 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] = s;
108  out_->add_state(s);
109  if(in_->has_name(s))
110  out_->set_state_name(s, in_->get_state_name(s));
111  }
112  out_state[in_->pre()]= out_->pre();
113  out_state[in_->post()]= out_->post();
114 
115  for (auto t : in_->all_transitions())
116  {
117  auto src = out_state.find(in_->src_of(t));
118  auto dst = out_state.find(in_->dst_of(t));
119  if (src != out_state.end() && dst != out_state.end()){
120  out_->new_transition(src->second, dst->second,
121  std::get<I>(in_->label_of(t)), in_->weight_of(t));
122  }
123  }
124  return out_;
125  }
126 
127  void set_history() {
128  auto history = std::make_shared<single_history<Aut>>(in_);
129  out_->set_history(history);
130  for (auto p: in_->all_states()) {
131  history->add_state(out_state[p], p);
132  if(in_->has_name(p)) {
133  out_->set_state_name(out_state[p], in_->get_state_name(p));
134  }
135  }
136  }
137 
139  const Aut& in_;
143  std::unordered_map<state_t, state_t> out_state;
144  };
145 
146  template <typename Aut>
147  struct imagers
148  {
155 
156  imagers(const Aut& in)
157  : in_(in)
158  {
159  context_t in_context = in->context();
160  auto in_labelset = in_context.labelset();
161  auto weightset = in_context.weightset();
162  auto out_labelset = select_tail<labelset_t>::get(*in_labelset);
163  out_context_t out_context{out_labelset, *weightset};
164  out_ = make_mutable_automaton(out_context);
165 
166  }
167 
169  {
170  // Copy the states. We cannot iterate on the transitions
171  // only, as we would lose the states without transitions.
172  for (auto s: in_->states()) {
173  out_state[s] = s;
174  out_->add_state(s);
175  if(in_->has_name(s))
176  out_->set_state_name(s, in_->get_state_name(s));
177  }
178  out_state[in_->pre()]= out_->pre();
179  out_state[in_->post()]= out_->post();
180 
181  for (auto t : in_->all_transitions())
182  {
183  auto src = out_state.find(in_->src_of(t));
184  auto dst = out_state.find(in_->dst_of(t));
185  if (src != out_state.end() && dst != out_state.end()){
186  out_->new_transition(src->second, dst->second,
187  tuple_tail(in_->label_of(t)), in_->weight_of(t));
188  }
189  }
190  return out_;
191  }
192 
193  void set_history() {
194  auto history = std::make_shared<single_history<Aut>>(in_);
195  out_->set_history(history);
196  for (auto p: in_->all_states())
197  history->add_state(out_state[p], p);
198  }
199 
201  const Aut& in_;
205  std::unordered_map<state_t, state_t> out_state;
206  };
207 
208 
209  template <typename Aut, size_t... I>
210  struct projectors
211  {
218 
219  projectors(const Aut& in)
220  : in_(in)
221  {
222  context_t in_context = in->context();
223  auto in_labelset = in_context.labelset();
224  auto weightset = in_context.weightset();
225  auto out_labelset = selects<labelset_t,I...>::get(*in_labelset);
226  out_context_t out_context{out_labelset, *weightset};
227  out_ = make_mutable_automaton(out_context);
228  }
229 
231  {
232  // Copy the states. We cannot iterate on the transitions
233  // only, as we would lose the states without transitions.
234  for (auto s: in_->states()) {
235  out_state[s] = s;
236  out_->add_state(s);
237  if(in_->has_name(s))
238  out_->set_state_name(s, in_->get_state_name(s));
239  }
240  out_state[in_->pre()]= out_->pre();
241  out_state[in_->post()]= out_->post();
242 
243  for (auto t : in_->all_transitions())
244  {
245  auto src = out_state.find(in_->src_of(t));
246  auto dst = out_state.find(in_->dst_of(t));
247  if (src != out_state.end() && dst != out_state.end()){
248  out_->new_transition(src->second, dst->second,
249  sub_tuple<I...>::get(in_->label_of(t)), in_->weight_of(t));
250  }
251  }
252  return out_;
253  }
254 
255  void set_history() {
256  auto history = std::make_shared<single_history<Aut>>(in_);
257  out_->set_history(history);
258  for (auto p: in_->all_states())
259  history->add_state(out_state[p], p);
260  }
261 
263  const Aut& in_;
267  std::unordered_map<state_t, state_t> out_state;
268  };
269  }
270 
279  template <size_t I, typename Tdc>
280  inline
281  auto
282  projection(const Tdc& in, bool keep_history=true) -> typename internal::projector<Tdc, I>::out_automaton_t
283  {
285  auto r= proj();
286  if(keep_history)
287  proj.set_history();
288  return r;
289  }
290 
298  template <typename Tdc>
299  inline
300  auto
301  images(const Tdc& in, bool keep_history=true) -> typename internal::imagers<Tdc>::out_automaton_t
302  {
303  internal::imagers<Tdc> proj(in);
304  auto r= proj();
305  if(keep_history)
306  proj.set_history();
307  return r;
308  }
309 
322  template <size_t... I, typename Tdc>
323  inline
324  auto
325  projections(const Tdc& in, bool keep_history=true) -> typename internal::projectors<Tdc, I...>::out_automaton_t
326  {
327  internal::projectors<Tdc, I...> proj(in);
328  auto r= proj();
329  if(keep_history)
330  proj.set_history();
331  return r;
332  }
333 }}//end of ns awali::stc
334 
335 #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:325
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:282
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:301
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:931
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:148
context_t_of< Aut > context_t
Definition: projection.hh:149
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:154
out_automaton_t operator()()
Definition: projection.hh:168
void set_history()
Definition: projection.hh:193
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:203
weightset_t_of< context_t > weightset_t
Definition: projection.hh:151
typename select_tail< labelset_t >::labelset_t out_labelset_t
Definition: projection.hh:152
labelset_t_of< context_t > labelset_t
Definition: projection.hh:150
imagers(const Aut &in)
Definition: projection.hh:156
const Aut & in_
Input automaton.
Definition: projection.hh:201
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:205
Definition: projection.hh:82
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:88
void set_history()
Definition: projection.hh:127
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:143
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:141
context_t_of< Aut > context_t
Definition: projection.hh:83
const Aut & in_
Input automaton.
Definition: projection.hh:139
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:211
context_t_of< Aut > context_t
Definition: projection.hh:212
out_automaton_t operator()()
Definition: projection.hh:230
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: projection.hh:267
projectors(const Aut &in)
Definition: projection.hh:219
weightset_t_of< context_t > weightset_t
Definition: projection.hh:214
mutable_automaton< out_context_t > out_automaton_t
Definition: projection.hh:217
mutable_automaton< out_context_t > out_
Output automaton.
Definition: projection.hh:265
void set_history()
Definition: projection.hh:255
labelset_t_of< context_t > labelset_t
Definition: projection.hh:213
const Aut & in_
Input automaton.
Definition: projection.hh:263
typename selects< labelset_t, I... >::labelset_t out_labelset_t
Definition: projection.hh:215
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