eesrg.cc 11.9 KB
Newer Older
1
// Copyright (C) 2003, 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// 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
// the Free Software Foundation; either version 2 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 Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

22
23
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
#include <cstring>
#include <map>
#include <cassert>
#include "eesrg.hh"
#include <gspnlib.h>
#include "misc/bddlt.hh"
#include <bdd.h>

namespace spot
{

  // state_gspn_eesrg
  //////////////////////////////////////////////////////////////////////

  class state_gspn_eesrg: public state
  {
  public:
    state_gspn_eesrg(State left, const state* right)
      : left_(left), right_(right)
    {
    }

    virtual
    ~state_gspn_eesrg()
    {
      delete right_;
    }

    virtual int
    compare(const state* other) const
    {
      const state_gspn_eesrg* o = dynamic_cast<const state_gspn_eesrg*>(other);
      assert(o);
      int res = (reinterpret_cast<char*>(o->left())
		 - reinterpret_cast<char*>(left()));
      if (res != 0)
	return res;
      return right_->compare(o->right());
    }

    virtual size_t
    hash() const
    {
      return (reinterpret_cast<char*>(left())
	      - static_cast<char*>(0)) << 10 + right_->hash();
    }

    virtual state_gspn_eesrg* clone() const
    {
71
      return new state_gspn_eesrg(left(), right()->clone());
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
    }

    State
    left() const
    {
      return left_;
    }

    const state*
    right() const
    {
      return right_;
    }

  private:
    State left_;
    const state* right_;
  }; // state_gspn_eesrg



  // tgba_gspn_eesrg_private_
  //////////////////////////////////////////////////////////////////////

  struct tgba_gspn_eesrg_private_
  {
    int refs;			// reference count

    bdd_dict* dict;
    typedef std::map<int, AtomicProp> prop_map;
    prop_map prop_dict;

    signed char* all_props;

    size_t prop_count;
    const tgba* operand;

    tgba_gspn_eesrg_private_(bdd_dict* dict, const gspn_environment& env,
			     const tgba* operand)
      : refs(1), dict(dict), all_props(0),
	operand(operand)
    {
      const gspn_environment::prop_map& p = env.get_prop_map();

      try
	{
	  AtomicProp max_prop = 0;

	  for (gspn_environment::prop_map::const_iterator i = p.begin();
	       i != p.end(); ++i)
	    {
	      int var = dict->register_proposition(i->second, this);
	      AtomicProp index;
	      int err = prop_index(i->first.c_str(), &index);
	      if (err)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
127
		throw gspn_exeption("prop_index(" + i->first + ")", err);
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
169

	      prop_dict[var] = index;

	      max_prop = std::max(max_prop, index);
	    }
	  prop_count = 1 + max_prop;
	  all_props = new signed char[prop_count];
	}
      catch (...)
	{
	  // If an exception occurs during the loop, we need to clean
	  // all BDD variables which have been registered so far.
	  dict->unregister_all_my_variables(this);
	  throw;
	}
    }

    tgba_gspn_eesrg_private_::~tgba_gspn_eesrg_private_()
    {
      dict->unregister_all_my_variables(this);
      if (all_props)
	delete[] all_props;
    }

  };


  // tgba_succ_iterator_gspn_eesrg
  //////////////////////////////////////////////////////////////////////

  class tgba_succ_iterator_gspn_eesrg: public tgba_succ_iterator
  {
  public:
    tgba_succ_iterator_gspn_eesrg(State state,
				  tgba_gspn_eesrg_private_* data,
				  tgba_succ_iterator* operand)
      : state_(state),
	operand_(operand),
	all_conds_(bddfalse),
	successors_(0),
	size_(0),
	current_(0),
170
171
	data_(data),
	not_first_(false)
172
173
174
175
176
177
    {
    }

    virtual
    ~tgba_succ_iterator_gspn_eesrg()
    {
178
      if (successors_)
179
	succ_free(successors_);
180
181
182
183
184
185
186
      if (operand_)
	delete operand_;
    }

