ce.cc 16.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (C) 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie.
//
// 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
// the Free Software Foundation; either version 2 of the License, or
// (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
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#include "ce.hh"
#include "tgba/bddprint.hh"
#include <map>

namespace spot
{
  namespace
  {
    typedef std::pair<const spot::state*, tgba_succ_iterator*> pair_state_iter;
  }

  counter_example::counter_example(const emptiness_check_status* ecs,
				   const explicit_connected_component_factory*
				   eccf)
    : ecs_(ecs)
  {
martinez's avatar
martinez committed
38
39
40
41

    std::cout << "counter_example" << std::endl;

    //counter_ = new ce::counter_example(ecs->aut);
martinez's avatar
martinez committed
42

43
44
45
46
47
48
49
    assert(!ecs_->root.empty());
    assert(suffix.empty());

    scc_stack::stack_type root = ecs_->root.s;
    int comp_size = root.size();
    // Transform the stack of connected component into an array.
    explicit_connected_component** scc =
50
      new explicit_connected_component*[comp_size];
51
52
53
54
55
56
57
58
59
60
    for (int j = comp_size - 1; 0 <= j; --j)
      {
	scc[j] = eccf->build();
	scc[j]->index = root.top().index;
	scc[j]->condition = root.top().condition;
	root.pop();
      }
    assert(root.empty());

    // Build the set of states for all SCCs.
61
    numbered_state_heap_const_iterator* i = ecs_->h->iterator();
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    for (i->first(); !i->done(); i->next())
      {
	int index = i->get_index();
	// Skip states from dead SCCs.
	if (index < 0)
	  continue;
	assert(index != 0);

	// Find the SCC this state belongs to.
	int j;
	for (j = 1; j < comp_size; ++j)
	  if (index < scc[j]->index)
	    break;
	scc[j - 1]->insert(i->get_state());
      }
    delete i;

79
80
81
82
    numbered_state_heap::state_index_p spi =
      ecs_->h->index(ecs_->aut->get_init_state());
    assert(spi.first);
    suffix.push_front(spi.first);
83

martinez's avatar
martinez committed
84
    /////
martinez's avatar
martinez committed
85
    // counter_->prefix.push_front(ce::state_ce(spi.first->clone(), bddfalse));
martinez's avatar
martinez committed
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
    // We build a path trough each SCC in the stack.  For the
    // first SCC, the starting state is the initial state of the
    // automaton.  The destination state is the closest state
    // from the next SCC.  This destination state becomes the
    // starting state when building a path though the next SCC.
    for (int k = 0; k < comp_size - 1; ++k)
      {
	// FIFO for the breadth-first search.
	// (we are looking for the closest state in the next SCC.)
	std::deque<pair_state_iter> todo;

	// Record the father of each state, while performing the BFS.
	typedef std::map<const state*, const state*,
	                 state_ptr_less_than> father_map;
	father_map father;

	// Initial state of the BFS.
	const state* start = suffix.back();
	{
	  tgba_succ_iterator* i = ecs_->aut->succ_iter(start);
	  todo.push_back(pair_state_iter(start, i));
	}

	while (!todo.empty())
	  {
	    const state* src = todo.front().first;
	    tgba_succ_iterator* i = todo.front().second;
	    todo.pop_front();

	    for (i->first(); !i->done(); i->next())
	      {
		const state* dest = i->current_state();

		// Are we leaving this SCC?
		const state* h_dest = scc[k]->has_state(dest);
		if (!h_dest)
		  {
martinez's avatar
martinez committed
125
		    // If we have found a state in greater SCC which.
126
127
128
129
130
131
		    // Unwind the path and populate SUFFIX.
		    h_dest = scc[k+1]->has_state(dest);
		    if (h_dest)
		      {
			state_sequence seq;

martinez's avatar
martinez committed
132
			///
martinez's avatar
martinez committed
133
			// ce::l_state_ce seq_count;
martinez's avatar
martinez committed
134
135
			///

136
137
138
			seq.push_front(h_dest);
			while (src->compare(start))
			  {
martinez's avatar
martinez committed
139
			    ///
martinez's avatar
martinez committed
140
141
			    // seq_count.push_front(ce::state_ce(src->clone(),
			    // bddfalse));
martinez's avatar
martinez committed
142
143
			    ///

144
145
146
147
148
			    seq.push_front(src);
			    src = father[src];
			  }
			// Append SEQ to SUFFIX.
			suffix.splice(suffix.end(), seq);
martinez's avatar
martinez committed
149
150

			///
martinez's avatar
martinez committed
151
152
			// counter_->prefix.splice(counter_->prefix.end(),
			// seq_count);
martinez's avatar
martinez committed
153
154
			///

155
156
157
158
159
160
161
162
163
			// Exit this BFS for this SCC.
			while (!todo.empty())
			  {
			    delete todo.front().second;
			    todo.pop_front();
			  }
			break;
		      }
		    // Restrict the BFS to state inside the SCC.
164
		    delete dest;
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
		    continue;
		  }

		if (father.find(h_dest) == father.end())
		  {
		    todo.push_back
		      (pair_state_iter(h_dest, ecs_->aut->succ_iter(h_dest)));
		    father[h_dest] = src;
		  }
	      }
	    delete i;
	  }
      }

    accepting_path(scc[comp_size - 1], suffix.back(),
		   scc[comp_size - 1]->condition);


    for (int j = comp_size - 1; 0 <= j; --j)
      delete scc[j];
    delete[] scc;
  }

