dotty.cc 11.4 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 27
#include "dotty.hh"
#include "tgba/bddprint.hh"
28
#include "reachiter.hh"
29
#include "misc/escape.hh"
30
#include "tgba/tgbagraph.hh"
31
#include "tgba/formula2bdd.hh"
32
#include "tgbaalgos/sccinfo.hh"
33
#include <cstdlib>
34 35 36
#include <cstring>
#include <ctype.h>

37 38 39

namespace spot
{
40
  namespace
41
  {
42 43
    constexpr int MAX_BULLET = 20;

44 45 46 47 48
    class dotty_output
    {
      std::ostream& os_;
      bool opt_force_acc_trans_ = false;
      bool opt_horizontal_ = true;
49
      bool opt_name_ = false;
50
      bool opt_circles_ = false;
51
      bool opt_show_acc_ = false;
52
      bool mark_states_ = false;
53
      bool opt_scc_ = false;
54
      bool opt_html_labels_ = false;
55
      const_twa_graph_ptr aut_;
56
      std::vector<std::string>* sn_;
57
      std::string* name_ = nullptr;
58 59 60 61 62
      acc_cond::mark_t inf_sets_ = 0U;
      acc_cond::mark_t fin_sets_ = 0U;
      bool opt_rainbow = false;
      bool opt_bullet = false;
      bool opt_all_bullets = false;
63
      bool opt_numbered_trans = false;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      std::string opt_font_;

      const char* const palette9[9] =
	{
	  "#5DA5DA", /* blue */
	  "#F17CB0", /* pink */
	  "#FAA43A", /* orange */
	  "#B276B2", /* purple */
	  "#60BD68", /* green */
	  "#F15854", /* red */
	  "#B2912F", /* brown */
	  "#4D4D4D", /* gray */
	  "#DECF3F", /* yellow */
	};
      const char*const* palette = palette9;
      int palette_mod = 9;
80 81

    public:
82 83 84

      void
      parse_opts(const char* options)
85
      {
86 87 88 89 90
	const char* orig = options;
	while (char c = *options++)
	  switch (c)
	    {
	    case '.':
91
	      {
92 93 94 95 96 97 98 99 100 101
	        // 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 : "";
		  }();
102
		// Prevent infinite recursions...
103
		if (orig == def.c_str())
104 105
		  throw std::runtime_error
		    (std::string("SPOT_DOTDEFAULT should not contain '.'"));
106 107
		if (!def.empty())
		  parse_opts(def.c_str());
108
		break;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	      }
	    case 'a':
	      opt_show_acc_ = true;
	      break;
	    case 'b':
	      opt_bullet = true;
	      break;
	    case 'c':
	      opt_circles_ = true;
	      break;
	    case 'h':
	      opt_horizontal_ = true;
	      break;
	    case 'f':
	      if (*options != '(')
124
		throw std::runtime_error
125 126 127 128 129 130 131 132
		  (std::string("invalid font specification for dotty()"));
	      {
		auto* end = strchr(++options, ')');
		if (!end)
		  throw std::runtime_error
		    (std::string("invalid font specification for dotty()"));
		opt_font_ = std::string(options, end - options);
		options = end + 1;
133
	      }
134 135 136 137 138 139 140
	      break;
	    case 'n':
	      opt_name_ = true;
	      break;
	    case 'N':
	      opt_name_ = false;
	      break;
141 142 143
	    case 'o':
	      opt_numbered_trans = true;
	      break;
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
	    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':
	      opt_horizontal_ = false;
	      break;
	    case 't':
	      opt_force_acc_trans_ = true;
	      break;
	    default:
	      throw std::runtime_error
		(std::string("unknown option for dotty(): ") + c);
	    }
      }

      dotty_output(std::ostream& os, const char* options)
	: os_(os)
      {
	parse_opts(options ? options : ".");
      }

      void
      output_set(std::ostream& os, int v) const
      {
	if (opt_bullet && (v >= 0) & (v <= MAX_BULLET))
	  {
	    static const char* const tab[MAX_BULLET + 1] = {
	      "⓿", "❶", "❷", "❸",
	      "❹", "❺", "❻", "❼",
	      "❽", "❾", "❿", "⓫",
	      "⓬", "⓭", "⓮", "⓯",
	      "⓰", "⓱", "⓲", "⓳",
	      "⓴",
	    };
	    os << tab[v];
	  }
	else
	  {
	    os << v;
	  }
      }

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      void
      output_set(acc_cond::mark_t a) const
      {
	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_ << '}';
      }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

      const char*
      html_set_color(int v) const
      {
	if (opt_rainbow)
	  return palette[v % 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];
	  }
      }

      void
      output_html_set_aux(std::ostream& os, int v) const
      {
	os << "<font color=\"" << html_set_color(v) << "\">";
	output_set(os, v);
	os << "</font>";
      }