    void
    step()
    {
187
188
      if (++current_ < size_)
	return;
189

190
      do
191
192
	{
	  if (successors_)
193
	    {
194
195
	      succ_free(successors_);
	      successors_ = 0;
196
	    }
197

198
	  if (all_conds_ == bddfalse)
199
	    {
200
201
202
203
204
205
206
207
208
209
210
211
212
213
	      if (not_first_)
		{
		  assert(!operand_->done());
		  operand_->next();
		}
	      else
		{
		  // operand_->first() has already been called from first().
		  not_first_ = true;
		}
	      if (operand_->done())
		return;
	      all_conds_ = operand_->current_condition();
	      outside_ = !all_conds_;
214
	    }
215
216
217
218
219
220
221
222
223

	  bdd cond = bdd_satone(all_conds_);
	  cond = bdd_simplify(cond, cond | outside_);
	  all_conds_ -= cond;

	  // Translate COND into an array of properties.
	  signed char* props = data_->all_props;
	  memset(props, -1, data_->prop_count);
	  while (cond != bddtrue)
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
	      int var = bdd_var(cond);

	      bdd high = bdd_high(cond);
	      int res;
	      if (high == bddfalse)
		{
		  cond = bdd_low(cond);
		  res = 0;
		}
	      else
		{
		  cond = high;
		  res = 1;
		}

	      // It's OK if VAR is unknown from GreatSPN (it might
	      // have been used to synchornize another automaton or
	      // something), just skip it.
	      tgba_gspn_eesrg_private_::prop_map::iterator i =
		data_->prop_dict.find(var);
	      if (i != data_->prop_dict.end())
		props[i->second] = res;

	      assert(cond != bddfalse);
249
	    }
250

251
252
	  succ(state_, props, &successors_, &size_);
	  current_ = 0;
253
	}
254
      while (size_ == 0); 	// Repeat until we have a successor.
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
    }

    virtual void
    first()
    {
      size_ = 0;
      all_conds_ = bddfalse;
      operand_->first();
      if (operand_->done())
	{
	  delete operand_;
	  operand_ = 0;
	  return;
	}
      if (successors_)
	{
	  delete successors_;
	  successors_ = 0;
	}
      step();
    }

    virtual void
    next()
    {
      assert(!done());
      step();
    }

    virtual bool
    done() const
    {
      return (size_ <= current_
	      && all_conds_ == bddfalse
289
	      && (!operand_ || operand_->done()));
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    }

    virtual state*
    current_state() const
    {
      return new state_gspn_eesrg(successors_[current_],
				  operand_->current_state());
    }

    virtual bdd
    current_condition() const
    {
      return bddtrue;
    }

