spot  2.6.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({})
92  {
93  }
94 
96  bdd cond,
97  acc_cond::mark_t acc = {}) noexcept
98  : cond(cond), acc(acc)
99  {
100  }
101 
102  bool operator<(const twa_graph_edge_data& other) const
103  {
104  if (cond.id() < other.cond.id())
105  return true;
106  if (cond.id() > other.cond.id())
107  return false;
108  return acc < other.acc;
109  }
110 
111  bool operator==(const twa_graph_edge_data& other) const
112  {
113  return cond.id() == other.cond.id() &&
114  acc == other.acc;
115  }
116  };
117 
118 
119 
124  template<class Graph>
125  class SPOT_API twa_graph_succ_iterator final:
126  public twa_succ_iterator
127  {
128  private:
129  typedef typename Graph::edge edge;
130  typedef typename Graph::state_data_t state;
131  const Graph* g_;
132  edge t_;
133  edge p_;
134 
135  public:
136  twa_graph_succ_iterator(const Graph* g, edge t)
137  : g_(g), t_(t)
138  {
139  }
140 
141  void recycle(edge t)
142  {
143  t_ = t;
144  }
145 
146  virtual bool first() override
147  {
148  p_ = t_;
149  return p_;
150  }
151 
152  virtual bool next() override
153  {
154  p_ = g_->edge_storage(p_).next_succ;
155  return p_;
156  }
157 
158  virtual bool done() const override
159  {
160  return !p_;
161  }
162 
163  virtual const twa_graph_state* dst() const override
164  {
165  SPOT_ASSERT(!done());
166  return &g_->state_data(g_->edge_storage(p_).dst);
167  }
168 
169  virtual bdd cond() const override
170  {
171  SPOT_ASSERT(!done());
172  return g_->edge_data(p_).cond;
173  }
174 
175  virtual acc_cond::mark_t acc() const override
176  {
177  SPOT_ASSERT(!done());
178  return g_->edge_data(p_).acc;
179  }
180 
181  edge pos() const
182  {
183  return p_;
184  }
185 
186  };
187 
190  class SPOT_API twa_graph final: public twa
191  {
192  public:
194  // We avoid using graph_t::edge_storage_t because graph_t is not
195  // instantiated in the SWIG bindings, and SWIG would therefore
196  // handle graph_t::edge_storage_t as an abstract type.
197  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
199  <twa_graph_edge_data, false>>
201  static_assert(std::is_same<typename graph_t::edge_storage_t,
202  edge_storage_t>::value, "type mismatch");
203  // We avoid using graph_t::state for the very same reason.
204  typedef unsigned state_num;
205  static_assert(std::is_same<typename graph_t::state, state_num>::value,
206  "type mismatch");
207 
208  protected:
209  graph_t g_;
210  mutable unsigned init_number_;
211 
212  public:
213  twa_graph(const bdd_dict_ptr& dict)
214  : twa(dict),
215  init_number_(0)
216  {
217  }
218 
219  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
220  : twa(other->get_dict()),
221  g_(other->g_), init_number_(other->init_number_)
222  {
223  copy_acceptance_of(other);
224  copy_ap_of(other);
225  prop_copy(other, p);
226  }
227 
228  virtual ~twa_graph()
229  {
230  }
231 
232 #ifndef SWIG
233  template <typename State_Name,
234  typename Name_Hash = std::hash<State_Name>,
235  typename Name_Equal = std::equal_to<State_Name>>
237 
238  template <typename State_Name,
239  typename Name_Hash = std::hash<State_Name>,
240  typename Name_Equal = std::equal_to<State_Name>>
242  create_namer()
243  {
245  }
246 
248  create_formula_namer()
249  {
250  return create_namer<formula>();
251  }
252 
253  void
254  release_formula_namer(namer<formula>* namer, bool keep_names);
255 #endif
256 
257  graph_t& get_graph()
258  {
259  return g_;
260  }
261 
262  const graph_t& get_graph() const
263  {
264  return g_;
265  }
266 
267  unsigned num_states() const
268  {
269  return g_.num_states();
270  }
271 
272  unsigned num_edges() const
273  {
274  return g_.num_edges();
275  }
276 
277  void set_init_state(state_num s)
278  {
279  if (SPOT_UNLIKELY(s >= num_states()))
280  throw std::invalid_argument
281  ("set_init_state() called with nonexisting state");
282  init_number_ = s;
283  }
284 
285  template<class I>
286  void set_univ_init_state(I dst_begin, I dst_end)
287  {
288  auto ns = num_states();
289  for (I i = dst_begin; i != dst_end; ++i)
290  if (SPOT_UNLIKELY(*i >= ns))
291  throw std::invalid_argument
292  ("set_univ_init_state() called with nonexisting state");
293  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
294  }
295 
296  void set_univ_init_state(const std::initializer_list<state_num>& il)
297  {
298  set_univ_init_state(il.begin(), il.end());
299  }
300 
301  state_num get_init_state_number() const
302  {
303  // If the automaton has no state, it has no initial state.
304  if (num_states() == 0)
305  throw std::runtime_error("automaton has no state at all");
306  return init_number_;
307  }
308 
309  virtual const twa_graph_state* get_init_state() const override
310  {
311  unsigned n = get_init_state_number();
312  if (SPOT_UNLIKELY(!is_existential()))
313  throw std::runtime_error
314  ("the abstract interface does not support alternating automata");
315  return state_from_number(n);
316  }
317 
318  virtual twa_succ_iterator*
319  succ_iter(const state* st) const override
320  {
321  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
322  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
323 
324  if (this->iter_cache_)
325  {
326  auto it =
327  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
328  it->recycle(s->succ);
329  this->iter_cache_ = nullptr;
330  return it;
331  }
332  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
333  }
334 
335  static constexpr bool is_univ_dest(const edge_storage_t& e)
336  {
337  return is_univ_dest(e.dst);
338  }
339 
340  static constexpr bool is_univ_dest(unsigned s)
341  {
342  // Universal destinations are stored with their most-significant
343  // bit set.
344  return (int) s < 0;
345  }
346 
347  state_num
348  state_number(const state* st) const
349  {
350  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
351  return s - &g_.state_storage(0);
352  }
353 
354  const twa_graph_state*
355  state_from_number(state_num n) const
356  {
357  return &g_.state_data(n);
358  }
359 
360  std::string format_state(unsigned n) const;
361 
362  virtual std::string format_state(const state* st) const override
363  {
364  return format_state(state_number(st));
365  }
366 
367  unsigned edge_number(const twa_succ_iterator* it) const
368  {
369  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
370  return i->pos();
371  }
372 
373  unsigned edge_number(const edge_storage_t& e) const
374  {
375  return g_.index_of_edge(e);
376  }
377 
378  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
379  {
380  return g_.edge_data(edge_number(it));
381  }
382 
383  twa_graph_edge_data& edge_data(unsigned t)
384  {
385  return g_.edge_data(t);
386  }
387 
388  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
389  {
390  return g_.edge_data(edge_number(it));
391  }
392 
393  const twa_graph_edge_data& edge_data(unsigned t) const
394  {
395  return g_.edge_data(t);
396  }
397 
398  edge_storage_t& edge_storage(const twa_succ_iterator* it)
399  {
400  return g_.edge_storage(edge_number(it));
401  }
402 
403  edge_storage_t& edge_storage(unsigned t)
404  {
405  return g_.edge_storage(t);
406  }
407 
408  const edge_storage_t
409  edge_storage(const twa_succ_iterator* it) const
410  {
411  return g_.edge_storage(edge_number(it));
412  }
413 
414  const edge_storage_t edge_storage(unsigned t) const
415  {
416  return g_.edge_storage(t);
417  }
418 
419  unsigned new_state()
420  {
421  return g_.new_state();
422  }
423 
424  unsigned new_states(unsigned n)
425  {
426  return g_.new_states(n);
427  }
428 
429  unsigned new_edge(unsigned src, unsigned dst,
430  bdd cond,
431  acc_cond::mark_t acc = {})
432  {
433  return g_.new_edge(src, dst, cond, acc);
434  }
435 
436  unsigned new_acc_edge(unsigned src, unsigned dst,
437  bdd cond, bool acc = true)
438  {
439  if (acc)
440  return g_.new_edge(src, dst, cond, this->acc().all_sets());
441  else
442  return g_.new_edge(src, dst, cond);
443  }
444 
445  template<class I>
446  unsigned new_univ_edge(unsigned src, I begin, I end,
447  bdd cond,
448  acc_cond::mark_t acc = {})
449  {
450  return g_.new_univ_edge(src, begin, end, cond, acc);
451  }
452 
453  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
454  bdd cond,
455  acc_cond::mark_t acc = {})
456  {
457  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
458  }
459 
460 #ifndef SWIG
462  out(unsigned src) const
463  {
464  return g_.out(src);
465  }
466 #endif
467 
469  out(unsigned src)
470  {
471  return g_.out(src);
472  }
473 
475  univ_dests(unsigned d) const noexcept
476  {
477  return g_.univ_dests(d);
478  }
479 
481  univ_dests(const edge_storage_t& e) const noexcept
482  {
483  return g_.univ_dests(e);
484  }
485 
487  bool is_existential() const
488  {
489  return g_.is_existential();
490  }
491 
492 #ifndef SWIG
493  SPOT_DEPRECATED("use !is_existential() instead")
498  bool is_alternating() const
499  {
500  return !is_existential();
501  }
502 #endif
503 
504 #ifndef SWIG
505  auto states() const
506  SPOT_RETURN(g_.states());
507  auto states()
508  SPOT_RETURN(g_.states());
509 
511  edges() const noexcept
512  {
513  return g_.edges();
514  }
515 #endif
516 
518  edges() noexcept
519  {
520  return g_.edges();
521  }
522 
523 #ifndef SWIG
524  auto edge_vector() const
525  SPOT_RETURN(g_.edge_vector());
526  auto edge_vector()
527  SPOT_RETURN(g_.edge_vector());
528 
529  auto is_dead_edge(const graph_t::edge_storage_t& t) const
530  SPOT_RETURN(g_.is_dead_edge(t));
531 #endif
532 
549  void merge_edges();
550 
554  void merge_univ_dests();
555 
562  void merge_states();
563 
579  void purge_dead_states();
580 
598  typedef void (*shift_action)(const std::vector<unsigned>& newst,
599  void* action_data);
600  void purge_unreachable_states(shift_action* f = nullptr,
601  void* action_data = nullptr);
602 
607  void remove_unused_ap();
608 
615  void copy_state_names_from(const const_twa_graph_ptr& other);
616 
620  {
621  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
622  throw std::runtime_error
623  ("state_acc_sets() should only be called on "
624  "automata with state-based acceptance");
625  for (auto& t: g_.out(s))
626  // Stop at the first edge, since the remaining should be
627  // labeled identically.
628  return t.acc;
629  return {};
630  }
631 
638  bool state_is_accepting(unsigned s) const
639  {
640  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
641  throw std::runtime_error
642  ("state_is_accepting() should only be called on "
643  "automata with state-based acceptance");
644  for (auto& t: g_.out(s))
645  // Stop at the first edge, since the remaining should be
646  // labeled identically.
647  return acc().accepting(t.acc);
648  return false;
649  }
650 
651  bool state_is_accepting(const state* s) const
652  {
653  return state_is_accepting(state_number(s));
654  }
656 
657  bool operator==(const twa_graph& aut) const
658  {
659  auto& dests1 = g_.dests_vector();
660  auto& dests2 = aut.get_graph().dests_vector();
661  if (num_states() != aut.num_states() ||
662  num_edges() != aut.num_edges() ||
663  num_sets() != aut.num_sets() ||
664  dests1.size() != dests2.size())
665  return false;
666  auto& trans1 = edge_vector();
667  auto& trans2 = aut.edge_vector();
668  if (!std::equal(trans1.begin() + 1, trans1.end(),
669  trans2.begin() + 1))
670  return false;
671  return std::equal(dests1.begin(), dests1.end(),
672  dests2.begin());
673  }
674 
684  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
685  };
686 
689  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
690  {
691  return std::make_shared<twa_graph>(dict);
692  }
693 
696  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
697  twa::prop_set p)
698  {
699  return std::make_shared<twa_graph>(aut, p);
700  }
701 
704  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
705  twa::prop_set p)
706  {
707  return std::make_shared<twa_graph>(aut, p);
708  }
709 
719  SPOT_API twa_graph_ptr
720  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
721  bool preserve_names = false,
722  // parentheses for SWIG, see
723  // https://github.com/swig/swig/issues/993
724  unsigned max_states = -(1U));
725 }
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:73
bool state_is_accepting(const state *s) const
Tell if a state is accepting.
Definition: twagraph.hh:651
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:61
Definition: graph.hh:183
Definition: ngraph.hh:32
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:362
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:309
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:146
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:169
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:619
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:158
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:163
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:175
bool state_is_accepting(unsigned s) const
Tell if a state is accepting.
Definition: twagraph.hh:638
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:319
Iterator used by the on-the-fly interface of twa_graph.
Definition: twagraph.hh:125
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:190
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:930
virtual bool next() override
Jump to the next successor (if any).
Definition: twagraph.hh:152
Definition: acc.hh:55
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:487
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