emptiness.cc 8.82 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2009, 2011, 2012, 2013, 2014 Laboratoire de Recherche
// et Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
4
// Copyright (C) 2004, 2005 Laboratoire d'Informatique de Paris 6 (LIP6),
5
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
6
7
8
9
10
11
// 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
12
// the Free Software Foundation; either version 3 of the License, or
13
14
15
16
17
18
19
20
// (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
21
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22

23
#include <sstream>
24
25
26
#include "emptiness.hh"
#include "tgba/tgba.hh"
#include "tgba/bddprint.hh"
27
28
29
30
31
32
#include "tgbaalgos/gtec/gtec.hh"
#include "tgbaalgos/gv04.hh"
#include "tgbaalgos/magic.hh"
#include "tgbaalgos/se05.hh"
#include "tgbaalgos/tau03.hh"
#include "tgbaalgos/tau03opt.hh"
33
34
35

namespace spot
{
36
37
38
39

  // tgba_run
  //////////////////////////////////////////////////////////////////////

40
41
42
  tgba_run::~tgba_run()
  {
    for (steps::const_iterator i = prefix.begin(); i != prefix.end(); ++i)
43
      i->s->destroy();
44
    for (steps::const_iterator i = cycle.begin(); i != cycle.end(); ++i)
45
      i->s->destroy();
46
47
48
49
50
51
52
  }

  tgba_run::tgba_run(const tgba_run& run)
  {
    for (steps::const_iterator i = run.prefix.begin();
	 i != run.prefix.end(); ++i)
      {
53
	step s = { i->s->clone(), i->label, i->acc };
54
55
56
57
58
	prefix.push_back(s);
      }
    for (steps::const_iterator i = run.cycle.begin();
	 i != run.cycle.end(); ++i)
      {
59
	step s = { i->s->clone(), i->label, i->acc };
60
61
62
63
	cycle.push_back(s);
      }
  }

64
65
66
67
68
69
70
71
72
73
74
75
76
77
  tgba_run&
  tgba_run::operator=(const tgba_run& run)
  {
    if (&run != this)
      {
	this->~tgba_run();
	new(this) tgba_run(run);
      }
    return *this;
  }

  // print_tgba_run
  //////////////////////////////////////////////////////////////////////

78
79
80
81
  std::ostream&
  print_tgba_run(std::ostream& os,
		 const tgba* a,
		 const tgba_run* run)
82
83
84
85
86
87
88
89
  {
    bdd_dict* d = a->get_dict();
    os << "Prefix:" << std::endl;
    for (tgba_run::steps::const_iterator i = run->prefix.begin();
	 i != run->prefix.end(); ++i)
      {
	os << "  " << a->format_state(i->s) << std::endl;
	os << "  |  ";
90
	bdd_print_formula(os, d, i->label);
91
	os << '\t';
92
93
94
95
96
97
98
99
100
	bdd_print_accset(os, d, i->acc);
	os << std::endl;
      }
    os << "Cycle:" << std::endl;
    for (tgba_run::steps::const_iterator i = run->cycle.begin();
	 i != run->cycle.end(); ++i)
      {
	os << "  " << a->format_state(i->s) << std::endl;
	os << "  |  ";
101
	bdd_print_formula(os, d, i->label);
102
	os << '\t';
103
	bdd_print_accset(os, d, i->acc);
104
	os << '\n';
105
106
107
108
      }
    return os;
  }

109
110
  // emptiness_check_result
  //////////////////////////////////////////////////////////////////////
111
112
113
114
115
116
117

  tgba_run*
  emptiness_check_result::accepting_run()
  {
    return 0;
  }

118
119
120
121
122
123
  const unsigned_statistics*
  emptiness_check_result::statistics() const
  {
    return dynamic_cast<const unsigned_statistics*>(this);
  }

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  const char*
  emptiness_check_result::parse_options(char* options)
  {
    option_map old(o_);
    const char* s = o_.parse_options(options);
    options_updated(old);
    return s;
  }

  void
  emptiness_check_result::options_updated(const option_map&)
  {
  }


139
140
141
  // emptiness_check
  //////////////////////////////////////////////////////////////////////

142
143
144
145
  emptiness_check::~emptiness_check()
  {
  }

146
147
148
149
150
151
  const unsigned_statistics*
  emptiness_check::statistics() const
  {
    return dynamic_cast<const unsigned_statistics*>(this);
  }

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  const char*
  emptiness_check::parse_options(char* options)
  {
    option_map old(o_);
    const char* s = o_.parse_options(options);
    options_updated(old);
    return s;
  }

  void
  emptiness_check::options_updated(const option_map&)
  {
  }

  bool
  emptiness_check::safe() const
  {
    return true;
  }

172
173
174
175
176
  std::ostream&
  emptiness_check::print_stats(std::ostream& os) const
  {
    return os;
  }
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
  // emptiness_check_instantiator
  //////////////////////////////////////////////////////////////////////

  namespace
  {

    spot::emptiness_check*
    couvreur99_cons(const spot::tgba* a, spot::option_map o)
    {
      return spot::couvreur99(a, o);
    }

