simulation.cc 51.2 KB
Newer Older
Thomas Badie's avatar
Thomas Badie committed
1
// -*- coding: utf-8 -*-
Thomas Badie's avatar
Thomas Badie committed
2
3
// Copyright (C) 2012, 2013 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
Thomas Badie's avatar
Thomas Badie committed
4
5
6
7
8
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 3 of the License, or
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

#include <queue>
#include <map>
#include <utility>
Thomas Badie's avatar
Thomas Badie committed
23
24
#include <cmath>
#include <limits>
Thomas Badie's avatar
Thomas Badie committed
25
26
#include "tgba/tgbaexplicit.hh"
#include "simulation.hh"
27
#include "priv/acccompl.hh"
Thomas Badie's avatar
Thomas Badie committed
28
#include "misc/minato.hh"
Thomas Badie's avatar
Thomas Badie committed
29
#include "misc/unique_ptr.hh"
Thomas Badie's avatar
Thomas Badie committed
30
31
#include "tgba/bddprint.hh"
#include "tgbaalgos/reachiter.hh"
32
#include "tgbaalgos/sccfilter.hh"
Thomas Badie's avatar
Thomas Badie committed
33
34
35
#include "tgbaalgos/scc.hh"
#include "tgbaalgos/dupexp.hh"
#include "tgbaalgos/dotty.hh"
Thomas Badie's avatar
Thomas Badie committed
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

// 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'.
68
// 3. Rename the class (to actualize the name in the previous_class and
Thomas Badie's avatar
Thomas Badie committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//    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
86
87
88
89
90
91
92
// 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
93
94
95
96
// TODO LIST: Play on the order of the selection in the
// dont_care_simulation. The good place to work is in add_to_map_imply.


Thomas Badie's avatar
Thomas Badie committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;

Thomas Badie's avatar
Thomas Badie committed
112
113
114
115
    typedef std::map<const state*, const state*,
                     state_ptr_less_than> map_state_state;


Thomas Badie's avatar
Thomas Badie committed
116
117
118
119
    // Get the list of state for each class.
    typedef std::map<bdd, std::list<const state*>,
                     bdd_less_than> map_bdd_lstate;

Thomas Badie's avatar
Thomas Badie committed
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
    typedef std::map<bdd, const state*,
                     bdd_less_than> map_bdd_state;

    // Our constraint: (state_src, state_dst) = to_add.
    // We define the couple of state as the key of the constraint.
    typedef std::pair<const state*, const state*> constraint_key;

    // But we need a comparator for that key.
    struct constraint_key_comparator
    {
      bool operator()(const constraint_key& l,
                      const constraint_key& r) const
      {
        if (l.first->compare(r.first) < 0)
          return true;
        else
          if (l.first->compare(r.first) > 0)
            return false;

        if (l.second->compare(r.second) < 0)
          return true;
        else
          if (l.second->compare(r.second) > 0)
            return false;

        return false;
      }
    };

    // The full definition of the constraint.
    typedef std::map<constraint_key, bdd,
                     constraint_key_comparator> map_constraint;

    typedef std::pair<constraint_key, bdd> constraint;

    // Helper to create the map of constraints to give to the
    // simulation.
    void add_to_map(const std::list<constraint>& list,
                    map_constraint& feed_me)
    {
      for (std::list<constraint>::const_iterator it = list.begin();
           it != list.end();
           ++it)
        {
          if (feed_me.find(it->first) == feed_me.end())
            feed_me[it->first] = it->second;
        }
    }


    // This class helps to compare two automata in term of
    // size.
172
173
174
175
176
177
178
179
    struct automaton_size
    {
      automaton_size()
        : transitions(0),
          states(0)
      {
      }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
180
      inline bool operator!=(const automaton_size& r)
181
182
183
184
      {
        return transitions != r.transitions || states != r.states;
      }

Thomas Badie's avatar
Thomas Badie committed
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
212
213
214
      inline bool operator<(const automaton_size& r)
      {
        if (states < r.states)
          return true;
        if (states > r.states)
          return false;

        if (transitions < r.transitions)
          return true;
        if (transitions > r.transitions)
          return false;

        return false;
      }

      inline bool operator>(const automaton_size& r)
      {
        if (states < r.states)
          return false;
        if (states > r.states)
          return true;

        if (transitions < r.transitions)
          return false;
        if (transitions > r.transitions)
          return true;

        return false;
      }

215
216
217
      int transitions;
      int states;
    };
Thomas Badie's avatar
Thomas Badie committed
218

Thomas Badie's avatar
Thomas Badie committed
219
220
221
222
223
224
    // This class takes an automaton, and return a (maybe new)
    // automaton. If Cosimulation is equal to true, we create a new
    // automaton. Otherwise, we create a new one.  The returned
    // automaton is similar to the old one, except that the acceptance
    // condition on the transitions are complemented.
    // There is a specialization below.
225
    template <bool Cosimulation, bool Sba>
226
    class acc_compl_automaton:
Thomas Badie's avatar
Thomas Badie committed
227
      public tgba_reachable_iterator_depth_first
228
    {
Thomas Badie's avatar
Thomas Badie committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    public:
       acc_compl_automaton(const tgba* a)
       : tgba_reachable_iterator_depth_first(a),
 	size(0),
 	ea_(down_cast<tgba_explicit_number*>(const_cast<tgba*>(a))),
 	ac_(ea_->all_acceptance_conditions(),
 	    ea_->neg_acceptance_conditions())
       {
         assert(ea_);
         out_ = ea_;
       }

       void process_link(const state*, int,
                         const state*, int,
                         const tgba_succ_iterator* si)
       {
245
         bdd acc = ac_.complement(si->current_acceptance_conditions());
Thomas Badie's avatar
Thomas Badie committed
246
247

         typename tgba_explicit_number::transition* t =
248
           ea_->get_transition(si);
Thomas Badie's avatar
Thomas Badie committed
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

         t->acceptance_conditions = acc;
       }

       void process_state(const state* s, int, tgba_succ_iterator*)
       {
         ++size;
         previous_class_[s] = bddfalse;
         old_name_[s] = s;
         order_.push_back(s);
       }

       ~acc_compl_automaton()
       {
       }

    public:
      size_t size;
      tgba_explicit_number* out_;
      map_state_bdd previous_class_;
      std::list<const state*> order_;
      map_state_state old_name_;

    private:
      tgba_explicit_number* ea_;
      acc_compl ac_;
    };

