Vcsn  2.2a
Be Rational
dot.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 
5 #include <vcsn/algos/project.hh> // bad layering: should not be in algos.
7 #include <vcsn/misc/format.hh>
8 #include <vcsn/misc/indent.hh>
9 
10 namespace vcsn
11 {
12  namespace rat
13  {
15  template <typename ExpSet>
17  : public ExpSet::const_visitor
18  {
19  public:
20  using expressionset_t = ExpSet;
21  using super_t = typename expressionset_t::const_visitor;
23 
25  using identities_t = typename expressionset_t::identities_t;
29 
31  using node_t = typename super_t::node_t;
33  using value_t = typename node_t::value_t;
34  template <type_t Type>
35  using constant_t = typename super_t::template constant_t<Type>;
36  template <type_t Type>
37  using unary_t = typename super_t::template unary_t<Type>;
38  template <type_t Type>
39  using variadic_t = typename super_t::template variadic_t<Type>;
40 
42  constexpr static const char* me() { return "dot_printer"; }
43 
45  dot_printer(const expressionset_t& rs, std::ostream& out)
46  : out_{out}
47  , rs_{rs}
48  {
49  format(vcsn::format("text"));
50  }
51 
53  void format(format fmt)
54  {
55  fmt_ = fmt;
56  if (fmt_ == format::latex)
57  {
58  lgroup_ = "{";
59  rgroup_ = "}";
60  langle_ = " \\left\\langle ";
61  rangle_ = " \\right\\rangle ";
62  lparen_ = "\\left(";
63  rparen_ = "\\right)";
64  lexponent_ = "^{";
65  rexponent_ = "}";
66  star_ = "^{*}";
67  complement_ = "^{c}";
68  transposition_ = "^{T}";
69  conjunction_ = " \\& ";
70  infiltration_ = " \\uparrow ";
71  shuffle_ = " \\between ";
72  product_ = " \\, ";
73  sum_ = (rs_.identities().is_distributive() ? " \\oplus "
74  : " + ");
75  zero_ = "\\emptyset";
76  one_ = "\\varepsilon";
77  lmul_ = "\\,";
78  rmul_ = "\\,";
79  ldiv_ = " \\backslash ";
80  tuple_left = " \\left. ";
81  tuple_middle = " \\middle| ";
82  tuple_right = " \\right. ";
84  }
85  else if (fmt_ == format::text)
86  {
87  lgroup_ = "";
88  rgroup_ = "";
89  langle_ = "<";
90  rangle_ = ">";
91  lparen_ = "(";
92  rparen_ = ")";
93  lexponent_ = "{";
94  rexponent_ = "}";
95  star_ = "*";
96  complement_ = "{c}";
97  transposition_ = "{T}";
98  conjunction_ = "&";
99  infiltration_ = "&:";
100  shuffle_ = ":";
101  product_ = "";
102  sum_ = "+";
103  zero_ = "\\z";
104  one_ = "\\e";
105  lmul_ = "";
106  rmul_ = "";
107  ldiv_ = "{\\}";
108  tuple_left = "";
109  tuple_middle = "|";
110  tuple_right = "";
112  }
113  else if (fmt_ == format::utf8)
114  {
115  lgroup_ = "";
116  rgroup_ = "";
117  langle_ = "⟨";
118  rangle_ = "⟩";
119  lparen_ = "(";
120  rparen_ = ")";
121  star_ = "*";
122  complement_ = "ᶜ";
123  transposition_ = "ᵗ";
124  conjunction_ = "&";
125  infiltration_ = "&:";
126  shuffle_ = ":";
127  product_ = "";
128  sum_ = "+";
129  zero_ = "∅";
130  one_ = "ε";
131  lmul_ = "";
132  rmul_ = "";
133  ldiv_ = "{\\}";
134  tuple_left = "";
135  tuple_middle = "|";
136  tuple_right = "";
138  }
139  else
140  raise("expression: invalid format: ", fmt_);
141  }
142 
144  std::ostream& operator()(const value_t& v)
145  {
146  out_ << "digraph" << vcsn::iendl
147  << "{" << vcsn::incendl;
148  print(v);
150  << "}";
151  return out_;
152  }
153 
155  std::ostream& print(const value_t& v)
156  {
157  return print_(*v);
158  }
159 
160  private:
162  std::ostream& print_(const node_t& v)
163  {
164  v.accept(*this);
165  return out_;
166  }
167 
182 
183  using tuple_t = typename super_t::tuple_t;
184 
185  template <bool = context_t::is_lat,
186  typename Dummy = void>
187  struct visit_tuple
188  {
190  template <size_t I>
191  void print_(const tuple_t& v)
192  {
193  auto rs = detail::project<I>(self_.rs_);
194  const auto& r = std::get<I>(v.sub());
195  self_.out_
196  << vcsn::iendl
197  << "x" << address(v) << " -> "
198  << "x" << address(*r) << vcsn::iendl;
201  printer.print(r);
202  }
203 
205  template <size_t... I>
207  {
208  using swallow = int[];
209  (void) swallow
210  {
211  (print_<I>(v),
212  0)...
213  };
214  }
215 
217  void operator()(const tuple_t& v)
218  {
219  self_.out_ << "x" << address(v)
220  << " [label=\"" << self_.tuple_middle << "\"]";
221  print_(v, labelset_t::indices);
222  }
223  const self_t& self_;
224  };
225 
226  template <typename Dummy>
227  struct visit_tuple<false, Dummy>
228  {
229  void operator()(const tuple_t&)
230  {
232  }
233  const self_t& self_;
234  };
235 
236  void visit(const tuple_t& v, std::true_type) override
237  {
238  visit_tuple<>{*this}(v);
239  }
240 
242  template <rat::exp::type_t Type>
243  void print_(const unary_t<Type>& n, const char* op)
244  {
245  out_ << "x" << address(n)
246  << " [label=\"" << op << "\"]" << vcsn::iendl;
247  out_ << "x" << address(n) << " -> "
248  << "x" << address(*n.sub()) << vcsn::iendl;
249  print(n.sub());
250  }
251 
253  template <rat::exp::type_t Type>
254  void print_(const constant_t<Type>& n, const char* op)
255  {
256  out_ << "x" << address(n)
257  << " [label=\"" << op << "\"]";
258  }
259 
261  template <rat::exp::type_t Type>
262  void print_(const variadic_t<Type>& n, const char* op)
263  {
264  out_ << "x" << address(n)
265  << " [label=\"" << op << "\"]" << vcsn::iendl;
266  out_ << "x" << address(n) << " -> {";
267  for (const auto& i: n)
268  out_ << " x" << address(*i);
269  out_ << " }";
270  for (const auto& i: n)
271  {
272  out_ << vcsn::iendl;
273  print(i);
274  }
275  }
276 
278  std::ostream& print_(const weight_t& w)
279  {
280  out_ << "x" << address(w) << " [label=\"";
281  rs_.weightset()->print(w, out_, fmt_.for_weights());
282  return out_ << "\"]";
283  }
284 
286  void print_(const lweight_t& n)
287  {
288  out_ << "x" << address(n)
289  << " [label=\"lweight\"]" << vcsn::iendl
290  << "x" << address(n) << " -> {"
291  << " x" << address(n.weight())
292  << " x" << address(*n.sub())
293  << " }" << vcsn::iendl;
294  print_(n.weight()) << vcsn::iendl;
295  print(n.sub());
296  }
297 
299  void print_(const rweight_t& n)
300  {
301  out_ << "x" << address(n)
302  << " [label=\"rweight\"]" << vcsn::iendl
303  << "x" << address(n) << " -> {"
304  << " x" << address(*n.sub())
305  << " x" << address(n.weight())
306  << " }" << vcsn::iendl;
307  print(n.sub()) << vcsn::iendl;
308  print_(n.weight());
309  }
310 
312  void print_(const atom_t& n)
313  {
314  out_ << "x" << address(n)
315  << " [label=\"";
316  rs_.labelset()->print(n.value(), out_, fmt_.for_labels());
317  out_ << "\"]";
318  }
319 
321  std::ostream& out_;
323  class format fmt_;
327  const bool debug_ = !!getenv("VCSN_PARENS");
328 
333  const char* lgroup_ = nullptr;
334  const char* rgroup_ = nullptr;
336  const char* langle_ = nullptr;
337  const char* rangle_ = nullptr;
339  const char* lparen_ = nullptr;
340  const char* rparen_ = nullptr;
342  const char* lexponent_ = nullptr;
343  const char* rexponent_ = nullptr;
345  const char* lmul_ = nullptr;
346  const char* rmul_ = nullptr;
348  const char* ldiv_ = nullptr;
350  const char* star_ = nullptr;
351  const char* complement_ = nullptr;
352  const char* transposition_ = nullptr;
353  const char* conjunction_ = nullptr;
354  const char* infiltration_ = nullptr;
355  const char* shuffle_ = nullptr;
356  const char* product_ = nullptr;
357  const char* sum_ = nullptr;
358 
360  const char* tuple_left = nullptr;
362  const char* tuple_middle = nullptr;
364  const char* tuple_right = nullptr;
365 
367  const char* zero_ = nullptr;
368  const char* one_ = nullptr;
369  unsigned int exponent_threshold_ = 0;
370  };
371 
372  template <typename ExpSet>
374  make_dot_printer(const ExpSet& rs, std::ostream& out)
375  {
376  return {rs, out};
377  }
378  } // namespace rat
379 } // namespace vcsn
const char * transposition_
Definition: dot.hh:352
weight_t_of< context_t > weight_t
Definition: dot.hh:28
const char * star_
The expression operators.
Definition: dot.hh:350
std::ostream & print(const value_t &v)
Print an expression as a tree.
Definition: dot.hh:155
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
const char * tuple_right
Right tuple delimiter.
Definition: dot.hh:364
ExpSet expressionset_t
Definition: dot.hh:20
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
Definition: traits.hh:53
const char * rgroup_
Definition: dot.hh:334
const char * lmul_
External product.
Definition: dot.hh:345
VCSN_RAT_VISIT(one, v)
Definition: dot.hh:174
VCSN_RAT_VISIT(conjunction, v)
Definition: dot.hh:170
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
Definition: traits.hh:54
VCSN_RAT_VISIT(rweight, v)
Definition: dot.hh:176
An inner node implementing a weight.
Definition: expression.hh:264
typename super_t::template constant_t< Type > constant_t
Definition: dot.hh:35
VCSN_RAT_VISIT(star, v)
Definition: dot.hh:178
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:13
typename node_t::value_t value_t
A shared_ptr to node_t.
Definition: dot.hh:33
const char * lgroup_
Left and right boundaries (typically braces for LaTeX).
Definition: dot.hh:333
const char * rangle_
Definition: dot.hh:337
Print as plain (ASCII) text, escaped.
Definition: format.hh:26
context_t_of< expressionset_t > context_t
Definition: dot.hh:24
VCSN_RAT_VISIT(zero, v)
Definition: dot.hh:181
const char * tuple_middle
Tuple tape separator.
Definition: dot.hh:362
typename super_t::template unary_t< Type > unary_t
Definition: dot.hh:37
void print_(const constant_t< Type > &n, const char *op)
Print a nullary node.
Definition: dot.hh:254
dot_printer(const expressionset_t &rs, std::ostream &out)
A printer.
Definition: dot.hh:45
typename super_t::node_t node_t
Actual node, without indirection.
Definition: dot.hh:31
std::ostream & operator()(const value_t &v)
Entry point: print v as a complete Dot graph.
Definition: dot.hh:144
typename super_t::tuple_t tuple_t
Definition: dot.hh:183
VCSN_RAT_VISIT(prod, v)
Definition: dot.hh:175
const char * rexponent_
Definition: dot.hh:343
format for_labels() const
A copy of this format, but to print labels.
Definition: format.hh:40
typename expressionset_t::identities_t identities_t
Definition: dot.hh:25
void operator()(const tuple_t &v)
Entry point.
Definition: dot.hh:217
const char * complement_
Definition: dot.hh:351
void format(format fmt)
Set output format.
Definition: dot.hh:53
void format(format fmt)
Set output format.
Definition: printer.hxx:68
void print_(const lweight_t &n)
Print a left-weight.
Definition: dot.hh:286
std::ostream & incendl(std::ostream &o)
Increment the indentation, print an end of line, and set the indentation.
Definition: indent.cc:54
void print_(const tuple_t &v, detail::index_sequence< I... >)
Print all the tapes.
Definition: dot.hh:206
VCSN_RAT_VISIT(shuffle, v)
Definition: dot.hh:177
const char * tuple_left
Left tuple delimiter.
Definition: dot.hh:360
const char * lparen_
Left and right parentheses.
Definition: dot.hh:339
const expressionset_t & rs_
The expressionset.
Definition: dot.hh:325
const char * ldiv_
Quotient.
Definition: dot.hh:348
const char * shuffle_
Definition: dot.hh:355
typename expressionset_t::const_visitor super_t
Definition: dot.hh:21
auto rs
Definition: lift.hh:151
void print_(const rweight_t &n)
Print a right-weight.
Definition: dot.hh:299
Print for LaTeX.
Definition: format.hh:20
Pretty-printer for rational expressions.
Definition: fwd.hh:28
label_t_of< context_t > label_t
Definition: dot.hh:27
void print_(const atom_t &n)
Print a label.
Definition: dot.hh:312
const char * rparen_
Definition: dot.hh:340
int address(const void *t)
Name pointers, to make them easier to read.
Definition: memory.hh:22
unsigned int exponent_threshold_
Definition: dot.hh:369
dot_printer< ExpSet > make_dot_printer(const ExpSet &rs, std::ostream &out)
Definition: dot.hh:374
Pretty-printer for rational expressions.
Definition: dot.hh:16
std::ostream & decendl(std::ostream &o)
Decrement the indentation, print an end of line, and set the indentation.
Definition: indent.cc:59
const bool debug_
Whether to be overly verbose.
Definition: dot.hh:327
Indentation relative functions.
const char * langle_
Left and right angle brackets for weights.
Definition: dot.hh:336
std::ostream & print_(const node_t &v)
Easy recursion: print an expression.
Definition: dot.hh:162
const char * infiltration_
Definition: dot.hh:354
format for_weights() const
A copy of this format, but to print weights.
Definition: format.hh:48
labelset_t_of< context_t > labelset_t
Definition: dot.hh:26
VCSN_RAT_VISIT(complement, v)
Definition: dot.hh:169
static constexpr const char * me()
Name of this algorithm, for error messages.
Definition: dot.hh:42
VCSN_RAT_VISIT(sum, v)
Definition: dot.hh:179
An inner node with multiple children.
Definition: expression.hh:118
VCSN_RAT_VISIT(infiltration, v)
Definition: dot.hh:171
void visit(const tuple_t &v, std::true_type) override
Definition: dot.hh:236
std::ostream & iendl(std::ostream &o)
Print an end of line, then set the indentation.
Definition: indent.cc:49
void print_(const tuple_t &v)
Print one tape.
Definition: dot.hh:191
const char * lexponent_
Left and right braces for exponents.
Definition: dot.hh:342
class format fmt_
Output format.
Definition: dot.hh:323
typename super_t::template variadic_t< Type > variadic_t
Definition: dot.hh:39
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:56
const char * product_
Definition: dot.hh:356
void print_(const unary_t< Type > &n, const char *op)
Print a unary node.
Definition: dot.hh:243
Print as rich UTF-8 text, escaped.
Definition: format.hh:28
std::ostream & out_
Output stream.
Definition: dot.hh:321
VCSN_RAT_VISIT(transposition, v)
Definition: dot.hh:180
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
Definition: traits.hh:55
void print_(const variadic_t< Type > &n, const char *op)
Print a variadic node.
Definition: dot.hh:262
const char * zero_
The constants.
Definition: dot.hh:367
VCSN_RAT_VISIT(atom, v)
Definition: dot.hh:168
const char * rmul_
Definition: dot.hh:346
VCSN_RAT_VISIT(lweight, v)
Definition: dot.hh:173
const char * sum_
Definition: dot.hh:357
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
Definition: traits.hh:58
std::ostream & print_(const weight_t &w)
Print a weight.
Definition: dot.hh:278
VCSN_RAT_VISIT(ldiv, v)
Definition: dot.hh:172
An input/output format for valuesets.
Definition: format.hh:11
const char * one_
Definition: dot.hh:368
const char * conjunction_
Definition: dot.hh:353
Definition: a-star.hh:8