  void
  counter_example::complete_cycle(const explicit_connected_component* scc,
				  const state* from,
				  const state* to)
  {
martinez's avatar
martinez committed
193
194
    //std::cout << "complete_cycle" << std::endl;

195
196
197
    // If by change or period already ends on the state we have
    // to reach back, we are done.
    if (from == to
198
	&& !period.empty())
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
      return;

    // Records backlinks to parent state during the BFS.
    // (This also stores the propositions of this link.)
    std::map<const state*, state_proposition, state_ptr_less_than> father;

    // BFS queue.
    std::deque<pair_state_iter> todo;

    // Initial state.
    {
      tgba_succ_iterator* i = ecs_->aut->succ_iter(from);
      todo.push_back(pair_state_iter(from, i));
    }

    while (!todo.empty())
      {
	const state* src = todo.front().first;
	tgba_succ_iterator* i = todo.front().second;
	todo.pop_front();
	for (i->first(); !i->done(); i->next())
	  {
	    const state* dest = i->current_state();

	    // Do not escape this SCC or visit a state already visited.
	    const state* h_dest = scc->has_state(dest);
	    if (!h_dest)
	      {
		delete dest;
		continue;
	      }
	    if (father.find(h_dest) != father.end())
	      continue;

	    bdd cond = i->current_condition();

	    // If we have reached our destination, unwind the path
	    // and populate PERIOD.
	    if (h_dest == to)
	      {
		cycle_path p;
martinez's avatar
martinez committed
240
241

		///
martinez's avatar
martinez committed
242
243
		// ce::l_state_ce p_counter;
		// p_counter.push_front(ce::state_ce(h_dest->clone(), cond));
martinez's avatar
martinez committed
244
245
		///

246
247
248
249
		p.push_front(state_proposition(h_dest, cond));
		while (src != from)
		  {
		    const state_proposition& psi = father[src];
martinez's avatar
martinez committed
250
		    ///
martinez's avatar
martinez committed
251
252
		    // p_counter.push_front(ce::state_ce(src->clone(),
		    // psi.second));
martinez's avatar
martinez committed
253
		    ///
254
255
256
257
		    p.push_front(state_proposition(src, psi.second));
		    src = psi.first;
		  }
		period.splice(period.end(), p);
martinez's avatar
martinez committed
258
		///
martinez's avatar
martinez committed
259
260
		// counter_->cycle.splice(counter_->cycle.end(),
		// p_counter);
martinez's avatar
martinez committed
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

		// Exit the BFS, but release all iterators first.
		while (!todo.empty())
		  {
		    delete todo.front().second;
		    todo.pop_front();
		  }
		break;
	      }

	    // Common case: record backlinks and continue BFS.
	    todo.push_back(pair_state_iter(h_dest,
					   ecs_->aut->succ_iter(h_dest)));
	    father[h_dest] = state_proposition(src, cond);
	  }
	delete i;
      }
  }


