Awali
Another Weighted Automata library
lal_lan_conversion.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_LAL_LAN_CONVERSION_HH
18 # define AWALI_ALGOS_LAL_LAN_CONVERSION_HH
19 
22 
23 namespace awali {
24  namespace sttc {
25  namespace internal {
26  template<typename Aut, typename L> struct dispatch_lal_lan{
27  using l_automaton_t = Aut;
28  using n_automaton_t = Aut;
29  static const Aut proper_(const Aut& aut, direction_t dir, bool prune, bool keep_history) {
30  raise("to_lal: ","unsupported operation");
31  }
32 
33  static Aut allow_eps_(const Aut& aut, bool keep_history) {
34  raise("to_lan: ","unsupported operation");
35  }
36  };
37 
38  template<typename Aut, typename T>
39  struct dispatch_lal_lan<Aut,letterset<T>> {
42  using l_automaton_t = Aut;
44 
45  static const Aut proper_(const Aut& aut, direction_t, bool /*prune*/, bool keep_history) {
46  return copy(aut, keep_history);
47  }
48 
49  static n_automaton_t allow_eps_(const Aut& aut, bool keep_history) {
50  auto res = sttc::make_mutable_automaton<n_context_t>(sttc::get_nullable_context(aut->context()));
51  sttc::copy_into(aut,res, keep_history);
52  return res;
53  }
54  };
55 
56  template<typename Aut, typename T>
57  struct dispatch_lal_lan<Aut,nullableset<T>> {
58  using letterset_t = typename labelset_trait<nullableset<T>>::not_nullable_t;
61  using n_automaton_t = Aut;
62 
63  static l_automaton_t proper_(const Aut& aut, direction_t dir, bool prune, bool keep_history) {
64  auto tmp = sttc::proper(aut, dir, prune, keep_history);
65  auto res = sttc::make_mutable_automaton<l_context_t>(sttc::get_not_nullable_context(aut->context()));
66  sttc::copy_into(tmp,res, keep_history);
67  if(keep_history) {
68  auto final_history = std::make_shared<single_history<n_automaton_t>>(aut);
69  auto & tmp_history = tmp->history()->template as<single_history<n_automaton_t>>();
70  auto & res_history = res->history()->template as<single_history<n_automaton_t>>();
71  for(auto s: res->states())
72  final_history->add_state(s,tmp_history.get_state(res_history.get_state(s)));
73  tmp->strip_history();
74  res->set_history(final_history);
75  }
76  return res;
77  }
78 
79  static const Aut allow_eps_(const Aut& aut, bool keep_history) {
80  return copy(aut, keep_history);
81  }
82  };
83  }
84 
85  template<typename Aut>
86  auto
87  to_lal(const Aut& aut, direction_t dir = BACKWARD,
88  bool prune = true, bool keep_history=true) -> typename internal::dispatch_lal_lan<Aut,labelset_t_of<Aut>>::l_automaton_t {
89  return internal::dispatch_lal_lan<Aut,labelset_t_of<Aut>>::proper_(aut, dir, prune, keep_history);
90  }
91 
92  template<typename Aut>
93  auto
94  to_lan(const Aut& aut, bool keep_history=true) -> typename internal::dispatch_lal_lan<Aut,labelset_t_of<Aut>>::n_automaton_t {
95  return internal::dispatch_lal_lan<Aut,labelset_t_of<Aut>>::allow_eps_(aut, keep_history);
96  }
97  }
98 }//end of ns awali::stc
99 
100 #endif // !AWALI_ALGOS_LAL_LAN_CONVERSION_HH
carries the algebraic settings of automata
Definition: context.hh:40
Implementation of labels are letters.
Definition: letterset.hh:43
Implementation of labels are nullables (letter or empty).
Definition: nullableset.hh:189
direction_t
Used in some algorithms in which one may considers transitions forward or backwards.
Definition: enums.hh:35
@ BACKWARD
Definition: enums.hh:37
auto proper(const Aut &aut, direction_t dir=BACKWARD, bool prune=true, bool keep_history=true) -> decltype(copy(aut))
Eliminate spontaneous transitions.
Definition: proper.hh:444
void copy_into(const AutIn &in, AutOut &out, Pred keep_state, bool keep_history=true, bool transpose=false)
Copy an automaton.
Definition: copy.hh:144
auto get_not_nullable_context(const Context &ctx) -> not_nullable_of< Context >
Definition: traits.hh:309
auto to_lan(const Aut &aut, bool keep_history=true) -> typename internal::dispatch_lal_lan< Aut, labelset_t_of< Aut >>::n_automaton_t
Definition: lal_lan_conversion.hh:94
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
auto to_lal(const Aut &aut, direction_t dir=BACKWARD, bool prune=true, bool keep_history=true) -> typename internal::dispatch_lal_lan< Aut, labelset_t_of< Aut >>::l_automaton_t
Definition: lal_lan_conversion.hh:87
auto get_nullable_context(const Context &ctx) -> nullable_of< Context >
Definition: traits.hh:302
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
Main namespace of Awali.
Definition: ato.hh:22
static const Aut proper_(const Aut &aut, direction_t, bool, bool keep_history)
Definition: lal_lan_conversion.hh:45
typename labelset_trait< letterset< T > >::nullable_t nullable_t
Definition: lal_lan_conversion.hh:40
Aut l_automaton_t
Definition: lal_lan_conversion.hh:42
mutable_automaton< n_context_t > n_automaton_t
Definition: lal_lan_conversion.hh:43
static n_automaton_t allow_eps_(const Aut &aut, bool keep_history)
Definition: lal_lan_conversion.hh:49
static l_automaton_t proper_(const Aut &aut, direction_t dir, bool prune, bool keep_history)
Definition: lal_lan_conversion.hh:63
Aut n_automaton_t
Definition: lal_lan_conversion.hh:61
mutable_automaton< l_context_t > l_automaton_t
Definition: lal_lan_conversion.hh:60
typename labelset_trait< nullableset< T > >::not_nullable_t letterset_t
Definition: lal_lan_conversion.hh:58
static const Aut allow_eps_(const Aut &aut, bool keep_history)
Definition: lal_lan_conversion.hh:79
Definition: lal_lan_conversion.hh:26
Aut n_automaton_t
Definition: lal_lan_conversion.hh:28
static const Aut proper_(const Aut &aut, direction_t dir, bool prune, bool keep_history)
Definition: lal_lan_conversion.hh:29
Aut l_automaton_t
Definition: lal_lan_conversion.hh:27
static Aut allow_eps_(const Aut &aut, bool keep_history)
Definition: lal_lan_conversion.hh:33
trait that computes the related types of a labelset
Definition: traits.hh:34