tgbareduc.cc 21 KB
Newer Older
1
// Copyright (C) 2004, 2005, 2008, 2009 Laboratoire d'Informatique de Paris
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
2
3
// 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
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
//
// 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 "tgbareduc.hh"
#include <sstream>

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

  tgba_reduc::tgba_reduc(const tgba* a,
			 const numbered_state_heap_factory* nshf)
34
    : tgba_explicit_string(a->get_dict()),
35
36
37
      tgba_reachable_iterator_breadth_first(a),
      h_(nshf->build())
  {
martinez's avatar
martinez committed
38
    dict_->register_all_variables_of(a, this);
39
40
41
42

    run();
    all_acceptance_conditions_ = a->all_acceptance_conditions();
    all_acceptance_conditions_computed_ = true;
43
    seen_ = 0;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    scc_computed_ = false;
  }

  tgba_reduc::~tgba_reduc()
  {
    sp_map::iterator i;
    for (i = state_predecessor_map_.begin();
	 i!= state_predecessor_map_.end(); ++i)
      {
	delete i->second;
      }

    delete h_;
  }

  void
martinez's avatar
martinez committed
60
  tgba_reduc::quotient_state(direct_simulation_relation* rel)
61
  {
martinez's avatar
martinez committed
62
63
    // Remember that for each state couple
    // (*i)->second simulate (*i)->first.
64

martinez's avatar
martinez committed
65
    for (direct_simulation_relation::iterator i = rel->begin();
66
67
68
69
70
71
72
73
74
	 i != rel->end(); ++i)
      {

	// All state simulate himself.
	if (((*i)->first)->compare((*i)->second) == 0)
	  continue;

	// We check if the two state are co-simulate.
	bool recip = false;
martinez's avatar
martinez committed
75
	for (direct_simulation_relation::iterator j = i;
76
77
78
79
80
	     j != rel->end(); ++j)
	  if ((((*i)->first)->compare((*j)->second) == 0) &&
	      (((*j)->first)->compare((*i)->second) == 0))
	    recip = true;

martinez's avatar
martinez committed
81
82
83
	if (recip)
	  //this->redirect_transition((*i)->first, (*i)->second);
	  this->merge_state((*i)->first, (*i)->second);
84
85
86
87
88
89
      }

    this->merge_transitions();
  }

  void
martinez's avatar
martinez committed
90
  tgba_reduc::quotient_state(delayed_simulation_relation* rel)
91
  {
martinez's avatar
martinez committed
92
93
    if (nb_set_acc_cond() > 1)
      return;
94

martinez's avatar
martinez committed
95
96
97
    //this->quotient_state(rel);

    for (delayed_simulation_relation::iterator i = rel->begin();
98
99
100
101
102
103
104
105
106
	 i != rel->end(); ++i)
      {

	// All state simulate himself.
	if (((*i)->first)->compare((*i)->second) == 0)
	  continue;

	// We check if the two state are co-simulate.
	bool recip = false;
martinez's avatar
martinez committed
107
	for (delayed_simulation_relation::iterator j = i;
108
109
110
111
112
113
114
115
116
117
118
119
	     j != rel->end(); ++j)
	  if ((((*i)->first)->compare((*j)->second) == 0) &&
	      (((*j)->first)->compare((*i)->second) == 0))
	    recip = true;

	if (recip)
	  this->merge_state((*i)->first, (*i)->second);
      }

    this->merge_transitions();
  }

martinez's avatar
martinez committed
120
121
122
123
124
125
126
127
128
129
130
131
132
  void
  tgba_reduc::delete_transitions(simulation_relation* rel)
  {
    for (simulation_relation::iterator i = rel->begin();
	 i != rel->end(); ++i)
      {
	if (((*i)->first)->compare((*i)->second) == 0)
	  continue;
	this->redirect_transition((*i)->first, (*i)->second);
      }
    this->merge_transitions();
  }

