common_aoutput.cc 12.3 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 = 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
enum {
76
  OPT_LBTT = 1,
77
78
  OPT_NAME,
  OPT_STATS,
79
  OPT_CHECK,
80
};
81
82
83
84

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

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

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

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

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

219
220
221
const struct argp aoutput_o_format_argp = { o_options,
					    nullptr, nullptr, nullptr,
					    nullptr, nullptr, nullptr };
222
223
224
225
226
227
228
229
230

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



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

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

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

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

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

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