Awali
Another Weighted Automata library
thompson.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_THOMPSON_HH
18 # define AWALI_ALGOS_THOMPSON_HH
19 
20 #include <awali/sttc/ctx/fwd.hh>
21 #include <awali/sttc/ctx/traits.hh>
24 #include <awali/sttc/misc/raise.hh>
25 #include <awali/sttc/labelset/traits.hh> //nullable_of
26 
27 namespace awali { namespace sttc {
28 
29  namespace rat
30  {
33  template <typename Aut,
34  typename Context = context_t_of<Aut>
35  >
37  : public Context::const_visitor
38  {
39  public:
40  using automaton_t = Aut;
41  using context_t = Context;
44 
45  using super_type = typename Context::const_visitor;
46  using history_t = std::shared_ptr<string_history>;
47 
48  static_assert(context_t::has_one(), "requires nullable labels");
49 
50  constexpr static const char* me() { return "thompson"; }
51 
52  thompson_visitor(const context_t& ctx, bool keep_history)
53  : res_(make_shared_ptr<automaton_t>(ctx)),
54  history_(std::make_shared<string_history>()),
55  keep_history_(keep_history)
56  {}
57 
59  operator()(const typename Context::ratexp_t& v)
60  {
61  if(keep_history_)
62  res_->set_history(history_);
63  v->accept(*this);
64  res_->set_initial(initial_);
65  res_->set_final(final_);
66  return std::move(res_);
67  }
68 
70  {
71  initial_ = res_->add_state();
72  final_ = res_->add_state();
73  res_->set_state_name(initial_,"s"+std::to_string(counter));
74  res_->set_state_name(final_,"t"+std::to_string(counter++));
75  history_->add_state(initial_,"zero");
76  }
77 
79  {
80  initial_ = res_->add_state();
81  final_ = res_->add_state();
82  res_->set_state_name(initial_,"s"+std::to_string(counter));
83  res_->set_state_name(final_,"t"+std::to_string(counter++));
84  res_->new_transition(initial_, final_, epsilon_);
85  history_->add_state(initial_,"one");
86  }
87 
89  {
90  initial_ = res_->add_state();
91  final_ = res_->add_state();
92  res_->set_state_name(initial_,"s"+std::to_string(counter));
93  res_->set_state_name(final_,"t"+std::to_string(counter++));
94  res_->new_transition(initial_, final_, e.value());
95  history_->add_state(initial_,"letter");
96  }
97 
99  {
100  state_t initial = res_->add_state();
101  state_t final = res_->add_state();
102  for (auto c: e)
103  {
104  c->accept(*this);
105  res_->new_transition(initial, initial_, epsilon_);
106  res_->new_transition(final_, final, epsilon_);
107  }
108  initial_ = initial;
109  final_ = final;
110  res_->set_state_name(initial_,"s"+std::to_string(counter));
111  res_->set_state_name(final_,"t"+std::to_string(counter++));
112  history_->add_state(initial_,"sum");
113  }
114 
120 
122  {
123  e.head()->accept(*this);
124  state_t initial = initial_;
125 
126  // Then the remainder.
127  for (auto c: e.tail())
128  {
129  state_t final = final_;
130  c->accept(*this);
131  res_->new_transition(final, initial_, epsilon_);
132  }
133  initial_ = initial;
134  }
135 
137  {
138  e.sub()->accept(*this);
139  state_t initial = res_->add_state();
140  state_t final = res_->add_state();
141  res_->new_transition(initial, initial_, epsilon_);
142  res_->new_transition(final_, final, epsilon_);
143  res_->new_transition(final_, initial_, epsilon_);
144  res_->new_transition(initial, final, epsilon_);
145  initial_ = initial;
146  final_ = final;
147  res_->set_state_name(initial_,"s"+std::to_string(counter));
148  res_->set_state_name(final_,"t"+std::to_string(counter++));
149  history_->add_state(initial_,"star");
150  }
151 
153  {
154  e.sub()->accept(*this);
155  state_t initial = res_->add_state();
156  state_t final = res_->add_state();
157  const weight_t& w = e.weight();
158  res_->new_transition(initial, initial_, epsilon_, w);
159  res_->new_transition(final_, final, epsilon_);
160  initial_ = initial;
161  final_ = final;
162  history_->add_state(initial_,"left wgt");
163  }
164 
166  {
167  e.sub()->accept(*this);
168  state_t initial = res_->add_state();
169  state_t final = res_->add_state();
170  const weight_t& w = e.weight();
171  res_->new_transition(initial, initial_, epsilon_);
172  res_->new_transition(final_, final, epsilon_, w);
173  initial_ = initial;
174  final_ = final;
175  history_->add_state(initial_,"right wgt");
176  }
177 
178  private:
179  automaton_t res_;
180  const weightset_t& ws_ = *res_->weightset();
181  using label_t = label_t_of<automaton_t>;
182  const label_t epsilon_ = res_->labelset()->one();
183  state_t initial_ = automaton_t::element_type::null_state();
184  state_t final_ = automaton_t::element_type::null_state();
185  history_t history_;
186  bool keep_history_;
187  unsigned counter=0;
188  };
189 
190  } // rat::
191 
201  template <typename Aut,
202  typename Context = context_t_of<Aut>>
203  Aut
204  thompson(const Context& ctx, const typename Context::ratexp_t& exp, bool keep_history=true)
205  {
207  return thompson(exp);
208  }
209 
218  template <typename RatExpSet>
219  mutable_automaton<sttc::nullable_of<typename RatExpSet::context_t>>
220  thompson(const RatExpSet& rs, const typename RatExpSet::ratexp_t& exp, bool keep_history=true)
221  {
224  return thompson(exp);
225  }
226 
227  }
228 }//end of ns awali::stc
229 
230 #endif // !AWALI_ALGOS_THOMPSON_HH
The semiring of complex numbers.
Definition: c.hh:44
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: thompson.hh:38
automaton_t operator()(const typename Context::ratexp_t &v)
Definition: thompson.hh:59
AWALI_RAT_VISIT(atom, e)
Definition: thompson.hh:88
AWALI_RAT_VISIT(sum, e)
Definition: thompson.hh:98
AWALI_RAT_VISIT(zero,)
Definition: thompson.hh:69
thompson_visitor(const context_t &ctx, bool keep_history)
Definition: thompson.hh:52
AWALI_RAT_VISIT(lweight, e)
Definition: thompson.hh:152
constexpr static const char * me()
Definition: thompson.hh:50
AWALI_RAT_VISIT(star, e)
Definition: thompson.hh:136
AWALI_RAT_VISIT(rweight, e)
Definition: thompson.hh:165
weightset_t_of< context_t > weightset_t
Definition: thompson.hh:42
typename Context::const_visitor super_type
Definition: thompson.hh:45
weight_t_of< context_t > weight_t
Definition: thompson.hh:43
AWALI_RAT_VISIT(one,)
Definition: thompson.hh:78
std::shared_ptr< string_history > history_t
Definition: thompson.hh:46
Context context_t
Definition: thompson.hh:41
Aut automaton_t
Definition: thompson.hh:40
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
Definition: string_history.hh:29
std::string to_string(identities i)
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
Aut thompson(const Context &ctx, const typename Context::ratexp_t &exp, bool keep_history=true)
builds a Thompson automaton
Definition: thompson.hh:204
SharedPtr make_shared_ptr(Args &&... args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
Definition: memory.hh:29
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
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
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
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73