277
278
279
280
    // The specialization for Cosimulation equals to true: We copy the
    // automaton and transpose it at the same time.
    template <bool Sba>
    class acc_compl_automaton<true, Sba>:
Thomas Badie's avatar
Thomas Badie committed
281
282
      public tgba_reachable_iterator_depth_first
    {
Thomas Badie's avatar
Thomas Badie committed
283
    public:
284
285
286
      acc_compl_automaton(const tgba* a)
      : tgba_reachable_iterator_depth_first(a),
	size(0),
Thomas Badie's avatar
Thomas Badie committed
287
        out_(new tgba_explicit_number(a->get_dict())),
288
289
	ac_(a->all_acceptance_conditions(),
	    a->neg_acceptance_conditions()),
290
	current_max(0)
Thomas Badie's avatar
Thomas Badie committed
291
      {
292
293
294
295
	a->get_dict()->register_all_variables_of(a, out_);
	out_->set_acceptance_conditions(a->all_acceptance_conditions());

        const state* init_ = a->get_init_state();
Thomas Badie's avatar
Thomas Badie committed
296
        out_->set_init_state(get_state(init_));
297
	init_->destroy();
Thomas Badie's avatar
Thomas Badie committed
298
299
300
301
302
      }

      inline unsigned
      get_state(const state* s)
      {
303
304
	map_state_unsigned::const_iterator i = state2int.find(s);
        if (i == state2int.end())
305
	  {
Thomas Badie's avatar
Thomas Badie committed
306
307
308
309
310
	    i = state2int.insert(std::make_pair(s, ++current_max)).first;
            state* in_new_aut = out_->add_state(current_max);
	    previous_class_[in_new_aut] = bddfalse;
            old_name_[in_new_aut] = s;
            order_.push_back(in_new_aut);
311
	  }
312
        return i->second;
Thomas Badie's avatar
Thomas Badie committed
313
314
315
      }

      void process_link(const state* in_s,
316
                        int,
Thomas Badie's avatar
Thomas Badie committed
317
                        const state* out_s,
318
                        int,
Thomas Badie's avatar
Thomas Badie committed
319
320
                        const tgba_succ_iterator* si)
      {
Thomas Badie's avatar
Thomas Badie committed
321
322
        unsigned src = get_state(in_s);
        unsigned dst = get_state(out_s);
Thomas Badie's avatar
Thomas Badie committed
323

324
	// Note the order of src and dst: the transition is reversed.
Thomas Badie's avatar
Thomas Badie committed
325
        tgba_explicit_number::transition* t
326
           = out_->create_transition(dst, src);
Thomas Badie's avatar
Thomas Badie committed
327

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
        t->condition = si->current_condition();
	if (!Sba)
	  {
	    bdd acc = ac_.complement(si->current_acceptance_conditions());
	    t->acceptance_conditions = acc;
	  }
	else
	  {
	    // If the acceptance is interpreted as state-based, to
	    // apply the reverse simulation on a SBA, we should pull
	    // the acceptance of the destination state on its incoming
	    // arcs (which now become outgoing args after
	    // transposition).
	    tgba_succ_iterator* it = out_->succ_iter(out_s);
	    it->first();
	    if (!it->done())
	      {
		bdd acc = ac_.complement(it->current_acceptance_conditions());
		t->acceptance_conditions = acc;
	      }
	    delete it;
	  }
Thomas Badie's avatar
Thomas Badie committed
350
351
352
353
354
355
356
      }

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

357
      ~acc_compl_automaton()
Thomas Badie's avatar
Thomas Badie committed
358
359
360
361
362
363
      {
      }

    public:
      size_t size;
      tgba_explicit_number* out_;
364
      map_state_bdd previous_class_;
365
      std::list<const state*> order_;
Thomas Badie's avatar
Thomas Badie committed
366
      map_state_state old_name_;
Thomas Badie's avatar
Thomas Badie committed
367
368

    private:
369
      acc_compl ac_;
Thomas Badie's avatar
Thomas Badie committed
370
371
      map_state_unsigned state2int;
      unsigned current_max;
372
    };
Thomas Badie's avatar
Thomas Badie committed
373

Thomas Badie's avatar
Thomas Badie committed
374
    // The direct_simulation. If Cosimulation is true, we are doing a
375
376
    // cosimulation.
    template <bool Cosimulation, bool Sba>
377
    class direct_simulation
