ltl2tgba.cc 11.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright (C) 2003, 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// 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
// the Free Software Foundation; either version 2 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 Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

22
23
#include <iostream>
#include <cassert>
24
25
#include <fstream>
#include <string>
26
27
28
#include "ltlvisit/destroy.hh"
#include "ltlast/allnodes.hh"
#include "ltlparse/public.hh"
29
#include "tgbaalgos/ltl2tgba_lacim.hh"
30
#include "tgbaalgos/ltl2tgba_fm.hh"
31
#include "tgba/bddprint.hh"
32
#include "tgbaalgos/dotty.hh"
33
#include "tgbaalgos/lbtt.hh"
34
#include "tgba/tgbatba.hh"
35
#include "tgbaalgos/magic.hh"
36
37
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/gtec/ce.hh"
38
#include "tgbaparse/public.hh"
39
#include "tgbaalgos/dupexp.hh"
40
#include "tgbaalgos/neverclaim.hh"
41
42
43
44

void
syntax(char* prog)
{
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
45
  std::cerr << "Usage: "<< prog << " [OPTIONS...] formula" << std::endl
46
            << "       "<< prog << " -F [OPTIONS...] file" << std::endl
47
            << "       "<< prog << " -X [OPTIONS...] file" << std::endl
Alexandre Duret-Lutz's avatar
spacing    
Alexandre Duret-Lutz committed
48
	    << std::endl
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
49
	    << "Options:" << std::endl
50
	    << "  -a   display the acceptance_conditions BDD, not the "
51
	    << "reachability graph"
52
	    << std::endl
53
	    << "  -A   same as -a, but as a set" << std::endl
54
	    << "  -d   turn on traces during parsing" << std::endl
55
	    << "  -D   degeneralize the automaton" << std::endl
56
57
	    << "  -e   emptiness-check (Couvreur), expect and compute "
	    << "a counter-example" << std::endl
58
59
	    << "  -e2  emptiness-check (Couvreur variant), expect and compute "
	    << "a counter-example" << std::endl
60
61
	    << "  -E   emptiness-check (Couvreur), expect no counter-example "
	    << std::endl
62
63
	    << "  -E2  emptiness-check (Couvreur variant), expect no "
	    << "counter-example " << std::endl
64
65
66
            << "  -f   use Couvreur's FM algorithm for translation"
	    << std::endl
            << "  -F   read the formula from the file" << std::endl
67
            << "  -L   fair-loop approximation (implies -f)" << std::endl
68
69
70
71
72
73
	    << "  -m   magic-search (implies -D), expect a counter-example"
	    << std::endl
	    << "  -M   magic-search (implies -D), expect no counter-example"
	    << std::endl
	    << "  -n   same as -m, but display more counter-examples"
	    << std::endl
74
75
	    << "  -N   display the never clain for Spin "
	    << "(implies -D)" << std::endl
76
            << "  -p   branching postponement (implies -f)" << std::endl
77
	    << "  -r   display the relation BDD, not the reachability graph"
78
	    << std::endl
79
	    << "  -R   same as -r, but as a set" << std::endl
80
81
82
83
	    << "  -s   convert to explicit automata, and number states "
	    << "in DFS order" << std::endl
	    << "  -S   convert to explicit automata, and number states "
	    << "in BFS order" << std::endl
84
	    << "  -t   display reachable states in LBTT's format" << std::endl
85
86
	    << "  -T   display reachable states in LBTT's format w/o "
	    << "acceptance conditions" << std::endl
Alexandre Duret-Lutz's avatar
spacing    
Alexandre Duret-Lutz committed
87
	    << "  -v   display the BDD variables used by the automaton"
88
	    << std::endl
89
90
            << "  -x   try to produce a more deterministic automata "
	    << "(implies -f)" << std::endl
91
	    << "  -X   do not compute an automaton, read it from a file"
92
93
	    << std::endl
	    << "  -y   do not merge states with same symbolic representation "
martinez's avatar
martinez committed
94
	    << "(implies -f)" << std::endl;
95
96
97
98
99
100
101
102
  exit(2);
}

