common_aoutput.cc 12.7 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2012, 2013, 2014, 2015, 2016 Laboratoire de Recherche
// et Développement de l'Epita (LRDE).
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 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
// the Free Software Foundation; either version 3 of the License, or
// (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
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

20
21
#include "common_sys.hh"
#include "error.h"
22
#include "argmatch.h"
23
24
25
26
27

#include "common_aoutput.hh"
#include "common_post.hh"
#include "common_cout.hh"

28
#include <spot/twa/bddprint.hh>
29

30
31
32
33
34
35
36
#include <spot/twaalgos/dot.hh>
#include <spot/twaalgos/lbtt.hh>
#include <spot/twaalgos/hoa.hh>
#include <spot/twaalgos/neverclaim.hh>
#include <spot/twaalgos/stutter.hh>
#include <spot/twaalgos/isunamb.hh>
#include <spot/twaalgos/strength.hh>
37

38
automaton_format_t automaton_format = Hoa;
39
static const char* automaton_format_opt = nullptr;
40
const char* opt_name = nullptr;
41
static const char* opt_output = nullptr;
42
static const char* stats = "";
43
44
enum check_type
  {
45
46
    check_unambiguous = (1 << 0),
    check_stutter = (1 << 1),
47
    check_strength = (1 << 2),
48
49
50
51
    check_all = -1U,
  };
static char const *const check_args[] =
  {
52
    "unambiguous",
53
54
55
    "stutter-invariant", "stuttering-invariant",
    "stutter-insensitive", "stuttering-insensitive",
    "stutter-sensitive", "stuttering-sensitive",
56
    "strength", "weak", "terminal",
57
    "all",
58
    nullptr
59
60
61
  };
static check_type const check_types[] =
  {
62
    check_unambiguous,
63
64
65
    check_stutter, check_stutter,
    check_stutter, check_stutter,
    check_stutter, check_stutter,
66
    check_strength, check_strength, check_strength,
67
68
69
70
    check_all
  };
ARGMATCH_VERIFY(check_args, check_types);
unsigned opt_check = 0U;
71

72
enum {
73
  OPT_LBTT = 1,
74
75
  OPT_NAME,
  OPT_STATS,
76
  OPT_CHECK,
77
};
78
79
80
81

static const argp_option options[] =
  {
    /**************************************************/
82
    { nullptr, 0, nullptr, 0, "Output format:", 3 },
83
    { "dot", 'd', "1|a|b|B|c|e|f(FONT)|h|n|N|o|r|R|s|t|v|+INT",
84
      OPTION_ARG_OPTIONAL,
85
      "GraphViz's format.  Add letters for "
86
87
      "(1) force numbered states, "
      "(a) acceptance display, (b) acceptance sets as bullets, "
88
      "(B) bullets except for Büchi/co-Büchi automata, "
89
90
      "(c) force circular nodes, (e) force elliptic nodes, "
      "(f(FONT)) use FONT, (h) horizontal layout, "
91
      "(v) vertical layout, (n) with name, (N) without name, "
92
93
94
      "(o) ordered transitions, "
      "(r) rainbow colors for acceptance sets, "
      "(R) color acceptance sets by Inf/Fin, (s) with SCCs, "
95
96
      "(t) force transition-based acceptance, "
      "(+INT) add INT to all set numbers", 0 },
97
    { "hoaf", 'H', "i|l|m|s|t|v", OPTION_ARG_OPTIONAL,
98
      "Output the automaton in HOA format (default).  Add letters to select "
99
      "(i) use implicit labels for complete deterministic automata, "
100
101
102
      "(s) prefer state-based acceptance when possible [default], "
      "(t) force transition-based acceptance, "
      "(m) mix state and transition-based acceptance, "
103
      "(k) use state labels when possible, "
104
105
      "(l) single-line output, "
      "(v) verbose properties", 0 },
106
107
108
    { "lbtt", OPT_LBTT, "t", OPTION_ARG_OPTIONAL,
      "LBTT's format (add =t to force transition-based acceptance even"
      " on Büchi automata)", 0 },
109
110
    { "name", OPT_NAME, "FORMAT", 0,
      "set the name of the output automaton", 0 },
111
112
113
114
    { "output", 'o', "FORMAT", 0,
      "send output to a file named FORMAT instead of standard output.  The"
      " first automaton sent to a file truncates it unless FORMAT starts"
      " with '>>'.", 0 },
115
    { "quiet", 'q', nullptr, 0, "suppress all normal output", 0 },
116
117
118
    { "spin", 's', "6|c", OPTION_ARG_OPTIONAL, "Spin neverclaim (implies --ba)."
      "  Add letters to select (6) Spin's 6.2.4 style, (c) comments on states",
      0 },
119
    { "utf8", '8', nullptr, 0, "enable UTF-8 characters in output "
120
121
122
      "(ignored with --lbtt or --spin)", 0 },
    { "stats", OPT_STATS, "FORMAT", 0,
      "output statistics about the automaton", 0 },
123
124
125
    { "check", OPT_CHECK, "PROP", OPTION_ARG_OPTIONAL,
      "test for the additional property PROP and output the result "
      "in the HOA format (implies -H).  PROP may be any prefix of "
126
127
      "'all' (default), 'unambiguous', 'stutter-invariant', or 'strength'.",
      0 },
128
    { nullptr, 0, nullptr, 0, nullptr, 0 }
129
130
  };

