simulation.cc 23.8 KB
Newer Older
Thomas Badie's avatar
Thomas Badie committed
1
2
3
4
5
6
7
8
// -*- coding: utf-8 -*-
// Copyright (C) 2012 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
//
// 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
Thomas Badie's avatar
Thomas Badie committed
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/>.
Thomas Badie's avatar
Thomas Badie committed
19
20
21
22
23
24
25
26

#include <queue>
#include <map>
#include <utility>
#include "tgba/tgbaexplicit.hh"
#include "simulation.hh"
#include "misc/acccompl.hh"
#include "misc/minato.hh"
Thomas Badie's avatar
Thomas Badie committed
27
#include "misc/unique_ptr.hh"
Thomas Badie's avatar
Thomas Badie committed
28
29
#include "tgba/bddprint.hh"
#include "tgbaalgos/reachiter.hh"
30
#include "tgbaalgos/sccfilter.hh"
Thomas Badie's avatar
Thomas Badie committed
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

// The way we developed this algorithm is the following: We take an
// automaton, and reverse all these acceptance conditions.  We reverse
// them to go make the meaning of the signature easier. We are using
// bdd, and we want to let it make all the simplification. Because of
// the format of the acceptance condition, it doesn't allow easy
// simplification. Instead of encoding them as: "a!b!c + !ab!c", we
// use them as: "ab". We complement them because we want a
// simplification if the condition of the transition A implies the
// transition of B, and if the acceptance condition of A is included
// in the acceptance condition of B. To let the bdd makes the job, we
// revert them.

// Then, to check if a transition i-dominates another, we'll use the bdd:
// "sig(transA) = cond(trans) & acc(trans) & implied(class(trans->state))"
// Idem for sig(transB). The 'implied'
// (represented by a hash table 'relation_' in the implementation) is
// a conjunction of all the class dominated by the class of the
// destination. This is how the relation is included in the
// signature. It makes the simplifications alone, and the work is
// done.  The algorithm is cut into several step:
//
// 1. Run through the tgba and switch the acceptance condition to their
//    negation, and initializing relation_ by the 'init_ -> init_' where
//    init_ is the bdd which represents the class. This function is the
//    constructor of Simulation.
// 2. Enter in the loop (run).
//    - Rename the class.
//    - run through the automaton and computing the signature of each
//      state. This function is `update_sig'.
//    - Enter in a double loop to adapt the partial order, and set
//      'relation_' accordingly. This function is `update_po'.
63
// 3. Rename the class (to actualize the name in the previous_class and
Thomas Badie's avatar
Thomas Badie committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//    in relation_).
// 4. Building an automaton with the result, with the condition:
// "a transition in the original automaton appears in the simulated one
// iff this transition is included in the set of i-maximal neighbour."
// This function is `build_output'.
// The automaton simulated is recomplemented to come back to its initial
// state when the object Simulation is destroyed.
//
// Obviously these functions are possibly cut into several little one.
// This is just the general development idea.

// How to use isop:
// I need all variable non_acceptance & non_class.
// bdd_support(sig(X)): All var
// bdd_support(sig(X)) - allacc - allclassvar


Thomas Badie's avatar
Thomas Badie committed
81
82
83
84
85
86
87
// We have had the Cosimulation by changing the acc_compl_automaton by
// adding a template parameter. If this parameter is set to true, we
// record the transition in the opposite direction (we just swap
// sources and destination). In the build result we are making the
// same thing to rebuild the automaton.
// In the signature,

Thomas Badie's avatar
Thomas Badie committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
namespace spot
{
  namespace
  {
    // Some useful typedef:

    // Used to get the signature of the state.
    typedef Sgi::hash_map<const state*, bdd,
                          state_ptr_hash,
                          state_ptr_equal> map_state_bdd;

    typedef Sgi::hash_map<const state*, unsigned,
                          state_ptr_hash,
                          state_ptr_equal> map_state_unsigned;

