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