spot  2.4
sccinfo.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2017 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
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 <vector>
23 #include <spot/twa/twagraph.hh>
24 
25 namespace spot
26 {
27  class scc_info;
28 
29  namespace internal
30  {
31  struct keep_all
32  {
33  template <typename Iterator>
34  bool operator()(Iterator, Iterator) const noexcept
35  {
36  return true;
37  }
38  };
39 
40  // Keep only transitions that have at least one destination in the
41  // current SCC.
43  {
44  private:
45  const std::vector<unsigned>& sccof_;
46  unsigned desired_scc_;
47  public:
48  keep_inner_scc(const std::vector<unsigned>& sccof, unsigned desired_scc)
49  : sccof_(sccof), desired_scc_(desired_scc)
50  {
51  }
52 
53  template <typename Iterator>
54  bool operator()(Iterator begin, Iterator end) const noexcept
55  {
56  bool want = false;
57  while (begin != end)
58  if (sccof_[*begin++] == desired_scc_)
59  {
60  want = true;
61  break;
62  }
63  return want;
64  }
65  };
66 
67  template <typename Graph, typename Filter>
68  class SPOT_API scc_edge_iterator
69  {
70  public:
71  typedef typename std::conditional<std::is_const<Graph>::value,
72  const typename Graph::edge_storage_t,
73  typename Graph::edge_storage_t>::type
74  value_type;
75  typedef value_type& reference;
76  typedef value_type* pointer;
77  typedef std::ptrdiff_t difference_type;
78  typedef std::forward_iterator_tag iterator_category;
79 
80  typedef std::vector<unsigned>::const_iterator state_iterator;
81 
82  typedef typename std::conditional<std::is_const<Graph>::value,
83  const typename Graph::edge_vector_t,
84  typename Graph::edge_vector_t>::type
85  tv_t;
86 
87  typedef typename std::conditional<std::is_const<Graph>::value,
88  const typename Graph::state_vector,
89  typename Graph::state_vector>::type
90  sv_t;
91  typedef const typename Graph::dests_vector_t dv_t;
92  protected:
93 
94  state_iterator pos_;
95  state_iterator end_;
96  unsigned t_;
97  tv_t* tv_;
98  sv_t* sv_;
99  dv_t* dv_;
100 
101  Filter filt_;
102 
103  void inc_state_maybe_()
104  {
105  while (!t_ && (++pos_ != end_))
106  t_ = (*sv_)[*pos_].succ;
107  }
108 
109  void inc_()
110  {
111  t_ = (*tv_)[t_].next_succ;
112  inc_state_maybe_();
113  }
114 
115  bool ignore_current()
116  {
117  unsigned dst = (*this)->dst;
118  if ((int)dst >= 0)
119  // Non-universal branching => a single destination.
120  return !filt_(&(*this)->dst, 1 + &(*this)->dst);
121  // Universal branching => multiple destinations.
122  const unsigned* d = dv_->data() + ~dst;
123  return !filt_(d + 1, d + *d + 1);
124  }
125 
126  public:
127  scc_edge_iterator(state_iterator begin, state_iterator end,
128  tv_t* tv, sv_t* sv, dv_t* dv, Filter filt) noexcept
129  : pos_(begin), end_(end), t_(0), tv_(tv), sv_(sv), dv_(dv), filt_(filt)
130  {
131  if (pos_ == end_)
132  return;
133 
134  t_ = (*sv_)[*pos_].succ;
135  inc_state_maybe_();
136  while (pos_ != end_ && ignore_current())
137  inc_();
138  }
139 
140  scc_edge_iterator& operator++()
141  {
142  do
143  inc_();
144  while (pos_ != end_ && ignore_current());
145  return *this;
146  }
147 
148  scc_edge_iterator operator++(int)
149  {
150  scc_edge_iterator old = *this;
151  ++*this;
152  return old;
153  }
154 
155  bool operator==(scc_edge_iterator o) const
156  {
157  return pos_ == o.pos_ && t_ == o.t_;
158  }
159 
160  bool operator!=(scc_edge_iterator o) const
161  {
162  return pos_ != o.pos_ || t_ != o.t_;
163  }
164 
165  reference operator*() const
166  {
167  return (*tv_)[t_];
168  }
169 
170  pointer operator->() const
171  {
172  return &**this;
173  }
174  };
175 
176 
177  template <typename Graph, typename Filter>
178  class SPOT_API scc_edges
179  {
180  public:
182  typedef typename iter_t::tv_t tv_t;
183  typedef typename iter_t::sv_t sv_t;
184  typedef typename iter_t::dv_t dv_t;
185  typedef typename iter_t::state_iterator state_iterator;
186  private:
187  state_iterator begin_;
188  state_iterator end_;
189  tv_t* tv_;
190  sv_t* sv_;
191  dv_t* dv_;
192  Filter filt_;
193  public:
194 
195  scc_edges(state_iterator begin, state_iterator end,
196  tv_t* tv, sv_t* sv, dv_t* dv, Filter filt) noexcept
197  : begin_(begin), end_(end), tv_(tv), sv_(sv), dv_(dv), filt_(filt)
198  {
199  }
200 
201  iter_t begin() const
202  {
203  return {begin_, end_, tv_, sv_, dv_, filt_};
204  }
205 
206  iter_t end() const
207  {
208  return {end_, end_, nullptr, nullptr, nullptr, filt_};
209  }
210  };
211  }
212 
213 
215  class SPOT_API scc_info_node
216  {
217  public:
218  typedef std::vector<unsigned> scc_succs;
219  friend class scc_info;
220  protected:
221  scc_succs succ_;
222  acc_cond::mark_t acc_;
223  acc_cond::mark_t common_;
224  std::vector<unsigned> states_; // States of the component
225  bool trivial_:1;
226  bool accepting_:1; // Necessarily accepting
227  bool rejecting_:1; // Necessarily rejecting
228  bool useful_:1;
229  public:
230  scc_info_node():
231  acc_(0U), trivial_(true), accepting_(false),
232  rejecting_(false), useful_(false)
233  {
234  }
235 
237  acc_cond::mark_t common, bool trivial):
238  acc_(acc), common_(common),
239  trivial_(trivial), accepting_(false),
240  rejecting_(false), useful_(false)
241  {
242  }
243 
244  bool is_trivial() const
245  {
246  return trivial_;
247  }
248 
253  bool is_accepting() const
254  {
255  return accepting_;
256  }
257 
258  // True if we are sure that the SCC is rejecting
262  bool is_rejecting() const
263  {
264  return rejecting_;
265  }
266 
267  bool is_useful() const
268  {
269  return useful_;
270  }
271 
272  acc_cond::mark_t acc_marks() const
273  {
274  return acc_;
275  }
276 
277  acc_cond::mark_t common_marks() const
278  {
279  return common_;
280  }
281 
282  const std::vector<unsigned>& states() const
283  {
284  return states_;
285  }
286 
287  const scc_succs& succ() const
288  {
289  return succ_;
290  }
291  };
292 
293 
306  class SPOT_API scc_info
307  {
308  public:
309  // scc_node used to be an inner class, but Swig 3.0.10 does not
310  // support that yet.
311  typedef scc_info_node scc_node;
312  typedef scc_info_node::scc_succs scc_succs;
338  enum class edge_filter_choice { keep, ignore, cut };
339  typedef edge_filter_choice
340  (*edge_filter)(const twa_graph::edge_storage_t& e, unsigned dst,
341  void* filter_data);
343 
344  protected:
345 
346  std::vector<unsigned> sccof_;
347  std::vector<scc_node> node_;
348  const_twa_graph_ptr aut_;
349  unsigned initial_state_;
350  edge_filter filter_;
351  void* filter_data_;
352 
353  // Update the useful_ bits. Called automatically.
354  void determine_usefulness();
355 
356  const scc_node& node(unsigned scc) const
357  {
358  return node_[scc];
359  }
360 
361  public:
362 
363  // Use ~0U instead of -1U to work around a bug in Swig.
364  // See https://github.com/swig/swig/issues/993
365  scc_info(const_twa_graph_ptr aut,
366  unsigned initial_state = ~0U,
367  edge_filter filter = nullptr,
368  void* filter_data = nullptr);
369 
370  const_twa_graph_ptr get_aut() const
371  {
372  return aut_;
373  }
374 
375  unsigned scc_count() const
376  {
377  return node_.size();
378  }
379 
380  bool reachable_state(unsigned st) const
381  {
382  return scc_of(st) != -1U;
383  }
384 
385  unsigned scc_of(unsigned st) const
386  {
387  return sccof_[st];
388  }
389 
390  std::vector<scc_node>::const_iterator begin() const
391  {
392  return node_.begin();
393  }
394 
395  std::vector<scc_node>::const_iterator end() const
396  {
397  return node_.end();
398  }
399 
400  std::vector<scc_node>::const_iterator cbegin() const
401  {
402  return node_.cbegin();
403  }
404 
405  std::vector<scc_node>::const_iterator cend() const
406  {
407  return node_.cend();
408  }
409 
410  std::vector<scc_node>::const_reverse_iterator rbegin() const
411  {
412  return node_.rbegin();
413  }
414 
415  std::vector<scc_node>::const_reverse_iterator rend() const
416  {
417  return node_.rend();
418  }
419 
420  const std::vector<unsigned>& states_of(unsigned scc) const
421  {
422  return node(scc).states();
423  }
424 
431  edges_of(unsigned scc) const
432  {
433  auto& states = states_of(scc);
434  return {states.begin(), states.end(),
435  &aut_->edge_vector(), &aut_->states(),
436  &aut_->get_graph().dests_vector(),
438  }
439 
448  inner_edges_of(unsigned scc) const
449  {
450  auto& states = states_of(scc);
451  return {states.begin(), states.end(),
452  &aut_->edge_vector(), &aut_->states(),
453  &aut_->get_graph().dests_vector(),
454  internal::keep_inner_scc(sccof_, scc)};
455  }
456 
457  unsigned one_state_of(unsigned scc) const
458  {
459  return states_of(scc).front();
460  }
461 
463  unsigned initial() const
464  {
465  SPOT_ASSERT(filter_ || scc_count() - 1 == scc_of(initial_state_));
466  return scc_of(initial_state_);
467  }
468 
469  const scc_succs& succ(unsigned scc) const
470  {
471  return node(scc).succ();
472  }
473 
474  bool is_trivial(unsigned scc) const
475  {
476  return node(scc).is_trivial();
477  }
478 
479  acc_cond::mark_t acc(unsigned scc) const
480  {
481  return node(scc).acc_marks();
482  }
483 
484  bool is_accepting_scc(unsigned scc) const
485  {
486  return node(scc).is_accepting();
487  }
488 
489  bool is_rejecting_scc(unsigned scc) const
490  {
491  return node(scc).is_rejecting();
492  }
493 
494  // Study the SCC that are currently reported neither as accepting
495  // nor rejecting because of the presence of Fin sets
496  void determine_unknown_acceptance();
497 
498  bool is_useful_scc(unsigned scc) const
499  {
500  return node(scc).is_useful();
501  }
502 
503  bool is_useful_state(unsigned st) const
504  {
505  return reachable_state(st) && node(scc_of(st)).is_useful();
506  }
507 
510  std::vector<std::set<acc_cond::mark_t>> used_acc() const;
511 
512  std::set<acc_cond::mark_t> used_acc_of(unsigned scc) const;
513 
515  acc_cond::mark_t acc_sets_of(unsigned scc) const
516  {
517  // FIXME: Why do we have two name for this method?
518  return acc(scc);
519  }
520 
522  acc_cond::mark_t common_sets_of(unsigned scc) const
523  {
524  return node(scc).common_marks();
525  }
526 
527  std::vector<bool> weak_sccs() const;
528 
529  bdd scc_ap_support(unsigned scc) const;
530  };
531 
532 
536  SPOT_API std::ostream&
537  dump_scc_info_dot(std::ostream& out,
538  const_twa_graph_ptr aut, scc_info* sccinfo = nullptr);
539 
540 }
Definition: automata.hh:26
Compute an SCC map and gather assorted information.
Definition: sccinfo.hh:306
Definition: sccinfo.hh:178
Storage for SCC related information.
Definition: sccinfo.hh:215
Definition: graph.hh:184
acc_cond::mark_t common_sets_of(unsigned scc) const
sets that contain the entire SCC
Definition: sccinfo.hh:522
Definition: sccinfo.hh:68
internal::scc_edges< const twa_graph::graph_t, internal::keep_all > edges_of(unsigned scc) const
A fake container to iterate over all edges leaving any state of an SCC.
Definition: sccinfo.hh:431
acc_cond::mark_t acc_sets_of(unsigned scc) const
sets that contain at least one transition of the SCC
Definition: sccinfo.hh:515
bool is_accepting() const
True if we are sure that the SCC is accepting.
Definition: sccinfo.hh:253
unsigned initial() const
Get number of the SCC containing the initial state.
Definition: sccinfo.hh:463
edge_filter_choice
An edge_filter may be called on each edge to decide what to do with it.
Definition: sccinfo.hh:338
bool is_rejecting() const
Definition: sccinfo.hh:262
Definition: twa.hh:1629
Definition: sccinfo.hh:31
Definition: sccinfo.hh:42
internal::scc_edges< const twa_graph::graph_t, internal::keep_inner_scc > inner_edges_of(unsigned scc) const
A fake container to iterate over all edges between states of an SCC.
Definition: sccinfo.hh:448
Definition: acc.hh:39

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Wed Sep 6 2017 05:14:05 for spot by doxygen 1.8.13