    virtual bdd
306
    current_acceptance_conditions() const
307
308
309
    {
      // There is no acceptance conditions in GSPN systems, so we just
      // return those from OPERAND_.
310
      return operand_->current_acceptance_conditions();
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
    }
  private:
    State state_;
    // Iterator on the right operand
    tgba_succ_iterator* operand_;
    // All conditions of the current successor of the right operand
    // (We will iterate on all conjunctions in this.)
    bdd all_conds_;
    bdd outside_;
    // All successors of STATE matching a selection conjunctions from
    // ALL_CONDS.
    State* successors_;		/// array of successors
    size_t size_;		/// size of successors_
    size_t current_;		/// current position in successors_
    tgba_gspn_eesrg_private_* data_;
326
    bool not_first_;		/// Whether this is not the first step.
327
328
329
330
331
332
  }; // tgba_succ_iterator_gspn_eesrg


  // tgba_gspn_eesrg
  //////////////////////////////////////////////////////////////////////

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  class tgba_gspn_eesrg: public tgba
  {
  public:
    tgba_gspn_eesrg(bdd_dict* dict, const gspn_environment& env,
		    const tgba* operand);
    tgba_gspn_eesrg(const tgba_gspn_eesrg& other);
    tgba_gspn_eesrg& operator=(const tgba_gspn_eesrg& other);
    virtual ~tgba_gspn_eesrg();
    virtual state* get_init_state() const;
    virtual tgba_succ_iterator*
    succ_iter(const state* local_state,
	      const state* global_state = 0,
	      const tgba* global_automaton = 0) const;
    virtual bdd_dict* get_dict() const;
    virtual std::string format_state(const state* state) const;
    virtual state* project_state(const state* s, const tgba* t) const;
    virtual bdd all_acceptance_conditions() const;
    virtual bdd neg_acceptance_conditions() const;
  protected:
    virtual bdd compute_support_conditions(const spot::state* state) const;
    virtual bdd compute_support_variables(const spot::state* state) const;
  private:
    tgba_gspn_eesrg_private_* data_;
  };
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

  tgba_gspn_eesrg::tgba_gspn_eesrg(bdd_dict* dict, const gspn_environment& env,
				   const tgba* operand)
  {
    data_ = new tgba_gspn_eesrg_private_(dict, env, operand);
  }

  tgba_gspn_eesrg::tgba_gspn_eesrg(const tgba_gspn_eesrg& other)
    : tgba()
  {
    data_ = other.data_;
    ++data_->refs;
  }

  tgba_gspn_eesrg::~tgba_gspn_eesrg()
  {
    if (--data_->refs == 0)
      delete data_;
  }

  tgba_gspn_eesrg&
  tgba_gspn_eesrg::operator=(const tgba_gspn_eesrg& other)
  {
    if (&other == this)
      return *this;
    this->~tgba_gspn_eesrg();
    new (this) tgba_gspn_eesrg(other);
    return *this;
  }

  state* tgba_gspn_eesrg::get_init_state() const
  {
389
390
391
392
    // Use 0 as initial state for the EESRG side.  State 0 does not
    // exists, but when passed to succ() it will produce the list
    // of initial states.
    return new state_gspn_eesrg(0, data_->operand->get_init_state());
393
394
395
396
  }

  tgba_succ_iterator*
  tgba_gspn_eesrg::succ_iter(const state* state,
397
398
			     const state* global_state,
			     const tgba* global_automaton) const
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
  {
    const state_gspn_eesrg* s = dynamic_cast<const state_gspn_eesrg*>(state);
    assert(s);
    (void) global_state;
    (void) global_automaton;
    tgba_succ_iterator* i = data_->operand->succ_iter(s->right());
    return new tgba_succ_iterator_gspn_eesrg(s->left(), data_, i);
  }

  bdd
  tgba_gspn_eesrg::compute_support_conditions(const spot::state* state) const
  {
    (void) state;
    return bddtrue;
  }

  bdd
  tgba_gspn_eesrg::compute_support_variables(const spot::state* state) const
  {
    (void) state;
    return bddtrue;
  }

  bdd_dict*
  tgba_gspn_eesrg::get_dict() const
  {
    return data_->dict;
  }

  std::string
  tgba_gspn_eesrg::format_state(const state* state) const
  {
    const state_gspn_eesrg* s = dynamic_cast<const state_gspn_eesrg*>(state);
    assert(s);
    char* str;
434
435
436
437
438
439
    State gs = s->left();
    if (gs)
      {
	int err = print_state(gs, &str);
	if (err)
	  throw gspn_exeption("print_state()", err);
440
441
442
443
	// Strip trailing \n...
	unsigned len = strlen(str);
	while (str[--len] == '\n')
	  str[len] = 0;
444
445
446
447
448
      }
    else
      {
	str = strdup("-1");
      }
449

450
    std::string res(str);
451
    free(str);
452
    return res + " * " + data_->operand->format_state(s->right());
453
454
  }

455
456
457
458
459
460
461
462
463
464
  state*
  tgba_gspn_eesrg::project_state(const state* s, const tgba* t) const
  {
    const state_gspn_eesrg* s2 = dynamic_cast<const state_gspn_eesrg*>(s);
    assert(s2);
    if (t == this)
      return s2->clone();
    return data_->operand->project_state(s2->right(), t);
  }

465
  bdd
466
  tgba_gspn_eesrg::all_acceptance_conditions() const
467
  {
468
469
470
    // There is no acceptance conditions in GSPN systems, they all
    // come from the operand automaton.
    return data_->operand->all_acceptance_conditions();
471
472
473
  }

  bdd
474
  tgba_gspn_eesrg::neg_acceptance_conditions() const
475
  {
476
477
478
    // There is no acceptance conditions in GSPN systems, they all
    // come from the operand automaton.
    return data_->operand->neg_acceptance_conditions();
479
480
  }

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
  // gspn_eesrg_interface
  //////////////////////////////////////////////////////////////////////

  gspn_eesrg_interface::gspn_eesrg_interface(int argc, char **argv,
					     bdd_dict* dict,
					     const gspn_environment& env)
    : dict_(dict), env_(env)
  {
    int res = initialize(argc, argv);
    if (res)
      throw gspn_exeption("initialize()", res);
  }

  gspn_eesrg_interface::~gspn_eesrg_interface()
  {
    int res = finalize();
    if (res)
      throw gspn_exeption("finalize()", res);
  }

  tgba*
  gspn_eesrg_interface::automaton(const tgba* operand) const
  {
    return new tgba_gspn_eesrg(dict_, env_, operand);
  }

507
}