ltl2tgba.cc 6.71 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
17
18
19

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

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

57
  bool debug_opt = false;
58
  bool degeneralize_opt = false;
59
60
  bool fm_opt = false;
  bool file_opt = false;
61
  int output = 0;
62
  int formula_index = 0;
63
64
65
  enum { None, Couvreur, MagicSearch } echeck = None;
  bool magic_many = false;
  bool expect_counter_example = false;
66

67
  for (;;)
68
    {
69
      if (argc < formula_index + 2)
70
	syntax(argv[0]);
71
72
73

      ++formula_index;

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

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  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];
    }

176
177
  spot::ltl::environment& env(spot::ltl::default_environment::instance());
  spot::ltl::parse_error_list pel;
178
  spot::ltl::formula* f = spot::ltl::parse(input, pel, env, debug_opt);
179

180
  exit_code = spot::ltl::format_parse_errors(std::cerr, input, pel);
181

182
  spot::bdd_dict* dict = new spot::bdd_dict();
183
184
  if (f)
    {
185
186
187
188
189
190
191
      spot::tgba_bdd_concrete* concrete = 0;
      spot::tgba* to_free = 0;
      spot::tgba* a = 0;

      if (fm_opt)
	to_free = a = spot::ltl_to_tgba_fm(f, dict);
      else
192
	to_free = a = concrete = spot::ltl_to_tgba_lacim(f, dict);
193

194
      spot::ltl::destroy(f);
195

196
      spot::tgba_tba_proxy* degeneralized = 0;
197
198
199
      if (degeneralize_opt)
	a = degeneralized = new spot::tgba_tba_proxy(a);

200
201
      switch (output)
	{
202
203
204
	case -1:
	  /* No output.  */
	  break;
205
206
207
208
	case 0:
	  spot::dotty_reachable(std::cout, a);
	  break;
	case 1:
209
210
211
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
212
213
	  break;
	case 2:
214
215
216
217
	  if (concrete)
	    spot::bdd_print_dot(std::cout, concrete->get_dict(),
				concrete->
				get_core_data().accepting_conditions);
218
219
	  break;
	case 3:
220
221
222
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->get_core_data().relation);
223
224
	  break;
	case 4:
225
226
227
228
	  if (concrete)
	    spot::bdd_print_set(std::cout, concrete->get_dict(),
				concrete->
				get_core_data().accepting_conditions);
229
	  break;
230
	case 5:
231
	  a->get_dict()->dump(std::cout);
232
	  break;
233
234
235
	case 6:
	  spot::lbtt_reachable(std::cout, a);
	  break;
236
237
238
	default:
	  assert(!"unknown output option");
	}
239

240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
      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;
	}

287
288
289
      if (degeneralize_opt)
	delete degeneralized;

290
      delete to_free;
291
292
293
294
295
296
297
298
299
300
    }
  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);
301
  delete dict;
302
303
  return exit_code;
}