    // Get the list of state for each class.
    typedef std::map<bdd, std::list<const state*>,
                     bdd_less_than> map_bdd_lstate;

107
108
109
110
111
112
113
114
    struct automaton_size
    {
      automaton_size()
        : transitions(0),
          states(0)
      {
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
115
      inline bool operator!=(const automaton_size& r)
116
117
118
119
120
121
122
      {
        return transitions != r.transitions || states != r.states;
      }

      int transitions;
      int states;
    };
Thomas Badie's avatar
Thomas Badie committed
123

124
125
    // This class takes an automaton and creates a copy with all
    // acceptance conditions complemented.
Thomas Badie's avatar
Thomas Badie committed
126
    template <bool Cosimulation>
127
    class acc_compl_automaton:
Thomas Badie's avatar
Thomas Badie committed
128
      public tgba_reachable_iterator_depth_first
129
    {
Thomas Badie's avatar
Thomas Badie committed
130
    public:
131
132
133
134
135
136
137
138
      acc_compl_automaton(const tgba* a)
      : tgba_reachable_iterator_depth_first(a),
	size(0),
	out_(new tgba_explicit_number(a->get_dict())),
	ea_(a),
	ac_(ea_->all_acceptance_conditions(),
	    ea_->neg_acceptance_conditions()),
	current_max(0)
Thomas Badie's avatar
Thomas Badie committed
139
140
141
142
143
144
145
146
      {
        init_ = ea_->get_init_state();
        out_->set_init_state(get_state(init_));
      }

      inline unsigned
      get_state(const state* s)
      {
147
148
	map_state_unsigned::const_iterator i = state2int.find(s);
        if (i == state2int.end())
149
	  {
150
151
152
153
            i = state2int.insert(std::make_pair(s, ++current_max)).first;
            const state* to_add = out_->add_state(current_max);
            previous_class_[to_add] = bddfalse;
            order_.push_back(to_add);
154
	  }
155
        return i->second;
Thomas Badie's avatar
Thomas Badie committed
156
157
158
      }

      void process_link(const state* in_s,
159
                        int,
Thomas Badie's avatar
Thomas Badie committed
160
                        const state* out_s,
161
                        int,
Thomas Badie's avatar
Thomas Badie committed
162
163
164
165
166
                        const tgba_succ_iterator* si)
      {
        int src = get_state(in_s);
        int dst = get_state(out_s);

Thomas Badie's avatar
Thomas Badie committed
167
168
169
170
171
172
173
174
        // In the case of the cosimulation, we want to have all the
        // ingoing transition, and to keep the rest of the code
        // similar, we just create equivalent transition in the other
        // direction. Since we do not have to run through the
        // automaton to get the signature, this is correct.
        if (Cosimulation)
          std::swap(src, dst);

Thomas Badie's avatar
Thomas Badie committed
175
176
177
178
179
180
181
182
183
184
185
186
187
        bdd acc = ac_.complement(si->current_acceptance_conditions());

        tgba_explicit_number::transition* t
          = out_->create_transition(src, dst);
        out_->add_acceptance_conditions(t, acc);
        out_->add_conditions(t, si->current_condition());
      }

      void process_state(const state*, int, tgba_succ_iterator*)
      {
        ++size;
      }

188
      ~acc_compl_automaton()
Thomas Badie's avatar
Thomas Badie committed
189
190
191
192
193
194
195
      {
        init_->destroy();
      }

    public:
      size_t size;
      tgba_explicit_number* out_;
196
      map_state_bdd previous_class_;
197
      std::list<const state*> order_;
Thomas Badie's avatar
Thomas Badie committed
198
199
200

    private:
      const tgba* ea_;
201
      acc_compl ac_;
Thomas Badie's avatar
Thomas Badie committed
202
203
204
      map_state_unsigned state2int;
      unsigned current_max;
      state* init_;
205
    };
Thomas Badie's avatar
Thomas Badie committed
206

Thomas Badie's avatar
Thomas Badie committed
207
    template <bool Cosimulation>
208
    class direct_simulation
Thomas Badie's avatar
Thomas Badie committed
209
    {
210
211
212
213
214
215
216
217
      // Shortcut used in update_po and go_to_next_it.
      typedef std::map<bdd, bdd, bdd_less_than> map_bdd_bdd;
    public:
      direct_simulation(const tgba* t)
      : a_(0),
	po_size_(0),
	all_class_var_(bddtrue)
      {
Thomas Badie's avatar
Thomas Badie committed
218
	acc_compl_automaton<Cosimulation>
219
220
221
222
223
224
225
226
	  acc_compl(t);

	// We'll start our work by replacing all the acceptance
	// conditions by their complement.
	acc_compl.run();

	a_ = acc_compl.out_;

Thomas Badie's avatar
Thomas Badie committed
227
228
        initial_state = a_->get_init_state();

229
230
231
232
233
234
235
236
237
	// We use the previous run to know the size of the
	// automaton, and to class all the reachable states in the
	// map previous_class_.
	size_a_ = acc_compl.size;

	// Now, we have to get the bdd which will represent the
	// class. We register one bdd by state, because in the worst
	// case, |Class| == |State|.
	unsigned set_num = a_->get_dict()
Thomas Badie's avatar
Thomas Badie committed
238
	  ->register_anonymous_variables(size_a_ + 1, a_);
239

Thomas Badie's avatar
Thomas Badie committed
240
241
        bdd_initial = bdd_ithvar(set_num++);
	bdd init = bdd_ithvar(set_num++);
242
243
244
245
246
247
248
249
250
251

	used_var_.push_back(init);

	// We fetch the result the run of acc_compl_automaton which
	// has recorded all the state in a hash table, and we set all
	// to init.
	for (map_state_bdd::iterator it
	       = acc_compl.previous_class_.begin();
	     it != acc_compl.previous_class_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
252
          {
253
            previous_class_[it->first] = init;
Thomas Badie's avatar
Thomas Badie committed
254
255
          }

256
257
258
259
	// Put all the anonymous variable in a queue, and record all
	// of these in a variable all_class_var_ which will be used
	// to understand the destination part in the signature when
	// building the resulting automaton.
260
	all_class_var_ = init;
261
	for (unsigned i = set_num; i < set_num + size_a_ - 1; ++i)
Thomas Badie's avatar
Thomas Badie committed
262
263
264
265
266
          {
            free_var_.push(i);
            all_class_var_ &= bdd_ithvar(i);
          }

267
	relation_[init] = init;
268
269

        order_ = acc_compl.order_;
270
      }
Thomas Badie's avatar
Thomas Badie committed
271
272


273
274
275
276
277
278
279
      // Reverse all the acceptance condition at the destruction of
      // this object, because it occurs after the return of the
      // function simulation.
      ~direct_simulation()
      {
	delete a_;
      }
Thomas Badie's avatar
Thomas Badie committed
280
281


282
283
284
285
286
287
288
289
290
291
      // We update the name of the class.
      void update_previous_class()
      {
	std::list<bdd>::iterator it_bdd = used_var_.begin();

	// We run through the map bdd/list<state>, and we update
	// the previous_class_ with the new data.
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
292
293
294
295
          {
            for (std::list<const state*>::iterator it_s = it->second.begin();
                 it_s != it->second.end();
                 ++it_s)
296
	      {
Thomas Badie's avatar
Thomas Badie committed
297
298
299
300
301
302
		// If the signature of a state is bddfalse (no
		// transitions) the class of this state is bddfalse
		// instead of an anonymous variable. It allows
		// simplifications in the signature by removing a
		// transition which has as a destination a state with
		// no outgoing transition.
303
		if (it->first == bddfalse)
304
                  previous_class_[*it_s] = bddfalse;
305
306
307
		else
		  previous_class_[*it_s] = *it_bdd;
	      }
308
309
            if (it->first != bddfalse)
              ++it_bdd;
Thomas Badie's avatar
Thomas Badie committed
310
          }
311
312
313
314
315
316
317
318
319
      }

      // The core loop of the algorithm.
      tgba* run()
      {
	unsigned int nb_partition_before = 0;
	unsigned int nb_po_before = po_size_ - 1;
	while (nb_partition_before != bdd_lstate_.size()
	       || nb_po_before != po_size_)
Thomas Badie's avatar
Thomas Badie committed
320
          {
321
            update_previous_class();
Thomas Badie's avatar
Thomas Badie committed
322
323
324
325
326
327
328
329
            nb_partition_before = bdd_lstate_.size();
            bdd_lstate_.clear();
            nb_po_before = po_size_;
            po_size_ = 0;
            update_sig();
            go_to_next_it();
          }

330
331
332
	update_previous_class();
	return build_result();
      }
Thomas Badie's avatar
Thomas Badie committed
333

334
335
336
      // Take a state and compute its signature.
      bdd compute_sig(const state* src)
      {
Thomas Badie's avatar
Thomas Badie committed
337
338
        tgba_succ_iterator* sit = a_->succ_iter(src);
        bdd res = bddfalse;
Thomas Badie's avatar
Thomas Badie committed
339

Thomas Badie's avatar
Thomas Badie committed
340
        for (sit->first(); !sit->done(); sit->next())
Thomas Badie's avatar
Thomas Badie committed
341
342
343
344
345
346
347
348
          {
            const state* dst = sit->current_state();
            bdd acc = sit->current_acceptance_conditions();

            // to_add is a conjunction of the acceptance condition,
            // the label of the transition and the class of the
            // destination and all the class it implies.
            bdd to_add = acc & sit->current_condition()
349
              & relation_[previous_class_[dst]];
Thomas Badie's avatar
Thomas Badie committed
350
351
352
353
354

            res |= to_add;
            dst->destroy();
          }

Thomas Badie's avatar
Thomas Badie committed
355
356
357
358
359
360
361
        // When we Cosimulate, we add a special flag to differentiate
        // initial state.
        if (Cosimulation && initial_state == src)
          res |= bdd_initial;

        delete sit;
        return res;
362
363
      }

Thomas Badie's avatar
Thomas Badie committed
364

365
366
367
368
369
370
371
372
373
374
375
      void update_sig()
      {
	// At this time, current_class_ must be empty.  It implies
	// that the "previous_class_ = current_class_" must be
	// done before.
	assert(current_class_.empty());

	// Here we suppose that previous_class_ always contains
	// all the reachable states of this automaton. We do not
	// have to make (again) a traversal. We just have to run
	// through this map.
376
377
378
        for (std::list<const state*>::const_iterator it = order_.begin();
             it != order_.end();
             ++it)
Thomas Badie's avatar
Thomas Badie committed
379
          {
380
            const state* src = previous_class_.find(*it)->first;
Thomas Badie's avatar
Thomas Badie committed
381
382
383

            bdd_lstate_[compute_sig(src)].push_back(src);
          }
384
      }
Thomas Badie's avatar
Thomas Badie committed
385
386


387
388
389
390
      // This method rename the color set, update the partial order.
      void go_to_next_it()
      {
	int nb_new_color = bdd_lstate_.size() - used_var_.size();
Thomas Badie's avatar
Thomas Badie committed
391

392
	for (int i = 0; i < nb_new_color; ++i)
Thomas Badie's avatar
Thomas Badie committed
393
394
395
396
397
398
          {
            assert(!free_var_.empty());
            used_var_.push_back(bdd_ithvar(free_var_.front()));
            free_var_.pop();
          }

399
400
401
402
403
404
405
406
407
408
409
	for (int i = 0; i > nb_new_color; --i)
          {
            assert(!used_var_.empty());
            free_var_.push(bdd_var(used_var_.front()));
            used_var_.pop_front();
          }


	assert((bdd_lstate_.size() == used_var_.size())
               || (bdd_lstate_.find(bddfalse) != bdd_lstate_.end()
                   && bdd_lstate_.size() == used_var_.size() + 1));
Thomas Badie's avatar
Thomas Badie committed
410

411
412
413
414
415
416
417
	// Now we make a temporary hash_table which links the tuple
	// "C^(i-1), N^(i-1)" to the new class coloring.  If we
	// rename the class before updating the partial order, we
	// loose the information, and if we make it after, I can't
	// figure out how to apply this renaming on rel_.
	// It adds a data structure but it solves our problem.
	map_bdd_bdd now_to_next;
Thomas Badie's avatar
Thomas Badie committed
418

419
	std::list<bdd>::iterator it_bdd = used_var_.begin();
Thomas Badie's avatar
Thomas Badie committed
420

421
422
423
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
424
425
426
427
428
429
430
431
          {
            // If the signature of a state is bddfalse (which is
            // roughly equivalent to no transition) the class of
            // this state is bddfalse instead of an anonymous
            // variable. It allows simplifications in the signature
            // by removing a transition which has as a destination a
            // state with no outgoing transition.
            if (it->first == bddfalse)
432
433
434
435
436
              {
                now_to_next[it->first] = bddfalse;
                free_var_.push(bdd_var(*it_bdd));
                it_bdd = used_var_.erase(it_bdd);
              }
Thomas Badie's avatar
Thomas Badie committed
437
            else
438
439
440
441
              {
                now_to_next[it->first] = *it_bdd;
                ++it_bdd;
              }
Thomas Badie's avatar
Thomas Badie committed
442
443
          }

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
	update_po(now_to_next);
      }

      // This function computes the new po with previous_class_
      // and the argument. `now_to_next' contains the relation
      // between the signature and the future name of the class.
      void update_po(const map_bdd_bdd& now_to_next)
      {
	// This loop follows the pattern given by the paper.
	// foreach class do
	// |  foreach class do
	// |  | update po if needed
	// |  od
	// od

	for (map_bdd_bdd::const_iterator it1 = now_to_next.begin();
	     it1 != now_to_next.end();
	     ++it1)
Thomas Badie's avatar
Thomas Badie committed
462
463
464
465
466
          {
            bdd accu = it1->second;
            for (map_bdd_bdd::const_iterator it2 = now_to_next.begin();
                 it2 != now_to_next.end();
                 ++it2)
Thomas Badie's avatar
Thomas Badie committed
467
468
469
470
471
472
473
474
475
476
477
              {
                // Skip the case managed by the initialization of accu.
                if (it1 == it2)
                  continue;

                if (bdd_implies(it1->first, it2->first))
                  {
                    accu &= it2->second;
                    ++po_size_;
                  }
              }
Thomas Badie's avatar
Thomas Badie committed
478
479
            relation_[it1->second] = accu;
          }
480
481
      }

482
483
484
485
486
487
488
489
      automaton_size get_stat() const
      {
        assert(stat.states != 0);

        return stat;
      }


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
      // Build the minimal resulting automaton.
      tgba* build_result()
      {
	// Now we need to create a state per partition. But the
	// problem is that we don't know exactly the class. We know
	// that it is a combination of the acceptance condition
	// contained in all_class_var_. So we need to make a little
	// workaround. We will create a map which will associate bdd
	// and unsigned.
	std::map<bdd, unsigned, bdd_less_than> bdd2state;
	unsigned int current_max = 0;

	bdd all_acceptance_conditions
	  = a_->all_acceptance_conditions();

	// We have all the a_'s acceptances conditions
	// complemented.  So we need to complement it when adding a
	// transition.  We *must* keep the complemented because it
	// is easy to know if an acceptance condition is maximal or
	// not.
	acc_compl reverser(all_acceptance_conditions,
			   a_->neg_acceptance_conditions());

	tgba_explicit_number* res
	  = new tgba_explicit_number(a_->get_dict());
	res->set_acceptance_conditions
	  (all_acceptance_conditions);

	bdd sup_all_acc = bdd_support(all_acceptance_conditions);
	// Non atomic propositions variables (= acc and class)
	bdd nonapvars = sup_all_acc & bdd_support(all_class_var_);

	// Create one state per partition.
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end(); ++it)
Thomas Badie's avatar
Thomas Badie committed
525
526
          {
            res->add_state(++current_max);
527
            bdd part = previous_class_[*it->second.begin()];
Thomas Badie's avatar
Thomas Badie committed
528
529
530
531
532
533
534
535

            // The difference between the two next lines is:
            // the first says "if you see A", the second "if you
            // see A and all the class implied by it".
            bdd2state[part] = current_max;
            bdd2state[relation_[part]] = current_max;
          }

536
537
538
        stat.states = bdd_lstate_.size();
        stat.transitions = 0;

539
540
541
542
543
	// For each partition, we will create
	// all the transitions between the states.
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
544
545
546
547
          {
            // Get the signature.
            bdd sig = compute_sig(*(it->second.begin()));

Thomas Badie's avatar
Thomas Badie committed
548
549
550
            if (Cosimulation)
              sig = bdd_compose(sig, bddfalse, bdd_var(bdd_initial));

Thomas Badie's avatar
Thomas Badie committed
551
552
553
554
555
556
557
558
559
560
561
562
563
            // Get all the variable in the signature.
            bdd sup_sig = bdd_support(sig);

            // Get the variable in the signature which represents the
            // conditions.
            bdd sup_all_atomic_prop = bdd_exist(sup_sig, nonapvars);

            // Get the part of the signature composed only with the atomic
            // proposition.
            bdd all_atomic_prop = bdd_exist(sig, nonapvars);

	    // First loop over all possible valuations atomic properties.
            while (all_atomic_prop != bddfalse)
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
	      {
		bdd one = bdd_satoneset(all_atomic_prop,
					sup_all_atomic_prop,
					bddtrue);
		all_atomic_prop -= one;

		// For each possible valuation, iterator over all possible
		// destination classes.   We use minato_isop here, because
		// if the same valuation of atomic properties can go
		// to two different classes C1 and C2, iterating on
		// C1 + C2 with the above bdd_satoneset loop will see
		// C1 then (!C1)C2, instead of C1 then C2.
		// With minatop_isop, we ensure that the no negative
		// class variable will be seen (likewise for promises).
		minato_isop isop(sig & one);

		bdd cond_acc_dest;
		while ((cond_acc_dest = isop.next()) != bddfalse)
		  {
583
584
                    ++stat.transitions;

585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
		    // Take the transition, and keep only the variable which
		    // are used to represent the class.
		    bdd dest = bdd_existcomp(cond_acc_dest,
					     all_class_var_);

		    // Keep only ones who are acceptance condition.
		    bdd acc = bdd_existcomp(cond_acc_dest, sup_all_acc);

		    // Keep the other !
		    bdd cond = bdd_existcomp(cond_acc_dest,
					     sup_all_atomic_prop);

		    // Because we have complemented all the acceptance
		    // condition on the input automaton, we must re
		    // invert them to create a new transition.
		    acc = reverser.reverse_complement(acc);

		    // Take the id of the source and destination.  To
		    // know the source, we must take a random state in
		    // the list which is in the class we currently
		    // work on.
		    int src = bdd2state[previous_class_[*it->second.begin()]];
		    int dst = bdd2state[dest];

Thomas Badie's avatar
Thomas Badie committed
609
610
611
                    if (Cosimulation)
                      std::swap(src, dst);

612
613
614
615
616
617
618
619
620
621
622
623
624
		    // src or dst == 0 means "dest" or "prev..." isn't
		    // in the map.  so it is a bug.
		    assert(src != 0);
		    assert(dst != 0);

		    // Create the transition, add the condition and the
		    // acceptance condition.
		    tgba_explicit_number::transition* t
		      = res->create_transition(src , dst);
		    res->add_conditions(t, cond);
		    res->add_acceptance_conditions(t, acc);
		  }
	      }
Thomas Badie's avatar
Thomas Badie committed
625
626
          }

627
628
	res->set_init_state(bdd2state[previous_class_
				      [a_->get_init_state()]]);
Thomas Badie's avatar
Thomas Badie committed
629

630
	res->merge_transitions();
Thomas Badie's avatar
Thomas Badie committed
631

632
633
634
635
636
637
638
639
640
641
642
643
644
645
	return res;
      }


      // Debug:
      // In a first time, print the signature, and the print a list
      // of each state in this partition.
      // In a second time, print foreach state, who is where,
      // where is the new class name.
      void print_partition()
      {
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
646
647
648
649
650
651
652
          {
            std::cerr << "partition: "
                      << bdd_format_set(a_->get_dict(), it->first) << std::endl;

            for (std::list<const state*>::iterator it_s = it->second.begin();
                 it_s != it->second.end();
                 ++it_s)
653
654
655
656
	      {
		std::cerr << "  - "
			  << a_->format_state(*it_s) << std::endl;
	      }
Thomas Badie's avatar
Thomas Badie committed
657
658
          }

659
	std::cerr << "\nPrevious iteration\n" << std::endl;
Thomas Badie's avatar
Thomas Badie committed
660

661
662
663
	for (map_state_bdd::const_iterator it = previous_class_.begin();
	     it != previous_class_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
664
665
666
667
668
669
          {
            std::cerr << a_->format_state(it->first)
                      << " was in "
                      << bdd_format_set(a_->get_dict(), it->second)
                      << std::endl;
          }
670
      }
Thomas Badie's avatar
Thomas Badie committed
671

672
673
674
    private:
      // The automaton which is simulated.
      tgba_explicit_number* a_;
Thomas Badie's avatar
Thomas Badie committed
675

676
677
678
679
680
681
682
      // Relation is aimed to represent the same thing than
      // rel_. The difference is in the way it does.
      // If A => A /\ A => B, rel will be (!A U B), but relation_
      // will have A /\ B at the key A. This trick is due to a problem
      // with the computation of the resulting automaton with the signature.
      // rel_ will pollute the meaning of the signature.
      map_bdd_bdd relation_;
Thomas Badie's avatar
Thomas Badie committed
683

684
685
      // Represent the class of each state at the previous iteration.
      map_state_bdd previous_class_;
Thomas Badie's avatar
Thomas Badie committed
686

687
688
      // The class at the current iteration.
      map_state_bdd current_class_;
Thomas Badie's avatar
Thomas Badie committed
689

690
691
692
      // The list of state for each class at the current_iteration.
      // Computed in `update_sig'.
      map_bdd_lstate bdd_lstate_;
Thomas Badie's avatar
Thomas Badie committed
693

694
695
696
      // The queue of free bdd. They will be used as the identifier
      // for the class.
      std::queue<int> free_var_;
Thomas Badie's avatar
Thomas Badie committed
697

698
699
      // The list of used bdd. They are in used as identifier for class.
      std::list<bdd> used_var_;
Thomas Badie's avatar
Thomas Badie committed
700

701
702
      // Size of the automaton.
      unsigned int size_a_;
Thomas Badie's avatar
Thomas Badie committed
703

704
705
706
      // Used to know when there is no evolution in the po. Updated
      // in the `update_po' method.
      unsigned int po_size_;
Thomas Badie's avatar
Thomas Badie committed
707

708
709
      // All the class variable:
      bdd all_class_var_;
Thomas Badie's avatar
Thomas Badie committed
710
711
712
713
714
715
716

      // The flag to say if the outgoing state is initial or not
      bdd bdd_initial;

      // Initial state of the automaton we are working on
      state* initial_state;

717
      automaton_size stat;
718
719
720

      // The order of the state.
      std::list<const state*> order_;
Thomas Badie's avatar
Thomas Badie committed
721
    };
Thomas Badie's avatar
Thomas Badie committed
722

Thomas Badie's avatar
Thomas Badie committed
723
724
725
726
727
  } // End namespace anonymous.