Thomas Badie's avatar
Thomas Badie committed
378
    {
Thomas Badie's avatar
Thomas Badie committed
379
    protected:
380
381
382
      // Shortcut used in update_po and go_to_next_it.
      typedef std::map<bdd, bdd, bdd_less_than> map_bdd_bdd;
    public:
Thomas Badie's avatar
Thomas Badie committed
383
384
385
386
387
388
389
      direct_simulation(const tgba* t, const map_constraint* map_cst = 0)
        : a_(0),
          po_size_(0),
          all_class_var_(bddtrue),
          map_cst_(map_cst),
          original_(t),
          dont_delete_old_(false)
390
      {
Thomas Badie's avatar
Thomas Badie committed
391
392
393
394
395
396
397
398
399
400
        // We need to do a dupexp for being able to run scc_map later.
        // new_original_ is the map that contains the relation between
        // the names (addresses) of the states in the automaton
        // returned by dupexp, and in automaton given in argument to
        // the constructor.
        a_ = tgba_dupexp_dfs(t, new_original_);
        scc_map_ = new scc_map(a_);
        scc_map_->build_map();
        old_a_ = a_;

401
	acc_compl_automaton<Cosimulation, Sba> acc_compl(a_);
402
403
404
405
406

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

Thomas Badie's avatar
Thomas Badie committed
407
408
        // Contains the relation between the names of the states in
        // the automaton returned by the complementation and the one
409
        // passed to the constructor of acc_compl.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
410
	std::swap(old_name_, acc_compl.old_name_);
Thomas Badie's avatar
Thomas Badie committed
411

412
413
	a_ = acc_compl.out_;

Thomas Badie's avatar
Thomas Badie committed
414
415
        initial_state = a_->get_init_state();

416
417
418
419
420
421
422
423
424
	// 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()
425
	  ->register_anonymous_variables(size_a_ + 1, this);
426

427
428
        all_acceptance_conditions_ = a_->all_acceptance_conditions();
        all_proms_ = bdd_support(all_acceptance_conditions_);
Thomas Badie's avatar
Thomas Badie committed
429

Thomas Badie's avatar
Thomas Badie committed
430
431
        bdd_initial = bdd_ithvar(set_num++);
	bdd init = bdd_ithvar(set_num++);
432
433
434
435
436
437
438
439
440
441

	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
442
          {
443
            previous_class_[it->first] = init;
Thomas Badie's avatar
Thomas Badie committed
444
445
          }

446
447
448
449
	// 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.
450
	all_class_var_ = init;
451
	for (unsigned i = set_num; i < set_num + size_a_ - 1; ++i)
Thomas Badie's avatar
Thomas Badie committed
452
453
454
455
456
          {
            free_var_.push(i);
            all_class_var_ &= bdd_ithvar(i);
          }

457
	relation_[init] = init;
458

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
459
	std::swap(order_, acc_compl.order_);
460
      }
Thomas Badie's avatar
Thomas Badie committed
461
462


463
464
465
      // Reverse all the acceptance condition at the destruction of
      // this object, because it occurs after the return of the
      // function simulation.
Thomas Badie's avatar
Thomas Badie committed
466
      virtual ~direct_simulation()
467
      {
468
	a_->get_dict()->unregister_all_my_variables(this);
Thomas Badie's avatar
Thomas Badie committed
469
        delete scc_map_;
470

Thomas Badie's avatar
Thomas Badie committed
471
        if (!dont_delete_old_)
472
473
	  delete old_a_;
        // a_ is a new automaton only if we are doing a cosimulation.
Thomas Badie's avatar
Thomas Badie committed
474
475
        if (Cosimulation)
          delete a_;
476
      }
Thomas Badie's avatar
Thomas Badie committed
477

478
      // Update the name of the classes.
479
480
481
482
483
484
485
486
487
      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
488
489
490
491
          {
            for (std::list<const state*>::iterator it_s = it->second.begin();
                 it_s != it->second.end();
                 ++it_s)
492
	      {
Thomas Badie's avatar
Thomas Badie committed
493
494
495
496
497
498
		// 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.
499
		if (it->first == bddfalse)
500
                  previous_class_[*it_s] = bddfalse;
501
502
503
		else
		  previous_class_[*it_s] = *it_bdd;
	      }
504
              ++it_bdd;
Thomas Badie's avatar
Thomas Badie committed
505
          }
506
507
      }

Thomas Badie's avatar
Thomas Badie committed
508
      void main_loop()
509
510
511
512
513
      {
	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
514
          {
515
            update_previous_class();
Thomas Badie's avatar
Thomas Badie committed
516
517
518
519
520
521
522
523
            nb_partition_before = bdd_lstate_.size();
            bdd_lstate_.clear();
            nb_po_before = po_size_;
            po_size_ = 0;
            update_sig();
            go_to_next_it();
          }

524
	update_previous_class();
Thomas Badie's avatar
Thomas Badie committed
525
526
527
528
529
530
      }

      // The core loop of the algorithm.
      tgba* run()
      {
        main_loop();
531
532
	return build_result();
      }
Thomas Badie's avatar
Thomas Badie committed
533

