remfin.cc 8.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// -*- coding: utf-8 -*-
// Copyright (C) 2015 Laboratoire de Recherche et
// Développement de l'Epita.
//
// 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 "remfin.hh"
#include "sccinfo.hh"
#include <iostream>
23
#include "cleanacc.hh"
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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

//#define TRACE
#ifdef TRACE
#define trace std::cerr
#else
#define trace while (0) std::cerr
#endif

namespace spot
{
  namespace
  {
    // If the DNF is
    //  Fin(1)&Inf(2)&Inf(4) | Fin(2)&Fin(3)&Inf(1) |
    //  Inf(1)&Inf(3) | Inf(1)&Inf(2) | Fin(4)
    // this returns the following map:
    //  {1}   => Inf(2)&Inf(4)
    //  {2,3} => Inf(1)
    //  {}    => Inf(3) | Inf(2)
    //  {4}   => t
    static std::map<acc_cond::mark_t, acc_cond::acc_code>
    split_dnf_acc_by_fin(const acc_cond::acc_code& acc)
    {
      std::map<acc_cond::mark_t, acc_cond::acc_code> res;
      auto pos = &acc.back();
      if (pos->op == acc_cond::acc_op::Or)
	--pos;
      auto start = &acc.front();
      while (pos > start)
	{
	  if (pos->op == acc_cond::acc_op::Fin)
	    {
	      // We have only a Fin term, without Inf.  In this case
	      // only, the Fin() may encode a disjunction of sets.
	      for (auto s: pos[-1].mark.sets())
		{
		  acc_cond::mark_t fin = 0U;
		  fin.set(s);
		  res[fin] = acc_cond::acc_code{};
		}
	      pos -= pos->size + 1;
	    }
	  else
	    {
	      // We have a conjunction of Fin and Inf sets.
	      auto end = pos - pos->size - 1;
	      acc_cond::mark_t fin = 0U;
	      acc_cond::mark_t inf = 0U;
	      while (pos > end)
		{
		  switch (pos->op)
		    {
		    case acc_cond::acc_op::And:
		      --pos;
		      break;
		    case acc_cond::acc_op::Fin:
		      fin |= pos[-1].mark;
		      assert(pos[-1].mark.count() == 1);
		      pos -= 2;
		      break;
		    case acc_cond::acc_op::Inf:
		      inf |= pos[-1].mark;
		      pos -= 2;
		      break;
		    case acc_cond::acc_op::FinNeg:
		    case acc_cond::acc_op::InfNeg:
		    case acc_cond::acc_op::Or:
		      SPOT_UNREACHABLE();
		      break;
		    }
		}
	      assert(pos == end);
	      acc_cond::acc_word w[2];
	      w[0].mark = inf;
	      w[1].op = acc_cond::acc_op::Inf;
	      w[1].size = 1;
	      acc_cond::acc_code c;
	      c.insert(c.end(), w, w + 2);
	      auto p = res.emplace(fin, c);
	      if (!p.second)
		p.first->second.append_or(std::move(c));
	    }
	}
      return res;
    }
  }

