spot  2.4.2
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 
254  bool is_accepting() const
255  {
256  return accepting_;
257  }
258 
264  bool is_rejecting() const
265  {
266  return rejecting_;
267  }
268 
269  bool is_useful() const
270  {
271  return useful_;
272  }
273 
274  acc_cond::mark_t acc_marks() const
275  {
276  return acc_;
277  }
278 
279  acc_cond::mark_t common_marks() const
280  {
281  return common_;
282  }
283 
284  const std::vector<unsigned>& states() const
285  {
286  return states_;
287  }
288 
289  const scc_succs& succ() const
290  {
291  return succ_;
292  }
293  };
294 
295 
313  class SPOT_API scc_info
314  {
315  public:
316  // scc_node used to be an inner class, but Swig 3.0.10 does not
317  // support that yet.
318  typedef scc_info_node scc_node;
319  typedef scc_info_node::scc_succs scc_succs;
345  enum class edge_filter_choice { keep, ignore, cut };
346  typedef edge_filter_choice
347  (*edge_filter)(const twa_graph::edge_storage_t& e, unsigned dst,
348  void* filter_data);
350 
351  protected:
352 
353  std::vector<unsigned> sccof_;
354  std::vector<scc_node> node_;
355  const_twa_graph_ptr aut_;
356  unsigned initial_state_;
357  edge_filter filter_;
358  void* filter_data_;
359 
360  // Update the useful_ bits. Called automatically.
361  void determine_usefulness();
362 
363  const scc_node& node(unsigned scc) const
364  {
365  return node_[scc];
366  }
367 
368  public:
369 
370  // Use ~0U instead of -1U to work around a bug in Swig.
371  // See https://github.com/swig/swig/issues/993
372  scc_info(const_twa_graph_ptr aut,
373  unsigned initial_state = ~0U,
374  edge_filter filter = nullptr,
375  void* filter_data = nullptr);
376 
377  const_twa_graph_ptr get_aut() const
378  {
379  return aut_;
380  }
381 
382  unsigned scc_count() const
383  {
384  return node_.size();
385  }
386 
387  bool reachable_state(unsigned st) const
388  {
389  return scc_of(st) != -1U;
390  }
391 
392  unsigned scc_of(unsigned st) const
393  {
394  return sccof_[st];
395  }
396 
397  std::vector<scc_node>::const_iterator begin() const
398  {
399  return node_.begin();
400  }
401 
402  std::vector<scc_node>::const_iterator end() const
403  {
404  return node_.end();
405  }
406 
407  std::vector<scc_node>::const_iterator cbegin() const
408  {
409  return node_.cbegin();
410  }
411 
412  std::vector<scc_node>::const_iterator cend() const
413  {
414  return node_.cend();
415  }
416 
417  std::vector<scc_node>::const_reverse_iterator rbegin() const
418  {
419  return node_.rbegin();
420  }
421 
422  std::vector<scc_node>::const_reverse_iterator rend() const
423  {
424  return node_.rend();
425  }
426 
427  const std::vector<unsigned>& states_of(unsigned scc) const
428  {
429  return node(scc).states();
430  }
431 
438  edges_of(unsigned scc) const
439  {
440  auto& states = states_of(scc);
441  return {states.begin(), states.end(),
442  &aut_->edge_vector(), &aut_->states(),
443  &aut_->get_graph().dests_vector(),
445  }
446 
455  inner_edges_of(unsigned scc) const
456  {
457  auto& states = states_of(scc);
458  return {states.begin(), states.end(),
459  &aut_->edge_vector(), &aut_->states(),
460  &aut_->get_graph().dests_vector(),
461  internal::keep_inner_scc(sccof_, scc)};
462  }
463 
464  unsigned one_state_of(unsigned scc) const
465  {
466  return states_of(scc).front();
467  }
468 
470  unsigned initial() const
471  {
472  SPOT_ASSERT(filter_ || scc_count() - 1 == scc_of(initial_state_));
473  return scc_of(initial_state_);
474  }
475 
476  const scc_succs& succ(unsigned scc) const
477  {
478  return node(scc).succ();
479  }
480 
481  bool is_trivial(unsigned scc) const
482  {
483  return node(scc).is_trivial();
484  }
485 
486  acc_cond::mark_t acc(unsigned scc) const
487  {
488  return node(scc).acc_marks();
489  }
490 
491  bool is_accepting_scc(unsigned scc) const
492  {
493  return node(scc).is_accepting();
494  }
495 
496  bool is_rejecting_scc(unsigned scc) const
497  {
498  return node(scc).is_rejecting();
499  }
500 
501  // Study the SCC that are currently reported neither as accepting
502  // nor rejecting because of the presence of Fin sets
503  void determine_unknown_acceptance();
504 
505  bool is_useful_scc(unsigned scc) const
506  {
507  return node(scc).is_useful();
508  }
509 
510  bool is_useful_state(unsigned st) const
511  {
512  return reachable_state(st) && node(scc_of(st)).is_useful();
513  }
514 
517  std::vector<std::set<acc_cond::mark_t>> used_acc() const;
518 
519  std::set<acc_cond::mark_t> used_acc_of(unsigned scc) const;
520 
522  acc_cond::mark_t acc_sets_of(unsigned scc) const
523  {
524  // FIXME: Why do we have two name for this method?
525  return acc(scc);
526  }
527 
529  acc_cond::mark_t common_sets_of(unsigned scc) const
530  {
531  return node(scc).common_marks();
532  }
533 
534  std::vector<bool> weak_sccs() const;
535 
536  bdd scc_ap_support(unsigned scc) const;
537  };
538 
539 
543  SPOT_API std::ostream&
544  dump_scc_info_dot(std::ostream& out,
545  const_twa_graph_ptr aut, scc_info* sccinfo = nullptr);
546 
547 }
Definition: automata.hh:26
Compute an SCC map and gather assorted information.
Definition: sccinfo.hh:313
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:529
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:438
acc_cond::mark_t acc_sets_of(unsigned scc) const
sets that contain at least one transition of the SCC
Definition: sccinfo.hh:522
bool is_accepting() const
True if we know that the SCC has an accepting cycle.
Definition: sccinfo.hh:254
unsigned initial() const
Get number of the SCC containing the initial state.
Definition: sccinfo.hh:470
edge_filter_choice
An edge_filter may be called on each edge to decide what to do with it.
Definition: sccinfo.hh:345
bool is_rejecting() const
True if we know that all cycles in the SCC are rejecting.
Definition: sccinfo.hh:264
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:455
Definition: acc.hh:39

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Sat Nov 25 2017 02:13:16 for spot by doxygen 1.8.13