spot  2.9.1
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 
539  int one_accepting_scc() const
540  {
541  return one_acc_scc_;
542  }
543 
544  bool reachable_state(unsigned st) const
545  {
546  return scc_of(st) != -1U;
547  }
548 
549  unsigned scc_of(unsigned st) const
550  {
551  return sccof_[st];
552  }
553 
554  std::vector<scc_node>::const_iterator begin() const
555  {
556  return node_.begin();
557  }
558 
559  std::vector<scc_node>::const_iterator end() const
560  {
561  return node_.end();
562  }
563 
564  std::vector<scc_node>::const_iterator cbegin() const
565  {
566  return node_.cbegin();
567  }
568 
569  std::vector<scc_node>::const_iterator cend() const
570  {
571  return node_.cend();
572  }
573 
574  std::vector<scc_node>::const_reverse_iterator rbegin() const
575  {
576  return node_.rbegin();
577  }
578 
579  std::vector<scc_node>::const_reverse_iterator rend() const
580  {
581  return node_.rend();
582  }
583 
584  const std::vector<unsigned>& states_of(unsigned scc) const
585  {
586  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_STATES)))
587  report_need_track_states();
588  return node(scc).states();
589  }
590 
597  edges_of(unsigned scc) const
598  {
599  auto& states = states_of(scc);
600  return {states.begin(), states.end(),
601  &aut_->edge_vector(), &aut_->states(),
602  &aut_->get_graph().dests_vector(),
603  internal::keep_all(), filter_, const_cast<void*>(filter_data_)};
604  }
605 
614  inner_edges_of(unsigned scc) const
615  {
616  auto& states = states_of(scc);
617  return {states.begin(), states.end(),
618  &aut_->edge_vector(), &aut_->states(),
619  &aut_->get_graph().dests_vector(),
620  internal::keep_inner_scc(sccof_, scc), filter_,
621  const_cast<void*>(filter_data_)};
622  }
623 
624  unsigned one_state_of(unsigned scc) const
625  {
626  return node(scc).one_state();
627  }
628 
630  unsigned initial() const
631  {
632  SPOT_ASSERT(filter_ || scc_count() - 1 == scc_of(initial_state_));
633  return scc_of(initial_state_);
634  }
635 
636  const scc_succs& succ(unsigned scc) const
637  {
638  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_SUCCS)))
639  report_need_track_succs();
640  return node(scc).succ();
641  }
642 
643  bool is_trivial(unsigned scc) const
644  {
645  return node(scc).is_trivial();
646  }
647 
648  SPOT_DEPRECATED("use acc_sets_of() instead")
649  acc_cond::mark_t acc(unsigned scc) const
650  {
651  return acc_sets_of(scc);
652  }
653 
654  bool is_accepting_scc(unsigned scc) const
655  {
656  return node(scc).is_accepting();
657  }
658 
659  bool is_rejecting_scc(unsigned scc) const
660  {
661  return node(scc).is_rejecting();
662  }
663 
668  void determine_unknown_acceptance();
669 
674  bool check_scc_emptiness(unsigned n) const;
675 
683  void get_accepting_run(unsigned scc, twa_run_ptr r) const;
684 
685  bool is_useful_scc(unsigned scc) const
686  {
687  if (SPOT_UNLIKELY(!!(options_ & scc_info_options::STOP_ON_ACC)))
688  report_incompatible_stop_on_acc();
689  if (SPOT_UNLIKELY(!(options_ & scc_info_options::TRACK_SUCCS)))
690  report_need_track_succs();
691  return node(scc).is_useful();
692  }
693 
694  bool is_useful_state(unsigned st) const
695  {
696  return reachable_state(st) && is_useful_scc(scc_of(st));
697  }
698 
701  std::vector<std::set<acc_cond::mark_t>> marks() const;
702  std::set<acc_cond::mark_t> marks_of(unsigned scc) const;
703 
704  // Same as above, with old names.
705  SPOT_DEPRECATED("use marks() instead")
706  std::vector<std::set<acc_cond::mark_t>> used_acc() const
707  {
708  return marks();
709  }
710  SPOT_DEPRECATED("use marks_of() instead")
711  std::set<acc_cond::mark_t> used_acc_of(unsigned scc) const
712  {
713  return marks_of(scc);
714  }
715 
719  acc_cond::mark_t acc_sets_of(unsigned scc) const
720  {
721  return node(scc).acc_marks();
722  }
723 
726  acc_cond::mark_t common_sets_of(unsigned scc) const
727  {
728  return node(scc).common_marks();
729  }
730 
731  std::vector<bool> weak_sccs() const;
732 
733  bdd scc_ap_support(unsigned scc) const;
734 
744  std::vector<twa_graph_ptr> split_on_sets(unsigned scc,
745  acc_cond::mark_t sets,
746  bool preserve_names = false) const;
747  protected:
749  void
750  states_on_acc_cycle_of_rec(unsigned scc,
751  acc_cond::mark_t all_fin,
752  acc_cond::mark_t all_inf,
753  unsigned nb_pairs,
754  std::vector<acc_cond::rs_pair>& pairs,
755  std::vector<unsigned>& res,
756  std::vector<unsigned>& old) const;
757  public:
762  std::vector<unsigned>
763  states_on_acc_cycle_of(unsigned scc) const;
764  };
765 
766 
772  class SPOT_API scc_and_mark_filter
773  {
774  protected:
775  const scc_info* lower_si_;
776  unsigned lower_scc_;
777  acc_cond::mark_t cut_sets_;
778  const_twa_graph_ptr aut_;
779  acc_cond old_acc_;
780  bool restore_old_acc_ = false;
781 
783  filter_scc_and_mark_(const twa_graph::edge_storage_t& e,
784  unsigned dst, void* data);
786  filter_mark_(const twa_graph::edge_storage_t& e, unsigned, void* data);
787 
788  public:
794  scc_and_mark_filter(const scc_info& lower_si,
795  unsigned lower_scc,
796  acc_cond::mark_t cut_sets)
797  : lower_si_(&lower_si), lower_scc_(lower_scc), cut_sets_(cut_sets),
798  aut_(lower_si_->get_aut()), old_acc_(aut_->get_acceptance())
799  {
800  auto f = lower_si.get_filter();
801  if (f == &filter_mark_ || f == &filter_scc_and_mark_)
802  {
803  const void* data = lower_si.get_filter_data();
804  auto& d = *reinterpret_cast<const scc_and_mark_filter*>(data);
805  cut_sets_ |= d.cut_sets_;
806  }
807  }
808 
813  scc_and_mark_filter(const const_twa_graph_ptr& aut,
814  acc_cond::mark_t cut_sets)
815  : lower_si_(nullptr), cut_sets_(cut_sets), aut_(aut),
816  old_acc_(aut_->get_acceptance())
817  {
818  }
819 
820  ~scc_and_mark_filter()
821  {
822  restore_acceptance();
823  }
824 
825  void override_acceptance(const acc_cond& new_acc)
826  {
827  std::const_pointer_cast<twa_graph>(aut_)->set_acceptance(new_acc);
828  restore_old_acc_ = true;
829  }
830 
831  void restore_acceptance()
832  {
833  if (!restore_old_acc_)
834  return;
835  std::const_pointer_cast<twa_graph>(aut_)->set_acceptance(old_acc_);
836  restore_old_acc_ = false;
837  }
838 
839  const_twa_graph_ptr get_aut() const
840  {
841  return aut_;
842  }
843 
844  unsigned start_state() const
845  {
846  if (lower_si_)
847  return lower_si_->one_state_of(lower_scc_);
848  return aut_->get_init_state_number();
849  }
850 
851  scc_info::edge_filter get_filter() const
852  {
853  if (lower_si_)
854  return filter_scc_and_mark_;
855  if (cut_sets_)
856  return filter_mark_;
857  return nullptr;
858  }
859  };
860 
861 
865  SPOT_API std::ostream&
866  dump_scc_info_dot(std::ostream& out,
867  const_twa_graph_ptr aut, scc_info* sccinfo = nullptr);
868 
869 }
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:794
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:726
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:813
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:597
int one_accepting_scc() const
Return the number of one accepting SCC if any, -1 otherwise.
Definition: sccinfo.hh:539
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:719
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:630
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:614
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