twagraph.hh 12.9 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2014, 2015, 2016 Laboratoire de Recherche et Développement
3
// de l'Epita.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// 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/>.

20
#pragma once
21

22
23
24
25
26
27
28
#include <spot/twa/fwd.hh>
#include <spot/graph/graph.hh>
#include <spot/graph/ngraph.hh>
#include <spot/twa/bdddict.hh>
#include <spot/twa/twa.hh>
#include <spot/twaalgos/copy.hh>
#include <spot/tl/formula.hh>
29
30
31
32
33
#include <sstream>

namespace spot
{

34
  struct SPOT_API twa_graph_state: public spot::state
35
36
  {
  public:
37
    twa_graph_state() noexcept
38
39
40
    {
    }

41
    virtual ~twa_graph_state() noexcept
42
43
44
    {
    }

45
    virtual int compare(const spot::state* other) const override
46
    {
47
      auto o = down_cast<const twa_graph_state*>(other);
48
      SPOT_ASSERT(o);
49
50
51

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

58
    virtual size_t hash() const override
59
60
    {
      return
61
        reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
62
63
    }

64
    virtual twa_graph_state*
65
    clone() const override
66
    {
67
      return const_cast<twa_graph_state*>(this);
68
69
    }

70
    virtual void destroy() const override
71
72
73
74
    {
    }
  };

75
  struct SPOT_API twa_graph_edge_data
76
77
  {
    bdd cond;
78
    acc_cond::mark_t acc;
79

80
    explicit twa_graph_edge_data() noexcept
81
      : cond(bddfalse), acc(0)
82
83
84
    {
    }

85
    twa_graph_edge_data(bdd cond, acc_cond::mark_t acc = 0U) noexcept
86
87
88
      : cond(cond), acc(acc)
    {
    }
89

90
    bool operator<(const twa_graph_edge_data& other) const
91
92
    {
      if (cond.id() < other.cond.id())
93
        return true;
94
      if (cond.id() > other.cond.id())
95
        return false;
96
97
      return acc < other.acc;
    }
98

99
    bool operator==(const twa_graph_edge_data& other) const
100
101
102
103
    {
      return cond.id() == other.cond.id() &&
        acc == other.acc;
    }
104
105
106
107
  };


  template<class Graph>
108
  class SPOT_API twa_graph_succ_iterator final:
109
    public twa_succ_iterator
110
111
  {
  private:
112
    typedef typename Graph::edge edge;
113
114
    typedef typename Graph::state_data_t state;
    const Graph* g_;
115
116
    edge t_;
    edge p_;
117
118

  public:
119
    twa_graph_succ_iterator(const Graph* g, edge t)
120
121
122
123
      : g_(g), t_(t)
    {
    }

124
    void recycle(edge t)
125
126
127
128
    {
      t_ = t;
    }

129
    virtual bool first() override
130
131
132
133
134
    {
      p_ = t_;
      return p_;
    }

135
    virtual bool next() override
136
    {
137
      p_ = g_->edge_storage(p_).next_succ;
138
139
140
      return p_;
    }

141
    virtual bool done() const override
142
143
144
145
    {
      return !p_;
    }

146
    virtual const twa_graph_state* dst() const override
147
    {
148
      SPOT_ASSERT(!done());
149
      return &g_->state_data(g_->edge_storage(p_).dst);
150
151
    }

152
    virtual bdd cond() const override
153
    {
154
      SPOT_ASSERT(!done());
155
      return g_->edge_data(p_).cond;
156
157
    }

158
    virtual acc_cond::mark_t acc() const override
159
    {
160
      SPOT_ASSERT(!done());
161
      return g_->edge_data(p_).acc;
162
163
    }

164
    edge pos() const
165
166
167
168
169
170
    {
      return p_;
    }

  };

171
  class SPOT_API twa_graph final: public twa
172
  {
173
  public:
174
175
    typedef digraph<twa_graph_state, twa_graph_edge_data> graph_t;
    typedef graph_t::edge_storage_t edge_storage_t;
176
177
178
179
180
    // We avoid using graph_t::state because graph_t is not
    // instantiated in the SWIG bindings, and SWIG would therefore
    // handle graph_t::state as an abstract type.
    typedef unsigned state_num;
    static_assert(std::is_same<typename graph_t::state, state_num>::value,
181
                  "type mismatch");
182
183

  protected:
184
    graph_t g_;
185
    mutable unsigned init_number_;
186
187

  public:
188
    twa_graph(const bdd_dict_ptr& dict)
189
      : twa(dict),
190
        init_number_(0)
191
192
193
    {
    }

194
    explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
195
      : twa(other->get_dict()),
196
197
        g_(other->g_), init_number_(other->init_number_)
      {
198
199
200
        copy_acceptance_of(other);
        copy_ap_of(other);
        prop_copy(other, p);
201
202
      }

203
    virtual ~twa_graph()
204
205
206
    {
    }

207
#ifndef SWIG
208
    template <typename State_Name,
209
210
              typename Name_Hash = std::hash<State_Name>,
              typename Name_Equal = std::equal_to<State_Name>>
211
    using namer = named_graph<graph_t, State_Name, Name_Hash, Name_Equal>;
212
213

    template <typename State_Name,
214
215
              typename Name_Hash = std::hash<State_Name>,
              typename Name_Equal = std::equal_to<State_Name>>
216
    namer<State_Name, Name_Hash, Name_Equal>*
217
218
219
220
221
    create_namer()
    {
      return new named_graph<graph_t, State_Name, Name_Hash, Name_Equal>(g_);
    }

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
222
    namer<formula>*
223
224
    create_formula_namer()
    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
225
      return create_namer<formula>();
226
227
228
    }

    void
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
229
    release_formula_namer(namer<formula>* namer, bool keep_names);
230
#endif
231

232
233
234
235
236
    graph_t& get_graph()
    {
      return g_;
    }

237
238
239
240
241
242
243
244
245
246
    const graph_t& get_graph() const
    {
      return g_;
    }

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

247
    unsigned num_edges() const
248
    {
249
      return g_.num_edges();
250
251
    }

252
    void set_init_state(state_num s)
253
    {
254
255
256
      if (SPOT_UNLIKELY(s >= num_states()))
        throw std::invalid_argument
          ("set_init_state() called with nonexisiting state");
257
      init_number_ = s;
258
259
260
261
    }

    void set_init_state(const state* s)
    {
262
      set_init_state(state_number(s));
263
264
    }

265
    state_num get_init_state_number() const
266
    {
267
      // If the automaton has no state, it has no initial state.
268
      if (num_states() == 0)
269
        throw std::runtime_error("automaton has no state at all");
270
      return init_number_;
271
272
    }

273
    virtual const twa_graph_state* get_init_state() const override
274
    {
275
      return state_from_number(get_init_state_number());
276
277
    }

278
    virtual twa_succ_iterator*
279
    succ_iter(const state* st) const override
280
281
    {
      auto s = down_cast<const typename graph_t::state_storage_t*>(st);
282
283
      SPOT_ASSERT(s);
      SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
284
285

      if (this->iter_cache_)
286
287
288
289
290
291
292
        {
          auto it =
            down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
          it->recycle(s->succ);
          this->iter_cache_ = nullptr;
          return it;
        }
293
      return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
294
295
    }

296
    state_num
297
298
299
    state_number(const state* st) const
    {
      auto s = down_cast<const typename graph_t::state_storage_t*>(st);
300
      SPOT_ASSERT(s);
301
302
303
      return s - &g_.state_storage(0);
    }

304
    const twa_graph_state*
305
    state_from_number(state_num n) const
306
307
308
309
    {
      return &g_.state_data(n);
    }

310
    std::string format_state(unsigned n) const
311
312
    {
      std::stringstream ss;
313
      ss << n;
314
315
316
      return ss.str();
    }

317
    virtual std::string format_state(const state* st) const override
318
319
320
321
    {
      return format_state(state_number(st));
    }

322
    unsigned edge_number(const twa_succ_iterator* it) const
323
    {
324
      auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
325
326
327
328
329
330
      return i->pos();
    }

    twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
    {
      return g_.edge_data(edge_number(it));
331
332
    }

333
    twa_graph_edge_data& edge_data(unsigned t)
334
    {
335
      return g_.edge_data(t);
336
    }
337

338
    const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
339
    {
340
      return g_.edge_data(edge_number(it));
341
342
    }

343
    const twa_graph_edge_data& edge_data(unsigned t) const
344
    {
345
      return g_.edge_data(t);
346
347
    }

348
    edge_storage_t& edge_storage(const twa_succ_iterator* it)
349
    {
350
      return g_.edge_storage(edge_number(it));
351
352
    }

353
    edge_storage_t& edge_storage(unsigned t)
354
    {
355
      return g_.edge_storage(t);
356
357
    }

358
359
    const edge_storage_t
      edge_storage(const twa_succ_iterator* it) const
360
    {
361
      return g_.edge_storage(edge_number(it));
362
363
    }

364
    const edge_storage_t edge_storage(unsigned t) const
365
    {
366
      return g_.edge_storage(t);
367
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);
    }

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

385
    unsigned new_acc_edge(unsigned src, unsigned dst,
386
                                bdd cond, bool acc = true)
387
388
    {
      if (acc)
389
        return g_.new_edge(src, dst, cond, this->acc().all_sets());
390
      else
391
        return g_.new_edge(src, dst, cond);
392
393
    }

394
#ifndef SWIG
395
396
397
398
399
400
    internal::state_out<const graph_t>
    out(unsigned src) const
    {
      return g_.out(src);
    }
#endif
401

402
403
404
405
406
407
408
    internal::state_out<graph_t>
    out(unsigned src)
    {
      return g_.out(src);
    }

#ifndef SWIG
409
410
411
412
413
    auto states() const
      SPOT_RETURN(g_.states());
    auto states()
      SPOT_RETURN(g_.states());

414
415
416
417
418
419
    internal::all_trans<const graph_t>
    edges() const
    {
      return g_.edges();
    }
#endif
420

421
422
423
424
425
426
427
    internal::all_trans<graph_t>
    edges()
    {
      return g_.edges();
    }

#ifndef SWIG
428
429
430
431
    auto edge_vector() const
      SPOT_RETURN(g_.edge_vector());
    auto edge_vector()
      SPOT_RETURN(g_.edge_vector());
432

433
434
    auto is_dead_edge(const graph_t::edge_storage_t& t) const
      SPOT_RETURN(g_.is_dead_edge(t));
435
#endif
436

437
    /// Iterate over all edges, and merge those with compatible
438
    /// extremities and acceptance.
439
    void merge_edges();
440

441
442
443
444
445
446
447
448
    /// \brief Remove all dead states
    ///
    /// Dead states are all the states that cannot be part of
    /// an infinite run of the automaton.  This includes
    /// states without successors, unreachable states, and states
    /// that only have dead successors.
    ///
    /// \see purge_unreachable_states
449
450
    void purge_dead_states();

451
452
453
454
455
456
457
458
459
460
461
    /// \brief Remove all unreachable states.
    ///
    /// A state is unreachable if it cannot be reached from the initial state.
    ///
    /// Use this function if you have declared more states than you
    /// actually need in the automaton.
    ///
    /// purge_dead_states() will remove more states than
    /// purge_unreachable_states().
    ///
    /// \see purge_dead_states
462
463
    void purge_unreachable_states();

464
    /// \brief Remove unused atomic propositions
465
466
467
468
469
    ///
    /// Remove, from the list of atomic propositions registered by the
    /// automaton, those that are not actually used by its labels.
    void remove_unused_ap();

470
471
    acc_cond::mark_t state_acc_sets(unsigned s) const
    {
472
473
474
475
476
      if (SPOT_UNLIKELY(!((bool)prop_state_acc() || num_sets() == 0)))
        throw std::runtime_error
          ("state_acc_sets() should only be called on "
           "automata with state-based acceptance");

477
      for (auto& t: g_.out(s))
478
479
480
        // Stop at the first edge, since the remaining should be
        // labeled identically.
        return t.acc;
481
482
483
      return 0U;
    }

484
485
    bool state_is_accepting(unsigned s) const
    {
486
487
488
489
      if (SPOT_UNLIKELY(!((bool)prop_state_acc() || num_sets() == 0)))
        throw std::runtime_error
          ("state_is_accepting() should only be called on "
           "automata with state-based acceptance");
490
      for (auto& t: g_.out(s))
491
492
493
        // Stop at the first edge, since the remaining should be
        // labeled identically.
        return acc().accepting(t.acc);
494
495
496
497
498
499
500
501
      return false;
    }

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

502
    bool operator==(const twa_graph& aut) const
503
504
    {
      if (num_states() != aut.num_states() ||
505
          num_edges() != aut.num_edges() ||
506
          num_sets() != aut.num_sets())
507
        return false;
508
509
      auto& trans1 = edge_vector();
      auto& trans2 = aut.edge_vector();
510
511
512
      return std::equal(trans1.begin() + 1, trans1.end(),
                        trans2.begin() + 1);
    }
513
514

    void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
515
516
  };

517
  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
518
  {
519
    return std::make_shared<twa_graph>(dict);
520
  }
521

522
  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
523
                                      twa::prop_set p)
524
  {
525
    return std::make_shared<twa_graph>(aut, p);
526
527
  }

528
  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
529
                                      twa::prop_set p)
530
  {
531
    return std::make_shared<twa_graph>(aut, p);
532
533
  }

534
  inline twa_graph_ptr make_twa_graph(const const_twa_ptr& aut,
535
                                      twa::prop_set p)
536
  {
537
    auto a = std::dynamic_pointer_cast<const twa_graph>(aut);
538
    if (a)
539
      return std::make_shared<twa_graph>(a, p);
540
    else
541
      return copy(aut, p);
542
  }
543
}