tgbagraph.hh 11.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// -*- coding: utf-8 -*-
// Copyright (C) 2014 Laboratoire de Recherche et Développement de
// l'Epita.
//
// 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 3 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 this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef SPOT_TGBA_TGBAGRAPH_HH
# define SPOT_TGBA_TGBAGRAPH_HH

23
#include "fwd.hh"
24
#include "graph/graph.hh"
25
#include "graph/ngraph.hh"
26
27
#include "tgba/bdddict.hh"
#include "tgba/tgba.hh"
28
#include "tgbaalgos/dupexp.hh"
29
30
31
32
33
34
35
36
#include <sstream>

namespace spot
{

  struct SPOT_API tgba_graph_state: public spot::state
  {
  public:
37
38
39
40
41
    tgba_graph_state():
      spot::state()
    {
    }

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
71
72
73
74
75
76
77
78
    virtual ~tgba_graph_state() noexcept
    {
    }

    virtual int compare(const spot::state* other) const
    {
      auto o = down_cast<const tgba_graph_state*>(other);
      assert(o);

      // Do not simply return "other - this", it might not fit in an int.
      if (o < this)
	return -1;
      if (o > this)
	return 1;
      return 0;
    }

    virtual size_t hash() const
    {
      return
	reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
    }

    virtual tgba_graph_state*
    clone() const
    {
      return const_cast<tgba_graph_state*>(this);
    }

    virtual void destroy() const
    {
    }
  };

  struct SPOT_API tgba_graph_trans_data
  {
    bdd cond;
79
    acc_cond::mark_t acc;
80
81

    explicit tgba_graph_trans_data()
82
      : cond(bddfalse), acc(0)
83
84
85
    {
    }

86
    tgba_graph_trans_data(bdd cond, acc_cond::mark_t acc = 0U)
87
88
89
      : cond(cond), acc(acc)
    {
    }
90
91
92
93
94
95
96
97
98

    bool operator<(const tgba_graph_trans_data& other) const
    {
      if (cond.id() < other.cond.id())
	return true;
      if (cond.id() > other.cond.id())
	return false;
      return acc < other.acc;
    }
99
100
101
102
  };


