Vcsn  2.8
Be Rational
to-automaton.hh
Go to the documentation of this file.
1 #pragma once
2 
3 namespace vcsn
4 {
6  template <Automaton Aut>
7  Aut&
8  to_automaton(const LabelSet& ls, const typename LabelSet::word_t& w)
10  {
11 
12  }
13 
15  template <Automaton Aut>
16  auto
17  eliminate_state(const Aut& aut,
18  state_t_of<Aut> s = Aut::element_type::null_state())
20  {
21  // Get a copy, but be sure to keep the correspondance bw original
22  // state numbers and the new ones.
23  auto res = make_fresh_automaton<Aut>(aut);
24  auto copy = make_copier(aut, res);
25  copy();
26  if (s != aut->null_state())
27  {
28  require(aut->has_state(s), "not a valid state: ", s);
29  s = copy.state_map().at(s);
30  }
31  return eliminate_state_here(res, s);
32  }
33 
34 
35  /*-----------------------.
36  | dyn::eliminate_state. |
37  `-----------------------*/
38 
39  namespace dyn
40  {
41  namespace detail
42  {
44  template <Automaton Aut, typename Int>
45  automaton
46  eliminate_state(const automaton& aut, int state)
47  {
48  const auto& a = aut->as<Aut>();
49  using state_t = state_t_of<Aut>;
50  auto s = 0 <= state ? state_t(state + 2) : a->null_state();
51  return vcsn::eliminate_state(a, s);
52  }
53  }
54  }
55 
56 
57  /*----------------------------.
58  | to_expression(automaton). |
59  `----------------------------*/
60 
61  template <Automaton Aut,
62  typename Profiler,
63  typename ExpSet = expressionset<context_t_of<Aut>>>
64  typename ExpSet::value_t
65  to_expression(Aut& a, Profiler& profiler)
66  {
67  try
68  {
69  auto eliminate_states = detail::make_state_eliminator(a, profiler);
70  eliminate_states();
71  return a->get_initial_weight(a->post());
72  }
73  catch (const std::runtime_error& e)
74  {
75  raise(e, " while making expression");
76  }
77  }
78 
80  {
81  best,
82  delgado,
84  naive,
85  };
86 
87  template <Automaton Aut,
88  typename ExpSet = expressionset<context_t_of<Aut>>>
89  typename ExpSet::value_t
92  {
93  // State elimination is performed on the lifted automaton.
94  auto a = lift(aut, ids);
95  using delgado_t = detail::delgado_profiler<decltype(a)>;
97  switch (algo)
98  {
100  raise("next_state: invalid algorithm: best");
101 
103  {
104  auto profiler = delgado_t(a);
105  return to_expression<decltype(a), delgado_t, ExpSet>(a, profiler);
106  }
107 
109  {
110  auto profiler = delgado_t(a, true);
111  return to_expression<decltype(a), delgado_t, ExpSet>(a, profiler);
112  }
113 
115  {
116  auto profiler = naive_t(a);
117  return to_expression<decltype(a), naive_t, ExpSet>(a, profiler);
118  }
119  }
121  }
122 
123  template <Automaton Aut,
124  typename ExpSet = expressionset<context_t_of<Aut>>>
125  typename ExpSet::value_t
128  {
130  {
131  typename ExpSet::value_t best;
132  auto best_size = std::numeric_limits<size_t>::max();
136  {
137  auto r = to_expression_heuristic<Aut, ExpSet>(aut, ids, a);
138  auto s = rat::size<ExpSet>(r);
139  if (s < best_size)
140  {
141  best = r;
142  best_size = s;
143  }
144  }
145  return best;
146  }
147  else
148  {
149  return to_expression_heuristic<Aut, ExpSet>(aut, ids, algo);
150  }
151  }
152 
153  template <Automaton Aut,
154  typename ExpSet = expressionset<context_t_of<Aut>>>
155  typename ExpSet::value_t
157  const std::string& algo)
158  {
159  static const auto map = getarg<to_expression_heuristic_t>
160  {
161  "heuristics",
162  {
163  {"auto", "best"},
166  {"delgado_label", to_expression_heuristic_t::delgado_label},
168  }
169  };
170  return to_expression<Aut, ExpSet>(a, ids, map[algo]);
171  }
172 
173  /*----------------------------.
174  | to_expression(automaton). |
175  `----------------------------*/
176 
177  namespace dyn
178  {
179  namespace detail
180  {
182  template <Automaton Aut, typename Identities, typename String>
183  expression
185  const std::string& algo)
186  {
187  const auto& a = aut->as<Aut>();
188  using context_t = context_t_of<Aut>;
189  using expressionset_t = vcsn::expressionset<context_t>;
190  auto rs = expressionset_t{a->context(), ids};
191  auto res = ::vcsn::to_expression(a, ids, algo);
192  return {rs, res};
193  }
194  }
195  }
196 
197  /*------------------------.
198  | to_expression(label). |
199  `------------------------*/
200 
201  namespace dyn
202  {
203  namespace detail
204  {
206  template <typename Context, typename Identities, typename Label>
207  expression
209  const label& lbl)
210  {
211  const auto& c = ctx->as<Context>();
212  const auto& l = lbl->as<Label>();
213  auto rs = vcsn::make_expressionset(c, ids);
214  return {rs, rs.atom(l.value())};
215  }
216  }
217  }
218 
219 
220  /*-------------------------------.
221  | to_expression(letter_class). |
222  `-------------------------------*/
223 
224  namespace detail
225  {
226  template <typename ExpSet>
227  auto letter_class_impl(const ExpSet&,
228  const letter_class_t&, bool,
229  std::false_type, std::true_type)
230  -> typename ExpSet::value_t
231  {
232  raise("letter_class: not implemented (is_expressionset)");
233  }
234 
235  template <typename ExpSet>
236  auto letter_class_impl(const ExpSet& rs,
237  const letter_class_t& chars, bool accept,
238  std::false_type, std::false_type)
239  -> typename ExpSet::value_t
240  {
241  auto ls = *rs.labelset();
242 
243  using labelset_t = decltype(ls);
244  using letter_t = typename labelset_t::letter_t;
245 
246  auto ccs = std::set<std::pair<letter_t, letter_t>>{};
247  for (const auto& cc: chars)
248  {
249  std::istringstream i1{cc.first};
250  std::istringstream i2{cc.second};
251  letter_t l1 = ls.get_letter(i1, false);
252  letter_t l2 = ls.get_letter(i2, false);
253  ccs.emplace(l1, l2);
254  }
255  return rs.letter_class(ccs, accept);
256  }
257 
258  template <typename ExpSet>
259  auto letter_class_impl(const ExpSet& rs,
260  const letter_class_t&, bool,
261  std::true_type, std::false_type)
262  -> typename ExpSet::value_t
263  {
264  return rs.one();
265  }
266  }
267 
277  template <typename ExpressionSet>
278  typename ExpressionSet::value_t
279  to_expression(const ExpressionSet& rs, const letter_class_t& letters,
280  bool accept = true)
281  {
282  using labelset_t = labelset_t_of<ExpressionSet>;
283  using is_one_t = std::is_same<labelset_t, vcsn::oneset>;
285  return detail::letter_class_impl(rs, letters, accept,
286  is_one_t{}, is_expset_t{});
287  }
288 
289  namespace dyn
290  {
291  namespace detail
292  {
294  template <typename Context, typename Identities,
295  typename Letters, typename Bool>
296  expression
298  const letter_class_t& letters, bool accept)
299  {
300  const auto& c = ctx->as<Context>();
301  auto rs = vcsn::make_expressionset(c, ids);
302  return {rs, to_expression(rs, letters, accept)};
303  }
304  }
305  }
306 } // vcsn::
A dyn automaton.
Definition: automaton.hh:17
std::shared_ptr< detail::mutable_automaton_impl< Context > > mutable_automaton
Definition: fwd.hh:25
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
typename detail::state_t_of_impl< base_t< ValueSet > >::type state_t_of
Definition: traits.hh:64
auto & as()
Extract wrapped typed value.
Definition: value.hh:55
Aut & to_automaton(const LabelSet &ls, const typename LabelSet::word_t &w) -> mutable_automaton<>
In place removal of state s from automaton res.
Definition: to-automaton.hh:8
state_eliminator< Aut, Profiler > make_state_eliminator(Aut &a, Profiler &profiler)
ExpressionSet::value_t to_expression(const ExpressionSet &rs, const letter_class_t &letters, bool accept=true)
An expression matching one letter in a letter class.
An expressionset can implement several different sets of identities on expressions.
Definition: identities.hh:20
typename detail::context_t_of_impl< base_t< ValueSet > >::type context_t_of
Definition: traits.hh:61
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
auto letter_class_impl(const ExpSet &, const letter_class_t &, bool, std::false_type, std::true_type) -> typename ExpSet::value_t
Case where labelset is an expressionset.
Aut & eliminate_state_here(Aut &res, state_t_of< Aut > s=Aut::element_type::null_state())
In place removal of state s from automaton res.
std::integral_constant< bool, B > bool_constant
Definition: type_traits.hh:12
A dyn Value/ValueSet.
Definition: fwd.hh:29
auto letter_class_impl(const ExpSet &rs, const letter_class_t &, bool, std::true_type, std::false_type) -> typename ExpSet::value_t
Case where labelset is oneset.
auto copy(const AutIn &input, KeepState keep_state, KeepTrans keep_trans) -> decltype(keep_state(input->null_state()), keep_trans(input->null_transition()), make_fresh_automaton< AutIn, AutOut >(input))
A copy of input keeping only its states that are accepted by keep_state, and transitions accepted by ...
Definition: copy.hh:322
expression to_expression_class(const context &ctx, identities ids, const letter_class_t &letters, bool accept)
Bridge (to_expression).
Template-less root for contexts.
Definition: context.hh:16
typename detail::labelset_t_of_impl< base_t< ValueSet > >::type labelset_t_of
Definition: traits.hh:63
automaton lift(const automaton &aut, const std::vector< unsigned > &tapes={}, identities ids={})
Lift some tapes of the transducer, or turn an automaton into a spontaneous automaton.
Definition: others.cc:68
Definition: a-star.hh:8
auto & as()
Downcast to the exact type.
Definition: context.hh:36
expression to_expression_label(const context &ctx, identities ids, const label &lbl)
Bridge (to_expression).
std::set< std::pair< std::string, std::string > > letter_class_t
A set of letter ranges.
Definition: fwd.hh:111
detail::copier< AutIn, AutOut > make_copier(const AutIn &in, AutOut &out, bool safe=true)
Build an automaton copier.
Definition: copy.hh:256
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37
auto make_expressionset(const context< LabelSet, WeightSet > &ctx, rat::identities ids={}) -> expressionset< context< LabelSet, WeightSet >>
Shorthand to expressionset constructor.
auto eliminate_state(const Aut &aut, state_t_of< Aut > s=Aut::element_type::null_state()) -> fresh_automaton_t_of< Aut >
A copy of automaton res without the state s.
Definition: to-automaton.hh:17
to_expression_heuristic_t
Definition: to-automaton.hh:79
A mapping from strings to Values.
Definition: getargs.hh:33
Compute a state profile for state-elimination based on connectivity.
automaton eliminate_state(const automaton &aut, int state)
Bridge.
Definition: to-automaton.hh:46
value_impl< detail::expression_tag > expression
Definition: fwd.hh:31
static identities ids(const driver &d)
Get the identities of the driver.
Definition: parse.cc:91
#define Automaton
Definition: automaton.hh:23
ExpSet::value_t to_expression_heuristic(const Aut &aut, vcsn::rat::identities ids, to_expression_heuristic_t algo)
Definition: to-automaton.hh:90
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
Compute a state profile for state-elimination based on the Delgado-Morais heuristic.
typename Aut::element_type::template fresh_automaton_t< Context > fresh_automaton_t_of
Given an automaton type, the type of its copies.
Definition: traits.hh:82
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
return res
Definition: multiply.hh:399
expression to_expression(const automaton &aut, identities ids, const std::string &algo)
Bridge.