ltl2tgba.cc 8.47 KB
Newer Older
1
2
#include <iostream>
#include <cassert>
3
4
#include <fstream>
#include <string>
5
6
7
#include "ltlvisit/destroy.hh"
#include "ltlast/allnodes.hh"
#include "ltlparse/public.hh"
8
#include "tgbaalgos/ltl2tgba_lacim.hh"
9
#include "tgbaalgos/ltl2tgba_fm.hh"
10
#include "tgba/bddprint.hh"
11
#include "tgbaalgos/dotty.hh"
12
#include "tgbaalgos/lbtt.hh"
13
#include "tgba/tgbatba.hh"
14
15
#include "tgbaalgos/magic.hh"
#include "tgbaalgos/emptinesscheck.hh"
16
#include "tgbaparse/public.hh"
17
#include "tgbaalgos/dupexp.hh"
18
19
20
21

void
syntax(char* prog)
{
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
22
  std::cerr << "Usage: "<< prog << " [OPTIONS...] formula" << std::endl
23
            << "       "<< prog << " -F [OPTIONS...] file" << std::endl
24
            << "       "<< prog << " -X [OPTIONS...] file" << std::endl
Alexandre Duret-Lutz's avatar
spacing    
Alexandre Duret-Lutz committed
25
	    << std::endl
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
26
	    << "Options:" << std::endl
27
	    << "  -a   display the acceptance_conditions BDD, not the "
28
	    << "reachability graph"
29
	    << std::endl
30
	    << "  -A   same as -a, but as a set" << std::endl
31
	    << "  -d   turn on traces during parsing" << std::endl
32
	    << "  -D   degeneralize the automaton" << std::endl
33
34
	    << "  -e   emptiness-check (Couvreur), expect and compute "
	    << "a counter-example" << std::endl
35
36
	    << "  -e2  emptiness-check (Couvreur variant), expect and compute "
	    << "a counter-example" << std::endl
37
38
	    << "  -E   emptiness-check (Couvreur), expect no counter-example "
	    << std::endl
39
40
	    << "  -E2  emptiness-check (Couvreur variant), expect no "
	    << "counter-example " << std::endl
41
42
43
            << "  -f   use Couvreur's FM algorithm for translation"
	    << std::endl
            << "  -F   read the formula from the file" << std::endl
44
45
46
47
48
49
	    << "  -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
50
	    << "  -r   display the relation BDD, not the reachability graph"
51
	    << std::endl
52
	    << "  -R   same as -r, but as a set" << std::endl
53
54
55
56
	    << "  -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
57
	    << "  -t   display reachable states in LBTT's format" << std::endl
Alexandre Duret-Lutz's avatar
spacing    
Alexandre Duret-Lutz committed
58
	    << "  -v   display the BDD variables used by the automaton"
59
60
	    << std::endl
	    << "  -X   do compute an automaton, read it from a file"
61
	    << std::endl;
62
63
64
65
66
67
68
69
  exit(2);
}

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

70
  bool debug_opt = false;
71
  bool degeneralize_opt = false;
72
73
  bool fm_opt = false;
  bool file_opt = false;
74
  int output = 0;
75
  int formula_index = 0;
76
  enum { None, Couvreur, Couvreur2, MagicSearch } echeck = None;
77
  enum { NoneDup, BFS, DFS } dupexp = NoneDup;
78
79
  bool magic_many = false;
  bool expect_counter_example = false;
80
  bool from_file = false;
81

82
  for (;;)
83
    {
84
      if (argc < formula_index + 2)
85
	syntax(argv[0]);
86
87
88

      ++formula_index;

89
90
91
92
93
94
95
96
97
      if (!strcmp(argv[formula_index], "-a"))
	{
	  output = 2;
	}
      else if (!strcmp(argv[formula_index], "-A"))
	{
	  output = 4;
	}
      else if (!strcmp(argv[formula_index], "-d"))
98
99
100
	{
	  debug_opt = true;
	}
101
102
103
104
      else if (!strcmp(argv[formula_index], "-D"))
	{
	  degeneralize_opt = true;
	}
105
106
107
108
109
110
      else if (!strcmp(argv[formula_index], "-e"))
	{
	  echeck = Couvreur;
	  expect_counter_example = true;
	  output = -1;
	}
111
112
113
114
115
116
      else if (!strcmp(argv[formula_index], "-e2"))
	{
	  echeck = Couvreur2;
	  expect_counter_example = true;
	  output = -1;
	}
117
118
119
120
121
122
      else if (!strcmp(argv[formula_index], "-E"))
	{
	  echeck = Couvreur;
	  expect_counter_example = false;
	  output = -1;
	}
123
124
125
126
127
128
      else if (!strcmp(argv[formula_index], "-E2"))
	{
	  echeck = Couvreur2;
	  expect_counter_example = false;
	  output = -1;
	}
129
130
131
132
133
134
135
136
      else if (!strcmp(argv[formula_index], "-f"))
	{
	  fm_opt = true;
	}
      else if (!strcmp(argv[formula_index], "-F"))
	{
	  file_opt = true;
	}
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
      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;
	}
