tgba2ta.cc 19.3 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Laboratoire de
3
// Recherche et Développement de l'Epita (LRDE).
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
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/>.
19

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

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

29
#include <spot/twa/formula2bdd.hh>
30
31
#include <cassert>
#include <iostream>
32
#include <spot/twa/bddprint.hh>
33
#include <stack>
34
35
36
#include <spot/taalgos/tgba2ta.hh>
#include <spot/taalgos/statessetbuilder.hh>
#include <spot/ta/tgtaexplicit.hh>
37
38
39
40
41
42

using namespace std;

namespace spot
{

43
44
  namespace
  {
45
    typedef std::pair<const spot::state*, twa_succ_iterator*> pair_state_iter;
46

47
48
    static void
    transform_to_single_pass_automaton
49
    (const ta_explicit_ptr& testing_automata,
50
     state_ta_explicit* artificial_livelock_acc_state = nullptr)
51
    {
52

53
      if (artificial_livelock_acc_state)
54
	{
55
	  auto artificial_livelock_acc_state_added =
56
            testing_automata->add_state(artificial_livelock_acc_state);
57

58
59
60
	  // unique artificial_livelock_acc_state
	  assert(artificial_livelock_acc_state_added
		 == artificial_livelock_acc_state);
61
	  (void)artificial_livelock_acc_state_added;
62
63
64
	  artificial_livelock_acc_state->set_livelock_accepting_state(true);
	  artificial_livelock_acc_state->free_transitions();
	}
65

66
67
      ta::states_set_t states_set = testing_automata->get_states_set();
      ta::states_set_t::iterator it;
68

69
      state_ta_explicit::transitions* transitions_to_livelock_states =
70
71
        new state_ta_explicit::transitions;

72
73
      for (it = states_set.begin(); it != states_set.end(); ++it)
	{
74
75
	  auto source = const_cast<state_ta_explicit*>
	    (static_cast<const state_ta_explicit*>(*it));
76

77
	  transitions_to_livelock_states->clear();
78

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

82
	  if (trans)
83
84
	    for (it_trans = trans->begin(); it_trans != trans->end();)
	      {
85
		auto dest = const_cast<state_ta_explicit*>((*it_trans)->dest);
86

87
		state_ta_explicit::transitions* dest_trans =
88
		  dest->get_transitions();
89
		bool dest_trans_empty = !dest_trans || dest_trans->empty();
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

		//select transitions where a destination is a livelock state
		// which isn't a Buchi accepting state and has successors
		if (dest->is_livelock_accepting_state()
		    && (!dest->is_accepting_state()) && (!dest_trans_empty))
		  transitions_to_livelock_states->push_front(*it_trans);

		// optimization to have, after minimization, an unique
		// livelock state which has no successors
		if (dest->is_livelock_accepting_state() && (dest_trans_empty))
		  dest->set_accepting_state(false);

		++it_trans;
	      }

105
	  if (transitions_to_livelock_states)
106
107
108
109
110
111
112
	    {
	      state_ta_explicit::transitions::iterator it_trans;

	      for (it_trans = transitions_to_livelock_states->begin();
		   it_trans != transitions_to_livelock_states->end();
		   ++it_trans)
		{
113
		  if (artificial_livelock_acc_state)
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
		    {
		      testing_automata->create_transition
			(source,
			 (*it_trans)->condition,
			 (*it_trans)->acceptance_conditions,
			 artificial_livelock_acc_state, true);
		    }
		  else
		    {
		      testing_automata->create_transition
			(source,
			 (*it_trans)->condition,
			 (*it_trans)->acceptance_conditions,
			 ((*it_trans)->dest)->stuttering_reachable_livelock,
			 true);
		    }
		}
	    }
	}
      delete transitions_to_livelock_states;

      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 =
139
            (state)->get_transitions();
140
	  bool state_trans_empty = !state_trans || state_trans->empty();
141

142
143
144
145
	  if (state->is_livelock_accepting_state()
	      && (!state->is_accepting_state()) && (!state_trans_empty))
	    state->set_livelock_accepting_state(false);
	}
146
147
    }

148
    static void
149
    compute_livelock_acceptance_states(const ta_explicit_ptr& testing_aut,
150
151
152
				       bool single_pass_emptiness_check,
				       state_ta_explicit*
				       artificial_livelock_acc_state)
153
    {
154
155
156
157
158
      // 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,
159
      std::stack<acc_cond::mark_t> arc;
160
161
162

      // * h: a hash of all visited nodes, with their order,
      //   (it is called "Hash" in Couvreur's paper)
163
      state_map<int> h; ///< Heap of visited states.
164
165
166
167
168
169

      // * 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
170
      //   form (STATE, ITERATOR) where ITERATOR is a twa_succ_iterator
171
172
173
174
175
176
      //   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
177
      std::stack<const state*> init_set;
178

179
      for (auto s: testing_aut->get_initial_states_set())
180
	init_set.push(s);
181
182
183
184
185
186

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

	  {
187
	    auto init = down_cast<const state_ta_explicit*> (init_set.top());
188
	    init_set.pop();
189

190
	    if (!h.emplace(init, num + 1).second)
191
192
193
194
	      {
		init->destroy();
		continue;
	      }
195

196
	    sscc.push(++num);
197
	    arc.push(0U);
198
	    sscc.top().is_accepting
199
              = testing_aut->is_accepting_state(init);
200
	    twa_succ_iterator* iter = testing_aut->succ_iter(init);
201
	    iter->first();
202
	    todo.emplace(init, iter);
203
204
205
206
	  }

	  while (!todo.empty())
	    {
207
	      auto curr = todo.top().first;
208

209
	      auto i = h.find(curr);
210
	      // If we have reached a dead component, ignore it.
211
	      if (i != h.end() && i->second == -1)
212
213
214
215
216
217
		{
		  todo.pop();
		  continue;
		}

	      // We are looking at the next successor in SUCC.
218
	      twa_succ_iterator* succ = todo.top().second;
219
220
221
222
223
224
225
226
227
228

	      // 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,
229
		  assert(i != h.end());
230
231
232
233
234
235
		  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());
236
		  if (sscc.top().index == i->second)
237
238
239
		    {
		      // removing states
		      bool is_livelock_accepting_sscc = (sscc.rem().size() > 1)
240
241
242
			&& ((sscc.top().is_accepting) ||
			    (testing_aut->acc().
			     accepting(sscc.top().condition)));
243
244
		      trace << "*** sscc.size()  = ***" <<  sscc.size() << '\n';
		      for (auto j: sscc.rem())
245
			{
246
			  h[j] = -1;
247

248
			  if (is_livelock_accepting_sscc)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
249
			    {
250
251
252
			      // if it is an accepting sscc add the state to
			      // G (=the livelock-accepting states set)
			      trace << "*** sscc.size() > 1: states: ***"
253
				    << testing_aut->format_state(j)
254
				    << '\n';
255
256
257
			      auto livelock_accepting_state =
				const_cast<state_ta_explicit*>
				(down_cast<const state_ta_explicit*>(j));
258
259
260
261
262
263
264
265
266
267
268
269

			      livelock_accepting_state->
				set_livelock_accepting_state(true);

			      if (single_pass_emptiness_check)
				{
				  livelock_accepting_state
				    ->set_accepting_state(true);
				  livelock_accepting_state
				    ->stuttering_reachable_livelock
				    = livelock_accepting_state;
				}
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
270
			    }
271
			}
272

273
274
275
276
277
278
		      assert(!arc.empty());
		      sscc.pop();
		      arc.pop();
		    }

		  // automata reduction
279
		  testing_aut->delete_stuttering_and_hole_successors(curr);
280
281
282
283
284
285
286

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

	      // Fetch the values destination state we are interested in...
287
	      auto dest = succ->dst();
288

289
	      auto acc_cond = succ->acc();
290
291
292
293
294
295
296
	      // ... 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 =
297
298
		testing_aut->get_state_condition(curr)
		== testing_aut->get_state_condition(dest);
299
	      auto id = h.find(dest);
300
301

	      // Is this a new state?
302
	      if (id == h.end())
303
304
305
306
		{
		  if (!is_stuttering_transition)
		    {
		      init_set.push(dest);
307
		      dest->destroy();
308
309
310
311
312
		      continue;
		    }

		  // Number it, stack it, and register its successors
		  // for later processing.
313
		  h[dest] = ++num;
314
315
316
		  sscc.push(num);
		  arc.push(acc_cond);
		  sscc.top().is_accepting =
317
		    testing_aut->is_accepting_state(dest);
318

319
		  twa_succ_iterator* iter = testing_aut->succ_iter(dest);
320
		  iter->first();
321
		  todo.emplace(dest, iter);
322
323
		  continue;
		}
324
	      dest->destroy();
325
326

	      // If we have reached a dead component, ignore it.
327
	      if (id->second == -1)
328
329
330
331
		continue;

	      trace << "***compute_livelock_acceptance_states: CYCLE***\n";

332
	      if (!curr->compare(id->first))
333
		{
334
335
		  auto self_loop_state = const_cast<state_ta_explicit*>
		    (down_cast<const state_ta_explicit*>(curr));
336
337
		  assert(self_loop_state);

338
339
		  if (testing_aut->is_accepting_state(self_loop_state)
		      || (testing_aut->acc().accepting(acc_cond)))
340
341
342
		    {
		      self_loop_state->set_livelock_accepting_state(true);
		      if (single_pass_emptiness_check)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
343
			{
344
345
346
			  self_loop_state->set_accepting_state(true);
			  self_loop_state->stuttering_reachable_livelock
			    = self_loop_state;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
347
			}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
		    }

		  trace
		    << "***compute_livelock_acceptance_states: CYCLE: "
		    << "self_loop_state***\n";
		}

	      // 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").
366
	      int threshold = id->second;
367
	      std::list<const state*> rem;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	      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);

	    }

	}

