Awali
Another Weighted Automata library
grail.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2021 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_GRAIL_HH
18 # define AWALI_ALGOS_GRAIL_HH
19 
20 # include <iostream>
21 # include <map>
22 
25 #include <awali/sttc/weightset/fwd.hh> // b
27 
28 namespace awali { namespace sttc {
29 
30 
31  namespace internal
32  {
33 
34  /*------------.
35  | outputter. |
36  `------------*/
37 
41  template <typename Aut>
42  class outputter
43  {
44  protected:
45  using automaton_t = Aut;
46 
47  public:
48  outputter(const automaton_t& aut, std::ostream& out)
49  : aut_(aut)
50  , os_(out)
51  {}
52 
53  // Should not be public, but needed by GCC 4.8.1.
54  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58972
55 
56  protected:
61 
63  using states_t = std::vector<state_t>;
64 
66  virtual std::string
67  label_(const label_t& l)
68  {
69  return ls_.is_one(l) ? "@epsilon" : format(ls_, l);
70  }
71 
75  {
76  aut_->print_state(aut_->src_of(t), os_);
77  os_ << ' ' << label_(aut_->label_of(t)) << ' ';
78  aut_->print_state(aut_->dst_of(t), os_);
79  }
80 
86  std::string format_entry_(state_t src, state_t dst,
87  const std::string& fmt = "text")
88  {
89  auto entry = get_entry(aut_, src, dst);
90  return ps_.format(entry, ", ", fmt);
91  }
92 
94  void output_state_(const state_t s)
95  {
96  std::vector<transition_t> ts;
97  for (auto t : aut_->out(s))
98  ts.push_back(t);
99  std::sort
100  (begin(ts), end(ts),
101  [this](transition_t l, transition_t r)
102  {
103  return (std::forward_as_tuple(aut_->label_of(l), aut_->dst_of(l))
104  < std::forward_as_tuple(aut_->label_of(r), aut_->dst_of(r)));
105  });
106  for (auto t : ts)
107  {
108  os_ << '\n';
110  }
111  }
112 
116  {
117  for (auto s: aut_->states())
118  output_state_(s);
119  }
120 
122  void list_states_(const states_t& ss)
123  {
124  for (auto s: ss)
125  {
126  os_ << ' ';
127  aut_->print_state(s, os_);
128  }
129  }
130 
133  {
134  states_t res;
135  for (auto t: aut_->initial_transitions())
136  res.emplace_back(aut_->dst_of(t));
137  std::sort(begin(res), end(res));
138  return res;
139  }
140 
143  {
144  states_t res;
145  for (auto t: aut_->final_transitions())
146  res.emplace_back(aut_->src_of(t));
147  std::sort(begin(res), end(res));
148  return res;
149  }
150 
154  std::ostream& os_;
156  const labelset_t_of<automaton_t>& ls_ = *aut_->labelset();
158  const weightset_t& ws_ = *aut_->weightset();
161  };
162 
163  }
164 
165 
166  /*---------.
167  | fadoer. |
168  `---------*/
169 
170  namespace internal
171  {
172 
176  template <typename Aut>
177  class fadoer: public outputter<Aut>
178  {
180  "requires labels_are_(letters|nullable)");
181  // FIXME: Not right: F2 is also using bool, but it is not bool.
182  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
183  "requires Boolean weights");
184 
185  using super_type = outputter<Aut>;
186 
187  using super_type::aut_;
188  using super_type::finals_;
189  using super_type::initials_;
191  using super_type::os_;
193  using super_type::ws_;
194 
195  using super_type::super_type;
196 
197  public:
199  // http://www.dcc.fc.up.pt/~rvr/FAdoDoc/_modules/fa.html#saveToFile
200  void operator()()
201  {
202  bool is_deter = is_deterministic_(aut_);
203  os_ << (is_deter ? "@DFA" : "@NFA");
204  list_states_(finals_());
205  if (!is_deter)
206  {
207  os_ << " *";
208  list_states_(initials_());
209  }
210  output_transitions_();
211  }
212 
213  private:
214  template <typename A>
215  typename std::enable_if<labelset_t_of<A>::is_free(),
216  bool>::type
217  is_deterministic_(const A& a)
218  {
219  return is_deterministic(a);
220  }
221 
222  template <typename A>
223  typename std::enable_if<!labelset_t_of<A>::is_free(),
224  bool>::type
225  is_deterministic_(const A&)
226  {
227  return false;
228  }
229  };
230 
231  }
232 
233  template <typename Aut>
234  std::ostream&
235  fado(const Aut& aut, std::ostream& out)
236  {
237  internal::fadoer<Aut> fado{aut, out};
238  fado();
239  return out;
240  }
241 
242  /*----------.
243  | grailer. |
244  `----------*/
245 
246  namespace internal
247  {
253  template <typename Aut>
254  class grailer: public outputter<Aut>
255  {
257  "requires labels_are_(letters|nullable)");
258  // FIXME: Not right: F2 is also using bool, but it is not bool.
259  static_assert(std::is_same<weightset_t_of<Aut>, b>::value,
260  "requires Boolean weights");
261 
262  using super_type = outputter<Aut>;
263 
264  using typename super_type::automaton_t;
265 
266  using super_type::aut_;
267  using super_type::finals_;
268  using super_type::initials_;
269  using super_type::os_;
271  using super_type::ws_;
272 
273  using super_type::super_type;
274 
275  public:
277  void operator()()
278  {
279  // Don't end with a \n.
280  const char* sep = "";
281  for (auto s: initials_())
282  {
283  os_ << sep
284  << "(START) |- ";
285  aut_->print_state(s, os_);
286  sep = "\n";
287  }
288  output_transitions_();
289  for (auto s: finals_())
290  {
291  os_ << '\n';
292  aut_->print_state(s, os_) << " -| (FINAL)";
293  }
294  }
295  };
296  }
297 
298  template <typename Aut>
299  std::ostream&
300  grail(const Aut& aut, std::ostream& out)
301  {
302  internal::grailer<Aut> grail{aut, out};
303  grail();
304  return out;
305  }
306 
307 }}//end of ns awali::stc
308 
309 #endif // !AWALI_ALGOS_GRAIL_HH
The Boolean semring.
Definition: b.hh:38
Format an automaton into Fado.
Definition: grail.hh:178
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:200
Format an automaton into Fado.
Definition: grail.hh:255
void operator()()
Actually output aut_ on os_.
Definition: grail.hh:277
Factor common bits in automaton formatting.
Definition: grail.hh:43
void output_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: grail.hh:94
void output_transitions_()
Output transitions, sorted lexicographically.
Definition: grail.hh:115
outputter(const automaton_t &aut, std::ostream &out)
Definition: grail.hh:48
std::string format_entry_(state_t src, state_t dst, const std::string &fmt="text")
The labels and weights of transitions from src to dst.
Definition: grail.hh:86
virtual std::string label_(const label_t &l)
Convert a label to its representation.
Definition: grail.hh:67
context_t_of< automaton_t > context_t
Definition: grail.hh:57
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:152
void list_states_(const states_t &ss)
List names of states in ss, preceded by ' '.
Definition: grail.hh:122
states_t finals_()
The list of final states, sorted.
Definition: grail.hh:142
weight_t_of< automaton_t > weight_t
Definition: grail.hh:60
virtual void output_transition_(transition_t t)
Output the transition t.
Definition: grail.hh:74
Aut automaton_t
Definition: grail.hh:45
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: grail.hh:156
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:158
std::ostream & os_
Output stream.
Definition: grail.hh:154
std::vector< state_t > states_t
A list of states.
Definition: grail.hh:63
const polynomialset< context_t_of< automaton_t > > ps_
Short-hand to the polynomialset used to print the entries.
Definition: grail.hh:160
label_t_of< automaton_t > label_t
Definition: grail.hh:58
states_t initials_()
The list of initial states, sorted.
Definition: grail.hh:132
weightset_t_of< automaton_t > weightset_t
Definition: grail.hh:59
Linear combination of labels: map labels to weights.
Definition: polynomialset.hh:69
std::string format(const value_t &v, const std::string &sep=" + ", const std::string &fmt="text") const
Definition: polynomialset.hh:620
The semiring of floating Numbers.
Definition: r.hh:34
static constexpr TOP< void > value
Definition: priority.hh:93
bool is_deterministic(const Aut &aut, state_t s)
Whether state s is deterministic in aut.
Definition: is_deterministic.hxx:48
typename internal::label_t_of_impl< internal::base_t< ValueSet > >::type label_t_of
Helper to retrieve the type of the labels of a value set.
Definition: traits.hh:71
void sort(Aut a, Compare p)
Definition: sort.hh:28
typename internal::weight_t_of_impl< internal::base_t< ValueSet > >::type weight_t_of
Helper to retrieve the type of the weights of a value set.
Definition: traits.hh:81
polynomialset< context_t_of< Aut > >::value_t get_entry(const Aut &aut, state_t s, state_t d)
The entry between two states of an automaton.
Definition: polynomialset.hh:787
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
std::ostream & grail(const Aut &aut, std::ostream &out)
Definition: grail.hh:300
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
std::ostream & fado(const Aut &aut, std::ostream &out)
Definition: grail.hh:235
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22