int
main(int argc, char** argv)
{
  int exit_code = 0;

103
  bool debug_opt = false;
104
  bool degeneralize_opt = false;
105
  bool fm_opt = false;
106
  bool fm_exprop_opt = false;
107
  bool fm_symb_merge_opt = true;
108
  bool file_opt = false;
109
  int output = 0;
110
  int formula_index = 0;
111
  enum { None, Couvreur, Couvreur2, MagicSearch } echeck = None;
112
  enum { NoneDup, BFS, DFS } dupexp = NoneDup;
113
114
  bool magic_many = false;
  bool expect_counter_example = false;
115
  bool from_file = false;
116
  bool post_branching = false;
117
  bool fair_loop_approx = false;
118

119
  for (;;)
120
    {
121
      if (argc < formula_index + 2)
122
	syntax(argv[0]);
123
124
125

      ++formula_index;

126
127
128
129
130
131
132
133
134
      if (!strcmp(argv[formula_index], "-a"))
	{
	  output = 2;
	}
      else if (!strcmp(argv[formula_index], "-A"))
	{
	  output = 4;
	}
      else if (!strcmp(argv[formula_index], "-d"))
135
136
137
	{
	  debug_opt = true;
	}
138
139
140
141
      else if (!strcmp(argv[formula_index], "-D"))
	{
	  degeneralize_opt = true;
	}
142
143
144
145
146
147
      else if (!strcmp(argv[formula_index], "-e"))
	{
	  echeck = Couvreur;
	  expect_counter_example = true;
	  output = -1;
	}
148
149
150
151
152
153
      else if (!strcmp(argv[formula_index], "-e2"))
	{
	  echeck = Couvreur2;
	  expect_counter_example = true;
	  output = -1;
	}
154
155
156
157
158
159
      else if (!strcmp(argv[formula_index], "-E"))
	{
	  echeck = Couvreur;
	  expect_counter_example = false;
	  output = -1;
	}
160
161
162
163
164
165
      else if (!strcmp(argv[formula_index], "-E2"))
	{
	  echeck = Couvreur2;
	  expect_counter_example = false;
	  output = -1;
	}
166
167
168
169
170
171
172
173
      else if (!strcmp(argv[formula_index], "-f"))
	{
	  fm_opt = true;
	}
      else if (!strcmp(argv[formula_index], "-F"))
	{
	  file_opt = true;
	}
174
175
176
177
178
      else if (!strcmp(argv[formula_index], "-L"))
	{
	  fair_loop_approx = true;
	  fm_opt = true;
	}
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
      else if (!strcmp(argv[formula_index], "-m"))
	{
	  echeck = MagicSearch;
	  degeneralize_opt = true;
	  expect_counter_example = true;
	  output = -1;
	}
      else if (!strcmp(argv[formula_index], "-M"))
	{
	  echeck = MagicSearch;
	  degeneralize_opt = true;
	  expect_counter_example = false;
	  output = -1;
	}
      else if (!strcmp(argv[formula_index], "-n"))
	{
	  echeck = MagicSearch;
	  degeneralize_opt = true;
	  expect_counter_example = true;
	  output = -1;
	  magic_many = true;
	}
201
202
203
204
205
      else if (!strcmp(argv[formula_index], "-N"))
	{
	  degeneralize_opt = true;
	  output = 8;
	}
206
207
208
209
210
      else if (!strcmp(argv[formula_index], "-p"))
	{
	  post_branching = true;
	  fm_opt = true;
	}
211
212
      else if (!strcmp(argv[formula_index], "-r"))
	{
213
214
215
216
217
	  output = 1;
	}
      else if (!strcmp(argv[formula_index], "-R"))
	{
	  output = 3;
218
	}
219
220
221
222
223
224
225
226
      else if (!strcmp(argv[formula_index], "-s"))
	{
	  dupexp = DFS;
	}
      else if (!strcmp(argv[formula_index], "-S"))
	{
	  dupexp = BFS;
	}
227
228
229
230
      else if (!strcmp(argv[formula_index], "-t"))
	{
	  output = 6;
	}
231
232
233
234
      else if (!strcmp(argv[formula_index], "-T"))
	{
	  output = 7;
	}
235
236
237
238
      else if (!strcmp(argv[formula_index], "-v"))
	{
	  output = 5;
	}
239
240
241
242
243
      else if (!strcmp(argv[formula_index], "-x"))
	{
	  fm_opt = true;
	  fm_exprop_opt = true;
	}
244
245
246
247
      else if (!strcmp(argv[formula_index], "-X"))
	{
	  from_file = true;
	}
248
249
250
251
252
      else if (!strcmp(argv[formula_index], "-y"))
	{
	  fm_opt = true;
	  fm_symb_merge_opt = false;
	}
253
254
255
256
      else
	{
	  break;
	}
257
258
    }

259
260
261
262
  std::string input;

  if (file_opt)
    {
263
      if (strcmp(argv[formula_index], "-"))
264
	{
265
266
267
268
269
270
	  std::ifstream fin(argv[formula_index]);
	  if (! fin)
	    {
	      std::cerr << "Cannot open " << argv[formula_index] << std::endl;
	      exit(2);
	    }
271

272
273
274
275
276
277
278
	  if (! std::getline(fin, input, '\0'))
	    {
	      std::cerr << "Cannot read " << argv[formula_index] << std::endl;
	      exit(2);
	    }
	}
      else
279
	{
280
	  std::getline(std::cin, input, '\0');
281
282
283
284
285
286
287
	}
    }
  else
    {
      input = argv[formula_index];
    }

288
  spot::ltl::environment& env(spot::ltl::default_environment::instance());
289
  spot::bdd_dict* dict = new spot::bdd_dict();
290
291
292
293
294
295
296
297
298

  spot::ltl::formula* f = 0;
  if (!from_file)
    {
      spot::ltl::parse_error_list pel;
      f = spot::ltl::parse(input, pel, env, debug_opt);
      exit_code = spot::ltl::format_parse_errors(std::cerr, input, pel);
    }
  if (f || from_file)
299
    {
300
301
302
303
      spot::tgba_bdd_concrete* concrete = 0;
      spot::tgba* to_free = 0;
      spot::tgba* a = 0;

304
305
306
      if (from_file)
	{
	  spot::tgba_parse_error_list pel;
307
308
	  spot::tgba_explicit* e;
	  to_free = a = e = spot::tgba_parse(input, pel, dict, env, debug_opt);
309
310
	  if (spot::format_tgba_parse_errors(std::cerr, pel))
	    return 2;
311
	  e->merge_transitions();
312
	}
313
      else
314
315
	{
	  if (fm_opt)
316
	    to_free = a = spot::ltl_to_tgba_fm(f, dict, fm_exprop_opt,
317
					       fm_symb_merge_opt,
318
319
					       post_branching,
					       fair_loop_approx);
320
321
322
	  else
	    to_free = a = concrete = spot::ltl_to_tgba_lacim(f, dict);
	}
323

324
      spot::tgba_tba_proxy* degeneralized = 0;
325
326
327
      if (degeneralize_opt)
	a = degeneralized = new spot::tgba_tba_proxy(a);

328
329
330
331
332
333
334
335
336
337
338
339
340
      spot::tgba_explicit* expl = 0;
      switch (dupexp)
	{
	case NoneDup:
	  break;
	case BFS:
	  a = expl = tgba_dupexp_bfs(a);
	  break;
	case DFS:
	  a = expl = tgba_dupexp_dfs(a);
	  break;
	}

341
342
      switch (output)
	{
343
344
345
	case -1:
	  /* No output.  */
	  break;
346
347
348
349
	case 0:
	  spot::dotty_reachable(std::cout, a);
	  break;
	case 1:
350
351
352
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
353
354
	  break;
	case 2:
355
356
357
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->
358
				get_core_data().acceptance_conditions);
359
360
	  break;
	case 3:
361
362
363
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
364
365
	  break;
	case 4:
366
367
368
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->
369
				get_core_data().acceptance_conditions);
370
	  break;
371
	case 5:
372
	  a->get_dict()->dump(std::cout);
373
	  break;
374
375
376
	case 6:
	  spot::lbtt_reachable(std::cout, a);
	  break;
377
378
379
	case 7:
	  spot::nonacceptant_lbtt_reachable(std::cout, a);
	  break;
380
381
382
	case 8:
	  spot::never_claim_reachable(std::cout, degeneralized, f);
	  break;
383
384
385
	default:
	  assert(!"unknown output option");
	}
