wdbacomp.cc 6.01 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2011, 2012, 2013 Laboratoire de Recherche et
// Développement de l'Epita.
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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#include "wdbacomp.hh"
#include "ltlast/constant.hh"

namespace spot
{
  namespace
  {
    class state_wdba_comp_proxy : public state
    {
    public:
      state_wdba_comp_proxy(state* s) : s_(s)
      {
      }

      /// Copy constructor
      state_wdba_comp_proxy(const state_wdba_comp_proxy& o)
	: state(),
	  s_(o.real_state())
      {
	if (s_)
	  s_ = s_->clone();
      }

      virtual
      ~state_wdba_comp_proxy()
      {
46
47
	if (s_)
	  s_->destroy();
48
49
50
51
52
53
54
55
56
57
58
59
      }

      state*
      real_state() const
      {
	return s_;
      }

      virtual int
      compare(const state* other) const
      {
	const state_wdba_comp_proxy* o =
60
	  down_cast<const state_wdba_comp_proxy*>(other);
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
121
122
123
124
125
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
	assert(o);
	const state* oo = o->real_state();
	if (s_ == 0)
	  return oo ? 1 : 0;
	if (oo == 0)
	  return -1;

	return s_->compare(oo);
      }

      virtual size_t
      hash() const
      {
	if (s_)
	  return s_->hash();
	return 0;
      }

      virtual
      state_wdba_comp_proxy* clone() const
      {
	return new state_wdba_comp_proxy(*this);
      }

    private:
      state* s_; // 0 if sink-state.
    };

    class tgba_wdba_comp_proxy_succ_iterator: public tgba_succ_iterator
    {
    public:
      tgba_wdba_comp_proxy_succ_iterator(tgba_succ_iterator* it,
					 bdd the_acceptance_cond)
	: it_(it), the_acceptance_cond_(the_acceptance_cond), left_(bddtrue)
      {
      }

      virtual
      ~tgba_wdba_comp_proxy_succ_iterator()
      {
	delete it_;
      }

      // iteration

      void
      first()
      {
	if (it_)
	  it_->first();
	left_ = bddtrue;
      }

      void
      next()
      {
	left_ -= current_condition();
	if (it_)
	  it_->next();
      }

      bool
      done() const
      {
	return left_ == bddfalse;
      }

      // inspection

      state_wdba_comp_proxy*
      current_state() const
      {
	if (!it_ || it_->done())
	  return new state_wdba_comp_proxy(0);
	return new state_wdba_comp_proxy(it_->current_state());
      }

      bdd
      current_condition() const
      {
	if (!it_ || it_->done())
	  return left_;
	return it_->current_condition();
      }

      bdd
      current_acceptance_conditions() const
      {
	if (!it_ || it_->done())
	  // The sink state is accepting in the negated automaton.
	  return the_acceptance_cond_;

	if (it_->current_acceptance_conditions() == bddfalse)
	  return the_acceptance_cond_;
	else
	  return bddfalse;
      }

    protected:
      tgba_succ_iterator* it_;
      const bdd the_acceptance_cond_;
      bdd left_;
    };

    class tgba_wdba_comp_proxy: public tgba
    {
    public:
      tgba_wdba_comp_proxy(const tgba* a)
169
	: a_(a), the_acceptance_cond_(a->all_acceptance_conditions())
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
      {
	if (the_acceptance_cond_ == bddfalse)
	  {
	    int v = get_dict()
	      ->register_acceptance_variable(ltl::constant::true_instance(),
					     this);
	    the_acceptance_cond_ = bdd_ithvar(v);
	  }
      }

      virtual ~tgba_wdba_comp_proxy()
      {
	get_dict()->unregister_all_my_variables(this);
      }

      virtual state* get_init_state() const
      {
	return new state_wdba_comp_proxy(a_->get_init_state());
      }

      virtual tgba_succ_iterator*
      succ_iter(const state* local_state,
		const state* global_state = 0,
		const tgba* global_automaton = 0) const
      {
	const state_wdba_comp_proxy* s =
196
	  down_cast<const state_wdba_comp_proxy*>(local_state);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	assert(s);

	const state* o = s->real_state();
	tgba_succ_iterator* it = 0;
	if (o)
	   it = a_->succ_iter(s->real_state(), global_state, global_automaton);
	return new tgba_wdba_comp_proxy_succ_iterator(it,
						      the_acceptance_cond_);
      }

      virtual bdd_dict*
      get_dict() const
      {
	return a_->get_dict();
      }

      virtual std::string
      format_state(const state* ostate) const
      {
	const state_wdba_comp_proxy* s =
217
	  down_cast<const state_wdba_comp_proxy*>(ostate);
218
219
220
221
222
223
224
225
226
227
228
229
	assert(s);
	const state* rs = s->real_state();
	if (rs)
	  return a_->format_state(s->real_state());
	else
	  return "(*)"; // sink state
      }

      virtual state*
      project_state(const state* s, const tgba* t) const
      {
	const state_wdba_comp_proxy* s2 =
230
	  down_cast<const state_wdba_comp_proxy*>(s);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	assert(s2);
	if (t == this)
	  return s2->clone();
	return a_->project_state(s2->real_state(), t);
      }

      virtual bdd
      all_acceptance_conditions() const
      {
	return the_acceptance_cond_;
      }

      virtual bdd
      neg_acceptance_conditions() const
      {
	return !the_acceptance_cond_;
      }

249
250
251
    protected:
      virtual bdd
      compute_support_conditions(const state*) const
252
      {
253
254
	// The automaton is complete, so we always support all variables.
	return bddtrue;
255
256
257
258
259
      }

      virtual bdd compute_support_variables(const state* ostate) const
      {
	const state_wdba_comp_proxy* s =
260
	  down_cast<const state_wdba_comp_proxy*>(ostate);
261
262
263
264
265
266
267
268
269
270
271
272
273
	assert(s);
	const state* rs = s->real_state();
	if (rs)
	  return a_->support_variables(rs);
	else
	  return bddtrue;
      }

      const tgba* a_;
    private:
      bdd the_acceptance_cond_;

      // Disallow copy.
274
275
      tgba_wdba_comp_proxy(const tgba_wdba_comp_proxy&) = delete;
      tgba_wdba_comp_proxy& operator=(const tgba_wdba_comp_proxy&) = delete;
276
277
278
279
280
281
282
283
284
285
    };

  }

  tgba*
  wdba_complement(const tgba* aut)
  {
    return new tgba_wdba_comp_proxy(aut);
  }
}