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