Vcsn  2.1
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 <typename 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 <typename Aut>
39  auto
40  fado_impl_(const Aut& aut, std::ostream& out)
41  -> enable_if_t<has_fado_t<Aut>{}, void>
42  {
43  fado(aut, out);
44  }
45 
46  template <typename Aut>
47  ATTRIBUTE_NORETURN
48  auto
49  fado_impl_(const Aut&, std::ostream&)
51  {
52  raise("print: FAdo requires letter or nullable labels,"
53  " and Boolean weights");
54  }
55 
56  template <typename Aut>
57  auto
58  grail_impl_(const Aut& aut, std::ostream& out)
59  -> enable_if_t<has_fado_t<Aut>{}, void>
60  {
61  grail(aut, out);
62  }
63 
64  template <typename Aut>
65  ATTRIBUTE_NORETURN
66  auto
67  grail_impl_(const Aut&, std::ostream&)
69  {
70  raise("print: Grail requires letter or nullable labels,"
71  " and Boolean weights");
72  }
73  }
74 
75  template <typename Aut>
76  std::ostream&
77  print(const Aut& aut, std::ostream& out, const std::string& format)
78  {
79  static const auto map
80  = std::map<std::string, std::function<void(const Aut&, std::ostream&)>>
81  {
82  {"dot", [](const Aut& a, std::ostream& o){ dot(a, o); }},
83  {"default", [](const Aut& a, std::ostream& o){ dot(a, o); }},
84  {"dot2tex", [](const Aut& a, std::ostream& o){ dot(a, o, true); }},
85  {"efsm", [](const Aut& a, std::ostream& o){ efsm(a, o); }},
86  {"fado", detail::fado_impl_<Aut>},
87  {"grail", detail::grail_impl_<Aut>},
88  {"info", [](const Aut& a, std::ostream& o){ info(a, o); }},
89  {"info,detailed",[](const Aut& a, std::ostream& o){ info(a, o, true); }},
90  {"null", [](const Aut&, std::ostream&){}},
91  {"tikz", [](const Aut& a, std::ostream& o){ tikz(a, o); }},
92  };
93  auto fun = getargs("automaton output format", map, format);
94  fun(aut, out);
95  return out;
96  }
97 
98  namespace dyn
99  {
100  namespace detail
101  {
103  template <typename Aut, typename Ostream, typename String>
104  std::ostream& print(const automaton& aut, std::ostream& o,
105  const std::string& fmt)
106  {
107  const auto& a = aut->as<Aut>();
108  return print(a, o, fmt);
109  }
110  }
111  }
112 
113  /*-------------------------.
114  | print(context, stream). |
115  `-------------------------*/
116 
117  namespace dyn
118  {
119  namespace detail
120  {
122  template <typename Context, typename Ostream, typename String>
123  std::ostream& print_context(const context& ctx, std::ostream& o,
124  const std::string& fmt)
125  {
126  const auto& c = ctx->as<Context>();
127  return c.print_set(o, format(fmt));
128  }
129  }
130  }
131 
132  /*---------------------------.
133  | print(expansion, stream). |
134  `---------------------------*/
135 
138  template <typename ValueSet>
139  inline
140  std::ostream&
141  print(const ValueSet& vs, const typename ValueSet::value_t& v,
142  std::ostream& o, format fmt)
143  {
144  return vs.print(v, o, format(fmt));
145  }
146 
147  namespace dyn
148  {
149  namespace detail
150  {
152  template <typename ExpansionSet, typename Ostream, typename String>
153  std::ostream& print_expansion(const expansion& expansion, std::ostream& o,
154  const std::string& fmt)
155  {
156  const auto& e = expansion->as<ExpansionSet>();
157  return vcsn::print(e.expansionset(), e.expansion(), o, format(fmt));
158  }
159  }
160  }
161 
162  /*-----------------------.
163  | print(label, stream). |
164  `-----------------------*/
165 
166  namespace dyn
167  {
168  namespace detail
169  {
171  template <typename LabelSet, typename Ostream, typename String>
172  std::ostream& print_label(const label& label, std::ostream& o,
173  const std::string& fmt)
174  {
175  const auto& l = label->as<LabelSet>();
176  return vcsn::print(l.labelset(), l.label(), o, format(fmt));
177  }
178  }
179  }
180 
181  /*---------------------------.
182  | list(polynomial, stream). |
183  `---------------------------*/
184 
185  template <typename PolynomialSet>
186  inline
187  std::ostream&
188  list(const PolynomialSet& ps, const typename PolynomialSet::value_t& p,
189  std::ostream& o)
190  {
191  bool first = true;
192  for (const auto& m: p)
193  {
194  if (!first)
195  o << std::endl;
196  first = false;
197  ps.print(m, o);
198  }
199  return o;
200  }
201 
202  namespace dyn
203  {
204  namespace detail
205  {
207  template <typename PolynomialSet, typename Ostream>
208  std::ostream& list_polynomial(const polynomial& polynomial,
209  std::ostream& o)
210  {
211  const auto& p = polynomial->as<PolynomialSet>();
212  return vcsn::list(p.polynomialset(), p.polynomial(), o);
213  }
214  }
215  }
216 
217  /*----------------------------.
218  | print(polynomial, stream). |
219  `----------------------------*/
220 
221  namespace dyn
222  {
223  namespace detail
224  {
226  template <typename PolynomialSet, typename Ostream, typename String>
227  std::ostream& print_polynomial(const polynomial& polynomial,
228  std::ostream& o, const std::string& fmt)
229  {
230  const auto& p = polynomial->as<PolynomialSet>();
231  return vcsn::print(p.polynomialset(), p.polynomial(), o, format(fmt));
232  }
233  }
234  }
235 
236 
237  /*-----------------------------.
238  | print(expression, stream). |
239  `-----------------------------*/
240 
241 #if 0
242  template <typename ExpSet>
244  inline
245  std::ostream&
246  print(const ExpSet& rs, const typename ExpSet::value_t& e,
247  std::ostream& o, format fmt)
248  {
249  return rs.print(e, o, format(fmt));
250  }
251 #endif
252 
253  namespace dyn
254  {
255  namespace detail
256  {
258  template <typename ExpSet, typename Ostream, typename String>
259  std::ostream& print_expression(const expression& exp, std::ostream& o,
260  const std::string& fmt)
261  {
262  const auto& e = exp->as<ExpSet>();
263  return vcsn::print(e.expressionset(), e.expression(), o, format(fmt));
264  }
265  }
266  }
267 
268  /*------------------------.
269  | print(weight, stream). |
270  `------------------------*/
271 
272 #if 0
273  template <typename WeightSet>
275  inline
276  std::ostream&
277  print(const WeightSet& ws, const typename WeightSet::value_t& w,
278  std::ostream& o)
279  {
280  return ws.print(w, o);
281  }
282 #endif
283 
284  namespace dyn
285  {
286  namespace detail
287  {
289  template <typename WeightSet, typename Ostream, typename String>
290  std::ostream& print_weight(const weight& weight, std::ostream& o,
291  const std::string& fmt)
292  {
293  const auto& w = weight->as<WeightSet>();
294  return vcsn::print(w.weightset(), w.weight(), o, format(fmt));
295  }
296  }
297  }
298 }
std::ostream & print_polynomial(const polynomial &polynomial, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:227
std::ostream & grail(const Aut &aut, std::ostream &out)
Definition: grail.hh:272
std::shared_ptr< const detail::label_base > label
Definition: fwd.hh:59
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:41
std::ostream & print(const automaton &aut, std::ostream &o, const std::string &fmt)
Bridge.
Definition: print.hh:104
std::ostream & print_expression(const expression &exp, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:259
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Print automaton to TikZ format.
Definition: tikz.hh:151
C::mapped_type getargs(const std::string &kind, const C &map, const std::string &key)
Find a correspondance in a map.
Definition: getargs.hh:21
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
Definition: traits.hh:51
typename std::enable_if< Cond, T >::type enable_if_t
Definition: type_traits.hh:16
std::ostream & fado(const Aut &aut, std::ostream &out)
Definition: grail.hh:204
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:80
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:86
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
An input/output format.
Definition: format.hh:11
std::ostream & print(const Aut &aut, std::ostream &out, const std::string &format)
Definition: print.hh:77
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
std::ostream & list(const PolynomialSet &ps, const typename PolynomialSet::value_t &p, std::ostream &o)
Definition: print.hh:188
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
std::shared_ptr< const detail::expansion_base > expansion
Definition: expansion.hh:73
auto fado_impl_(const Aut &aut, std::ostream &out) -> enable_if_t< has_fado_t< Aut >
Definition: print.hh:40
auto rs
Definition: lift.hh:151
std::shared_ptr< const detail::polynomial_base > polynomial
Definition: fwd.hh:68
std::ostream & print_label(const label &label, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:172
std::ostream & print_context(const context &ctx, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:123
std::ostream & efsm(const Aut &aut, std::ostream &out)
Format automaton to EFSM format, based on FSM format.
Definition: efsm.hh:343
std::ostream & list_polynomial(const polynomial &polynomial, std::ostream &o)
Bridge (list).
Definition: print.hh:208
std::ostream & dot(const Aut &aut, std::ostream &out, bool dot2tex=false)
Definition: dot.hh:371
std::shared_ptr< detail::expression_base > expression
Definition: expression.hh:78
auto grail_impl_(const Aut &aut, std::ostream &out) -> enable_if_t< has_fado_t< Aut >
Definition: print.hh:58
std::ostream & print_expansion(const expansion &expansion, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:153
std::ostream & print_weight(const weight &weight, std::ostream &o, const std::string &fmt)
Bridge (print).
Definition: print.hh:290
std::ostream & info(const Aut &aut, std::ostream &out, bool detailed=false)
Definition: info.hh:207