133
134
135
136
137
  void
  tgba_reduc::prune_scc()
  {
    if (!scc_computed_)
      this->compute_scc();
martinez's avatar
martinez committed
138
139
    this->prune_acc();
    this->delete_scc();
martinez's avatar
martinez committed
140
141

    this->merge_transitions();
142
143
144
145
146
147
148
149
150
  }

  std::string
  tgba_reduc::format_state(const spot::state* s) const
  {
    std::ostringstream os;
    const state_explicit* se = dynamic_cast<const state_explicit*>(s);
    assert(se);
    sn_map::const_iterator i = state_name_map_.find(se->get_state());
151
    seen_map::const_iterator j = si_.find(s);
152
153
154
155
156
157
158
    assert(i != state_name_map_.end());
    if (j != si_.end()) // SCC have been computed
      {
	os << ", SCC " << j->second;
	return i->second + std::string(os.str());
      }
    else
159
      return i->second;
160
161
162
  }

  ////////////////////////////////////////////
martinez's avatar
martinez committed
163
  // for build tgba_reduc
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

  void
  tgba_reduc::start()
  {
  }

  void
  tgba_reduc::end()
  {
  }

  void
  tgba_reduc::process_state(const spot::state* s, int, tgba_succ_iterator* si)
  {
    spot::state* init = automata_->get_init_state();
    if (init->compare(s) == 0)
      this->set_init_state(automata_->format_state(s));
    delete init;

    transition* t;
    for (si->first(); !si->done(); si->next())
      {
	init = si->current_state();
	t = this->create_transition(s, init);
	this->add_conditions(t, si->current_condition());
	this->add_acceptance_conditions(t, si->current_acceptance_conditions());
	delete init;
      }
  }

  void
  tgba_reduc::process_link(int, int, const tgba_succ_iterator*)
  {
  }

  tgba_explicit::transition*
  tgba_reduc::create_transition(const spot::state* source,
				const spot::state* dest)
  {
    const std::string ss = automata_->format_state(source);
    const std::string sd = automata_->format_state(dest);

206
207
    tgba_explicit::state* s = tgba_explicit_string::add_state(ss);
    tgba_explicit::state* d = tgba_explicit_string::add_state(sd);
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
240

    transition* t = new transition();
    t->dest = d;

    sp_map::iterator i = state_predecessor_map_.find(d);
    if (i == state_predecessor_map_.end())
      {
	std::list<state*>* pred = new std::list<state*>;
	pred->push_back(s);
	state_predecessor_map_[d] = pred;
      }
    else
      {
	(i->second)->push_back(s);
      }

    t->condition = bddtrue;
    t->acceptance_conditions = bddfalse;
    s->push_back(t);

    return t;
  }

  ///////////////////////////////////////////////////

  void
  tgba_reduc::redirect_transition(const spot::state* s,
				  const spot::state* simul)
  {
    bool belong = false;
    bdd cond_simul;
    bdd acc_simul;
    std::list<state*> ltmp;
241
    const tgba_explicit::state* s1 =
242
      name_state_map_[tgba_explicit_string::format_state(s)];
243
    const tgba_explicit::state* s2 =
244
      name_state_map_[tgba_explicit_string::format_state(simul)];
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

    sp_map::iterator i = state_predecessor_map_.find(s1);
    if (i == state_predecessor_map_.end()) // 0 predecessor
	return;

    // for all predecessor of s.
    for (std::list<state*>::iterator p = (i->second)->begin();
	 p != (i->second)->end(); ++p)
      {

	// We check if simul belong to the successor of p,
	// as s belong too.
	for (tgba_explicit::state::iterator j = (*p)->begin();
	     j != (*p)->end(); ++j)
	  if ((*j)->dest == s2) // simul belong to the successor of p.
	    {
	      belong = true;
	      cond_simul = (*j)->condition;
	      acc_simul = (*j)->acceptance_conditions;
	      break;
	    }

	// If not, we check for another predecessor of s.
	if (!belong)
	  continue;

	// for all successor of p, a predecessor of s and simul.
	for (tgba_explicit::state::iterator j = (*p)->begin();
	     j != (*p)->end(); ++j)
	  {
	    // if the transition lead to s.
	    if (((*j)->dest == s1) &&
		// if the label of the transition whose lead to s implies
		// this leading to simul.
		(((!(*j)->condition | cond_simul) == bddtrue) &&
		 ((!(*j)->acceptance_conditions) | acc_simul) == bddtrue))
	      {
		// We can redirect transition leading to s on simul.
		(*j)->dest = const_cast<tgba_explicit::state*>(s2);

		// We memorize that we have to remove p
		// of the predecessor of s.
		ltmp.push_back(*p);
	      }
	  }
	belong = false;
      }

    // We remove all the predecessor of s than we have memorized.
    std::list<state*>::iterator k;
    for (k = ltmp.begin();
	 k != ltmp.end(); ++k)
      this->remove_predecessor_state(i->first, *k);
  }

  void
  tgba_reduc::remove_predecessor_state(const state* s, const state* p)
  {
    sp_map::iterator i = state_predecessor_map_.find(s);
    if (i == state_predecessor_map_.end()) // 0 predecessor
      return;

    // for all predecessor of s we remove p.
    for (std::list<state*>::iterator j = (i->second)->begin();
	 j != (i->second)->end();)
      if (p == *j)
	j = (i->second)->erase(j);
      else
	++j;
  }

  void
  tgba_reduc::remove_state(const spot::state* s)
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
319
320
321
    // We suppose that the state is not reachable when called by
    // merge_state => NO PREDECESSOR.  But it can be have some
    // predecessor in state_predecessor_map_.
