gspn.cc 9.83 KB
Newer Older
1
2
#include <sstream>
#include <map>
3
#include <cassert>
4
5
6
7
8
9
10
#include "gspnlib.h"
#include "gspn.hh"
#include "ltlvisit/destroy.hh"

namespace spot
{

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  gspn_interface::gspn_interface(int argc, char **argv)
  {
    int res = initialize(argc, argv);
    if (res)
      throw gspn_exeption("initialize()", res);
  }

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


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
  // state_gspn
  //////////////////////////////////////////////////////////////////////

  class state_gspn: public state
  {
  public:
    state_gspn(State s)
      : state_(s)
    {
    }

    virtual
    ~state_gspn()
    {
    }

    virtual int
    compare(const state* other) const
    {
      const state_gspn* o = dynamic_cast<const state_gspn*>(other);
      assert(o);
      return reinterpret_cast<char*>(o->get_state())
	- reinterpret_cast<char*>(get_state());
    }

    virtual state_gspn* clone() const
    {
      return new state_gspn(get_state());
    }

    State
    get_state() const
    {
      return state_;
    }

  private:
    State state_;
  }; // state_gspn


67
68
  // tgba_gspn_private_
  //////////////////////////////////////////////////////////////////////
69

70
  struct tgba_gspn_private_
71
72
73
  {
    int refs;			// reference count

74
    bdd_dict* dict;
75
    typedef std::pair<AtomicPropKind, bdd> ab_pair;
76
    typedef std::map<AtomicProp, ab_pair> prop_map;
77
78
79
    prop_map prop_dict;
    AtomicProp *all_indexes;
    size_t index_count;
80
81
82
    const state_gspn* last_state_conds_input;
    bdd last_state_conds_output;

83

84
    tgba_gspn_private_(bdd_dict* dict, const gspn_environment& env)
85
      : refs(1), dict(dict), all_indexes(0), last_state_conds_input(0)
86
87
88
    {
      const gspn_environment::prop_map& p = env.get_prop_map();

89
      try
90
	{
91
92
93
94
95
96
97
	  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)
98
		throw gspn_exeption("prop_index()", err);
99
100
101
	      AtomicPropKind kind;
	      err = prop_kind(index, &kind);
	      if (err)
102
		throw gspn_exeption("prop_kind()", err);
103
104
105
106
107
108
109
110
111
112
113

	      prop_dict[index] = ab_pair(kind, bdd_ithvar(var));
	    }

	  index_count = prop_dict.size();
	  all_indexes = new AtomicProp[index_count];

	  unsigned idx = 0;
	  for (prop_map::const_iterator i = prop_dict.begin();
	       i != prop_dict.end(); ++i)
	    all_indexes[idx++] = i->first;
114
	}
115
116
117
118
119
      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);
120
	  throw;
121
122
	}
    }
123

124
125
126
    tgba_gspn_private_::~tgba_gspn_private_()
    {
      dict->unregister_all_my_variables(this);
127
128
      if (last_state_conds_input)
	delete last_state_conds_input;
129
130
      if (all_indexes)
	delete[] all_indexes;
131
132
133
134
135
136
137
138
139
140
141
    }

    bdd index_to_bdd(AtomicProp index) const
    {
      if (index == EVENT_TRUE)
	return bddtrue;
      prop_map::const_iterator i = prop_dict.find(index);
      assert(i != prop_dict.end());
      return i->second.second;
    }

142
    bdd state_conds(const state_gspn* s)
143
    {
144
145
146
147
148
149
      // Use cached value if possible.
      if (!last_state_conds_input ||
	  last_state_conds_input->compare(s) != 0)
	{
	  // Build the BDD of the conditions available on this state.
	  unsigned char* cube = 0;
150
	  // FIXME: This is temporary. We ought to ask only what we need.
151
152
153
154
155
156
157
158
159
160
161
162
163
	  AtomicProp* want = all_indexes;
	  size_t count = index_count;
	  int res = satisfy(s->get_state(), want, &cube, count);
	  if (res)
	    throw gspn_exeption("satisfy()", res);
	  assert(cube);
	  last_state_conds_output = bddtrue;
	  for (size_t i = 0; i < count; ++i)
	    {
	      bdd v = index_to_bdd(want[i]);
	      last_state_conds_output &= cube[i] ? v : !v;
	    }
	  satisfy_free(cube);
164

165
166
167
168
169
	  if (last_state_conds_input)
	    delete last_state_conds_input;
	  last_state_conds_input = s->clone();
	}
      return last_state_conds_output;
170
    }