  tgba*
  simulation(const tgba* t)
  {
Thomas Badie's avatar
Thomas Badie committed
728
729
730
731
732
733
734
735
736
    direct_simulation<false> simul(t);

    return simul.run();
  }

  tgba*
  cosimulation(const tgba* t)
  {
    direct_simulation<true> simul(t);
Thomas Badie's avatar
Thomas Badie committed
737
738
739
740

    return simul.run();
  }

741
742
743
744
745
746
747
748
749
750
751
752
  tgba*
  iterated_simulations(const tgba* t)
  {
    tgba* res = const_cast<tgba*> (t);
    automaton_size prev;
    automaton_size next;

    do
      {
        prev = next;
        direct_simulation<false> simul(res);

Thomas Badie's avatar
Thomas Badie committed
753
        unique_ptr<tgba> after_simulation(simul.run());
754
755
756
757
758
759

        if (res != t)
          delete res;

        direct_simulation<true> cosimul(after_simulation);

Thomas Badie's avatar
Thomas Badie committed
760
        unique_ptr<tgba> after_cosimulation(cosimul.run());
761
762
763
764
765
766
767
768
769
770
771

        next = cosimul.get_stat();

        res = scc_filter(after_cosimulation, false);
      }
    while (prev != next);

    return res;
  }


Thomas Badie's avatar
Thomas Badie committed
772
} // End namespace spot.