386

387
388
389
390
391
      switch (echeck)
	{
	case None:
	  break;
	case Couvreur:
392
	case Couvreur2:
393
	  {
394
	    spot::emptiness_check* ec;
395
	    if (echeck == Couvreur)
396
	      ec = new spot::emptiness_check(a);
397
	    else
398
399
400
	      ec = new spot::emptiness_check_shy(a);

	    bool res = ec->check();
401

402
403
404
405
406
	    if (expect_counter_example)
	      {
		if (res)
		  {
		    exit_code = 1;
407
		    delete ec;
408
409
		    break;
		  }
410
		spot::counter_example ce(ec->result());
411
		ce.print_result(std::cout);
412
413
414
415
416
	      }
	    else
	      {
		exit_code = !res;
	      }
417
	    delete ec;
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
	  }
	  break;
	case MagicSearch:
	  {
	    spot::magic_search ms(degeneralized);
	    bool res = ms.check();
	    if (expect_counter_example)
	      {
		if (!res)
		  {
		    exit_code = 1;
		    break;
		  }
		do
		  ms.print_result(std::cout);
		while (magic_many && ms.check());
	      }
	    else
	      {
		exit_code = res;
	      }
	  }
	  break;
	}

443
444
      if (f)
        spot::ltl::destroy(f);
445
446
      if (expl)
	delete expl;
447
448
449
      if (degeneralize_opt)
	delete degeneralized;

450
      delete to_free;
451
452
453
454
455
456
457
458
459
460
    }
  else
    {
      exit_code = 1;
    }

  assert(spot::ltl::atomic_prop::instance_count() == 0);
  assert(spot::ltl::unop::instance_count() == 0);
  assert(spot::ltl::binop::instance_count() == 0);
  assert(spot::ltl::multop::instance_count() == 0);
461
  delete dict;
462
463
  return exit_code;
}