powerset.cc 8.14 KB
Newer Older
1
2
// Copyright (C) 2009, 2010, 2011, 2013 Laboratoire de Recherche et
// Dveloppement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
3
// Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
4
5
6
7
8
9
10
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// et Marie Curie.
//
// 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
11
// the Free Software Foundation; either version 3 of the License, or
12
13
14
15
16
17
18
19
// (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
20
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22
23

#include <set>
#include <deque>
24
25
#include <iterator>
#include <vector>
26
#include "powerset.hh"
27
28
29
#include "misc/hash.hh"
#include "tgbaalgos/powerset.hh"
#include "bdd.h"
30
31
32
33
34
35
#include "tgbaalgos/scc.hh"
#include "tgbaalgos/cycles.hh"
#include "tgbaalgos/gtec/gtec.hh"
#include "tgba/tgbaproduct.hh"
#include "tgba/bddprint.hh"
#include "tgbaalgos/dotty.hh"
36
37
38

namespace spot
{
39
  tgba_explicit_number*
40
  tgba_powerset(const tgba* aut, power_map& pm, bool merge)
41
  {
42
43
44
    typedef power_map::power_state power_state;
    typedef std::map<power_map::power_state, int> power_set;
    typedef std::deque<power_map::power_state> todo_list;
45
46
47

    power_set seen;
    todo_list todo;
48
    tgba_explicit_number* res = new tgba_explicit_number(aut->get_dict());
49
50
51
52

    {
      power_state ps;
      state* s = aut->get_init_state();
53
      pm.states.insert(s);
54
55
      ps.insert(s);
      todo.push_back(ps);
56
      seen[ps] = 1;
57
      pm.map_[1] = ps;
58
59
60
61
    }

    unsigned state_num = 1;

62
    while (!todo.empty())
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
      {
	power_state src = todo.front();
	todo.pop_front();
	// Compute all variables occurring on outgoing arcs.
	bdd all_vars = bddtrue;
	power_state::const_iterator i;

	for (i = src.begin(); i != src.end(); ++i)
	  all_vars &= aut->support_variables(*i);

	// Compute all possible combinations of these variables.
	bdd all_conds = bddtrue;
	while (all_conds != bddfalse)
	  {
	    bdd cond = bdd_satoneset(all_conds, all_vars, bddtrue);
	    all_conds -= cond;

	    // Construct the set of all states reachable via COND.
	    power_state dest;
	    for (i = src.begin(); i != src.end(); ++i)
	      {
		tgba_succ_iterator *si = aut->succ_iter(*i);
85
		for (si->first(); !si->done(); si->next())
86
87
		  if ((cond >> si->current_condition()) == bddtrue)
		    {
88
		      const state* s = pm.canonicalize(si->current_state());
89
90
91
92
93
94
95
96
		      dest.insert(s);
		    }
		delete si;
	      }
	    if (dest.empty())
	      continue;
	    // Add that transition.
	    power_set::const_iterator i = seen.find(dest);
97
	    int dest_num;
Pierre PARUTTO's avatar
Pierre PARUTTO committed
98
	    state_explicit_number::transition* t;
99
100
	    if (i != seen.end())
	      {
101
102
		dest_num = i->second;
                t = res->create_transition(seen[src], dest_num);
103
104
105
	      }
	    else
	      {
106
107
		dest_num = ++state_num;
		seen[dest] = dest_num;
108
		pm.map_[dest_num] = dest;
109
		todo.push_back(dest);
110
                t = res->create_transition(seen[src], dest_num);
111
112
113
114
	      }
	    res->add_conditions(t, cond);
	  }
      }
115
116
    if (merge)
      res->merge_transitions();
117
118
    return res;
  }
119
120
121
122
123
124
125

  tgba_explicit_number*
  tgba_powerset(const tgba* aut)
  {
    power_map pm;
    return tgba_powerset(aut, pm);
  }
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
172
173
174
175
176
177
178
179
180
181
182
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311


  namespace
  {

    class fix_scc_acceptance: protected enumerate_cycles
    {
    public:
      typedef dfs_stack::const_iterator cycle_iter;
      typedef state_explicit_number::transition trans;
      typedef std::set<trans*> trans_set;
      typedef std::vector<trans_set> set_set;
    protected:
      const tgba* ref_;
      power_map& refmap_;
      trans_set reject_; // set of rejecting transitions
      set_set accept_;	// set of cycles that are accepting
      trans_set all_; // all non rejecting transitions

    public:
      fix_scc_acceptance(const scc_map& sm, const tgba* ref, power_map& refmap)
	: enumerate_cycles(sm), ref_(ref), refmap_(refmap)
      {
      }

