twaproduct.cc 10.5 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2009, 2011, 2012, 2014, 2015 Laboratoire de Recherche et
3
// Développement de l'Epita (LRDE).
Guillaume Sadegh's avatar
Guillaume Sadegh committed
4
// Copyright (C) 2003, 2004, 2006 Laboratoire d'Informatique de
5 6
// Paris 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
7 8 9 10 11
//
// 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
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
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/>.
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
22

23
#include <spot/twa/twaproduct.hh>
24
#include <string>
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
25
#include <cassert>
26 27
#include <spot/misc/hashfunc.hh>
#include <spot/kripke/kripke.hh>
28 29 30 31 32

namespace spot
{

  ////////////////////////////////////////////////////////////
33
  // state_product
34

35
  state_product::~state_product()
36
  {
37 38
    left_->destroy();
    right_->destroy();
39 40
  }

41 42 43 44 45 46 47 48 49 50
  void
  state_product::destroy() const
  {
    if (--count_)
      return;
    fixed_size_pool* p = pool_;
    this->~state_product();
    p->deallocate(this);
  }

Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
51
  int
52
  state_product::compare(const state* other) const
53
  {
54
    const state_product* o = down_cast<const state_product*>(other);
55 56 57 58 59 60 61
    assert(o);
    int res = left_->compare(o->left());
    if (res != 0)
      return res;
    return right_->compare(o->right());
  }

62 63 64
  size_t
  state_product::hash() const
  {
65 66
    // We assume that size_t is 32-bit wide.
    return wang32_hash(left_->hash()) ^ wang32_hash(right_->hash());
67 68
  }

69 70
  state_product*
  state_product::clone() const
71
  {
72 73
    ++count_;
    return const_cast<state_product*>(this);
74 75
  }

76
  ////////////////////////////////////////////////////////////
77
  // twa_succ_iterator_product
78

79
  namespace
80
  {
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
81

82
    class twa_succ_iterator_product_common: public twa_succ_iterator
83 84
    {
    public:
85 86
      twa_succ_iterator_product_common(twa_succ_iterator* left,
					twa_succ_iterator* right,
87
					const twa_product* prod,
88
					fixed_size_pool* pool)
89
	: left_(left), right_(right), prod_(prod), pool_(pool)
90 91
      {
      }
92

93 94
      void recycle(const const_twa_ptr& l, twa_succ_iterator* left,
		   const_twa_ptr r, twa_succ_iterator* right)
95 96 97 98 99 100 101
      {
	l->release_iter(left_);
	left_ = left;
	r->release_iter(right_);
	right_ = right;
      }

102
      virtual ~twa_succ_iterator_product_common()
103
      {
104 105
	delete left_;
	delete right_;
106 107
      }

108
      virtual bool next_non_false_() = 0;
109

110
      bool first()
111
      {
112
	if (!right_)
113
	  return false;
114

115 116 117 118
	// If one of the two successor sets is empty initially, we
	// reset right_, so that done() can detect this situation
	// easily.  (We choose to reset right_ because this variable
	// is already used by done().)
119
	if (!(left_->first() && right_->first()))
120
	  {
121
	    delete right_;
122
	    right_ = nullptr;
123
	    return false;
124
	  }
125
	return next_non_false_();
126 127 128 129 130 131 132
      }

      bool done() const
      {
	return !right_ || right_->done();
      }

133
      const state_product* dst() const
134
      {
135 136
	return new(pool_->allocate()) state_product(left_->dst(),
						    right_->dst(),
137
						    pool_);
138 139 140
      }

    protected:
141 142
      twa_succ_iterator* left_;
      twa_succ_iterator* right_;
143
      const twa_product* prod_;
144
      fixed_size_pool* pool_;
145
      friend class spot::twa_product;
146 147 148 149
    };


    /// \brief Iterate over the successors of a product computed on the fly.
150
    class twa_succ_iterator_product: public twa_succ_iterator_product_common
151 152
    {
    public:
153 154
      twa_succ_iterator_product(twa_succ_iterator* left,
				 twa_succ_iterator* right,
155
				 const twa_product* prod,
156
				 fixed_size_pool* pool)
157
	: twa_succ_iterator_product_common(left, right, prod, pool)
158 159 160
      {
      }

161
      virtual ~twa_succ_iterator_product()
162 163 164
      {
      }

165
      bool step_()
166
      {
167 168 169 170
	if (left_->next())
	  return true;
	left_->first();
	return right_->next();
171 172
      }

173
      bool next_non_false_()
174
      {
175 176
	assert(!done());
	do
177
	  {
178 179
	    bdd l = left_->cond();
	    bdd r = right_->cond();
180 181 182 183 184
	    bdd current_cond = l & r;

	    if (current_cond != bddfalse)
	      {
		current_cond_ = current_cond;
185
		return true;
186 187
	      }
	  }
188 189
	while (step_());
	return false;
190 191
      }

192
      bool next()
193
      {
194 195 196
	if (step_())
	  return next_non_false_();
	return false;
197 198
      }

199
      bdd cond() const
200
      {
201
	return current_cond_;
202
      }
203

204
      acc_cond::mark_t acc() const
205
      {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
206 207
	return left_->acc() | (right_->acc() << prod_->left_acc().num_sets());
      }
208

209 210 211 212 213 214
    protected:
      bdd current_cond_;
    };

    /// Iterate over the successors of a product computed on the fly.
    /// This one assumes that LEFT is an iterator over a Kripke structure
215 216
    class twa_succ_iterator_product_kripke:
      public twa_succ_iterator_product_common
217 218
    {
    public:
219 220
      twa_succ_iterator_product_kripke(twa_succ_iterator* left,
					twa_succ_iterator* right,
221
					const twa_product* prod,
222
					fixed_size_pool* pool)
223
	: twa_succ_iterator_product_common(left, right, prod, pool)
224 225
      {
      }
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
226

227
      virtual ~twa_succ_iterator_product_kripke()
228 229
      {
      }
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
230

231
      bool next_non_false_()
232 233 234
      {
	// All the transitions of left_ iterator have the
	// same label, because it is a Kripke structure.
235
	bdd l = left_->cond();
236 237
	assert(!right_->done());
	do
238
	  {
239
	    bdd r = right_->cond();
240 241 242 243 244
	    bdd current_cond = l & r;

	    if (current_cond != bddfalse)
	      {
		current_cond_ = current_cond;
245
		return true;
246 247
	      }
	  }
248 249
	while (right_->next());
	return false;
250
      }
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
251

252
      bool next()
253
      {
254 255 256 257 258 259
	if (left_->next())
	  return true;
	left_->first();
	if (right_->next())
	  return next_non_false_();
	return false;
260
      }
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
261

262
      bdd cond() const
263 264 265 266
      {
	return current_cond_;
      }

267
      acc_cond::mark_t acc() const
268
      {
269
	return right_->acc();
270 271 272 273 274 275 276
      }

    protected:
      bdd current_cond_;
    };

  } // anonymous
277 278

