spot  2.5.1
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2018 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  virtual ~twa_graph_state() noexcept
46  {
47  }
48 
49  virtual int compare(const spot::state* other) const override
50  {
51  auto o = down_cast<const twa_graph_state*>(other);
52 
53  // Do not simply return "other - this", it might not fit in an int.
54  if (o < this)
55  return -1;
56  if (o > this)
57  return 1;
58  return 0;
59  }
60 
61  virtual size_t hash() const override
62  {
63  return
64  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
65  }
66 
67  virtual twa_graph_state*
68  clone() const override
69  {
70  return const_cast<twa_graph_state*>(this);
71  }
72 
73  virtual void destroy() const override
74  {
75  }
76  };
77 
85  struct SPOT_API twa_graph_edge_data
86  {
87  bdd cond;
88  acc_cond::mark_t acc;
89 
90  explicit twa_graph_edge_data() noexcept
91  : cond(bddfalse), acc(0)
92  {
93  }
94 
95  twa_graph_edge_data(bdd cond, acc_cond::mark_t acc = 0U) noexcept
96  : cond(cond), acc(acc)
97  {
98  }
99 
100  bool operator<(const twa_graph_edge_data& other) const
101  {
102  if (cond.id() < other.cond.id())
103  return true;
104  if (cond.id() > other.cond.id())
105  return false;
106  return acc < other.acc;
107  }
108 
109  bool operator==(const twa_graph_edge_data& other) const
110  {
111  return cond.id() == other.cond.id() &&
112  acc == other.acc;
113  }
114  };
115 
116 
117 
122  template<class Graph>
123  class SPOT_API twa_graph_succ_iterator final:
124  public twa_succ_iterator
125  {
126  private:
127  typedef typename Graph::edge edge;
128  typedef typename Graph::state_data_t state;
129  const Graph* g_;
130  edge t_;
131  edge p_;
132 
133  public:
134  twa_graph_succ_iterator(const Graph* g, edge t)
135  : g_(g), t_(t)
136  {
137  }
138 
139  void recycle(edge t)
140  {
141  t_ = t;
142  }
143 
144  virtual bool first() override
145  {
146  p_ = t_;
147  return p_;
148  }
149 
150  virtual bool next() override
151  {
152  p_ = g_->edge_storage(p_).next_succ;
153  return p_;
154  }
155 
156  virtual bool done() const override
157  {
158  return !p_;
159  }
160 
161  virtual const twa_graph_state* dst() const override
162  {
163  SPOT_ASSERT(!done());
164  return &g_->state_data(g_->edge_storage(p_).dst);
165  }
166 
167  virtual bdd cond() const override
168  {
169  SPOT_ASSERT(!done());
170  return g_->edge_data(p_).cond;
171  }
172 
173  virtual acc_cond::mark_t acc() const override
174  {
175  SPOT_ASSERT(!done());
176  return g_->edge_data(p_).acc;
177  }
178 
179  edge pos() const
180  {
181  return p_;
182  }
183 
184  };
185 
188  class SPOT_API twa_graph final: public twa
189  {
190  public:
192  // We avoid using graph_t::edge_storage_t because graph_t is not
193  // instantiated in the SWIG bindings, and SWIG would therefore
194  // handle graph_t::edge_storage_t as an abstract type.
195  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
197  <twa_graph_edge_data, false>>
199  static_assert(std::is_same<typename graph_t::edge_storage_t,
200  edge_storage_t>::value, "type mismatch");
201  // We avoid using graph_t::state for the very same reason.
202  typedef unsigned state_num;
203  static_assert(std::is_same<typename graph_t::state, state_num>::value,
204  "type mismatch");
205 
206  protected:
207  graph_t g_;
208  mutable unsigned init_number_;
209 
210  public:
211  twa_graph(const bdd_dict_ptr& dict)
212  : twa(dict),
213  init_number_(0)
214  {
215  }
216 
217  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
218  : twa(other->get_dict()),
219  g_(other->g_), init_number_(other->init_number_)
220  {
221  copy_acceptance_of(other);
222  copy_ap_of(other);
223  prop_copy(other, p);
224  }
225 
226  virtual ~twa_graph()
227  {
228  }
229 
230 #ifndef SWIG
231  template <typename State_Name,
232  typename Name_Hash = std::hash<State_Name>,
233  typename Name_Equal = std::equal_to<State_Name>>
235 
236  template <typename State_Name,
237  typename Name_Hash = std::hash<State_Name>,
238  typename Name_Equal = std::equal_to<State_Name>>
240  create_namer()
241  {
243  }
244 
246  create_formula_namer()
247  {
248  return create_namer<formula>();
249  }
250 
251  void
252  release_formula_namer(namer<formula>* namer, bool keep_names);
253 #endif
254 
255  graph_t& get_graph()
256  {
257  return g_;
258  }
259 
260  const graph_t& get_graph() const
261  {
262  return g_;
263  }
264 
265  unsigned num_states() const
266  {
267  return g_.num_states();
268  }
269 
270  unsigned num_edges() const
271  {
272  return g_.num_edges();
273  }
274 
275  void set_init_state(state_num s)
276  {
277  if (SPOT_UNLIKELY(s >= num_states()))
278  throw std::invalid_argument
279  ("set_init_state() called with nonexisting state");
280  init_number_ = s;
281  }
282 
283  template<class I>
284  void set_univ_init_state(I dst_begin, I dst_end)
285  {
286  auto ns = num_states();
287  for (I i = dst_begin; i != dst_end; ++i)
288  if (SPOT_UNLIKELY(*i >= ns))
289  throw std::invalid_argument
290  ("set_univ_init_state() called with nonexisting state");
291  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
292  }
293 
294  void set_univ_init_state(const std::initializer_list<state_num>& il)
295  {
296  set_univ_init_state(il.begin(), il.end());
297  }
298 
299  state_num get_init_state_number() const
300  {
301  // If the automaton has no state, it has no initial state.
302  if (num_states() == 0)
303  throw std::runtime_error("automaton has no state at all");
304  return init_number_;
305  }
306 
307  virtual const twa_graph_state* get_init_state() const override
308  {
309  unsigned n = get_init_state_number();
310  if (SPOT_UNLIKELY(!is_existential()))
311  throw std::runtime_error
312  ("the abstract interface does not support alternating automata");
313  return state_from_number(n);
314  }
315 
316  virtual twa_succ_iterator*
317  succ_iter(const state* st) const override
318  {
319  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
320  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
321 
322  if (this->iter_cache_)
323  {
324  auto it =
325  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
326  it->recycle(s->succ);
327  this->iter_cache_ = nullptr;
328  return it;
329  }
330  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
331  }
332 
333  static constexpr bool is_univ_dest(const edge_storage_t& e)
334  {
335  return is_univ_dest(e.dst);
336  }
337 
338  static constexpr bool is_univ_dest(unsigned s)
339  {
340  // Universal destinations are stored with their most-significant
341  // bit set.
342  return (int) s < 0;
343  }
344 
345  state_num
346  state_number(const state* st) const
347  {
348  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
349  return s - &g_.state_storage(0);
350  }
351 
352  const twa_graph_state*
353  state_from_number(state_num n) const
354  {
355  return &g_.state_data(n);
356  }
357 
358  std::string format_state(unsigned n) const;
359 
360  virtual std::string format_state(const state* st) const override
361  {
362  return format_state(state_number(st));
363  }
364 
365  unsigned edge_number(const twa_succ_iterator* it) const
366  {
367  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
368  return i->pos();
369  }
370 
371  unsigned edge_number(const edge_storage_t& e) const
372  {
373  return g_.index_of_edge(e);
374  }
375 
376  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
377  {
378  return g_.edge_data(edge_number(it));
379  }
380 
381  twa_graph_edge_data& edge_data(unsigned t)
382  {
383  return g_.edge_data(t);
384  }
385 
386  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
387  {
388  return g_.edge_data(edge_number(it));
389  }
390 
391  const twa_graph_edge_data& edge_data(unsigned t) const
392  {
393  return g_.edge_data(t);
394  }
395 
396  edge_storage_t& edge_storage(const twa_succ_iterator* it)
397  {
398  return g_.edge_storage(edge_number(it));
399  }
400 
401  edge_storage_t& edge_storage(unsigned t)
402  {
403  return g_.edge_storage(t);
404  }
405 
406  const edge_storage_t
407  edge_storage(const twa_succ_iterator* it) const
408  {
409  return g_.edge_storage(edge_number(it));
410  }
411 
412  const edge_storage_t edge_storage(unsigned t) const
413  {
414  return g_.edge_storage(t);
415  }
416 
417  unsigned new_state()
418  {
419  return g_.new_state();
420  }
421 
422  unsigned new_states(unsigned n)
423  {
424  return g_.new_states(n);
425  }
426 
427  unsigned new_edge(unsigned src, unsigned dst,
428  bdd cond, acc_cond::mark_t acc = 0U)
429  {
430  return g_.new_edge(src, dst, cond, acc);
431  }
432 
433  unsigned new_acc_edge(unsigned src, unsigned dst,
434  bdd cond, bool acc = true)
435  {
436  if (acc)
437  return g_.new_edge(src, dst, cond, this->acc().all_sets());
438  else
439  return g_.new_edge(src, dst, cond);
440  }
441 
442  template<class I>
443  unsigned new_univ_edge(unsigned src, I begin, I end,
444  bdd cond, acc_cond::mark_t acc = 0U)
445  {
446  return g_.new_univ_edge(src, begin, end, cond, acc);
447  }
448 
449  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
450  bdd cond, acc_cond::mark_t acc = 0U)
451  {
452  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
453  }
454 
455 #ifndef SWIG
457  out(unsigned src) const
458  {
459  return g_.out(src);
460  }
461 #endif
462 
464  out(unsigned src)
465  {
466  return g_.out(src);
467  }
468 
470  univ_dests(unsigned d) const noexcept
471  {
472  return g_.univ_dests(d);
473  }
474 
476  univ_dests(const edge_storage_t& e) const noexcept
477  {
478  return g_.univ_dests(e);
479  }
480 
482  bool is_existential() const
483  {
484  return g_.is_existential();
485  }
486 
487 #ifndef SWIG
488  SPOT_DEPRECATED("use !is_existential() instead")
493  bool is_alternating() const
494  {
495  return !is_existential();
496  }
497 #endif
498 
499 #ifndef SWIG
500  auto states() const
501  SPOT_RETURN(g_.states());
502  auto states()
503  SPOT_RETURN(g_.states());
504 
506  edges() const noexcept
507  {
508  return g_.edges();
509  }
510 #endif
511 
513  edges() noexcept
514  {
515  return g_.edges();
516  }
517 
518 #ifndef SWIG
519  auto edge_vector() const
520  SPOT_RETURN(g_.edge_vector());
521  auto edge_vector()
522  SPOT_RETURN(g_.edge_vector());
523 
524  auto is_dead_edge(const graph_t::edge_storage_t& t) const
525  SPOT_RETURN(g_.is_dead_edge(t));
526 #endif
527 
544  void merge_edges();
545 
549  void merge_univ_dests();
550 
566  void purge_dead_states();
567 
580  void purge_unreachable_states();
581 
586  void remove_unused_ap();
587 
594  void copy_state_names_from(const const_twa_graph_ptr& other);
595 
599  {
600  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
601  throw std::runtime_error
602  ("state_acc_sets() should only be called on "
603  "automata with state-based acceptance");
604  for (auto& t: g_.out(s))
605  // Stop at the first edge, since the remaining should be
606  // labeled identically.
607  return t.acc;
608  return 0U;
609  }
610 
617  bool state_is_accepting(unsigned s) const
618  {
619  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
620  throw std::runtime_error
621  ("state_is_accepting() should only be called on "
622  "automata with state-based acceptance");
623  for (auto& t: g_.out(s))
624  // Stop at the first edge, since the remaining should be
625  // labeled identically.
626  return acc().accepting(t.acc);
627  return false;
628  }
629 
630  bool state_is_accepting(const state* s) const
631  {
632  return state_is_accepting(state_number(s));
633  }
635 
636  bool operator==(const twa_graph& aut) const
637  {
638  auto& dests1 = g_.dests_vector();
639  auto& dests2 = aut.get_graph().dests_vector();
640  if (num_states() != aut.num_states() ||
641  num_edges() != aut.num_edges() ||
642  num_sets() != aut.num_sets() ||
643  dests1.size() != dests2.size())
644  return false;
645  auto& trans1 = edge_vector();
646  auto& trans2 = aut.edge_vector();
647  if (!std::equal(trans1.begin() + 1, trans1.end(),
648  trans2.begin() + 1))
649  return false;
650  return std::equal(dests1.begin(), dests1.end(),
651  dests2.begin());
652  }
653 
663  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
664  };
665 
668  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
669  {
670  return std::make_shared<twa_graph>(dict);
671  }
672 
675  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
676  twa::prop_set p)
677  {
678  return std::make_shared<twa_graph>(aut, p);
679  }
680 
683  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
684  twa::prop_set p)
685  {
686  return std::make_shared<twa_graph>(aut, p);
687  }
688 
698  SPOT_API twa_graph_ptr
699  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
700  bool preserve_names = false,
701  // parentheses for SWIG, see
702  // https://github.com/swig/swig/issues/993
703  unsigned max_states = -(1U));
704 }
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:697
Definition: automata.hh:26
Definition: graph.hh:62
virtual void destroy() const override
Release a state.
Definition: twagraph.hh:73
bool state_is_accepting(const state *s) const
Tell if a state is acceptince.
Definition: twagraph.hh:630
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1010
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:683
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:660
virtual size_t hash() const override
Hash a state.
Definition: twagraph.hh:61
Definition: graph.hh:184
Definition: ngraph.hh:32
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:748
virtual std::string format_state(const state *st) const override
Format the state as a string for printing.
Definition: twagraph.hh:360
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:785
virtual const twa_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: twagraph.hh:307
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:730
Definition: graph.hh:159
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:712
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: twagraph.hh:144
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:766
Definition: graph.hh:496
virtual bdd cond() const override
Get the condition on the edge leading to this successor.
Definition: twagraph.hh:167
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:598
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:646
virtual bool done() const override
Check whether the iteration is finished.
Definition: twagraph.hh:156
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:49
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:161
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:654
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:982
edge index_of_edge(const edge_storage_t &tt) const
Conveart a storage reference into an edge number.
Definition: graph.hh:878
virtual acc_cond::mark_t acc() const override
Get the acceptance mark of the edge leading to this successor.
Definition: twagraph.hh:173
bool state_is_accepting(unsigned s) const
Tell if a state is acceptince.
Definition: twagraph.hh:617
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:887
virtual twa_succ_iterator * succ_iter(const state *st) const override
Get an iterator over the successors of local_state.
Definition: twagraph.hh:317
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:123
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:809
Definition: graph.hh:385
Graph-based representation of a TωA.
Definition: twagraph.hh:188
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:829
unsigned num_sets() const
Number of acceptance sets used by the automaton.
Definition: twa.hh:906
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:150
Definition: acc.hh:40
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:482
Data attached to edges of a twa_graph.
Definition: twagraph.hh:85
virtual twa_graph_state * clone() const override
Duplicate a state.
Definition: twagraph.hh:68

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