Vcsn  2.4
Be Rational
grail.hh
Go to the documentation of this file.
1 #pragma once
2 
5 #include <vcsn/dyn/fwd.hh>
6 #include <vcsn/weightset/fwd.hh> // b
7 
8 namespace vcsn
9 {
10  /*--------.
11  | fado. |
12  `--------*/
13 
14  namespace detail
15  {
16 
20  template <Automaton Aut>
21  class fado_impl: public printer<Aut>
22  {
25  "fado: requires tuple, letter or nullable labels");
26  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
27  "fado: requires Boolean weights");
28 
30 
31  using super_t::aut_;
32  using super_t::finals_;
33  using super_t::initials_;
36  using super_t::ls_;
37  using super_t::os_;
39 
40  using super_t::super_t;
41  using typename super_t::transition_t;
42 
43  public:
45  // http://www.dcc.fc.up.pt/~rvr/FAdoDoc/_modules/fio.html#saveToFile
46  void operator()()
47  {
48  bool is_deter = is_deterministic_(aut_);
49  os_ << (is_transducer_ ? "@Transducer" : is_deter ? "@DFA" : "@NFA");
51  if (!is_deter)
52  {
53  os_ << " *";
55  }
57  }
58 
59  private:
60  template <typename LS>
61  void print_label_(const LS& ls, const typename LS::value_t& l) const
62  {
63  if (ls.is_one(l))
64  os_ << "@epsilon";
65  else
66  ls.print(l, os_, format::raw);
67  }
68 
70  template <typename Label>
71  void print_label_(const Label& l, std::false_type) const
72  {
73  print_label_(ls_, l);
74  }
75 
77  template <typename Label>
78  void print_label_(const Label& l, std::true_type) const
79  {
80  print_label_(ls_.template set<0>(), std::get<0>(l));
81  os_ << ' ';
82  print_label_(ls_.template set<1>(), std::get<1>(l));
83  }
84 
85  void print_transition_(const transition_t t) const override
86  {
87  aut_->print_state(aut_->src_of(t), os_);
88  os_ << ' ';
89  print_label_(aut_->label_of(t), is_transducer_);
90  os_ << ' ';
91  aut_->print_state(aut_->dst_of(t), os_);
92  }
93 
94  template <Automaton A>
95  std::enable_if_t<labelset_t_of<A>::is_free(), bool>
96  is_deterministic_(const A& a)
97  {
98  return vcsn::is_deterministic(a);
99  }
100 
101  template <Automaton A>
102  std::enable_if_t<!labelset_t_of<A>::is_free(), bool>
104  {
105  return false;
106  }
107  };
108  }
109 
113  template <Automaton Aut>
114  std::ostream&
115  fado(const Aut& aut, std::ostream& out = std::cout)
116  {
117  auto fado = detail::fado_impl<Aut>{aut, out};
118  fado();
119  return out;
120  }
121 
122 
123 
124  /*----------.
125  | grailer. |
126  `----------*/
127 
128  namespace detail
129  {
135  template <Automaton Aut>
136  class grail_impl: public printer<Aut>
137  {
139  "grail: requires letter or nullable labels");
140  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
141  "grail: requires Boolean weights");
142 
144 
145  using typename super_t::automaton_t;
146  using typename super_t::state_t;
147  using typename super_t::transition_t;
148 
149  using super_t::aut_;
150  using super_t::finals_;
151  using super_t::initials_;
152  using super_t::os_;
154 
155  using super_t::super_t;
156 
157  public:
159  void operator()()
160  {
161  // Don't end with a \n.
162  const char* sep = "";
163  for (auto s: initials_())
164  {
165  os_ << sep
166  << "(START) |- ";
167  aut_->print_state(s, os_);
168  sep = "\n";
169  }
171  for (auto s: finals_())
172  {
173  os_ << '\n';
174  aut_->print_state(s, os_) << " -| (FINAL)";
175  }
176  }
177  };
178  }
179 
183  template <Automaton Aut>
184  std::ostream&
185  grail(const Aut& aut, std::ostream& out)
186  {
187  auto grail = detail::grail_impl<Aut>{aut, out};
188  grail();
189  return out;
190  }
191 }
state_t_of< automaton_t > state_t
Definition: printer.hh:50
std::ostream & grail(const Aut &aut, std::ostream &out)
Print automaton in Grail format.
Definition: grail.hh:185
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:46
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
void print_label_(const Label &l, std::true_type) const
Two-tape automaton.
Definition: grail.hh:78
transition_t_of< automaton_t > transition_t
Definition: printer.hh:55
std::ostream & os_
Output stream.
Definition: printer.hh:135
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
const is_transducer_t is_transducer_
Definition: printer.hh:147
std::enable_if_t<!labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &)
Definition: grail.hh:103
void print_transitions_()
Output transitions, sorted lexicographically.
Definition: printer.hh:96
Definition: a-star.hh:8
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:85
Factor common bits in automaton formatting.
Definition: printer.hh:37
std::enable_if_t< labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &a)
Definition: grail.hh:96
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: printer.hh:103
Print an automaton in Fado format.
Definition: grail.hh:136
void print_label_(const LS &ls, const typename LS::value_t &l) const
Definition: grail.hh:61
std::ostream & fado(const Aut &aut, std::ostream &out=std::cout)
Format automaton to FAdo format.
Definition: grail.hh:115
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:133
void print_label_(const Label &l, std::false_type) const
Acceptor.
Definition: grail.hh:71
states_t initials_()
The list of initial states, sorted.
Definition: printer.hh:113
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
void print_transition_(const transition_t t) const override
Output the transition t.
Definition: grail.hh:85
states_t finals_()
The list of final states, sorted.
Definition: printer.hh:123
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
Format an automaton into FAdo.
Definition: grail.hh:21
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:159
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: printer.hh:137