spot  2.4.4
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2017 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 
32  struct SPOT_API twa_graph_state: public spot::state
33  {
34  public:
35  twa_graph_state() noexcept
36  {
37  }
38 
39  virtual ~twa_graph_state() noexcept
40  {
41  }
42 
43  virtual int compare(const spot::state* other) const override
44  {
45  auto o = down_cast<const twa_graph_state*>(other);
46 
47  // Do not simply return "other - this", it might not fit in an int.
48  if (o < this)
49  return -1;
50  if (o > this)
51  return 1;
52  return 0;
53  }
54 
55  virtual size_t hash() const override
56  {
57  return
58  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
59  }
60 
61  virtual twa_graph_state*
62  clone() const override
63  {
64  return const_cast<twa_graph_state*>(this);
65  }
66 
67  virtual void destroy() const override
68  {
69  }
70  };
71 
72  struct SPOT_API twa_graph_edge_data
73  {
74  bdd cond;
75  acc_cond::mark_t acc;
76 
77  explicit twa_graph_edge_data() noexcept
78  : cond(bddfalse), acc(0)
79  {
80  }
81 
82  twa_graph_edge_data(bdd cond, acc_cond::mark_t acc = 0U) noexcept
83  : cond(cond), acc(acc)
84  {
85  }
86 
87  bool operator<(const twa_graph_edge_data& other) const
88  {
89  if (cond.id() < other.cond.id())
90  return true;
91  if (cond.id() > other.cond.id())
92  return false;
93  return acc < other.acc;
94  }
95 
96  bool operator==(const twa_graph_edge_data& other) const
97  {
98  return cond.id() == other.cond.id() &&
99  acc == other.acc;
100  }
101  };
102 
103 
104  template<class Graph>
105  class SPOT_API twa_graph_succ_iterator final:
106  public twa_succ_iterator
107  {
108  private:
109  typedef typename Graph::edge edge;
110  typedef typename Graph::state_data_t state;
111  const Graph* g_;
112  edge t_;
113  edge p_;
114 
115  public:
116  twa_graph_succ_iterator(const Graph* g, edge t)
117  : g_(g), t_(t)
118  {
119  }
120 
121  void recycle(edge t)
122  {
123  t_ = t;
124  }
125 
126  virtual bool first() override
127  {
128  p_ = t_;
129  return p_;
130  }
131 
132  virtual bool next() override
133  {
134  p_ = g_->edge_storage(p_).next_succ;
135  return p_;
136  }
137 
138  virtual bool done() const override
139  {
140  return !p_;
141  }
142 
143  virtual const twa_graph_state* dst() const override
144  {
145  SPOT_ASSERT(!done());
146  return &g_->state_data(g_->edge_storage(p_).dst);
147  }
148 
149  virtual bdd cond() const override
150  {
151  SPOT_ASSERT(!done());
152  return g_->edge_data(p_).cond;
153  }
154 
155  virtual acc_cond::mark_t acc() const override
156  {
157  SPOT_ASSERT(!done());
158  return g_->edge_data(p_).acc;
159  }
160 
161  edge pos() const
162  {
163  return p_;
164  }
165 
166  };
167 
168  class SPOT_API twa_graph final: public twa
169  {
170  public:
172  // We avoid using graph_t::edge_storage_t because graph_t is not
173  // instantiated in the SWIG bindings, and SWIG would therefore
174  // handle graph_t::edge_storage_t as an abstract type.
175  typedef spot::internal::edge_storage<unsigned, unsigned, unsigned,
177  <twa_graph_edge_data, false>>
179  static_assert(std::is_same<typename graph_t::edge_storage_t,
180  edge_storage_t>::value, "type mismatch");
181  // We avoid using graph_t::state for the very same reason.
182  typedef unsigned state_num;
183  static_assert(std::is_same<typename graph_t::state, state_num>::value,
184  "type mismatch");
185 
186  protected:
187  graph_t g_;
188  mutable unsigned init_number_;
189 
190  public:
191  twa_graph(const bdd_dict_ptr& dict)
192  : twa(dict),
193  init_number_(0)
194  {
195  }
196 
197  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
198  : twa(other->get_dict()),
199  g_(other->g_), init_number_(other->init_number_)
200  {
201  copy_acceptance_of(other);
202  copy_ap_of(other);
203  prop_copy(other, p);
204  }
205 
206  virtual ~twa_graph()
207  {
208  }
209 
210 #ifndef SWIG
211  template <typename State_Name,
212  typename Name_Hash = std::hash<State_Name>,
213  typename Name_Equal = std::equal_to<State_Name>>
215 
216  template <typename State_Name,
217  typename Name_Hash = std::hash<State_Name>,
218  typename Name_Equal = std::equal_to<State_Name>>
220  create_namer()
221  {
223  }
224 
226  create_formula_namer()
227  {
228  return create_namer<formula>();
229  }
230 
231  void
232  release_formula_namer(namer<formula>* namer, bool keep_names);
233 #endif
234 
235  graph_t& get_graph()
236  {
237  return g_;
238  }
239 
240  const graph_t& get_graph() const
241  {
242  return g_;
243  }
244 
245  unsigned num_states() const
246  {
247  return g_.num_states();
248  }
249 
250  unsigned num_edges() const
251  {
252  return g_.num_edges();
253  }
254 
255  void set_init_state(state_num s)
256  {
257  if (SPOT_UNLIKELY(s >= num_states()))
258  throw std::invalid_argument
259  ("set_init_state() called with nonexisting state");
260  init_number_ = s;
261  }
262 
263  template<class I>
264  void set_univ_init_state(I dst_begin, I dst_end)
265  {
266  auto ns = num_states();
267  for (I i = dst_begin; i != dst_end; ++i)
268  if (SPOT_UNLIKELY(*i >= ns))
269  throw std::invalid_argument
270  ("set_univ_init_state() called with nonexisting state");
271  init_number_ = g_.new_univ_dests(dst_begin, dst_end);
272  }
273 
274  void set_univ_init_state(const std::initializer_list<state_num>& il)
275  {
276  set_univ_init_state(il.begin(), il.end());
277  }
278 
279  state_num get_init_state_number() const
280  {
281  // If the automaton has no state, it has no initial state.
282  if (num_states() == 0)
283  throw std::runtime_error("automaton has no state at all");
284  return init_number_;
285  }
286 
287  virtual const twa_graph_state* get_init_state() const override
288  {
289  unsigned n = get_init_state_number();
290  if (SPOT_UNLIKELY(!is_existential()))
291  throw std::runtime_error
292  ("the abstract interface does not support alternating automata");
293  return state_from_number(n);
294  }
295 
296  virtual twa_succ_iterator*
297  succ_iter(const state* st) const override
298  {
299  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
300  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
301 
302  if (this->iter_cache_)
303  {
304  auto it =
305  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
306  it->recycle(s->succ);
307  this->iter_cache_ = nullptr;
308  return it;
309  }
310  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
311  }
312 
313  static constexpr bool is_univ_dest(const edge_storage_t& e)
314  {
315  return is_univ_dest(e.dst);
316  }
317 
318  static constexpr bool is_univ_dest(unsigned s)
319  {
320  // Universal destinations are stored with their most-significant
321  // bit set.
322  return (int) s < 0;
323  }
324 
325  state_num
326  state_number(const state* st) const
327  {
328  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
329  return s - &g_.state_storage(0);
330  }
331 
332  const twa_graph_state*
333  state_from_number(state_num n) const
334  {
335  return &g_.state_data(n);
336  }
337 
338  std::string format_state(unsigned n) const;
339 
340  virtual std::string format_state(const state* st) const override
341  {
342  return format_state(state_number(st));
343  }
344 
345  unsigned edge_number(const twa_succ_iterator* it) const
346  {
347  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
348  return i->pos();
349  }
350 
351  unsigned edge_number(const edge_storage_t& e) const
352  {
353  return g_.index_of_edge(e);
354  }
355 
356  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
357  {
358  return g_.edge_data(edge_number(it));
359  }
360 
361  twa_graph_edge_data& edge_data(unsigned t)
362  {
363  return g_.edge_data(t);
364  }
365 
366  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
367  {
368  return g_.edge_data(edge_number(it));
369  }
370 
371  const twa_graph_edge_data& edge_data(unsigned t) const
372  {
373  return g_.edge_data(t);
374  }
375 
376  edge_storage_t& edge_storage(const twa_succ_iterator* it)
377  {
378  return g_.edge_storage(edge_number(it));
379  }
380 
381  edge_storage_t& edge_storage(unsigned t)
382  {
383  return g_.edge_storage(t);
384  }
385 
386  const edge_storage_t
387  edge_storage(const twa_succ_iterator* it) const
388  {
389  return g_.edge_storage(edge_number(it));
390  }
391 
392  const edge_storage_t edge_storage(unsigned t) const
393  {
394  return g_.edge_storage(t);
395  }
396 
397  unsigned new_state()
398  {
399  return g_.new_state();
400  }
401 
402  unsigned new_states(unsigned n)
403  {
404  return g_.new_states(n);
405  }
406 
407  unsigned new_edge(unsigned src, unsigned dst,
408  bdd cond, acc_cond::mark_t acc = 0U)
409  {
410  return g_.new_edge(src, dst, cond, acc);
411  }
412 
413  unsigned new_acc_edge(unsigned src, unsigned dst,
414  bdd cond, bool acc = true)
415  {
416  if (acc)
417  return g_.new_edge(src, dst, cond, this->acc().all_sets());
418  else
419  return g_.new_edge(src, dst, cond);
420  }
421 
422  template<class I>
423  unsigned new_univ_edge(unsigned src, I begin, I end,
424  bdd cond, acc_cond::mark_t acc = 0U)
425  {
426  return g_.new_univ_edge(src, begin, end, cond, acc);
427  }
428 
429  unsigned new_univ_edge(unsigned src, std::initializer_list<unsigned> dst,
430  bdd cond, acc_cond::mark_t acc = 0U)
431  {
432  return g_.new_univ_edge(src, dst.begin(), dst.end(), cond, acc);
433  }
434 
435 #ifndef SWIG
437  out(unsigned src) const
438  {
439  return g_.out(src);
440  }
441 #endif
442 
444  out(unsigned src)
445  {
446  return g_.out(src);
447  }
448 
450  univ_dests(unsigned d) const noexcept
451  {
452  return g_.univ_dests(d);
453  }
454 
456  univ_dests(const edge_storage_t& e) const noexcept
457  {
458  return g_.univ_dests(e);
459  }
460 
462  bool is_existential() const
463  {
464  return g_.is_existential();
465  }
466 
467 #ifndef SWIG
468  SPOT_DEPRECATED("use !is_existential() instead")
473  bool is_alternating() const
474  {
475  return !is_existential();
476  }
477 #endif
478 
479 #ifndef SWIG
480  auto states() const
481  SPOT_RETURN(g_.states());
482  auto states()
483  SPOT_RETURN(g_.states());
484 
486  edges() const noexcept
487  {
488  return g_.edges();
489  }
490 #endif
491 
493  edges() noexcept
494  {
495  return g_.edges();
496  }
497 
498 #ifndef SWIG
499  auto edge_vector() const
500  SPOT_RETURN(g_.edge_vector());
501  auto edge_vector()
502  SPOT_RETURN(g_.edge_vector());
503 
504  auto is_dead_edge(const graph_t::edge_storage_t& t) const
505  SPOT_RETURN(g_.is_dead_edge(t));
506 #endif
507 
510  void merge_edges();
511 
515  void merge_univ_dests();
516 
532  void purge_dead_states();
533 
546  void purge_unreachable_states();
547 
552  void remove_unused_ap();
553 
560  void copy_state_names_from(const const_twa_graph_ptr& other);
561 
562  acc_cond::mark_t state_acc_sets(unsigned s) const
563  {
564  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
565  throw std::runtime_error
566  ("state_acc_sets() should only be called on "
567  "automata with state-based acceptance");
568  for (auto& t: g_.out(s))
569  // Stop at the first edge, since the remaining should be
570  // labeled identically.
571  return t.acc;
572  return 0U;
573  }
574 
575  bool state_is_accepting(unsigned s) const
576  {
577  if (SPOT_UNLIKELY(!(bool)prop_state_acc()))
578  throw std::runtime_error
579  ("state_is_accepting() should only be called on "
580  "automata with state-based acceptance");
581  for (auto& t: g_.out(s))
582  // Stop at the first edge, since the remaining should be
583  // labeled identically.
584  return acc().accepting(t.acc);
585  return false;
586  }
587 
588  bool state_is_accepting(const state* s) const
589  {
590  return state_is_accepting(state_number(s));
591  }
592 
593  bool operator==(const twa_graph& aut) const
594  {
595  auto& dests1 = g_.dests_vector();
596  auto& dests2 = aut.get_graph().dests_vector();
597  if (num_states() != aut.num_states() ||
598  num_edges() != aut.num_edges() ||
599  num_sets() != aut.num_sets() ||
600  dests1.size() != dests2.size())
601  return false;
602  auto& trans1 = edge_vector();
603  auto& trans2 = aut.edge_vector();
604  if (!std::equal(trans1.begin() + 1, trans1.end(),
605  trans2.begin() + 1))
606  return false;
607  return std::equal(dests1.begin(), dests1.end(),
608  dests2.begin());
609  }
610 
611  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states);
612  };
613 
616  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
617  {
618  return std::make_shared<twa_graph>(dict);
619  }
620 
623  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
624  twa::prop_set p)
625  {
626  return std::make_shared<twa_graph>(aut, p);
627  }
628 
631  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
632  twa::prop_set p)
633  {
634  return std::make_shared<twa_graph>(aut, p);
635  }
636 
646  SPOT_API twa_graph_ptr
647  make_twa_graph(const const_twa_ptr& aut, twa::prop_set p,
648  bool preserve_names = false,
649  // parentheses for SWIG, see
650  // https://github.com/swig/swig/issues/993
651  unsigned max_states = -(1U));
652 }
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:67
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
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,.
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:55
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:340
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:287
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:126
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:149
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:138
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: twagraph.hh:43
virtual const twa_graph_state * dst() const override
Get the destination state of the current edge.
Definition: twagraph.hh:143
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:155
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:297
Definition: twagraph.hh:105
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:809
Definition: graph.hh:385
Definition: twagraph.hh:168
Definition: twagraph.hh:32
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:132
Definition: acc.hh:39
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: twagraph.hh:462
Definition: twagraph.hh:72
virtual twa_graph_state * clone() const override
Duplicate a state.
Definition: twagraph.hh:62

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Dec 25 2017 14:51:14 for spot by doxygen 1.8.13