spot  2.12.1
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <spot/twa/fwd.hh>
22 #include <spot/graph/graph.hh>
23 #include <spot/graph/ngraph.hh>
24 #include <spot/twa/bdddict.hh>
25 #include <spot/twa/twa.hh>
26 #include <spot/tl/formula.hh>
27 #include <sstream>
28 
29 namespace spot
30 {
31 
38  struct SPOT_API twa_graph_state: public spot::state
39  {
40  public:
41  twa_graph_state() noexcept
42  {
43  }
44 
45  twa_graph_state(const twa_graph_state&) noexcept
46  {
47  }
48 
49  twa_graph_state& operator=(const twa_graph_state&) noexcept
50  {
51  return *this;
52  }
53 
54  virtual ~twa_graph_state() noexcept
55  {
56  }
57 
58  virtual int compare(const spot::state* other) const override
59  {
60  auto o = down_cast<const twa_graph_state*>(other);
61 
62  // Do not simply return "other - this", it might not fit in an int.
63  if (o < this)
64  return -1;
65  if (o > this)
66  return 1;
67  return 0;
68  }
69 
70  virtual size_t hash() const override
71  {
72  return reinterpret_cast<size_t>(this);
73  }
74 
75  virtual twa_graph_state*
76  clone() const override
77  {
78  return const_cast<twa_graph_state*>(this);
79  }
80 
81  virtual void destroy() const override
82  {
83  }
84  };
85 
93  struct SPOT_API twa_graph_edge_data
94  {
95  bdd cond;
96  acc_cond::mark_t acc;
97 
98  explicit twa_graph_edge_data() noexcept
99  : cond(bddfalse), acc({})
100  {
101  }
102 
104  bdd cond,
105  acc_cond::mark_t acc = {}) noexcept
106  : cond(cond), acc(acc)
107  {
108  }
109 
110  bool operator<(const twa_graph_edge_data& other) const
111  {
112  if (cond.id() < other.cond.id())
113  return true;
114  if (cond.id() > other.cond.id())
115  return false;
116  return acc < other.acc;
117  }
118 
119  bool operator==(const twa_graph_edge_data& other) const
120  {
121  return cond.id() == other.cond.id() &&
122  acc == other.acc;
123  }
124  };
125 
126 
127 
132  template<class Graph>
133  class SPOT_API twa_graph_succ_iterator final:
134  public twa_succ_iterator
135  {
136  private:
137  typedef typename Graph::edge edge;
138  typedef typename Graph::state_data_t state;
139  const Graph* g_;
140  edge t_;
141  edge p_;
142 
143  public:
144  twa_graph_succ_iterator(const Graph* g, edge t)
145  : g_(g), t_(t)
146  {
147  }
148 
149  void recycle(edge t)
150  {
151  t_ = t;
152  }
153 
154  virtual bool first() override
155  {
156  p_ = t_;
157  return p_;
158  }
159 
160  virtual bool next() override
161  {
162  p_ = g_->edge_storage(p_).next_succ;
163  return p_;
164  }
165 
166  virtual bool done() const override
167  {
168  return !p_;
169  }
170 
171  virtual const twa_graph_state* dst() const override
172  {
173  SPOT_ASSERT(!done());
174  return &g_->state_data(g_->edge_storage(p_).dst);
175  }
176 
177  virtual bdd cond() const override
178  {
179  SPOT_ASSERT(!done());
180  return g_->edge_data(p_).cond;
181  }
182 
183  virtual acc_cond::mark_t acc() const override
184  {
185  SPOT_ASSERT(!done());
186  return g_->edge_data(p_).acc;
187  }
188 
189  edge pos() const
190  {
191  return p_;
192  }
193 
194  };
195 
198  class SPOT_API twa_graph final: public twa
199  {
200  public:
202  // We avoid using graph_t::edge_storage_t because graph_t is not
203  // instantiated in the SWIG bindings, and SWIG would therefore
204  // handle graph_t::edge_storage_t as an abstract type.
205  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
207  <twa_graph_edge_data, false>>
209  static_assert(std::is_same<typename graph_t::edge_storage_t,
210  edge_storage_t>::value, "type mismatch");
211  // We avoid using graph_t::state for the very same reason.
212  typedef unsigned state_num;
213  static_assert(std::is_same<typename graph_t::state, state_num>::value,
214  "type mismatch");
215 
216  protected:
217  graph_t g_;
218  mutable unsigned init_number_;
219 
220  public:
221 
222  twa_graph(const bdd_dict_ptr& dict)
223  : twa(dict),
224  init_number_(0)
225  {
226  }
227 
228  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
229  : twa(other->get_dict()),
230  g_(other->g_), init_number_(other->init_number_)
231  {
232  copy_acceptance_of(other);
233  copy_ap_of(other);
234  prop_copy(other, p);
235  }
236 
237  virtual ~twa_graph()
238  {
239  }
240 
241 #ifndef SWIG
242  template <typename State_Name,
243  typename Name_Hash = std::hash<State_Name>,
244  typename Name_Equal = std::equal_to<State_Name>>
246 
247  template <typename State_Name,
248  typename Name_Hash = std::hash<State_Name>,
249  typename Name_Equal = std::equal_to<State_Name>>
251  create_namer()
252  {
254  }
255 
257  create_formula_namer()
258  {
259  return create_namer<formula>();
260  }
261 
262  void
263  release_formula_namer(namer<formula>* namer, bool keep_names);
264 #endif
265 
266  graph_t& get_graph()
267  {
268  return g_;
269  }
270 
271  const graph_t& get_graph() const
272  {
273  return g_;
274  }
275 
276  unsigned num_states() const
277  {
278  return g_.num_states();
279  }
280 
281  unsigned num_edges() const
282  {
283  return g_.num_edges();
284  }
285 
286  void set_init_state(state_num s)
287  {
288  bool univ = is_univ_dest(s);
289  if (SPOT_UNLIKELY((!univ && s >= num_states())
290  // univ destinations have at least length 2.
291  || (univ && 2 + ~s >= g_.dests_vector().size())))
292  throw std::invalid_argument
293  ("set_init_state() called with nonexisting state");
294  init_number_ = s;
295  }
296 
297  template<class I>
298  void set_univ_init_state(I dst_begin, I dst_end)
299  {
300  auto ns = num_states();
301  for (I i = dst_begin; i != dst_end; ++i)
302  if (SPOT_UNLIKELY(*i >= ns))
303  throw std::invalid_argument
304  ("set_univ_init_state() called with nonexisting state");
305  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
306  }
307 
308  void set_univ_init_state(const std::initializer_list<state_num>& il)
309  {
310  set_univ_init_state(il.begin(), il.end());
311  }
312 
313  state_num get_init_state_number() const
314  {
315  // If the automaton has no state, it has no initial state.
316  if (num_states() == 0)
317  throw std::runtime_error("automaton has no state at all");
318  return init_number_;
319  }
320 
321  virtual const twa_graph_state* get_init_state() const override
322  {
323  unsigned n = get_init_state_number();
324  if (SPOT_UNLIKELY(!is_existential()))
325  throw std::runtime_error
326  ("the abstract interface does not support alternating automata");
327  return state_from_number(n);
328  }
329 
330  virtual twa_succ_iterator*
331  succ_iter(const state* st) const override
332  {
333  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
334  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
335 
336  if (this->iter_cache_)
337  {
338  auto it =
339  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
340  it->recycle(s->succ);
341  this->iter_cache_ = nullptr;
342  return it;
343  }
344  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
345  }
346 
347  static constexpr bool is_univ_dest(const edge_storage_t& e)
348  {
349  return is_univ_dest(e.dst);
350  }
351 
352  static constexpr bool is_univ_dest(unsigned s)
353  {
354  // Universal destinations are stored with their most-significant
355  // bit set.
356  return (int) s < 0;
357  }
358 
359  state_num
360  state_number(const state* st) const
361  {
362  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
363  return s - &g_.state_storage(0);
364  }
365 
366  const twa_graph_state*
367  state_from_number(state_num n) const
368  {
369  return &g_.state_data(n);
370  }
371 
372  std::string format_state(unsigned n) const;
373 
374  virtual std::string format_state(const state* st) const override
375  {
376  return format_state(state_number(st));
377  }
378 
379  unsigned edge_number(const twa_succ_iterator* it) const
380  {
381  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
382  return i->pos();
383  }
384 
385  unsigned edge_number(const edge_storage_t& e) const
386  {
387  return g_.index_of_edge(e);
388  }
389 
390  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
391  {
392  return g_.edge_data(edge_number(it));
393  }
394 
395  twa_graph_edge_data& edge_data(unsigned t)
396  {
397  return g_.edge_data(t);
398  }
399 
400  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
401  {
402  return g_.edge_data(edge_number(it));
403  }
404 
405  const twa_graph_edge_data& edge_data(unsigned t) const
406  {
407  return g_.edge_data(t);
408  }
409 
410  edge_storage_t& edge_storage(const twa_succ_iterator* it)
411  {
412  return g_.edge_storage(edge_number(it));
413  }
414 
415  edge_storage_t& edge_storage(unsigned t)
416  {
417  return g_.edge_storage(t);
418  }
419 
420  const edge_storage_t
421  edge_storage(const twa_succ_iterator* it) const
422  {
423  return g_.edge_storage(edge_number(it));
424  }
425 
426  const edge_storage_t edge_storage(unsigned t) const
427  {
428  return g_.edge_storage(t);
429  }
430 
431  unsigned new_state()
432  {
433  return g_.new_state();
434  }
435 
436  unsigned new_states(unsigned n)
437  {
438  return g_.new_states(n);
439  }
440 
441  unsigned new_edge(unsigned src, unsigned dst,
442  bdd cond,
443  acc_cond::mark_t acc = {})
444  {
445  return g_.new_edge(src, dst, cond, acc);
446  }
447 
448  unsigned new_acc_edge(unsigned src, unsigned dst,
449  bdd cond, bool acc = true)
450  {
451  if (acc)
452  return g_.new_edge(src, dst, cond, this->acc().all_sets());
453  else
454  return g_.new_edge(src, dst, cond);
455  }
456 
457  template<class I>
458  unsigned new_univ_edge(unsigned src, I begin, I end,
459  bdd cond,
460  acc_cond::mark_t acc = {})
461  {
462  return g_.new_univ_edge(src, begin, end, cond, acc);
463  }
464 
465  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
466  bdd cond,
467  acc_cond::mark_t acc = {})
468  {
469  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
470  }
471 
472 #ifndef SWIG
473  internal::state_out<const graph_t>
474  out(unsigned src) const
475  {
476  return g_.out(src);
477  }
478 #endif
479 
480  internal::state_out<graph_t>
481  out(unsigned src)
482  {
483  return g_.out(src);
484  }
485 
486  internal::killer_edge_iterator<graph_t>
487  out_iteraser(unsigned src)
488  {
489  return g_.out_iteraser(src);
490  }
491 
492  internal::const_universal_dests
493  univ_dests(unsigned d) const noexcept
494  {
495  return g_.univ_dests(d);
496  }
497 
498  internal::const_universal_dests
499  univ_dests(const edge_storage_t& e) const noexcept
500  {
501  return g_.univ_dests(e);
502  }
503 
505  bool is_existential() const
506  {
507  return g_.is_existential();
508  }
509 
510 #ifndef SWIG
511  auto states() const
512  SPOT_RETURN(g_.states());
513  auto states()
514  SPOT_RETURN(g_.states());
515 
516  internal::all_trans<const graph_t>
517  edges() const noexcept
518  {
519  return g_.edges();
520  }
521 #endif
522 
523  internal::all_trans<graph_t>
524  edges() noexcept
525  {
526  return g_.edges();
527  }
528 
529 #ifndef SWIG
530  auto edge_vector() const
531  SPOT_RETURN(g_.edge_vector());
532  auto edge_vector()
533  SPOT_RETURN(g_.edge_vector());
534 #endif
535 
536  bool is_dead_edge(unsigned t) const
537  {
538  return g_.is_dead_edge(t);
539  }
540 
541  bool is_dead_edge(const graph_t::edge_storage_t& t) const
542  {
543  return g_.is_dead_edge(t);
544  }
545 
562  void merge_edges();
563 
568 
595 
606  unsigned merge_states_of(bool stable = true,
607  const std::vector<bool>* to_merge_ptr = nullptr);
608 
626 
644  typedef void (*shift_action)(const std::vector<unsigned>& newst,
645  void* action_data);
646  void purge_unreachable_states(shift_action* f = nullptr,
647  void* action_data = nullptr);
648 
654 
661  void copy_state_names_from(const const_twa_graph_ptr& other);
662 
666  {
667  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
668  throw std::runtime_error
669  ("state_acc_sets() should only be called on "
670  "automata with state-based acceptance");
671  for (auto& t: g_.out(s))
672  // Stop at the first edge, since the remaining should be
673  // labeled identically.
674  return t.acc;
675  return {};
676  }
677 
684  bool state_is_accepting(unsigned s) const
685  {
686  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
687  throw std::runtime_error
688  ("state_is_accepting() should only be called on "
689  "automata with state-based acceptance");
690  for (auto& t: g_.out(s))
691  // Stop at the first edge, since the remaining should be
692  // labeled identically.
693  return acc().accepting(t.acc);
694  return false;
695  }
696 
697  bool state_is_accepting(const state* s) const
698  {
699  return state_is_accepting(state_number(s));
700  }
702 
703  bool operator==(const twa_graph& aut) const
704  {
705  auto& dests1 = g_.dests_vector();
706  auto& dests2 = aut.get_graph().dests_vector();
707  if (num_states() != aut.num_states() ||
708  num_edges() != aut.num_edges() ||
709  num_sets() != aut.num_sets() ||
710  dests1.size() != dests2.size())
711  return false;
712  auto& trans1 = edge_vector();
713  auto& trans2 = aut.edge_vector();
714  if (!std::equal(trans1.begin() + 1, trans1.end(),
715  trans2.begin() + 1))
716  return false;
717  return std::equal(dests1.begin(), dests1.end(),
718  dests2.begin());
719  }
720 
721 #ifndef SWIG
745  void defrag_states(std::vector<unsigned>& newst,
746  unsigned used_states);
747 
748  // prototype was changed in Spot 2.10
749  SPOT_DEPRECATED("use reference version of this method")
750  void defrag_states(std::vector<unsigned>&& newst,
751  unsigned used_states)
752  {
753  return defrag_states(newst, used_states);
754  }
756 #endif // SWIG
757 
765  void kill_state(unsigned state);
766 
772  void dump_storage_as_dot(std::ostream& out,
773  const char* opt = nullptr) const;
774  };
775 
776  // This is a workaround for
777 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
778 # define SPOT_make_twa_graph__(...) \
779  std::shared_ptr<twa_graph>(new twa_graph(__VA_ARGS__))
780 #else
781 # define SPOT_make_twa_graph__(...) \
782  std::make_shared<twa_graph>(__VA_ARGS__)
783 #endif
784 
787  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
788  {
789  return SPOT_make_shared_enabled__(twa_graph, dict);
790  }
791 
794  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
795  twa::prop_set p)
796  {
797  return SPOT_make_shared_enabled__(twa_graph, aut, p);
798  }
799 
806  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
807  twa::prop_set p,
808  bool preserve_name_properties = false)
809  {
810  twa_graph_ptr res = SPOT_make_shared_enabled__(twa_graph, aut, p);
811  if (preserve_name_properties)
812  res->copy_named_properties_of(aut);
813  return res;
814  }
815 
825  SPOT_API twa_graph_ptr
826  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
827  bool preserve_names = false,
828  // parentheses for SWIG, see
829  // https://github.com/swig/swig/issues/993
830  unsigned max_states = -(1U));
831 }
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:657
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:930
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:696
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:682
edge index_of_edge(const edge_storage_t &tt) const
Convert a storage reference into an edge number.
Definition: graph.hh:892
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:996
edge_storage_t::data_t & edge_data(edge s)
return the Edge_Data of an edge.
Definition: graph.hh:765
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1024
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:747
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: graph.hh:671
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:901
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:808
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:711
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:665
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:729
bool is_dead_edge(unsigned t) const
Tests whether an edge has been erased.
Definition: graph.hh:1008
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:843
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:784
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (excluding erased edges)
Definition: graph.hh:960
Definition: ngraph.hh:32
This class is used to tell parallel algorithms what resources they may use.
Definition: common.hh:155
Abstract class for states.
Definition: twa.hh:47
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:135
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:160
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: twagraph.hh:177
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: twagraph.hh:183
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: twagraph.hh:154
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:171
virtual bool done() const override
Check whether the iteration is finished.
Definition: twagraph.hh:166
Graph-based representation of a TωA.
Definition: twagraph.hh:199
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:331
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:684
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:697
virtual const twa_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: twagraph.hh:321
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:374
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:665
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:505
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:394
A Transition-based ω-Automaton.
Definition: twa.hh:619
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:937
LTL/PSL formula interface.
twa_graph_ptr make_twa_graph(const bdd_dict_ptr &dict)
Build an explicit automaton from all states of aut,.
Definition: twagraph.hh:787
Definition: automata.hh:26
void prop_copy(const const_twa_ptr &other, prop_set p)
Copy the properties of another automaton.
Definition: twa.hh:1630
An acceptance mark.
Definition: acc.hh:84
Definition: graph.hh:65
Definition: graph.hh:187
Data attached to edges of a twa_graph.
Definition: twagraph.hh:94
Graph-based representation of a TωA.
Definition: twagraph.hh:39
virtual void destroy() const override
Release a state.
Definition: twagraph.hh:81
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:58
virtual size_t hash() const override
Hash a state.
Definition: twagraph.hh:70
virtual twa_graph_state * clone() const override
Duplicate a state.
Definition: twagraph.hh:76

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