spot  2.9.6
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2020 Laboratoire de Recherche et Développement
3 // de l'Epita.
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include <spot/twa/fwd.hh>
23 #include <spot/graph/graph.hh>
24 #include <spot/graph/ngraph.hh>
25 #include <spot/twa/bdddict.hh>
26 #include <spot/twa/twa.hh>
27 #include <spot/tl/formula.hh>
28 #include <sstream>
29 
30 namespace spot
31 {
32 
39  struct SPOT_API twa_graph_state: public spot::state
40  {
41  public:
42  twa_graph_state() noexcept
43  {
44  }
45 
46  twa_graph_state(const twa_graph_state&) noexcept
47  {
48  }
49 
50  twa_graph_state& operator=(const twa_graph_state&) noexcept
51  {
52  return *this;
53  }
54 
55  virtual ~twa_graph_state() noexcept
56  {
57  }
58 
59  virtual int compare(const spot::state* other) const override
60  {
61  auto o = down_cast<const twa_graph_state*>(other);
62 
63  // Do not simply return "other - this", it might not fit in an int.
64  if (o < this)
65  return -1;
66  if (o > this)
67  return 1;
68  return 0;
69  }
70 
71  virtual size_t hash() const override
72  {
73  return
74  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
75  }
76 
77  virtual twa_graph_state*
78  clone() const override
79  {
80  return const_cast<twa_graph_state*>(this);
81  }
82 
83  virtual void destroy() const override
84  {
85  }
86  };
87 
95  struct SPOT_API twa_graph_edge_data
96  {
97  bdd cond;
98  acc_cond::mark_t acc;
99 
100  explicit twa_graph_edge_data() noexcept
101  : cond(bddfalse), acc({})
102  {
103  }
104 
106  bdd cond,
107  acc_cond::mark_t acc = {}) noexcept
108  : cond(cond), acc(acc)
109  {
110  }
111 
112  bool operator<(const twa_graph_edge_data& other) const
113  {
114  if (cond.id() < other.cond.id())
115  return true;
116  if (cond.id() > other.cond.id())
117  return false;
118  return acc < other.acc;
119  }
120 
121  bool operator==(const twa_graph_edge_data& other) const
122  {
123  return cond.id() == other.cond.id() &&
124  acc == other.acc;
125  }
126  };
127 
128 
129 
134  template<class Graph>
135  class SPOT_API twa_graph_succ_iterator final:
136  public twa_succ_iterator
137  {
138  private:
139  typedef typename Graph::edge edge;
140  typedef typename Graph::state_data_t state;
141  const Graph* g_;
142  edge t_;
143  edge p_;
144 
145  public:
146  twa_graph_succ_iterator(const Graph* g, edge t)
147  : g_(g), t_(t)
148  {
149  }
150 
151  void recycle(edge t)
152  {
153  t_ = t;
154  }
155 
156  virtual bool first() override
157  {
158  p_ = t_;
159  return p_;
160  }
161 
162  virtual bool next() override
163  {
164  p_ = g_->edge_storage(p_).next_succ;
165  return p_;
166  }
167 
168  virtual bool done() const override
169  {
170  return !p_;
171  }
172 
173  virtual const twa_graph_state* dst() const override
174  {
175  SPOT_ASSERT(!done());
176  return &g_->state_data(g_->edge_storage(p_).dst);
177  }
178 
179  virtual bdd cond() const override
180  {
181  SPOT_ASSERT(!done());
182  return g_->edge_data(p_).cond;
183  }
184 
185  virtual acc_cond::mark_t acc() const override
186  {
187  SPOT_ASSERT(!done());
188  return g_->edge_data(p_).acc;
189  }
190 
191  edge pos() const
192  {
193  return p_;
194  }
195 
196  };
197 
200  class SPOT_API twa_graph final: public twa
201  {
202  public:
204  // We avoid using graph_t::edge_storage_t because graph_t is not
205  // instantiated in the SWIG bindings, and SWIG would therefore
206  // handle graph_t::edge_storage_t as an abstract type.
207  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
209  <twa_graph_edge_data, false>>
211  static_assert(std::is_same<typename graph_t::edge_storage_t,
212  edge_storage_t>::value, "type mismatch");
213  // We avoid using graph_t::state for the very same reason.
214  typedef unsigned state_num;
215  static_assert(std::is_same<typename graph_t::state, state_num>::value,
216  "type mismatch");
217 
218  protected:
219  graph_t g_;
220  mutable unsigned init_number_;
221 
222  public:
223 
224  void apply_permutation(std::vector<unsigned> permut);
225 
226  twa_graph(const bdd_dict_ptr& dict)
227  : twa(dict),
228  init_number_(0)
229  {
230  }
231 
232  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
233  : twa(other->get_dict()),
234  g_(other->g_), init_number_(other->init_number_)
235  {
236  copy_acceptance_of(other);
237  copy_ap_of(other);
238  prop_copy(other, p);
239  }
240 
241  virtual ~twa_graph()
242  {
243  }
244 
245 #ifndef SWIG
246  template <typename State_Name,
247  typename Name_Hash = std::hash<State_Name>,
248  typename Name_Equal = std::equal_to<State_Name>>
250 
251  template <typename State_Name,
252  typename Name_Hash = std::hash<State_Name>,
253  typename Name_Equal = std::equal_to<State_Name>>
255  create_namer()
256  {
258  }
259 
261  create_formula_namer()
262  {
263  return create_namer<formula>();
264  }
265 
266  void
267  release_formula_namer(namer<formula>* namer, bool keep_names);
268 #endif
269 
270  graph_t& get_graph()
271  {
272  return g_;
273  }
274 
275  const graph_t& get_graph() const
276  {
277  return g_;
278  }
279 
280  unsigned num_states() const
281  {
282  return g_.num_states();
283  }
284 
285  unsigned num_edges() const
286  {
287  return g_.num_edges();
288  }
289 
290  void set_init_state(state_num s)
291  {
292  bool univ = is_univ_dest(s);
293  if (SPOT_UNLIKELY((!univ && s >= num_states())
294  // univ destinations have at least length 2.
295  || (univ && 2 + ~s >= g_.dests_vector().size())))
296  throw std::invalid_argument
297  ("set_init_state() called with nonexisting state");
298  init_number_ = s;
299  }
300 
301  template<class I>
302  void set_univ_init_state(I dst_begin, I dst_end)
303  {
304  auto ns = num_states();
305  for (I i = dst_begin; i != dst_end; ++i)
306  if (SPOT_UNLIKELY(*i >= ns))
307  throw std::invalid_argument
308  ("set_univ_init_state() called with nonexisting state");
309  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
310  }
311 
312  void set_univ_init_state(const std::initializer_list<state_num>& il)
313  {
314  set_univ_init_state(il.begin(), il.end());
315  }
316 
317  state_num get_init_state_number() const
318  {
319  // If the automaton has no state, it has no initial state.
320  if (num_states() == 0)
321  throw std::runtime_error("automaton has no state at all");
322  return init_number_;
323  }
324 
325  virtual const twa_graph_state* get_init_state() const override
326  {
327  unsigned n = get_init_state_number();
328  if (SPOT_UNLIKELY(!is_existential()))
329  throw std::runtime_error
330  ("the abstract interface does not support alternating automata");
331  return state_from_number(n);
332  }
333 
334  virtual twa_succ_iterator*
335  succ_iter(const state* st) const override
336  {
337  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
338  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
339 
340  if (this->iter_cache_)
341  {
342  auto it =
343  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
344  it->recycle(s->succ);
345  this->iter_cache_ = nullptr;
346  return it;
347  }
348  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
349  }
350 
351  static constexpr bool is_univ_dest(const edge_storage_t& e)
352  {
353  return is_univ_dest(e.dst);
354  }
355 
356  static constexpr bool is_univ_dest(unsigned s)
357  {
358  // Universal destinations are stored with their most-significant
359  // bit set.
360  return (int) s < 0;
361  }
362 
363  state_num
364  state_number(const state* st) const
365  {
366  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
367  return s - &g_.state_storage(0);
368  }
369 
370  const twa_graph_state*
371  state_from_number(state_num n) const
372  {
373  return &g_.state_data(n);
374  }
375 
376  std::string format_state(unsigned n) const;
377 
378  virtual std::string format_state(const state* st) const override
379  {
380  return format_state(state_number(st));
381  }
382 
383  unsigned edge_number(const twa_succ_iterator* it) const
384  {
385  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
386  return i->pos();
387  }
388 
389  unsigned edge_number(const edge_storage_t& e) const
390  {
391  return g_.index_of_edge(e);
392  }
393 
394  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
395  {
396  return g_.edge_data(edge_number(it));
397  }
398 
399  twa_graph_edge_data& edge_data(unsigned t)
400  {
401  return g_.edge_data(t);
402  }
403 
404  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
405  {
406  return g_.edge_data(edge_number(it));
407  }
408 
409  const twa_graph_edge_data& edge_data(unsigned t) const
410  {
411  return g_.edge_data(t);
412  }
413 
414  edge_storage_t& edge_storage(const twa_succ_iterator* it)
415  {
416  return g_.edge_storage(edge_number(it));
417  }
418 
419  edge_storage_t& edge_storage(unsigned t)
420  {
421  return g_.edge_storage(t);
422  }
423 
424  const edge_storage_t
425  edge_storage(const twa_succ_iterator* it) const
426  {
427  return g_.edge_storage(edge_number(it));
428  }
429 
430  const edge_storage_t edge_storage(unsigned t) const
431  {
432  return g_.edge_storage(t);
433  }
434 
435  unsigned new_state()
436  {
437  return g_.new_state();
438  }
439 
440  unsigned new_states(unsigned n)
441  {
442  return g_.new_states(n);
443  }
444 
445  unsigned new_edge(unsigned src, unsigned dst,
446  bdd cond,
447  acc_cond::mark_t acc = {})
448  {
449  return g_.new_edge(src, dst, cond, acc);
450  }
451 
452  unsigned new_acc_edge(unsigned src, unsigned dst,
453  bdd cond, bool acc = true)
454  {
455  if (acc)
456  return g_.new_edge(src, dst, cond, this->acc().all_sets());
457  else
458  return g_.new_edge(src, dst, cond);
459  }
460 
461  template<class I>
462  unsigned new_univ_edge(unsigned src, I begin, I end,
463  bdd cond,
464  acc_cond::mark_t acc = {})
465  {
466  return g_.new_univ_edge(src, begin, end, cond, acc);
467  }
468 
469  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
470  bdd cond,
471  acc_cond::mark_t acc = {})
472  {
473  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
474  }
475 
476 #ifndef SWIG
478  out(unsigned src) const
479  {
480  return g_.out(src);
481  }
482 #endif
483 
485  out(unsigned src)
486  {
487  return g_.out(src);
488  }
489 
491  out_iteraser(unsigned src)
492  {
493  return g_.out_iteraser(src);
494  }
495 
497  univ_dests(unsigned d) const noexcept
498  {
499  return g_.univ_dests(d);
500  }
501 
503  univ_dests(const edge_storage_t& e) const noexcept
504  {
505  return g_.univ_dests(e);
506  }
507 
509  bool is_existential() const
510  {
511  return g_.is_existential();
512  }
513 
514 #ifndef SWIG
515  auto states() const
516  SPOT_RETURN(g_.states());
517  auto states()
518  SPOT_RETURN(g_.states());
519 
521  edges() const noexcept
522  {
523  return g_.edges();
524  }
525 #endif
526 
528  edges() noexcept
529  {
530  return g_.edges();
531  }
532 
533 #ifndef SWIG
534  auto edge_vector() const
535  SPOT_RETURN(g_.edge_vector());
536  auto edge_vector()
537  SPOT_RETURN(g_.edge_vector());
538 #endif
539 
540  bool is_dead_edge(unsigned t) const
541  {
542  return g_.is_dead_edge(t);
543  }
544 
545  bool is_dead_edge(const graph_t::edge_storage_t& t) const
546  {
547  return g_.is_dead_edge(t);
548  }
549 
566  void merge_edges();
567 
571  void merge_univ_dests();
572 
579  void merge_states();
580 
596  void purge_dead_states();
597 
615  typedef void (*shift_action)(const std::vector<unsigned>& newst,
616  void* action_data);
617  void purge_unreachable_states(shift_action* f = nullptr,
618  void* action_data = nullptr);
619 
624  void remove_unused_ap();
625 
632  void copy_state_names_from(const const_twa_graph_ptr& other);
633 
637  {
638  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
639  throw std::runtime_error
640  ("state_acc_sets() should only be called on "
641  "automata with state-based acceptance");
642  for (auto& t: g_.out(s))
643  // Stop at the first edge, since the remaining should be
644  // labeled identically.
645  return t.acc;
646  return {};
647  }
648 
655  bool state_is_accepting(unsigned s) const
656  {
657  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
658  throw std::runtime_error
659  ("state_is_accepting() should only be called on "
660  "automata with state-based acceptance");
661  for (auto& t: g_.out(s))
662  // Stop at the first edge, since the remaining should be
663  // labeled identically.
664  return acc().accepting(t.acc);
665  return false;
666  }
667 
668  bool state_is_accepting(const state* s) const
669  {
670  return state_is_accepting(state_number(s));
671  }
673 
674  bool operator==(const twa_graph& aut) const
675  {
676  auto& dests1 = g_.dests_vector();
677  auto& dests2 = aut.get_graph().dests_vector();
678  if (num_states() != aut.num_states() ||
679  num_edges() != aut.num_edges() ||
680  num_sets() != aut.num_sets() ||
681  dests1.size() != dests2.size())
682  return false;
683  auto& trans1 = edge_vector();
684  auto& trans2 = aut.edge_vector();
685  if (!std::equal(trans1.begin() + 1, trans1.end(),
686  trans2.begin() + 1))
687  return false;
688  return std::equal(dests1.begin(), dests1.end(),
689  dests2.begin());
690  }
691 
701  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
702 
708  void dump_storage_as_dot(std::ostream& out,
709  const char* opt = nullptr) const;
710  };
711 
712  // This is a workaround for
713 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
714 # define SPOT_make_twa_graph__(...) \
715  std::shared_ptr<twa_graph>(new twa_graph(__VA_ARGS__))
716 #else
717 # define SPOT_make_twa_graph__(...) \
718  std::make_shared<twa_graph>(__VA_ARGS__)
719 #endif
720 
723  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
724  {
725  return SPOT_make_shared_enabled__(twa_graph, dict);
726  }
727 
730  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
731  twa::prop_set p)
732  {
733  return SPOT_make_shared_enabled__(twa_graph, aut, p);
734  }
735 
738  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
739  twa::prop_set p)
740  {
741  return SPOT_make_shared_enabled__(twa_graph, aut, p);
742  }
743 
753  SPOT_API twa_graph_ptr
754  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
755  bool preserve_names = false,
756  // parentheses for SWIG, see
757  // https://github.com/swig/swig/issues/993
758  unsigned max_states = -(1U));
759 }
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:684
Definition: automata.hh:26
Definition: graph.hh:61
virtual void destroy() const override
Release a state.
Definition: twagraph.hh:83
bool state_is_accepting(const state *s) const
Tell if a state is accepting.
Definition: twagraph.hh:668
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:997
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:670
A Transition-based ω-Automaton.
Definition: twa.hh:622
LTL/PSL formula interface.
Abstract class for states.
Definition: twa.hh:50
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: graph.hh:659
virtual size_t hash() const override
Hash a state.
Definition: twagraph.hh:71
Definition: graph.hh:183
Definition: ngraph.hh:32
internal::killer_edge_iterator< digraph > out_iteraser(state_storage_t &src)
Return a fake container with all edges leaving src, allowing erasure.
Definition: graph.hh:903
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:735
virtual std::string format_state(const state *st) const override
Format the state as a string for printing.
Definition: twagraph.hh:378
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:772
const state_vector & states() const
Return the vector of states.
Definition: graph.hh:918
virtual const twa_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: twagraph.hh:325
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:717
Definition: graph.hh:158
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:699
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: twagraph.hh:156
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:933
Iterate over the successors of a state.
Definition: twa.hh:397
bool is_dead_edge(unsigned t) const
Tests whether an edge has been erased.
Definition: graph.hh:981
edge_storage_t::data_t & edge_data(edge s)
return the Edgeg_Data of an edge.
Definition: graph.hh:753
Definition: graph.hh:495
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: twagraph.hh:179
acc_cond::mark_t state_acc_sets(unsigned s) const
Return the marks associated to a state if the acceptance is state-based.
Definition: twagraph.hh:636
twa_graph_ptr make_twa_graph(const const_twa_ptr &aut, twa::prop_set p, bool preserve_names=false, unsigned max_states=-(1U))
Build an explicit automaton from all states of aut,.
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:645
const edge_vector_t & edge_vector() const
Return the vector of all edges.
Definition: graph.hh:952
virtual bool done() const override
Check whether the iteration is finished.
Definition: twagraph.hh:168
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:59
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:173
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:653
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:969
edge index_of_edge(const edge_storage_t &tt) const
Convert a storage reference into an edge number.
Definition: graph.hh:865
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: twagraph.hh:185
bool state_is_accepting(unsigned s) const
Tell if a state is accepting.
Definition: twagraph.hh:655
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:874
virtual twa_succ_iterator * succ_iter(const state *st) const override
Get an iterator over the successors of local_state.
Definition: twagraph.hh:335
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:135
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:796
Definition: graph.hh:384
Graph-based representation of a TωA.
Definition: twagraph.hh:200
Graph-based representation of a TωA.
Definition: twagraph.hh:39
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:816
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:934
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:162
An acceptance mark.
Definition: acc.hh:87
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:509
Data attached to edges of a twa_graph.
Definition: twagraph.hh:95
Definition: graph.hh:315
virtual twa_graph_state * clone() const override
Duplicate a state.
Definition: twagraph.hh:78

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.8.13