TestOperations.cc 51.4 KB
Newer Older
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1
/*
2
 *  Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *  Heikki Tauriainen <Heikki.Tauriainen@hut.fi>
 *
 *  This program 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.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef __GNUC__
#pragma implementation
#endif /* __GNUC__ */

#include <config.h>
25
#include <csignal>
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
26
27
28
#include <cstdio>
#include <cstdlib>
#include <sys/times.h>
29
#include <sys/wait.h>
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include "BitArray.h"
#include "BuchiAutomaton.h"
#include "DispUtil.h"
#include "LtlFormula.h"
#include "PathEvaluator.h"
#include "ProductAutomaton.h"
#include "Random.h"
#include "SccIterator.h"
#include "SharedTestData.h"
#include "PathIterator.h"
#include "StateSpace.h"
#include "StatDisplay.h"
#include "StringUtil.h"
#include "TestOperations.h"
#include "TestRoundInfo.h"
#include "TestStatistics.h"

/******************************************************************************
 *
 * Implementations for the operations used in the main test loop.
 *
 *****************************************************************************/

namespace TestOperations
{

using namespace ::SharedTestData;
using namespace ::StatDisplay;
using namespace ::StringUtil;
using namespace ::DispUtil;

/* ========================================================================= */
void openFile
  (const char* filename, ifstream& stream, ios::openmode mode, int indent)
/* ----------------------------------------------------------------------------
 *
 * Description:   Function for opening a file for input.
 *
 * Arguments:     filename  --  A pointer to a constant C-style string
 *                              containing the name of the file to be opened.
 *                stream    --  A reference to the input stream that should be
 *                              associated with the file.
 *                mode      --  A constant of type `ios::openmode' determining
 *                              the open mode.
 *                indent    --  Number of spaces to leave to the left of
 *                              messages given to the user.
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  printText(string("<opening `") + filename + "'>", 5, indent);

  stream.open(filename, mode);

  if (!stream.good())
  {
    printText(" error\n", 5);
    throw FileOpenException(string("`") + filename + "'");
  }
  else
    printText(" ok\n", 5);
}

/* ========================================================================= */
void openFile
  (const char* filename, ofstream& stream, ios::openmode mode, int indent)
/* ----------------------------------------------------------------------------
 *
 * Description:   Function for opening a file for output.
 *
 * Arguments:     filename  --  A pointer to a constant C-style string with
 *                              the name of the file to be opened.
 *                stream    --  A reference to the output stream that should be
 *                              associated with the file.
 *                mode      --  A constant of type `ios::openmode' determining
 *                              the open mode.
 *                indent    --  Number of spaces to leave to the left of
 *                              messages given to the user.
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  printText(string("<") + (mode & ios::trunc ? "creat" : "open") + "ing `"
	    + filename + "'>",
	    5,
	    indent);

  stream.open(filename, mode);

  if (!stream.good())
  {
    printText(" error\n", 5);

    if (mode & ios::trunc)
      throw FileCreationException(string("`") + filename + "'");
    else
      throw FileOpenException(string("`") + filename + "'");
  }
  else
    printText(" ok\n", 5);
}

/* ========================================================================= */
void removeFile(const char* filename, int indent)
/* ----------------------------------------------------------------------------
 *
 * Description:   Removes a file.
 *
 * Arguments:     filename  --  A pointer to a constant C-style string with
 *                              the name of the file to be removed.
 *                indent    --  Number of spaces to leave to the left of
 *                              messages given to the user.
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  printText(string("<removing `") + filename + "'>", 5, indent);

  if (remove(filename) == 0)
    printText(" ok\n", 5);
  else
    printText(" error\n", 5);
}

/* ========================================================================= */
void printFileContents
  (ostream& stream, const char* message, const char* filename, int indent,
   const char* line_prefix)
/* ----------------------------------------------------------------------------
 *
 * Description:   Outputs the contents of a file into a stream.
 *
 * Arguments:     stream       --  A reference to the output stream into which
 *                                 the file contents should be outputted.
 *                message      --  A pointer to a constant C-style string
 *                                 containing a message to be outputted to the
 *                                 stream before the contents of the file. (To
 *                                 display only the file contents, use a null
 *                                 pointer for the message; an empty string
 *                                 results in an empty line to be outputted
 *                                 before the file contents.)
 *                filename     --  A pointer to a constant C-style string
 *                                 containing the name of the input file.
 *                indent       --  Number of spaces to leave to the left of
 *                                 output.
 *                line_prefix  --  A pointer to a constant C-style string
 *                                 that will be prepended to each outputted
 *                                 line.
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  ifstream file;
  Exceptional_ostream estream(&stream, ios::failbit | ios::badbit);

  openFile(filename, file, ios::in, indent);

  bool first_line_printed = false;
  string message_line;

  printText(string("<reading from `") + filename + "'>\n", 5, indent);

  while (file.good())
  {
    message_line = "";

    getline(file, message_line, '\n');

    if (!file.eof())
    {
      if (!first_line_printed && message != 0)
      {
	first_line_printed = true;
	estream << string(indent, ' ') + message + '\n';
      }
212

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
      estream << string(indent, ' ') + line_prefix + message_line + '\n';
    }
  }

  estream.flush();

  file.close();
}

/* ========================================================================= */
void writeToTranscript(const string& message, bool show_formula_in_header)
/* ----------------------------------------------------------------------------
 *
 * Description:   Writes a message into the transcript file.  (If this is the
 *                first message to be written to the file in the current test
 *                round, the message is preceded with a header showing the
 *                round number and the LTL formulae used in the round.)
 *
 * Argument:      message                 --  A message to be written to the
 *                                            file.
 *                show_formula_in_header  --  If false, prevents displaying
 *                                            an LTL formula in the error
 *                                            report header (e.g., when it is
 *                                            the formula generation that
 *                                            failed).
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  if (round_info.error_report_round != round_info.current_round)
  {
    round_info.error_report_round = round_info.current_round;

    const string roundstring = "Round " + toString(round_info.current_round);

    round_info.transcript_file << roundstring + '\n'
250
251
				  + string(roundstring.length(), '-')
				  + "\n\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
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
287
288
289
290
291
292
293
294
295

    if (show_formula_in_header)
    {
      const int formula
	= (configuration.formula_options.output_mode == Configuration::NNF
	   ? 0
	   : 2);

      try
      {
	round_info.transcript_file << "    Formula (+): ";
	round_info.formulae[formula]->print(round_info.transcript_file);
	round_info.transcript_file << "\n    Negated (-): ";
	round_info.formulae[formula + 1]->print(round_info.transcript_file);
	round_info.transcript_file << endl << endl;
      }
      catch (const IOException&)
      {
      }
    }
  }

  round_info.transcript_file << "    " + message + '\n';
  round_info.transcript_file.flush();
}

/* ========================================================================= */
void generateStateSpace()
/* ----------------------------------------------------------------------------
 *
 * Description:   Generates a random state space. The global variable
 *                `configuration.global_options.statespace_generation_mode'
 *                determines the type of state space to be generated.
 *
 * Arguments:     None.
 *
 * Returns:       Nothing. The result can be accessed through
 *                `round_info.statespace'.
 *
 * ------------------------------------------------------------------------- */
{
  using ::Graph::StateSpace;

  if (configuration.global_options.statespace_generation_mode
296
	== Configuration::ENUMERATEDPATH)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
  {
    StateSpace::size_type current_size
      = configuration.statespace_generator.min_size;

    /*
     *  If the state spaces are enumerated paths, generate the next path.
     */

    printText("Generating next state space\n", 2, 4);

    if (round_info.path_iterator != 0)
    {
      current_size = (*round_info.path_iterator)->size();
      ++(*round_info.path_iterator);
      if (round_info.path_iterator->atEnd())
      {
	delete round_info.path_iterator;
	round_info.path_iterator = 0;

	if (current_size < configuration.statespace_generator.max_size)
	{
	  current_size++;
	  printText("[Increasing state space size to " + toString(current_size)
		    + "]\n",
		    2,
		    6);
	}
324
	else
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
325
326
327
328
329
330
331
332
333
334
335
336
337
	{
	  current_size = configuration.statespace_generator.min_size;
	  printText("[All state spaces have been enumerated. Staring over]\n",
		    2,
		    6);
	}
      }
    }

    if (round_info.path_iterator == 0)
    {
      round_info.path_iterator
	= new Graph::PathIterator
338
		(configuration.statespace_generator.atoms_per_state,
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
		 current_size);
    }

    printText("\n", 2);

    round_info.statespace = &(**round_info.path_iterator);
  }
  else
  {
    /*
     *  Otherwise generate a random state space.
     */

    if (round_info.statespace != 0)
    {
      delete round_info.statespace;
      round_info.statespace = 0;
    }

    if (printText("Generating random state space\n", 2, 4))
      printText("<generating>", 4, 6);

    /*
     *  Determine the type of the state space to be generated according to the
     *  configuration options, then generate it.
     */

    StateSpace* statespace;

    try
    {
      switch (configuration.global_options.statespace_generation_mode)
      {
372
	case Configuration::RANDOMGRAPH :
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
373
374
375
	  statespace = configuration.statespace_generator.generateGraph();
	  break;

376
	case Configuration::RANDOMCONNECTEDGRAPH :
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
377
	  statespace = configuration.statespace_generator.
378
			 generateConnectedGraph();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
379
380
	  break;

381
	default : /* Configuration::RANDOMPATH */
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	  statespace = configuration.statespace_generator.generatePath();
	  break;
      }
    }
    catch (const UserBreakException&)
    {
      printText(" user break\n\n", 4);

      if (round_info.transcript_file.is_open())
	writeToTranscript("User break while generating state space. No tests "
			  "performed.\n", false);

      throw;
    }
    catch (const bad_alloc&)
    {
      if (!printText(" out of memory\n\n", 4))
	printText("[Out of memory]\n\n", 2, 6);

      if (round_info.transcript_file.is_open())
	writeToTranscript("Out of memory while generating state space. No "
			  "tests performed.\n", false);

      throw StateSpaceGenerationException();
    }

    round_info.statespace = statespace;

    printText(" ok\n", 4);

412
413
    if (configuration.statespace_generator.max_size
	  > configuration.statespace_generator.min_size)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
414
415
416
417
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
      printText("number of states: "
		+ toString(round_info.statespace->size())
		+ '\n',
		3,
		6);

    printText("\n", 2);
  }

  round_info.num_generated_statespaces++;

  pair<StateSpace::size_type, unsigned long int>
    statespace_stats(round_info.statespace->stats());

  round_info.total_statespace_states += statespace_stats.first;
  round_info.total_statespace_transitions += statespace_stats.second;

  round_info.real_emptiness_check_size
    = (configuration.global_options.product_mode == Configuration::GLOBAL
       ? round_info.statespace->size()
       : 1);
}

/* ========================================================================= */
void generateFormulae(istream* formula_input_stream)
/* ----------------------------------------------------------------------------
 *
 * Description:   Generates a random LTL formula according to the current
 *                configuration and stores the formula, its negation and their
 *                negated normal forms into `round_info.formulae'.
 *
 * Argument:      formula_input_stream  --  A pointer to an input stream. If
 *                                          the pointer is nonzero, no random
 *                                          formula will be generated; instead,
 *                                          a formula will be read from the
 *                                          stream.
 *
 * Returns:       Nothing. The generated formulae can be found in the global
 *                array `round_info.formulae'.
 *
 * ------------------------------------------------------------------------- */
{
  for (int f = 0; f < 4; f++)
  {
    if (round_info.formulae[f] != 0)
    {
      ::Ltl::LtlFormula::destruct(round_info.formulae[f]);
      round_info.formulae[f] = static_cast<class ::Ltl::LtlFormula*>(0);
    }
  }

  if (printText(string(formula_input_stream == 0 ? "Random " : "")
		+ "LTL formula:\n",
		3,
		4))
    printText(string("<") + (formula_input_stream == 0 ? "generat" : "read")
	      + "ing>",
	      4,
	      6);
  else
    printText(string(formula_input_stream == 0
		     ? "Generating random"
		     : "Reading")
	      + " LTL formula\n",
	      2,
	      4);

  if (formula_input_stream != 0)
  {
    /*
     *  If a valid pointer to a stream was given as a parameter, try to read a
     *  formula from the stream.
     */

    try
    {
      try
      {
	round_info.formulae[2]
	  = ::Ltl::LtlFormula::read(*formula_input_stream);
      }
      catch (...)
      {
	printText(" error\n", 4);
	throw;
      }
    }
    catch (const ::Ltl::LtlFormula::ParseErrorException&)
    {
      printText(string("[Error parsing the ")
		+ (formula_input_stream == &round_info.formula_input_file
		   ? "formula file"
		   : "input formula")
		+ ". Aborting]\n",
		2,
		6);

      if (round_info.transcript_file.is_open())
	writeToTranscript("Error parsing input formula. Testing aborted.\n",
			  false);

      throw FormulaGenerationException();
    }
    catch (const IOException&)
    {
      bool fatal_io_error
	= (configuration.global_options.formula_input_filename.empty()
	   || !round_info.formula_input_file.eof());

      printText(string("[") + (fatal_io_error
			       ? "Error reading formula"
			       : "No more input formulae")
		+ ". Aborting]\n",
		2,
		6);

      if (round_info.transcript_file.is_open())
	writeToTranscript(fatal_io_error
			  ? "Error reading input formula. Testing "
			    "aborted.\n"
			  : "No more input formulae. Testing aborted.\n",
			  false);

      throw FormulaGenerationException();
    }

    printText(" ok\n", 4);
  }
  else
  {
    /*
     *  Otherwise generate a random formula.
     */

    try
    {
      round_info.formulae[2]
	= configuration.formula_options.formula_generator.generate();
    }
    catch (...)
    {
      printText(" error\n", 4);
      throw;
    }

    printText(" ok\n", 4);

    if (configuration.formula_options.formula_generator.max_size
	  > configuration.formula_options.formula_generator.size)
      printText("parse tree size:" + string(11, ' ')
		+ toString(round_info.formulae[2]->size()) + '\n',
		3,
		6);
  }

  ++round_info.num_processed_formulae;

  printText("<converting to negation normal form>", 4, 6);

  round_info.formulae[0] = round_info.formulae[2]->nnfClone();

  if (printText(" ok\n", 4))
    printText("<negating formula>", 4, 6);
577

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
  round_info.formulae[3] = &(::Ltl::Not::construct(*round_info.formulae[2]));

  if (printText(" ok\n", 4))
    printText("<converting negated formula to negation normal form>", 4, 6);

  round_info.formulae[1] = round_info.formulae[3]->nnfClone();

  if (configuration.global_options.verbosity >= 3)
  {
    printText(" ok\n", 4);

    for (int f = 0; f <= 1; f++)
    {
      round_info.cout << string(6, ' ') + (f == 0 ? "" : "negated ")
592
			 + "formula:" + string(19 - 8 * f, ' ');
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
      round_info.formulae[f + 2]->print(round_info.cout);
      round_info.cout << '\n';

      if (configuration.formula_options.output_mode == Configuration::NNF)
      {
	round_info.cout << string(8, ' ') + "in negation normal form: ";
	round_info.formulae[f]->print(round_info.cout);
	round_info.cout << '\n';
      }
    }
  }

  printText("\n", 2);
}

/* ========================================================================= */
void verifyFormulaOnPath()
/* ----------------------------------------------------------------------------
 *
 * Description:   Model checks an LTL formula (accessed through the global
 *                data structure `round_info' on a path directly and stores the
 *                result into the test result data structure.
 *
 * Arguments:     None.
 *
 * Returns:       Nothing. The model checking results are stored into the test
 *                result data structure.
 *
 * ------------------------------------------------------------------------- */
{
  if (printText("Model checking formula using internal algorithm\n", 2, 4))
    printText("<model checking>", 4, 6);

  test_results[round_info.number_of_translators].automaton_stats[0].
    emptiness_check_result.clear();
  test_results[round_info.number_of_translators].automaton_stats[1].
    emptiness_check_result.clear();

  try
  {
    int formula = (configuration.formula_options.output_mode
		     == Configuration::NNF
		   ? 0
		   : 2);

    Ltl::PathEvaluator path_evaluator;
    path_evaluator.evaluate(*round_info.formulae[formula],
			    *(round_info.statespace));

    for (unsigned long int s = 0; s < round_info.real_emptiness_check_size;
	 s++)
    {
      if (path_evaluator.getResult(s))
	test_results[round_info.number_of_translators].automaton_stats[0].
	  emptiness_check_result.setBit(s);
      else
	test_results[round_info.number_of_translators].automaton_stats[1].
	  emptiness_check_result.setBit(s);
    }
  }
  catch (const UserBreakException&)
  {
    if (!printText(" user break\n\n", 4))
      printText("[User break]\n\n", 2, 6);

    if (round_info.transcript_file.is_open())
      writeToTranscript("User break while model checking formulas. No tests "
			"performed.\n");

    throw;
  }
  catch (const bad_alloc&)
  {
    if (!printText(" out of memory\n\n", 4))
      printText("[Out of memory]\n\n", 2, 6);

    if (round_info.transcript_file.is_open())
      writeToTranscript("Out of memory while model checking formulas. No "
			"tests performed.\n");

    round_info.error = true;
    return;
  }

  printText(" ok\n", 4);
  printText("\n", 2);

  test_results[round_info.number_of_translators].automaton_stats[0].
    emptiness_check_performed = true;
  test_results[round_info.number_of_translators].automaton_stats[1].
    emptiness_check_performed = true;
}

/* ========================================================================= */
void writeFormulaeToFiles()
/* ----------------------------------------------------------------------------
 *
 * Description:   Writes the LTL formulae used in the tests into files in the
 *                output mode specified in the program configuration.
 *
 * Arguments:     None.
 *
 * Returns:       Nothing.
 *
 * ------------------------------------------------------------------------- */
{
  ofstream formula_file;

  for (int f = 0; f < 2; f++)
  {
    Exceptional_ostream eformula_file(&formula_file,
				      ios::failbit | ios::badbit);

    if (!round_info.formula_in_file[f])
    {
      printText(string("<writing ") + (f == 0 ? "posi" : "nega")
		+ "tive formula to `" + round_info.formula_file_name[f]
		+ "'>\n",
		5,
		6);

      try
      {
	openFile(round_info.formula_file_name[f], formula_file,
		 ios::out | ios::trunc, 6);

	round_info.formulae[configuration.formula_options.
			      output_mode == Configuration::NNF
			    ? f
			    : f + 2]->print(eformula_file, ::Ltl::LTL_PREFIX);

	eformula_file << '\n';

	formula_file.close();
	round_info.formula_in_file[f] = true;
      }
      catch (const IOException& e)
      {
	if (formula_file.is_open())
	  formula_file.close();

	printText(string("Error: ") + e.what() + "\n\n", 2, 6);

	round_info.error = true;
	return;
      }
    }
  }
}

/* ========================================================================= */
void generateBuchiAutomaton
  (int f,
   vector<Configuration::AlgorithmInformation,
747
	  ALLOC(Configuration::AlgorithmInformation) >::size_type algorithm_id)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* ----------------------------------------------------------------------------
 *
 * Description:   Constructs a BuchiAutomaton by invoking an external program
 *                that will perform the conversion of a LTL formula (stored
 *                into a file) into a Büchi automaton.
 *
 * Arguments:     f             --  Indicates the formula to be converted into
 *                                  an automaton. 0 corresponds to the positive
 *                                  and 1 to the negated formula.
 *                algorithm_id  --  Identifier of the LTL-to-Büchi translator
 *                                  to use.
 *
 * Returns:       Nothing. The result is stored in
 *                    `test_results[algorithm_id].automaton_stats[f].
 *                       buchi_automaton'.
 *
 * ------------------------------------------------------------------------- */
{
  using ::Graph::BuchiAutomaton;

  AutomatonStats& automaton_stats
    = test_results[algorithm_id].automaton_stats[f];

  if (automaton_stats.buchiAutomatonComputed())
    printText("Büchi automaton (cached):\n", 2, 8);
  else
  {
    if (!printText("Büchi automaton:\n", 3, 8))
      printText("Computing Büchi automaton\n", 2, 8);

    const Configuration::AlgorithmInformation& algorithm
      = configuration.algorithms[algorithm_id];

    final_statistics[algorithm_id].buchi_automaton_count[f]++;

    BuchiAutomaton* buchi_automaton = new BuchiAutomaton();

    struct tms timing_information_begin, timing_information_end;

    int exitcode;
    string command_line;

    try
    {
      /*
       *  Generate the command line to be used for executing the program that
       *  is supposed to generate the automaton from a formula. The name of the
       *  executable and any extra command line parameters are obtained from
       *  the program configuration data structures. The command line will be
       *  of the form
       *    [path to executable] [additional parameters] [input file]
       *                         [output file] 1>[stdout capture file]
       *                         2>[stderr capture file],
       *  so the program used for generating the automaton is assumed to follow
       *  this format for passing the necessary input and output filenames.
       *  Note: This kind of output redirection requires that the call to
       *        `system' invokes a POSIX compatible shell!
       *
       *  The output of stdout and stderr will be redirected to two temporary
       *  files.
       */