397
      if (artificial_livelock_acc_state || single_pass_emptiness_check)
398
	transform_to_single_pass_automaton(testing_aut,
399
400
					   artificial_livelock_acc_state);
    }
401

402
403
404
    ta_explicit_ptr
    build_ta(const ta_explicit_ptr& ta, bdd atomic_propositions_set_,
	     bool degeneralized,
405
	     bool single_pass_emptiness_check,
406
407
	     bool artificial_livelock_state_mode,
	     bool no_livelock)
408
    {
409

410
      std::stack<state_ta_explicit*> todo;
411
      const_twa_ptr tgba_ = ta->get_tgba();
412
413

      // build Initial states set:
414
      auto tgba_init_state = tgba_->get_init_state();
415
416
417

      bdd tgba_condition = tgba_->support_conditions(tgba_init_state);

418
419
420
      bool is_acc = false;
      if (degeneralized)
	{
421
	  twa_succ_iterator* it = tgba_->succ_iter(tgba_init_state);
422
423
	  it->first();
	  if (!it->done())
424
	    is_acc = it->acc() != 0U;
425
426
427
	  delete it;
	}

428
429
430
431
432
433
      bdd satone_tgba_condition;
      while ((satone_tgba_condition = bdd_satoneset(tgba_condition,
						    atomic_propositions_set_,
						    bddtrue)) != bddfalse)
	{
	  tgba_condition -= satone_tgba_condition;
434
435
436
	  state_ta_explicit* init_state = new
	    state_ta_explicit(tgba_init_state->clone(),
			      satone_tgba_condition, true, is_acc);
437
438
439
440
441
442
443
	  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();
444

445
446
447
448
449
      while (!todo.empty())
	{
	  state_ta_explicit* source = todo.top();
	  todo.pop();

450
	  twa_succ_iterator* twa_succ_it =
451
	    tgba_->succ_iter(source->get_tgba_state());
452
453
	  for (twa_succ_it->first(); !twa_succ_it->done();
	       twa_succ_it->next())
454
	    {
455
456
	      const state* tgba_state = twa_succ_it->dst();
	      bdd tgba_condition = twa_succ_it->cond();
457
	      acc_cond::mark_t tgba_acceptance_conditions =
458
                twa_succ_it->acc();
459
460
461
462
463
464
465
466
467
468
	      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;
469
470
471
472

		  bool is_acc = false;
		  if (degeneralized)
		  {
473
		    twa_succ_iterator* it = tgba_->succ_iter(tgba_state);
474
475
		    it->first();
		    if (!it->done())
476
		      is_acc = it->acc() != 0U;
477
478
479
		    delete it;
		  }

480
481
482
483
484
485
486
		  if (satone_tgba_condition == source->get_tgba_condition())
		    while ((dest_condition =
			    bdd_satoneset(all_props,
					  atomic_propositions_set_, bddtrue))
			   != bddfalse)
		      {
			all_props -= dest_condition;
487
488
489
			state_ta_explicit* new_dest =
			  new state_ta_explicit(tgba_state->clone(),
						dest_condition, false, is_acc);
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
			state_ta_explicit* dest = ta->add_state(new_dest);

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

			bdd cs = bdd_setxor(source->get_tgba_condition(),
					    dest->get_tgba_condition());
			ta->create_transition(source, cs,
					      tgba_acceptance_conditions, dest);
		      }
		}
	      tgba_state->destroy();
	    }
511
	  delete twa_succ_it;
512
513
	}

514
515
516
      if (no_livelock)
	return ta;

517
      state_ta_explicit* artificial_livelock_acc_state = nullptr;
518
519
520
521
522
523
524
525
526

      trace << "*** build_ta: artificial_livelock_acc_state_mode = ***"
	    << artificial_livelock_state_mode << std::endl;

      if (artificial_livelock_state_mode)
	{
	  single_pass_emptiness_check = true;
	  artificial_livelock_acc_state =
	    new state_ta_explicit(ta->get_tgba()->get_init_state(), bddtrue,
527
				  false, false, true, nullptr);
528
529
530
531
532
533
534
535
536
	  trace
	    << "*** build_ta: artificial_livelock_acc_state = ***"
	    << artificial_livelock_acc_state << std::endl;
	}

      compute_livelock_acceptance_states(ta, single_pass_emptiness_check,
					 artificial_livelock_acc_state);
      return ta;
    }
