ltl2tgba.cc 7.27 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
18
19
20

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

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

61
  bool debug_opt = false;
62
  bool degeneralize_opt = false;
63
64
  bool fm_opt = false;
  bool file_opt = false;
65
  int output = 0;
66
  int formula_index = 0;
67
68
69
  enum { None, Couvreur, MagicSearch } echeck = None;
  bool magic_many = false;
  bool expect_counter_example = false;
70
  bool from_file = false;
71

72
  for (;;)
73
    {
74
      if (argc < formula_index + 2)
75
	syntax(argv[0]);
76
77
78

      ++formula_index;

79
80
81
82
83
84
85
86
87
      if (!strcmp(argv[formula_index], "-a"))
	{
	  output = 2;
	}
      else if (!strcmp(argv[formula_index], "-A"))
	{
	  output = 4;
	}
      else if (!strcmp(argv[formula_index], "-d"))
88
89
90
	{
	  debug_opt = true;
	}
91
92
93
94
      else if (!strcmp(argv[formula_index], "-D"))
	{
	  degeneralize_opt = true;
	}
95
96
97
98
99
100
101
102
103
104
105
106
      else if (!strcmp(argv[formula_index], "-e"))
	{
	  echeck = Couvreur;
	  expect_counter_example = true;
	  output = -1;
	}
      else if (!strcmp(argv[formula_index], "-E"))
	{
	  echeck = Couvreur;
	  expect_counter_example = false;
	  output = -1;
	}
107
108
109
110
111
112
113
114
      else if (!strcmp(argv[formula_index], "-f"))
	{
	  fm_opt = true;
	}
      else if (!strcmp(argv[formula_index], "-F"))
	{
	  file_opt = true;
	}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
      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;
	}
137
138
      else if (!strcmp(argv[formula_index], "-r"))
	{
139
140
141
142
143
	  output = 1;
	}
      else if (!strcmp(argv[formula_index], "-R"))
	{
	  output = 3;
144
	}
145
146
147
148
      else if (!strcmp(argv[formula_index], "-t"))
	{
	  output = 6;
	}
149
150
151
152
      else if (!strcmp(argv[formula_index], "-v"))
	{
	  output = 5;
	}
153
154
155
156
      else if (!strcmp(argv[formula_index], "-X"))
	{
	  from_file = true;
	}
157
158
159
160
      else
	{
	  break;
	}
161
162
    }

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  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];
    }

185
  spot::ltl::environment& env(spot::ltl::default_environment::instance());
186
  spot::bdd_dict* dict = new spot::bdd_dict();
187
188
189
190
191
192
193
194
195

  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)
196
    {
197
198
199
200
      spot::tgba_bdd_concrete* concrete = 0;
      spot::tgba* to_free = 0;
      spot::tgba* a = 0;

201
202
203
204
205
206
207
      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;
	}
208
      else
209
210
211
212
213
214
215
	{
	  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);
	}
216

217
      spot::tgba_tba_proxy* degeneralized = 0;
218
219
220
      if (degeneralize_opt)
	a = degeneralized = new spot::tgba_tba_proxy(a);

221
222
      switch (output)
	{
223
224
225
	case -1:
	  /* No output.  */
	  break;
226
227
228
229
	case 0:
	  spot::dotty_reachable(std::cout, a);
	  break;
	case 1:
230
231
232
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
233
234
	  break;
	case 2:
235
236
237
238
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->
				get_core_data().accepting_conditions);
239
240
	  break;
	case 3:
241
242
243
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
244
245
	  break;
	case 4:
246
247
248
249
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->
				get_core_data().accepting_conditions);
250
	  break;
251
	case 5:
252
	  a->get_dict()->dump(std::cout);
253
	  break;
254
255
256
	case 6:
	  spot::lbtt_reachable(std::cout, a);
	  break;
257
258
259
	default:
	  assert(!"unknown output option");
	}
260

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
      switch (echeck)
	{
	case None:
	  break;
	case Couvreur:
	  {
	    spot::emptiness_check ec = spot::emptiness_check();
	    bool res = ec.tgba_emptiness_check(a);
	    if (expect_counter_example)
	      {
		if (res)
		  {
		    exit_code = 1;
		    break;
		  }
		ec.counter_example(a);
		ec.print_result(std::cout, a);
	      }
	    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;
	}

308
309
310
      if (degeneralize_opt)
	delete degeneralized;

311
      delete to_free;
312
313
314
315
316
317
318
319
320
321
    }
  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);
322
  delete dict;
323
324
  return exit_code;
}