spot  2.11.5
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2022 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 reinterpret_cast<size_t>(this);
74  }
75 
76  virtual twa_graph_state*
77  clone() const override
78  {
79  return const_cast<twa_graph_state*>(this);
80  }
81 
82  virtual void destroy() const override
83  {
84  }
85  };
86 
94  struct SPOT_API twa_graph_edge_data
95  {
96  bdd cond;
97  acc_cond::mark_t acc;
98 
99  explicit twa_graph_edge_data() noexcept
100  : cond(bddfalse), acc({})
101  {
102  }
103 
105  bdd cond,
106  acc_cond::mark_t acc = {}) noexcept
107  : cond(cond), acc(acc)
108  {
109  }
110 
111  bool operator<(const twa_graph_edge_data& other) const
112  {
113  if (cond.id() < other.cond.id())
114  return true;
115  if (cond.id() > other.cond.id())
116  return false;
117  return acc < other.acc;
118  }
119 
120  bool operator==(const twa_graph_edge_data& other) const
121  {
122  return cond.id() == other.cond.id() &&
123  acc == other.acc;
124  }
125  };
126 
127 
128 
133  template<class Graph>
134  class SPOT_API twa_graph_succ_iterator final:
135  public twa_succ_iterator
136  {
137  private:
138  typedef typename Graph::edge edge;
139  typedef typename Graph::state_data_t state;
140  const Graph* g_;
141  edge t_;
142  edge p_;
143 
144  public:
145  twa_graph_succ_iterator(const Graph* g, edge t)
146  : g_(g), t_(t)
147  {
148  }
149 
150  void recycle(edge t)
151  {
152  t_ = t;
153  }
154 
155  virtual bool first() override
156  {
157  p_ = t_;
158  return p_;
159  }
160 
161  virtual bool next() override
162  {
163  p_ = g_->edge_storage(p_).next_succ;
164  return p_;
165  }
166 
167  virtual bool done() const override
168  {
169  return !p_;
170  }
171 
172  virtual const twa_graph_state* dst() const override
173  {
174  SPOT_ASSERT(!done());
175  return &g_->state_data(g_->edge_storage(p_).dst);
176  }
177 
178  virtual bdd cond() const override
179  {
180  SPOT_ASSERT(!done());
181  return g_->edge_data(p_).cond;
182  }
183 
184  virtual acc_cond::mark_t acc() const override
185  {
186  SPOT_ASSERT(!done());
187  return g_->edge_data(p_).acc;
188  }
189 
190  edge pos() const
191  {
192  return p_;
193  }
194 
195  };
196 
199  class SPOT_API twa_graph final: public twa
200  {
201  public:
203  // We avoid using graph_t::edge_storage_t because graph_t is not
204  // instantiated in the SWIG bindings, and SWIG would therefore
205  // handle graph_t::edge_storage_t as an abstract type.
206  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
208  <twa_graph_edge_data, false>>
210  static_assert(std::is_same<typename graph_t::edge_storage_t,
211  edge_storage_t>::value, "type mismatch");
212  // We avoid using graph_t::state for the very same reason.
213  typedef unsigned state_num;
214  static_assert(std::is_same<typename graph_t::state, state_num>::value,
215  "type mismatch");
216 
217  protected:
218  graph_t g_;
219  mutable unsigned init_number_;
220 
221  public:
222 
223  void apply_permutation(std::vector<unsigned> permut);
224 
225  twa_graph(const bdd_dict_ptr& dict)
226  : twa(dict),
227  init_number_(0)
228  {
229  }
230 
231  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
232  : twa(other->get_dict()),
233  g_(other->g_), init_number_(other->init_number_)
234  {
235  copy_acceptance_of(other);
236  copy_ap_of(other);
237  prop_copy(other, p);
238  }
239 
240  virtual ~twa_graph()
241  {
242  }
243 
244 #ifndef SWIG
245  template <typename State_Name,
246  typename Name_Hash = std::hash<State_Name>,
247  typename Name_Equal = std::equal_to<State_Name>>
249 
250  template <typename State_Name,
251  typename Name_Hash = std::hash<State_Name>,
252  typename Name_Equal = std::equal_to<State_Name>>
254  create_namer()
255  {
257  }
258 
260  create_formula_namer()
261  {
262  return create_namer<formula>();
263  }
264 
265  void
266  release_formula_namer(namer<formula>* namer, bool keep_names);
267 #endif
268 
269  graph_t& get_graph()
270  {
271  return g_;
272  }
273 
274  const graph_t& get_graph() const
275  {
276  return g_;
277  }
278 
279  unsigned num_states() const
280  {
281  return g_.num_states();
282  }
283 
284  unsigned num_edges() const
285  {
286  return g_.num_edges();
287  }
288 
289  void set_init_state(state_num s)
290  {
291  bool univ = is_univ_dest(s);
292  if (SPOT_UNLIKELY((!univ && s >= num_states())
293  // univ destinations have at least length 2.
294  || (univ && 2 + ~s >= g_.dests_vector().size())))
295  throw std::invalid_argument
296  ("set_init_state() called with nonexisting state");
297  init_number_ = s;
298  }
299 
300  template<class I>
301  void set_univ_init_state(I dst_begin, I dst_end)
302  {
303  auto ns = num_states();
304  for (I i = dst_begin; i != dst_end; ++i)
305  if (SPOT_UNLIKELY(*i >= ns))
306  throw std::invalid_argument
307  ("set_univ_init_state() called with nonexisting state");
308  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
309  }
310 
311  void set_univ_init_state(const std::initializer_list<state_num>& il)
312  {
313  set_univ_init_state(il.begin(), il.end());
314  }
315 
316  state_num get_init_state_number() const
317  {
318  // If the automaton has no state, it has no initial state.
319  if (num_states() == 0)
320  throw std::runtime_error("automaton has no state at all");
321  return init_number_;
322  }
323 
324  virtual const twa_graph_state* get_init_state() const override
325  {
326  unsigned n = get_init_state_number();
327  if (SPOT_UNLIKELY(!is_existential()))
328  throw std::runtime_error
329  ("the abstract interface does not support alternating automata");
330  return state_from_number(n);
331  }
332 
333  virtual twa_succ_iterator*
334  succ_iter(const state* st) const override
335  {
336  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
337  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
338 
339  if (this->iter_cache_)
340  {
341  auto it =
342  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
343  it->recycle(s->succ);
344  this->iter_cache_ = nullptr;
345  return it;
346  }
347  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
348  }
349 
350  static constexpr bool is_univ_dest(const edge_storage_t& e)
351  {
352  return is_univ_dest(e.dst);
353  }
354 
355  static constexpr bool is_univ_dest(unsigned s)
356  {
357  // Universal destinations are stored with their most-significant
358  // bit set.
359  return (int) s < 0;
360  }
361 
362  state_num
363  state_number(const state* st) const
364  {
365  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
366  return s - &g_.state_storage(0);
367  }
368 
369  const twa_graph_state*
370  state_from_number(state_num n) const
371  {
372  return &g_.state_data(n);
373  }
374 
375  std::string format_state(unsigned n) const;
376 
377  virtual std::string format_state(const state* st) const override
378  {
379  return format_state(state_number(st));
380  }
381 
382  unsigned edge_number(const twa_succ_iterator* it) const
383  {
384  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
385  return i->pos();
386  }
387 
388  unsigned edge_number(const edge_storage_t& e) const
389  {
390  return g_.index_of_edge(e);
391  }
392 
393  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
394  {
395  return g_.edge_data(edge_number(it));
396  }
397 
398  twa_graph_edge_data& edge_data(unsigned t)
399  {
400  return g_.edge_data(t);
401  }
402 
403  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
404  {
405  return g_.edge_data(edge_number(it));
406  }
407 
408  const twa_graph_edge_data& edge_data(unsigned t) const
409  {
410  return g_.edge_data(t);
411  }
412 
413  edge_storage_t& edge_storage(const twa_succ_iterator* it)
414  {
415  return g_.edge_storage(edge_number(it));
416  }
417 
418  edge_storage_t& edge_storage(unsigned t)
419  {
420  return g_.edge_storage(t);
421  }
422 
423  const edge_storage_t
424  edge_storage(const twa_succ_iterator* it) const
425  {
426  return g_.edge_storage(edge_number(it));
427  }
428 
429  const edge_storage_t edge_storage(unsigned t) const
430  {
431  return g_.edge_storage(t);
432  }
433 
434  unsigned new_state()
435  {
436  return g_.new_state();
437  }
438 
439  unsigned new_states(unsigned n)
440  {
441  return g_.new_states(n);
442  }
443 
444  unsigned new_edge(unsigned src, unsigned dst,
445  bdd cond,
446  acc_cond::mark_t acc = {})
447  {
448  return g_.new_edge(src, dst, cond, acc);
449  }
450 
451  unsigned new_acc_edge(unsigned src, unsigned dst,
452  bdd cond, bool acc = true)
453  {
454  if (acc)
455  return g_.new_edge(src, dst, cond, this->acc().all_sets());
456  else
457  return g_.new_edge(src, dst, cond);
458  }
459 
460  template<class I>
461  unsigned new_univ_edge(unsigned src, I begin, I end,
462  bdd cond,
463  acc_cond::mark_t acc = {})
464  {
465  return g_.new_univ_edge(src, begin, end, cond, acc);
466  }
467 
468  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
469  bdd cond,
470  acc_cond::mark_t acc = {})
471  {
472  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
473  }
474 
475 #ifndef SWIG
476  internal::state_out<const graph_t>
477  out(unsigned src) const
478  {
479  return g_.out(src);
480  }
481 #endif
482 
483  internal::state_out<graph_t>
484  out(unsigned src)
485  {
486  return g_.out(src);
487  }
488 
489  internal::killer_edge_iterator<graph_t>
490  out_iteraser(unsigned src)
491  {
492  return g_.out_iteraser(src);
493  }
494 
495  internal::const_universal_dests
496  univ_dests(unsigned d) const noexcept
497  {
498  return g_.univ_dests(d);
499  }
500 
501  internal::const_universal_dests
502  univ_dests(const edge_storage_t& e) const noexcept
503  {
504  return g_.univ_dests(e);
505  }
506 
508  bool is_existential() const
509  {
510  return g_.is_existential();
511  }
512 
513 #ifndef SWIG
514  auto states() const
515  SPOT_RETURN(g_.states());
516  auto states()
517  SPOT_RETURN(g_.states());
518 
519  internal::all_trans<const graph_t>
520  edges() const noexcept
521  {
522  return g_.edges();
523  }
524 #endif
525 
526  internal::all_trans<graph_t>
527  edges() noexcept
528  {
529  return g_.edges();
530  }
531 
532 #ifndef SWIG
533  auto edge_vector() const
534  SPOT_RETURN(g_.edge_vector());
535  auto edge_vector()
536  SPOT_RETURN(g_.edge_vector());
537 #endif
538 
539  bool is_dead_edge(unsigned t) const
540  {
541  return g_.is_dead_edge(t);
542  }
543 
544  bool is_dead_edge(const graph_t::edge_storage_t& t) const
545  {
546  return g_.is_dead_edge(t);
547  }
548 
565  void merge_edges();
566 
571 
598 
609  unsigned merge_states_of(bool stable = true,
610  const std::vector<bool>* to_merge_ptr = nullptr);
611 
629 
647  typedef void (*shift_action)(const std::vector<unsigned>& newst,
648  void* action_data);
649  void purge_unreachable_states(shift_action* f = nullptr,
650  void* action_data = nullptr);
651 
657 
664  void copy_state_names_from(const const_twa_graph_ptr& other);
665 
669  {
670  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
671  throw std::runtime_error
672  ("state_acc_sets() should only be called on "
673  "automata with state-based acceptance");
674  for (auto& t: g_.out(s))
675  // Stop at the first edge, since the remaining should be
676  // labeled identically.
677  return t.acc;
678  return {};
679  }
680 
687  bool state_is_accepting(unsigned s) const
688  {
689  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
690  throw std::runtime_error
691  ("state_is_accepting() should only be called on "
692  "automata with state-based acceptance");
693  for (auto& t: g_.out(s))
694  // Stop at the first edge, since the remaining should be
695  // labeled identically.
696  return acc().accepting(t.acc);
697  return false;
698  }
699 
700  bool state_is_accepting(const state* s) const
701  {
702  return state_is_accepting(state_number(s));
703  }
705 
706  bool operator==(const twa_graph& aut) const
707  {
708  auto& dests1 = g_.dests_vector();
709  auto& dests2 = aut.get_graph().dests_vector();
710  if (num_states() != aut.num_states() ||
711  num_edges() != aut.num_edges() ||
712  num_sets() != aut.num_sets() ||
713  dests1.size() != dests2.size())
714  return false;
715  auto& trans1 = edge_vector();
716  auto& trans2 = aut.edge_vector();
717  if (!std::equal(trans1.begin() + 1, trans1.end(),
718  trans2.begin() + 1))
719  return false;
720  return std::equal(dests1.begin(), dests1.end(),
721  dests2.begin());
722  }
723 
724 #ifndef SWIG
748  void defrag_states(std::vector<unsigned>& newst,
749  unsigned used_states);
750 
751  // prototype was changed in Spot 2.10
752  SPOT_DEPRECATED("use reference version of this method")
753  void defrag_states(std::vector<unsigned>&& newst,
754  unsigned used_states)
755  {
756  return defrag_states(newst, used_states);
757  }
759 #endif // SWIG
760 
768  void kill_state(unsigned state);
769 
775  void dump_storage_as_dot(std::ostream& out,
776  const char* opt = nullptr) const;
777  };
778 
779  // This is a workaround for
780 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
781 # define SPOT_make_twa_graph__(...) \
782  std::shared_ptr<twa_graph>(new twa_graph(__VA_ARGS__))
783 #else
784 # define SPOT_make_twa_graph__(...) \
785  std::make_shared<twa_graph>(__VA_ARGS__)
786 #endif
787 
790  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
791  {
792  return SPOT_make_shared_enabled__(twa_graph, dict);
793  }
794 
797  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
798  twa::prop_set p)
799  {
800  return SPOT_make_shared_enabled__(twa_graph, aut, p);
801  }
802 
809  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
810  twa::prop_set p,
811  bool preserve_name_properties = false)
812  {
813  twa_graph_ptr res = SPOT_make_shared_enabled__(twa_graph, aut, p);
814  if (preserve_name_properties)
815  res->copy_named_properties_of(aut);
816  return res;
817  }
818 
828  SPOT_API twa_graph_ptr
829  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
830  bool preserve_names = false,
831  // parentheses for SWIG, see
832  // https://github.com/swig/swig/issues/993
833  unsigned max_states = -(1U));
834 }
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:649
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:922
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:688
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:674
edge index_of_edge(const edge_storage_t &tt) const
Convert a storage reference into an edge number.
Definition: graph.hh:884
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:988
edge_storage_t::data_t & edge_data(edge s)
return the Edgeg_Data of an edge.
Definition: graph.hh:757
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1016
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:739
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: graph.hh:663
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:893
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:800
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:703
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:657
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:721
bool is_dead_edge(unsigned t) const
Tests whether an edge has been erased.
Definition: graph.hh:1000
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:835
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:776
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:952
Definition: ngraph.hh:33
This class is used to tell parallel algorithms what resources they may use.
Definition: common.hh:156
Abstract class for states.
Definition: twa.hh:51
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:136
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:161
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: twagraph.hh:178
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: twagraph.hh:184
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: twagraph.hh:155
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:172
virtual bool done() const override
Check whether the iteration is finished.
Definition: twagraph.hh:167
Graph-based representation of a TωA.
Definition: twagraph.hh:200
void copy_state_names_from(const const_twa_graph_ptr &other)
Define the state names of this automaton using the names from other.
void merge_univ_dests()
Merge common universal destinations.
virtual twa_succ_iterator * succ_iter(const state *st) const override
Get an iterator over the successors of local_state.
Definition: twagraph.hh:334
void merge_edges()
Merge edges that can be merged.
void kill_state(unsigned state)
Make a state dead.
unsigned merge_states_of(bool stable=true, const std::vector< bool > *to_merge_ptr=nullptr)
Like merge states, but one can chose which states are candidates for merging.
bool state_is_accepting(unsigned s) const
Tell if a state is accepting.
Definition: twagraph.hh:687
void remove_unused_ap()
Remove unused atomic propositions.
void purge_dead_states()
Remove all dead states.
void defrag_states(std::vector< unsigned > &newst, unsigned used_states)
Renumber all states, and drop some.
bool state_is_accepting(const state *s) const
Tell if a state is accepting.
Definition: twagraph.hh:700
virtual const twa_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: twagraph.hh:324
unsigned merge_states(parallel_policy ppolicy=parallel_policy())
Merge states that can be merged.
virtual std::string format_state(const state *st) const override
Format the state as a string for printing.
Definition: twagraph.hh:377
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:668
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:508
void dump_storage_as_dot(std::ostream &out, const char *opt=nullptr) const
Print the data structures used to represent the automaton in dot's format.
Iterate over the successors of a state.
Definition: twa.hh:398
A Transition-based ω-Automaton.
Definition: twa.hh:623
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:934
LTL/PSL formula interface.
SPOT_DEPRECATED("use to_parity() instead") twa_graph_ptr iar(const const_twa_graph_ptr &aut
Turn a Rabin-like or Streett-like automaton into a parity automaton based on the index appearence rec...
twa_graph_ptr make_twa_graph(const bdd_dict_ptr &dict)
Build an explicit automaton from all states of aut,.
Definition: twagraph.hh:790
Definition: automata.hh:27
void prop_copy(const const_twa_ptr &other, prop_set p)
Copy the properties of another automaton.
Definition: twa.hh:1627
An acceptance mark.
Definition: acc.hh:90
Definition: graph.hh:66
Definition: graph.hh:188
Data attached to edges of a twa_graph.
Definition: twagraph.hh:95
Graph-based representation of a TωA.
Definition: twagraph.hh:40
virtual void destroy() const override
Release a state.
Definition: twagraph.hh:82
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:59
virtual size_t hash() const override
Hash a state.
Definition: twagraph.hh:71
virtual twa_graph_state * clone() const override
Duplicate a state.
Definition: twagraph.hh:77

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.9.1