  ////////////////////////////////////////////////////////////
279
  // twa_product
280

281 282
  twa_product::twa_product(const const_twa_ptr& left,
			     const const_twa_ptr& right)
283
    : twa(left->get_dict()), left_(left), right_(right),
284
      pool_(sizeof(state_product))
285
  {
286
    assert(get_dict() == right_->get_dict());
287 288 289 290

    // If one of the side is a Kripke structure, it is easier to deal
    // with (we don't have to fix the acceptance conditions, and
    // computing the successors can be improved a bit).
291
    if (dynamic_cast<const kripke*>(left_.get()))
292 293 294
      {
	left_kripke_ = true;
      }
295
    else if (dynamic_cast<const kripke*>(right_.get()))
296 297 298 299 300 301 302 303 304
      {
	std::swap(left_, right_);
	left_kripke_ = true;
      }
    else
      {
	left_kripke_ = false;
      }

305 306 307
    auto d = get_dict();
    d->register_all_propositions_of(&left_, this);
    d->register_all_propositions_of(&right_, this);
308

309 310
    assert(num_sets() == 0);
    auto left_num = left->num_sets();
311 312
    auto right_acc = right->get_acceptance() << left_num;
    right_acc &= left->get_acceptance();
313
    set_acceptance(left_num + right->num_sets(), right_acc);
314 315
  }

316
  twa_product::~twa_product()
317
  {
318 319 320 321 322
    // Prevent these states from being destroyed by ~tgba(): they
    // will be destroyed before when the pool is destructed.
    if (last_support_conditions_input_)
      {
	last_support_conditions_input_->destroy();
323
	last_support_conditions_input_ = nullptr;
324
      }
325 326
  }

327
  const state*
328
  twa_product::get_init_state() const
329
  {
330 331 332
    fixed_size_pool* p = const_cast<fixed_size_pool*>(&pool_);
    return new(p->allocate()) state_product(left_->get_init_state(),
					    right_->get_init_state(), p);
333 334
  }

335
  twa_succ_iterator*
336
  twa_product::succ_iter(const state* state) const
337
  {
338
    const state_product* s = down_cast<const state_product*>(state);
339
    assert(s);
340 341
    twa_succ_iterator* li = left_->succ_iter(s->left());
    twa_succ_iterator* ri = right_->succ_iter(s->right());
342

343 344
    if (iter_cache_)
      {
345 346
	twa_succ_iterator_product_common* it =
	  down_cast<twa_succ_iterator_product_common*>(iter_cache_);
347 348 349 350 351
	it->recycle(left_, li, right_, ri);
	iter_cache_ = nullptr;
	return it;
      }

352
    fixed_size_pool* p = const_cast<fixed_size_pool*>(&pool_);
353
    if (left_kripke_)
354
      return new twa_succ_iterator_product_kripke(li, ri, this, p);
355
    else
356
      return new twa_succ_iterator_product(li, ri, this, p);
357 358
  }

359
  bdd
360
  twa_product::compute_support_conditions(const state* in) const
361
  {
362
    const state_product* s = down_cast<const state_product*>(in);
363 364 365 366 367 368
    assert(s);
    bdd lsc = left_->support_conditions(s->left());
    bdd rsc = right_->support_conditions(s->right());
    return lsc & rsc;
  }

369
  const acc_cond& twa_product::left_acc() const
370
  {
371 372 373
    return left_->acc();
  }

374
  const acc_cond& twa_product::right_acc() const
375 376
  {
    return right_->acc();
377 378
  }

Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
379
  std::string
380
  twa_product::format_state(const state* state) const
381
  {
382
    const state_product* s = down_cast<const state_product*>(state);
383
    assert(s);
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
384 385
    return (left_->format_state(s->left())
	    + " * "
386 387
	    + right_->format_state(s->right()));
  }
Alexandre Duret-Lutz's avatar
typo  
Alexandre Duret-Lutz committed
388

389
  state*
390
  twa_product::project_state(const state* s, const const_twa_ptr& t) const
391
  {
392
    const state_product* s2 = down_cast<const state_product*>(s);
393
    assert(s2);
394
    if (t.get() == this)
395 396 397 398 399 400 401
      return s2->clone();
    state* res = left_->project_state(s2->left(), t);
    if (res)
      return res;
    return right_->project_state(s2->right(), t);
  }

402
  std::string
403
  twa_product::transition_annotation(const twa_succ_iterator* t) const
404
  {
405 406
    const twa_succ_iterator_product_common* i =
      down_cast<const twa_succ_iterator_product_common*>(t);
407 408 409 410 411 412 413 414 415 416
    assert(i);
    std::string left = left_->transition_annotation(i->left_);
    std::string right = right_->transition_annotation(i->right_);
    if (left == "")
      return right;
    if (right == "")
      return left;
    return "<" + left + ", " + right + ">";
  }

417
  //////////////////////////////////////////////////////////////////////
418
  // twa_product_init
419

420 421
  twa_product_init::twa_product_init(const const_twa_ptr& left,
				       const const_twa_ptr& right,
422 423
				       const state* left_init,
				       const state* right_init)
424
    : twa_product(left, right),
425 426
      left_init_(left_init), right_init_(right_init)
  {
427 428
    if (left_ != left)
      std::swap(left_init_, right_init_);
429 430
  }

431
  const state*
432
  twa_product_init::get_init_state() const
433
  {
434 435 436
    fixed_size_pool* p = const_cast<fixed_size_pool*>(&pool_);
    return new(p->allocate()) state_product(left_init_->clone(),
					    right_init_->clone(), p);
437 438
  }

439
}