sccfilter.cc 15 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Laboratoire de
// 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
#include "sccfilter.hh"
21
#include "reachiter.hh"
22
23
#include "scc.hh"
#include "sccinfo.hh"
24
25
26
27
28

namespace spot
{
  namespace
  {
29
30
31
32
    // BDD.id -> Acc number
    typedef std::map<int, unsigned> accremap_t;
    typedef std::vector<accremap_t> remap_table_t;

33
    typedef std::tuple<bool, bdd, bdd> filtered_trans;
34
    typedef std::pair<bdd, bdd> acc_pair;
35

36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    // SCC filters are objects with two methods:
    //  state(src) return true iff s should be kept
    //  trans(src, dst, cond, acc) returns a triplet
    //     (keep, cond2, acc2) where keep is a Boolean stating if the
    //                      transition should be kept, and cond2/acc2
    //                      give replacement values for cond/acc
    struct id_filter
    {
      scc_info* si;
      id_filter(scc_info* si)
	: si(si)
      {
      }

      // Accept all states
      bool state(unsigned)
      {
	return true;
      }

57
58
59
60
61
      acc_pair accsets(bdd all, bdd all_neg)
      {
	return acc_pair(all, all_neg);
      }

62
63
64
65
66
67
68
69
      // Accept all transitions, unmodified
      filtered_trans trans(unsigned, unsigned, bdd cond, bdd acc)
      {
	return filtered_trans{true, cond, acc};
      }
    };

    // Remove useless states.
70
71
    template <class next_filter = id_filter>
    struct state_filter: next_filter
72
    {
73
74
75
      template<typename... Args>
      state_filter(scc_info* si, Args&&... args)
	: next_filter(si, std::forward<Args>(args)...)
76
77
      {
      }
78
79
80

      bool state(unsigned s)
      {
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
	return this->next_filter::state(s) && this->si->is_useful_state(s);
      }
    };

    // Suspension filter, used only by compsusp.cc
    template <class next_filter = id_filter>
    struct susp_filter: next_filter
    {
      bdd suspvars;
      bdd ignoredvars;
      bool early_susp;

      template<typename... Args>
      susp_filter(scc_info* si,
		  bdd suspvars, bdd ignoredvars, bool early_susp,
		  Args&&... args)
	: next_filter(si, std::forward<Args>(args)...),
	  suspvars(suspvars),
	  ignoredvars(ignoredvars),
	  early_susp(early_susp)
      {
      }

      filtered_trans trans(unsigned src, unsigned dst,
			   bdd cond, bdd acc)
      {
	bool keep;
	std::tie(keep, cond, acc) =
	  this->next_filter::trans(src, dst, cond, acc);

	if (keep)
	  {
	    // Always remove ignored variables
	    cond = bdd_exist(cond, ignoredvars);

	    // Remove the suspension variables only if
	    // the destination in not in an accepting SCC,
	    // or if we are between SCC with early_susp unset.
	    unsigned u = this->si->scc_of(dst);
	    if (!this->si->is_accepting_scc(u)
		|| (!early_susp && (u != this->si->scc_of(src))))
	      cond = bdd_exist(cond, suspvars);
	  }

	return filtered_trans(keep, cond, acc);
126
127
128
129
130
      }
    };

    // Remove acceptance conditions from all transitions outside of
    // non-accepting SCCs.
131
132
    template <class next_filter = id_filter>
    struct acc_filter_all: next_filter
133
    {
134
135
136
      template<typename... Args>
      acc_filter_all(scc_info* si, Args&&... args)
	: next_filter(si, std::forward<Args>(args)...)
137
138
139
140
141
142
      {
      }

      filtered_trans trans(unsigned src, unsigned dst,
			   bdd cond, bdd acc)
      {
143
144
145
	bool keep;
	std::tie(keep, cond, acc) =
	  this->next_filter::trans(src, dst, cond, acc);
146

147
148
149
150
151
152
153
154
155
156
	if (keep)
	  {
	    unsigned u = this->si->scc_of(src);
	    // If the transition is between two SCCs, or in a
	    // non-accepting SCC.  Remove the acceptance sets.
	    if (!this->si->is_accepting_scc(u) || u != this->si->scc_of(dst))
	      acc = bddfalse;
	  }

	return filtered_trans(keep, cond, acc);
157
158
159
160
161
      }
    };

