spot  2.8.1
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  if (SPOT_UNLIKELY(s >= num_states()))
289  throw std::invalid_argument
290  ("set_init_state() called with nonexisting state");
291  init_number_ = s;
292  }
293 
294  template<class I>
295  void set_univ_init_state(I dst_begin, I dst_end)
296  {
297  auto ns = num_states();
298  for (I i = dst_begin; i != dst_end; ++i)
299  if (SPOT_UNLIKELY(*i >= ns))
300  throw std::invalid_argument
301  ("set_univ_init_state() called with nonexisting state");
302  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
303  }
304 
305  void set_univ_init_state(const std::initializer_list<state_num>& il)
306  {
307  set_univ_init_state(il.begin(), il.end());
308  }
309 
310  state_num get_init_state_number() const
311  {
312  // If the automaton has no state, it has no initial state.
313  if (num_states() == 0)
314  throw std::runtime_error("automaton has no state at all");
315  return init_number_;
316  }
317 
318  virtual const twa_graph_state* get_init_state() const override
319  {
320  unsigned n = get_init_state_number();
321  if (SPOT_UNLIKELY(!is_existential()))
322  throw std::runtime_error
323  ("the abstract interface does not support alternating automata");
324  return state_from_number(n);
325  }
326 
327  virtual twa_succ_iterator*
328  succ_iter(const state* st) const override
329  {
330  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
331  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
332 
333  if (this->iter_cache_)
334  {
335  auto it =
336  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
337  it->recycle(s->succ);
338  this->iter_cache_ = nullptr;
339  return it;
340  }
341  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
342  }
343 
344  static constexpr bool is_univ_dest(const edge_storage_t& e)
345  {
346  return is_univ_dest(e.dst);
347  }
348 
349  static constexpr bool is_univ_dest(unsigned s)
350  {
351  // Universal destinations are stored with their most-significant
352  // bit set.
353  return (int) s < 0;
354  }
355 
356  state_num
357  state_number(const state* st) const
358  {
359  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
360  return s - &g_.state_storage(0);
361  }
362 
363  const twa_graph_state*
364  state_from_number(state_num n) const
365  {
366  return &g_.state_data(n);
367  }
368 
369  std::string format_state(unsigned n) const;
370 
371  virtual std::string format_state(const state* st) const override
372  {
373  return format_state(state_number(st));
374  }
375 
376  unsigned edge_number(const twa_succ_iterator* it) const
377  {
378  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
379  return i->pos();
380  }
381 
382  unsigned edge_number(const edge_storage_t& e) const
383  {
384  return g_.index_of_edge(e);
385  }
386 
387  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
388  {
389  return g_.edge_data(edge_number(it));
390  }
391 
392  twa_graph_edge_data& edge_data(unsigned t)
393  {
394  return g_.edge_data(t);
395  }
396 
397  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
398  {
399  return g_.edge_data(edge_number(it));
400  }
401 
402  const twa_graph_edge_data& edge_data(unsigned t) const
403  {
404  return g_.edge_data(t);
405  }
406 
407  edge_storage_t& edge_storage(const twa_succ_iterator* it)
408  {
409  return g_.edge_storage(edge_number(it));
410  }
411 
412  edge_storage_t& edge_storage(unsigned t)
413  {
414  return g_.edge_storage(t);
415  }
416 
417  const edge_storage_t
418  edge_storage(const twa_succ_iterator* it) const
419  {
420  return g_.edge_storage(edge_number(it));
421  }
422 
423  const edge_storage_t edge_storage(unsigned t) const
424  {
425  return g_.edge_storage(t);
426  }
427 
428  unsigned new_state()
429  {
430  return g_.new_state();
431  }
432 
433  unsigned new_states(unsigned n)
434  {
435  return g_.new_states(n);
436  }
437 
438  unsigned new_edge(unsigned src, unsigned dst,
439  bdd cond,
440  acc_cond::mark_t acc = {})
441  {
442  return g_.new_edge(src, dst, cond, acc);
443  }
444 
445  unsigned new_acc_edge(unsigned src, unsigned dst,
446  bdd cond, bool acc = true)
447  {
448  if (acc)
449  return g_.new_edge(src, dst, cond, this->acc().all_sets());
450  else
451  return g_.new_edge(src, dst, cond);
452  }
453 
454  template<class I>
455  unsigned new_univ_edge(unsigned src, I begin, I end,
456  bdd cond,
457  acc_cond::mark_t acc = {})
458  {
459  return g_.new_univ_edge(src, begin, end, cond, acc);
460  }
461 
462  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
463  bdd cond,
464  acc_cond::mark_t acc = {})
465  {
466  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
467  }
468 
469 #ifndef SWIG
471  out(unsigned src) const
472  {
473  return g_.out(src);
474  }
475 #endif
476 
478  out(unsigned src)
479  {
480  return g_.out(src);
481  }
482 
484  out_iteraser(unsigned src)
485  {
486  return g_.out_iteraser(src);
487  }
488 
490  univ_dests(unsigned d) const noexcept
491  {
492  return g_.univ_dests(d);
493  }
494 
496  univ_dests(const edge_storage_t& e) const noexcept
497  {
498  return g_.univ_dests(e);
499  }
500 
502  bool is_existential() const
503  {
504  return g_.is_existential();
505  }
506 
507 #ifndef SWIG
508  SPOT_DEPRECATED("use !is_existential() instead")
513  bool is_alternating() const
514  {
515  return !is_existential();
516  }
517 #endif
518 
519 #ifndef SWIG
520  auto states() const
521  SPOT_RETURN(g_.states());
522  auto states()
523  SPOT_RETURN(g_.states());
524 
526  edges() const noexcept
527  {
528  return g_.edges();
529  }
530 #endif
531 
533  edges() noexcept
534  {
535  return g_.edges();
536  }
537 
538 #ifndef SWIG
539  auto edge_vector() const
540  SPOT_RETURN(g_.edge_vector());
541  auto edge_vector()
542  SPOT_RETURN(g_.edge_vector());
543 #endif
544 
545  bool is_dead_edge(unsigned t) const
546  {
547  return g_.is_dead_edge(t);
548  }
549 
550  bool is_dead_edge(const graph_t::edge_storage_t& t) const
551  {
552  return g_.is_dead_edge(t);
553  }
554 
571  void merge_edges();
572 
576  void merge_univ_dests();
577 
584  void merge_states();
585 
601  void purge_dead_states();
602 
620  typedef void (*shift_action)(const std::vector<unsigned>& newst,
621  void* action_data);
622  void purge_unreachable_states(shift_action* f = nullptr,
623  void* action_data = nullptr);
624 
629  void remove_unused_ap();
630 
637  void copy_state_names_from(const const_twa_graph_ptr& other);
638 
642  {
643  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
644  throw std::runtime_error
645  ("state_acc_sets() should only be called on "
646  "automata with state-based acceptance");
647  for (auto& t: g_.out(s))
648  // Stop at the first edge, since the remaining should be
649  // labeled identically.
650  return t.acc;
651  return {};
652  }
653 
660  bool state_is_accepting(unsigned s) const
661  {
662  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
663  throw std::runtime_error
664  ("state_is_accepting() should only be called on "
665  "automata with state-based acceptance");
666  for (auto& t: g_.out(s))
667  // Stop at the first edge, since the remaining should be
668  // labeled identically.
669  return acc().accepting(t.acc);
670  return false;
671  }
672 
673  bool state_is_accepting(const state* s) const
674  {
675  return state_is_accepting(state_number(s));
676  }
678 
679  bool operator==(const twa_graph& aut) const
680  {
681  auto& dests1 = g_.dests_vector();
682  auto& dests2 = aut.get_graph().dests_vector();
683  if (num_states() != aut.num_states() ||
684  num_edges() != aut.num_edges() ||
685  num_sets() != aut.num_sets() ||
686  dests1.size() != dests2.size())
687  return false;
688  auto& trans1 = edge_vector();
689  auto& trans2 = aut.edge_vector();
690  if (!std::equal(trans1.begin() + 1, trans1.end(),
691  trans2.begin() + 1))
692  return false;
693  return std::equal(dests1.begin(), dests1.end(),
694  dests2.begin());
695  }
696 
706  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
707 
713  void dump_storage_as_dot(std::ostream& out,
714  const char* opt = nullptr) const;
715  };
716 
717  // This is a workaround for
718 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
719 # define SPOT_make_twa_graph__(...) \
720  std::shared_ptr<twa_graph>(new twa_graph(__VA_ARGS__))
721 #else
722 # define SPOT_make_twa_graph__(...) \
723  std::make_shared<twa_graph>(__VA_ARGS__)
724 #endif
725 
728  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
729  {
730  return SPOT_make_shared_enabled__(twa_graph, dict);
731  }
732 
735  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
736  twa::prop_set p)
737  {
738  return SPOT_make_shared_enabled__(twa_graph, aut, p);
739  }
740 
743  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
744  twa::prop_set p)
745  {
746  return SPOT_make_shared_enabled__(twa_graph, aut, p);
747  }
748 
758  SPOT_API twa_graph_ptr
759  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
760  bool preserve_names = false,
761  // parentheses for SWIG, see
762  // https://github.com/swig/swig/issues/993
763  unsigned max_states = -(1U));
764 }
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:673
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:371
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:318
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:641
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:660
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:328
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:502
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