Awali
Another Weighted Automata library
compact_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_COMPACT_THOMPSON_HH
18 # define AWALI_ALGOS_COMPACT_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 "compact-thompson"; }
51 
52  compact_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_,"i");
74  res_->set_state_name(final_,"s"+std::to_string(counter++));
75  history_->add_state(final_,"zero");
76  }
77 
79  {
80  initial_ = res_->add_state();
81  final_ = res_->add_state();
82  res_->set_state_name(initial_,"i");
83  res_->set_state_name(final_,"s"+std::to_string(counter++));
84  res_->new_transition(initial_, final_, epsilon_);
85  history_->add_state(final_,"one");
86  }
87 
89  {
90  initial_ = res_->add_state();
91  final_ = res_->add_state();
92  res_->set_state_name(initial_,"i");
93  res_->set_state_name(final_,"s"+std::to_string(counter++));
94  res_->new_transition(initial_, final_, e.value());
95  history_->add_state(final_,"letter");
96  }
97 
99  {
100  e.head()->accept(*this);
101  state_t initial = initial_;
102  state_t final = final_;
103  for (auto c: e.tail())
104  {
105  c->accept(*this);
106  for (auto t: res_->out(initial_))
107  res_->new_transition(initial,
108  res_->dst_of(t),
109  res_->label_of(t),
110  res_->weight_of(t));
111  for (auto t: res_->in(final_))
112  res_->new_transition(res_->src_of(t),
113  final,
114  res_->label_of(t),
115  res_->weight_of(t));
116  res_->del_state(initial_);
117  res_->del_state(final_);
118  }
119  initial_ = initial;
120  final_ = final;
121  history_->add_state(final_,"sum");
122  }
123 
129 
131  {
132  e.head()->accept(*this);
133  state_t initial = initial_;
134  // Then the remainder.
135  for (auto c: e.tail())
136  {
137  state_t final = final_;
138  c->accept(*this);
139  for (auto t: res_->out(initial_))
140  res_->new_transition(final,
141  res_->dst_of(t),
142  res_->label_of(t),
143  res_->weight_of(t));
144  res_->del_state(initial_);
145  }
146  initial_ = initial;
147  }
148 
150  {
151  e.sub()->accept(*this);
152  state_t initial = res_->add_state();
153  state_t final = res_->add_state();
154  for (auto t: res_->out(initial_))
155  res_->new_transition(final_,
156  res_->dst_of(t),
157  res_->label_of(t),
158  res_->weight_of(t));
159  res_->del_state(initial_);
160  res_->new_transition(initial, final_, epsilon_);
161  res_->new_transition(final_, final, epsilon_);
162  initial_ = initial;
163  final_ = final;
164  res_->set_state_name(initial_,"i");
165  res_->set_state_name(final_,"s"+std::to_string(counter++));
166  history_->add_state(final_,"star");
167  }
168 
170  {
171  e.sub()->accept(*this);
172 
173  const weight_t& w = e.weight();
174  for (auto t: res_->out(initial_))
175  res_->set_weight(t, ws_.mul(w, res_->weight_of(t)));
176  }
177 
179  {
180  e.sub()->accept(*this);
181 
182  const weight_t& w = e.weight();
183  for (auto t: res_->in(final_))
184  res_->set_weight(t, ws_.mul(res_->weight_of(t), w));
185  }
186 
187  private:
188  automaton_t res_;
189  const weightset_t& ws_ = *res_->weightset();
190  using label_t = label_t_of<automaton_t>;
191  const label_t epsilon_ = res_->labelset()->one();
192  state_t initial_ = automaton_t::element_type::null_state();
193  state_t final_ = automaton_t::element_type::null_state();
194  history_t history_;
195  bool keep_history_;
196  unsigned counter=0;
197  };
198 
199  } // rat::
200 
231  template <typename Aut,
232  typename Context = context_t_of<Aut>>
233  Aut
234  compact_thompson(const Context& ctx, const typename Context::ratexp_t& exp, bool keep_history=true)
235  {
236  rat::compact_thompson_visitor<Aut, Context> cthompson{ctx,keep_history};
237  return cthompson(exp);
238  }
239 
248  template <typename RatExpSet>
249  mutable_automaton<sttc::nullable_of<typename RatExpSet::context_t>>
250  compact_thompson(const RatExpSet& rs, const typename RatExpSet::ratexp_t& exp, bool keep_history=true)
251  {
254  return cthompson(exp);
255  }
256 
257 
258 
259 }}//end of ns awali::stc
260 
261 #endif // !AWALI_ALGOS_COMPACT_THOMPSON_HH
The semiring of complex numbers.
Definition: c.hh:44
Definition: ratexp.hh:280
Definition: compact_thompson.hh:38
AWALI_RAT_VISIT(one,)
Definition: compact_thompson.hh:78
AWALI_RAT_VISIT(sum, e)
Definition: compact_thompson.hh:98
weightset_t_of< context_t > weightset_t
Definition: compact_thompson.hh:42
constexpr static const char * me()
Definition: compact_thompson.hh:50
AWALI_RAT_VISIT(star, e)
Definition: compact_thompson.hh:149
weight_t_of< context_t > weight_t
Definition: compact_thompson.hh:43
Aut automaton_t
Definition: compact_thompson.hh:40
automaton_t operator()(const typename Context::ratexp_t &v)
Definition: compact_thompson.hh:59
AWALI_RAT_VISIT(lweight, e)
Definition: compact_thompson.hh:169
compact_thompson_visitor(const context_t &ctx, bool keep_history)
Definition: compact_thompson.hh:52
AWALI_RAT_VISIT(rweight, e)
Definition: compact_thompson.hh:178
AWALI_RAT_VISIT(atom, e)
Definition: compact_thompson.hh:88
AWALI_RAT_VISIT(zero,)
Definition: compact_thompson.hh:69
std::shared_ptr< string_history > history_t
Definition: compact_thompson.hh:46
Context context_t
Definition: compact_thompson.hh:41
typename Context::const_visitor super_type
Definition: compact_thompson.hh:45
Definition: ratexp.hh:262
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 compact_thompson(const Context &ctx, const typename Context::ratexp_t &exp, bool keep_history=true)
builds a compact variant of the Thompson automaton
Definition: compact_thompson.hh:234
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