131
132
const struct argp aoutput_argp = { options, parse_opt_aoutput, nullptr, nullptr,
				   nullptr, nullptr, nullptr };
133
134
135
136
137
138
139
140
141

// Those can be overridden by individual tools. E.g. randaut has no
// notion of input file, so %F and %L represent something else.
char F_doc[32] = "name of the input file";
char L_doc[32] = "location in the input file";

static const argp_option io_options[] =
  {
    /**************************************************/
142
    { nullptr, 0, nullptr, 0, "Any FORMAT string may use "\
143
144
      "the following interpreted sequences (capitals for input,"
      " minuscules for output):", 4 },
145
146
147
    { "%F", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE, F_doc, 0 },
    { "%L", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE, L_doc, 0 },
    { "%M, %m", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
148
      "name of the automaton", 0 },
149
150
151
152
153
    { "%S, %s", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of states", 0 },
    { "%E, %e", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of edges", 0 },
    { "%T, %t", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
154
      "number of transitions", 0 },
155
    { "%A, %a", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
156
      "number of acceptance sets", 0 },
157
    { "%G, %g", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
158
      "acceptance condition (in HOA syntax)", 0 },
159
160
161
    { "%C, %c", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of SCCs", 0 },
    { "%n", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
162
      "number of nondeterministic states in output", 0 },
163
    { "%d", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
164
      "1 if the output is deterministic, 0 otherwise", 0 },
165
    { "%p", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
166
      "1 if the output is complete, 0 otherwise", 0 },
167
    { "%r", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
168
      "processing time (excluding parsing) in seconds", 0 },
169
    { "%w", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
170
      "one word accepted by the output automaton", 0 },
171
172
173
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "a single %", 0 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
174
175
  };

176
177
178
const struct argp aoutput_io_format_argp = { io_options, nullptr, nullptr,
					     nullptr, nullptr,
					     nullptr, nullptr };
179
180
181
182

static const argp_option o_options[] =
  {
    /**************************************************/
183
    { nullptr, 0, nullptr, 0, "Any FORMAT string may use "\
184
      "the following interpreted sequences:", 4 },
185
186
187
188
189
190
191
192
193
194
195
    { "%F", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE, F_doc, 0 },
    { "%L", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE, L_doc, 0 },
    { "%m", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "name of the automaton", 0 },
    { "%s", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of states", 0 },
    { "%e", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of edges", 0 },
    { "%t", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of transitions", 0 },
    { "%a", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
196
      "number of acceptance sets", 0 },
197
    { "%g", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
198
      "acceptance condition (in HOA syntax)", 0 },
199
200
201
    { "%c", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of SCCs", 0 },
    { "%n", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
202
      "number of nondeterministic states in output", 0 },
203
    { "%d", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
204
      "1 if the output is deterministic, 0 otherwise", 0 },
205
    { "%p", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
206
      "1 if the output is complete, 0 otherwise", 0 },
207
    { "%r", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
208
      "processing time (excluding parsing) in seconds", 0 },
209
    { "%w", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
210
      "one word accepted by the output automaton", 0 },
211
212
213
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "a single %", 0 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
214
215
  };

216
217
218
const struct argp aoutput_o_format_argp = { o_options,
					    nullptr, nullptr, nullptr,
					    nullptr, nullptr, nullptr };
219
220
221
222
223
224
225
226
227

int parse_opt_aoutput(int key, char* arg, struct argp_state*)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case '8':
      spot::enable_utf8();
      break;
228
229
    case 'd':
      automaton_format = Dot;
230
      automaton_format_opt = arg;
231
      break;
232
233
    case 'H':
      automaton_format = Hoa;
234
      automaton_format_opt = arg;
235
      break;
236
237
238
    case 'o':
      opt_output = arg;
      break;
239
240
241
242
243
244
245
    case 'q':
      automaton_format = Quiet;
      break;
    case 's':
      automaton_format = Spin;
      if (type != spot::postprocessor::Monitor)
	type = spot::postprocessor::BA;
246
      automaton_format_opt = arg;
247
      break;
248
249
250
251
252
253
254
    case OPT_CHECK:
      automaton_format = Hoa;
      if (arg)
	opt_check |= XARGMATCH("--check", arg, check_args, check_types);
      else
	opt_check |= check_all;
      break;
255
    case OPT_LBTT:
256
      automaton_format = Lbtt;
257
      automaton_format_opt = arg;
258
259
260
261
262
      // This test could be removed when more options are added,
      // because print_lbtt will raise an exception anyway.  The
      // error message is slightly better in the current way.
      if (arg && (arg[0] != 't' || arg[1] != 0))
	error(2, 0, "unknown argument for --lbtt: '%s'", arg);
263
264
      break;
    case OPT_NAME:
265
      opt_name = arg;
266
267
268
269
270
271
272
273
274
275
276
277
278
      break;
    case OPT_STATS:
      if (!*arg)
	error(2, 0, "empty format string for --stats");
      stats = arg;
      automaton_format = Stats;
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
void setup_default_output_format()
{
  if (auto val = getenv("SPOT_DEFAULT_FORMAT"))
    {
      static char const *const args[] =
	{
	  "dot", "hoa", "hoaf", nullptr
	};
      static automaton_format_t const format[] =
	{
	  Dot, Hoa, Hoa
	};
      auto eq = strchr(val, '=');
      if (eq)
	{
	  val = strndup(val, eq - val);
	  automaton_format_opt = eq + 1;
	}
      ARGMATCH_VERIFY(args, format);
      automaton_format = XARGMATCH("SPOT_DEFAULT_FORMAT", val, args, format);
      if (eq)
	free(val);
    }
}
303

304
305
automaton_printer::automaton_printer(stat_style input)
  : statistics(std::cout, stats, input),
306
307
    namer(name, opt_name, input),
    outputnamer(outputname, opt_output, input)
308
{
309
310
311
  if (automaton_format == Count && opt_output)
    throw std::runtime_error
      ("options --output and --count are incompatible");
312
}
313
314

void
315
automaton_printer::print(const spot::twa_graph_ptr& aut,
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
316
			 spot::formula f,
317
318
			 // Input location for errors and statistics.
			 const char* filename,
319
			 int loc,
320
321
			 // Time and input automaton for statistics
			 double time,
322
			 const spot::const_parsed_aut_ptr& haut)
323
{
324
325
326
  if (opt_check)
    {
      if (opt_check & check_stutter)
327
	spot::check_stutter_invariance(aut, f);
328
329
      if (opt_check & check_unambiguous)
	spot::check_unambiguous(aut);
330
331
      if (opt_check & check_strength)
	spot::check_strength(aut);
332
333
    }

334
335
336
337
  // Name the output automaton.
  if (opt_name)
    {
      name.str("");
338
      namer.print(haut, aut, f, filename, loc, time);
339
340
341
      aut->set_named_prop("automaton-name", new std::string(name.str()));
    }

342
343
344
345
346
347
348
349
350
351
352
353
  std::ostream* out = &std::cout;
  if (opt_output)
    {
      outputname.str("");
      outputnamer.print(haut, aut, f, filename, loc, time);
      std::string fname = outputname.str();
      auto p = outputfiles.emplace(fname, nullptr);
      if (p.second)
	p.first->second.reset(new output_file(fname.c_str()));
      out = &p.first->second->ostream();
    }

354
355
356
357
358
359
360
361
  // Output it.
  switch (automaton_format)
    {
    case Count:
    case Quiet:
      // Do not output anything.
      break;
    case Dot:
362
      spot::print_dot(*out, aut, automaton_format_opt);
363
364
      break;
    case Lbtt:
365
      spot::print_lbtt(*out, aut, automaton_format_opt);
366
367
      break;
    case Hoa:
368
      spot::print_hoa(*out, aut, automaton_format_opt) << '\n';
369
370
      break;
    case Spin:
371
      spot::print_never_claim(*out, aut, automaton_format_opt);
372
373
      break;
    case Stats:
374
      statistics.set_output(*out);
375
      statistics.print(haut, aut, f, filename, loc, time) << '\n';
376
377
378
379
      break;
    }
  flush_cout();
}
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
380
381
382
383
384

void automaton_printer::add_stat(char c, const spot::printable* p)
{
  namer.declare(c, p);
  statistics.declare(c, p);
385
  outputnamer.declare(c, p);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
386
}