tgba2ta.cc 22.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright (C) 2010, 2011 Laboratoire de Recherche et Developpement
// 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
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

21
//#define TRACE
22
23
24
25
26
27
28
29

#include <iostream>
#ifdef TRACE
#define trace std::clog
#else
#define trace while (0) std::clog
#endif

30
31
32
33
34
35
36
37
38
39
40
41
#include "ltlast/atomic_prop.hh"
#include "ltlast/constant.hh"
#include "tgba/formula2bdd.hh"
#include "misc/bddop.hh"
#include <cassert>
#include "ltlvisit/tostring.hh"
#include <iostream>
#include "tgba/bddprint.hh"
#include "tgbaalgos/gtec/nsheap.hh"
#include <stack>
#include "tgba2ta.hh"
#include "taalgos/statessetbuilder.hh"
42
#include "ta/tgbtaexplicit.hh"
43
44
45
46
47
48

using namespace std;

namespace spot
{

49
50
  ta_explicit*
  build_ta(ta_explicit* ta, bdd atomic_propositions_set_,
51
52
53
54
55
      bool artificial_initial_state_mode,
      bool artificial_livelock_accepting_state_mode, bool degeneralized)
  {

    std::stack<state_ta_explicit*> todo;
56
    const tgba* tgba_ = ta->get_tgba();
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    // build Initial states set:
    state* tgba_init_state = tgba_->get_init_state();

    bdd tgba_condition = tgba_->support_conditions(tgba_init_state);

    bdd satone_tgba_condition;
    while ((satone_tgba_condition = bdd_satoneset(tgba_condition,
        atomic_propositions_set_, bddtrue)) != bddfalse)
      {
        tgba_condition -= satone_tgba_condition;
        state_ta_explicit* init_state;
        if (degeneralized)
          {
            init_state = new state_ta_explicit(tgba_init_state->clone(),
                satone_tgba_condition, true,
                ((tgba_sba_proxy*) tgba_)->state_is_accepting(tgba_init_state));
          }
        else
          {
            init_state = new state_ta_explicit(tgba_init_state->clone(),
                satone_tgba_condition, true, false);
          }

        state_ta_explicit* s = ta->add_state(init_state);
        assert(s == init_state);
        ta->add_to_initial_states_set(s);

        todo.push(init_state);
      }
    tgba_init_state->destroy();

    while (!todo.empty())
      {
        state_ta_explicit* source = todo.top();
        todo.pop();

        tgba_succ_iterator* tgba_succ_it = tgba_->succ_iter(
            source->get_tgba_state());
        for (tgba_succ_it->first(); !tgba_succ_it->done(); tgba_succ_it->next())
          {
            const state* tgba_state = tgba_succ_it->current_state();
            bdd tgba_condition = tgba_succ_it->current_condition();
            bdd tgba_acceptance_conditions =
                tgba_succ_it->current_acceptance_conditions();
            bdd satone_tgba_condition;
            while ((satone_tgba_condition = bdd_satoneset(tgba_condition,
                atomic_propositions_set_, bddtrue)) != bddfalse)
              {

                tgba_condition -= satone_tgba_condition;

                bdd all_props = bddtrue;
                bdd dest_condition;
                if (satone_tgba_condition == source->get_tgba_condition())
                  while ((dest_condition = bdd_satoneset(all_props,
                      atomic_propositions_set_, bddtrue)) != bddfalse)
                    {
                      all_props -= dest_condition;
                      state_ta_explicit* new_dest;
                      if (degeneralized)
                        {

                          new_dest = new state_ta_explicit(tgba_state->clone(),
                              dest_condition, false,
                              ((tgba_sba_proxy*) tgba_)->state_is_accepting(
                                  tgba_state));

                        }
                      else
                        {
                          new_dest = new state_ta_explicit(tgba_state->clone(),
                              dest_condition, false, false);

                        }
                      state_ta_explicit* dest = ta->add_state(new_dest);

                      if (dest != new_dest)
                        {
                          // the state dest already exists in the testing automata
                          new_dest->get_tgba_state()->destroy();
                          delete new_dest;
                        }
                      else
                        {
                          todo.push(dest);
                        }

                      ta->create_transition(source, bdd_setxor(
                          source->get_tgba_condition(),
                          dest->get_tgba_condition()),
                          tgba_acceptance_conditions, dest);

                    }

              }
            tgba_state->destroy();
          }
        delete tgba_succ_it;

      }

159
160
161
162
    state_ta_explicit* artificial_livelock_accepting_state = 0;

    trace << "*** build_ta: artificial_livelock_accepting_state_mode = ***"
        << artificial_livelock_accepting_state_mode << std::endl;
163

164
165
    if (artificial_livelock_accepting_state_mode)
      {
166
167

        artificial_livelock_accepting_state =
168
169
            new state_ta_explicit(ta->get_tgba()->get_init_state(), bddtrue,
                false, false, true, 0);
170
171
172
        trace << "*** build_ta: artificial_livelock_accepting_state = ***"
                << artificial_livelock_accepting_state << std::endl;

173
      }
174
175


176
177

    compute_livelock_acceptance_states(ta, artificial_livelock_accepting_state);
178
179
180
181
182

    return ta;

  }

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  ta_explicit*
  tgba_to_ta(const tgba* tgba_, bdd atomic_propositions_set_,
      bool artificial_initial_state_mode,
      bool artificial_livelock_accepting_state_mode, bool degeneralized)
  {
    ta_explicit* ta;

    state* tgba_init_state = tgba_->get_init_state();
    if (artificial_initial_state_mode)
      {
        state_ta_explicit* ta_init_state = new state_ta_explicit(
            tgba_init_state->clone(), bddfalse, true);

        ta = new spot::ta_explicit(tgba_, tgba_->all_acceptance_conditions(),
            ta_init_state);
      }
    else
      {
        ta = new spot::ta_explicit(tgba_, tgba_->all_acceptance_conditions());
      }
    tgba_init_state->destroy();

    // build ta automata:
    build_ta(ta, atomic_propositions_set_, artificial_initial_state_mode,
        artificial_livelock_accepting_state_mode, degeneralized);
    return ta;
  }

211
212
213
214
215
216
217
218
219
220
221
222
  void
  add_artificial_livelock_accepting_state(ta_explicit* testing_automata,
      state_ta_explicit* artificial_livelock_accepting_state)
  {

    state_ta_explicit* artificial_livelock_accepting_state_added =
        testing_automata->add_state(artificial_livelock_accepting_state);

    // unique artificial_livelock_accepting_state
    assert(artificial_livelock_accepting_state_added
        == artificial_livelock_accepting_state);

223
224
225
226
227
    trace
        << "*** add_artificial_livelock_accepting_state: assert(artificial_livelock_accepting_state_added == artificial_livelock_accepting_state) = ***"
        << (artificial_livelock_accepting_state_added
            == artificial_livelock_accepting_state) << std::endl;

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    ta::states_set_t states_set = testing_automata->get_states_set();
    ta::states_set_t::iterator it;

    std::set<bdd, bdd_less_than>* conditions_to_livelock_accepting_states =
        new std::set<bdd, bdd_less_than>;

    for (it = states_set.begin(); it != states_set.end(); it++)
      {

        state_ta_explicit* source = static_cast<state_ta_explicit*> (*it);

        conditions_to_livelock_accepting_states->clear();

        state_ta_explicit::transitions* trans = source->get_transitions();
        state_ta_explicit::transitions::iterator it_trans;

        if (trans != 0)
          for (it_trans = trans->begin(); it_trans != trans->end();)
            {
              state_ta_explicit* dest = (*it_trans)->dest;

249
250
251
252
253
              state_ta_explicit::transitions* dest_trans =
                  (dest)->get_transitions();
              bool dest_trans_empty = dest_trans == 0 || dest_trans->empty();

              //TODO TA++
254
              if (dest->is_livelock_accepting_state()
255
                  && (!dest->is_accepting_state() || dest_trans_empty))
256
257
258
259
260
261
262
                {
                  conditions_to_livelock_accepting_states->insert(
                      (*it_trans)->condition);

                }

              //remove hole successors states
263

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
              if (dest_trans_empty)
                {
                  source->get_transitions((*it_trans)->condition)->remove(
                      *it_trans);
                  delete (*it_trans);
                  it_trans = trans->erase(it_trans);
                }
              else
                {
                  it_trans++;
                }
            }

        if (conditions_to_livelock_accepting_states != 0)
          {
            std::set<bdd, bdd_less_than>::iterator it_conditions;
            for (it_conditions
                = conditions_to_livelock_accepting_states->begin(); it_conditions
                != conditions_to_livelock_accepting_states->end(); it_conditions++)
              {

                testing_automata->create_transition(source, (*it_conditions),
286
                    bddfalse, artificial_livelock_accepting_state, true);
287
288
289
290
291
292
293

              }
          }

      }
    delete conditions_to_livelock_accepting_states;

294
295
296
297
298
299
300
301
302
303
304
305
306
    for (it = states_set.begin(); it != states_set.end(); it++)
      {

        state_ta_explicit* state = static_cast<state_ta_explicit*> (*it);
        state_ta_explicit::transitions* state_trans =
            (state)->get_transitions();
        bool state_trans_empty = state_trans == 0 || state_trans->empty();

        if (state->is_livelock_accepting_state()
            && (!state->is_accepting_state()) && (!state_trans_empty))
          state->set_livelock_accepting_state(false);
      }

307
308
309
310
311
312
313
314
  }

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

