Vcsn  2.2a
Be Rational
grail.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 #include <boost/range/algorithm/sort.hpp>
6 
8 #include <vcsn/algos/sort.hh> // transition_less.
9 #include <vcsn/dyn/fwd.hh>
10 #include <vcsn/misc/escape.hh>
11 #include <vcsn/weightset/fwd.hh> // b
13 
14 namespace vcsn
15 {
16 
17  namespace detail
18  {
19 
20  /*-----------.
21  | printer. |
22  `-----------*/
23 
27  template <Automaton Aut>
28  class printer
29  {
30  protected:
31  using automaton_t = Aut;
32 
33  public:
34  printer(const automaton_t& aut, std::ostream& out)
35  : aut_(aut)
36  , os_(out)
37  {}
38 
39  // Should not be public, but needed by GCC 4.8.1.
40  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
42 
43  protected:
51 
53  using states_t = std::vector<state_t>;
54 
56  virtual std::string
57  label_(const label_t& l) const
58  {
59  return ls_.is_one(l) ? "@epsilon" : to_string(ls_, l);
60  }
61 
64  virtual void print_transition_(transition_t t) const
65  {
66  aut_->print_state(aut_->src_of(t), os_);
67  os_ << ' ' << label_(aut_->label_of(t)) << ' ';
68  aut_->print_state(aut_->dst_of(t), os_);
69  }
70 
72  void print_state_(const state_t s)
73  {
74  std::vector<transition_t> ts;
75  for (auto t : out(aut_, s))
76  ts.emplace_back(t);
78  for (auto t : ts)
79  {
80  os_ << '\n';
82  }
83  }
84 
88  {
89  for (auto s: aut_->states())
90  print_state_(s);
91  }
92 
94  void list_states_(const states_t& ss)
95  {
96  for (auto s: ss)
97  {
98  os_ << ' ';
99  aut_->print_state(s, os_);
100  }
101  }
102 
105  {
106  states_t res;
107  for (auto t: initial_transitions(aut_))
108  res.emplace_back(aut_->dst_of(t));
109  boost::sort(res);
110  return res;
111  }
112 
115  {
116  states_t res;
117  for (auto t: final_transitions(aut_))
118  res.emplace_back(aut_->src_of(t));
119  boost::sort(res);
120  return res;
121  }
122 
126  std::ostream& os_;
128  const labelset_t_of<automaton_t>& ls_ = *aut_->labelset();
130  const weightset_t& ws_ = *aut_->weightset();
132  const polynomialset_t ps_{aut_->context()};
133  };
134 
135  }
136 
137 
138  /*---------.
139  | fadoer. |
140  `---------*/
141 
142  namespace detail
143  {
144 
148  template <Automaton Aut>
149  class fadoer: public printer<Aut>
150  {
152  "fado: requires letter or nullable labels");
153  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
154  "fado: requires Boolean weights");
155 
157 
158  using super_t::aut_;
159  using super_t::finals_;
160  using super_t::initials_;
161  using super_t::list_states_;
162  using super_t::os_;
164  using super_t::ws_;
165 
166  using super_t::super_t;
167 
168  public:
170  // http://www.dcc.fc.up.pt/~rvr/FAdoDoc/_modules/fa.html#saveToFile
171  void operator()()
172  {
173  bool is_deter = is_deterministic_(aut_);
174  os_ << (is_deter ? "@DFA" : "@NFA");
176  if (!is_deter)
177  {
178  os_ << " *";
180  }
182  }
183 
184  private:
185  template <Automaton A>
186  std::enable_if_t<labelset_t_of<A>::is_free(), bool>
187  is_deterministic_(const A& a)
188  {
189  return vcsn::is_deterministic(a);
190  }
191 
192  template <Automaton A>
193  std::enable_if_t<!labelset_t_of<A>::is_free(), bool>
195  {
196  return false;
197  }
198  };
199 
200  }
201 
202  template <Automaton Aut>
203  std::ostream&
204  fado(const Aut& aut, std::ostream& out)
205  {
206  auto fado = detail::fadoer<Aut>{aut, out};
207  fado();
208  return out;
209  }
210 
211 
212 
213  /*----------.
214  | grailer. |
215  `----------*/
216 
217  namespace detail
218  {
224  template <Automaton Aut>
225  class grailer: public printer<Aut>
226  {
228  "grail: requires letter or nullable labels");
229  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
230  "grail: requires Boolean weights");
231 
233 
234  using typename super_t::automaton_t;
235  using typename super_t::state_t;
236  using typename super_t::transition_t;
237 
238  using super_t::aut_;
239  using super_t::finals_;
240  using super_t::initials_;
241  using super_t::os_;
243  using super_t::ws_;
244 
245  using super_t::super_t;
246 
247  public:
249  void operator()()
250  {
251  // Don't end with a \n.
252  const char* sep = "";
253  for (auto s: initials_())
254  {
255  os_ << sep
256  << "(START) |- ";
257  aut_->print_state(s, os_);
258  sep = "\n";
259  }
261  for (auto s: finals_())
262  {
263  os_ << '\n';
264  aut_->print_state(s, os_) << " -| (FINAL)";
265  }
266  }
267  };
268  }
269 
270  template <Automaton Aut>
271  std::ostream&
272  grail(const Aut& aut, std::ostream& out)
273  {
274  auto grail = detail::grailer<Aut>{aut, out};
275  grail();
276  return out;
277  }
278 }
weightset_t_of< automaton_t > weightset_t
Definition: grail.hh:47
auto initial_transitions(const Aut &aut) -> decltype(aut->all_out(aut->pre()))
Indexes of transitions to (visible) initial states.
Definition: automaton.hh:137
states_t initials_()
The list of initial states, sorted.
Definition: grail.hh:104
std::enable_if_t< labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &a)
Definition: grail.hh:187
weight_t_of< automaton_t > weight_t
Definition: grail.hh:48
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
Compare transitions of an automaton.
Definition: sort.hh:23
Factor common bits in automaton formatting.
Definition: grail.hh:28
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:57
auto sort(const Aut &a) -> permutation_automaton< Aut >
Definition: sort.hh:163
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:130
label_t_of< automaton_t > label_t
Definition: grail.hh:45
std::vector< state_t > states_t
A list of states.
Definition: grail.hh:53
virtual void print_transition_(transition_t t) const
Output the transition t.
Definition: grail.hh:64
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:249
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: grail.hh:128
state_t_of< automaton_t > state_t
Definition: grail.hh:41
Format an automaton into Fado.
Definition: grail.hh:149
auto final_transitions(const Aut &aut) -> decltype(aut->all_in(aut->post()))
Indexes of transitions from (visible) final states.
Definition: automaton.hh:148
std::ostream & grail(const Aut &aut, std::ostream &out)
Definition: grail.hh:272
typename polynomialset_t::value_t polynomial_t
Definition: grail.hh:50
printer(const automaton_t &aut, std::ostream &out)
Definition: grail.hh:34
context_t_of< automaton_t > context_t
Definition: grail.hh:44
void print_transitions_()
Output transitions, sorted lexicographically.
Definition: grail.hh:87
virtual std::string label_(const label_t &l) const
Convert a label to its representation.
Definition: grail.hh:57
automaton_t aut_
The automaton we have to output.
Definition: grail.hh:124
std::ostream & fado(const Aut &aut, std::ostream &out)
Definition: grail.hh:204
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:171
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: grail.hh:94
std::ostream & os_
Output stream.
Definition: grail.hh:126
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
states_t finals_()
The list of final states, sorted.
Definition: grail.hh:114
std::enable_if_t<!labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &)
Definition: grail.hh:194
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:56
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
Format an automaton into Fado.
Definition: grail.hh:225
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
void print_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: grail.hh:72
const polynomialset_t ps_
Short-hand to the polynomialset used to print the entries.
Definition: grail.hh:132
transition_t_of< automaton_t > transition_t
Definition: grail.hh:46
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
Definition: a-star.hh:8