spot  2.8.3
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2019 Laboratoire de Recherche et Développement de l'Epita.
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
73  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
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  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
474  out(unsigned src) const
475  {
476  return g_.out(src);
477  }
478 #endif
479 
481  out(unsigned src)
482  {
483  return g_.out(src);
484  }
485 
487  out_iteraser(unsigned src)
488  {
489  return g_.out_iteraser(src);
490  }
491 
493  univ_dests(unsigned d) const noexcept
494  {
495  return g_.univ_dests(d);
496  }
497 
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  SPOT_DEPRECATED("use !is_existential() instead")
516  bool is_alternating() const
517  {
518  return !is_existential();
519  }
520 #endif
521 
522 #ifndef SWIG
523  auto states() const
524  SPOT_RETURN(g_.states());
525  auto states()
526  SPOT_RETURN(g_.states());
527 
529  edges() const noexcept
530  {
531  return g_.edges();
532  }
533 #endif
534 
536  edges() noexcept
537  {
538  return g_.edges();
539  }
540 
541 #ifndef SWIG
542  auto edge_vector() const
543  SPOT_RETURN(g_.edge_vector());
544  auto edge_vector()
545  SPOT_RETURN(g_.edge_vector());
546 #endif
547 
548  bool is_dead_edge(unsigned t) const
549  {
550  return g_.is_dead_edge(t);
551  }
552 
553  bool is_dead_edge(const graph_t::edge_storage_t& t) const
554  {
555  return g_.is_dead_edge(t);
556  }
557 
574  void merge_edges();
575 
579  void merge_univ_dests();
580 
587  void merge_states();
588 
604  void purge_dead_states();
605 
623  typedef void (*shift_action)(const std::vector<unsigned>& newst,
624  void* action_data);
625  void purge_unreachable_states(shift_action* f = nullptr,
626  void* action_data = nullptr);
627 
632  void remove_unused_ap();
633 
640  void copy_state_names_from(const const_twa_graph_ptr& other);
641 
645  {
646  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
647  throw std::runtime_error
648  ("state_acc_sets() should only be called on "
649  "automata with state-based acceptance");
650  for (auto& t: g_.out(s))
651  // Stop at the first edge, since the remaining should be
652  // labeled identically.
653  return t.acc;
654  return {};
655  }
656 
663  bool state_is_accepting(unsigned s) const
664  {
665  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
666  throw std::runtime_error
667  ("state_is_accepting() should only be called on "
668  "automata with state-based acceptance");
669  for (auto& t: g_.out(s))
670  // Stop at the first edge, since the remaining should be
671  // labeled identically.
672  return acc().accepting(t.acc);
673  return false;
674  }
675 
676  bool state_is_accepting(const state* s) const
677  {
678  return state_is_accepting(state_number(s));
679  }
681 
682  bool operator==(const twa_graph& aut) const
683  {
684  auto& dests1 = g_.dests_vector();
685  auto& dests2 = aut.get_graph().dests_vector();
686  if (num_states() != aut.num_states() ||
687  num_edges() != aut.num_edges() ||
688  num_sets() != aut.num_sets() ||
689  dests1.size() != dests2.size())
690  return false;
691  auto& trans1 = edge_vector();
692  auto& trans2 = aut.edge_vector();
693  if (!std::equal(trans1.begin() + 1, trans1.end(),
694  trans2.begin() + 1))
695  return false;
696  return std::equal(dests1.begin(), dests1.end(),
697  dests2.begin());
698  }
699 
709  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
710 
716  void dump_storage_as_dot(std::ostream& out,
717  const char* opt = nullptr) const;
718  };
719 
720  // This is a workaround for
721 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
722 # define SPOT_make_twa_graph__(...) \
723  std::shared_ptr<twa_graph>(new twa_graph(__VA_ARGS__))
724 #else
725 # define SPOT_make_twa_graph__(...) \
726  std::make_shared<twa_graph>(__VA_ARGS__)
727 #endif
728 
731  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
732  {
733  return SPOT_make_shared_enabled__(twa_graph, dict);
734  }
735 
738  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
739  twa::prop_set p)
740  {
741  return SPOT_make_shared_enabled__(twa_graph, aut, p);
742  }
743 
746  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
747  twa::prop_set p)
748  {
749  return SPOT_make_shared_enabled__(twa_graph, aut, p);
750  }
751 
761  SPOT_API twa_graph_ptr
762  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
763  bool preserve_names = false,
764  // parentheses for SWIG, see
765  // https://github.com/swig/swig/issues/993
766  unsigned max_states = -(1U));
767 }
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:696
Definition: automata.hh:26
Definition: graph.hh:61
virtual void destroy() const override
Release a state.
Definition: twagraph.hh:82
bool state_is_accepting(const state *s) const
Tell if a state is accepting.
Definition: twagraph.hh:676
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1009
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:682
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:70
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:915
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:747
virtual std::string format_state(const state *st) const override
Format the state as a string for printing.
Definition: twagraph.hh:374
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:784
virtual const twa_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: twagraph.hh:321
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:729
Definition: graph.hh:158
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:711
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: twagraph.hh:155
Iterate over the successors of a state.
Definition: twa.hh:397
edge_storage_t::data_t & edge_data(edge s)
return the Edgeg_Data of an edge.
Definition: graph.hh:765
Definition: graph.hh:495
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: twagraph.hh:178
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:644
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
virtual bool done() const override
Check whether the iteration is finished.
Definition: twagraph.hh:167
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:58
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:172
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:981
edge index_of_edge(const edge_storage_t &tt) const
Conveart a storage reference into an edge number.
Definition: graph.hh:877
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: twagraph.hh:184
bool state_is_accepting(unsigned s) const
Tell if a state is accepting.
Definition: twagraph.hh:663
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:886
virtual twa_succ_iterator * succ_iter(const state *st) const override
Get an iterator over the successors of local_state.
Definition: twagraph.hh:331
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:134
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:808
Definition: graph.hh:384
Graph-based representation of a TωA.
Definition: twagraph.hh:199
Graph-based representation of a TωA.
Definition: twagraph.hh:38
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:828
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:954
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:161
An acceptance mark.
Definition: acc.hh:81
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:505
Data attached to edges of a twa_graph.
Definition: twagraph.hh:94
Definition: graph.hh:315
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.8.13