Awali
Another Weighted Automata library
daut.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_DAUT_HH
18 # define AWALI_ALGOS_DAUT_HH
19 
20 # include <algorithm>
21 # include <iostream>
22 
25 #include <awali/sttc/misc/set.hh>
26 
27 namespace awali { namespace sttc {
28 
29  namespace internal
30  {
31  /*-------------------------.
32  | daut(automaton, stream). |
33  `-------------------------*/
34 
38  template <typename Aut>
39  class dautter: public outputter<Aut>
40  {
41  private:
42  using super_type = outputter<Aut>;
43  using typename super_type::automaton_t;
44  using typename super_type::weightset_t;
45  using typename super_type::weight_t;
46 
47  using super_type::aut_;
48  using super_type::finals_;
51  using super_type::os_;
52  using super_type::ws_;
53 
54  using super_type::super_type;
55 
56  public:
57  dautter(const automaton_t& aut, std::ostream& out)
58  : super_type(aut, out)
59  {}
60 
65  bool format(const std::string& sep,
66  const std::string& kind, const weight_t& w)
67  {
68  if (ws_.is_zero(w))
69  return false;
70  else
71  {
72  os_ << sep << kind;
73  if (ws_.show_one() || !ws_.is_one(w))
74  {
75  os_ << ", " << kind << " text={";
76  ws_.print(w, os_) << '}';
77  }
78  return true;
79  }
80  }
81 
82  void
84  {
85  os_ << (s-2);
86  os_ << " [label = \"";
87  aut_->print_state_name(s, os_, "text");
88  os_ << "\"]";
89  }
90 
91  std::ostream& operator()() {
92  os_ <<
93  "context = \"" << aut_->context().vname() << "\"\n";
94  if (!aut_->states().empty()) {
95  os_ << "{\n";
96  for (auto s : aut_->states())
97  {
98  os_ << " ";
99  print_state_(s);
100  os_ << '\n';
101  }
102  os_ << "}\n";
103  }
104  for (auto src : aut_->all_states()) {
105  // Sort by destination state.
106  std::set<state_t> ds;
107  for (auto t: aut_->all_out(src))
108  ds.insert(aut_->dst_of(t));
109  for (auto dst: ds) {
110  if (src == aut_->pre())
111  os_ << '$' << " -> " << (dst-2);
112  else if (dst == aut_->post())
113  os_ << (src-2) << " -> " << '$';
114  else
115  os_ << (src-2) << " -> " << (dst-2);
116  std::string s = format_entry_(src, dst, "text");
117  if (!s.empty()) {
118  os_ << " [";
119  os_ << "label = \"" << str_escape(s) << "\"";
120  os_ << ']';
121  }
122  os_ << '\n';
123  }
124  }
125  return os_;
126  }
127  };
128  }
133  template <typename Aut>
134  std::ostream&
135  daut(const Aut& aut, std::ostream& out)
136  {
137  internal::dautter<Aut> daut(aut, out);
138  return daut();
139  }
140 }}//end of ns awali::stc
141 
142 #endif // !AWALI_ALGOS_DAUT_HH
Format an automaton into daut vcsn format.
Definition: daut.hh:40
dautter(const automaton_t &aut, std::ostream &out)
Definition: daut.hh:57
std::ostream & operator()()
Definition: daut.hh:91
bool format(const std::string &sep, const std::string &kind, const weight_t &w)
Format a TikZ attribute.
Definition: daut.hh:65
void print_state_(state_t s)
Definition: daut.hh:83
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
states_t finals_()
The list of final states, sorted.
Definition: grail.hh:144
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
states_t initials_()
The list of initial states, sorted.
Definition: grail.hh:134
weightset_t_of< automaton_t > weightset_t
Definition: grail.hh:61
std::ostream & daut(const Aut &aut, std::ostream &out)
Output in 'daut' format.
Definition: daut.hh:135
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21