common_aoutput.cc 12.8 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 },
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
83
    { "dot", 'd', "1|a|b|B|c|e|f(FONT)|h|n|N|o|r|R|s|t|v|+INT|<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
      "(t) force transition-based acceptance, "
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
96
97
      "(+INT) add INT to all set numbers, "
      "(<INT) display at most INT states", 0 },
98
    { "hoaf", 'H', "i|l|m|s|t|v", OPTION_ARG_OPTIONAL,
99
      "Output the automaton in HOA format (default).  Add letters to select "
100
      "(i) use implicit labels for complete deterministic automata, "
101
102
103
      "(s) prefer state-based acceptance when possible [default], "
      "(t) force transition-based acceptance, "
      "(m) mix state and transition-based acceptance, "
104
      "(k) use state labels when possible, "
105
106
      "(l) single-line output, "
      "(v) verbose properties", 0 },
107
108
109
    { "lbtt", OPT_LBTT, "t", OPTION_ARG_OPTIONAL,
      "LBTT's format (add =t to force transition-based acceptance even"
      " on Büchi automata)", 0 },
110
111
    { "name", OPT_NAME, "FORMAT", 0,
      "set the name of the output automaton", 0 },
112
113
114
115
    { "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 },
116
    { "quiet", 'q', nullptr, 0, "suppress all normal output", 0 },
117
118
119
    { "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 },
120
    { "utf8", '8', nullptr, 0, "enable UTF-8 characters in output "
121
122
123
      "(ignored with --lbtt or --spin)", 0 },
    { "stats", OPT_STATS, "FORMAT", 0,
      "output statistics about the automaton", 0 },
124
125
126
    { "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 "
127
128
      "'all' (default), 'unambiguous', 'stutter-invariant', or 'strength'.",
      0 },
129
    { nullptr, 0, nullptr, 0, nullptr, 0 }
130
131
  };

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

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

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

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

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

int parse_opt_aoutput(int key, char* arg, struct argp_state*)
{
  // This switch is alphabetically-ordered.
  switch (key)
    {
    case '8':
      spot::enable_utf8();
      break;
229
230
    case 'd':
      automaton_format = Dot;
231
      automaton_format_opt = arg;
232
      break;
233
234
    case 'H':
      automaton_format = Hoa;
235
      automaton_format_opt = arg;
236
      break;
237
238
239
    case 'o':
      opt_output = arg;
      break;
240
241
242
243
244
245
246
    case 'q':
      automaton_format = Quiet;
      break;
    case 's':
      automaton_format = Spin;
      if (type != spot::postprocessor::Monitor)
	type = spot::postprocessor::BA;
247
      automaton_format_opt = arg;
248
      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
    case OPT_LBTT:
257
      automaton_format = Lbtt;
258
      automaton_format_opt = arg;
259
260
261
262
263
      // 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);
264
265
      break;
    case OPT_NAME:
266
      opt_name = arg;
267
268
269
270
271
272
273
274
275
276
277
278
279
      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;
}

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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);
    }
}
304

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

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

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

343
344
345
346
347
348
349
350
351
352
353
354
  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();
    }

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

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