      command_line = *(algorithm.path_to_program) + ' ';

      if (algorithm.extra_parameters != 0)
	command_line += *(algorithm.extra_parameters) + ' ';

      command_line += string(round_info.formula_file_name[f])
816
817
818
		      + ' ' + round_info.automaton_file_name
		      + " 1>" + round_info.cout_capture_file
		      + " 2>" + round_info.cerr_capture_file;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840

      if (!printText("<executing `" + command_line + "'>", 5, 10))
	printText("<computing Büchi automaton>", 4, 10);

      /*
       *  Execute the translator and record its running time (user time) into
       *  `automaton_stats.buchi_generation_time'. (The variable will have a
       *  negative value if the time could not be determined.)
       */

      automaton_stats.buchi_generation_time = -1.0;

      times(&timing_information_begin);

      exitcode = system(command_line.c_str());

      times(&timing_information_end);

      if (timing_information_begin.tms_utime != static_cast<clock_t>(-1)
	  && timing_information_begin.tms_cutime != static_cast<clock_t>(-1)
	  && timing_information_end.tms_utime != static_cast<clock_t>(-1)
	  && timing_information_end.tms_cutime != static_cast<clock_t>(-1))
841
	automaton_stats.buchi_generation_time
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
842
	  = static_cast<double>(timing_information_end.tms_utime
843
844
845
				+ timing_information_end.tms_cutime
				- timing_information_begin.tms_utime
				- timing_information_begin.tms_cutime)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
846
847
848
849
850
851
852
853
854
855
	    / sysconf(_SC_CLK_TCK);

