Vcsn  2.3
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  {
24  "fado: requires letter or nullable labels");
25  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
26  "fado: requires Boolean weights");
27 
29 
30  using super_t::aut_;
31  using super_t::finals_;
32  using super_t::initials_;
34  using super_t::os_;
36 
37  using super_t::super_t;
38 
39  public:
41  // http://www.dcc.fc.up.pt/~rvr/FAdoDoc/_modules/fa.html#saveToFile
42  void operator()()
43  {
44  bool is_deter = is_deterministic_(aut_);
45  os_ << (is_deter ? "@DFA" : "@NFA");
47  if (!is_deter)
48  {
49  os_ << " *";
51  }
53  }
54 
55  private:
56  template <Automaton A>
57  std::enable_if_t<labelset_t_of<A>::is_free(), bool>
58  is_deterministic_(const A& a)
59  {
60  return vcsn::is_deterministic(a);
61  }
62 
63  template <Automaton A>
64  std::enable_if_t<!labelset_t_of<A>::is_free(), bool>
66  {
67  return false;
68  }
69  };
70  }
71 
75  template <Automaton Aut>
76  std::ostream&
77  fado(const Aut& aut, std::ostream& out = std::cout)
78  {
79  auto fado = detail::fado_impl<Aut>{aut, out};
80  fado();
81  return out;
82  }
83 
84 
85 
86  /*----------.
87  | grailer. |
88  `----------*/
89 
90  namespace detail
91  {
97  template <Automaton Aut>
98  class grail_impl: public printer<Aut>
99  {
101  "grail: requires letter or nullable labels");
102  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
103  "grail: requires Boolean weights");
104 
106 
107  using typename super_t::automaton_t;
108  using typename super_t::state_t;
109  using typename super_t::transition_t;
110 
111  using super_t::aut_;
112  using super_t::finals_;
113  using super_t::initials_;
114  using super_t::os_;
116 
117  using super_t::super_t;
118 
119  public:
121  void operator()()
122  {
123  // Don't end with a \n.
124  const char* sep = "";
125  for (auto s: initials_())
126  {
127  os_ << sep
128  << "(START) |- ";
129  aut_->print_state(s, os_);
130  sep = "\n";
131  }
133  for (auto s: finals_())
134  {
135  os_ << '\n';
136  aut_->print_state(s, os_) << " -| (FINAL)";
137  }
138  }
139  };
140  }
141 
145  template <Automaton Aut>
146  std::ostream&
147  grail(const Aut& aut, std::ostream& out)
148  {
149  auto grail = detail::grail_impl<Aut>{aut, out};
150  grail();
151  return out;
152  }
153 }
Factor common bits in automaton formatting.
Definition: printer.hh:24
void print_transitions_()
Output transitions, sorted lexicographically.
Definition: printer.hh:83
Format an automaton into Fado.
Definition: grail.hh:21
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
states_t initials_()
The list of initial states, sorted.
Definition: printer.hh:100
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:42
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:64
std::enable_if_t< labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &a)
Definition: grail.hh:58
states_t finals_()
The list of final states, sorted.
Definition: printer.hh:110
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: printer.hh:90
bool is_deterministic(const Aut &aut, state_t_of< Aut > s)
Whether state s is deterministic in aut.
Print an automaton in Fado format.
Definition: grail.hh:98
Definition: a-star.hh:8
std::ostream & grail(const Aut &aut, std::ostream &out)
Print automaton in Grail format.
Definition: grail.hh:147
transition_t_of< automaton_t > transition_t
Definition: printer.hh:42
state_t_of< automaton_t > state_t
Definition: printer.hh:37
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:121
std::ostream & os_
Output stream.
Definition: printer.hh:122
std::ostream & fado(const Aut &aut, std::ostream &out=std::cout)
Format automaton to FAdo format.
Definition: grail.hh:77
std::enable_if_t<!labelset_t_of< A >::is_free(), bool > is_deterministic_(const A &)
Definition: grail.hh:65
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:120
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46