    // Remove acceptance conditions from all transitions whose
    // destination is not an accepting SCCs.
162
163
    template <class next_filter = id_filter>
    struct acc_filter_some: next_filter
164
    {
165
166
167
      template<typename... Args>
      acc_filter_some(scc_info* si, Args&&... args)
	: next_filter(si, std::forward<Args>(args)...)
168
169
170
      {
      }

171
      filtered_trans trans(unsigned src, unsigned dst,
172
173
			   bdd cond, bdd acc)
      {
174
175
176
177
178
	bool keep;
	std::tie(keep, cond, acc) =
	  this->next_filter::trans(src, dst, cond, acc);

	if (!this->si->is_accepting_scc(this->si->scc_of(dst)))
179
	  acc = bddfalse;
180
	return filtered_trans(keep, cond, acc);
181
182
183
      }
    };

184
185
186
    // Simplify redundant acceptance sets used in each SCCs.
    template <class next_filter = id_filter>
    struct acc_filter_simplify: next_filter
187
188
189
190
191
192
    {
      std::vector<bdd> acc_;
      typedef std::map<int, bdd> map_t;
      typedef std::vector<map_t> remap_t;
      remap_t remap_;

193
194
195
      template<typename... Args>
      acc_filter_simplify(scc_info* si, Args&&... args)
	: next_filter(si, std::forward<Args>(args)...)
196
197
198
199
200
      {
      }

      acc_pair accsets(bdd in_all, bdd in_all_neg)
      {
201
202
203
204
	std::tie(in_all, in_all_neg) =
	  this->next_filter::accsets(in_all, in_all_neg);

	unsigned scc_count = this->si->scc_count();
205
206
207
208
209
	remap_table_t remap_table(scc_count);
	std::vector<unsigned> max_table(scc_count);
	std::vector<bdd> useful_table(scc_count);
	std::vector<bdd> useless_table(scc_count);
	unsigned max_num = 1;
210
	const tgba_digraph* aut = this->si->get_aut();
211

212
	std::vector<bdd> used_acc = this->si->used_acc();
213
214
215

	for (unsigned n = 0; n < scc_count; ++n)
	  {
216
	    if (!this->si->is_accepting_scc(n))
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
312
313
314
315
	      continue;
	    bdd all = used_acc[n];

	    //std::cerr << "SCC #" << n << "; used = " << all << std::endl;

	    // Compute a set of useless acceptance sets.
	    // If the acceptance combinations occurring in
	    // the automata are  { a, ab, abc, bd }, then
	    // ALL contains (a&!b&!c&!d)|(a&b&!c&!d)|(a&b&c&!d)|(!a&b&!c&d)
	    // and we want to find that 'a' and 'b' are useless because
	    // they always occur with 'c'.
	    // The way we check if 'a' is useless is to look whether ALL
	    // implications (x -> a) for some other acceptance set x.
	    //
	    // The two while() loops in the code perform the equivalent of
	    //    for all a in allconds_a:
	    //       for all x in allconds_x:
	    //          check whether x -> a
	    //          ...
	    //
	    // Initially allconds_a = allconds_x contains all acceptance
	    // sets.  However when an acceptance set 'a' is determined to
	    // be useless, it can be removed from allconds_x from future
	    // iterations.
	    bdd allconds_a = bdd_support(in_all_neg);
	    bdd allconds_x = allconds_a;
	    bdd useless = bddtrue;
	    while (allconds_a != bddtrue)
	      {
		// Speed-up the computation of implied acceptance sets by
		// removing those that are always present.  We detect
		// those that appear as conjunction of positive variables
		// at the start of ALL.
		bdd prefix = bdd_satprefix(all);
		if (prefix != bddtrue)
		  {
		    assert(prefix == bdd_support(prefix));
		    allconds_a = bdd_exist(allconds_a, prefix);
		    if (allconds_a != bddtrue)
		      {
			useless &= prefix;
		      }
		    else
		      {
			// Never erase all conditions: at least keep one.
			useless &= bdd_high(prefix);
			break;
		      }
		    allconds_x = bdd_exist(allconds_x, prefix);
		  }

		// Pick an acceptance set 'a'.
		bdd a = bdd_ithvar(bdd_var(allconds_a));
		// For all acceptance sets 'x' that are not already
		// useless...
		bdd others = allconds_x;
		while (others != bddtrue)
		  {
		    bdd x = bdd_ithvar(bdd_var(others));
		    // ... check whether 'x' always implies 'a'.
		    if (x != a && bdd_implies(all, x >> a))
		      {
			// If so, 'a' is useless.
			all = bdd_exist(all, a);
			useless &= a;
			allconds_x = bdd_exist(allconds_x, a);
			break;
		      }
		    others = bdd_high(others);
		  }
		allconds_a = bdd_high(allconds_a);
	      }

	    // We never remove ALL acceptance marks.
	    assert(in_all_neg == bddtrue || useless != bdd_support(in_all_neg));

	    useless_table[n] = useless;
	    bdd useful = bdd_exist(in_all_neg, useless);

	    //std::cerr << "SCC #" << n << "; useful = " << useful << std::endl;

	    // Go over all useful sets of acceptance marks, and give them
	    // a number.
	    unsigned num = 1;
	    // First compute the number of acceptance conditions used.
	    for (BDD c = useful.id(); c != 1; c = bdd_low(c))
	      ++num;
	    max_table[n] = num;
	    if (num > max_num)
	      max_num = num;

	    useful_table[n] = useful;
	  }

	// Now that we know about the max number of acceptance
	// conditions, add extra acceptance conditions to those SCC
	// that do not have enough.
	for (unsigned n = 0; n < scc_count; ++n)
	  {
316
	    if (!this->si->is_accepting_scc(n))
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	      continue;
	    //std::cerr << "SCC " << n << '\n';
	    bdd useful = useful_table[n];

	    int missing = max_num - max_table[n];
	    bdd useless = useless_table[n];
	    while (missing--)
	      {
		//std::cerr << useful << "  :  " << useless << std::endl;
		useful &= bdd_nithvar(bdd_var(useless));
		useless = bdd_high(useless);
	      }
	    int num = max_num;
	    // Then number all of these acceptance conditions in the
	    // reverse order.  This makes sure that the associated number
	    // varies in the same direction as the bdd variables, which in
	    // turn makes sure we preserve the acceptance condition
	    // ordering (which matters for degeneralization).
	    for (BDD c = useful.id(); c != 1; c = bdd_low(c))
336
	      remap_table[n].emplace(bdd_var(c), --num);
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379

	    max_table[n] = max_num;
	  }

	acc_.resize(max_num);
	acc_[0] = bddfalse;
	bdd tmp = in_all;
	assert(aut->number_of_acceptance_conditions() >= max_num - 1);

	bdd all = bddfalse;
	bdd all_neg = bddtrue;

	if (tmp != bddfalse)
	  {
	    for (unsigned n = max_num - 1; n > 0; --n)
	      {
	    	assert(tmp != bddfalse);
		int v = bdd_var(tmp);
		bdd vn = bdd_nithvar(v);
		all = (all & vn) | (all_neg & bdd_ithvar(v));
		all_neg &= vn;
	    	tmp = bdd_low(tmp);
	      }
	    tmp = in_all;
	    for (unsigned n = max_num - 1; n > 0; --n)
	      {
		int v = bdd_var(tmp);
		acc_[n] = bdd_compose(all_neg, bdd_nithvar(v), v);
		tmp = bdd_low(tmp);
	      }
	  }
	else
	  {
	    assert(max_num == 1);
	  }

	remap_.resize(scc_count);
	bdd all_orig_neg = in_all_neg;
	bdd all_sup = bdd_support(all_orig_neg);

	for (unsigned n = 0; n < scc_count; ++n)
	  {
	    //std::cerr << "SCC #" << n << '\n';
380
	    if (!this->si->is_accepting_scc(n))
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
	      continue;

	    bdd all = used_acc[n];
	    while (all != bddfalse)
	      {
		bdd one = bdd_satoneset(all, all_sup, bddtrue);
		all -= one;
		bdd res = bddfalse;
		bdd resacc = bddfalse;
		while (one != bddtrue)
		  {
		    if (bdd_high(one) == bddfalse)
		      {
			one = bdd_low(one);
			continue;
		      }
		    int vn = bdd_var(one);
		    bdd v = bdd_ithvar(vn);
		    resacc |= bdd_exist(all_orig_neg, v) & v;
		    res |= acc_[remap_table[n][vn]];
		    one = bdd_high(one);
		  }
		int id = resacc.id();
		//std::cerr << resacc << " -> " << res << '\n';
		remap_[n][id] = res;
	      }
	  }
	return acc_pair{all, all_neg};
      }

411
      filtered_trans trans(unsigned src, unsigned dst, bdd cond, bdd acc)
412
      {
413
414
415
416
417
	bool keep;
	std::tie(keep, cond, acc) =
	  this->next_filter::trans(src, dst, cond, acc);

	if (keep && acc != bddfalse)
418
	  {
419
420
	    unsigned u = this->si->scc_of(dst);

421
422
423
424
425
426
	    auto i = remap_[u].find(acc.id());
	    if (i != remap_[u].end())
	      acc = i->second;
	    else
	      acc = bddfalse;
	  }
427
	return filtered_trans{keep, cond, acc};
428
429
430
431
432
      }

    };


433
    template<class F, typename... Args>
434
    tgba_digraph* scc_filter_apply(const tgba_digraph* aut,
435
				   scc_info* given_si, Args&&... args)
436
    {
437
438
439
440
441
442
      bdd_dict* bd = aut->get_dict();
      tgba_digraph* filtered = new tgba_digraph(bd);
      unsigned in_n = aut->num_states(); // Number of input states.
      if (in_n == 0)			 // Nothing to filter.
	return filtered;

443
444
445
446
447
      // Compute scc_info if not supplied.
      scc_info* si = given_si;
      if (!si)
	si = new scc_info(aut);

448
      F filter(si, std::forward<Args>(args)...);
449
450
451
452
453
454
455
456
457
458
459
460

      // Renumber all useful states.
      unsigned out_n = 0;		 // Number of output states.
      std::vector<unsigned> inout; // Associate old states to new ones.
      inout.reserve(in_n);
      for (unsigned i = 0; i < in_n; ++i)
	if (filter.state(i))
	  inout.push_back(out_n++);
	else
	  inout.push_back(-1U);

      bd->register_all_variables_of(aut, filtered);
461
462
463
464
465
      {
	bdd all = aut->all_acceptance_conditions();
	bdd neg = aut->neg_acceptance_conditions();
	filtered->set_acceptance_conditions(filter.accsets(all, neg).first);
      }
466
      filtered->new_states(out_n);
467
468
469
470
471
      for (unsigned isrc = 0; isrc < in_n; ++isrc)
	{
	  unsigned osrc = inout[isrc];
	  if (osrc >= out_n)
	    continue;
472
	  for (auto& t: aut->out(isrc))
473
474
475
476
477
478
479
480
481
482
	    {
	      unsigned odst = inout[t.dst];
	      if (odst >= out_n)
		continue;
	      bool want;
	      bdd cond;
	      bdd acc;
	      std::tie(want, cond, acc) =
		filter.trans(isrc, t.dst, t.cond, t.acc);
	      if (want)
483
		filtered->new_transition(osrc, odst, cond, acc);
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	    }
	}
      if (!given_si)
	delete si;
      // If the initial state has been filtered out, we don't attempt
      // to fix it.
      auto init = inout[aut->get_init_state_number()];
      if (init < out_n)
	filtered->set_init_state(init);
      return filtered;
    }


  }

