common_aoutput.cc 12.3 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2012, 2013, 2014, 2015 Laboratoire de Recherche et
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Développement de l'Epita (LRDE).
//
// 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 = Dot;
39
static const char* opt_dot = nullptr;
40
static const char* opt_never = nullptr;
41
static const char* hoa_opt = nullptr;
42
static const char* opt_lbtt = nullptr;
43
const char* opt_name = nullptr;
44
static const char* opt_output = nullptr;
45
static const char* stats = "";
46
47
enum check_type
  {
48
49
    check_unambiguous = (1 << 0),
    check_stutter = (1 << 1),
50
    check_strength = (1 << 2),
51
52
53
54
    check_all = -1U,
  };
static char const *const check_args[] =
  {
55
    "unambiguous",
56
57
58
    "stutter-invariant", "stuttering-invariant",
    "stutter-insensitive", "stuttering-insensitive",
    "stutter-sensitive", "stuttering-sensitive",
59
    "strength", "weak", "terminal",
60
    "all",
61
    nullptr
62
63
64
  };
static check_type const check_types[] =
  {
65
    check_unambiguous,
66
67
68
    check_stutter, check_stutter,
    check_stutter, check_stutter,
    check_stutter, check_stutter,
69
    check_strength, check_strength, check_strength,
70
71
72
73
    check_all
  };
ARGMATCH_VERIFY(check_args, check_types);
unsigned opt_check = 0U;
74

75
76
77
78
79
enum {
  OPT_DOT = 1,
  OPT_LBTT,
  OPT_NAME,
  OPT_STATS,
80
  OPT_CHECK,
81
};
82
83
84
85

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

135
136
const struct argp aoutput_argp = { options, parse_opt_aoutput, nullptr, nullptr,
				   nullptr, nullptr, nullptr };
137
138
139
140
141
142
143
144
145

// 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[] =
  {
    /**************************************************/
146
    { nullptr, 0, nullptr, 0, "Any FORMAT string may use "\
147
148
      "the following interpreted sequences (capitals for input,"
      " minuscules for output):", 4 },
149
150
151
    { "%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,
152
      "name of the automaton", 0 },
153
154
155
156
157
    { "%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,
158
      "number of transitions", 0 },
159
    { "%A, %a", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
160
      "number of acceptance sets", 0 },
161
    { "%G, %g", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
162
      "acceptance condition (in HOA syntax)", 0 },
163
164
165
    { "%C, %c", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of SCCs", 0 },
    { "%n", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
166
      "number of nondeterministic states in output", 0 },
167
    { "%d", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
168
      "1 if the output is deterministic, 0 otherwise", 0 },
169
    { "%p", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
170
      "1 if the output is complete, 0 otherwise", 0 },
171
    { "%r", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
172
      "processing time (excluding parsing) in seconds", 0 },
173
    { "%w", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
174
      "one word accepted by the output automaton", 0 },
175
176
177
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "a single %", 0 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
178
179
  };

180
181
182
const struct argp aoutput_io_format_argp = { io_options, nullptr, nullptr,
					     nullptr, nullptr,
					     nullptr, nullptr };
183
184
185
186

static const argp_option o_options[] =
  {
    /**************************************************/
187
    { nullptr, 0, nullptr, 0, "Any FORMAT string may use "\
188
      "the following interpreted sequences:", 4 },
189
190
191
192
193
194
195
196
197
198
199
    { "%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,
200
      "number of acceptance sets", 0 },
201
    { "%g", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
202
      "acceptance condition (in HOA syntax)", 0 },
203
204
205
    { "%c", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "number of SCCs", 0 },
    { "%n", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
206
      "number of nondeterministic states in output", 0 },
207
    { "%d", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
208
      "1 if the output is deterministic, 0 otherwise", 0 },
209
    { "%p", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
210
      "1 if the output is complete, 0 otherwise", 0 },
211
    { "%r", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
212
      "processing time (excluding parsing) in seconds", 0 },
213
    { "%w", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
214
      "one word accepted by the output automaton", 0 },
215
216
217
    { "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
      "a single %", 0 },
    { nullptr, 0, nullptr, 0, nullptr, 0 }
218
219
  };

220
221
222
const struct argp aoutput_o_format_argp = { o_options,
					    nullptr, nullptr, nullptr,
					    nullptr, nullptr, nullptr };
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

int parse_opt_aoutput(int key, char* arg, struct argp_state*)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case '8':
      spot::enable_utf8();
      break;
    case 'H':
      automaton_format = Hoa;
      hoa_opt = arg;
      break;
    case 'q':
      automaton_format = Quiet;
      break;
    case 's':
      automaton_format = Spin;
      if (type != spot::postprocessor::Monitor)
	type = spot::postprocessor::BA;
243
244
      if (arg)
	opt_never = arg;
245
      break;
246
247
248
    case 'o':
      opt_output = arg;
      break;
249
250
251
252
253
254
255
    case OPT_CHECK:
      automaton_format = Hoa;
      if (arg)
	opt_check |= XARGMATCH("--check", arg, check_args, check_types);
      else
	opt_check |= check_all;
      break;
256
257
258
259
260
    case OPT_DOT:
      automaton_format = Dot;
      opt_dot = arg;
      break;
    case OPT_LBTT:
261
262
263
264
265
266
267
      automaton_format = Lbtt;
      opt_lbtt = arg;
      // 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);
268
269
      break;
    case OPT_NAME:
270
      opt_name = arg;
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
      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;
}



286
287
automaton_printer::automaton_printer(stat_style input)
  : statistics(std::cout, stats, input),
288
289
    namer(name, opt_name, input),
    outputnamer(outputname, opt_output, input)
290
{
291
292
293
  if (automaton_format == Count && opt_output)
    throw std::runtime_error
      ("options --output and --count are incompatible");
294
}
295
296

void
297
automaton_printer::print(const spot::twa_graph_ptr& aut,
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
298
			 spot::formula f,
299
300
			 // Input location for errors and statistics.
			 const char* filename,
301
			 int loc,
302
303
			 // Time and input automaton for statistics
			 double time,
304
			 const spot::const_parsed_aut_ptr& haut)
305
{
306
307
308
  if (opt_check)
    {
      if (opt_check & check_stutter)
309
	spot::check_stutter_invariance(aut, f);
310
311
      if (opt_check & check_unambiguous)
	spot::check_unambiguous(aut);
312
313
      if (opt_check & check_strength)
	spot::check_strength(aut);
314
315
    }

316
317
318
319
  // Name the output automaton.
  if (opt_name)
    {
      name.str("");
320
      namer.print(haut, aut, f, filename, loc, time);
321
322
323
      aut->set_named_prop("automaton-name", new std::string(name.str()));
    }

324
325
326
327
328
329
330
331
332
333
334
335
  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();
    }

336
337
338
339
340
341
342
343
  // Output it.
  switch (automaton_format)
    {
    case Count:
    case Quiet:
      // Do not output anything.
      break;
    case Dot:
344
      spot::print_dot(*out, aut, opt_dot);
345
346
      break;
    case Lbtt:
347
      spot::print_lbtt(*out, aut, opt_lbtt);
348
349
      break;
    case Hoa:
350
      spot::print_hoa(*out, aut, hoa_opt) << '\n';
351
352
      break;
    case Spin:
353
      spot::print_never_claim(*out, aut, opt_never);
354
355
      break;
    case Stats:
356
      statistics.set_output(*out);
357
      statistics.print(haut, aut, f, filename, loc, time) << '\n';
358
359
360
361
      break;
    }
  flush_cout();
}
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
362
363
364
365
366

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