537
538
  }

539
  ta_explicit_ptr
540
  tgba_to_ta(const const_twa_ptr& tgba_, bdd atomic_propositions_set_,
541
542
543
544
	     bool degeneralized, bool artificial_initial_state_mode,
	     bool single_pass_emptiness_check,
	     bool artificial_livelock_state_mode,
	     bool no_livelock)
545
  {
546
    ta_explicit_ptr ta;
547

548
    auto tgba_init_state = tgba_->get_init_state();
549
550
    if (artificial_initial_state_mode)
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
551
552
        state_ta_explicit* artificial_init_state =
	  new state_ta_explicit(tgba_init_state->clone(), bddfalse, true);
553

554
        ta = make_ta_explicit(tgba_, tgba_->acc().num_sets(),
555
			      artificial_init_state);
556
557
558
      }
    else
      {
559
        ta = make_ta_explicit(tgba_, tgba_->acc().num_sets());
560
561
562
      }
    tgba_init_state->destroy();

563
    // build ta automaton
564
    build_ta(ta, atomic_propositions_set_, degeneralized,
565
566
	     single_pass_emptiness_check, artificial_livelock_state_mode,
	     no_livelock);
567
568
569
570
571
572
573
574
575

    // (degeneralized=true) => TA
    if (degeneralized)
      return ta;

    // (degeneralized=false) => GTA
    // adapt a GTA to remove acceptance conditions from states
    ta::states_set_t states_set = ta->get_states_set();
    ta::states_set_t::iterator it;
