Vcsn  2.4
Be Rational
print.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iosfwd>
4 
5 #include <vcsn/algos/daut.hh>
6 #include <vcsn/algos/dot.hh>
7 #include <vcsn/algos/efsm.hh>
8 #include <vcsn/algos/grail.hh>
9 #include <vcsn/algos/info.hh>
10 #include <vcsn/algos/tikz.hh>
11 #include <vcsn/core/rat/dot.hh>
12 #include <vcsn/ctx/context.hh>
13 #include <vcsn/dyn/context.hh>
14 #include <vcsn/dyn/fwd.hh>
15 #include <vcsn/dyn/value.hh>
16 #include <vcsn/misc/attributes.hh>
17 #include <vcsn/misc/format.hh>
18 #include <vcsn/misc/raise.hh>
19 
20 namespace vcsn
21 {
22  /*----------------------------.
23  | print(automaton, stream). |
24  `----------------------------*/
25 
26  namespace detail
27  {
28  // FIXME: C++14 and templated variables.
29  template <Automaton Aut>
30  using has_fado_t
31  = std::integral_constant<bool,
32  ((context_t_of<Aut>::is_lal
33  || context_t_of<Aut>::is_lan
34  || context_t_of<Aut>::is_lat)
35  && std::is_same<weightset_t_of<Aut>, b>::value)>;
36  template <Automaton Aut>
37  using has_grail_t
38  = std::integral_constant<bool,
41  && std::is_same<weightset_t_of<Aut>, b>::value)>;
42 
43 
44  template <Automaton Aut>
45  auto
46  fado_impl_(const Aut& aut, std::ostream& out)
47  -> std::enable_if_t<has_fado_t<Aut>{}, void>
48  {
49  fado(aut, out);
50  }
51 
52  template <Automaton Aut>
53  ATTRIBUTE_NORETURN
54  auto
55  fado_impl_(const Aut&, std::ostream&)
56  -> std::enable_if_t<!has_fado_t<Aut>{}, void>
57  {
58  raise("print: FAdo requires tuple, letter or nullable labels,"
59  " and Boolean weights");
60  }
61 
62  template <Automaton Aut>
63  auto
64  grail_impl_(const Aut& aut, std::ostream& out)
65  -> std::enable_if_t<has_grail_t<Aut>{}, void>
66  {
67  grail(aut, out);
68  }
69 
70  template <Automaton Aut>
71  ATTRIBUTE_NORETURN
72  auto
73  grail_impl_(const Aut&, std::ostream&)
74  -> std::enable_if_t<!has_grail_t<Aut>{}, void>
75  {
76  raise("print: Grail requires letter or nullable labels,"
77  " and Boolean weights");
78  }
79  }
80 
81  template <Automaton Aut>
82  std::ostream&
83  print(const Aut& aut,
84  std::ostream& out = std::cout, const std::string& fmt = "default")
85  {
86  static const auto map
88  {
89  "automaton output format",
90  {
91  {"debug", [](const Aut& a, std::ostream& o){ a->print(o); }},
92  {"daut", [](const Aut& a, std::ostream& o){ daut(a, o); }},
93  {"dot", [](const Aut& a, std::ostream& o){ dot(a, o); }},
94  {"default", "dot"},
95  {"dot,latex", [](const Aut& a, std::ostream& o)
96  { dot(a, o, format("latex")); }},
97  {"dot,mathjax", [](const Aut& a, std::ostream& o)
98  { dot(a, o, format("latex"), true); }},
99  {"dot,utf8", [](const Aut& a, std::ostream& o)
100  { dot(a, o, format("utf8")); }},
101  {"efsm", [](const Aut& a, std::ostream& o){ efsm(a, o); }},
102  {"fado", detail::fado_impl_<Aut>},
103  {"grail", detail::grail_impl_<Aut>},
104  {"info", [](const Aut& a, std::ostream& o){ info(a, o, 2); }},
105  {"info,detailed",[](const Aut& a, std::ostream& o){ info(a, o, 3); }},
106  {"info,size", [](const Aut& a, std::ostream& o){ info(a, o, 1); }},
107  {"null", [](const Aut&, std::ostream&){}},
108  {"tikz", [](const Aut& a, std::ostream& o){ tikz(a, o); }},
109  }
110  };
111  map[fmt](aut, out);
112  return out;
113  }
114 
115  namespace dyn
116  {
117  namespace detail
118  {
120  template <Automaton Aut, typename Ostream, typename String>
121  std::ostream& print(const automaton& aut, std::ostream& o,
122  const std::string& fmt)
123  {
124  const auto& a = aut->as<Aut>();
125  return print(a, o, fmt);
126  }
127  }
128  }
129 
130  /*-------------------------.
131  | print(context, stream). |
132  `-------------------------*/
133 
134  namespace dyn
135  {
136  namespace detail
137  {
139  template <typename Context, typename Ostream, typename String>
140  std::ostream& print_context(const context& ctx, std::ostream& o,
141  const std::string& fmt)
142  {
143  const auto& c = ctx->as<Context>();
144  return c.print_set(o, format(fmt));
145  }
146  }
147  }
148 
149  /*---------------------------.
150  | print(expansion, stream). |
151  `---------------------------*/
152 
158  template <typename ValueSet>
159  std::ostream&
160  print(const ValueSet& vs, const typename ValueSet::value_t& v,
161  std::ostream& o = std::cout, format fmt = {})
162  {
163  return vs.print(v, o, format(fmt));
164  }
165 
166  namespace dyn
167  {
168  namespace detail
169  {
171  template <typename ExpansionSet, typename Ostream, typename String>
172  std::ostream& print_expansion(const expansion& expansion,
173  std::ostream& o,
174  const std::string& fmt)
175  {
176  const auto& e = expansion->as<ExpansionSet>();
177  return vcsn::print(e.valueset(), e.value(), o, format(fmt));
178  }
179  }
180  }
181 
182  /*-----------------------------.
183  | print(expression, stream). |
184  `-----------------------------*/
185 
187  template <typename Context>
188  std::ostream&
190  const typename expressionset<Context>::value_t& r,
191  std::ostream& o = std::cout,
192  const std::string& fmt = "default")
193  {
194  if (fmt == "dot" || fmt == "dot,logical")
195  {
196  auto print = make_dot_printer(rs, o);
197  return print(r);
198  }
199  else if (fmt == "dot,physical")
200  {
201  auto print = make_dot_printer(rs, o, true);
202  return print(r);
203  }
204  else
205  return print(rs, r, o, format(fmt));
206  }
207 
208  namespace dyn
209  {
210  namespace detail
211  {
213  template <typename ExpSet, typename Ostream, typename String>
214  std::ostream& print_expression(const expression& exp, std::ostream& o,
215  const std::string& fmt)
216  {
217  const auto& e = exp->as<ExpSet>();
218  return vcsn::print(e.valueset(), e.value(), o, fmt);
219  }
220  }
221  }
222 
223  /*-----------------------.
224  | print(label, stream). |
225  `-----------------------*/
226 
227  namespace dyn
228  {
229  namespace detail
230  {
232  template <typename LabelSet, typename Ostream, typename String>
233  std::ostream& print_label(const label& label, std::ostream& o,
234  const std::string& fmt)
235  {
236  const auto& l = label->as<LabelSet>();
237  return vcsn::print(l.valueset(), l.value(),
238  o, format(fmt).for_labels());
239  }
240  }
241  }
242 
243  /*---------------------------.
244  | list(polynomial, stream). |
245  `---------------------------*/
246 
247  template <typename PolynomialSet>
248  std::ostream&
249  list(const PolynomialSet& ps, const typename PolynomialSet::value_t& p,
250  std::ostream& o = std::cout)
251  {
252  bool first = true;
253  for (const auto& m: p)
254  {
255  if (!first)
256  o << std::endl;
257  first = false;
258  ps.print(m, o);
259  }
260  return o;
261  }
262 
263  namespace dyn
264  {
265  namespace detail
266  {
268  template <typename PolynomialSet, typename Ostream>
269  std::ostream& list_polynomial(const polynomial& polynomial,
270  std::ostream& o)
271  {
272  const auto& p = polynomial->as<PolynomialSet>();
273  return vcsn::list(p.valueset(), p.value(), o);
274  }
275  }
276  }
277 
278  /*----------------------------.
279  | print(polynomial, stream). |
280  `----------------------------*/
281 
282  namespace dyn
283  {
284  namespace detail
285  {
287  template <typename PolynomialSet, typename Ostream, typename String>
288  std::ostream& print_polynomial(const polynomial& polynomial,
289  std::ostream& o, const std::string& fmt)
290  {
291  const auto& p = polynomial->as<PolynomialSet>();
292  return vcsn::print(p.valueset(), p.value(), o, format(fmt));
293  }
294  }
295  }
296 
297 
298  /*------------------------.
299  | print(weight, stream). |
300  `------------------------*/
301 
302  namespace dyn
303  {
304  namespace detail
305  {
307  template <typename WeightSet, typename Ostream, typename String>
308  std::ostream& print_weight(const weight& weight, std::ostream& o,
309  const std::string& fmt)
310  {
311  const auto& w = weight->as<WeightSet>();
312  return vcsn::print(w.valueset(), w.value(), o, format(fmt));
313  }
314  }
315  }
316 }
std::ostream & grail(const Aut &aut, std::ostream &out)
Print automaton in Grail format.
Definition: grail.hh:185
std::ostream & efsm(const Aut &aut, std::ostream &out=std::cout)
Print automaton to EFSM format, based on FSM format.
Definition: efsm.hh:332
auto rs
Definition: lift.hh:152
format for_labels() const
A copy of this format, but to print labels.
Definition: format.hh:42
std::ostream & print_polynomial(const polynomial &polynomial, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:288
An input/output format for valuesets.
Definition: format.hh:13
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::ostream & list_polynomial(const polynomial &polynomial, std::ostream &o)
Bridge (list).
Definition: print.hh:269
std::ostream & print_expansion(const expansion &expansion, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:172
auto & as()
Downcast to the exact type.
Definition: context.hh:36
std::ostream & print_expression(const expression &exp, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:214
std::integral_constant< bool,((context_t_of< Aut >::is_lal||context_t_of< Aut >::is_lan||context_t_of< Aut >::is_lat)&&std::is_same< weightset_t_of< Aut >, b >::value)> has_fado_t
Definition: print.hh:35
std::ostream & print(const Aut &aut, std::ostream &out=std::cout, const std::string &fmt="default")
Definition: print.hh:83
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
std::ostream & print(const automaton &aut, std::ostream &o, const std::string &fmt)
Bridge.
Definition: print.hh:121
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_grail_t
Definition: print.hh:41
Template-less root for contexts.
Definition: context.hh:16
std::ostream & print_context(const context &ctx, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:140
A dyn automaton.
Definition: automaton.hh:17
std::ostream & fado(const Aut &aut, std::ostream &out=std::cout)
Format automaton to FAdo format.
Definition: grail.hh:115
dot_printer< ExpSet > make_dot_printer(const ExpSet &rs, std::ostream &out, bool physical=false)
Definition: dot.hh:421
std::ostream & dot(const Aut &aut, std::ostream &out=std::cout, format fmt={}, bool mathjax=false)
Print an automaton in Graphviz's Dot format.
Definition: dot.hh:395
auto fado_impl_(const Aut &aut, std::ostream &out) -> std::enable_if_t< has_fado_t< Aut >
Definition: print.hh:46
return v
Definition: multiply.hh:361
std::ostream & print_label(const label &label, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:233
auto & as()
Extract wrapped typed value.
Definition: value.hh:53
std::ostream & info(const Aut &aut, std::ostream &out=std::cout, unsigned details=2)
Print info about an automaton.
Definition: info.hh:73
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:67
std::ostream & daut(const Aut &aut, std::ostream &out=std::cout)
Print an automaton in Daut format.
Definition: daut.hh:101
std::ostream & print_weight(const weight &weight, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:308
A dyn Value/ValueSet.
Definition: fwd.hh:23
A mapping from strings to Values.
Definition: getargs.hh:33
value_impl< detail::expression_tag > expression
Definition: fwd.hh:25
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Print automaton to TikZ format.
Definition: tikz.hh:157
auto & as()
Extract wrapped typed automaton.
Definition: automaton.hh:37
std::ostream & list(const PolynomialSet &ps, const typename PolynomialSet::value_t &p, std::ostream &o=std::cout)
Definition: print.hh:249
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:61
auto grail_impl_(const Aut &aut, std::ostream &out) -> std::enable_if_t< has_grail_t< Aut >
Definition: print.hh:64