  void
315
316
  compute_livelock_acceptance_states(ta_explicit* testing_automata,
      state_ta_explicit* artificial_livelock_accepting_state)
317
318
319
320
321
322
  {
    // We use five main data in this algorithm:
    // * sscc: a stack of strongly stuttering-connected components (SSCC)
    scc_stack_ta sscc;

    // * arc, a stack of acceptance conditions between each of these SCC,
323
    std::stack<bdd> arc;
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

    // * h: a hash of all visited nodes, with their order,
    //   (it is called "Hash" in Couvreur's paper)
    numbered_state_heap* h =
        numbered_state_heap_hash_map_factory::instance()->build(); ///< Heap of visited states.

    // * num: the number of visited nodes.  Used to set the order of each
    //   visited node,
    int num = 0;

    // * todo: the depth-first search stack.  This holds pairs of the
    //   form (STATE, ITERATOR) where ITERATOR is a tgba_succ_iterator
    //   over the successors of STATE.  In our use, ITERATOR should
    //   always be freed when TODO is popped, but STATE should not because
    //   it is also used as a key in H.
    std::stack<pair_state_iter> todo;

    // * init: the set of the depth-first search initial states
    std::stack<state*> init_set;

    ta::states_set_t::const_iterator it;
    ta::states_set_t init_states = testing_automata->get_initial_states_set();
    for (it = init_states.begin(); it != init_states.end(); it++)
      {
        state* init_state = (*it);
        init_set.push(init_state);

      }

    while (!init_set.empty())
      {
        // Setup depth-first search from initial states.
356

357
358
359
360
          {
            state_ta_explicit* init =
                down_cast<state_ta_explicit*> (init_set.top());
            init_set.pop();
361
            state_ta_explicit* init_clone = init;
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
            numbered_state_heap::state_index_p h_init = h->find(init_clone);

            if (h_init.first)
              continue;

            h->insert(init_clone, ++num);
            sscc.push(num);
            arc.push(bddfalse);
            sscc.top().is_accepting
                = testing_automata->is_accepting_state(init);
            tgba_succ_iterator* iter = testing_automata->succ_iter(init);
            iter->first();
            todo.push(pair_state_iter(init, iter));

          }

        while (!todo.empty())
          {

            state* curr = todo.top().first;

383
            numbered_state_heap::state_index_p spi = h->find(curr);
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
            // If we have reached a dead component, ignore it.
            if (*spi.second == -1)
              {
                todo.pop();
                continue;
              }

            // We are looking at the next successor in SUCC.
            tgba_succ_iterator* succ = todo.top().second;

            // If there is no more successor, backtrack.
            if (succ->done())
              {
                // We have explored all successors of state CURR.

                // Backtrack TODO.
                todo.pop();

                // fill rem with any component removed,
403
                numbered_state_heap::state_index_p spi = h->index(curr);
404
405
406
407
408
409
410
411
412
413
414
415
                assert(spi.first);

                sscc.rem().push_front(curr);

                // When backtracking the root of an SSCC, we must also
                // remove that SSCC from the ROOT stacks.  We must
                // discard from H all reachable states from this SSCC.
                assert(!sscc.empty());
                if (sscc.top().index == *spi.second)
                  {
                    // removing states
                    std::list<state*>::iterator i;
416
417
418
                    bool is_livelock_accepting_sscc = (sscc.rem().size() > 1)
                        && ((sscc.top().is_accepting) || (sscc.top().condition
                            == testing_automata->all_acceptance_conditions()));
419
420
421

                    for (i = sscc.rem().begin(); i != sscc.rem().end(); ++i)
                      {
422
                        numbered_state_heap::state_index_p spi = h->index((*i));
423
424
425
426
427
428
429
430
431
432
433
434
                        assert(spi.first->compare(*i) == 0);
                        assert(*spi.second != -1);
                        *spi.second = -1;
                        if (is_livelock_accepting_sscc)
                          {//if it is an accepting sscc
                            //add the state to G (=the livelock-accepting states set)

                            state_ta_explicit * livelock_accepting_state =
                                down_cast<state_ta_explicit*> (*i);

                            livelock_accepting_state->set_livelock_accepting_state(
                                true);
435
436
437
                            if (artificial_livelock_accepting_state != 0)
                              livelock_accepting_state->set_accepting_state(
                                  true);
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
                          }

                      }

                    assert(!arc.empty());
                    sscc.pop();
                    arc.pop();

                  }

                // automata reduction
                testing_automata->delete_stuttering_and_hole_successors(curr);

                delete succ;
                // Do not delete CURR: it is a key in H.
                continue;
              }

            // Fetch the values destination state we are interested in...
            state* dest = succ->current_state();

            bdd acc_cond = succ->current_acceptance_conditions();
            // ... and point the iterator to the next successor, for
            // the next iteration.
            succ->next();
            // We do not need SUCC from now on.


            // Are we going to a new state through a stuttering transition?
            bool is_stuttering_transition =
                testing_automata->get_state_condition(curr)
                    == testing_automata->get_state_condition(dest);
470
            state* dest_clone = dest;
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
            spi = h->find(dest_clone);

            // Is this a new state?
            if (!spi.first)
              {
                if (!is_stuttering_transition)
                  {
                    init_set.push(dest);
                    dest_clone->destroy();
                    continue;
                  }

                // Number it, stack it, and register its successors
                // for later processing.
                h->insert(dest_clone, ++num);
                sscc.push(num);
                arc.push(acc_cond);
                sscc.top().is_accepting = testing_automata->is_accepting_state(
                    dest);

                tgba_succ_iterator* iter = testing_automata->succ_iter(dest);
                iter->first();
                todo.push(pair_state_iter(dest, iter));
                continue;
              }

            // If we have reached a dead component, ignore it.
            if (*spi.second == -1)
              continue;

501
502
            trace << "***compute_livelock_acceptance_states: CYCLE***"
                << std::endl;
503

504
505
506
507
508
509
            if (!curr->compare(dest))
              {
                state_ta_explicit * self_loop_state =
                    down_cast<state_ta_explicit*> (curr);
                assert(self_loop_state);

510
511
512
                if (testing_automata->is_accepting_state(self_loop_state)
                    || (acc_cond
                        == testing_automata->all_acceptance_conditions()))
513
514
                  {
                    self_loop_state->set_livelock_accepting_state(true);
515
                    if (artificial_livelock_accepting_state != 0) self_loop_state->set_accepting_state(true);
516
517
518

                  }

519
                trace
520
521
                    << "***compute_livelock_acceptance_states: CYCLE: self_loop_state***"
                    << std::endl;
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

              }

            // Now this is the most interesting case.  We have reached a
            // state S1 which is already part of a non-dead SSCC.  Any such
            // non-dead SSCC has necessarily been crossed by our path to
            // this state: there is a state S2 in our path which belongs
            // to this SSCC too.  We are going to merge all states between
            // this S1 and S2 into this SSCC.
            //
            // This merge is easy to do because the order of the SSCC in
            // ROOT is ascending: we just have to merge all SSCCs from the
            // top of ROOT that have an index greater to the one of
            // the SSCC of S2 (called the "threshold").
            int threshold = *spi.second;
            std::list<state*> rem;
            bool acc = false;

            while (threshold < sscc.top().index)
              {
                assert(!sscc.empty());
                assert(!arc.empty());
                acc |= sscc.top().is_accepting;
                acc_cond |= sscc.top().condition;
                acc_cond |= arc.top();
                rem.splice(rem.end(), sscc.rem());
                sscc.pop();
                arc.pop();
              }

            // Note that we do not always have
            //  threshold == sscc.top().index
            // after this loop, the SSCC whose index is threshold might have
            // been merged with a lower SSCC.

            // Accumulate all acceptance conditions into the merged SSCC.
            sscc.top().is_accepting |= acc;
            sscc.top().condition |= acc_cond;

            sscc.rem().splice(sscc.rem().end(), rem);

          }

      }
    delete h;

568
569
570
571
572
573
574
575
576
577
578
    trace
        << "*** compute_livelock_acceptance_states: PRE call add_artificial_livelock_accepting_state() method ... (artificial_livelock_accepting_state != 0) :***"
        << (artificial_livelock_accepting_state != 0) << std::endl;

    if (artificial_livelock_accepting_state != 0)
      add_artificial_livelock_accepting_state(testing_automata,
          artificial_livelock_accepting_state);

    trace
        << "*** compute_livelock_acceptance_states: POST call add_artificial_livelock_accepting_state() method ***"
        << std::endl;
579
  }
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595