534
535
536
      // Take a state and compute its signature.
      bdd compute_sig(const state* src)
      {
Thomas Badie's avatar
Thomas Badie committed
537
538
        tgba_succ_iterator* sit = a_->succ_iter(src);
        bdd res = bddfalse;
Thomas Badie's avatar
Thomas Badie committed
539

Thomas Badie's avatar
Thomas Badie committed
540
        for (sit->first(); !sit->done(); sit->next())
Thomas Badie's avatar
Thomas Badie committed
541
542
          {
            const state* dst = sit->current_state();
Thomas Badie's avatar
Thomas Badie committed
543
            bdd acc = bddtrue;
Thomas Badie's avatar
Thomas Badie committed
544

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
	    map_constraint::const_iterator it;
	    // We are using new_original_[old_name_[...]] because
	    // we have the constraints in the original automaton
	    // which has been duplicated twice to get the current
	    // automaton.
	    if (map_cst_
		&& ((it = map_cst_
		     ->find(std::make_pair(new_original_[old_name_[src]],
					   new_original_[old_name_[dst]])))
		    != map_cst_->end()))
	      {
		acc = it->second;
	      }
	    else
	      {
		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()
	      & relation_[previous_class_[dst]];
Thomas Badie's avatar
Thomas Badie committed
568

569
570
571
	    res |= to_add;
	    dst->destroy();
	  }
Thomas Badie's avatar
Thomas Badie committed
572

Thomas Badie's avatar
Thomas Badie committed
573
        // When we Cosimulate, we add a special flag to differentiate
574
        // the initial state from the other.
Thomas Badie's avatar
Thomas Badie committed
575
576
577
578
579
        if (Cosimulation && initial_state == src)
          res |= bdd_initial;

        delete sit;
        return res;
580
581
      }

Thomas Badie's avatar
Thomas Badie committed
582

583
584
585
586
587
588
      void update_sig()
      {
	// 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.
589
590
591
        for (std::list<const state*>::const_iterator it = order_.begin();
             it != order_.end();
             ++it)
Thomas Badie's avatar
Thomas Badie committed
592
          {
593
            const state* src = previous_class_.find(*it)->first;
Thomas Badie's avatar
Thomas Badie committed
594
595
596

            bdd_lstate_[compute_sig(src)].push_back(src);
          }
597
      }
Thomas Badie's avatar
Thomas Badie committed
598
599


600
601
602
603
      // 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
604

Thomas Badie's avatar
Thomas Badie committed
605
606
607

        // If we have created more partitions, we need to use more
        // variables.
608
	for (int i = 0; i < nb_new_color; ++i)
Thomas Badie's avatar
Thomas Badie committed
609
610
611
612
613
614
          {
            assert(!free_var_.empty());
            used_var_.push_back(bdd_ithvar(free_var_.front()));
            free_var_.pop();
          }

Thomas Badie's avatar
Thomas Badie committed
615
616
617

        // If we have reduced the number of partition, we 'free' them
        // in the free_var_ list.
618
619
620
621
622
623
624
625
626
627
628
	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
629

630
631
632
633
634
635
636
	// 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
637

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

640
641
642
	for (map_bdd_lstate::iterator it = bdd_lstate_.begin();
	     it != bdd_lstate_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
643
644
645
646
647
648
649
650
          {
            // 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)
651
                now_to_next[it->first] = bddfalse;
Thomas Badie's avatar
Thomas Badie committed
652
            else
Thomas Badie's avatar
Thomas Badie committed
653
654
655
              now_to_next[it->first] = *it_bdd;

            ++it_bdd;
Thomas Badie's avatar
Thomas Badie committed
656
657
          }

Thomas Badie's avatar
Thomas Badie committed
658
	update_po(now_to_next, relation_);
659
660
      }

Thomas Badie's avatar
Thomas Badie committed
661
662
663
664
665
666
667
668
669
      // 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.  We need a
      // template parameter because we use this function with a
      // map_bdd_bdd, but later, we need a list_bdd_bdd. So to
      // factorize some code we use a template.
      template <typename container_bdd_bdd>
      void update_po(const container_bdd_bdd& now_to_next,
                     map_bdd_bdd& relation)
670
671
672
673
674
675
676
677
      {
	// This loop follows the pattern given by the paper.
	// foreach class do
	// |  foreach class do
	// |  | update po if needed
	// |  od
	// od

Thomas Badie's avatar
Thomas Badie committed
678
679
	for (typename container_bdd_bdd::const_iterator it1
               = now_to_next.begin();
680
681
	     it1 != now_to_next.end();
	     ++it1)
Thomas Badie's avatar
Thomas Badie committed
682
683
          {
            bdd accu = it1->second;
Thomas Badie's avatar
Thomas Badie committed
684
685
            for (typename container_bdd_bdd::const_iterator it2
                   = now_to_next.begin();
Thomas Badie's avatar
Thomas Badie committed
686
687
                 it2 != now_to_next.end();
                 ++it2)
Thomas Badie's avatar
Thomas Badie committed
688
689
690
691
692
693
694
695
696
697
698
              {
                // 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
699
            relation[it1->second] = accu;
Thomas Badie's avatar
Thomas Badie committed
700
          }
701
702
      }

703
704
705
706
707
708
709
      automaton_size get_stat() const
      {
        assert(stat.states != 0);

        return stat;
      }

Thomas Badie's avatar
Thomas Badie committed
710
711
712
713
714
715
716
717
      bool result_is_deterministic() const
      {
        assert(stat.states != 0);

        return res_is_deterministic;
      }


718

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
      // 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;

	// 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.
Thomas Badie's avatar
Thomas Badie committed
736
	acc_compl reverser(all_acceptance_conditions_,
737
738
			   a_->neg_acceptance_conditions());

739
740
741
742
	bdd_dict* d = a_->get_dict();
	tgba_explicit_number* res = new tgba_explicit_number(d);
	d->register_all_variables_of(a_, res);
	res->set_acceptance_conditions(all_acceptance_conditions_);
743

Thomas Badie's avatar
Thomas Badie committed
744
	bdd sup_all_acc = bdd_support(all_acceptance_conditions_);
745
746
747
748
749
750
	// 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
751
752
          {
            res->add_state(++current_max);
753
            bdd part = previous_class_[*it->second.begin()];
Thomas Badie's avatar
Thomas Badie committed
754
755
756

            // The difference between the two next lines is:
            // the first says "if you see A", the second "if you
757
            // see A and all the classes implied by it".
Thomas Badie's avatar
Thomas Badie committed
758
759
760
761
            bdd2state[part] = current_max;
            bdd2state[relation_[part]] = current_max;
          }

762
763
764
765
766
	// Acceptance of states.  Only used if Sba && Cosimulation.
	std::vector<bdd> accst;
	if (Sba && Cosimulation)
	  accst.resize(current_max + 1, bddfalse);

767
768
769
        stat.states = bdd_lstate_.size();
        stat.transitions = 0;

Thomas Badie's avatar
Thomas Badie committed
770
771
772
        unsigned nb_satoneset = 0;
        unsigned nb_minato = 0;

773
774
775
776
777
	// 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
778
779
780
781
          {
            // Get the signature.
            bdd sig = compute_sig(*(it->second.begin()));

Thomas Badie's avatar
Thomas Badie committed
782
783
784
            if (Cosimulation)
              sig = bdd_compose(sig, bddfalse, bdd_var(bdd_initial));

Thomas Badie's avatar
Thomas Badie committed
785
786
787
788
789
790
791
792
793
794
795
796
797
            // 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)
798
799
800
801
802
803
	      {
		bdd one = bdd_satoneset(all_atomic_prop,
					sup_all_atomic_prop,
					bddtrue);
		all_atomic_prop -= one;

804
		// For each possible valuation, iterate over all possible
805
806
807
808
809
810
811
812
813
		// 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);

Thomas Badie's avatar
Thomas Badie committed
814
815
                ++nb_satoneset;

816
817
818
		bdd cond_acc_dest;
		while ((cond_acc_dest = isop.next()) != bddfalse)
		  {
819
820
                    ++stat.transitions;

Thomas Badie's avatar
Thomas Badie committed
821
822
                    ++nb_minato;

823
824
825
826
827
828
829
830
		    // 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);

831
		    // Keep the other!
832
833
834
835
		    bdd cond = bdd_existcomp(cond_acc_dest,
					     sup_all_atomic_prop);

		    // Because we have complemented all the acceptance
836
837
		    // conditions on the input automaton, we must
		    // revert them to create a new transition.
838
839
840
841
842
843
844
845
846
		    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
847
848
849
                    if (Cosimulation)
                      std::swap(src, dst);

850
851
852
853
854
855
856
857
		    // 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
858
859
860
861
862
863
		      = res->create_transition(src, dst);
		    t->condition = cond;
		    if (Sba && Cosimulation)
		      accst[dst] = acc;
		    else
		      t->acceptance_conditions = acc;
864
865
		  }
	      }
Thomas Badie's avatar
Thomas Badie committed
866
867
          }

868
869
	res->set_init_state(bdd2state[previous_class_
				      [a_->get_init_state()]]);
Thomas Badie's avatar
Thomas Badie committed
870

871
	res->merge_transitions();
Thomas Badie's avatar
Thomas Badie committed
872

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
	// Mark all accepting state in a second pass, when
	// dealing with SBA in cosimulation.
	if (Sba && Cosimulation)
	  for (unsigned snum = current_max; snum > 0; --snum)
	    {
	      const state* s = res->get_state(snum);
	      tgba_succ_iterator* it = res->succ_iter(s);
	      bdd acc = accst[snum];
	      for (it->first(); !it->done(); it->next())
		{
		  tgba_explicit_number::transition* t =
		    res->get_transition(it);
		  t->acceptance_conditions = acc;
		}
	      delete it;
	    }

Thomas Badie's avatar
Thomas Badie committed
890
891
        res_is_deterministic = nb_minato == nb_satoneset;

892
893
894
895
896
897
898
899
900
901
902
903
904
905
	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
906
907
          {
            std::cerr << "partition: "
Thomas Badie's avatar
Thomas Badie committed
908
909
                      << bdd_format_isop(a_->get_dict(), it->first)
                      << std::endl;
Thomas Badie's avatar
Thomas Badie committed
910
911
912
913

            for (std::list<const state*>::iterator it_s = it->second.begin();
                 it_s != it->second.end();
                 ++it_s)
914
915
916
917
	      {
		std::cerr << "  - "
			  << a_->format_state(*it_s) << std::endl;
	      }
Thomas Badie's avatar
Thomas Badie committed
918
919
          }

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

922
923
924
	for (map_state_bdd::const_iterator it = previous_class_.begin();
	     it != previous_class_.end();
	     ++it)
Thomas Badie's avatar
Thomas Badie committed
925
926
927
928
929
930
          {
            std::cerr << a_->format_state(it->first)
                      << " was in "
                      << bdd_format_set(a_->get_dict(), it->second)
                      << std::endl;
          }
931
      }
Thomas Badie's avatar
Thomas Badie committed
932

Thomas Badie's avatar
Thomas Badie committed
933
    protected:
934
935
      // The automaton which is simulated.
      tgba_explicit_number* a_;
Thomas Badie's avatar
Thomas Badie committed
936
      tgba_explicit_number* old_a_;
Thomas Badie's avatar
Thomas Badie committed
937

938
939
940
941
942
943
944
      // 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
945

946
947
      // Represent the class of each state at the previous iteration.
      map_state_bdd previous_class_;
Thomas Badie's avatar
Thomas Badie committed
948

949
950
951
      // 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
952

953
954
955
      // 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
956

957
958
      // 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
959

960
961
      // Size of the automaton.
      unsigned int size_a_;
Thomas Badie's avatar
Thomas Badie committed
962

963
964
965
      // 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
966

967
968
      // All the class variable:
      bdd all_class_var_;
Thomas Badie's avatar
Thomas Badie committed
969
970
971
972
973
974
975

      // 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;

Thomas Badie's avatar
Thomas Badie committed
976
977
      bdd all_proms_;

978
      automaton_size stat;
979
980
981

      // The order of the state.
      std::list<const state*> order_;
Thomas Badie's avatar
Thomas Badie committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
      scc_map* scc_map_;
      map_state_state old_name_;
      map_state_state new_original_;

      // This table link a state in the current automaton with a state
      // in the original one.
      map_state_state old_old_name_;

      const map_constraint* map_cst_;

      const tgba* original_;

      bdd all_acceptance_conditions_;

      // This variable is used when we return the copy, so we avoid
      // deleting what we return. It is better!
      bool dont_delete_old_;

      bool res_is_deterministic;
    };

    // For now, we don't try to handle cosimulation.
