Awali
Another Weighted Automata library
tikz.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_TIKZ_HH
18 # define AWALI_ALGOS_TIKZ_HH
19 
20 # include <algorithm>
21 # include <iostream>
22 # include <unordered_map>
23 # include <unordered_set>
24 # include <vector>
25 
26 // FIXME: factor dot and tikz.
27 #include <awali/sttc/algos/grail.hh> // outputter
28 #include <awali/sttc/algos/dot.hh> // format_entry
29 
30 namespace awali { namespace sttc {
31 
32  /*--------------------------.
33  | tikz(automaton, stream). |
34  `--------------------------*/
35 
36  namespace internal
37  {
41  template <typename Aut>
42  class tikzer: public outputter<Aut>
43  {
44  public:
46  using typename super_type::automaton_t;
47  using typename super_type::state_t;
48  using typename super_type::transition_t;
49  using typename super_type::weight_t;
50 
51  using super_type::aut_;
53  using super_type::os_;
54  using super_type::ws_;
55 
56  using super_type::super_type;
57 
61  void format(const std::string& kind, const weight_t& w)
62  {
63  if (!ws_.is_zero(w))
64  {
65  os_ << ',' << kind;
66  if (ws_.show_one() || !ws_.is_one(w))
67  {
68  os_ << ',' << kind << " text=$\\langle ";
69  ws_.print(w, os_, "latex") << "\\rangle$";
70  }
71  }
72  }
73 
74  void operator()()
75  {
76  os_ <<
77  "\\documentclass{standalone}\n"
78  " \\usepackage{tikz}\n"
79  " \\usetikzlibrary{arrows, automata, positioning}\n"
80  " \\tikzstyle{automaton}=[shorten >=1pt, pos=.4,\n"
81  " >=stealth', initial text=]\n"
82  " \\tikzstyle{accepting}=[accepting by arrow]\n"
83  "\n"
84  "\\begin{document}\n"
85  "\\begin{tikzpicture}[automaton]\n"
86  ;
87 
88  for (auto s : aut_->states())
89  {
90  os_ << " \\node[state";
91  format("initial", aut_->get_initial_weight(s));
92  format("accepting", aut_->get_final_weight(s));
93  os_ << "] (";
94  aut_->print_state(s, os_);
95  os_ << ')';
96  if (2 < s)
97  {
98  os_ << " [right=of ";
99  aut_->print_state(s - 1, os_);
100  os_ << ']';
101  }
102  os_ << " {$";
103  aut_->print_state(s, os_);
104  os_ << "$};\n";
105  }
106 
107  for (auto src : aut_->states())
108  {
109  std::set<state_t> ds;
110  for (auto t: aut_->out(src))
111  ds.insert(aut_->dst_of(t));
112  for (auto dst: ds)
113  {
114  os_ << " \\path[->] (";
115  aut_->print_state(src, os_);
116  os_ << ')'
117  << " edge"
118  << (src == dst ? "[loop above]" : "")
119  << " node[above]"
120  << " {$" << format_entry_(src, dst, "latex") << "$}"
121  << " (";
122  aut_->print_state(dst, os_);
123  os_ << ");\n";
124  }
125  }
126  os_ <<
127  "\\end{tikzpicture}\n"
128  "\\end{document}";
129  }
130  };
131  }
132 
136  template <typename AutPtr>
137  std::ostream&
138  tikz(const AutPtr& aut, std::ostream& out)
139  {
140  internal::tikzer<AutPtr> t{aut, out};
141  t();
142  return out;
143  }
144 
145 }}//end of ns awali::stc
146 
147 #endif // !AWALI_ALGOS_TIKZ_HH
Factor common bits in automaton formatting.
Definition: grail.hh:43
std::string format_entry_(state_t src, state_t dst, const std::string &fmt="text")
The labels and weights of transitions from src to dst.
Definition: grail.hh:88
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:154
weight_t_of< automaton_t > weight_t
Definition: grail.hh:62
Aut automaton_t
Definition: grail.hh:45
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:160
std::ostream & os_
Output stream.
Definition: grail.hh:156
Format automaton to TikZ format.
Definition: tikz.hh:43
std::string format_entry_(state_t src, state_t dst, const std::string &fmt="text")
The labels and weights of transitions from src to dst.
Definition: grail.hh:88
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:154
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:160
void operator()()
Definition: tikz.hh:74
std::ostream & os_
Output stream.
Definition: grail.hh:156
void format(const std::string &kind, const weight_t &w)
Format a TikZ attribute.
Definition: tikz.hh:61
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Format automaton to TikZ format.
Definition: tikz.hh:138
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22