      void fix_scc(const int m)
      {
	reject_.clear();
	accept_.clear();
	run(m);

//	std::cerr << "SCC #" << m << "\n";
//	std::cerr << "REJECT: ";
//	print_set(std::cerr, reject_) << "\n";
//	std::cerr << "ALL: ";
//	print_set(std::cerr, all_) << "\n";
//	for (set_set::const_iterator j = accept_.begin();
//	     j != accept_.end(); ++j)
//	  {
//	    std::cerr << "ACCEPT: ";
//	    print_set(std::cerr, *j) << "\n";
//	  }

	bdd acc = aut_->all_acceptance_conditions();
	for (trans_set::iterator i = all_.begin(); i != all_.end(); ++i)
	  {
	    (*i)->acceptance_conditions = acc;
	  }
      }

      bool is_cycle_accepting(cycle_iter begin, trans_set& ts) const
      {
	tgba_explicit_number* a =
	  down_cast<tgba_explicit_number*>(const_cast<tgba*>(aut_));
	// Build an automaton representing this loop.
	tgba_explicit_number loop_a(aut_->get_dict());
	int loop_size = std::distance(begin, dfs_.end());
	int n;
	cycle_iter i;
	for (n = 1, i = begin; n <= loop_size; ++n, ++i)
	  {
	    trans* t = a->get_transition(i->succ);
	    loop_a.create_transition(n - 1, n % loop_size)->condition =
	      t->condition;
	    if (reject_.find(t) == reject_.end())
	      ts.insert(t);
	  }
	assert(i == dfs_.end());

	const state* loop_a_init = loop_a.get_init_state();
	assert(loop_a.get_label(loop_a_init) == 0);

	// Check if the loop is accepting in the original automaton.
	bool accepting = false;

	// Iterate on each original state corresponding to the
	// start of the loop in the determinized automaton.
	const power_map::power_state& ps =
	  refmap_.states_of(a->get_label(begin->ts->first));
	for (power_map::power_state::const_iterator it = ps.begin();
	     it != ps.end() && !accepting; ++it)
	  {
	    // Construct a product between
	    // LOOP_A, and ORIG_A starting in *IT.

	    tgba* p = new tgba_product_init(&loop_a, ref_,
					    loop_a_init, *it);

	    //spot::dotty_reachable(std::cout, p);

	    couvreur99_check* ec = down_cast<couvreur99_check*>(couvreur99(p));
	    assert(ec);
	    emptiness_check_result* res = ec->check();
	    if (res)
	      accepting = true;
	    delete res;
	    delete ec;
	    delete p;
	  }

	loop_a_init->destroy();
	return accepting;
      }

      std::ostream&
      print_set(std::ostream& o, const trans_set& s) const
      {
	o << "{ ";
	for (trans_set::const_iterator i = s.begin(); i != s.end(); ++i)
	  o << *i << " ";
	o << "}";
	return o;
      }

      virtual bool
      cycle_found(const state* start)
      {
	cycle_iter i = dfs_.begin();
	while (i->ts->first != start)
	  ++i;
	trans_set ts;
	bool is_acc = is_cycle_accepting(i, ts);
	do
	  {
	    //	    std::cerr << aut_->format_state(i->ts->first) << " ";
	    ++i;
	  }
	while (i != dfs_.end());
	//	std::cerr << "  acc=" << is_acc << "  (";
	//	bdd_print_accset(std::cerr, aut_->get_dict(), s) << ") ";
	//	print_set(std::cerr, ts) << "\n";
	if (is_acc)
	  {
	    accept_.push_back(ts);
	    all_.insert(ts.begin(), ts.end());
	  }
	else
	  {
	    for (trans_set::const_iterator i = ts.begin(); i != ts.end(); ++i)
	      {
		trans* t = *i;
		reject_.insert(t);
		for (set_set::iterator j = accept_.begin();
		     j != accept_.end(); ++j)
		  {
		    j->erase(t);
		  }
		all_.erase(t);
	      }
	  }

	return true;
      }
    };

    static void
    fix_dba_acceptance(tgba_explicit_number* det,
		       const tgba* ref, power_map& refmap)
    {
      det->copy_acceptance_conditions_of(ref);

      scc_map sm(det);
      sm.build_map();

      unsigned scc_count = sm.scc_count();

      fix_scc_acceptance fsa(sm, ref, refmap);

      for (unsigned m = 0; m < scc_count; ++m)
	fsa.fix_scc(m);
    }

  }

  tgba_explicit_number*
  tba_determinize(const tgba* aut)
  {
    power_map pm;
    // Do not merge transitions in the deterministic automaton.  If we
    // add two self-loops labeled by "a" and "!a", we do not want
    // these to be merged as "1" before the acceptance has been fixed.
    tgba_explicit_number* det = tgba_powerset(aut, pm, false);
    fix_dba_acceptance(det, aut, pm);
    det->merge_transitions();
    return det;
  }
312
}