576
    for (it = states_set.begin(); it != states_set.end(); ++it)
577
578
579
580
581
582
583
584
      {
        state_ta_explicit* state = static_cast<state_ta_explicit*> (*it);

        if (state->is_accepting_state())
          {
            state_ta_explicit::transitions* trans = state->get_transitions();
            state_ta_explicit::transitions::iterator it_trans;

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
585
            for (it_trans = trans->begin(); it_trans != trans->end();
586
		 ++it_trans)
587
	      (*it_trans)->acceptance_conditions = ta->acc().all_sets();
588
589
590
591
592

            state->set_accepting_state(false);
          }
      }

593
594
595
    return ta;
  }

596
  tgta_explicit_ptr
597
  tgba_to_tgta(const const_twa_ptr& tgba_, bdd atomic_propositions_set_)
598
  {
599
    auto tgba_init_state = tgba_->get_init_state();
600
601
    auto artificial_init_state = new state_ta_explicit(tgba_init_state->clone(),
						       bddfalse, true);
602
603
    tgba_init_state->destroy();

604
    auto tgta = make_tgta_explicit(tgba_, tgba_->acc().num_sets(),
605
				   artificial_init_state);
606

607
608
    // build a Generalized TA automaton involving a single_pass_emptiness_check
    // (without an artificial livelock state):
609
    auto ta = tgta->get_ta();
610
    build_ta(ta, atomic_propositions_set_, false, true, false, false);
611

612
    trace << "***tgba_to_tgbta: POST build_ta***" << std::endl;
613

614
    // adapt a ta automata to build tgta automata :
615
    ta::states_set_t states_set = ta->get_states_set();
616
    ta::states_set_t::iterator it;
617
    twa_succ_iterator* initial_states_iter =
618
      ta->succ_iter(ta->get_artificial_initial_state());
619
620
    initial_states_iter->first();
    if (initial_states_iter->done())
621
622
623
624
      {
	delete initial_states_iter;
	return tgta;
      }
625
    bdd first_state_condition = initial_states_iter->cond();
626
627
628
    delete initial_states_iter;

    bdd bdd_stutering_transition = bdd_setxor(first_state_condition,
629
					      first_state_condition);
630

631
    for (it = states_set.begin(); it != states_set.end(); ++it)
632
633
634
635
636
637
      {
        state_ta_explicit* state = static_cast<state_ta_explicit*> (*it);

        state_ta_explicit::transitions* trans = state->get_transitions();
        if (state->is_livelock_accepting_state())
          {
638
            bool trans_empty = !trans || trans->empty();
639
            if (trans_empty || state->is_accepting_state())
640
              {
641
                ta->create_transition(state, bdd_stutering_transition,
642
				      ta->acc().all_sets(), state);
643
644
645
              }
          }

646
        if (state->compare(ta->get_artificial_initial_state()))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
647
          ta->create_transition(state, bdd_stutering_transition,
648
				0U, state);
649

650
651
        state->set_livelock_accepting_state(false);
        state->set_accepting_state(false);
652
        trace << "***tgba_to_tgbta: POST create_transition ***" << std::endl;
653
654
      }

655
    return tgta;
656
  }
657
}