Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
grail.hh
Go to the documentation of this file.
1 #ifndef VCSN_ALGOS_GRAIL_HH
2 # define VCSN_ALGOS_GRAIL_HH
3 
4 # include <iostream>
5 # include <map>
6 
8 # include <vcsn/dyn/fwd.hh>
9 # include <vcsn/misc/escape.hh>
10 # include <vcsn/weightset/fwd.hh> // b
12 
13 namespace vcsn
14 {
15 
16  namespace detail
17  {
18 
19  /*------------.
20  | outputter. |
21  `------------*/
22 
26  template <typename Aut>
27  class outputter
28  {
29  protected:
30  using automaton_t = Aut;
31 
32  public:
33  outputter(const automaton_t& aut, std::ostream& out)
34  : aut_(aut)
35  , os_(out)
36  {}
37 
38  // Should not be public, but needed by GCC 4.8.1.
39  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
41 
42  protected:
48 
50  using states_t = std::vector<state_t>;
51 
53  virtual std::string
54  label_(const label_t& l)
55  {
56  return ls_.is_one(l) ? "@epsilon" : to_string(ls_, l);
57  }
58 
62  {
63  aut_->print_state(aut_->src_of(t), os_);
64  os_ << ' ' << label_(aut_->label_of(t)) << ' ';
65  aut_->print_state(aut_->dst_of(t), os_);
66  }
67 
73  std::ostream& print_entry_(state_t src, state_t dst,
74  std::ostream& os,
75  const std::string& fmt = "text")
76  {
77  auto entry = get_entry(aut_, src, dst);
78  return ps_.print(entry, os, fmt, ", ");
79  }
80 
82  void output_state_(const state_t s)
83  {
84  std::vector<transition_t> ts;
85  for (auto t : aut_->out(s))
86  ts.push_back(t);
87  std::sort
88  (begin(ts), end(ts),
89  [this](transition_t l, transition_t r)
90  {
91  return
92  (std::forward_as_tuple(aut_->label_of(l), aut_->dst_of(l))
93  < std::forward_as_tuple(aut_->label_of(r), aut_->dst_of(r)));
94  });
95  for (auto t : ts)
96  {
97  os_ << '\n';
99  }
100  }
101 
105  {
106  for (auto s: aut_->states())
107  output_state_(s);
108  }
109 
111  void list_states_(const states_t& ss)
112  {
113  for (auto s: ss)
114  {
115  os_ << ' ';
116  aut_->print_state(s, os_);
117  }
118  }
119 
122  {
123  states_t res;
124  for (auto t: aut_->initial_transitions())
125  res.emplace_back(aut_->dst_of(t));
126  std::sort(begin(res), end(res));
127  return res;
128  }
129 
132  {
133  states_t res;
134  for (auto t: aut_->final_transitions())
135  res.emplace_back(aut_->src_of(t));
136  std::sort(begin(res), end(res));
137  return res;
138  }
139 
143  std::ostream& os_;
145  const labelset_t_of<automaton_t>& ls_ = *aut_->labelset();
147  const weightset_t& ws_ = *aut_->weightset();
150  };
151 
152  }
153 
154 
155  /*---------.
156  | fadoer. |
157  `---------*/
158 
159  namespace detail
160  {
161 
165  template <typename Aut>
166  class fadoer: public outputter<Aut>
167  {
169  "fado: requires letter or nullable labels");
170  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
171  "fado: requires Boolean weights");
172 
174 
175  using super_t::aut_;
176  using super_t::finals_;
177  using super_t::initials_;
178  using super_t::list_states_;
179  using super_t::os_;
181  using super_t::ws_;
182 
183  using super_t::super_t;
184 
185  public:
187  // http://www.dcc.fc.up.pt/~rvr/FAdoDoc/_modules/fa.html#saveToFile
188  void operator()()
189  {
190  bool is_deter = is_deterministic_(aut_);
191  os_ << (is_deter ? "@DFA" : "@NFA");
193  if (!is_deter)
194  {
195  os_ << " *";
197  }
199  }
200 
201  private:
202  template <typename A>
203  typename std::enable_if<labelset_t_of<A>::is_free(),
204  bool>::type
205  is_deterministic_(const A& a)
206  {
207  return vcsn::is_deterministic(a);
208  }
209 
210  template <typename A>
211  typename std::enable_if<!labelset_t_of<A>::is_free(),
212  bool>::type
214  {
215  return false;
216  }
217  };
218 
219  }
220 
221  template <typename Aut>
222  std::ostream&
223  fado(const Aut& aut, std::ostream& out)
224  {
225  detail::fadoer<Aut> fado{aut, out};
226  fado();
227  return out;
228  }
229 
230 
231  namespace dyn
232  {
233  namespace detail
234  {
235  /*------------.
236  | dyn::fado. |
237  `------------*/
238 
240  template <typename Aut, typename Ostream>
241  std::ostream& fado(const automaton& aut, std::ostream& out)
242  {
243  return fado(aut->as<Aut>(), out);
244  }
245 
247  (const automaton& aut, std::ostream& out)
248  -> std::ostream&);
249 
250  }
251  }
252 
253 
254  /*----------.
255  | grailer. |
256  `----------*/
257 
258  namespace detail
259  {
265  template <typename Aut>
266  class grailer: public outputter<Aut>
267  {
269  "grail: requires letter or nullable labels");
270  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
271  "grail:requires Boolean weights");
272 
274 
275  using typename super_t::automaton_t;
276  using typename super_t::state_t;
277  using typename super_t::transition_t;
278 
279  using super_t::aut_;
280  using super_t::finals_;
281  using super_t::initials_;
282  using super_t::os_;
284  using super_t::ws_;
285 
286  using super_t::super_t;
287 
288  public:
290  void operator()()
291  {
292  // Don't end with a \n.
293  const char* sep = "";
294  for (auto s: initials_())
295  {
296  os_ << sep
297  << "(START) |- ";
298  aut_->print_state(s, os_);
299  sep = "\n";
300  }
302  for (auto s: finals_())
303  {
304  os_ << '\n';
305  aut_->print_state(s, os_) << " -| (FINAL)";
306  }
307  }
308  };
309  }
310 
311  template <typename Aut>
312  std::ostream&
313  grail(const Aut& aut, std::ostream& out)
314  {
315  detail::grailer<Aut> grail{aut, out};
316  grail();
317  return out;
318  }
319 
320  namespace dyn
321  {
322  namespace detail
323  {
325  template <typename Aut, typename Ostream>
326  std::ostream& grail(const automaton& aut, std::ostream& out)
327  {
328  return grail(aut->as<Aut>(), out);
329  }
330 
332  (const automaton& aut, std::ostream& out)
333  -> std::ostream&);
334  }
335  }
336 }
337 
338 #endif // !VCSN_ALGOS_GRAIL_HH
Linear combination of labels: map labels to weights.
Definition: fwd.hh:32
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
std::ostream & print_entry_(state_t src, state_t dst, std::ostream &os, const std::string &fmt="text")
The labels and weights of transitions from src to dst.
Definition: grail.hh:73
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:147
virtual void output_transition_(transition_t t)
Output the transition t.
Definition: grail.hh:61
outputter(const automaton_t &aut, std::ostream &out)
Definition: grail.hh:33
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:34
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: grail.hh:145
states_t initials_()
The list of initial states, sorted.
Definition: grail.hh:121
void output_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: grail.hh:82
weight_t_of< automaton_t > weight_t
Definition: grail.hh:47
std::enable_if<!labelset_t_of< A >::is_free(), bool >::type is_deterministic_(const A &)
Definition: grail.hh:213
std::ostream & os_
Output stream.
Definition: grail.hh:143
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:32
transition_t_of< automaton_t > transition_t
Definition: grail.hh:45
auto sort(const Aut &a) -> permutation_automaton< Aut >
Definition: sort.hh:185
std::ostream & grail(const automaton &aut, std::ostream &out)
Bridge.
Definition: grail.hh:326
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:188
Factor common bits in automaton formatting.
Definition: grail.hh:27
std::ostream & grail(const Aut &aut, std::ostream &out)
Definition: grail.hh:313
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:33
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:38
Format an automaton into Fado.
Definition: grail.hh:166
std::ostream & fado(const automaton &aut, std::ostream &out)
Bridge.
Definition: grail.hh:241
std::ostream & print(const monomial_t &m, std::ostream &out, symbol format=symbol{"text"}) const
Print a monomial.
typename detail::transition_t_of_impl< base_t< ValueSet >>::type transition_t_of
Definition: traits.hh:36
context_t_of< automaton_t > context_t
Definition: grail.hh:43
weightset_t_of< automaton_t > weightset_t
Definition: grail.hh:46
const polynomialset< context_t_of< automaton_t > > ps_
Short-hand to the polynomialset used to print the entries.
Definition: grail.hh:149
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:36
polynomialset< context_t_of< Aut > >::value_t get_entry(const Aut &aut, state_t_of< Aut > s, state_t_of< Aut > d)
The entry between two states of an automaton.
label_t_of< automaton_t > label_t
Definition: grail.hh:44
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
Definition: translate.cc:331
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:37
states_t finals_()
The list of final states, sorted.
Definition: grail.hh:131
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: grail.hh:111
Format an automaton into Fado.
Definition: grail.hh:266
void output_transitions_()
Output transitions, sorted lexicographically.
Definition: grail.hh:104
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:141
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
Definition: traits.hh:35
state_t_of< automaton_t > state_t
Definition: grail.hh:40
std::ostream & fado(const Aut &aut, std::ostream &out)
Definition: grail.hh:223
std::enable_if< labelset_t_of< A >::is_free(), bool >::type is_deterministic_(const A &a)
Definition: grail.hh:205
std::vector< state_t > states_t
A list of states.
Definition: grail.hh:50
virtual std::string label_(const label_t &l)
Convert a label to its representation.
Definition: grail.hh:54
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:290
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7