spot  1.99.1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
twagraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014, 2015 Laboratoire de Recherche et Développement
3 // de l'Epita.
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include "fwd.hh"
23 #include "graph/graph.hh"
24 #include "graph/ngraph.hh"
25 #include "twa/bdddict.hh"
26 #include "twa/twa.hh"
27 #include "twaalgos/dupexp.hh"
28 #include "ltlast/formula.hh"
29 #include <sstream>
30 
31 namespace spot
32 {
33 
34  struct SPOT_API twa_graph_state: public spot::state
35  {
36  public:
38  spot::state()
39  {
40  }
41 
42  virtual ~twa_graph_state() noexcept
43  {
44  }
45 
46  virtual int compare(const spot::state* other) const
47  {
48  auto o = down_cast<const twa_graph_state*>(other);
49  assert(o);
50 
51  // Do not simply return "other - this", it might not fit in an int.
52  if (o < this)
53  return -1;
54  if (o > this)
55  return 1;
56  return 0;
57  }
58 
59  virtual size_t hash() const
60  {
61  return
62  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
63  }
64 
65  virtual twa_graph_state*
66  clone() const
67  {
68  return const_cast<twa_graph_state*>(this);
69  }
70 
71  virtual void destroy() const
72  {
73  }
74  };
75 
76  struct SPOT_API twa_graph_edge_data
77  {
78  bdd cond;
79  acc_cond::mark_t acc;
80 
81  explicit twa_graph_edge_data()
82  : cond(bddfalse), acc(0)
83  {
84  }
85 
86  twa_graph_edge_data(bdd cond, acc_cond::mark_t acc = 0U)
87  : cond(cond), acc(acc)
88  {
89  }
90 
91  bool operator<(const twa_graph_edge_data& other) const
92  {
93  if (cond.id() < other.cond.id())
94  return true;
95  if (cond.id() > other.cond.id())
96  return false;
97  return acc < other.acc;
98  }
99 
100  bool operator==(const twa_graph_edge_data& other) const
101  {
102  return cond.id() == other.cond.id() &&
103  acc == other.acc;
104  }
105  };
106 
107 
108  template<class Graph>
109  class SPOT_API twa_graph_succ_iterator final:
110  public twa_succ_iterator
111  {
112  private:
113  typedef typename Graph::edge edge;
114  typedef typename Graph::state_data_t state;
115  const Graph* g_;
116  edge t_;
117  edge p_;
118 
119  public:
120  twa_graph_succ_iterator(const Graph* g, edge t)
121  : g_(g), t_(t)
122  {
123  }
124 
125  virtual void recycle(edge t)
126  {
127  t_ = t;
128  }
129 
130  virtual bool first()
131  {
132  p_ = t_;
133  return p_;
134  }
135 
136  virtual bool next()
137  {
138  p_ = g_->edge_storage(p_).next_succ;
139  return p_;
140  }
141 
142  virtual bool done() const
143  {
144  return !p_;
145  }
146 
148  {
149  assert(!done());
150  return const_cast<twa_graph_state*>
151  (&g_->state_data(g_->edge_storage(p_).dst));
152  }
153 
154  virtual bdd current_condition() const
155  {
156  assert(!done());
157  return g_->edge_data(p_).cond;
158  }
159 
161  {
162  assert(!done());
163  return g_->edge_data(p_).acc;
164  }
165 
166  edge pos() const
167  {
168  return p_;
169  }
170 
171  };
172 
173  class SPOT_API twa_graph final: public twa
174  {
175  public:
178 
179  protected:
180  graph_t g_;
181  mutable unsigned init_number_;
182 
183  public:
184  twa_graph(const bdd_dict_ptr& dict)
185  : twa(dict),
186  init_number_(0)
187  {
188  }
189 
190  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
191  : twa(other->get_dict()),
192  g_(other->g_), init_number_(other->init_number_)
193  {
194  copy_acceptance_of(other);
195  copy_ap_of(other);
196  prop_copy(other, p);
197  }
198 
199  virtual ~twa_graph()
200  {
201  get_dict()->unregister_all_my_variables(this);
202  // Prevent this state from being destroyed by ~twa(),
203  // as the state will be destroyed when g_ is destroyed.
204  last_support_conditions_input_ = 0;
205  }
206 
207  // FIXME: Once we ditch GCC 4.6, we can using a template aliases
208  // (supported from GCC 4.7 onward) instead of this.
209  template <typename State_Name,
210  typename Name_Hash = std::hash<State_Name>,
211  typename Name_Equal = std::equal_to<State_Name>>
212  struct namer
213  {
215  };
216 
217  template <typename State_Name,
218  typename Name_Hash = std::hash<State_Name>,
219  typename Name_Equal = std::equal_to<State_Name>>
221  create_namer()
222  {
224  }
225 
226  typename namer<const ltl::formula*>::type*
227  create_formula_namer()
228  {
229  return create_namer<const ltl::formula*>();
230  }
231 
232  void
233  release_formula_namer(typename namer<const ltl::formula*>::type* namer,
234  bool keep_names);
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(graph_t::state s)
257  {
258  assert(s < num_states());
259  init_number_ = s;
260  }
261 
262  void set_init_state(const state* s)
263  {
264  set_init_state(state_number(s));
265  }
266 
267  graph_t::state get_init_state_number() const
268  {
269  if (num_states() == 0)
270  const_cast<graph_t&>(g_).new_state();
271  return init_number_;
272  }
273 
274  // FIXME: The return type ought to be const.
276  {
277  if (num_states() == 0)
278  const_cast<graph_t&>(g_).new_state();
279  return const_cast<twa_graph_state*>(state_from_number(init_number_));
280  }
281 
282  virtual twa_succ_iterator*
283  succ_iter(const state* st) const
284  {
285  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
286  assert(s);
287  assert(!s->succ || g_.valid_trans(s->succ));
288 
289  if (this->iter_cache_)
290  {
291  auto it =
292  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
293  it->recycle(s->succ);
294  this->iter_cache_ = nullptr;
295  return it;
296  }
297  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
298  }
299 
300  graph_t::state
301  state_number(const state* st) const
302  {
303  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
304  assert(s);
305  return s - &g_.state_storage(0);
306  }
307 
308  const twa_graph_state*
309  state_from_number(graph_t::state n) const
310  {
311  return &g_.state_data(n);
312  }
313 
314  std::string format_state(unsigned n) const
315  {
316  std::stringstream ss;
317  ss << n;
318  return ss.str();
319  }
320 
321  virtual std::string format_state(const state* st) const
322  {
323  return format_state(state_number(st));
324  }
325 
326  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
327  {
328  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
329  return g_.edge_data(i->pos());
330  }
331 
332  twa_graph_edge_data& edge_data(unsigned t)
333  {
334  return g_.edge_data(t);
335  }
336 
337  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
338  {
339  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
340  return g_.edge_data(i->pos());
341  }
342 
343  const twa_graph_edge_data& edge_data(unsigned t) const
344  {
345  return g_.edge_data(t);
346  }
347 
348  edge_storage_t& edge_storage(const twa_succ_iterator* it)
349  {
350  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
351  return g_.edge_storage(i->pos());
352  }
353 
354  edge_storage_t& edge_storage(unsigned t)
355  {
356  return g_.edge_storage(t);
357  }
358 
359  const edge_storage_t
360  edge_storage(const twa_succ_iterator* it) const
361  {
362  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
363  return g_.edge_storage(i->pos());
364  }
365 
366  const edge_storage_t edge_storage(unsigned t) const
367  {
368  return g_.edge_storage(t);
369  }
370 
371  unsigned new_state()
372  {
373  return g_.new_state();
374  }
375 
376  unsigned new_states(unsigned n)
377  {
378  return g_.new_states(n);
379  }
380 
381  unsigned new_edge(unsigned src, unsigned dst,
382  bdd cond, acc_cond::mark_t acc = 0U)
383  {
384  return g_.new_edge(src, dst, cond, acc);
385  }
386 
387  unsigned new_acc_edge(unsigned src, unsigned dst,
388  bdd cond, bool acc = true)
389  {
390  if (acc)
391  return g_.new_edge(src, dst, cond, acc_.all_sets());
392  else
393  return g_.new_edge(src, dst, cond);
394  }
395 
396 #ifndef SWIG
397  auto out(unsigned src) const
398  SPOT_RETURN(g_.out(src));
399  auto out(unsigned src)
400  SPOT_RETURN(g_.out(src));
401 
402  auto states() const
403  SPOT_RETURN(g_.states());
404  auto states()
405  SPOT_RETURN(g_.states());
406 
407  auto edges() const
408  SPOT_RETURN(g_.edges());
409  auto edges()
410  SPOT_RETURN(g_.edges());
411 
412  auto edge_vector() const
413  SPOT_RETURN(g_.edge_vector());
414  auto edge_vector()
415  SPOT_RETURN(g_.edge_vector());
416 
417  auto is_dead_edge(const graph_t::edge_storage_t& t) const
418  SPOT_RETURN(g_.is_dead_edge(t));
419 #endif
420 
421  virtual bdd compute_support_conditions(const state* s) const
422  {
423  bdd sum = bddfalse;
424  for (auto& t: out(state_number(s)))
425  sum |= t.cond;
426  return sum;
427  }
428 
431  void merge_edges();
432 
434  void purge_dead_states();
435 
437  void purge_unreachable_states();
438 
439  acc_cond::mark_t state_acc_sets(unsigned s) const
440  {
441  assert(has_state_based_acc() || num_sets() == 0);
442  for (auto& t: g_.out(s))
443  // Stop at the first edge, since the remaining should be
444  // labeled identically.
445  return t.acc;
446  return 0U;
447  }
448 
449  bool state_is_accepting(unsigned s) const
450  {
451  assert(has_state_based_acc() || num_sets() == 0);
452  for (auto& t: g_.out(s))
453  // Stop at the first edge, since the remaining should be
454  // labeled identically.
455  return acc_.accepting(t.acc);
456  return false;
457  }
458 
459  bool state_is_accepting(const state* s) const
460  {
461  return state_is_accepting(state_number(s));
462  }
463 
464  bool operator==(const twa_graph& aut) const
465  {
466  if (num_states() != aut.num_states() ||
467  num_edges() != aut.num_edges() ||
468  num_sets() != aut.num_sets())
469  return false;
470  auto& trans1 = edge_vector();
471  auto& trans2 = aut.edge_vector();
472  return std::equal(trans1.begin() + 1, trans1.end(),
473  trans2.begin() + 1);
474  }
475  };
476 
477  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
478  {
479  return std::make_shared<twa_graph>(dict);
480  }
481 
482  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
483  twa::prop_set p)
484  {
485  return std::make_shared<twa_graph>(aut, p);
486  }
487 
488  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
489  twa::prop_set p)
490  {
491  return std::make_shared<twa_graph>(aut, p);
492  }
493 
494  inline twa_graph_ptr make_twa_graph(const const_twa_ptr& aut,
495  twa::prop_set p)
496  {
497  auto a = std::dynamic_pointer_cast<const twa_graph>(aut);
498  if (a)
499  return std::make_shared<twa_graph>(a, p);
500  else
501  return tgba_dupexp_dfs(aut, p);
502  }
503 }
Definition: public.hh:31
Definition: twagraph.hh:212
virtual twa_graph_state * clone() const
Duplicate a state.
Definition: twagraph.hh:66
virtual bool done() const
Check whether the iteration is finished.
Definition: twagraph.hh:142
virtual int compare(const spot::state *other) const
Compares two states (that come from the same automaton).
Definition: twagraph.hh:46
virtual twa_succ_iterator * succ_iter(const state *st) const
Get an iterator over the successors of local_state.
Definition: twagraph.hh:283
A Transition-based ω-Automaton.
Definition: twa.hh:480
LTL formula interface.
Abstract class for states.
Definition: twa.hh:40
virtual bool first()
Position the iterator on the first successor (if any).
Definition: twagraph.hh:130
Definition: graph.hh:171
virtual size_t hash() const
Hash a state.
Definition: twagraph.hh:59
virtual twa_graph_state * current_state() const
Get the state of the current successor.
Definition: twagraph.hh:147
Definition: twa.hh:836
Definition: ngraph.hh:32
virtual bdd current_condition() const
Get the condition on the transition leading to this successor.
Definition: twagraph.hh:154
Definition: graph.hh:148
virtual bool next()
Jump to the next successor (if any).
Definition: twagraph.hh:136
Iterate over the successors of a state.
Definition: twa.hh:329
virtual twa_graph_state * get_init_state() const
Get the initial state of the automaton.
Definition: twagraph.hh:275
virtual void destroy() const
Release a state.
Definition: twagraph.hh:71
SPOT_API twa_graph_ptr tgba_dupexp_dfs(const const_twa_ptr &aut, twa::prop_set p)
Build an explicit automaton from all states of aut, numbering states in depth first order as they are...
Definition: twagraph.hh:109
Definition: twagraph.hh:173
Definition: twagraph.hh:34
virtual acc_cond::mark_t current_acceptance_conditions() const
Get the acceptance conditions on the transition leading to this successor.
Definition: twagraph.hh:160
Definition: acc.hh:34
Definition: twagraph.hh:76

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Tue Jun 23 2015 06:55:46 for spot by doxygen 1.8.8