159
160
      else if (!strcmp(argv[formula_index], "-r"))
	{
161
162
163
164
165
	  output = 1;
	}
      else if (!strcmp(argv[formula_index], "-R"))
	{
	  output = 3;
166
	}
167
168
169
170
171
172
173
174
      else if (!strcmp(argv[formula_index], "-s"))
	{
	  dupexp = DFS;
	}
      else if (!strcmp(argv[formula_index], "-S"))
	{
	  dupexp = BFS;
	}
175
176
177
178
      else if (!strcmp(argv[formula_index], "-t"))
	{
	  output = 6;
	}
179
180
181
182
      else if (!strcmp(argv[formula_index], "-v"))
	{
	  output = 5;
	}
183
184
185
186
      else if (!strcmp(argv[formula_index], "-X"))
	{
	  from_file = true;
	}
187
188
189
190
      else
	{
	  break;
	}
191
192
    }

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  std::string input;

  if (file_opt)
    {
      std::ifstream fin(argv[formula_index]);
      if (! fin)
	{
	  std::cerr << "Cannot open " << argv[formula_index] << std::endl;
	  exit(2);
	}

      if (! std::getline(fin, input, '\0'))
	{
	  std::cerr << "Cannot read " << argv[formula_index] << std::endl;
	  exit(2);
	}
    }
  else
    {
      input = argv[formula_index];
    }

215
  spot::ltl::environment& env(spot::ltl::default_environment::instance());
216
  spot::bdd_dict* dict = new spot::bdd_dict();
217
218
219
220
221
222
223
224
225

  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)
226
    {
227
228
229
230
      spot::tgba_bdd_concrete* concrete = 0;
      spot::tgba* to_free = 0;
      spot::tgba* a = 0;

231
232
233
234
235
236
237
      if (from_file)
	{
	  spot::tgba_parse_error_list pel;
	  to_free = a = spot::tgba_parse(input, pel, dict, env, debug_opt);
	  if (spot::format_tgba_parse_errors(std::cerr, pel))
	    return 2;
	}
238
      else
239
240
241
242
243
244
245
	{
	  if (fm_opt)
	    to_free = a = spot::ltl_to_tgba_fm(f, dict);
	  else
	    to_free = a = concrete = spot::ltl_to_tgba_lacim(f, dict);
	  spot::ltl::destroy(f);
	}
246

247
      spot::tgba_tba_proxy* degeneralized = 0;
248
249
250
      if (degeneralize_opt)
	a = degeneralized = new spot::tgba_tba_proxy(a);

251
252
253
254
255
256
257
258
259
260
261
262
263
      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;
	}

264
265
      switch (output)
	{
266
267
268
	case -1:
	  /* No output.  */
	  break;
269
270
271
272
	case 0:
	  spot::dotty_reachable(std::cout, a);
	  break;
	case 1:
273
274
275
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
276
277
	  break;
	case 2:
278
279
280
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->
281
				get_core_data().acceptance_conditions);
282
283
	  break;
	case 3:
284
285
286
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
287
288
	  break;
	case 4:
289
290
291
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->
292
				get_core_data().acceptance_conditions);
293
	  break;
294
	case 5:
295
	  a->get_dict()->dump(std::cout);
296
	  break;
297
298
299
	case 6:
	  spot::lbtt_reachable(std::cout, a);
	  break;
300
301
302
	default:
	  assert(!"unknown output option");
	}
303

304
305
306
307
308
      switch (echeck)
	{
	case None:
	  break;
	case Couvreur:
309
	case Couvreur2:
310
	  {
311
	    spot::emptiness_check ec = spot::emptiness_check(a);
312
313
314
315
316
317
318
	    bool res;

	    if (echeck == Couvreur)
	      res = ec.check();
	    else
	      res = ec.check2();

319
320
321
322
323
324
325
	    if (expect_counter_example)
	      {
		if (res)
		  {
		    exit_code = 1;
		    break;
		  }
326
327
		ec.counter_example();
		ec.print_result(std::cout);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
	      }
	    else
	      {
		exit_code = !res;
	      }
	  }
	  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;
	}

358
359
      if (expl)
	delete expl;
360
361
362
      if (degeneralize_opt)
	delete degeneralized;

363
      delete to_free;
364
365
366
367
368
369
370
371
372
373
    }
  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);
374
  delete dict;
375
376
  return exit_code;
}