1004
    class direct_simulation_dont_care: public direct_simulation<false, false>
Thomas Badie's avatar
Thomas Badie committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
    {
      typedef std::vector<std::list<constraint> > constraints;
      typedef std::map<bdd,                  // Source Class.
                       std::map<bdd,         // Destination (implied) Class.
                                std::list<constraint>, // Constraints list.
                                bdd_less_than>,
                       bdd_less_than> constraint_list;
      typedef std::list<std::pair<bdd, bdd> > list_bdd_bdd;


    public:
      direct_simulation_dont_care(const tgba* t)
1017
      : direct_simulation<false, false>(t)
Thomas Badie's avatar
Thomas Badie committed
1018
1019
1020
      {
        // This variable is used in the new signature.
        on_cycle_ =
1021
          bdd_ithvar(a_->get_dict()->register_anonymous_variables(1, this));
Thomas Badie's avatar
Thomas Badie committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111

        // This one is used for the iteration on all the
        // possibilities. Avoid computing two times "no constraints".
        empty_seen_ = false;


        // If this variable is set to true, we have a number limit of
        // simulation to run.
        has_limit_ = false;

	notap = (bdd_support(all_acceptance_conditions_)
                 & all_class_var_ & on_cycle_);
      }

      // This function computes the don't care signature of the state
      // src. This signature is similar to the classic one, excepts
      // that if the transition is on a SCC, we add a on_cycle_ on it,
      // otherwise we add !on_cycle_. This allows us to split the
      // signature later.
      bdd dont_care_compute_sig(const state* src)
      {
        tgba_succ_iterator* sit = a_->succ_iter(src);
        bdd res = bddfalse;

        unsigned scc = scc_map_->scc_of_state(old_name_[src]);
        bool sccacc = scc_map_->accepting(scc);

        for (sit->first(); !sit->done(); sit->next())
          {
            const state* dst = sit->current_state();
            bdd cl = previous_class_[dst];
            bdd acc;

            if (scc != scc_map_->scc_of_state(old_name_[dst]))
              acc = !on_cycle_;
            else if (sccacc)
              acc = on_cycle_ & sit->current_acceptance_conditions();
            else
              acc = on_cycle_ & all_proms_;

            bdd to_add = acc & sit->current_condition() & relation_[cl];
            res |= to_add;
          }

        delete sit;
        return res;
      }

      // We used to have
      //   sig(s1) = (f1 | g1)
      //   sig(s2) = (f2 | g2)
      // and we say that s2 simulates s1 if sig(s1)=>sig(s2).
      // This amount to testing whether (f1|g1)=>(f2|g2),
      // which is equivalent to testing both
      //    f1=>(f2|g2)  and g1=>(f2|g2)
      // separately.
      //
      // Now we have a slightly improved version of this rule.
      // g1 and g2 are not on cycle, so they can make as many
      // promises as we wish, if that helps.  Adding promises
      // to g2 will not help, but adding promises to g1 can.
      //
      // So we test whether
      //    f1=>(f2|g2)
      //    g1=>noprom(f2|g2)
      // Where noprom(f2|g2) removes all promises from f2|g2.
      // (g1 do not have promises, and neither do g2).

      bool could_imply_aux(bdd f1, bdd g1, bdd left_class,
			   bdd right, bdd right_class)
      {
        (void) left_class;
        (void) right_class;

        bdd f2g2 = bdd_exist(right, on_cycle_);
        bdd f2g2n = bdd_exist(f2g2, all_proms_);

	bdd both = left_class & right_class;
	int lc = bdd_var(left_class);
        f1 = bdd_compose(f1, both, lc);
        g1 = bdd_compose(g1, both, lc);
        f2g2 = bdd_compose(f2g2, both, lc);
        f2g2n = bdd_compose(f2g2n, both, lc);

        return bdd_implies(f1, f2g2) && bdd_implies(g1, f2g2n);
      }

      bool could_imply(bdd left, bdd left_class,
		       bdd right, bdd right_class)
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1112
1113
1114
1115
1116
	bdd f1 = bdd_relprod(left, on_cycle_, on_cycle_);
	bdd g1 = bdd_relprod(left, !on_cycle_, on_cycle_);

        //bdd f1 = bdd_restrict(left, on_cycle_);
        //bdd g1 = bdd_restrict(left, !on_cycle_);
Thomas Badie's avatar
Thomas Badie committed
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
	return could_imply_aux(f1, g1, left_class,
			       right, right_class);
      }

      void dont_care_update_po(const list_bdd_bdd& now_to_next,
                               map_bdd_bdd& relation)
      {
        // This loop follows the pattern given by the paper.
        // foreach class do
        // |  foreach class do
        // |  | update po if needed
        // |  od
        // od

        for (list_bdd_bdd::const_iterator it1 = now_to_next.begin();
             it1 != now_to_next.end();
             ++it1)
          {
            bdd accu = it1->second;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1137
1138
1139
1140
1141
	    bdd f1 = bdd_relprod(it1->first, on_cycle_, on_cycle_);
	    bdd g1 = bdd_relprod(it1->first, !on_cycle_, on_cycle_);

            // bdd f1 = bdd_restrict(it1->first_, on_cycle_);
            // bdd g1 = bdd_restrict(it1->first_, !on_cycle_);
Thomas Badie's avatar
Thomas Badie committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

            for (list_bdd_bdd::const_iterator it2 = now_to_next.begin();
                 it2 != now_to_next.end();
                 ++it2)
              {
                // Skip the case managed by the initialization of accu.
                if (it1 == it2)
                  continue;

		if (could_imply_aux(f1, g1, it1->second,
				    it2->first, it2->second))
                  {
                    accu &= it2->second;
                    ++po_size_;
                  }
              }
            relation[it1->second] = accu;
          }
      }