  tgba_digraph*
  scc_filter_states(const tgba_digraph* aut, scc_info* given_si)
  {
502
503
504
505
506
    bool sb = aut->get_bprop(tgba_digraph::StateBasedAcc);
    auto res = scc_filter_apply<state_filter<>>(aut, given_si);
    if (sb)
      res->set_bprop(tgba_digraph::StateBasedAcc);
    return res;
507
508
509
510
511
512
  }

  tgba_digraph*
  scc_filter(const tgba_digraph* aut, bool remove_all_useless,
	     scc_info* given_si)
  {
513
    tgba_digraph* res;
514
    if (remove_all_useless)
515
516
517
      res = scc_filter_apply<state_filter
			     <acc_filter_all
			      <acc_filter_simplify<>>>>(aut, given_si);
518
    else
519
520
521
522
523
      res = scc_filter_apply<state_filter
			     <acc_filter_some
			      <acc_filter_simplify<>>>>(aut, given_si);
    res->merge_transitions();
    return res;
524
525
526
527
528
529
530
  }

  tgba_digraph*
  scc_filter_susp(const tgba_digraph* aut, bool remove_all_useless,
		  bdd suspvars, bdd ignoredvars, bool early_susp,
		  scc_info* given_si)
  {
531
    tgba_digraph* res;
532
    if (remove_all_useless)
533
534
535
536
537
538
539
      res = scc_filter_apply<susp_filter
			     <state_filter
			      <acc_filter_all
			       <acc_filter_simplify<>>>>>(aut, given_si,
							  suspvars,
							  ignoredvars,
							  early_susp);
540
    else
541
542
543
544
545
546
547
548
549
      res = scc_filter_apply<susp_filter
			     <state_filter
			      <acc_filter_some
			       <acc_filter_simplify<>>>>>(aut, given_si,
							  suspvars,
							  ignoredvars,
							  early_susp);
    res->merge_transitions();
    return res;
550
551
  }

552
}