dotty.cc 7.42 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2011, 2012, 2014, 2015 Laboratoire de Recherche et
3
// Developpement de l'Epita (LRDE).
4
// Copyright (C) 2003, 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
5
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
6
7
8
9
10
11
// et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
12
// the Free Software Foundation; either version 3 of the License, or
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
13
14
15
16
17
18
19
20
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
22

23
#include <ostream>
24
#include <stdexcept>
25
#include "tgba/tgbagraph.hh"
26
#include "dotty.hh"
27
#include "dottydec.hh"
28
#include "tgba/bddprint.hh"
29
#include "reachiter.hh"
30
#include "misc/escape.hh"
31
#include "tgba/tgbagraph.hh"
32
#include "tgba/formula2bdd.hh"
33
#include "tgbaalgos/sccinfo.hh"
34
35
36

namespace spot
{
37
  namespace
38
  {
39
40
41
42
43
    class dotty_output
    {
      std::ostream& os_;
      bool opt_force_acc_trans_ = false;
      bool opt_horizontal_ = true;
44
      bool opt_name_ = false;
45
46
      bool opt_circles_ = false;
      bool mark_states_ = false;
47
      bool opt_scc_ = false;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
      const_tgba_digraph_ptr aut_;

    public:
      dotty_output(std::ostream& os, const char* options)
	: os_(os)
      {
	if (options)
	  while (char c = *options++)
	    switch (c)
	      {
	      case 'c':
		opt_circles_ = true;
		break;
	      case 'h':
		opt_horizontal_ = true;
		break;
	      case 'n':
		opt_name_ = true;
	        break;
	      case 'N':
		opt_name_ = false;
	        break;
70
71
72
	      case 's':
		opt_scc_ = true;
		break;
73
74
75
76
	      case 'v':
		opt_horizontal_ = false;
		break;
	      case 't':
77
		opt_force_acc_trans_ = true;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
		break;
	      default:
		throw std::runtime_error
		  (std::string("unknown option for dotty(): ") + c);
	      }
      }

      void
      start()
      {
	os_ << "digraph G {\n";
	if (opt_horizontal_)
	  os_ << "  rankdir=LR\n";
	if (opt_name_)
	  if (auto n = aut_->get_named_prop<std::string>("automaton-name"))
	    escape_str(os_ << "  label=\"", *n) << "\"\n  labelloc=\"t\"\n";
	if (opt_circles_)
	  os_ << "  node [shape=\"circle\"]\n";
	os_ << "  I [label=\"\", style=invis, ";
	os_ << (opt_horizontal_ ? "width" : "height");
	os_ << "=0]\n  I -> " << aut_->get_init_state_number() << '\n';
      }

      void
      end()
      {
	os_ << '}' << std::endl;
      }

      void
      process_state(unsigned s)
      {
	os_ << "  " << s << " [label=\"" << escape_str(aut_->format_state(s))
	    << '"';
	if (mark_states_ && aut_->state_is_accepting(s))
	  os_ << ", peripheries=2";
	os_ << "]\n";
      }

      void
      process_link(const tgba_digraph::trans_storage_t& t)
      {
	std::string label = bdd_format_formula(aut_->get_dict(), t.cond);
	label = escape_str(label);
	if (!mark_states_)
	  if (auto a = t.acc)
	    {
	      label += "\\n";
	      label += aut_->acc().format(a);
	    }
	os_ << "  " << t.src << " -> " << t.dst
	    << " [label=\"" + label + "\"]\n";
      }

      void print(const const_tgba_digraph_ptr& aut)
      {
	aut_ = aut;
135
	mark_states_ = !opt_force_acc_trans_ && aut_->is_sba();
136
137
	auto si =
	  std::unique_ptr<scc_info>(opt_scc_ ? new scc_info(aut) : nullptr);
138
	start();
139
140
141
142
143
144
	if (si)
	  {
	    unsigned sccs = si->scc_count();
	    for (unsigned i = 0; i < sccs; ++i)
	      {
		os_ << "  subgraph cluster_" << i << " {\n";
145
146
147
148
		if (opt_name_)
		  // Reset the label, otherwise the graph label would
		  // be inherited by the cluster.
		  os_ << "  label=\"\"\n";
149
150
151
152
153
		for (auto s: si->states_of(i))
		  process_state(s);
		os_ << "  }\n";
	      }
	  }
154
155
156
	unsigned ns = aut_->num_states();
	for (unsigned n = 0; n < ns; ++n)
	  {
157
	    if (!si || !si->reachable_state(n))
158
	      process_state(n);
159
160
161
162
163
164
165
166
	    for (auto& t: aut_->out(n))
	      process_link(t);
	  }
	end();
      }
    };


167
    class dotty_bfs : public tgba_reachable_iterator_breadth_first
168
    {
169
    public:
170
      dotty_bfs(std::ostream& os, const_tgba_ptr a, bool mark_accepting_states,
171
		const char* options, dotty_decorator* dd)
172
173
	: tgba_reachable_iterator_breadth_first(a), os_(os),
	  mark_accepting_states_(mark_accepting_states), dd_(dd),
174
	  sba_(std::dynamic_pointer_cast<const tgba_digraph>(a))
175
      {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	if (options)
	  while (char c = *options++)
	    switch (c)
	      {
	      case 'c':
		opt_circles = true;
		break;
	      case 'h':
		opt_horizontal = true;
		break;
	      case 'n':
		opt_name = true;
	        break;
	      case 'N':
		opt_name = false;
	        break;
	      case 'v':
		opt_horizontal = false;
		break;
	      case 't':
		mark_accepting_states_ = false;
		break;
	      default:
		throw std::runtime_error
		  (std::string("unknown option for dotty(): ") + c);
	      }
202
      }
203

204
205
206
      void
      start()
      {
207
208
209
210
211
212
213
214
215
216
217
	os_ << "digraph G {\n";
	if (opt_horizontal)
	  os_ << "  rankdir=LR\n";
	if (opt_name)
	  if (auto n = aut_->get_named_prop<std::string>("automaton-name"))
	    escape_str(os_ << "  label=\"", *n) << "\"\n  labelloc=\"t\"\n";
	if (opt_circles)
	  os_ << "  node [shape=\"circle\"]\n";
	os_ << "  0 [label=\"\", style=invis, ";
	os_ << (opt_horizontal ? "width" : "height");
	os_ << "=0]\n  0 -> 1\n";
218
      }
219

220
221
222
      void
      end()
      {
223
	os_ << '}' << std::endl;
224
      }
225

226
      void
227
      process_state(const state* s, int n, tgba_succ_iterator* si)
228
      {
229
230
231
232
233
234
235
236
237
238
239
	bool accepting;

	if (mark_accepting_states_)
	  {
	    if (sba_)
	      {
		accepting = sba_->state_is_accepting(s);
	      }
	    else
	      {
		si->first();
240
241
		auto a = si->current_acceptance_conditions();
		accepting = !si->done() && aut_->acc().accepting(a);
242
243
244
245
246
247
248
	      }
	  }
	else
	  {
	    accepting = false;
	  }

249
	os_ << "  " << n << ' '
250
251
	    << dd_->state_decl(aut_, s, n, si,
			       escape_str(aut_->format_state(s)),
252
			       accepting)
253
	    << '\n';
254
      }
255

256
      void
257
258
      process_link(const state* in_s, int in,
		   const state* out_s, int out, const tgba_succ_iterator* si)
259
      {
260
	std::string label =
261
	  bdd_format_formula(aut_->get_dict(),
262
			     si->current_condition());
263
264
265
266
267
268
	if (!mark_accepting_states_)
	  if (auto a = si->current_acceptance_conditions())
	    {
	      label += "\n";
	      label += aut_->acc().format(a);
	    }
269

270
	std::string s = aut_->transition_annotation(si);
271
272
273
274
275
276
277
	if (!s.empty())
	  {
	    if (*label.rbegin() != '\n')
	      label += '\n';
	    label += s;
	  }

278
	os_ << "  " << in << " -> " << out << ' '
279
	    << dd_->link_decl(aut_, in_s, in, out_s, out, si,
280
			      escape_str(label))
281
	    << '\n';
282
      }
283

284
285
    private:
      std::ostream& os_;
286
      bool mark_accepting_states_;
287
      dotty_decorator* dd_;
288
      const_tgba_digraph_ptr sba_;
289
290
291
      bool opt_horizontal = true;
      bool opt_name = true;
      bool opt_circles = false;
292
293
    };
  }
294
295

  std::ostream&
296
  dotty_reachable(std::ostream& os, const const_tgba_ptr& g,
297
298
		  bool assume_sba, const char* options,
		  dotty_decorator* dd)
299
  {
300
301
302
303
304
305
306
    if (dd)
      {
	dotty_bfs d(os, g, assume_sba || g->has_state_based_acc(), options, dd);
	d.run();
	return os;
      }
    dotty_output d(os, options);
307
308
309
    auto aut = std::dynamic_pointer_cast<const tgba_digraph>(g);
    if (!aut)
      aut = make_tgba_digraph(g, tgba::prop_set::all());
310
    d.print(aut);
311
312
313
314
315
    return os;
  }


}