      /*
       *  Nonzero exit codes from the external program are interpreted as
       *  errors. In this case, throw an exception indicating that the program
       *  execution failed.
       */

      if (exitcode != 0)
      {
856
857
858
859
860
861
862
863
864
865
866
	/*
	 * system() blocks SIGINT and SIGQUIT.  If the child was killed
	 * by such a signal, forward the signal to the current process.
	 * If lbtt is interactive, SIGINT will be handled as a user
	 * break.  If lbtt is non-interactive, SIGINT will kill lbtt.
	 * This is what we expect when hitting C-c while lbtt is running.
	 */
	if (WIFSIGNALED(exitcode) &&
	    (WTERMSIG(exitcode) == SIGINT || WTERMSIG(exitcode) == SIGQUIT))
	  raise(WTERMSIG(exitcode));

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
867
868
869
870
871
872
873
874
875
	ExecFailedException e;
	e.changeMessage("Execution of `" + *(algorithm.path_to_program)
			+ "' failed"
			+ (automaton_stats.buchi_generation_time >= 0.0
			   ? " ("
			     + toString(automaton_stats.buchi_generation_time,
					2)
			     + " seconds elapsed)"
			   : string(""))
876
			+ " with exit status " + toString(exitcode));
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
	throw e;
      }

      printText(" ok\n", 5);

