sbacc.cc 4.43 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2015-2017 Laboratoire de Recherche et Développement
// de l'Epita (LRDE).
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <vector>
#include <map>
#include <utility>
23
#include <spot/twaalgos/sbacc.hh>
24
#include <spot/twaalgos/sccinfo.hh>
25
#include <spot/twaalgos/stripacc.hh>
26
27
28

namespace spot
{
29
  twa_graph_ptr sbacc(twa_graph_ptr old)
30
  {
31
    if (old->prop_state_acc())
32
      return old;
33
    if (!old->is_existential())
34
35
      throw std::runtime_error
        ("sbacc() does not support alternation");
36

37
38
39
40
41
42
43
44
45
46
47
48
    // We will need a mark that is rejecting to mark rejecting states.
    // If no such mark exist, our work is actually quite simple: we
    // just have to copy the automaton and give it "t" as acceptance
    // condition.
    auto unsat_mark = old->acc().unsat_mark();
    if (!unsat_mark.first)
      {
        auto res = make_twa_graph(old, twa::prop_set::all());
        strip_acceptance_here(res);
        return res;
      }

49
50
51
52
    scc_info si(old);

    unsigned ns = old->num_states();
    acc_cond::mark_t all = old->acc().all_sets();
53
    // Marks that are common to all ingoing or outgoing transitions.
54
55
    std::vector<acc_cond::mark_t> common_in(ns, all);
    std::vector<acc_cond::mark_t> common_out(ns, all);
56
    // Marks that label one incoming transition from the same SCC.
57
58
59
60
61
62
63
64
65
66
67
    std::vector<acc_cond::mark_t> one_in(ns, 0U);
    for (auto& e: old->edges())
      if (si.scc_of(e.src) == si.scc_of(e.dst))
        {
          common_in[e.dst] &= e.acc;
          common_out[e.src] &= e.acc;
        }
    for (unsigned s = 0; s < ns; ++s)
      common_out[s] |= common_in[s];
    for (auto& e: old->edges())
      if (si.scc_of(e.src) == si.scc_of(e.dst))
68
        one_in[e.dst] = e.acc - common_out[e.src];
69

70
    auto res = make_twa_graph(old->get_dict());
71
    res->copy_ap_of(old);
72
    res->copy_acceptance_of(old);
73
    res->prop_copy(old, {false, true, true, true, true, true});
74
    res->prop_state_acc(true);
75
76
77
78
79
80
81
82
83

    typedef std::pair<unsigned, acc_cond::mark_t> pair_t;
    std::map<pair_t, unsigned> s2n;

    std::vector<std::pair<pair_t, unsigned>> todo;

    auto new_state =
      [&](unsigned state, acc_cond::mark_t m) -> unsigned
      {
84
85
86
87
88
89
90
91
92
        pair_t x(state, m);
        auto p = s2n.emplace(x, 0);
        if (p.second)                // This is a new state
          {
            unsigned s = res->new_state();
            p.first->second = s;
            todo.emplace_back(x, s);
          }
        return p.first->second;
93
94
95
      };

    unsigned old_init = old->get_init_state_number();
96
97
98
99
    acc_cond::mark_t init_acc = 0U;
    if (!si.is_rejecting_scc(si.scc_of(old_init)))
      // Use any edge going into the initial state to set the first
      // acceptance mark.
100
      init_acc = one_in[old_init] | common_out[old_init];
101
102
103
104

    res->set_init_state(new_state(old_init, init_acc));
    while (!todo.empty())
      {
105
106
        auto one = todo.back();
        todo.pop_back();
107
108
        unsigned scc_src = si.scc_of(one.first.first);
        bool maybe_accepting = !si.is_rejecting_scc(scc_src);
109
        for (auto& t: old->out(one.first.first))
110
111
112
          {
            unsigned scc_dst = si.scc_of(t.dst);
            acc_cond::mark_t acc = 0U;
113
            bool dst_acc = !si.is_rejecting_scc(scc_dst);
114
115
116
117
118
119
120
121
            if (maybe_accepting && scc_src == scc_dst)
              acc = t.acc - common_out[t.src];
            else if (dst_acc)
              // We enter a new accepting SCC. Use any edge going into
              // t.dst from this SCC to set the initial acceptance mark.
              acc = one_in[t.dst];
            if (dst_acc)
              acc |= common_out[t.dst];
122
123
            else
              acc = unsat_mark.second;
124
125
126
            res->new_edge(one.second, new_state(t.dst, acc),
                          t.cond, one.first.second);
          }
127
      }
128
    res->merge_edges();
129
130
131
    return res;
  }
}