Awali
Another Weighted Automata library
letterize.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_LETTERIZE_HH
18 #define AWALI_ALGOS_LETTERIZE_HH
19 
22 #include <awali/sttc/misc/add_epsilon_trans.hh> // is_epsilon
23 #include<unordered_map>
24 
25 /*
26  Convert a law automaton into a lan automaton.
27  Proper can then be applied to get a lal automaton.
28 */
29 
30 namespace awali {
31  namespace sttc {
32  namespace internal {
33 
34  template <typename Aut, typename Labelset>
35  struct letterizer {
36  using ret_automaton_t = Aut;
37 
38  static ret_automaton_t letterize(const Aut& aut, bool keep_history) {
39  return copy(aut, keep_history);
40  }
41  };
42 
43 
44  template <typename Aut, typename L>
45  struct letterizer<Aut,wordset<L>> {
46  using automaton_t = Aut;
53 
54  static ret_automaton_t letterize(const automaton_t& aut, bool keep_history) {
55  nletterset_t nletset = get_nullableset(get_letterset(*aut->context().labelset()));
56  auto ret= make_mutable_automaton(ret_context_t{nletset,*aut->context().weightset()});
57  std::unordered_map<state_t,state_t> state_map;
58  for(auto s : aut->states()) {
59  state_map[s]=ret->add_state();
60  }
61  state_map[ret->pre()]=aut->pre();
62  state_map[ret->post()]=aut->post();
63  for(auto tr: aut->all_transitions()) {
64  const auto& w=aut->label_of(tr);
65  if(w.length()==0)
66  new_epsilon_trans(ret, state_map[aut->src_of(tr)], state_map[aut->dst_of(tr)], aut->weight_of(tr));
67  else if (w.length()==1)
68  ret->new_transition(state_map[aut->src_of(tr)], state_map[aut->dst_of(tr)], w[0], aut->weight_of(tr));
69  else {
70  state_t dst=state_map[aut->dst_of(tr)];
71  for(unsigned i=w.length()-1; i>0;i--) {
72  state_t src=ret->add_state();
73  ret->new_transition(src, dst, w[i]);
74  dst=src;
75  }
76  ret->new_transition(state_map[aut->src_of(tr)], dst, w[0], aut->weight_of(tr));
77  }
78  }
79  if(keep_history) {
80  auto history = std::make_shared<single_history<automaton_t>>(aut);
81  for(auto p : state_map) {
82  history->add_state(p.second,p.first);
83  if(aut->has_name(p.first)) {
84  ret->set_state_name(p.second, aut->get_state_name(p.first));
85  }
86  }
87  ret->set_history(history);
88  }
89  return ret;
90  }
91  };
92  }//end of internal
93 
94  template<typename Aut>
95  auto letterize(const Aut& aut, bool keep_history=true) -> typename internal::letterizer<Aut,labelset_t_of<Aut>>::ret_automaton_t {
97  }
98 
99  }
100 }//end of ns awali::stc
101 
102 #endif
carries the algebraic settings of automata
Definition: context.hh:40
context(const context &that)
Definition: context.hh:67
Implementation of labels are words.
Definition: wordset.hh:35
weightset_description weightset(const std::string &k)
auto get_nullableset(const L &labelset) -> typename labelset_trait< L >::nullable_t
Definition: traits.hh:246
auto get_letterset(const L &labelset) -> typename labelset_trait< L >::letterset_t
Definition: traits.hh:225
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 letterize(const Aut &aut, bool keep_history=true) -> typename internal::letterizer< Aut, labelset_t_of< Aut >>::ret_automaton_t
Definition: letterize.hh:95
AutOut copy(const AutIn &input, Pred keep_state, bool keep_history=true, bool transpose=false)
A copy of input keeping only its states that are accepted by keep_state.
Definition: copy.hh:189
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
transition_t new_epsilon_trans(Aut a, state_t src, state_t dst, weight_t_of< Aut > w)
Helper to create a new epsilon transition.
Definition: add_epsilon_trans.hh:165
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
unsigned state_t
Definition: types.hh:21
typename labelset_trait< labelset_t >::letterset_t letterset_t
Definition: letterize.hh:49
static ret_automaton_t letterize(const automaton_t &aut, bool keep_history)
Definition: letterize.hh:54
weightset_t_of< Aut > weightset_t
Definition: letterize.hh:48
labelset_t_of< Aut > labelset_t
Definition: letterize.hh:47
typename labelset_trait< letterset_t >::nullable_t nletterset_t
Definition: letterize.hh:50
mutable_automaton< ret_context_t > ret_automaton_t
Definition: letterize.hh:52
Definition: letterize.hh:35
static ret_automaton_t letterize(const Aut &aut, bool keep_history)
Definition: letterize.hh:38
Aut ret_automaton_t
Definition: letterize.hh:36
L nullable_t
Definition: traits.hh:35
L letterset_t
Definition: traits.hh:37