      void
      output_html_set(int v) const
      {
	output_html_set_aux(os_, v);
242 243
      }

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
      void
      output_html_set(acc_cond::mark_t a) const
      {
	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_ << '}';
      }

261 262 263
      void
      start()
      {
264 265 266 267 268 269
	if (opt_html_labels_)
	  std::tie(inf_sets_, fin_sets_) =
	    aut_->get_acceptance().used_inf_fin_sets();
	if (opt_bullet && aut_->acc().num_sets() <= MAX_BULLET)
	  opt_all_bullets = true;

270 271 272
	os_ << "digraph G {\n";
	if (opt_horizontal_)
	  os_ << "  rankdir=LR\n";
273 274
	if (name_ || opt_show_acc_)
	  {
275
	    if (!opt_html_labels_)
276
	      {
277 278 279 280 281 282 283
		os_ << "  label=\"";
		if (name_)
		  {
		    escape_str(os_, *name_);
		    if (opt_show_acc_)
		      os_ << "\\n";
		  }
284
		if (opt_show_acc_)
285 286 287 288 289 290
		  aut_->get_acceptance().to_text
		    (os_, [this](std::ostream& os, int v)
		     {
		       this->output_set(os, v);
		     });
		os_ << "\"\n";
291
	      }
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	    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";
310
	  }
311 312
	if (opt_circles_)
	  os_ << "  node [shape=\"circle\"]\n";
313 314 315 316 317
	if (!opt_font_.empty())
	  os_ << "  fontname=\"" << opt_font_
	      << "\"\n  node [fontname=\"" << opt_font_
	      << "\"]\n  edge [fontname=\"" << opt_font_
	      << "\"]\n";
318 319 320 321 322 323 324 325
	// 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 : "";
	  }();
326 327 328
	// 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.
329
	if (!extra.empty())
330
	  os_ << "  " << extra << '\n';
331 332 333 334 335 336 337 338 339 340 341 342 343 344
	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)
      {
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if (mark_states_ && (opt_bullet || aut_->acc().num_sets() != 1))
	  {
	    acc_cond::mark_t acc = 0U;
	    for (auto& t: aut_->out(s))
	      {
		acc = t.acc;
		break;
	      }

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

	    os_ << "  " << s << " [label=";
	    if (!opt_html_labels_)
	      {
		os_ << '"';
		if (has_name)
		  escape_str(os_, (*sn_)[s]);
		else
		  os_ << s;
		if (acc)
		  {
		    os_ << "\\n";
		    output_set(acc);
		  }
		os_ << '"';
	      }
	    else
	      {
		os_ << '<';
		if (has_name)
		  escape_html(os_, (*sn_)[s]);
		else
		  os_ << s;
		if (acc)
		  {
		    os_ << "<br/>";
		    output_html_set(acc);
		  }
		os_ << '>';
	      }
	    os_ << "]\n";

	  }
388
	else
389 390 391 392 393 394 395 396 397 398 399
	  {
	    os_ << "  " << s << " [label=\"";
	    if (sn_ && s < sn_->size() && !(*sn_)[s].empty())
	      escape_str(os_, (*sn_)[s]);
	    else
	      os_ << s;
	    os_ << '"';
	    if (mark_states_ && aut_->state_is_accepting(s))
	      os_ << ", peripheries=2";
	    os_ << "]\n";
	  }
400 401 402
      }

      void
403
      process_link(const twa_graph::trans_storage_t& t, int number)
404 405
      {
	std::string label = bdd_format_formula(aut_->get_dict(), t.cond);
406 407 408 409 410 411 412 413 414
	os_ << "  " << t.src << " -> " << t.dst;
	if (!opt_html_labels_)
	  {
	    os_ << " [label=\"";
	    escape_str(os_, label);
	    if (!mark_states_)
	      if (auto a = t.acc)
		{
		  os_ << "\\n";
415
		  output_set(a);
416
		}
417
	    os_ << '"';
418 419 420 421 422 423 424 425 426
	  }
	else
	  {
	    os_ << " [label=<";
	    escape_html(os_, label);
	    if (!mark_states_)
	      if (auto a = t.acc)
		{
		  os_ << "<br/>";
427
		  output_html_set(a);
428
		}
429
	    os_ << '>';
430
	  }
431 432 433
	if (opt_numbered_trans)
	  os_ << ",taillabel=\"" << number << '"';
	os_ << "]\n";
434 435
      }

436
      void print(const const_twa_graph_ptr& aut)
437 438
      {
	aut_ = aut;
439
	sn_ = aut->get_named_prop<std::vector<std::string>>("state-names");
440 441
	if (opt_name_)
	  name_ = aut_->get_named_prop<std::string>("automaton-name");
442
	mark_states_ = !opt_force_acc_trans_ && aut_->has_state_based_acc();
443 444
	auto si =
	  std::unique_ptr<scc_info>(opt_scc_ ? new scc_info(aut) : nullptr);
445
	start();
446 447 448 449 450 451
	if (si)
	  {
	    unsigned sccs = si->scc_count();
	    for (unsigned i = 0; i < sccs; ++i)
	      {
		os_ << "  subgraph cluster_" << i << " {\n";
452 453 454 455 456 457 458 459 460 461 462 463 464

		// 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
		  os_ << "  color=orange\n";

465
		if (name_ || opt_show_acc_)
466 467 468 469 470
		  {
		    // Reset the label, otherwise the graph label would
		    // be inherited by the cluster.
		    os_ << "  label=\"\"\n";
		  }
471 472 473 474 475
		for (auto s: si->states_of(i))
		  process_state(s);
		os_ << "  }\n";
	      }
	  }
476 477 478
	unsigned ns = aut_->num_states();
	for (unsigned n = 0; n < ns; ++n)
	  {
479
	    if (!si || !si->reachable_state(n))
480
	      process_state(n);
481
	    int trans_num = 0;
482
	    for (auto& t: aut_->out(n))
483
	      process_link(t, trans_num++);
484 485 486 487
	  }
	end();
      }
    };
488
  } // anonymous namespace
489 490

  std::ostream&
491
  dotty_reachable(std::ostream& os, const const_twa_ptr& g,
492
		  const char* options)
493
  {
494
    dotty_output d(os, options);
495
    auto aut = std::dynamic_pointer_cast<const twa_graph>(g);
496
    if (!aut)
497
      aut = make_twa_graph(g, twa::prop_set::all());
498
    d.print(aut);
499 500 501 502 503
    return os;
  }


}