dot.cc 26.5 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2011, 2012, 2014-2017 Laboratoire de Recherche
// et Développement 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 <sstream>
25
#include <stdexcept>
26
27
28
29
30
31
32
33
#include <spot/twa/twagraph.hh>
#include <spot/twaalgos/dot.hh>
#include <spot/twa/bddprint.hh>
#include <spot/twaalgos/reachiter.hh>
#include <spot/misc/escape.hh>
#include <spot/twa/twagraph.hh>
#include <spot/twa/formula2bdd.hh>
#include <spot/twaalgos/sccinfo.hh>
34
#include <spot/kripke/fairkripke.hh>
35
#include <cstdlib>
36
#include <cstring>
37
#include <algorithm>
38
#include <ctype.h>
39
#include <utility>
40

41
42
43

namespace spot
{
44
  namespace
45
  {
46
47
    constexpr int MAX_BULLET = 20;

48
49
50
51
52
53
54
55
56
57
58
59
60
61
    static constexpr const char palette[][8] =
      {
        "#5DA5DA", /* blue */
        "#F17CB0", /* pink */
        "#FAA43A", /* orange */
        "#B276B2", /* purple */
        "#60BD68", /* green */
        "#F15854", /* red */
        "#B2912F", /* brown */
        "#4D4D4D", /* gray */
        "#DECF3F", /* yellow */
      };
    constexpr int palette_mod = sizeof(palette) / sizeof(*palette);

62
    class dotty_output final
63
    {
64
      // Keep all 0/false-initialized values together.
65
      std::vector<std::string>* sn_ = nullptr;
66
67
      std::map<unsigned, unsigned>* highlight_edges_ = nullptr;
      std::map<unsigned, unsigned>* highlight_states_ = nullptr;
68
      std::vector<std::pair<unsigned, unsigned>>* sprod_ = nullptr;
69
      std::vector<unsigned>* orig_ = nullptr;
70
      std::set<unsigned>* incomplete_ = nullptr;
71
      std::string* name_ = nullptr;
72
73
      std::map<std::pair<int, int>, int> univ_done;

74
75
      acc_cond::mark_t inf_sets_ = 0U;
      acc_cond::mark_t fin_sets_ = 0U;
76
77
78
79
80
81
82
83
84
85
      unsigned opt_shift_sets_ = 0;
      enum { ShapeAuto = 0, ShapeCircle, ShapeEllipse } opt_shape_ = ShapeAuto;
      bool opt_force_acc_trans_ = false;
      bool opt_vertical_ = false;
      bool opt_name_ = false;
      bool opt_show_acc_ = false;
      bool mark_states_ = false;
      bool opt_scc_ = false;
      bool opt_html_labels_ = false;
      bool opt_state_labels_ = false;
86
87
      bool opt_rainbow = false;
      bool opt_bullet = false;
88
      bool opt_bullet_but_buchi = false;
89
      bool opt_all_bullets = false;
90
      bool opt_ordered_edges_ = false;
91
      bool opt_numbered_edges_ = false;
92
      bool opt_orig_show_ = false;
93
94
95
      bool max_states_given_ = false; // related to max_states_

      const_twa_graph_ptr aut_;
96
      std::string opt_font_;
97
      std::string opt_node_color_;
98
99
100
      std::ostream& os_;
      bool opt_want_state_names_ = true;
      unsigned max_states_ = -1U; // related to max_states_given_
101

102
103
      bool opt_shared_univ_dest_ = true;

104
    public:
105

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
106
107
      unsigned max_states()
      {
108
        return max_states_;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
109
110
111
112
      }

      bool max_states_given()
      {
113
        return max_states_given_;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
114
115
      }

116
117
      void
      parse_opts(const char* options)
118
      {
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
202
203
204
        const char* orig = options;
        while (char c = *options++)
          switch (c)
            {
            case '.':
              {
                // Copy the value in a string, so future calls to
                // parse_opts do not fail if the environment has
                // changed.  (This matters particularly in an ipython
                // notebook, where it is tempting to redefine
                // SPOT_DOTDEFAULT.)
                static std::string def = []()
                  {
                    auto s = getenv("SPOT_DOTDEFAULT");
                    return s ? s : "";
                  }();
                // Prevent infinite recursions...
                if (orig == def.c_str())
                  throw std::runtime_error
                    (std::string("SPOT_DOTDEFAULT should not contain '.'"));
                if (!def.empty())
                  parse_opts(def.c_str());
                break;
              }
            case '+':
              {
                char* end;
                opt_shift_sets_ = strtoul(options, &end, 10);
                if (options == end)
                  throw std::runtime_error
                    ("missing number after '+' in print_dot() options");
                options = end;
                break;
              }
            case '<':
              {
                char* end;
                max_states_ = strtoul(options, &end, 10);
                if (options == end)
                  throw std::runtime_error
                    ("missing number after '<' in print_dot() options");
                if (max_states_ == 0)
                  {
                    max_states_ = -1U;
                    max_states_given_ = false;
                  }
                else
                  {
                    max_states_given_ = true;
                  }
                options = end;
                break;
              }
            case '#':
              opt_numbered_edges_ = true;
              break;
            case '1':
              opt_want_state_names_ = false;
              break;
            case 'a':
              opt_show_acc_ = true;
              break;
            case 'b':
              opt_bullet = true;
              opt_bullet_but_buchi = false;
              break;
            case 'B':
              opt_bullet = true;
              opt_bullet_but_buchi = true;
              break;
            case 'c':
              opt_shape_ = ShapeCircle;
              break;
            case 'C':
              if (*options != '(')
                throw std::runtime_error
                  ("invalid node color specification for print_dot()");
              {
                auto* end = strchr(++options, ')');
                if (!end)
                  throw std::runtime_error
                    ("invalid node color specification for print_dot()");
                opt_node_color_ = std::string(options, end - options);
                options = end + 1;
              }
              break;
205
206
207
            case 'd':
              opt_orig_show_ = true;
              break;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
            case 'e':
              opt_shape_ = ShapeEllipse;
              break;
            case 'f':
              if (*options != '(')
                throw std::runtime_error
                  ("invalid font specification for print_dot()");
              {
                auto* end = strchr(++options, ')');
                if (!end)
                  throw std::runtime_error
                    ("invalid font specification for print_dot()");
                opt_font_ = std::string(options, end - options);
                options = end + 1;
              }
              break;
            case 'h':
225
              opt_vertical_ = false;
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
              break;
            case 'k':
              opt_state_labels_ = true;
              break;
            case 'n':
              opt_name_ = true;
              break;
            case 'N':
              opt_name_ = false;
              break;
            case 'o':
              opt_ordered_edges_ = true;
              break;
            case 'r':
              opt_html_labels_ = true;
              opt_rainbow = true;
              break;
            case 'R':
              opt_html_labels_ = true;
              opt_rainbow = false;
              break;
            case 's':
              opt_scc_ = true;
              break;
            case 'v':
251
              opt_vertical_ = true;
252
253
254
255
              break;
            case 't':
              opt_force_acc_trans_ = true;
              break;
256
257
258
            case 'y':
              opt_shared_univ_dest_ = false;
              break;
259
260
261
262
            default:
              throw std::runtime_error
                (std::string("unknown option for print_dot(): ") + c);
            }
263
264
265
      }

      dotty_output(std::ostream& os, const char* options)
266
        : os_(os)
267
      {
268
        parse_opts(options ? options : ".");
269
270
271
272
273
      }

      void
      output_set(std::ostream& os, int v) const
      {
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        v += opt_shift_sets_;
        if (opt_bullet && (v >= 0) & (v <= MAX_BULLET))
          {
            static const char* const tab[MAX_BULLET + 1] = {
              "⓿", "❶", "❷", "❸",
              "❹", "❺", "❻", "❼",
              "❽", "❾", "❿", "⓫",
              "⓬", "⓭", "⓮", "⓯",
              "⓰", "⓱", "⓲", "⓳",
              "⓴",
            };
            os << tab[v];
          }
        else
          {
            os << v;
          }
291
292
      }

293
294
295
      void
      output_set(acc_cond::mark_t a) const
      {
296
297
298
299
300
301
302
303
304
305
306
307
        if (!opt_all_bullets)
          os_ << '{';
        const char* space = "";
        for (auto v: a.sets())
          {
            if (!opt_all_bullets)
              os_ << space;
            output_set(os_, v);
            space = ",";
          }
        if (!opt_all_bullets)
          os_ << '}';
308
      }
309
310
311
312

      const char*
      html_set_color(int v) const
      {
313
314
315
316
317
318
319
320
321
322
323
324
325
326
        if (opt_rainbow)
          return palette[(v + opt_shift_sets_) % palette_mod];
        // Color according to Fin/Inf
        if (inf_sets_.has(v))
          {
            if (fin_sets_.has(v))
              return palette[2];
            else
              return palette[0];
          }
        else
          {
            return palette[1];
          }
327
328
329
330
331
      }

      void
      output_html_set_aux(std::ostream& os, int v) const
      {
332
333
334
        os << "<font color=\"" << html_set_color(v) << "\">";
        output_set(os, v);
        os << "</font>";
335
336
337
338
339
      }

      void
      output_html_set(int v) const
      {
340
        output_html_set_aux(os_, v);
341
342
      }

343
344
345
      void
      output_html_set(acc_cond::mark_t a) const
      {
346
347
348
349
350
351
352
353
354
355
356
357
        if (!opt_all_bullets)
          os_ << '{';
        const char* space = "";
        for (auto v: a.sets())
          {
            if (!opt_all_bullets)
              os_ << space;
            output_html_set(v);
            space = ",";
          }
        if (!opt_all_bullets)
          os_ << '}';
358
359
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
360
361
362
      std::string
      state_label(unsigned s) const
      {
363
364
365
366
367
368
369
370
371
372
        bdd label = bddfalse;
        for (auto& t: aut_->out(s))
          {
            label = t.cond;
            break;
          }
        if (label == bddfalse
            && incomplete_ && incomplete_->find(s) != incomplete_->end())
          return "...";
        return bdd_format_formula(aut_->get_dict(), label);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
373
374
      }

375
      std::string string_dst(int dst, int color_num = -1)
376
      {
377
378
        std::ostringstream tmp_dst;
        tmp_dst << dst;
379
        if (!opt_shared_univ_dest_ && color_num >= 0)
380
          tmp_dst << '.' << color_num;
381
382
383
384
385
386
387
388
389
390
391
392
393
394
        return tmp_dst.str();
      }

      void
      print_dst(int dst, bool print_edges = false,
                const char* style = nullptr, int color_num = -1)
      {
        int& univ = univ_done[std::make_pair(dst, color_num)];
        if (univ == 2)
          return;
        std::string dest = string_dst(dst, color_num);
        if (univ == 0)
          os_ << "  " << dest << " [label=<>,shape=point]\n";
        if (print_edges)
395
          {
396
397
398
399
400
401
402
403
404
405
406
407
            for (unsigned d: aut_->univ_dests(dst))
              {
                os_ << "  " << dest << " -> " << d;
                if (style && *style)
                  os_ << " [" << style << ']';
                os_ << '\n';
              }
            univ = 2;
          }
        else
          {
            univ = 1;
408
409
410
          }
      }

411
412
413
      void
      start()
      {
414
415
416
417
418
419
        if (opt_html_labels_)
          std::tie(inf_sets_, fin_sets_) =
            aut_->get_acceptance().used_inf_fin_sets();
        if (opt_bullet && aut_->num_sets() <= MAX_BULLET)
          opt_all_bullets = true;
        os_ << "digraph G {\n";
420
        if (!opt_vertical_)
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
          os_ << "  rankdir=LR\n";
        if (name_ || opt_show_acc_)
          {
            if (!opt_html_labels_)
              {
                os_ << "  label=\"";
                if (name_)
                  {
                    escape_str(os_, *name_);
                    if (opt_show_acc_)
                      os_ << "\\n";
                  }
                if (opt_show_acc_)
                  aut_->get_acceptance().to_text
                    (os_, [this](std::ostream& os, int v)
                     {
                       this->output_set(os, v);
                     });
                os_ << "\"\n";
              }
            else
              {
                os_ << "  label=<";
                if (name_)
                  {
                    escape_html(os_, *name_);
                    if (opt_show_acc_)
                      os_ << "<br/>";
                  }
                if (opt_show_acc_)
                  aut_->get_acceptance().to_html
                    (os_, [this](std::ostream& os, int v)
                     {
                       this->output_html_set_aux(os, v);
                     });
                os_ << ">\n";
              }
            os_ << "  labelloc=\"t\"\n";
          }
        switch (opt_shape_)
          {
          case ShapeCircle:
            os_ << "  node [shape=\"circle\"]\n";
            break;
          case ShapeEllipse:
            // Do not print anything.  Ellipse is
            // the default shape used by GraphViz.
            break;
          case ShapeAuto:
            SPOT_UNREACHABLE();
          }
        if (!opt_node_color_.empty())
          os_ << "  node [style=\"filled\", fillcolor=\""
              << opt_node_color_ << "\"]\n";
        if (!opt_font_.empty())
          os_ << "  fontname=\"" << opt_font_
              << "\"\n  node [fontname=\"" << opt_font_
              << "\"]\n  edge [fontname=\"" << opt_font_
              << "\"]\n";
        // Always copy the environment variable into a static string,
        // so that we (1) look it up once, but (2) won't crash if the
        // environment is changed.
        static std::string extra = []()
          {
            auto s = getenv("SPOT_DOTEXTRA");
            return s ? s : "";
          }();
        // Any extra text passed in the SPOT_DOTEXTRA environment
        // variable should be output at the end of the "header", so
        // that our setup can be overridden.
        if (!extra.empty())
          os_ << "  " << extra << '\n';
        os_ << "  I [label=\"\", style=invis, ";
494
        os_ << (opt_vertical_ ? "height" : "width");
495
496
497
        int init = (int) aut_->get_init_state_number();
        os_ << "=0]\n  I -> " << init;
        if (init >= 0)
498
          os_ << '\n';
499
        else
500
          os_ << " [arrowhead=onormal]\n";
501
502
503
504
505
      }

      void
      end()
      {
506
        os_ << '}' << std::endl;
507
508
509
510
511
      }

      void
      process_state(unsigned s)
      {
512
513
514
515
516
517
518
519
520
521
        if (mark_states_ &&
            ((opt_bullet && !opt_bullet_but_buchi)
             || aut_->num_sets() != 1))
          {
            acc_cond::mark_t acc = 0U;
            for (auto& t: aut_->out(s))
              {
                acc = t.acc;
                break;
              }
522

523
            bool has_name = sn_ && s < sn_->size() && !(*sn_)[s].empty();
524

525
526
527
528
529
530
531
532
533
534
            os_ << "  " << s << " [label=";
            if (!opt_html_labels_)
              {
                os_ << '"';
                if (has_name)
                  escape_str(os_, (*sn_)[s]);
                else if (sprod_)
                  os_ << (*sprod_)[s].first << ',' << (*sprod_)[s].second;
                else
                  os_ << s;
535
536
                if (orig_ && s < orig_->size())
                  os_ << " (" << (*orig_)[s] << ')';
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
                if (acc)
                  {
                    os_ << "\\n";
                    output_set(acc);
                  }
                if (opt_state_labels_)
                  escape_str(os_ << "\\n", state_label(s));
                os_ << '"';
              }
            else
              {
                os_ << '<';
                if (has_name)
                  escape_html(os_, (*sn_)[s]);
                else if (sprod_)
                  os_ << (*sprod_)[s].first << ',' << (*sprod_)[s].second;
                else
                  os_ << s;
555
556
                if (orig_ && s < orig_->size())
                  os_ << " (" << (*orig_)[s] << ')';
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
                if (acc)
                  {
                    os_ << "<br/>";
                    output_html_set(acc);
                  }
                if (opt_state_labels_)
                  escape_html(os_ << "<br/>", state_label(s));
                os_ << '>';
              }
          }
        else
          {
            os_ << "  " << s << " [label=\"";
            if (sn_ && s < sn_->size() && !(*sn_)[s].empty())
              escape_str(os_, (*sn_)[s]);
            else if (sprod_)
              os_ << (*sprod_)[s].first << ',' << (*sprod_)[s].second;
            else
              os_ << s;
576
577
            if (orig_ && s < orig_->size())
              os_ << " (" << (*orig_)[s] << ')';
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            if (opt_state_labels_)
              escape_str(os_ << "\\n", state_label(s));
            os_ << '"';
            // Use state_acc_sets(), not state_is_accepting() because
            // on co-Büchi automata we want to mark the rejecting
            // states.
            if (mark_states_ && aut_->state_acc_sets(s))
              os_ << ", peripheries=2";
          }
        if (highlight_states_)
          {
            auto iter = highlight_states_->find(s);
            if (iter != highlight_states_->end())
              {
                os_ << ", style=\"bold";
                if (!opt_node_color_.empty())
                  os_ << ",filled";
                os_ << "\", color=\"" << palette[iter->second % palette_mod]
                    << '"';
              }
          }
        os_ << "]\n";
        if (incomplete_ && incomplete_->find(s) != incomplete_->end())
          os_ << "  u" << s << " [label=\"...\", shape=none, width=0, height=0"
            "]\n  " << s << " -> u" << s << " [style=dashed]\n";
603
604
605
      }

      void
606
      process_link(const twa_graph::edge_storage_t& t, int number,
607
                   bool print_edges)
608
      {
609
        if (print_edges)
610
          {
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
            os_ << "  " << t.src << " -> " << (int)t.dst;
            if (aut_->is_univ_dest(t.dst) && highlight_edges_
                && !opt_shared_univ_dest_)
              {
                auto idx = aut_->get_graph().index_of_edge(t);
                auto iter = highlight_edges_->find(idx);
                if (iter != highlight_edges_->end())
                  os_ << '.' << iter->second % palette_mod;
              }
            std::string label;
            if (!opt_state_labels_)
              label = bdd_format_formula(aut_->get_dict(), t.cond);
            if (!opt_html_labels_)
              {
                os_ << " [label=\"";
                escape_str(os_, label);
                if (!mark_states_)
                  if (auto a = t.acc)
                    {
                      if (!opt_state_labels_)
                        os_ << "\\n";
                      output_set(a);
                    }
                os_ << '"';
              }
            else
              {
                os_ << " [label=<";
                escape_html(os_, label);
                if (!mark_states_)
                  if (auto a = t.acc)
                    {
                      if (!opt_state_labels_)
                        os_ << "<br/>";
                      output_html_set(a);
                    }
                os_ << '>';
              }
            if (opt_ordered_edges_ || opt_numbered_edges_)
              {
                os_ << ", taillabel=\"";
                if (opt_ordered_edges_)
                  os_ << number;
                if (opt_ordered_edges_ && opt_numbered_edges_)
                  os_ << ' ';
                if (opt_numbered_edges_)
                  os_ << '#' << aut_->get_graph().index_of_edge(t);
                os_ << '"';
              }
660
          }
661
662

        std::string highlight;
663
        int color_num = -1;
664
665
666
667
668
        if (highlight_edges_)
          {
            auto idx = aut_->get_graph().index_of_edge(t);
            auto iter = highlight_edges_->find(idx);
            if (iter != highlight_edges_->end())
669
670
671
              {
                std::ostringstream o;
                o << "style=bold, color=\""
672
673
                  << palette[iter->second % palette_mod]
                  << '"';
674
                highlight = o.str();
675
676
                if (print_edges)
                  os_ << ", " << highlight;
677
678
                if (!opt_shared_univ_dest_)
                  color_num = iter->second % palette_mod;
679
              }
680
          }
681
        if (print_edges)
682
          {
683
684
685
            if (aut_->is_univ_dest(t.dst))
              os_ << ", arrowhead=onormal";
            os_ << "]\n";
686
          }
687
688
        if (aut_->is_univ_dest(t.dst))
          print_dst(t.dst, print_edges, highlight.c_str(), color_num);
689
690
      }

691
      void print(const const_twa_graph_ptr& aut)
692
      {
693
694
695
696
697
698
699
700
701
702
703
704
705
706
        aut_ = aut;
        if (opt_want_state_names_)
          {
            sn_ = aut->get_named_prop<std::vector<std::string>>("state-names");
            // We have no names.  Do we have product sources?
            if (!sn_)
              {
                sprod_ = aut->get_named_prop
                  <std::vector<std::pair<unsigned, unsigned>>>
                  ("product-states");
                if (sprod_ && aut->num_states() != sprod_->size())
                  sprod_ = nullptr;
              }
          }
707
708
        if (opt_orig_show_)
          orig_ = aut->get_named_prop<std::vector<unsigned>>("original-states");
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
        if (opt_state_labels_)
          {
            // Verify that we can use state labels for this automaton.
            unsigned n = aut->num_states();
            for (unsigned s = 0; s < n; ++s)
              {
                bool first = true;
                bdd cond = bddfalse;
                for (auto& t: aut->out(s))
                  if (first)
                    {
                      cond = t.cond;
                      first = false;
                    }
                  else if (t.cond != cond)
                    {
                      opt_state_labels_ = false;
                      break;
                    }
              }
          }
        highlight_edges_ =
          aut->get_named_prop<std::map<unsigned, unsigned>>("highlight-edges");
        highlight_states_ =
          aut->get_named_prop<std::map<unsigned, unsigned>>("highlight-states");
        incomplete_ =
          aut->get_named_prop<std::set<unsigned>>("incomplete-states");
        if (opt_name_)
          name_ = aut_->get_named_prop<std::string>("automaton-name");
        mark_states_ = (!opt_force_acc_trans_
                        && aut_->prop_state_acc().is_true());
        if (opt_shape_ == ShapeAuto)
          {
742
743
            if (sn_ || sprod_ || aut->num_states() > 100
                || opt_state_labels_ || orig_)
744
745
746
              {
                opt_shape_ = ShapeEllipse;
                // If all state names are short, prefer circles.
747
                if (!opt_state_labels_ && !orig_ &&
748
749
750
751
752
753
754
755
756
757
758
                    sn_ && std::all_of(sn_->begin(), sn_->end(),
                                       [](const std::string& s)
                                       { return s.size() <= 2; }))
                  opt_shape_ = ShapeCircle;
              }
            else
              {
                opt_shape_ = ShapeCircle;
              }
          }
        auto si =
759
          std::unique_ptr<scc_info>(opt_scc_ ? new scc_info(aut) : nullptr);
760

761
762
763
        start();
        if (si)
          {
764
            if (aut->is_existential())
765
              si->determine_unknown_acceptance();
766

767
768
769
770
            unsigned sccs = si->scc_count();
            for (unsigned i = 0; i < sccs; ++i)
              {
                os_ << "  subgraph cluster_" << i << " {\n";
771

772
773
774
775
776
777
778
779
780
781
782
783
784
                // Color the SCC to indicate whether is it accepting.
                if (!si->is_useful_scc(i))
                  os_ << "  color=grey\n";
                else if (si->is_trivial(i))
                  os_ << "  color=black\n";
                else if (si->is_accepting_scc(i))
                  os_ << "  color=green\n";
                else if (si->is_rejecting_scc(i))
                  os_ << "  color=red\n";
                else
                  // May only occur if the call to
                  // determine_unknown_acceptance() above is removed.
                  os_ << "  color=orange\n";
785

786
787
788
789
790
791
792
                if (name_ || opt_show_acc_)
                  {
                    // Reset the label, otherwise the graph label would
                    // be inherited by the cluster.
                    os_ << "  label=\"\"\n";
                  }
                for (auto s: si->states_of(i))
793
794
795
                  {
                    process_state(s);
                    int trans_num = 0;
796
                    unsigned scc_of_s = si->scc_of(s);
797
                    for (auto& t : aut_->out(s))
798
799
                      for (unsigned d: aut_->univ_dests(t.dst))
                        if (si->scc_of(d) == scc_of_s)
800
                          {
801
802
                            process_link(t, trans_num++, false);
                            break;
803
804
                          }
                  }
805
806
807
                os_ << "  }\n";
              }
          }
808
809
810
        int init = (int) aut_->get_init_state_number();
        if (init < 0)
          print_dst(init, true);
811
812
813
814
        unsigned ns = aut_->num_states();
        for (unsigned n = 0; n < ns; ++n)
          {
            if (!si || !si->reachable_state(n))
815
816
817
818
              process_state(n);
            int trans_num = 0;
            for (auto& t: aut_->out(n))
              process_link(t, trans_num++, true);
819
820
          }
        end();
821
822
      }
    };
823
  } // anonymous namespace
824
825

  std::ostream&
826
  print_dot(std::ostream& os, const const_twa_ptr& g,
827
                  const char* options)
828
  {
829
    dotty_output d(os, options);
830
    // Enable automatic state labels for Kripke structure.
831
    if (std::dynamic_pointer_cast<const fair_kripke>(g))
832
      d.parse_opts("k");
833
    auto aut = std::dynamic_pointer_cast<const twa_graph>(g);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
834
    if (!aut || (d.max_states_given() && aut->num_states() >= d.max_states()))
835
      aut = make_twa_graph(g, twa::prop_set::all(), true, d.max_states() - 1);
836
    d.print(aut);
837
838
839
840
841
    return os;
  }


}