322

323
    ns_map::iterator k =
324
      name_state_map_.find(tgba_explicit_string::format_state(s));
325
326
327
    if (k == name_state_map_.end()) // 0 predecessor
	return;

328
    tgba_explicit::state* st =
329
      name_state_map_[tgba_explicit_string::format_state(s)];
330
331

    // for all successor q of s, we remove s of the predecessor of q.
332
    // Note that the initial node can't be removed.
333
334
335
336
337
338
339
340
    for (state::iterator j = st->begin(); j != st->end(); ++j)
      this->remove_predecessor_state((*j)->dest, st);


    sp_map::iterator i = state_predecessor_map_.find(st);
    if (i == state_predecessor_map_.end()) // 0 predecessor
	return;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
341
    // for all predecessor of s (none when called by merge_state)
342
343
344
345
346
347
348
349
350
351
    for (std::list<state*>::iterator p = (i->second)->begin();
	 p != (i->second)->end(); ++p)
      {
	// for all transition of p, a predecessor of s.
	for (state::iterator j = (*p)->begin();
	     j != (*p)->end();)
	  {
	    if ((*j)->dest == st)
	      {
		// Remove the transition
352
		delete *j;
353
354
355
356
357
358
359
360
361
		j = (*p)->erase(j);
		++j;
	      }
	    else
	      ++j;
	  }
      }

    // DESTROY THE STATE !? USELESS
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
362
    // it will be destroyed when the automaton is deleted
363
364
365
366
367
368
369
    // name_state_map_::iterator = name_state_map_[st];
    // const tgba_explicit::state* st = name_state_map_[this->format_state(s)];
  }

  void
  tgba_reduc::merge_state(const spot::state* sim1, const spot::state* sim2)
  {
370
    const tgba_explicit::state* s1 =
371
      name_state_map_[tgba_explicit_string::format_state(sim1)];
372
    const tgba_explicit::state* s2 =
373
      name_state_map_[tgba_explicit_string::format_state(sim2)];
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
    const tgba_explicit::state* stmp = s1;
    const spot::state* simtmp = sim1;

    // if sim1 is the init state, we remove sim2.
    spot::state* init = this->get_init_state();
    if (sim1->compare(init) == 0)
      {
	s1 = s2;
	s2 = stmp;
	sim1 = sim2;
	sim2 = simtmp;
      }
    delete init;

    sp_map::iterator i = state_predecessor_map_.find(s1);
    if (i == state_predecessor_map_.end()) // 0 predecessor
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
391
	// We can remove s1 safely, without changing the language
392
393
394
395
396
397
	// of the automaton.
	this->remove_state(sim1);
	return;
      }

    // for all predecessor of s1, not the initial state,
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
398
    // we redirect to s2 the transitions that lead to s1.
399
400
401
402
403
404
405
    for (std::list<state*>::iterator p = (i->second)->begin();
	 p != (i->second)->end(); ++p)
      {
	// for all successor of p, a predecessor of s1.
	for (tgba_explicit::state::iterator j = (*p)->begin();
	     j != (*p)->end(); ++j)
	  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
406
	    // if the successor was s1...
407
408
	    if ((*j)->dest == s1)
	      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
409
		// ... make it s2.
410
411
412
413
414
		(*j)->dest = const_cast<tgba_explicit::state*>(s2);
	      }
	  }
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
415
416
417
418
419
420
    // FIXME: The following justification sounds really dubious.
    //
    // We have to stock on s2 the acceptance condition of the arc
    // leaving s1 (possible when the simulation is delayed). Since s2
    // simulates s1, s2 has some labels that imply these of s1, so we
    // can put the acceptance conditions on its arcs.
