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

8
9
10
11
12
// FIXME: Override signed definition of EVENT_TRUE until this is fixed 
// in gspnlib.h.
#undef EVENT_TRUE
#define EVENT_TRUE 0U

13
14
15
namespace spot
{

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  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);
  }


31
32
  // tgba_gspn_private_
  //////////////////////////////////////////////////////////////////////
33

34
  struct tgba_gspn_private_
35
36
37
  {
    int refs;			// reference count

38
    bdd_dict* dict;
39
    typedef std::pair<AtomicPropKind, bdd> ab_pair;
40
    typedef std::map<AtomicProp, ab_pair> prop_map;
41
42
43
    prop_map prop_dict;
    AtomicProp *all_indexes;
    size_t index_count;
44

45
46
    tgba_gspn_private_(bdd_dict* dict, const gspn_environment& env)
      : refs(0), dict(dict)
47
48
49
    {
      const gspn_environment::prop_map& p = env.get_prop_map();

50
      try
51
	{
52
53
54
55
56
57
58
	  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)
59
		throw gspn_exeption("prop_index()", err);
60
61
62
	      AtomicPropKind kind;
	      err = prop_kind(index, &kind);
	      if (err)
63
		throw gspn_exeption("prop_kind()", err);
64
65
66
67
68
69
70
71
72
73
74

	      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;
75
	}
76
77
78
79
80
81
82
      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);
	}
    }
83

84
85
86
    tgba_gspn_private_::~tgba_gspn_private_()
    {
      dict->unregister_all_my_variables(this);
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
    }

    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;
    }
  };

  // state_gspn
  //////////////////////////////////////////////////////////////////////

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

    virtual
    ~state_gspn()
    {
    }

115
    virtual int
116
117
118
119
    compare(const state* other) const
    {
      const state_gspn* o = dynamic_cast<const state_gspn*>(other);
      assert(o);
120
121
      return reinterpret_cast<char*>(o->get_state())
	- reinterpret_cast<char*>(get_state());
122
123
124
125
126
127
    }

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

129
130
131
132
133
134
135
136
137
    State
    get_state() const
    {
      return state_;
    }

  private:
    State state_;
  }; // state_gspn
138

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

  // 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)
    {
154
      int res = succ(state, &successors_, &size_);
155
      if (res)
156
	throw gspn_exeption("succ()", res);
157
158
159
160
161
      assert(successors_);
      // GSPN is expected to return a looping "dead" transition where
      // there is no successor,
      assert(size_> 0);
    }
162
163

    virtual
164
165
166
167
    ~tgba_succ_iterator_gspn()
    {
      succ_free(successors_);
    }
168
169

    virtual void
170
171
172
173
    first()
    {
      current_ = 0;
    }
174
175

    virtual void
176
177
178
179
180
181
    next()
    {
      assert(!done());
      ++current_;
    }

182
    virtual bool
183
184
185
186
187
    done() const
    {
      return current_ >= size_;
    }

188
    virtual state*
189
190
191
192
193
    current_state() const
    {
      return new state_gspn(successors_[current_].s);
    }

194
    virtual bdd
195
196
197
198
199
    current_condition() const
    {
      bdd p = data_->index_to_bdd(successors_[current_].p);
      return state_conds_ & p;
    }
200
201

    virtual bdd
202
203
204
205
206
207
208
209
    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
210
    size_t size_;		/// size of successors_
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
    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;
  }
237
238

  ltl::formula*
239
240
241
242
243
244
245
246
247
248
  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.
249
  const std::string&
250
251
252
  gspn_environment::name()
  {
    static std::string name("gspn environment");
253
    return name;
254
  }
255
256

  const gspn_environment::prop_map&
257
258
259
260
261
262
263
264
265
266
  gspn_environment::get_prop_map() const
  {
    return props_;
  }


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


267
  tgba_gspn::tgba_gspn(bdd_dict* dict, const gspn_environment& env)
268
  {
269
    data_ = new tgba_gspn_private_(dict, env);
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  }

  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;
  }
294

295
296
297
298
299
  state* tgba_gspn::get_init_state() const
  {
    State s;
    int err = initial_state(&s);
    if (err)
300
      throw gspn_exeption("initial_state()", err);
301
302
303
    return new state_gspn(s);
  }

304
  tgba_succ_iterator*
305
306
307
308
309
310
311
  tgba_gspn::succ_iter(const state* state) const
  {
    const state_gspn* s = dynamic_cast<const state_gspn*>(state);
    assert(s);

    // Build the BDD of the conditions available on this state.
    unsigned char* cube = 0;
312
313
314
315
    // This is temporary. We ought to ask only what we need.
    AtomicProp* want = data_->all_indexes;
    size_t count = data_->index_count;
    int res = satisfy(s->get_state(), want, &cube, count);
316
    if (res)
317
      throw gspn_exeption("satisfy()", res);
318
319
    assert(cube);
    bdd state_conds = bddtrue;
320
    for (size_t i = 0; i < count; ++i)
321
      {
322
323
	bdd v = data_->index_to_bdd(want[i]);
	state_conds &= cube[i] ? v : !v;
324
325
      }
    satisfy_free(cube);
326

327
328
329
    return new tgba_succ_iterator_gspn(state_conds, s->get_state(), data_);
  }

330
  bdd_dict*
331
332
333
334
335
  tgba_gspn::get_dict() const
  {
    return data_->dict;
  }

336
  std::string
337
338
339
340
341
342
343
344
345
346
  tgba_gspn::format_state(const state* state) const
  {
    const state_gspn* s = dynamic_cast<const state_gspn*>(state);
    assert(s);
    // FIXME: We ought to ask GSPN to format the state.
    std::ostringstream os;
    os << s->get_state();
    return os.str();
  }

347
  bdd
348
349
350
351
352
  tgba_gspn::all_accepting_conditions() const
  {
    // There is no accepting conditions in GSPN systems.
    return bddfalse;
  }
353
354

  bdd
355
356
357
358
359
360
361
362
363
  tgba_gspn::neg_accepting_conditions() const
  {
    // There is no accepting conditions in GSPN systems.
    return bddtrue;
  }



}