Awali
Another Weighted Automata library
partial_identity.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_PARTIAL_IDENTITY_HH
18 # define AWALI_ALGOS_PARTIAL_IDENTITY_HH
19 
20 # include <unordered_map>
21 
23 #include <awali/sttc/misc/set.hh>
28 
29 namespace awali { namespace sttc {
30 
31  /*--------------------------------------------.
32  | partial identity (automaton -> transducer). |
33  `--------------------------------------------*/
34 
35  namespace internal
36  {
37  template <size_t N, typename Labelset>
38  struct multitupleset {
39  using type=typename concat_tupleset
41  typename multitupleset<N-1,Labelset>::type>::type;
42  };
43 
44  template <size_t N>
45  struct multituple {
46  template<typename T>
47  using type = typename std::cons_tuple<T, typename multituple<N-1>::template type<T>>::type;
48 
49  template<typename T>
50  static
51  auto get(const T& v) -> type<T> {
52  return std::tuple_cat(std::make_tuple(v),multituple<N-1>::get(v));
53  }
54  };
55 
56  template <typename Labelset>
57  struct multitupleset<1u, Labelset> {
59  };
60 
61  template <>
62  struct multituple<1u> {
63  template<typename T>
64  using type = std::tuple<T>;
65 
66  template<typename T>
67  static
68  auto get(const T& v) -> type<T> {
69  return std::make_tuple(v);
70  }
71  };
72 
73  template <typename Aut, size_t I>
75  {
82 
83  partial_identiter(const Aut& in)
84  : in_(in)
85  {
86  in_context_t in_context = in->context();
87  auto in_labelset = in_context.labelset();
88  auto weightset = in_context.weightset();
89  out_labelset_t out_labelset{multituple<I>::get(*in_labelset)};
90  out_context_t out_context{out_labelset, *weightset};
91  out_ = make_mutable_automaton(out_context);
92 
93  }
94 
95  void operator()()
96  {
97  // Copy the states. We cannot iterate on the transitions
98  // only, as we would lose the states without transitions.
99  for (auto s: in_->states()) {
100  out_state[s] = s;
101  out_->add_state(s);
102  if(in_->has_name(s))
103  out_->set_state_name(s, in_->get_state_name(s));
104  }
105  out_state[in_->pre()]= out_->pre();
106  out_state[in_->post()]= out_->post();
107 
108  for (auto t : in_->all_transitions())
109  {
110  auto src = out_state.find(in_->src_of(t));
111  auto dst = out_state.find(in_->dst_of(t));
112  if (src != out_state.end() && dst != out_state.end()){
113  out_->new_transition(src->second, dst->second,
114  multituple<I>::get(in_->label_of(t)), in_->weight_of(t));
115  }
116  }
117  }
118 
119  void set_history() {
120  auto history = std::make_shared<single_history<Aut>>(in_);
121  out_->set_history(history);
122  for (auto p: in_->all_states()) {
123  history->add_state(out_state[p], p);
124  if(in_->has_name(p)) {
125  out_->set_state_name(out_state[p], in_->get_state_name(p));
126  }
127  }
128  }
129 
131  const Aut& in_;
135  std::unordered_map<state_t, state_t> out_state;
136  };
137  }
138 
147  template <size_t I, typename Aut>
148  inline
149  auto
150  partial_identity(const Aut& in, bool keep_history=true) -> typename internal::partial_identiter<Aut, I>::out_automaton_t
151  {
153  id();
154  if(keep_history)
155  id.set_history();
156  return id.out_;
157  }
158 
159 }}//end of ns awali::stc
160 
161 #endif // !AWALI_ALGOS_PARTIAL_IDENTITY_HH
carries the algebraic settings of automata
Definition: context.hh:40
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
weightset_description weightset(const std::string &k)
typename concat_tupleset< tupleset< Labelset >, typename multitupleset< N-1, Labelset >::type >::type type
Definition: partial_identity.hh:41
Definition: tupleset.hh:972
Definition: partial_identity.hh:38
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
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:931
auto partial_identity(const Aut &in, bool keep_history=true) -> typename internal::partial_identiter< Aut, I >::out_automaton_t
Builds a transducer realizing the identity on a language.
Definition: partial_identity.hh:150
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
static auto get(const T &v) -> type< T >
Definition: partial_identity.hh:68
std::tuple< T > type
Definition: partial_identity.hh:64
Definition: partial_identity.hh:45
static auto get(const T &v) -> type< T >
Definition: partial_identity.hh:51
typename std::cons_tuple< T, typename multituple< N-1 >::template type< T > >::type type
Definition: partial_identity.hh:47
Definition: partial_identity.hh:75
void operator()()
Definition: partial_identity.hh:95
const Aut & in_
Input automaton.
Definition: partial_identity.hh:131
typename multitupleset< I, in_labelset_t >::type out_labelset_t
Definition: partial_identity.hh:79
mutable_automaton< out_context_t > out_automaton_t
Definition: partial_identity.hh:81
void set_history()
Definition: partial_identity.hh:119
weightset_t_of< in_context_t > weightset_t
Definition: partial_identity.hh:78
partial_identiter(const Aut &in)
Definition: partial_identity.hh:83
mutable_automaton< out_context_t > out_
Output automaton.
Definition: partial_identity.hh:133
context_t_of< Aut > in_context_t
Definition: partial_identity.hh:76
std::unordered_map< state_t, state_t > out_state
input state -> output state.
Definition: partial_identity.hh:135
labelset_t_of< in_context_t > in_labelset_t
Definition: partial_identity.hh:77
Definition: tuple.hh:359