martinez's avatar
martinez committed
421
422
423
424
425
426
427
428
429
430
    for (tgba_explicit::state::const_iterator j = s1->begin();
	 j != s1->end(); ++j)
      {
	transition* t = new transition();
	t->dest = (*j)->dest;
	t->condition = (*j)->condition;
	t->acceptance_conditions = (*j)->acceptance_conditions;
	const_cast<tgba_explicit::state*>(s2)->push_back(t);
      }

431
432
433
    // We remove all the predecessor of s1.
    (i->second)->clear();

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
434
    // then we can remove s1 safely, without changing the language
435
436
437
438
    // of the automaton.
    this->remove_state(sim1);
  }

martinez's avatar
martinez committed
439
440
441
442
  void
  tgba_reduc::merge_state_delayed(const spot::state*,
				  const spot::state*)
  {
martinez's avatar
martinez committed
443
    // TO DO
martinez's avatar
martinez committed
444
445
  }

martinez's avatar
martinez committed
446
  /////////////////////////////////////////
447
  /////////////////////////////////////////
martinez's avatar
martinez committed
448
  // Compute SCC
449

450
  // From gtec.cc
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
  void
  tgba_reduc::remove_component(const spot::state* from)
  {
    std::stack<tgba_succ_iterator*> to_remove;

    numbered_state_heap::state_index_p spi = h_->index(from);
    assert(spi.first);
    assert(*spi.second != -1);
    *spi.second = -1;
    tgba_succ_iterator* i = this->succ_iter(from);

    for (;;)
      {
	for (i->first(); !i->done(); i->next())
	  {
	    spot::state* s = i->current_state();
	    numbered_state_heap::state_index_p spi = h_->index(s);

	    if (!spi.first)
	      continue;

	    if (*spi.second != -1)
	      {
		*spi.second = -1;
		to_remove.push(this->succ_iter(spi.first));
	      }
	  }
	delete i;
	if (to_remove.empty())
	  break;
	i = to_remove.top();
	to_remove.pop();
      }
  }

486
  // From gtec.cc
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
577
578
579
580
581
582
  void
  tgba_reduc::compute_scc()
  {
    std::stack<bdd> arc;
    int num = 1;
    std::stack<pair_state_iter> todo;


    {
      spot::state* init = this->get_init_state();
      h_->insert(init, 1);
      si_[init] = 1;
      state_scc_.push(init);
      root_.push(1);
      arc.push(bddfalse);
      tgba_succ_iterator* iter = this->succ_iter(init);
      iter->first();
      todo.push(pair_state_iter(init, iter));
    }

    while (!todo.empty())
      {
	assert(root_.size() == arc.size());

	tgba_succ_iterator* succ = todo.top().second;

	if (succ->done())
	  {
	    const spot::state* curr = todo.top().first;

	    todo.pop();

	    numbered_state_heap::state_index_p spi = h_->index(curr);
	    assert(spi.first);
	    assert(!root_.empty());
	    if (root_.top().index == *spi.second)
	      {
		assert(!arc.empty());
		arc.pop();
		root_.pop();
		remove_component(curr);
	      }

	    delete succ;

	    continue;
	  }

	const spot::state* dest = succ->current_state();
	bdd acc = succ->current_acceptance_conditions();
	succ->next();

	numbered_state_heap::state_index_p spi = h_->find(dest);
	if (!spi.first)
	  {
	    h_->insert(dest, ++num);
	    si_[dest] = num;
	    state_scc_.push(dest);
	    root_.push(num);
	    arc.push(acc);
	    tgba_succ_iterator* iter = this->succ_iter(dest);
	    iter->first();
	    todo.push(pair_state_iter(dest, iter));
	    continue;
	  }

	if (*spi.second == -1)
	  continue;

	int threshold = *spi.second;
	while (threshold < root_.top().index)
	  {
	    assert(!root_.empty());
	    assert(!arc.empty());
	    acc |= root_.top().condition;
	    acc |= arc.top();
	    root_.pop();
	    arc.pop();

	    si_[state_scc_.top()] = threshold;
	    state_scc_.pop();
	  }

	root_.top().condition |= acc;

      }

    seen_map::iterator i;
    for (i = si_.begin(); i != si_.end(); ++i)
      {
	state_scc_v_[i->second] = i->first;
      }

    scc_computed_ = true;
  }

