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

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Mon Feb 20 2017 07:08:25 for spot by doxygen 1.8.8