modelcheck.cc 8.74 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2011, 2012, 2013, 2014, 2015, 2016 Laboratoire de
// Recherche et Developpement de l'Epita (LRDE)
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
#include <spot/ltsmin/ltsmin.hh>
21
22
23
24
25
26
27
28
29
#include <spot/twaalgos/dot.hh>
#include <spot/tl/defaultenv.hh>
#include <spot/tl/parse.hh>
#include <spot/twaalgos/translate.hh>
#include <spot/twaalgos/emptiness.hh>
#include <spot/twaalgos/postproc.hh>
#include <spot/twa/twaproduct.hh>
#include <spot/misc/timer.hh>
#include <spot/misc/memusage.hh>
30
#include <cstring>
31
32
#include <spot/kripke/kripkegraph.hh>
#include <spot/twaalgos/hoa.hh>
33

34
static void
35
36
37
38
39
40
41
syntax(char* prog)
{
  // Display the supplied name unless it appears to be a libtool wrapper.
  char* slash = strrchr(prog, '/');
  if (slash && (strncmp(slash + 1, "lt-", 3) == 0))
    prog = slash + 4;

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  std::cerr << "usage: " << prog << " [options] model formula\n\
\n\
Options:\n\
  -dDEAD use DEAD as property for marking DEAD states\n\
          (by default DEAD = true)\n\
  -e[ALGO]  run emptiness check, expect an accepting run\n\
  -E[ALGO]  run emptiness check, expect no accepting run\n\
  -C     compute an accepting run (Counterexample) if it exists\n\
  -D     favor a deterministic translation over a small transition\n\
  -gf    output the automaton of the formula in dot format\n\
  -gm    output the model state-space in dot format\n\
  -gK    output the model state-space in Kripke format\n\
  -gp    output the product state-space in dot format\n\
  -T     time the different phases of the execution\n\
  -z     compress states to handle larger models\n\
  -Z     compress states (faster) assuming all values in [0 .. 2^28-1]\n\
";
59
60
  exit(1);
}
61

62
static int
63
checked_main(int argc, char **argv)
64
{
65
66
67
68
  spot::timer_map tm;

  bool use_timer = false;

69
  enum { DotFormula, DotModel, DotProduct, EmptinessCheck, Kripke }
70
71
72
  output = EmptinessCheck;
  bool accepting_run = false;
  bool expect_counter_example = false;
73
  bool deterministic = false;
74
  char *dead = nullptr;
75
  int compress_states = 0;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

  const char* echeck_algo = "Cou99";

  int dest = 1;
  int n = argc;
  for (int i = 1; i < n; ++i)
    {
      char* opt = argv[i];
      if (*opt == '-')
	{
	  switch (*++opt)
	    {
	    case 'C':
	      accepting_run = true;
	      break;
91
92
93
	    case 'd':
	      dead = opt + 1;
	      break;
94
95
96
	    case 'D':
	      deterministic = true;
	      break;
97
98
99
100
101
102
103
	    case 'e':
	    case 'E':
	      {
		echeck_algo = opt + 1;
		if (!*echeck_algo)
		  echeck_algo = "Cou99";

104
		expect_counter_example = (*opt == 'e');
105
106
107
108
109
110
		output = EmptinessCheck;
		break;
	      }
	    case 'g':
	      switch (opt[1])
		{
111
112
113
114
115
116
117
118
119
		case 'm':
		  output = DotModel;
		  break;
		case 'p':
		  output = DotProduct;
		  break;
		case 'f':
		  output = DotFormula;
		  break;
120
121
122
                case 'K':
                  output = Kripke;
                  break;
123
124
125
126
127
128
129
		default:
		  goto error;
		}
	      break;
	    case 'T':
	      use_timer = true;
	      break;
130
	    case 'z':
131
132
133
134
	      compress_states = 1;
	      break;
	    case 'Z':
	      compress_states = 2;
135
	      break;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
	    default:
	    error:
	      std::cerr << "Unknown option `" << argv[i] << "'." << std::endl;
	      exit(1);
	    }
	  --argc;
	}
      else
	{
	  argv[dest++] = argv[i];
	}
    }

  if (argc != 3)
    syntax(argv[0]);

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
152
153
  spot::default_environment& env =
    spot::default_environment::instance();
154

155

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
156
  spot::atomic_prop_set ap;
157
  auto dict = spot::make_bdd_dict();
158
  spot::const_kripke_ptr model = nullptr;
159
160
  spot::const_twa_ptr prop = nullptr;
  spot::const_twa_ptr product = nullptr;
161
  spot::emptiness_check_instantiator_ptr echeck_inst = nullptr;
162
  int exit_code = 0;
163
  spot::postprocessor post;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
164
165
  spot::formula deadf = nullptr;
  spot::formula f = nullptr;
166

167
  if (!dead || !strcasecmp(dead, "true"))
168
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
169
      deadf = spot::formula::tt();
170
171
172
    }
  else if (!strcasecmp(dead, "false"))
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
173
      deadf = spot::formula::ff();
174
175
176
177
178
    }
  else
    {
      deadf = env.require(dead);
    }
179

180
  if (output == EmptinessCheck)
181
    {
182
      const char* err;
183
      echeck_inst = spot::make_emptiness_check_instantiator(echeck_algo, &err);
184
185
186
      if (!echeck_inst)
	{
	  std::cerr << "Failed to parse argument of -e/-E near `"
187
		    << err <<  "'\n";
188
189
190
	  exit_code = 1;
	  goto safe_exit;
	}
191
192
    }