  tgbta_explicit*
  tgba_to_tgbta(const tgba* tgba_, bdd atomic_propositions_set_)
  {

    state* tgba_init_state = tgba_->get_init_state();
    state_ta_explicit* ta_init_state = new state_ta_explicit(
        tgba_init_state->clone(), bddfalse, true);
    tgba_init_state->destroy();

    tgbta_explicit* tgbta = new spot::tgbta_explicit(tgba_,
        tgba_->all_acceptance_conditions(), ta_init_state);

    // build ta automata:
    build_ta(tgbta, atomic_propositions_set_, true, true, false);

596
    trace << "***tgba_to_tgbta: POST build_ta***" << std::endl;
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621

    // adapt a ta automata to build tgbta automata :
    ta::states_set_t states_set = tgbta->get_states_set();
    ta::states_set_t::iterator it;
    tgba_succ_iterator* initial_states_iter = tgbta->succ_iter(
        tgbta->get_artificial_initial_state());
    initial_states_iter->first();
    if (initial_states_iter->done())
      return tgbta;
    bdd first_state_condition = (initial_states_iter)->current_condition();
    delete initial_states_iter;

    bdd bdd_stutering_transition = bdd_setxor(first_state_condition,
        first_state_condition);

    for (it = states_set.begin(); it != states_set.end(); it++)
      {

        state_ta_explicit* state = static_cast<state_ta_explicit*> (*it);

        state_ta_explicit::transitions* trans = state->get_transitions();
        if (state->is_livelock_accepting_state())
          {

            bool trans_empty = (trans == 0 || trans->empty());
622
            if (trans_empty || state->is_accepting_state())
623
624
              {
                trace
625
626
                    << "***tgba_to_tgbta: PRE if (state->is_livelock_accepting_state()) ... create_transition ***"
                    << std::endl;
627
628
629
                tgbta->create_transition(state, bdd_stutering_transition,
                    tgbta->all_acceptance_conditions(), state);
                trace
630
631
                    << "***tgba_to_tgbta: POST if (state->is_livelock_accepting_state()) ... create_transition ***"
                    << std::endl;
632
633

              }
634

635
636
637
638
639
640
          }

        if (state->compare(tgbta->get_artificial_initial_state()))
          tgbta->create_transition(state, bdd_stutering_transition, bddfalse,
              state);

641
642
        state->set_livelock_accepting_state(false);
        state->set_accepting_state(false);
643
        trace << "***tgba_to_tgbta: POST create_transition ***" << std::endl;
644
645
646
647
648
649
650

      }

    return tgbta;

  }

651
}