171
  };
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

  // tgba_succ_iterator_gspn
  //////////////////////////////////////////////////////////////////////

  class tgba_succ_iterator_gspn: public tgba_succ_iterator
  {
  public:
    tgba_succ_iterator_gspn(bdd state_conds, State state,
			    tgba_gspn_private_* data)
      : state_conds_(state_conds),
	successors_(0),
	size_(0),
	current_(0),
	data_(data)
    {
188
      int res = succ(state, &successors_, &size_);
189
      if (res)
190
	throw gspn_exeption("succ()", res);
191
192
      assert(successors_);
      // GSPN is expected to return a looping "dead" transition where
193
      // there is no successor.
194
195
      assert(size_> 0);
    }
196
197

    virtual
198
199
200
201
    ~tgba_succ_iterator_gspn()
    {
      succ_free(successors_);
    }
202
203

    virtual void
204
205
206
207
    first()
    {
      current_ = 0;
    }
208
209

    virtual void
210
211
212
213
214
215
    next()
    {
      assert(!done());
      ++current_;
    }

216
    virtual bool
217
218
219
220
221
    done() const
    {
      return current_ >= size_;
    }

222
    virtual state*
223
224
225
226
227
    current_state() const
    {
      return new state_gspn(successors_[current_].s);
    }

228
    virtual bdd
229
230
231
232
233
    current_condition() const
    {
      bdd p = data_->index_to_bdd(successors_[current_].p);
      return state_conds_ & p;
    }
234
235

    virtual bdd
236
237
238
239
240
241
242
243
    current_accepting_conditions() const
    {
      // There is no accepting conditions in GSPN systems.
      return bddfalse;
    }
  private:
    bdd state_conds_;		/// All conditions known on STATE.
    EventPropSucc* successors_; /// array of successors
244
    size_t size_;		/// size of successors_
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
    size_t current_;		/// current position in successors_
    tgba_gspn_private_* data_;
  }; // tgba_succ_iterator_gspn


  // gspn_environment
  //////////////////////////////////////////////////////////////////////

  gspn_environment::gspn_environment()
  {
  }

  gspn_environment::~gspn_environment()
  {
    for (prop_map::iterator i = props_.begin(); i != props_.end(); ++i)
      ltl::destroy(i->second);
  }

  bool
  gspn_environment::declare(const std::string& prop_str)
  {
    if (props_.find(prop_str) != props_.end())
      return false;
    props_[prop_str] = ltl::atomic_prop::instance(prop_str, *this);
    return true;
  }
271
272

  ltl::formula*
273
274
275
276
277
278
279
280
281
282
  gspn_environment::require(const std::string& prop_str)
  {
    prop_map::iterator i = props_.find(prop_str);
    if (i == props_.end())
      return 0;
    // It's an atomic_prop, so we do not have to use the clone() visitor.
    return i->second->ref();
  }

  /// Get the name of the environment.
283
  const std::string&
284
285
286
  gspn_environment::name()
  {
    static std::string name("gspn environment");
287
    return name;
288
  }
289
290

  const gspn_environment::prop_map&
291
292
293
294
295
296
297
298
299
300
  gspn_environment::get_prop_map() const
  {
    return props_;
  }


  // tgba_gspn
  //////////////////////////////////////////////////////////////////////


301
  tgba_gspn::tgba_gspn(bdd_dict* dict, const gspn_environment& env)
302
  {
303
    data_ = new tgba_gspn_private_(dict, env);
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  }

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

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

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

329
330
331
332
333
  state* tgba_gspn::get_init_state() const
  {
    State s;
    int err = initial_state(&s);
    if (err)
334
      throw gspn_exeption("initial_state()", err);
335
336
337
    return new state_gspn(s);
  }

338
  tgba_succ_iterator*
339
340
341
  tgba_gspn::succ_iter(const state* state,
		       const state* global_state,
		       const tgba* global_automaton) const
342
343
344
  {
    const state_gspn* s = dynamic_cast<const state_gspn*>(state);
    assert(s);
345
346
347
348
349
350
351
    (void) global_state;
    (void) global_automaton;
    // FIXME: Should pass global_automaton->support_variables(state)
    // to state_conds.
    bdd state_conds = data_->state_conds(s);
    return new tgba_succ_iterator_gspn(state_conds, s->get_state(), data_);
  }
352

353
354
355
356
357
358
359
  bdd
  tgba_gspn::compute_support_conditions(const spot::state* state) const
  {
    const state_gspn* s = dynamic_cast<const state_gspn*>(state);
    assert(s);
    return data_->state_conds(s);
  }
360

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  bdd
  tgba_gspn::compute_support_variables(const spot::state* state) const
  {
    // FIXME: At the time of writing, only tgba_gspn calls
    // support_variables on the root of a product to gather the
    // variables used by all other automata and let GPSN compute only
    // these.  Because support_variables() is recursive over the
    // product treee, tgba_gspn::support_variables should not output
    // all the variables known by GSPN; this would ruin the sole
    // purpose of this function.
    // However this works because we assume there is at most one
    // tgba_gspn automata in a product (a legitimate assumption
    // since the GSPN API is not re-entrant) and only this automata
    // need to call support_variables (now _this_ is shady).
    (void) state;
    return bddtrue;
377
378
  }

379
  bdd_dict*
380
381
382
383
384
  tgba_gspn::get_dict() const
  {
    return data_->dict;
  }

385
  std::string
386
387
388
389
390
  tgba_gspn::format_state(const state* state) const
  {
    const state_gspn* s = dynamic_cast<const state_gspn*>(state);
    assert(s);
    std::ostringstream os;
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    char* str;
    int err = print_state(s->get_state(), &str);
    if (err)
      throw gspn_exeption("print_state()", err);

    // Rewrite all new lines as \\\n.
    const char* pos = str;
    while (*pos)
      {
	switch (*pos)
	  {
	    // Rewrite all new lines as \\n, and strip the last one.
	  case '\n':
	    if (pos[1])
	      os << "\\n";
	    break;
	  default:
	    os << *pos;
	  }
	++pos;
      }
    free(str);
413
414
415
    return os.str();
  }

416
  bdd
417
418
419
420
421
  tgba_gspn::all_accepting_conditions() const
  {
    // There is no accepting conditions in GSPN systems.
    return bddfalse;
  }
422
423

  bdd
424
425
426
427
428
429
430
431
432
  tgba_gspn::neg_accepting_conditions() const
  {
    // There is no accepting conditions in GSPN systems.
    return bddtrue;
  }



}