    struct ec_algo
    {
      const char* name;
      spot::emptiness_check* (*construct)(const spot::tgba*,
					  spot::option_map);
      unsigned int min_acc;
      unsigned int max_acc;
    };

    ec_algo ec_algos[] =
      {
	{ "Cou99",     couvreur99_cons,                     0, -1U },
	{ "CVWY90",    spot::magic_search,                  0,   1 },
	{ "GV04",      spot::explicit_gv04_check,           0,   1 },
	{ "SE05",      spot::se05,                          0,   1 },
	{ "Tau03",     spot::explicit_tau03_search,         1, -1U },
	{ "Tau03_opt", spot::explicit_tau03_opt_search,     0, -1U },
      };
  }

  emptiness_check_instantiator::emptiness_check_instantiator(option_map o,
							     void* i)
    : o_(o), info_(i)
  {
  }

  unsigned int
  emptiness_check_instantiator::min_acceptance_conditions() const
  {
    return static_cast<ec_algo*>(info_)->min_acc;
  }

  unsigned int
  emptiness_check_instantiator::max_acceptance_conditions() const
  {
    return static_cast<ec_algo*>(info_)->max_acc;
  }

  emptiness_check*
  emptiness_check_instantiator::instantiate(const tgba* a) const
  {
    return static_cast<ec_algo*>(info_)->construct(a, o_);
  }

  emptiness_check_instantiator*
  emptiness_check_instantiator::construct(const char* name, const char** err)
  {
    // Skip spaces.
    while (*name && strchr(" \t\n", *name))
      ++name;

    const char* opt_str = strchr(name, '(');
    option_map o;
    if (opt_str)
      {
	const char* opt_start = opt_str + 1;
	const char* opt_end = strchr(opt_start, ')');
	if (!opt_end)
	  {
	    *err = opt_start;
	    return 0;
	  }
	std::string opt(opt_start, opt_end);

	const char* res = o.parse_options(opt.c_str());
	if (res)
	  {
	    *err  = opt.c_str() - res + opt_start;
	    return 0;
	  }
      }

    if (!opt_str)
      opt_str = name + strlen(name);

    // Ignore spaces before `(' (or trailing spaces).
    while (opt_str > name && strchr(" \t\n", *--opt_str))
      continue;
    std::string n(name, opt_str + 1);


    ec_algo* info = ec_algos;
    for (unsigned i = 0; i < sizeof(ec_algos)/sizeof(*ec_algos); ++i, ++info)
      if (n == info->name)
274
275
276
277
	{
	  *err = 0;
	  return new emptiness_check_instantiator(o, info);
	}
278
279
280
281
282
283
284
    *err = name;
    return 0;
  }

  // tgba_run_to_tgba
  //////////////////////////////////////////////////////////////////////

285
  tgba_digraph*
286
  tgba_run_to_tgba(const tgba* a, const tgba_run* run)
287
  {
288
289
    auto d = a->get_dict();
    auto res = new tgba_digraph(d);
290
291
    res->copy_ap_of(a);
    res->copy_acceptance_conditions_of(a);
292
293

    const state* s = a->get_init_state();
294
295
    unsigned src;
    unsigned dst;
296
297
298
    const tgba_run::steps* l;
    bdd seen_acc = bddfalse;

299
    typedef std::unordered_map<const state*, unsigned,
300
			       state_ptr_hash, state_ptr_equal> state_map;
301
302
303
304
305
306
307
308
309
310
    state_map seen;

    if (run->prefix.empty())
        l = &run->cycle;
    else
        l = &run->prefix;

    tgba_run::steps::const_iterator i = l->begin();

    assert(s->compare(i->s) == 0);
311
    src = res->new_state();
312
    seen.emplace(i->s, src);
313

314
    for (; i != l->end();)
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
      {
        // expected outgoing transition
        bdd label = i->label;
        bdd acc = i->acc;

        // compute the next expected state
        const state* next;
        ++i;
        if (i != l->end())
          {
            next = i->s;
          }
        else
          {
            if (l == &run->prefix)
              {
                l = &run->cycle;
                i = l->begin();
              }
            next = l->begin()->s;
          }

337
338
339
340
        // browse the actual outgoing transitions and
	// look for next;
	const state* the_next = nullptr;
	for (auto j: a->succ(s))
341
342
343
344
345
346
          {
            if (j->current_condition() != label
                || j->current_acceptance_conditions() != acc)
              continue;

            const state* s2 = j->current_state();
347
            if (s2->compare(next) == 0)
348
              {
349
350
351
352
		the_next = s2;
		break;
	      }
	    s2->destroy();
353
          }
354
355
356
        assert(res);
        s->destroy();
	s = the_next;
357
358


359
	auto p = seen.emplace(next, 0);
360
	if (p.second)
361
	  p.first->second = res->new_state();
362
363
	dst = p.first->second;

364
	res->new_transition(src, dst, label, acc);
365
	src = dst;
366
367
368

        // Sum acceptance conditions.
        if (l == &run->cycle && i != l->begin())
369
	  seen_acc |= acc;
370
      }
371

372
    s->destroy();
373
374
375
376
377

    assert(seen_acc == a->all_acceptance_conditions());
    return res;
  }

378
}