spot  1.99.6
 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/copy.hh"
28 #include "tl/formula.hh"
29 #include <sstream>
30 
31 namespace spot
32 {
33 
34  struct SPOT_API twa_graph_state: public spot::state
35  {
36  public:
37  twa_graph_state() noexcept
38  {
39  }
40 
41  virtual ~twa_graph_state() noexcept
42  {
43  }
44 
45  virtual int compare(const spot::state* other) const
46  {
47  auto o = down_cast<const twa_graph_state*>(other);
48  assert(o);
49 
50  // Do not simply return "other - this", it might not fit in an int.
51  if (o < this)
52  return -1;
53  if (o > this)
54  return 1;
55  return 0;
56  }
57 
58  virtual size_t hash() const
59  {
60  return
61  reinterpret_cast<const char*>(this) - static_cast<const char*>(nullptr);
62  }
63 
64  virtual twa_graph_state*
65  clone() const
66  {
67  return const_cast<twa_graph_state*>(this);
68  }
69 
70  virtual void destroy() const
71  {
72  }
73  };
74 
75  struct SPOT_API twa_graph_edge_data
76  {
77  bdd cond;
78  acc_cond::mark_t acc;
79 
80  explicit twa_graph_edge_data() noexcept
81  : cond(bddfalse), acc(0)
82  {
83  }
84 
85  twa_graph_edge_data(bdd cond, acc_cond::mark_t acc = 0U) noexcept
86  : cond(cond), acc(acc)
87  {
88  }
89 
90  bool operator<(const twa_graph_edge_data& other) const
91  {
92  if (cond.id() < other.cond.id())
93  return true;
94  if (cond.id() > other.cond.id())
95  return false;
96  return acc < other.acc;
97  }
98 
99  bool operator==(const twa_graph_edge_data& other) const
100  {
101  return cond.id() == other.cond.id() &&
102  acc == other.acc;
103  }
104  };
105 
106 
107  template<class Graph>
108  class SPOT_API twa_graph_succ_iterator final:
109  public twa_succ_iterator
110  {
111  private:
112  typedef typename Graph::edge edge;
113  typedef typename Graph::state_data_t state;
114  const Graph* g_;
115  edge t_;
116  edge p_;
117 
118  public:
119  twa_graph_succ_iterator(const Graph* g, edge t)
120  : g_(g), t_(t)
121  {
122  }
123 
124  virtual void recycle(edge t)
125  {
126  t_ = t;
127  }
128 
129  virtual bool first()
130  {
131  p_ = t_;
132  return p_;
133  }
134 
135  virtual bool next()
136  {
137  p_ = g_->edge_storage(p_).next_succ;
138  return p_;
139  }
140 
141  virtual bool done() const
142  {
143  return !p_;
144  }
145 
146  virtual const twa_graph_state* dst() const
147  {
148  assert(!done());
149  return &g_->state_data(g_->edge_storage(p_).dst);
150  }
151 
152  virtual bdd cond() const
153  {
154  assert(!done());
155  return g_->edge_data(p_).cond;
156  }
157 
158  virtual acc_cond::mark_t acc() const
159  {
160  assert(!done());
161  return g_->edge_data(p_).acc;
162  }
163 
164  edge pos() const
165  {
166  return p_;
167  }
168 
169  };
170 
171  class SPOT_API twa_graph final: public twa
172  {
173  public:
176 
177  protected:
178  graph_t g_;
179  mutable unsigned init_number_;
180 
181  public:
182  twa_graph(const bdd_dict_ptr& dict)
183  : twa(dict),
184  init_number_(0)
185  {
186  }
187 
188  explicit twa_graph(const const_twa_graph_ptr& other, prop_set p)
189  : twa(other->get_dict()),
190  g_(other->g_), init_number_(other->init_number_)
191  {
192  copy_acceptance_of(other);
193  copy_ap_of(other);
194  prop_copy(other, p);
195  }
196 
197  virtual ~twa_graph()
198  {
199  get_dict()->unregister_all_my_variables(this);
200  // Prevent this state from being destroyed by ~twa(),
201  // as the state will be destroyed when g_ is destroyed.
202  last_support_conditions_input_ = nullptr;
203  }
204 
205 #ifndef SWIG
206  template <typename State_Name,
207  typename Name_Hash = std::hash<State_Name>,
208  typename Name_Equal = std::equal_to<State_Name>>
210 
211  template <typename State_Name,
212  typename Name_Hash = std::hash<State_Name>,
213  typename Name_Equal = std::equal_to<State_Name>>
215  create_namer()
216  {
218  }
219 
221  create_formula_namer()
222  {
223  return create_namer<formula>();
224  }
225 
226  void
227  release_formula_namer(namer<formula>* namer, bool keep_names);
228 #endif
229 
230  graph_t& get_graph()
231  {
232  return g_;
233  }
234 
235  const graph_t& get_graph() const
236  {
237  return g_;
238  }
239 
240  unsigned num_states() const
241  {
242  return g_.num_states();
243  }
244 
245  unsigned num_edges() const
246  {
247  return g_.num_edges();
248  }
249 
250  void set_init_state(graph_t::state s)
251  {
252  assert(s < num_states());
253  init_number_ = s;
254  }
255 
256  void set_init_state(const state* s)
257  {
258  set_init_state(state_number(s));
259  }
260 
261  graph_t::state get_init_state_number() const
262  {
263  if (num_states() == 0)
264  const_cast<graph_t&>(g_).new_state();
265  return init_number_;
266  }
267 
268  virtual const twa_graph_state* get_init_state() const
269  {
270  if (num_states() == 0)
271  const_cast<graph_t&>(g_).new_state();
272  return state_from_number(init_number_);
273  }
274 
275  virtual twa_succ_iterator*
276  succ_iter(const state* st) const
277  {
278  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
279  assert(s);
280  assert(!s->succ || g_.valid_trans(s->succ));
281 
282  if (this->iter_cache_)
283  {
284  auto it =
285  down_cast<twa_graph_succ_iterator<graph_t>*>(this->iter_cache_);
286  it->recycle(s->succ);
287  this->iter_cache_ = nullptr;
288  return it;
289  }
290  return new twa_graph_succ_iterator<graph_t>(&g_, s->succ);
291  }
292 
293  graph_t::state
294  state_number(const state* st) const
295  {
296  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
297  assert(s);
298  return s - &g_.state_storage(0);
299  }
300 
301  const twa_graph_state*
302  state_from_number(graph_t::state n) const
303  {
304  return &g_.state_data(n);
305  }
306 
307  std::string format_state(unsigned n) const
308  {
309  std::stringstream ss;
310  ss << n;
311  return ss.str();
312  }
313 
314  virtual std::string format_state(const state* st) const
315  {
316  return format_state(state_number(st));
317  }
318 
319  twa_graph_edge_data& edge_data(const twa_succ_iterator* it)
320  {
321  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
322  return g_.edge_data(i->pos());
323  }
324 
325  twa_graph_edge_data& edge_data(unsigned t)
326  {
327  return g_.edge_data(t);
328  }
329 
330  const twa_graph_edge_data& edge_data(const twa_succ_iterator* it) const
331  {
332  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
333  return g_.edge_data(i->pos());
334  }
335 
336  const twa_graph_edge_data& edge_data(unsigned t) const
337  {
338  return g_.edge_data(t);
339  }
340 
341  edge_storage_t& edge_storage(const twa_succ_iterator* it)
342  {
343  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
344  return g_.edge_storage(i->pos());
345  }
346 
347  edge_storage_t& edge_storage(unsigned t)
348  {
349  return g_.edge_storage(t);
350  }
351 
352  const edge_storage_t
353  edge_storage(const twa_succ_iterator* it) const
354  {
355  auto* i = down_cast<const twa_graph_succ_iterator<graph_t>*>(it);
356  return g_.edge_storage(i->pos());
357  }
358 
359  const edge_storage_t edge_storage(unsigned t) const
360  {
361  return g_.edge_storage(t);
362  }
363 
364  unsigned new_state()
365  {
366  return g_.new_state();
367  }
368 
369  unsigned new_states(unsigned n)
370  {
371  return g_.new_states(n);
372  }
373 
374  unsigned new_edge(unsigned src, unsigned dst,
375  bdd cond, acc_cond::mark_t acc = 0U)
376  {
377  return g_.new_edge(src, dst, cond, acc);
378  }
379 
380  unsigned new_acc_edge(unsigned src, unsigned dst,
381  bdd cond, bool acc = true)
382  {
383  if (acc)
384  return g_.new_edge(src, dst, cond, acc_.all_sets());
385  else
386  return g_.new_edge(src, dst, cond);
387  }
388 
389 #ifndef SWIG
390  auto out(unsigned src) const
391  SPOT_RETURN(g_.out(src));
392  auto out(unsigned src)
393  SPOT_RETURN(g_.out(src));
394 
395  auto states() const
396  SPOT_RETURN(g_.states());
397  auto states()
398  SPOT_RETURN(g_.states());
399 
400  auto edges() const
401  SPOT_RETURN(g_.edges());
402  auto edges()
403  SPOT_RETURN(g_.edges());
404 
405  auto edge_vector() const
406  SPOT_RETURN(g_.edge_vector());
407  auto edge_vector()
408  SPOT_RETURN(g_.edge_vector());
409 
410  auto is_dead_edge(const graph_t::edge_storage_t& t) const
411  SPOT_RETURN(g_.is_dead_edge(t));
412 #endif
413 
414  virtual bdd compute_support_conditions(const state* s) const
415  {
416  bdd sum = bddfalse;
417  for (auto& t: out(state_number(s)))
418  sum |= t.cond;
419  return sum;
420  }
421 
424  void merge_edges();
425 
427  void purge_dead_states();
428 
430  void purge_unreachable_states();
431 
432  acc_cond::mark_t state_acc_sets(unsigned s) const
433  {
434  assert(prop_state_acc() || num_sets() == 0);
435  for (auto& t: g_.out(s))
436  // Stop at the first edge, since the remaining should be
437  // labeled identically.
438  return t.acc;
439  return 0U;
440  }
441 
442  bool state_is_accepting(unsigned s) const
443  {
444  assert(prop_state_acc() || num_sets() == 0);
445  for (auto& t: g_.out(s))
446  // Stop at the first edge, since the remaining should be
447  // labeled identically.
448  return acc_.accepting(t.acc);
449  return false;
450  }
451 
452  bool state_is_accepting(const state* s) const
453  {
454  return state_is_accepting(state_number(s));
455  }
456 
457  bool operator==(const twa_graph& aut) const
458  {
459  if (num_states() != aut.num_states() ||
460  num_edges() != aut.num_edges() ||
461  num_sets() != aut.num_sets())
462  return false;
463  auto& trans1 = edge_vector();
464  auto& trans2 = aut.edge_vector();
465  return std::equal(trans1.begin() + 1, trans1.end(),
466  trans2.begin() + 1);
467  }
468  };
469 
470  inline twa_graph_ptr make_twa_graph(const bdd_dict_ptr& dict)
471  {
472  return std::make_shared<twa_graph>(dict);
473  }
474 
475  inline twa_graph_ptr make_twa_graph(const twa_graph_ptr& aut,
476  twa::prop_set p)
477  {
478  return std::make_shared<twa_graph>(aut, p);
479  }
480 
481  inline twa_graph_ptr make_twa_graph(const const_twa_graph_ptr& aut,
482  twa::prop_set p)
483  {
484  return std::make_shared<twa_graph>(aut, p);
485  }
486 
487  inline twa_graph_ptr make_twa_graph(const const_twa_ptr& aut,
488  twa::prop_set p)
489  {
490  auto a = std::dynamic_pointer_cast<const twa_graph>(aut);
491  if (a)
492  return std::make_shared<twa_graph>(a, p);
493  else
494  return copy(aut, p);
495  }
496 }
Definition: graph.hh:32
virtual twa_graph_state * clone() const
Duplicate a state.
Definition: twagraph.hh:65
virtual bool done() const
Check whether the iteration is finished.
Definition: twagraph.hh:141
virtual int compare(const spot::state *other) const
Compares two states (that come from the same automaton).
Definition: twagraph.hh:45
virtual twa_succ_iterator * succ_iter(const state *st) const
Get an iterator over the successors of local_state.
Definition: twagraph.hh:276
A Transition-based ω-Automaton.
Definition: twa.hh:482
LTL/PSL formula interface.
Abstract class for states.
Definition: twa.hh:42
virtual bool first()
Position the iterator on the first successor (if any).
Definition: twagraph.hh:129
Definition: graph.hh:177
virtual size_t hash() const
Hash a state.
Definition: twagraph.hh:58
Definition: twa.hh:910
Definition: ngraph.hh:32
virtual acc_cond::mark_t acc() const
Get the acceptance conditions on the transition leading to this successor.
Definition: twagraph.hh:158
Definition: graph.hh:153
virtual const twa_graph_state * dst() const
Get the state of the current successor.
Definition: twagraph.hh:146
virtual bool next()
Jump to the next successor (if any).
Definition: twagraph.hh:135
Iterate over the successors of a state.
Definition: twa.hh:331
virtual void destroy() const
Release a state.
Definition: twagraph.hh:70
virtual const twa_graph_state * get_init_state() const
Get the initial state of the automaton.
Definition: twagraph.hh:268
Definition: twagraph.hh:108
virtual bdd cond() const
Get the condition on the transition leading to this successor.
Definition: twagraph.hh:152
Definition: twagraph.hh:171
Definition: twagraph.hh:34
Definition: acc.hh:34
Definition: twagraph.hh:75
twa_graph_ptr copy(const const_twa_ptr &aut, twa::prop_set p, bool preserve_names=false)
Build an explicit automaton from all states of aut,.

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Dec 4 2015 07:31:52 for spot by doxygen 1.8.8