martinez's avatar
martinez committed
583
584
585
  ///////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////

586
  void
martinez's avatar
martinez committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  tgba_reduc::prune_acc()
  {
    if (!scc_computed_)
      this->compute_scc();

    Sgi::hash_map<int, const spot::state*>::iterator i;
    for (i = state_scc_v_.begin(); i != state_scc_v_.end(); ++i)
      {
	if (is_not_accepting(i->second))
	    remove_acc(i->second);
      }
  }

  void
  tgba_reduc::remove_acc(const spot::state* s)
  {
    tgba_explicit::state* s1;
    seen_map::iterator sm = si_.find(s);
    sm = si_.find(s);
    int n = sm->second;

    for (sm == si_.begin(); sm != si_.end(); ++sm)
      {

	if (sm->second == n)
	  {
613
	    s1 = name_state_map_[tgba_explicit_string::format_state(s)];
martinez's avatar
martinez committed
614
615
616
617
618
619
620
621
622
623
624
	    s1 = const_cast<tgba_explicit::state*>(s1);
	    for (state::iterator i = s1->begin();
		 i != s1->end(); ++i)
	      (*i)->acceptance_conditions = bddfalse;
	  }
      }
  }

  bool
  tgba_reduc::is_not_accepting(const spot::state* s,
			       int n)
625
  {
martinez's avatar
martinez committed
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
    bool b = false;

    // First call of is_terminal //
    seen_map::const_iterator i;
    if (n == -1)
      {
	acc_ = bddfalse;
	b = true;
	assert(seen_ == 0);
	seen_ = new seen_map();
	i = si_.find(s);
	assert(i->first != 0);
	n = i->second;
      }
    ///////////////////////////////
641

martinez's avatar
martinez committed
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
    seen_map::const_iterator sm = seen_->find(s);
    if (sm == seen_->end())
      {
	// this state is visited for the first time.
	seen_->insert(std::pair<const spot::state*, int>(s, 1));
	i = si_.find(s);
	assert(i->first != 0);
	if (n != i->second)
	    return true;
      }
    else
      // This state is already visited.
      {
	delete s;
	s = 0;
	return true;
      }

    spot::state* s2;
    tgba_succ_iterator* j = this->succ_iter(s);
    for (j->first(); !j->done(); j->next())
      {
	s2 = j->current_state();
	i = si_.find(s2);
	assert(i->first != 0);
	if (n == i->second)
	  {
	    acc_ |= j->current_acceptance_conditions();
	    this->is_not_accepting(s2, n);
	  }
      }
    delete j;

    // First call of is_terminal //
    if (b)
      {
	for (seen_map::iterator i = seen_->begin();
	     i != seen_->end(); ++i)
	  {
	    s2 = const_cast<spot::state*>(i->first);
	    assert(s2 != 0);
	    delete dynamic_cast<tgba_explicit*>(s2);
	  }
	seen_->clear();
	delete seen_;
	seen_ = 0;
	return acc_ != this->all_acceptance_conditions();
      }
    ///////////////////////////////

    return true;
  }

  void
  tgba_reduc::delete_scc()
  {
698
699
    bool change = true;
    Sgi::hash_map<int, const spot::state*>::iterator i;
700
    spot::state* s;
701
702
703
704
705
706
707

    // we check if there is a terminal SCC we can be remove while
    // they have been one removed, because a terminal SCC removed
    // can generate a new terminal SCC
    while (change)
      {
	change = false;
708
	for (i = state_scc_v_.begin(); i != state_scc_v_.end(); ++i)
709
	  {
710
	    s = (i->second)->clone();
martinez's avatar
martinez committed
711

712
	    if (is_terminal(s))
713
	      {
714
		change = true;
martinez's avatar
martinez committed
715
		remove_scc(const_cast<spot::state*>(i->second));
716
717
		state_scc_v_.erase(i);
		break;
718
	      }
719
	    delete s;
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
	  }
      }
  }

  bool
  tgba_reduc::is_terminal(const spot::state* s, int n)
  {
    // a SCC is terminal if there are no transition
    // leaving the SCC AND she doesn't contain all
    // the acceptance condition.
    // So we can remove it safely without change the
    // automaton language.

    bool b = false;

735
    // First call of is_terminal //
736
737
738
    seen_map::const_iterator i;
    if (n == -1)
      {
739
	acc_ = bddfalse;
740
	b = true;
741
	assert(seen_ == 0);
742
743
	seen_ = new seen_map();
	i = si_.find(s);
744
	assert(i->first != 0);
745
746
	n = i->second;
      }
747
    ///////////////////////////////
748
749
750
751
752
753
754
755
756
757
758
759
760

    seen_map::const_iterator sm = seen_->find(s);
    if (sm == seen_->end())
      {
	// this state is visited for the first time.
	seen_->insert(std::pair<const spot::state*, int>(s, 1));
	i = si_.find(s);
	assert(i->first != 0);
	if (n != i->second)
	    return false;
      }
    else
      {
martinez's avatar
martinez committed
761
	// This state is already visited.
762
763
	delete s;
	s = 0;
764
765
766
767
	return true;
      }

    bool ret = true;
768
    spot::state* s2;
769
770
771
    tgba_succ_iterator* j = this->succ_iter(s);
    for (j->first(); !j->done(); j->next())
      {
772
	s2 = j->current_state();
773
	acc_ |= j->current_acceptance_conditions();
774
	ret &= this->is_terminal(s2, n);
775
776
	if (!ret)
	  break;
777
      }
778
    delete j;
779

780
    // First call of is_terminal //
781
782
    if (b)
      {
783
784
785
786
787
788
789
790
	for (seen_map::iterator i = seen_->begin();
	     i != seen_->end(); ++i)
	  {
	    s2 = const_cast<spot::state*>(i->first);
	    assert(s2 != 0);
	    delete dynamic_cast<tgba_explicit*>(s2);
	  }
	seen_->clear();
791
	delete seen_;
792
	seen_ = 0;
793
794
795
	if (acc_ == this->all_acceptance_conditions())
	  ret = false;
      }
796
    ///////////////////////////////
797
798
799
800
801
802
803

    return ret;
  }

  void
  tgba_reduc::remove_scc(spot::state* s)
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
804
    // To remove a scc, we remove all its states.
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820

    seen_map::iterator sm = si_.find(s);
    sm = si_.find(s);
    int n = sm->second;

    for (sm == si_.begin(); sm != si_.end(); ++sm)
      {
	if (sm->second == n)
	  {
	    this->remove_state(const_cast<spot::state*>(sm->first));
	    sm->second = -1;
	  }
      }

  }