  namespace
  {
    struct triplet
    {
      const state* s;		// Current state.
      tgba_succ_iterator* iter;	// Iterator to successor of the current state.
      bdd acc;			// All acceptance conditions traversed by
				// the path so far.

      triplet (const state* s, tgba_succ_iterator* iter, bdd acc)
	: s(s), iter(iter), acc(acc)
      {
      }
    };

  }

martinez's avatar
martinez committed
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  ////////////////////////////////////////////////////////////////////////
  /*

  void
  counter_example::accepting_path(const explicit_connected_component* scc,
				  const state* start, bdd acc_to_traverse)
  {
    // State seen during the DFS.
    typedef Sgi::hash_set<const state*,
                          state_ptr_hash, state_ptr_equal> set_type;
    set_type seen;
    // DFS stack.
    std::stack<triplet> todo;

    while (acc_to_traverse != bddfalse)
      {
	// Initial state.
	{
	  tgba_succ_iterator* i = ecs_->aut->succ_iter(start);
	  i->first();
	  todo.push(triplet(start, i, bddfalse));
	  seen.insert(start);
	}

	// The path being explored currently.
	cycle_path path;
	// The best path seen so far.
	cycle_path best_path;
	// The acceptance conditions traversed by BEST_PATH.
	bdd best_acc = bddfalse;

	while (!todo.empty())
	  {
	    tgba_succ_iterator* iter = todo.top().iter;
	    const state* s = todo.top().s;

	    // Nothing more to explore, backtrack.
	    if (iter->done())
	      {
		todo.pop();
		delete iter;
		seen.erase(s);
341
		if (!todo.empty())
martinez's avatar
martinez committed
342
		  {
343
		    assert(!path.empty());
martinez's avatar
martinez committed
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
372
373
374
375
376
377
378
379
		    path.pop_back();
		  }
		continue;
	      }

	    // We must not escape the current SCC.
	    const state* dest = iter->current_state();
	    const state* h_dest = scc->has_state(dest);
	    if (!h_dest)
	      {
		delete dest;
		iter->next();
		continue;
	      }

	    bdd acc = iter->current_acceptance_conditions() | todo.top().acc;
	    path.push_back(state_proposition(h_dest,
					     iter->current_condition()));

	    // Advance iterator for next step.
	    iter->next();

	    if (seen.find(h_dest) == seen.end())
	      {
		// A new state: continue the DFS.
		tgba_succ_iterator* di = ecs_->aut->succ_iter(h_dest);
		di->first();
		todo.push(triplet(h_dest, di, acc));
		seen.insert(h_dest);
		continue;
	      }

	    // We have completed a full cycle.

	    // If we already have a best path, let see if the current
	    // one is better.
380
	    if (!best_path.empty())
martinez's avatar
martinez committed
381
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
412
413
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
	      {
		// When comparing the merits of two paths, only the
		// acceptance conditions we are trying the traverse
		// are important.
		bdd acc_restrict = acc & acc_to_traverse;
		bdd best_acc_restrict = best_acc & acc_to_traverse;

		// If the best path and the current one traverse the
		// same acceptance conditions, we keep the shorter
		// path.  Otherwise, we keep the path which has the
		// more acceptance conditions.
		if (best_acc_restrict == acc_restrict)
		  {
		    if (best_path.size() <= path.size())
		      goto backtrack_path;
		  }
		else
		  {
		    // `best_acc_restrict >> acc_restrict' is true
		    // when the set of acceptance conditions of
		    // best_acc_restrict is included in the set of
		    // acceptance conditions of acc_restrict.
		    //
		    // FIXME: It would be better to count the number
		    // of acceptance conditions.
		    if (bddtrue != (best_acc_restrict >> acc_restrict))
		      goto backtrack_path;
		  }
	      }

	    // The current path the best one.
	    best_path = path;
	    best_acc = acc;

	  backtrack_path:
	    // Continue exploration from parent to find better paths.
	    // (Do not pop PATH if ITER is done, because that will be
	    // done at the top of the loop, among other things.)
	    if (!iter->done())
	      path.pop_back();
	  }

	// Append our best path to the period.
	for (cycle_path::iterator it = best_path.begin();
	     it != best_path.end(); ++it)
	  {
	    period.push_back(*it);
	    ce::state_ce ce(it->first, it->second);
	    counter_->cycle.push_back(ce);
	    counter_->cycle.push_back(*it);
	  }

	// Prepare to find another path for the remaining acceptance
	// conditions.
	acc_to_traverse -= best_acc;
	start = period.back().first;
      }

    // Complete the path so that it goes back to its beginning,
    // forming a cycle.
    complete_cycle(scc, start, suffix.back());
  }

  */
  ////////////////////////////////////////////////////////////////////////

447
448
449
450
  void
  counter_example::accepting_path(const explicit_connected_component* scc,
				  const state* start, bdd acc_to_traverse)
  {
martinez's avatar
martinez committed
451
452
    //std::cout << "accepting_path" << std::endl;

453
454
455
456
457
458
459
460
461
    // State seen during the DFS.
    typedef Sgi::hash_set<const state*,
                          state_ptr_hash, state_ptr_equal> set_type;
    set_type seen;
    // DFS stack.
    std::stack<triplet> todo;

    while (acc_to_traverse != bddfalse)
      {
martinez's avatar
martinez committed
462
463
464
	//std::cout << "accepting_path : while (acc_to_traverse != bddfalse)"
	// << std::endl;

465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
	// Initial state.
	{
	  tgba_succ_iterator* i = ecs_->aut->succ_iter(start);
	  i->first();
	  todo.push(triplet(start, i, bddfalse));
	  seen.insert(start);
	}

	// The path being explored currently.
	cycle_path path;
	// The best path seen so far.
	cycle_path best_path;
	// The acceptance conditions traversed by BEST_PATH.
	bdd best_acc = bddfalse;

	while (!todo.empty())
	  {
martinez's avatar
martinez committed
482
483
484
	    // std::cout << "accepting_path : while (!todo.empty())"
	    // << std::endl;

485
486
487
488
489
490
491
492
493
	    tgba_succ_iterator* iter = todo.top().iter;
	    const state* s = todo.top().s;

	    // Nothing more to explore, backtrack.
	    if (iter->done())
	      {
		todo.pop();
		delete iter;
		seen.erase(s);
494
		if (!todo.empty())
495
		  {
496
		    assert(!path.empty());
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
		    path.pop_back();
		  }
		continue;
	      }

	    // We must not escape the current SCC.
	    const state* dest = iter->current_state();
	    const state* h_dest = scc->has_state(dest);
	    if (!h_dest)
	      {
		delete dest;
		iter->next();
		continue;
	      }

	    bdd acc = iter->current_acceptance_conditions() | todo.top().acc;
	    path.push_back(state_proposition(h_dest,
					     iter->current_condition()));

	    // Advance iterator for next step.
	    iter->next();

	    if (seen.find(h_dest) == seen.end())
	      {
		// A new state: continue the DFS.
		tgba_succ_iterator* di = ecs_->aut->succ_iter(h_dest);
		di->first();
		todo.push(triplet(h_dest, di, acc));
		seen.insert(h_dest);
		continue;
	      }

	    // We have completed a full cycle.

	    // If we already have a best path, let see if the current
	    // one is better.
533
	    if (!best_path.empty())
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
577
578
	      {
		// When comparing the merits of two paths, only the
		// acceptance conditions we are trying the traverse
		// are important.
		bdd acc_restrict = acc & acc_to_traverse;
		bdd best_acc_restrict = best_acc & acc_to_traverse;

		// If the best path and the current one traverse the
		// same acceptance conditions, we keep the shorter
		// path.  Otherwise, we keep the path which has the
		// more acceptance conditions.
		if (best_acc_restrict == acc_restrict)
		  {
		    if (best_path.size() <= path.size())
		      goto backtrack_path;
		  }
		else
		  {
		    // `best_acc_restrict >> acc_restrict' is true
		    // when the set of acceptance conditions of
		    // best_acc_restrict is included in the set of
		    // acceptance conditions of acc_restrict.
		    //
		    // FIXME: It would be better to count the number
		    // of acceptance conditions.
		    if (bddtrue != (best_acc_restrict >> acc_restrict))
		      goto backtrack_path;
		  }
	      }

	    // The current path the best one.
	    best_path = path;
	    best_acc = acc;

	  backtrack_path:
	    // Continue exploration from parent to find better paths.
	    // (Do not pop PATH if ITER is done, because that will be
	    // done at the top of the loop, among other things.)
	    if (!iter->done())
	      path.pop_back();
	  }

	// Append our best path to the period.
	for (cycle_path::iterator it = best_path.begin();
	     it != best_path.end(); ++it)
martinez's avatar
martinez committed
579
580
	  {
	    period.push_back(*it);
martinez's avatar
martinez committed
581
582
	    //ce::state_ce ce(it->first->clone(), it->second);
	    //counter_->cycle.push_back(ce);
martinez's avatar
martinez committed
583
584
	    //counter_->cycle.push_back(*it);
	  }
585
586
587
588
589
590
591
592
593
594
595
596

	// Prepare to find another path for the remaining acceptance
	// conditions.
	acc_to_traverse -= best_acc;
	start = period.back().first;
      }

    // Complete the path so that it goes back to its beginning,
    // forming a cycle.
    complete_cycle(scc, start, suffix.back());
  }

martinez's avatar
martinez committed
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
  std::ostream&
  counter_example::print_result(std::ostream& os, const tgba* restrict) const
  {
    os << "Prefix:" << std::endl;
    const bdd_dict* d = ecs_->aut->get_dict();
    for (state_sequence::const_iterator i_se = suffix.begin();
	 i_se != suffix.end(); ++i_se)
      {
	os << "  ";
	if (restrict)
	  {
	    const state* s = ecs_->aut->project_state(*i_se, restrict);
	    assert(s);
	    os << restrict->format_state(s) << std::endl;
	    delete s;
	  }
	else
	  {
	    os << ecs_->aut->format_state(*i_se) << std::endl;
	  }
      }
    os << "Cycle:" <<std::endl;
    for (cycle_path::const_iterator it = period.begin();
	 it != period.end(); ++it)
      {
	os << "    | " << bdd_format_set(d, it->second) << std::endl;
	os << "  ";
	if (restrict)
	  {
	    const state* s = ecs_->aut->project_state(it->first, restrict);
	    assert(s);
	    os << restrict->format_state(s) << std::endl;
	    delete s;
	  }
	else
	  {
	    os << ecs_->aut->format_state(it->first) << std::endl;
	  }
      }
    return os;
  }


  void
  counter_example::print_stats(std::ostream& os) const
  {
    ecs_->print_stats(os);
    os << suffix.size() << " states in suffix" << std::endl;
    os << period.size() << " states in period" << std::endl;
  }

martinez's avatar
martinez committed
650
651
652
653
654
655
  ce::counter_example*
  counter_example::get_counter_example() const
  {
    return counter_;
  }

656
}