  template<class Graph>
103
  class SPOT_API tgba_digraph_succ_iterator: public tgba_succ_iterator
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
  {
  private:
    typedef typename Graph::transition transition;
    typedef typename Graph::state_data_t state;
    const Graph* g_;
    transition t_;
    transition p_;

  public:
    tgba_digraph_succ_iterator(const Graph* g, transition t)
      : g_(g), t_(t)
    {
    }

    virtual void recycle(transition t)
    {
      t_ = t;
    }

    virtual bool first()
    {
      p_ = t_;
      return p_;
    }

    virtual bool next()
    {
      p_ = g_->trans_storage(p_).next_succ;
      return p_;
    }

    virtual bool done() const
    {
      return !p_;
    }

    virtual tgba_graph_state* current_state() const
    {
      assert(!done());
      return const_cast<tgba_graph_state*>
	(&g_->state_data(g_->trans_storage(p_).dst));
    }

    virtual bdd current_condition() const
    {
      assert(!done());
      return g_->trans_data(p_).cond;
    }

153
    virtual acc_cond::mark_t current_acceptance_conditions() const
154
155
156
157
158
159
160
161
162
163
164
165
    {
      assert(!done());
      return g_->trans_data(p_).acc;
    }

    transition pos() const
    {
      return p_;
    }

  };

166
  class SPOT_API tgba_digraph: public tgba
167
  {
168
  public:
169
    typedef digraph<tgba_graph_state, tgba_graph_trans_data> graph_t;
170
171

  protected:
172
    graph_t g_;
173
    mutable unsigned init_number_;
174
175

  public:
176
    tgba_digraph(const bdd_dict_ptr& dict)
177
      : tgba(dict),
178
	init_number_(0)
179
180
181
    {
    }

182
183
184
185
186
187
188
189
    explicit tgba_digraph(const const_tgba_digraph_ptr& other)
      : tgba(other->get_dict()),
        g_(other->g_), init_number_(other->init_number_)
      {
	copy_acceptance_conditions_of(other);
	copy_ap_of(other);
      }

190
191
    virtual ~tgba_digraph()
    {
192
      get_dict()->unregister_all_my_variables(this);
193
194
195
196
197
      // Prevent this state from being destroyed by ~tgba(),
      // as the state will be destroyed when g_ is destroyed.
      last_support_conditions_input_ = 0;
    }

198
199
    // FIXME: Once we ditch GCC 4.6, we can using a template aliases
    // (supported from GCC 4.7 onward) instead of this.
200
201
202
    template <typename State_Name,
	      typename Name_Hash = std::hash<State_Name>,
	      typename Name_Equal = std::equal_to<State_Name>>
203
204
205
206
207
208
209
210
211
    struct namer
    {
      typedef named_graph<graph_t, State_Name, Name_Hash, Name_Equal> type;
    };

    template <typename State_Name,
	      typename Name_Hash = std::hash<State_Name>,
	      typename Name_Equal = std::equal_to<State_Name>>
    typename namer<State_Name, Name_Hash, Name_Equal>::type*
212
213
214
215
216
    create_namer()
    {
      return new named_graph<graph_t, State_Name, Name_Hash, Name_Equal>(g_);
    }

217
218
219
220
221
    graph_t& get_graph()
    {
      return g_;
    }

222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    const graph_t& get_graph() const
    {
      return g_;
    }

    unsigned num_states() const
    {
      return g_.num_states();
    }

    unsigned num_transitions() const
    {
      return g_.num_transitions();
    }

    void set_init_state(graph_t::state s)
    {
239
240
      assert(s < num_states());
      init_number_ = s;
241
242
243
244
    }

    void set_init_state(const state* s)
    {
245
      set_init_state(state_number(s));
246
247
    }

248
    graph_t::state get_init_state_number() const
249
250
251
    {
      if (num_states() == 0)
	const_cast<graph_t&>(g_).new_state();
252
      return init_number_;
253
254
    }

255
256
    // FIXME: The return type ought to be const.
    virtual tgba_graph_state* get_init_state() const
257
258
259
    {
      if (num_states() == 0)
	const_cast<graph_t&>(g_).new_state();
260
      return const_cast<tgba_graph_state*>(state_from_number(init_number_));
261
262
263
264
265
266
267
    }

    virtual tgba_succ_iterator*
    succ_iter(const state* st) const
    {
      auto s = down_cast<const typename graph_t::state_storage_t*>(st);
      assert(s);
268
      assert(!s->succ || g_.valid_trans(s->succ));
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

      if (this->iter_cache_)
	{
	  auto it =
	    down_cast<tgba_digraph_succ_iterator<graph_t>*>(this->iter_cache_);
	  it->recycle(s->succ);
	  this->iter_cache_ = nullptr;
	  return it;
	}
      return new tgba_digraph_succ_iterator<graph_t>(&g_, s->succ);
    }

    graph_t::state
    state_number(const state* st) const
    {
      auto s = down_cast<const typename graph_t::state_storage_t*>(st);
      assert(s);
      return s - &g_.state_storage(0);
    }

289
    const tgba_graph_state*
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    state_from_number(graph_t::state n) const
    {
      return &g_.state_data(n);
    }

    virtual std::string format_state(const state* st) const
    {
      std::stringstream ss;
      ss << state_number(st);
      return ss.str();
    }

    tgba_graph_trans_data& trans_data(const tgba_succ_iterator* it)
    {
      auto* i = down_cast<const tgba_digraph_succ_iterator<graph_t>*>(it);
      return g_.trans_data(i->pos());
    }

308
309
310
311
    tgba_graph_trans_data& trans_data(unsigned t)
    {
      return g_.trans_data(t);
    }
312

313
314
315
316
317
318
319
320
321
322
323
    const tgba_graph_trans_data& trans_data(const tgba_succ_iterator* it) const
    {
      auto* i = down_cast<const tgba_digraph_succ_iterator<graph_t>*>(it);
      return g_.trans_data(i->pos());
    }

    const tgba_graph_trans_data& trans_data(unsigned t) const
    {
      return g_.trans_data(t);
    }

324
325
326
327
328
329
330
331
332
    void set_acceptance_conditions(unsigned num)
    {
      if (num < acc_.num_sets())
	{
	  acc_.~acc_cond();
	  new (&acc_) acc_cond(get_dict());
	}
      acc_.add_sets(num - acc_.num_sets());
      prop_single_acc_set(num == 1);
333
334
      if (num == 0)
	prop_state_based_acc();
335
336
337
338
339
340
341
    }

    acc_cond::mark_t set_single_acceptance_set()
    {
      set_acceptance_conditions(1);
      return acc_.mark(0);
    }
342

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    graph_t::trans_storage_t& trans_storage(const tgba_succ_iterator* it)
    {
      auto* i = down_cast<const tgba_digraph_succ_iterator<graph_t>*>(it);
      return g_.trans_storage(i->pos());
    }

    graph_t::trans_storage_t& trans_storage(unsigned t)
    {
      return g_.trans_storage(t);
    }

    const graph_t::trans_storage_t
      trans_storage(const tgba_succ_iterator* it) const
    {
      auto* i = down_cast<const tgba_digraph_succ_iterator<graph_t>*>(it);
      return g_.trans_storage(i->pos());
    }

    const graph_t::trans_storage_t trans_storage(unsigned t) const
    {
      return g_.trans_storage(t);
    }

    void set_acceptance_conditions(bdd all);

368
369
370
371
372
373
374
375
376
377
378
    unsigned new_state()
    {
      return g_.new_state();
    }

    unsigned new_states(unsigned n)
    {
      return g_.new_states(n);
    }

    unsigned new_transition(unsigned src, unsigned dst,
379
			    bdd cond, acc_cond::mark_t acc = 0U)
380
381
382
383
    {
      return g_.new_transition(src, dst, cond, acc);
    }

384
385
386
387
    unsigned new_acc_transition(unsigned src, unsigned dst,
				bdd cond, bool acc = true)
    {
      if (acc)
388
	return g_.new_transition(src, dst, cond, acc_.all_sets());
389
390
391
392
      else
	return g_.new_transition(src, dst, cond);
    }

393
394
395
396
397
398
399
400
401
402
403
404
405
406
    auto out(unsigned src) const
      SPOT_RETURN(g_.out(src));
    auto out(unsigned src)
      SPOT_RETURN(g_.out(src));

    auto states() const
      SPOT_RETURN(g_.states());
    auto states()
      SPOT_RETURN(g_.states());

    auto transitions() const
      SPOT_RETURN(g_.transitions());
    auto transitions()
      SPOT_RETURN(g_.transitions());
407

408
    auto is_dead_transition(const graph_t::trans_storage_t& t) const
409
410
      SPOT_RETURN(g_.is_dead_transition(t));

411
    /// \brief Copy the acceptance conditions of another tgba.
412
    void copy_acceptance_conditions_of(const const_tgba_ptr& a)
413
    {
414
415
      assert(acc_.num_sets() == 0);
      acc_.add_sets(a->acc().num_sets());
416
417
    }

418
    void copy_ap_of(const const_tgba_ptr& a)
419
    {
420
      get_dict()->register_all_propositions_of(a, this);
421
422
    }

423
    virtual bdd compute_support_conditions(const state* s) const
424
    {
425
426
427
428
      bdd sum = bddfalse;
      for (auto& t: out(state_number(s)))
	sum |= t.cond;
      return sum;
429
    }
430
431
432

    /// Iterate over all transitions, and merge those with compatible
    /// extremities and acceptance.
433
    void merge_transitions();
434

435
436
437
    /// Remove all states without successors.
    void purge_dead_states();

438
439
440
    /// Remove all unreachable states.
    void purge_unreachable_states();

441
442
    bool state_is_accepting(unsigned s) const
    {
443
      assert(has_state_based_acc() || acc_.num_sets() == 0);
444
445
446
      for (auto& t: g_.out(s))
	// Stop at the first transition, since the remaining should be
	// labeled identically.
447
	return acc_.accepting(t.acc);
448
449
450
451
452
453
454
455
      return false;
    }

    bool state_is_accepting(const state* s) const
    {
      return state_is_accepting(state_number(s));
    }

456
457
  };

458
  inline tgba_digraph_ptr make_tgba_digraph(const bdd_dict_ptr& dict)
459
460
461
  {
    return std::make_shared<tgba_digraph>(dict);
  }
462

463
464
465
466
467
  inline tgba_digraph_ptr make_tgba_digraph(const tgba_digraph_ptr& aut)
  {
    return std::make_shared<tgba_digraph>(aut);
  }

468
469
470
471
472
473
474
475
476
477
478
479
480
481
  inline tgba_digraph_ptr make_tgba_digraph(const const_tgba_digraph_ptr& aut)
  {
    return std::make_shared<tgba_digraph>(aut);
  }

  inline tgba_digraph_ptr make_tgba_digraph(const const_tgba_ptr& aut)
  {
    auto p = std::dynamic_pointer_cast<const tgba_digraph>(aut);
    if (p)
      return std::make_shared<tgba_digraph>(p);
    else
      return tgba_dupexp_dfs(aut);
  }

482
483
484
}

#endif // SPOT_TGBA_TGBAGRAPH_HH