  tgba_digraph_ptr remove_fin(const const_tgba_digraph_ptr& aut)
  {
    if (!aut->acc().uses_fin_acceptance())
      return std::const_pointer_cast<tgba_digraph>(aut);

    std::vector<acc_cond::acc_code> code;
    std::vector<acc_cond::mark_t> rem;
    std::vector<acc_cond::mark_t> keep;
    std::vector<acc_cond::mark_t> add;
    bool has_true_term = false;
121
122
    acc_cond::mark_t allinf = 0U;
    acc_cond::mark_t allfin = 0U;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    {
      auto acccode = aut->get_acceptance();

      if (!acccode.is_dnf())
	acccode = acccode.to_dnf();

      auto split = split_dnf_acc_by_fin(acccode);

      auto sz = split.size();
      assert(sz > 0);

      rem.reserve(sz);
      code.reserve(sz);
      keep.reserve(sz);
      add.reserve(sz);
      for (auto p: split)
	{
	  // The empty Fin should always come first
	  assert(p.first != 0U || rem.empty());
	  rem.push_back(p.first);
143
	  allfin |= p.first;
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
	  acc_cond::mark_t inf = 0U;
	  if (!p.second.empty())
	    {
	      auto pos = &p.second.back();
	      auto end = &p.second.front();
	      while (pos > end)
		{
		  switch (pos->op)
		    {
		    case acc_cond::acc_op::And:
		    case acc_cond::acc_op::Or:
		      --pos;
		      break;
		    case acc_cond::acc_op::Inf:
		      inf |= pos[-1].mark;
		      pos -= 2;
		      break;
		    case acc_cond::acc_op::Fin:
		    case acc_cond::acc_op::FinNeg:
		    case acc_cond::acc_op::InfNeg:
		      SPOT_UNREACHABLE();
		      break;
		    }
		}
	    }
169
	  if (inf == 0U)
170
171
172
173
174
	    {
	      has_true_term = true;
	    }
	  code.push_back(std::move(p.second));
	  keep.push_back(inf);
175
	  allinf |= inf;
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
	  add.push_back(0U);
	}
    }
    assert(add.size() > 0);

    acc_cond acc = aut->acc();
    unsigned extra_sets = 0;

    // Do we have common sets between the acceptance terms?
    // If so, we need extra sets to distinguish the terms.
    bool interference = false;
    {
      auto sz = keep.size();
      acc_cond::mark_t sofar = 0U;
      for (unsigned i = 0; i < sz; ++i)
	{
	  auto k = keep[i];
	  if (k & sofar)
	    {
	      interference = true;
	      break;
	    }
	  sofar |= k;
	}
      if (interference)
	{
	  trace << "We have interferences\n";
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
	  // We need extra set, but we will try
	  // to reuse the Fin number if they are
	  // not used as Inf as well.
	  std::vector<int> exs(acc.num_sets());
	  for (auto f: allfin.sets())
	    {
	      if (allinf.has(f)) // Already used as Inf
		{
		  exs[f] = acc.add_set();
		  ++extra_sets;
		}
	      else
		{
		  exs[f] = f;
		}
	    }
219
220
	  for (unsigned i = 0; i < sz; ++i)
	    {
221
222
223
224
225
	      acc_cond::mark_t m = 0U;
	      for (auto f: rem[i].sets())
		m.set(exs[f]);
	      trace << "rem[" << i << "] = " << rem[i]
		    << "  m = " << m << '\n';
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
	      add[i] = m;
	      code[i].append_and(acc.inf(m));
	      trace << "code[" << i << "] = " << code[i] << '\n';
	    }
	}
      else if (has_true_term)
	{
	  trace << "We have a true term\n";
	  unsigned one = acc.add_sets(1);
	  extra_sets += 1;
	  auto m = acc.marks({one});
	  auto c = acc.inf(m);
	  for (unsigned i = 0; i < sz; ++i)
	    {
	      if (!code[i].is_true())
		continue;
	      add[i] = m;
	      code[i].append_and(c);
	      c = acc.fin(0U);	// Use false for the other terms.
	      trace << "code[" << i << "] = " << code[i] << '\n';
	    }

	}
    }

    acc_cond::acc_code new_code = aut->acc().fin(0U);
    for (auto c: code)
      new_code.append_or(std::move(c));

    unsigned cs = code.size();
    for (unsigned i = 0; i < cs; ++i)
      trace << i << " Rem " << rem[i] << "  Code " << code[i]
	    << " Keep " << keep[i] << '\n';

    scc_info si(aut);

    unsigned nst = aut->num_states();
    auto res = make_tgba_digraph(aut->get_dict());
    res->copy_ap_of(aut);
265
    res->prop_copy(aut, { false, false, false, false, true });
266
267
268
269
270
271
272
273
274
275
276
277
    res->new_states(nst);
    res->set_acceptance(aut->acc().num_sets() + extra_sets, new_code);
    res->set_init_state(aut->get_init_state_number());

    unsigned nscc = si.scc_count();
    std::vector<unsigned> state_map(nst);
    for (unsigned n = 0; n < nscc; ++n)
      {
	auto m = si.acc(n);
	auto states = si.states_of(n);
	trace << "SCC #" << n << " uses " << m << '\n';

278
279
280
	// What to keep and add into the main copy
	acc_cond::mark_t main_sets = 0U;
	acc_cond::mark_t main_add = 0U;
281
282
283
284
285
286
	for (unsigned i = 0; i < cs; ++i)
	  if (!(m & rem[i]))
	    {
	      main_sets |= keep[i];
	      main_add |= add[i];
	    }
287
288
	trace << "main_sets " << main_sets << "\nmain_add " << main_add << '\n';

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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
	// Create the main copy
	for (auto s: states)
	  for (auto& t: aut->out(s))
	    res->new_transition(s, t.dst, t.cond,
				(t.acc & main_sets) | main_add);

	// Create clones
	for (unsigned i = 0; i < cs; ++i)
	  if (m & rem[i])
	    {
	      auto r = rem[i];
	      trace << "rem[" << i << "] = " << r << " requires a copy\n";
	      unsigned base = res->new_states(states.size());
	      for (auto s: states)
		state_map[s] = base++;
	      auto k = keep[i];
	      auto a = add[i];
	      for (auto s: states)
		{
		  auto ns = state_map[s];
		  for (auto& t: aut->out(s))
		    {
		      if ((t.acc & r) || si.scc_of(t.dst) != n)
			continue;
		      auto nd = state_map[t.dst];
		      res->new_transition(ns, nd, t.cond, (t.acc & k) | a);
		      // We need only one non-deterministic jump per
		      // cycle.  As an approximation, we only do
		      // them on back-links.
		      //
		      // The acceptance marks on these transition
		      // are useless, but we keep them to preserve
		      // state-based acceptance if any.
		      if (t.dst <= s)
			res->new_transition(s, nd, t.cond,
					    (t.acc & main_sets) | main_add);
		    }
		}
	    }


      }
331
    res->purge_unreachable_states();
332
    trace << "before cleanup: " << res->get_acceptance() << '\n';
333
    cleanup_acceptance(res);
334
    trace << "after cleanup: " << res->get_acceptance() << '\n';
335
336
337
    return res;
  }
}