spot  2.9.8
sccinfo.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2020 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 #include <spot/twaalgos/emptiness.hh>
25 
26 namespace spot
27 {
28  class scc_info;
29 
56  enum class edge_filter_choice { keep, ignore, cut };
57  typedef edge_filter_choice
58  (*edge_filter)(const twa_graph::edge_storage_t& e, unsigned dst,
59  void* filter_data);
61 
62  namespace internal
63  {
64  struct keep_all
65  {
66  template <typename Iterator>
67  bool operator()(Iterator, Iterator) const noexcept
68  {
69  return true;
70  }
71  };
72 
73  // Keep only transitions that have at least one destination in the
74  // current SCC.
76  {
77  private:
78  const std::vector<unsigned>& sccof_;
79  unsigned desired_scc_;
80  public:
81  keep_inner_scc(const std::vector<unsigned>& sccof, unsigned desired_scc)
82  : sccof_(sccof), desired_scc_(desired_scc)
83  {
84  }
85 
86  template <typename Iterator>
87  bool operator()(Iterator begin, Iterator end) const noexcept
88  {
89  bool want = false;
90  while (begin != end)
91  if (sccof_[*begin++] == desired_scc_)
92  {
93  want = true;
94  break;
95  }
96  return want;
97  }
98  };
99 
100  template <typename Graph, typename Filter>
101  class SPOT_API scc_edge_iterator
102  {
103  public:
104  typedef typename std::conditional<std::is_const<Graph>::value,
105  const typename Graph::edge_storage_t,
106  typename Graph::edge_storage_t>::type
107  value_type;
108  typedef value_type& reference;
109  typedef value_type* pointer;
110  typedef std::ptrdiff_t difference_type;
111  typedef std::forward_iterator_tag iterator_category;
112 
113  typedef std::vector<unsigned>::const_iterator state_iterator;
114 
115  typedef typename std::conditional<std::is_const<Graph>::value,
116  const typename Graph::edge_vector_t,
117  typename Graph::edge_vector_t>::type
118  tv_t;
119 
120  typedef typename std::conditional<std::is_const<Graph>::value,
121  const typename Graph::state_vector,
122  typename Graph::state_vector>::type
123  sv_t;
124  typedef const typename Graph::dests_vector_t dv_t;
125  protected:
126 
127  state_iterator pos_;
128  state_iterator end_;
129  unsigned t_;
130  tv_t* tv_;
131  sv_t* sv_;
132  dv_t* dv_;
133 
134  Filter filt_;
135  edge_filter efilter_;
136  void* efilter_data_;
137 
138 
139  void inc_state_maybe_()
140  {
141  while (!t_ && (++pos_ != end_))
142  t_ = (*sv_)[*pos_].succ;
143  }
144 
145  void inc_()
146  {
147  t_ = (*tv_)[t_].next_succ;
148  inc_state_maybe_();
149  }
150 
151  // Do we ignore the current transition?
152  bool ignore_current()
153  {
154  unsigned dst = (*this)->dst;
155  if ((int)dst >= 0)
156  {
157  // Non-universal branching => a single destination.
158  if (!filt_(&(*this)->dst, 1 + &(*this)->dst))
159  return true;
160  if (efilter_)
161  return efilter_((*tv_)[t_], dst, efilter_data_)
162  != edge_filter_choice::keep;
163  return false;
164  }
165  else
166  {
167  // Universal branching => multiple destinations.
168  const unsigned* d = dv_->data() + ~dst;
169  if (!filt_(d + 1, d + *d + 1))
170  return true;
171  if (efilter_)
172  {
173  // Keep the transition if at least one destination
174  // is not filtered.
175  const unsigned* end = d + *d + 1;
176  for (const unsigned* i = d + 1; i != end; ++i)
177  {
178  if (efilter_((*tv_)[t_], *i, efilter_data_)
179  == edge_filter_choice::keep)
180  return false;
181  return true;
182  }
183  }
184  return false;
185  }
186  }
187 
188  public:
189  scc_edge_iterator(state_iterator begin, state_iterator end,
190  tv_t* tv, sv_t* sv, dv_t* dv, Filter filt,
191  edge_filter efilter, void* efilter_data) noexcept
192  : pos_(begin), end_(end), t_(0), tv_(tv), sv_(sv), dv_(dv), filt_(filt),
193  efilter_(efilter), efilter_data_(efilter_data)
194  {
195  if (pos_ == end_)
196  return;
197 
198  t_ = (*sv_)[*pos_].succ;
199  inc_state_maybe_();
200  while (pos_ != end_ && ignore_current())
201  inc_();
202  }
203 
204  scc_edge_iterator& operator++()
205  {
206  do
207  inc_();
208  while (pos_ != end_ && ignore_current());
209  return *this;
210  }
211 
212  scc_edge_iterator operator++(int)
213  {
214  scc_edge_iterator old = *this;
215  ++*this;
216  return old;
217  }
218 
219  bool operator==(scc_edge_iterator o) const
220  {
221  return pos_ == o.pos_ && t_ == o.t_;
222  }
223 
224  bool operator!=(scc_edge_iterator o) const
225  {
226  return pos_ != o.pos_ || t_ != o.t_;
227  }
228 
229  reference operator*() const
230  {
231  return (*tv_)[t_];
232  }
233 
234  pointer operator->() const
235  {
236  return &**this;
237  }
238  };
239 
240 
241  template <typename Graph, typename Filter>
242  class SPOT_API scc_edges
243  {
244  public:
246  typedef typename iter_t::tv_t tv_t;
247  typedef typename iter_t::sv_t sv_t;
248  typedef typename iter_t::dv_t dv_t;
249  typedef typename iter_t::state_iterator state_iterator;
250  private:
251  state_iterator begin_;
252  state_iterator end_;
253  tv_t* tv_;
254  sv_t* sv_;
255  dv_t* dv_;
256  Filter filt_;
257  edge_filter efilter_;
258  void* efilter_data_;
259  public:
260 
261  scc_edges(state_iterator begin, state_iterator end,
262  tv_t* tv, sv_t* sv, dv_t* dv, Filter filt,
263  edge_filter efilter, void* efilter_data) noexcept
264  : begin_(begin), end_(end), tv_(tv), sv_(sv), dv_(dv), filt_(filt),
265  efilter_(efilter), efilter_data_(efilter_data)
266  {
267  }
268 
269  iter_t begin() const
270  {
271  return {begin_, end_, tv_, sv_, dv_, filt_, efilter_, efilter_data_};
272  }
273 
274  iter_t end() const
275  {
276  return {end_, end_, nullptr, nullptr, nullptr, filt_, nullptr, nullptr};
277  }
278  };
279  }
280 
281 
284  class SPOT_API scc_info_node
285  {
286  public:
287  typedef std::vector<unsigned> scc_succs;
288  friend class scc_info;
289  protected:
290  scc_succs succ_;
291  std::vector<unsigned> states_; // States of the component
292  unsigned one_state_;
293  acc_cond::mark_t acc_;
294  acc_cond::mark_t common_;
295  bool trivial_:1;
296  bool accepting_:1; // Necessarily accepting
297  bool rejecting_:1; // Necessarily rejecting
298  bool useful_:1;
299  public:
300  scc_info_node() noexcept:
301  acc_({}), trivial_(true), accepting_(false),
302  rejecting_(false), useful_(false)
303  {
304  }
305 
307  acc_cond::mark_t common, bool trivial) noexcept
308  : acc_(acc), common_(common),
309  trivial_(trivial), accepting_(false),
310  rejecting_(false), useful_(false)
311  {
312  }
313 
314  bool is_trivial() const
315  {
316  return trivial_;
317  }
318 
324  bool is_accepting() const
325  {
326  return accepting_;
327  }
328 
334  bool is_rejecting() const
335  {
336  return rejecting_;
337  }
338 
339  bool is_useful() const
340  {
341  return useful_;
342  }
343 
344  acc_cond::mark_t acc_marks() const
345  {
346  return acc_;
347  }
348 
349  acc_cond::mark_t common_marks() const
350  {
351  return common_;
352  }
353 
354  const std::vector<unsigned>& states() const
355  {
356  return states_;
357  }
358 
359  unsigned one_state() const
360  {
361  return one_state_;
362  }
363 
364  const scc_succs& succ() const
365  {
366  return succ_;
367  }
368  };
369 
372  enum class scc_info_options
373  {
377  NONE = 0,
382  STOP_ON_ACC = 1,
387  TRACK_STATES = 2,
391  TRACK_SUCCS = 4,
397  };
398 
399  inline
400  bool operator!(scc_info_options me)
401  {
402  return me == scc_info_options::NONE;
403  }
404 
405  inline
406  scc_info_options operator&(scc_info_options left, scc_info_options right)
407  {
408  typedef std::underlying_type_t<scc_info_options> ut;
409  return static_cast<scc_info_options>(static_cast<ut>(left)
410  & static_cast<ut>(right));
411  }
412 
413  inline
414  scc_info_options operator|(scc_info_options left, scc_info_options right)
415  {
416  typedef std::underlying_type_t<scc_info_options> ut;
417  return static_cast<scc_info_options>(static_cast<ut>(left)
418  | static_cast<ut>(right));
419  }
420 
421  class SPOT_API scc_and_mark_filter;
422 
441  class SPOT_API scc_info
442  {
443  public:
444  // scc_node used to be an inner class, but Swig 3.0.10 does not
445  // support that yet.
446  typedef scc_info_node scc_node;
447  typedef scc_info_node::scc_succs scc_succs;
448 
449  // These types used to be defined here in Spot up to 2.9.
452 
453  protected:
454 
455  std::vector<unsigned> sccof_;
456  std::vector<scc_node> node_;
457  const_twa_graph_ptr aut_;
458  unsigned initial_state_;
459  edge_filter filter_;
460  void* filter_data_;
461  int one_acc_scc_ = -1;
462  scc_info_options options_;
463 
464  // Update the useful_ bits. Called automatically.
465  void determine_usefulness();
466 
467  const scc_node& node(unsigned scc) const
468  {
469  return node_[scc];
470  }
471 
472 #ifndef SWIG
473  private:
474  [[noreturn]] static void report_need_track_states();
475  [[noreturn]] static void report_need_track_succs();
476  [[noreturn]] static void report_incompatible_stop_on_acc();
477 #endif
478 
479  public:
482  scc_info(const_twa_graph_ptr aut,
483  // Use ~0U instead of -1U to work around a bug in Swig.
484  // See https://github.com/swig/swig/issues/993
485  unsigned initial_state = ~0U,
486  edge_filter filter = nullptr,
487  void* filter_data = nullptr,
489 
490  scc_info(const_twa_graph_ptr aut, scc_info_options options)
491  : scc_info(aut, ~0U, nullptr, nullptr, options)
492  {
493  }
495 
502  scc_info(const scc_and_mark_filter& filt, scc_info_options options);
503  // we separate the two functions so that we can rename
504  // scc_info(x,options) into scc_info_with_options(x,options) in Python.
505  // Otherwrise calling scc_info(aut,options) can be confused with
506  // scc_info(aut,initial_state).
507  scc_info(const scc_and_mark_filter& filt)
508  : scc_info(filt, scc_info_options::ALL)
509  {
510  }
512 
513  const_twa_graph_ptr get_aut() const
514  {
515  return aut_;
516  }
517 
518  scc_info_options get_options() const
519  {
520  return options_;
521  }
522 
523  edge_filter get_filter() const
524  {
525  return filter_;
526  }
527 
528  const void* get_filter_data() const
529  {
530  return filter_data_;
531  }
532 
533  unsigned scc_count() const
534  {
535  return node_.size();
536  }
537 
546  int one_accepting_scc() const
547  {
548  return one_acc_scc_;
549  }
550 
551  bool reachable_state(unsigned st) const
552  {
553  return scc_of(st) != -1U;
554  }
555 
556  unsigned scc_of(unsigned st) const
557  {
558  return sccof_[st];
559  }
560 
561  std::vector<scc_node>::const_iterator begin() const
562  {
563  return node_.begin();
564  }
565 
566  std::vector<scc_node>::const_iterator end() const
567  {
568  return node_.end();
569  }
570 
571  std::vector<scc_node>::const_iterator cbegin() const
572  {
573  return node_.cbegin();
574  }
575 
576  std::vector<scc_node>::const_iterator cend() const
577  {
578  return node_.cend();
579  }
580 
581  std::vector<scc_node>::const_reverse_iterator rbegin() const
582  {
583  return node_.rbegin();
584  }
585 
586  std::vector<scc_node>::const_reverse_iterator rend() const
587  {
588  return node_.rend();
589  }
590 
591  const std::vector<unsigned>& states_of(unsigned scc) const
592  {
593  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_STATES)))
594  report_need_track_states();
595  return node(scc).states();
596  }
597 
604  edges_of(unsigned scc) const
605  {
606  auto& states = states_of(scc);
607  return {states.begin(), states.end(),
608  &aut_->edge_vector(), &aut_->states(),
609  &aut_->get_graph().dests_vector(),
610  internal::keep_all(), filter_, const_cast<void*>(filter_data_)};
611  }
612 
621  inner_edges_of(unsigned scc) const
622  {
623  auto& states = states_of(scc);
624  return {states.begin(), states.end(),
625  &aut_->edge_vector(), &aut_->states(),
626  &aut_->get_graph().dests_vector(),
627  internal::keep_inner_scc(sccof_, scc), filter_,
628  const_cast<void*>(filter_data_)};
629  }
630 
631  unsigned one_state_of(unsigned scc) const
632  {
633  return node(scc).one_state();
634  }
635 
637  unsigned initial() const
638  {
639  SPOT_ASSERT(filter_ || scc_count() - 1 == scc_of(initial_state_));
640  return scc_of(initial_state_);
641  }
642 
643  const scc_succs& succ(unsigned scc) const
644  {
645  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_SUCCS)))
646  report_need_track_succs();
647  return node(scc).succ();
648  }
649 
650  bool is_trivial(unsigned scc) const
651  {
652  return node(scc).is_trivial();
653  }
654 
655  SPOT_DEPRECATED("use acc_sets_of() instead")
656  acc_cond::mark_t acc(unsigned scc) const
657  {
658  return acc_sets_of(scc);
659  }
660 
661  bool is_accepting_scc(unsigned scc) const
662  {
663  return node(scc).is_accepting();
664  }
665 
666  bool is_rejecting_scc(unsigned scc) const
667  {
668  return node(scc).is_rejecting();
669  }
670 
675  void determine_unknown_acceptance();
676 
681  bool check_scc_emptiness(unsigned n) const;
682 
690  void get_accepting_run(unsigned scc, twa_run_ptr r) const;
691 
692  bool is_useful_scc(unsigned scc) const
693  {
694  if (SPOT_UNLIKELY(!!(options_ & scc_info_options::STOP_ON_ACC)))
695  report_incompatible_stop_on_acc();
696  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_SUCCS)))
697  report_need_track_succs();
698  return node(scc).is_useful();
699  }
700 
701  bool is_useful_state(unsigned st) const
702  {
703  return reachable_state(st) && is_useful_scc(scc_of(st));
704  }
705 
708  std::vector<std::set<acc_cond::mark_t>> marks() const;
709  std::set<acc_cond::mark_t> marks_of(unsigned scc) const;
710 
711  // Same as above, with old names.
712  SPOT_DEPRECATED("use marks() instead")
713  std::vector<std::set<acc_cond::mark_t>> used_acc() const
714  {
715  return marks();
716  }
717  SPOT_DEPRECATED("use marks_of() instead")
718  std::set<acc_cond::mark_t> used_acc_of(unsigned scc) const
719  {
720  return marks_of(scc);
721  }
722 
726  acc_cond::mark_t acc_sets_of(unsigned scc) const
727  {
728  return node(scc).acc_marks();
729  }
730 
733  acc_cond::mark_t common_sets_of(unsigned scc) const
734  {
735  return node(scc).common_marks();
736  }
737 
738  std::vector<bool> weak_sccs() const;
739 
740  bdd scc_ap_support(unsigned scc) const;
741 
751  std::vector<twa_graph_ptr> split_on_sets(unsigned scc,
752  acc_cond::mark_t sets,
753  bool preserve_names = false) const;
754  protected:
756  void
757  states_on_acc_cycle_of_rec(unsigned scc,
758  acc_cond::mark_t all_fin,
759  acc_cond::mark_t all_inf,
760  unsigned nb_pairs,
761  std::vector<acc_cond::rs_pair>& pairs,
762  std::vector<unsigned>& res,
763  std::vector<unsigned>& old) const;
764  public:
769  std::vector<unsigned>
770  states_on_acc_cycle_of(unsigned scc) const;
771  };
772 
773 
779  class SPOT_API scc_and_mark_filter
780  {
781  protected:
782  const scc_info* lower_si_;
783  unsigned lower_scc_;
784  acc_cond::mark_t cut_sets_;
785  const_twa_graph_ptr aut_;
786  acc_cond old_acc_;
787  bool restore_old_acc_ = false;
788 
790  filter_scc_and_mark_(const twa_graph::edge_storage_t& e,
791  unsigned dst, void* data);
793  filter_mark_(const twa_graph::edge_storage_t& e, unsigned, void* data);
794 
795  public:
801  scc_and_mark_filter(const scc_info& lower_si,
802  unsigned lower_scc,
803  acc_cond::mark_t cut_sets)
804  : lower_si_(&lower_si), lower_scc_(lower_scc), cut_sets_(cut_sets),
805  aut_(lower_si_->get_aut()), old_acc_(aut_->get_acceptance())
806  {
807  auto f = lower_si.get_filter();
808  if (f == &filter_mark_ || f == &filter_scc_and_mark_)
809  {
810  const void* data = lower_si.get_filter_data();
811  auto& d = *reinterpret_cast<const scc_and_mark_filter*>(data);
812  cut_sets_ |= d.cut_sets_;
813  }
814  }
815 
820  scc_and_mark_filter(const const_twa_graph_ptr& aut,
821  acc_cond::mark_t cut_sets)
822  : lower_si_(nullptr), cut_sets_(cut_sets), aut_(aut),
823  old_acc_(aut_->get_acceptance())
824  {
825  }
826 
827  ~scc_and_mark_filter()
828  {
829  restore_acceptance();
830  }
831 
832  void override_acceptance(const acc_cond& new_acc)
833  {
834  std::const_pointer_cast<twa_graph>(aut_)->set_acceptance(new_acc);
835  restore_old_acc_ = true;
836  }
837 
838  void restore_acceptance()
839  {
840  if (!restore_old_acc_)
841  return;
842  std::const_pointer_cast<twa_graph>(aut_)->set_acceptance(old_acc_);
843  restore_old_acc_ = false;
844  }
845 
846  const_twa_graph_ptr get_aut() const
847  {
848  return aut_;
849  }
850 
851  unsigned start_state() const
852  {
853  if (lower_si_)
854  return lower_si_->one_state_of(lower_scc_);
855  return aut_->get_init_state_number();
856  }
857 
858  scc_info::edge_filter get_filter() const
859  {
860  if (lower_si_)
861  return filter_scc_and_mark_;
862  if (cut_sets_)
863  return filter_mark_;
864  return nullptr;
865  }
866  };
867 
868 
872  SPOT_API std::ostream&
873  dump_scc_info_dot(std::ostream& out,
874  const_twa_graph_ptr aut, scc_info* sccinfo = nullptr);
875 
876 }
Definition: automata.hh:26
Compute an SCC map and gather assorted information.
Definition: sccinfo.hh:441
scc_and_mark_filter(const scc_info &lower_si, unsigned lower_scc, acc_cond::mark_t cut_sets)
Specify how to restrict scc_info to some SCC and acceptance sets.
Definition: sccinfo.hh:801
Definition: sccinfo.hh:242
Storage for SCC related information.
Definition: sccinfo.hh:284
Default behavior: explore everything and track states and succs.
Definition: graph.hh:183
acc_cond::mark_t common_sets_of(unsigned scc) const
Definition: sccinfo.hh:733
Definition: sccinfo.hh:101
scc_and_mark_filter(const const_twa_graph_ptr &aut, acc_cond::mark_t cut_sets)
Specify how to restrict scc_info to some acceptance sets.
Definition: sccinfo.hh:820
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:604
int one_accepting_scc() const
Return the number of one accepting SCC if any, -1 otherwise.
Definition: sccinfo.hh:546
edge_filter_choice
An edge_filter may be called on each edge to decide what to do with it.
Definition: sccinfo.hh:56
acc_cond::mark_t acc_sets_of(unsigned scc) const
Returns, for a given SCC, the set of all colors appearing in it. It is the set of colors that appear ...
Definition: sccinfo.hh:726
scc_info_options
Options to alter the behavior of scc_info.
Definition: sccinfo.hh:372
An acceptance condition.
Definition: acc.hh:60
scc_info(const_twa_graph_ptr aut, scc_info_options options)
Create the scc_info map for aut.
Definition: sccinfo.hh:490
bool is_accepting() const
True if we know that the SCC has an accepting cycle.
Definition: sccinfo.hh:324
scc_info(const scc_and_mark_filter &filt)
Create an scc_info map from some filter.
Definition: sccinfo.hh:507
SPOT_DEPRECATED("use to_parity() instead") twa_graph_ptr iar(const const_twa_graph_ptr &aut
Turn a Rabin-like or Streett-like automaton into a parity automaton based on the index appearence rec...
unsigned initial() const
Get number of the SCC containing the initial state.
Definition: sccinfo.hh:637
bool is_rejecting() const
True if we know that all cycles in the SCC are rejecting.
Definition: sccinfo.hh:334
Definition: twa.hh:1699
Definition: sccinfo.hh:64
Definition: sccinfo.hh:75
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:621
edge_filter_choice(* edge_filter)(const twa_graph::edge_storage_t &e, unsigned dst, void *filter_data)
An edge_filter may be called on each edge to decide what to do with it.
Definition: sccinfo.hh:58
Graph-based representation of a TωA.
Definition: twagraph.hh:200
An acceptance mark.
Definition: acc.hh:87

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.8.13