193
194
  tm.start("parsing formula");
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
195
196
197
    spot::parse_error_list pel;
    f = spot::parse_infix_psl(argv[2], pel, env, false);
    exit_code = spot::format_parse_errors(std::cerr, argv[2], pel);
198
199
200
201
202
203
  }
  tm.stop("parsing formula");

  if (exit_code)
    goto safe_exit;

204
  tm.start("translating formula");
205
  {
206
207
208
209
210
    spot::translator trans(dict);
    if (deterministic)
      trans.set_pref(spot::postprocessor::Deterministic);

    prop = trans.run(&f);
211
  }
212
  tm.stop("translating formula");
213

214
215
216
  atomic_prop_collect(f, &ap);

  if (output != DotFormula)
217
    {
218
      tm.start("loading ltsmin model");
219
220
221
222
223
224
225
226
227
      try
	{
	  model = spot::ltsmin_model::load(argv[1]).kripke(&ap, dict, deadf,
							   compress_states);
	}
      catch (std::runtime_error& e)
	{
	  std::cerr << e.what() << '\n';
	}
228
      tm.stop("loading ltsmin model");
229
230
231
232
233
234
235
236
237

      if (!model)
	{
	  exit_code = 1;
	  goto safe_exit;
	}

      if (output == DotModel)
	{
238
239
240
	  tm.start("dot output");
	  spot::print_dot(std::cout, model);
	  tm.stop("dot output");
241
242
	  goto safe_exit;
	}
243
244
245
      if (output == Kripke)
      {
        tm.start("kripke output");
246
	spot::print_hoa(std::cout, model);
247
248
249
        tm.stop("kripke output");
        goto safe_exit;
      }
250
251
    }

252
253
  if (output == DotFormula)
    {
254
255
256
      tm.start("dot output");
      spot::print_dot(std::cout, prop);
      tm.stop("dot output");
257
258
259
      goto safe_exit;
    }

260
  product = spot::otf_product(model, prop);
261

262
  if (output == DotProduct)
263
    {
264
265
266
      tm.start("dot output");
      spot::print_dot(std::cout, product);
      tm.stop("dot output");
267
      goto safe_exit;
268
269
    }

270
271
272
  assert(echeck_inst);

  {
273
    auto ec = echeck_inst->instantiate(product);
274
275
276
277
    bool search_many = echeck_inst->options().get("repeated");
    assert(ec);
    do
      {
278
	int memused = spot::memusage();
279
	tm.start("running emptiness check");
280
	spot::emptiness_check_result_ptr res;
281
282
283
284
285
286
287
288
	try
	  {
	    res = ec->check();
	  }
	catch (std::bad_alloc)
	  {
	    std::cerr << "Out of memory during emptiness check."
		      << std::endl;
289
290
	    if (!compress_states)
	      std::cerr << "Try option -z for state compression." << std::endl;
291
292
293
	    exit_code = 2;
	    exit(exit_code);
	  }
294
	tm.stop("running emptiness check");
295
	memused = spot::memusage() - memused;
296
297

	ec->print_stats(std::cout);
298
299
	std::cout << memused << " pages allocated for emptiness check"
		  << std::endl;
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322

	if (expect_counter_example == !res &&
	    (!expect_counter_example || ec->safe()))
	  exit_code = 1;

	if (!res)
	  {
	    std::cout << "no accepting run found";
	    if (!ec->safe() && expect_counter_example)
	      {
		std::cout << " even if expected" << std::endl;
		std::cout << "this may be due to the use of the bit"
			  << " state hashing technique" << std::endl;
		std::cout << "you can try to increase the heap size "
			  << "or use an explicit storage"
			  << std::endl;
	      }
	    std::cout << std::endl;
	    break;
	  }
	else if (accepting_run)
	  {

323
	    spot::twa_run_ptr run;
324
	    tm.start("computing accepting run");
325
326
327
328
329
330
331
332
333
334
335
	    try
	      {
		run = res->accepting_run();
	      }
	    catch (std::bad_alloc)
	      {
		std::cerr << "Out of memory while looking for counterexample."
			  << std::endl;
		exit_code = 2;
		exit(exit_code);
	      }
336
337
338
339
340
341
342
343
344
	    tm.stop("computing accepting run");

	    if (!run)
	      {
		std::cout << "an accepting run exists" << std::endl;
	      }
	    else
	      {
		tm.start("reducing accepting run");
345
		run = run->reduce();
346
347
		tm.stop("reducing accepting run");
		tm.start("printing accepting run");
348
		std::cout << run;
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
		tm.stop("printing accepting run");
	      }
	  }
	else
	  {
	    std::cout << "an accepting run exists "
		      << "(use -C to print it)" << std::endl;
	  }
      }
    while (search_many);
  }

 safe_exit:
  if (use_timer)
    tm.print(std::cout);
364
  tm.reset_all();		// This helps valgrind.
365
366
  return exit_code;
}
367

368
369
370
371
372
373
int
main(int argc, char **argv)
{
  auto exit_code = checked_main(argc, argv);

  // Additional checks to debug reference counts in formulas.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
374
  assert(spot::fnode::instances_check());
375
  exit(exit_code);
376
}