#define ISOP(bdd) #bdd" - " << bdd_format_isop(a_->get_dict(), bdd)

      inline bool is_out_scc(bdd b)
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1166
1167
	return bddfalse !=  bdd_relprod(b, !on_cycle_, on_cycle_);
        // return bddfalse != bdd_restrict(b, !on_cycle_);
Thomas Badie's avatar
Thomas Badie committed
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
      }

#define create_cstr(src, dst, constraint) \
      std::make_pair(std::make_pair(src, dst), constraint)

      // This method solves three kind of problems, where we have two
      // conjunctions of variable (that corresponds to a particular
      // transition), and where left could imply right.
      // Three cases:
      //   - αP₁ ⇒ xβP₁ where x is unknown.
      //   - xβP₁ ⇒ αP₁ where x is unknown.
      //   - xαP₁ ⇒ yβP₁ where x, y are unknown.
      void create_simple_constraint(bdd left, bdd right,
                                    const state* src_left,
                                    const state* src_right,
                                    std::list<constraint>& constraint)
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1185
	assert(src_left != src_right);
Thomas Badie's avatar
Thomas Badie committed
1186
1187
1188
1189
1190
1191
1192
1193
1194
        // Determine which is the current case.
        bool out_scc_left = is_out_scc(left);
        bool out_scc_right = is_out_scc(right);
        bdd dest_class = bdd_existcomp(left, all_class_var_);
        assert(revert_relation_.find(dest_class) != revert_relation_.end());
        const state* dst_left = revert_relation_[dest_class];
        dest_class = bdd_existcomp(right, all_class_var_);
        const state* dst_right = revert_relation_[dest_class];

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1195
1196
	assert(src_left != dst_left || src_right != dst_right);

Thomas Badie's avatar
Thomas Badie committed
1197
1198
1199
        left = bdd_exist(left, all_class_var_ & on_cycle_);
        right = bdd_exist(right, all_class_var_ & on_cycle_);

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1200