martinez's avatar
martinez committed
821
822
823
824
825
826
827
828
829
  int
  tgba_reduc::nb_set_acc_cond() const
  {
    bdd acc, all;
    acc = all = this->all_acceptance_conditions();
    int count = 0;
    while (all != bddfalse)
      {
	all -= bdd_satone(all);
830
	++count;
martinez's avatar
martinez committed
831
832
833
      }
    return count;
  }
834

835
836
837
838
839
840
841
842
843
844
845
846
847
  //////// JUST FOR DEBUG //////////

  void
  tgba_reduc::display_rel_sim(simulation_relation* rel,
			      std::ostream& os)
  {
    int n = 0;
    simulation_relation::iterator i;
    for (i = rel->begin(); i != rel->end(); ++i)
      {
	if (((*i)->first)->compare((*i)->second) == 0)
	  continue;

848
	  ++n;
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
	  os << "couple " << n
	     << std::endl
	     << "  " << " [label=\""
	     << this->format_state((*i)->first) << "\"]"
	     << std::endl
	     << "  " << " [label=\""
	     << this->format_state((*i)->second) << "\"]"
	     << std::endl
	     << std::endl;
      }
  }

  void
  tgba_reduc::display_scc(std::ostream& os)
  {


    while (!root_.empty())
      {
	os << "index : " << root_.top().index << std::endl;
	root_.pop();
      }

    seen_map::iterator i;
    for (i = si_.begin(); i != si_.end(); ++i)
      {
	os << " [label=\""
	   << this->format_state(i->first)
	   << "\"]"
	   << " scc : "
	   << i->second
	   << std::endl;
      }

    os << " Root of each SCC :"
       << std::endl;

    Sgi::hash_map<int, const spot::state*>::iterator j;
    for (j = state_scc_v_.begin(); j != state_scc_v_.end(); ++j)
      {
	os << " [label=\""
	   << this->format_state(j->second)
	   << "\"]"
	   << std::endl;
	state_scc_.pop();
      }

  }

}