complete.cc 3.33 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2013, 2014, 2015 Laboratoire de Recherche et
// Développement de l'Epita.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// 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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.

#include "complete.hh"

namespace spot
{
24
  unsigned tgba_complete_here(tgba_digraph_ptr aut)
25
  {
26
27
    unsigned n = aut->num_states();
    unsigned sink = -1U;
28
29
    acc_cond::mark_t allacc = aut->acc().all_sets();
    if (allacc == 0U)
30
      {
31
32
33
	// We cannot safely complete an automaton if it has no
	// acceptance set as the added sink would become accepting.
	// In this case, add an acceptance set to all transitions.
34
	allacc = aut->set_single_acceptance_set();
35
	for (auto& t: aut->transition_vector())
36
	  t.acc = allacc;
37
      }
38
    else
39
      {
40
41
42
43
	// If some acceptance sets were used, loop over the states and
	// seek a state that has only self loops, and that is not
	// accepting.  This will be our sink state.
	for (unsigned i = 0; i < n; ++i)
44
	  {
45
	    bool selfloop = true;
46
	    acc_cond::mark_t accsum = 0U;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	    for (auto& t: aut->out(i))
	      {
		if (t.dst != i)	// Not a self-loop
		  {
		    selfloop = false;
		    break;
		  }
		accsum |= t.acc;
	      }
	    if (selfloop && accsum != allacc) // We have found a sink!
	      {
		sink = i;
		break;
	      }
61
62
63
	  }
      }

64
65
    // Now complete all states (including the sink).
    for (unsigned i = 0; i < n; ++i)
66
      {
67
	bdd missingcond = bddtrue;
68
	acc_cond::mark_t acc = 0U;
69
	for (auto& t: aut->out(i))
70
	  {
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	    missingcond -= t.cond;
	    // FIXME: This is ugly.
	    //
	    // In case the automaton uses state-based acceptance, we
	    // need to put the new transition in the same set as all
	    // the other.
	    //
	    // In case the automaton uses transition-based acceptance,
	    // it does not matter what acceptance set we put the new
	    // transition into.
	    //
	    // So in both cases, we put the transition in the same
	    // acceptance sets as the last outgoing transition of the
	    // state.
	    acc = t.acc;
86
	  }
87
88
	// If the state has incomplete successors, we need to add a
	// transition to some sink state.
89
	if (missingcond != bddfalse)
90
91
92
93
94
95
96
97
98
99
100
	  {
	    // If we haven't found any sink, simply add one.
	    if (sink == -1U)
	      {
		sink = aut->new_state();
		++n;
	      }
	    // In case the automaton use state-based acceptance, propagate
	    // the acceptance of the first transition to the one we add.
	    aut->new_transition(i, sink, missingcond, acc);
	  }
101
      }
102
103
    return sink;
  }
104

105
  tgba_digraph_ptr tgba_complete(const const_tgba_ptr& aut)
106
  {
107
    auto res = make_tgba_digraph(aut, {
108
109
110
111
112
113
					true, // state based
					true, // single acc
					true, // inherently_weak
					true, // deterministic
					true, // stutter inv.
				      });
114
115
    tgba_complete_here(res);
    return res;
116
117
  }

118
}