Thomas Badie's avatar
Thomas Badie committed
1201
1202
1203
1204
1205
1206
1207
1208
        if (!out_scc_left && out_scc_right)
          {
            bdd b = bdd_exist(right, notap);
            bdd add = bdd_exist(left & b, bdd_support(b));

            if (add != bddfalse
                && bdd_exist(add, all_acceptance_conditions_) == bddtrue)
              {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1209
1210
		assert(src_right != dst_right);

Thomas Badie's avatar
Thomas Badie committed
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
                constraint
                  .push_back(create_cstr(new_original_[old_name_[src_right]],
                                         new_original_[old_name_[dst_right]],
                                         add));
              }
          }
        else if (out_scc_left && !out_scc_right)
          {
            bdd b = bdd_exist(left, notap);
            bdd add = bdd_exist(right & b, bdd_support(b));

            if (add != bddfalse
                && bdd_exist(add, all_acceptance_conditions_) == bddtrue)
              {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1225
1226
		assert(src_left != dst_left);

Thomas Badie's avatar
Thomas Badie committed
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
                constraint
                  .push_back(create_cstr(new_original_[old_name_[src_left]],
                                         new_original_[old_name_[dst_left]],
                                         add));
              }
          }
        else if (out_scc_left && out_scc_right)
          {
            bdd b = bdd_exist(left, notap);
            bdd add = bdd_exist(right & b, bdd_support(b));

            if (add != bddfalse
                && bdd_exist(add, all_acceptance_conditions_) == bddtrue)
              {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1241
		assert(src_left != dst_left && src_right != dst_right);
Thomas Badie's avatar
Thomas Badie committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
		// FIXME: cas pas compris.
                constraint
                  .push_back(create_cstr(new_original_[old_name_[src_left]],
                                         new_original_[old_name_[dst_left]],
                                         add));
		constraint
		  .push_back(create_cstr(new_original_[old_name_[src_right]],
					 new_original_[old_name_[dst_right]],
                                         add));
              }

          }
        else
          assert(0);
      }


      // This function run over the signatures, and select the
      // transitions that are out of a SCC and call the function
      // create_simple_constraint to solve the problem.

      // NOTE: Currently, this may not be the most accurate method,
      // because we check for equality in the destination part of the
      // signature. We may just check the destination that can be
      // implied instead.
      std::list<constraint> create_new_constraint(const state* left,
                                                  const state* right,
                                                  map_state_bdd& state2sig)
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1271
1272
1273
	bdd pcl = previous_class_[left];
	bdd pcr = previous_class_[right];

Thomas Badie's avatar
Thomas Badie committed
1274
1275
1276
1277
1278
        bdd sigl = state2sig[left];
        bdd sigr = state2sig[right];

        std::list<constraint> res;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1279
1280
1281
1282
1283
1284
	bdd ex = all_class_var_ & on_cycle_;

	bdd both = pcl & pcr;
	int lc = bdd_var(pcl);
#define DEST(x) bdd_compose(bdd_existcomp(x, ex), both, lc)

Thomas Badie's avatar
Thomas Badie committed
1285
1286
        // Key is destination class, value is the signature part that
        // led to this destination class.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
	map_bdd_bdd sigl_map;
	{
	  minato_isop isop(sigl & on_cycle_);
	  bdd cond_acc_dest;
	  while ((cond_acc_dest = isop.next()) != bddfalse)
	    sigl_map[DEST(cond_acc_dest)]
	      |= cond_acc_dest;
	}
	{
	  minato_isop isop(sigl & !on_cycle_);
	  bdd cond_acc_dest;
	  while ((cond_acc_dest = isop.next()) != bddfalse)
	    sigl_map[DEST(cond_acc_dest)]
	      |= cond_acc_dest;
	}
Thomas Badie's avatar
Thomas Badie committed
1302
        map_bdd_bdd sigr_map;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
	{
	  minato_isop isop2(sigr & on_cycle_);
	  bdd cond_acc_dest2;
	  while ((cond_acc_dest2 = isop2.next()) != bddfalse)
	    sigr_map[DEST(cond_acc_dest2)]
	      |= cond_acc_dest2;
	}
	{
	  minato_isop isop2(sigr & !on_cycle_);
	  bdd cond_acc_dest2;
	  while ((cond_acc_dest2 = isop2.next()) != bddfalse)
	    sigr_map[DEST(cond_acc_dest2)]
	      |= cond_acc_dest2;
	}
Thomas Badie's avatar
Thomas Badie committed
1317
1318
1319
1320
1321
1322

        // Iterate over the transitions of both states.
        for (map_bdd_bdd::const_iterator lit = sigl_map.begin();
             lit != sigl_map.end(); ++lit)
	    for (map_bdd_bdd::iterator rit = sigr_map.begin();
		 rit != sigr_map.end(); ++rit)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1323
	      {
Thomas Badie's avatar
Thomas Badie committed
1324
1325
1326
	      // And create constraints if any of the transitions
	      // is out of the SCC and the left could imply the right.
	      if ((is_out_scc(lit->second) || is_out_scc(rit->second))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1327
1328
		  && (bdd_exist(lit->first, on_cycle_) ==
		      bdd_exist(rit->first, on_cycle_)))
Thomas Badie's avatar
Thomas Badie committed
1329
1330
1331
1332
                {
                  create_simple_constraint(lit->second, rit->second,
                                           left, right, res);
                }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1333
	      }
