Vcsn  2.2
Be Rational
print.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iosfwd>
4 
5 #include <vcsn/algos/dot.hh>
6 #include <vcsn/algos/efsm.hh>
7 #include <vcsn/algos/grail.hh>
8 #include <vcsn/algos/info.hh>
9 #include <vcsn/algos/tikz.hh>
10 #include <vcsn/ctx/context.hh>
11 #include <vcsn/dyn/context.hh>
12 #include <vcsn/dyn/expansion.hh>
13 #include <vcsn/dyn/label.hh>
14 #include <vcsn/dyn/fwd.hh>
15 #include <vcsn/dyn/polynomial.hh>
16 #include <vcsn/dyn/expression.hh>
17 #include <vcsn/dyn/weight.hh>
18 #include <vcsn/misc/attributes.hh>
19 #include <vcsn/misc/format.hh>
20 #include <vcsn/misc/raise.hh>
21 
22 namespace vcsn
23 {
24  /*----------------------------.
25  | print(automaton, stream). |
26  `----------------------------*/
27 
28  namespace detail
29  {
30  // FIXME: C++14 and templated variables.
31  template <Automaton Aut>
32  using has_fado_t
33  = std::integral_constant<bool,
34  ((context_t_of<Aut>::is_lal
35  || context_t_of<Aut>::is_lan)
36  && std::is_same<weightset_t_of<Aut>, b>::value)>;
37 
38  template <Automaton Aut>
39  auto
40  fado_impl_(const Aut& aut, std::ostream& out)
41  -> std::enable_if_t<has_fado_t<Aut>{}, void>
42  {
43  fado(aut, out);
44  }
45 
46  template <Automaton Aut>
47  ATTRIBUTE_NORETURN
48  auto
49  fado_impl_(const Aut&, std::ostream&)
50  -> std::enable_if_t<!has_fado_t<Aut>{}, void>
51  {
52  raise("print: FAdo requires letter or nullable labels,"
53  " and Boolean weights");
54  }
55 
56  template <Automaton Aut>
57  auto
58  grail_impl_(const Aut& aut, std::ostream& out)
59  -> std::enable_if_t<has_fado_t<Aut>{}, void>
60  {
61  grail(aut, out);
62  }
63 
64  template <Automaton Aut>
65  ATTRIBUTE_NORETURN
66  auto
67  grail_impl_(const Aut&, std::ostream&)
68  -> std::enable_if_t<!has_fado_t<Aut>{}, void>
69  {
70  raise("print: Grail requires letter or nullable labels,"
71  " and Boolean weights");
72  }
73  }
74 
75  template <Automaton Aut>
76  std::ostream&
77  print(const Aut& aut, std::ostream& out, const std::string& fmt)
78  {
79  static const auto map
81  {
82  "automaton output format",
83  {
84  {"dot", [](const Aut& a, std::ostream& o){ dot(a, o); }},
85  {"default", "dot"},
86  {"dot,latex", [](const Aut& a, std::ostream& o)
87  { dot(a, o, format("latex")); }},
88  {"dot,utf8", [](const Aut& a, std::ostream& o)
89  { dot(a, o, format("utf8")); }},
90  {"efsm", [](const Aut& a, std::ostream& o){ efsm(a, o); }},
91  {"fado", detail::fado_impl_<Aut>},
92  {"grail", detail::grail_impl_<Aut>},
93  {"info", [](const Aut& a, std::ostream& o){ info(a, o); }},
94  {"info,detailed",[](const Aut& a, std::ostream& o){ info(a, o, true); }},
95  {"null", [](const Aut&, std::ostream&){}},
96  {"tikz", [](const Aut& a, std::ostream& o){ tikz(a, o); }},
97  }
98  };
99  map[fmt](aut, out);
100  return out;
101  }
102 
103  namespace dyn
104  {
105  namespace detail
106  {
108  template <Automaton Aut, typename Ostream, typename String>
109  std::ostream& print(const automaton& aut, std::ostream& o,
110  const std::string& fmt)
111  {
112  const auto& a = aut->as<Aut>();
113  return print(a, o, fmt);
114  }
115  }
116  }
117 
118  /*-------------------------.
119  | print(context, stream). |
120  `-------------------------*/
121 
122  namespace dyn
123  {
124  namespace detail
125  {
127  template <typename Context, typename Ostream, typename String>
128  std::ostream& print_context(const context& ctx, std::ostream& o,
129  const std::string& fmt)
130  {
131  const auto& c = ctx->as<Context>();
132  return c.print_set(o, format(fmt));
133  }
134  }
135  }
136 
137  /*---------------------------.
138  | print(expansion, stream). |
139  `---------------------------*/
140 
143  template <typename ValueSet>
144  inline
145  std::ostream&
146  print(const ValueSet& vs, const typename ValueSet::value_t& v,
147  std::ostream& o, format fmt)
148  {
149  return vs.print(v, o, format(fmt));
150  }
151 
152  namespace dyn
153  {
154  namespace detail
155  {
157  template <typename ExpansionSet, typename Ostream, typename String>
158  std::ostream& print_expansion(const expansion& expansion, std::ostream& o,
159  const std::string& fmt)
160  {
161  const auto& e = expansion->as<ExpansionSet>();
162  return vcsn::print(e.expansionset(), e.expansion(), o, format(fmt));
163  }
164  }
165  }
166 
167  /*-----------------------.
168  | print(label, stream). |
169  `-----------------------*/
170 
171  namespace dyn
172  {
173  namespace detail
174  {
176  template <typename LabelSet, typename Ostream, typename String>
177  std::ostream& print_label(const label& label, std::ostream& o,
178  const std::string& fmt)
179  {
180  const auto& l = label->as<LabelSet>();
181  return vcsn::print(l.labelset(), l.label(), o, format(fmt));
182  }
183  }
184  }
185 
186  /*---------------------------.
187  | list(polynomial, stream). |
188  `---------------------------*/
189 
190  template <typename PolynomialSet>
191  inline
192  std::ostream&
193  list(const PolynomialSet& ps, const typename PolynomialSet::value_t& p,
194  std::ostream& o)
195  {
196  bool first = true;
197  for (const auto& m: p)
198  {
199  if (!first)
200  o << std::endl;
201  first = false;
202  ps.print(m, o);
203  }
204  return o;
205  }
206 
207  namespace dyn
208  {
209  namespace detail
210  {
212  template <typename PolynomialSet, typename Ostream>
213  std::ostream& list_polynomial(const polynomial& polynomial,
214  std::ostream& o)
215  {
216  const auto& p = polynomial->as<PolynomialSet>();
217  return vcsn::list(p.polynomialset(), p.polynomial(), o);
218  }
219  }
220  }
221 
222  /*----------------------------.
223  | print(polynomial, stream). |
224  `----------------------------*/
225 
226  namespace dyn
227  {
228  namespace detail
229  {
231  template <typename PolynomialSet, typename Ostream, typename String>
232  std::ostream& print_polynomial(const polynomial& polynomial,
233  std::ostream& o, const std::string& fmt)
234  {
235  const auto& p = polynomial->as<PolynomialSet>();
236  return vcsn::print(p.polynomialset(), p.polynomial(), o, format(fmt));
237  }
238  }
239  }
240 
241 
242  /*-----------------------------.
243  | print(expression, stream). |
244  `-----------------------------*/
245 
246 #if 0
247  template <typename ExpSet>
249  inline
250  std::ostream&
251  print(const ExpSet& rs, const typename ExpSet::value_t& e,
252  std::ostream& o, format fmt)
253  {
254  return rs.print(e, o, format(fmt));
255  }
256 #endif
257 
258  namespace dyn
259  {
260  namespace detail
261  {
263  template <typename ExpSet, typename Ostream, typename String>
264  std::ostream& print_expression(const expression& exp, std::ostream& o,
265  const std::string& fmt)
266  {
267  const auto& e = exp->as<ExpSet>();
268  return vcsn::print(e.expressionset(), e.expression(), o, format(fmt));
269  }
270  }
271  }
272 
273  /*------------------------.
274  | print(weight, stream). |
275  `------------------------*/
276 
277 #if 0
278  template <typename WeightSet>
280  inline
281  std::ostream&
282  print(const WeightSet& ws, const typename WeightSet::value_t& w,
283  std::ostream& o)
284  {
285  return ws.print(w, o);
286  }
287 #endif
288 
289  namespace dyn
290  {
291  namespace detail
292  {
294  template <typename WeightSet, typename Ostream, typename String>
295  std::ostream& print_weight(const weight& weight, std::ostream& o,
296  const std::string& fmt)
297  {
298  const auto& w = weight->as<WeightSet>();
299  return vcsn::print(w.weightset(), w.weight(), o, format(fmt));
300  }
301  }
302  }
303 }
std::ostream & print(const automaton &aut, std::ostream &o, const std::string &fmt)
Bridge.
Definition: print.hh:109
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Print automaton to TikZ format.
Definition: tikz.hh:157
std::ostream & grail(const Aut &aut, std::ostream &out)
Definition: grail.hh:272
std::ostream & print_expression(const expression &exp, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:264
std::shared_ptr< const detail::polynomial_base > polynomial
Definition: fwd.hh:70
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:88
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:59
std::integral_constant< bool,((context_t_of< Aut >::is_lal||context_t_of< Aut >::is_lan)&&std::is_same< weightset_t_of< Aut >, b >::value)> has_fado_t
Definition: print.hh:36
Definition: a-star.hh:8
std::shared_ptr< const detail::expansion_base > expansion
Definition: expansion.hh:73
std::ostream & fado(const Aut &aut, std::ostream &out)
Definition: grail.hh:204
std::ostream & print_polynomial(const polynomial &polynomial, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:232
An input/output format for valuesets.
Definition: format.hh:11
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::shared_ptr< const detail::label_base > label
Definition: fwd.hh:61
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
A mapping from strings to Values.
Definition: getargs.hh:33
auto grail_impl_(const Aut &aut, std::ostream &out) -> std::enable_if_t< has_fado_t< Aut >
Definition: print.hh:58
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:43
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
auto rs
Definition: lift.hh:151
auto fado_impl_(const Aut &aut, std::ostream &out) -> std::enable_if_t< has_fado_t< Aut >
Definition: print.hh:40
std::ostream & print(const Aut &aut, std::ostream &out, const std::string &fmt)
Definition: print.hh:77
std::ostream & efsm(const Aut &aut, std::ostream &out)
Format automaton to EFSM format, based on FSM format.
Definition: efsm.hh:346
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
std::ostream & print_context(const context &ctx, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:128
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:92
std::ostream & print_label(const label &label, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:177
std::ostream & print_expansion(const expansion &expansion, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:158
std::ostream & info(const Aut &aut, std::ostream &out, bool detailed=false)
Definition: info.hh:72
std::ostream & print_weight(const weight &weight, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:295
std::ostream & dot(const Aut &aut, std::ostream &out, format fmt={})
Print an automaton in Graphviz's Dot format.
Definition: dot.hh:377
std::ostream & list_polynomial(const polynomial &polynomial, std::ostream &o)
Bridge (list).
Definition: print.hh:213
std::ostream & list(const PolynomialSet &ps, const typename PolynomialSet::value_t &p, std::ostream &o)
Definition: print.hh:193