      /*
       *  Read the automaton description into memory from the result file.
       */

      ifstream automaton_file;
      openFile(round_info.automaton_file_name, automaton_file, ios::in, 10);

      printText("<reading automaton description>", 5, 10);

      automaton_file >> *buchi_automaton;

      printText(" ok\n", 4);

      automaton_file.close();

      automaton_stats.buchi_automaton = buchi_automaton;
    }
    catch (...)
    {
      delete buchi_automaton;

      printText(" error\n", 4);
      printText("Error", 2, 10);

      if (round_info.transcript_file.is_open())
      {
	writeToTranscript("Büchi automaton generation failed ("
909
910
911
			  + configuration.algorithmString(algorithm_id)
			  + ", "
			  + (f == 0 ? "posi" : "nega")
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
912
913
914
915
			  + "tive formula)");

	if (automaton_stats.buchi_generation_time >= 0.0)
	  round_info.transcript_file << string(8, ' ') + "Elapsed time: "
916
					+ toString(automaton_stats.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
917
918
						     buchi_generation_time,
						   2)
919
					+ " seconds (user time)\n";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
920
921
922
923
924
925
926
927
928
929
930
      }

      try
      {
	throw;
      }
      catch (const ExecFailedException& e)
      {
	printText(string(": ") + e.what(), 2);
	if (round_info.transcript_file.is_open())
	  round_info.transcript_file << string(8, ' ')
931
932
933
					+ "Program execution failed with exit "
					  "status "
					+ toString(exitcode);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
934
935
936
937
938
939
      }
      catch (const BuchiAutomaton::AutomatonParseException& e)
      {
	printText(string(" parsing input: ") + e.what(), 2);
	if (round_info.transcript_file.is_open())
	  round_info.transcript_file << string(8, ' ')
940
941
					+ "Error reading automaton: "
					+ e.what();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
942
943
944
945
946
947
      }
      catch (const Exception& e)
      {
	printText(string(": ") + e.what(), 2);
	if (round_info.transcript_file.is_open())
	  round_info.transcript_file << string(8, ' ')
948
949
					+ "lbtt internal error: "
					+ e.what();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
950
951
952
953
954
955
      }
      catch (const bad_alloc&)
      {
	printText(": out of memory", 2);
	if (round_info.transcript_file.is_open())
	  round_info.transcript_file << string(8, ' ')
956
957
					+ "Out of memory while reading "
					  "automaton";
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
      }

      printText("\n", 2);

      if (round_info.transcript_file.is_open())
	round_info.transcript_file << '\n';

      removeFile(round_info.automaton_file_name, 10);

      try
      {
	const char* msg = "Contents of stdout";

	if (configuration.global_options.verbosity >= 3)
	  printFileContents(cout, msg, round_info.cout_capture_file, 10, "> ");
	if (round_info.transcript_file.is_open())
	  printFileContents(round_info.transcript_file, msg,
			    round_info.cout_capture_file, 8, "> ");
      }
      catch (const IOException&)
      {
      }

      try
      {
	const char* msg = "Contents of stderr:";

	if (configuration.global_options.verbosity >= 3)
	  printFileContents(cout, msg, round_info.cerr_capture_file, 10, "> ");
	if (round_info.transcript_file.is_open())
	  printFileContents(round_info.transcript_file, msg,
			    round_info.cerr_capture_file, 8, "> ");
      }
      catch (const IOException&)
      {
      }

      if (round_info.transcript_file.is_open())
	round_info.transcript_file << '\n';

      removeFile(round_info.cout_capture_file, 10);
      removeFile(round_info.cerr_capture_file, 10);