Thomas Badie's avatar
Thomas Badie committed
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357

        return res;
      }

      inline automaton_size get_stat() const
      {
        return min_size_;
      }

      tgba* run()
      {
        // Iterate the simulation until the end. We just don't return
        // an automaton. This allows us to get all the information
        // about the states and their signature.
        main_loop();

        // Compute the don't care signatures,
        map_bdd_lstate dont_care_bdd_lstate;
        // Useful to keep track of who is who.
        map_state_bdd dont_care_state2sig;
        map_state_bdd state2sig;

        list_bdd_bdd dont_care_now_to_now;
        map_bdd_state class2state;
1358
1359
1360
        list_bdd_bdd now_to_now;
        bdd_lstate_.clear();

Thomas Badie's avatar
Thomas Badie committed
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
        // Compute the don't care signature for all the states.
        for (std::list<const state*>::const_iterator my_it = order_.begin();
             my_it != order_.end();
             ++my_it)
          {
            map_state_bdd::iterator it = previous_class_.find(*my_it);
            const state* src = it->first;

            bdd sig = dont_care_compute_sig(src);
            dont_care_bdd_lstate[sig].push_back(src);
            dont_care_state2sig[src] = sig;
            dont_care_now_to_now.push_back(std::make_pair(sig, it->second));
            class2state[it->second] = it->first;

1375
            sig = compute_sig(src);
Thomas Badie's avatar
Thomas Badie committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
            bdd_lstate_[sig].push_back(src);
            state2sig[src] = sig;
            now_to_now.push_back(std::make_pair(sig, it->second));
          }

        map_bdd_bdd dont_care_relation;
        map_bdd_bdd relation;
        update_po(now_to_now, relation);
        dont_care_update_po(dont_care_now_to_now, dont_care_relation);

        constraint_list cc;

        for (map_bdd_bdd::iterator it = relation.begin();
             it != relation.end();
             ++it)
          {
            revert_relation_[it->second] = class2state[it->first];
          }

        int number_constraints = 0;
        relation_ = relation;


        // order_ is here for the determinism.  Here we make the diff
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1400
        // between the two tables: imply and could_imply.
Thomas Badie's avatar
Thomas Badie committed
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
        for (std::list<const state*>::const_iterator my_it = order_.begin();
             my_it != order_.end();
             ++my_it)
          {
            map_state_bdd::iterator it = previous_class_.find(*my_it);
            assert(relation.find(it->second) != relation.end());
            assert(dont_care_relation.find(it->second)
                   != dont_care_relation.end());

            bdd care_rel = relation[it->second];
            bdd dont_care_rel = dont_care_relation[it->second];

            if (care_rel == dont_care_rel)
              continue;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1416
1417
            // If they are different we necessarily have
	    // dont_care_rel == care_rel & diff
Thomas Badie's avatar
Thomas Badie committed
1418
            bdd diff = bdd_exist(dont_care_rel, care_rel);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1419
1420
	    assert(dont_care_rel == (care_rel & diff));
	    assert(diff != bddtrue);
Thomas Badie's avatar
Thomas Badie committed
1421

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1422
	    do
Thomas Badie's avatar
Thomas Badie committed
1423
              {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1424
                bdd cur_diff = bdd_ithvar(bdd_var(diff));
Thomas Badie's avatar
Thomas Badie committed
1425
1426
1427
1428
1429
                cc[it->second][cur_diff]
                  = create_new_constraint(it->first,
                                          class2state[cur_diff],
                                          dont_care_state2sig);
                ++number_constraints;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1430
                diff = bdd_high(diff);
Thomas Badie's avatar
Thomas Badie committed
1431
              }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1432
	    while (diff != bddtrue);
Thomas Badie's avatar
Thomas Badie committed
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
          }
#ifndef NDEBUG
	for (map_bdd_state::const_iterator i = class2state.begin();
	     i != class2state.end(); ++i)
	  assert(previous_class_[i->second] == i->first);
#endif

        tgba* min = 0;

        map_constraint cstr;

        if (has_limit_)
          rec(cc, cstr, &min, limit_);
        else
          rec(cc, cstr, &min);

        return min;
      }

#define ERASE(inner_map, bigger_map, it)        \
      inner_map.erase(it);                      \
      if (inner_map.empty())                    \
        bigger_map.erase(bigger_map.begin())

      // Add and erase.
      void add_to_map_imply(constraint_list& constraints,
                            map_constraint& cstr)
      {
        constraint_list::iterator it = constraints.begin();
        std::map<bdd,
                 std::list<constraint>,
                 bdd_less_than>::iterator it2 = it->second.begin();

        add_to_map(it2->second, cstr);

        bdd implied_list = relation_[it2->first]; // it2->first:
                                                  // destination class.

        ERASE(it->second, constraints, it2);
        if (constraints.empty())
          return;
        it = constraints.begin();
        // At worst, implied_list is equal to it2->first.
        while (implied_list != bddtrue)
          {
            bdd cur_implied = bdd_ithvar(bdd_var(implied_list));

            std::map<bdd,
                     std::list<constraint>,
                     bdd_less_than>::iterator tmp
              = it->second.find(cur_implied);
            if (tmp != it->second.end())
              {
                add_to_map(tmp->second, cstr);
                ERASE(it->second, constraints, tmp);
                if (constraints.empty())
                  return;
              }

            implied_list = bdd_high(implied_list);
          }
      }

      // Compute recursively all the combinations.
      void rec(constraint_list constraints,
               map_constraint cstr,
               tgba** min,
               int max_depth = std::numeric_limits<int>::max())
      {
        assert(max_depth > 0);
        while (!constraints.empty())
          {
            if (!--max_depth)
                break;
            add_to_map_imply(constraints, cstr);
            rec(constraints, cstr, min, max_depth);
          }

        if (empty_seen_ && cstr.empty())
          return;
        else if (cstr.empty())
          empty_seen_ = true;

1516
        direct_simulation<false, false> dir_sim(original_, &cstr);
Thomas Badie's avatar
Thomas Badie committed
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
        tgba* tmp = dir_sim.run();
        automaton_size cur_size = dir_sim.get_stat();
        if (*min == 0 || min_size_ > cur_size)
          {
            delete *min;
            *min = tmp;
            min_size_ = cur_size;
            res_is_deterministic = dir_sim.result_is_deterministic();
          }
        else
          {
            delete tmp;
          }
      }

      void set_limit(int n)
      {
        has_limit_ = true;
        limit_ = n;
      }

    private:
      // This bdd is used to differentiate parts of the signature that
      // are in a SCC and those that are not.
      bdd on_cycle_;

      map_bdd_bdd dont_care_relation_;

      map_bdd_state revert_relation_;

      automaton_size min_size_;

      bool empty_seen_;

      bool has_limit_;
      int limit_;

      bdd notap;
Thomas Badie's avatar
Thomas Badie committed
1555
    };
Thomas Badie's avatar
Thomas Badie committed
1556