spot 2.11.4.dev
mask.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) 2015, 2016, 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 <spot/twa/twagraph.hh>
23
24namespace spot
25{
51 template<typename Trans>
52 void transform_accessible(const const_twa_graph_ptr& old,
53 twa_graph_ptr& cpy,
54 Trans trans, unsigned int init,
55 bool drop_univ_branches = false)
56 {
57 std::vector<unsigned> todo;
58 std::vector<unsigned> seen(old->num_states(), -1U);
59
60 auto orig_states = new std::vector<unsigned>();
61 orig_states->reserve(old->num_states()); // maybe less are needed.
62 cpy->set_named_prop("original-states", orig_states);
63
64 auto new_state =
65 [&](unsigned old_state) -> unsigned
66 {
67 unsigned tmp = seen[old_state];
68 if (tmp == -1U)
69 {
70 tmp = cpy->new_state();
71 seen[old_state] = tmp;
72 orig_states->emplace_back(old_state);
73 todo.emplace_back(old_state);
74 }
75 return tmp;
76 };
77
78 // Deal with alternating automata, possibly.
79 std::map<std::vector<unsigned>, unsigned> uniq;
80 auto new_univ_state =
81 [&](unsigned old_state) -> unsigned
82 {
83 if (!old->is_univ_dest(old_state))
84 return new_state(old_state);
85
86 std::vector<unsigned> tmp;
87 for (auto s: old->univ_dests(old_state))
88 tmp.emplace_back(new_state(s));
89 std::sort(tmp.begin(), tmp.end());
90 tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
91 auto p = uniq.emplace(tmp, 0);
92 if (p.second)
93 p.first->second =
94 cpy->get_graph().new_univ_dests(tmp.begin(), tmp.end());
95 return p.first->second;
96 };
97
98 cpy->set_init_state(new_univ_state(init));
99 if (seen[init] != 0)
100 throw std::runtime_error
101 ("the destination automaton of transform_accessible() should be empty");
102
103 while (!todo.empty())
104 {
105 unsigned old_src = todo.back();
106 todo.pop_back();
107
108 unsigned new_src = seen[old_src];
109 SPOT_ASSERT(new_src != -1U);
110
111 for (auto& t: old->out(old_src))
112 {
113 bdd cond = t.cond;
114 acc_cond::mark_t acc = t.acc;
115 trans(t.src, cond, acc, t.dst);
116 if (cond == bddfalse)
117 continue;
118 if (drop_univ_branches)
119 for (unsigned d: old->univ_dests(t.dst))
120 cpy->new_edge(new_src, new_state(d), cond, acc);
121 else
122 cpy->new_edge(new_src, new_univ_state(t.dst), cond, acc);
123 }
124 }
125 orig_states->shrink_to_fit();
126 }
127
142 template<typename Trans>
143 void transform_copy(const const_twa_graph_ptr& old,
144 twa_graph_ptr& cpy,
145 Trans trans, unsigned int init)
146 {
147 if (!old->is_existential())
148 throw std::runtime_error
149 ("transform_copy() does not support alternation");
150
151 // Each state in cpy corresponds to a unique state in old.
152 cpy->new_states(old->num_states());
153 cpy->set_init_state(init);
154
155 for (auto& t: old->edges())
156 {
157 bdd cond = t.cond;
158 acc_cond::mark_t acc = t.acc;
159 trans(t.src, cond, acc, t.dst);
160 // Having the same number of states should assure that state ids are
161 // equivilent in old and cpy.
162 SPOT_ASSERT(t.src < cpy->num_states() && t.dst < cpy->num_states());
163 if (cond != bddfalse)
164 cpy->new_edge(t.src, t.dst, cond, acc);
165 }
166 }
167
168 template<typename Trans>
169 void transform_accessible(const const_twa_graph_ptr& old,
170 twa_graph_ptr& cpy,
171 Trans trans)
172 {
173 transform_accessible(old, cpy, trans, old->get_init_state_number());
174 }
175
176 template<typename Trans>
177 void transform_copy(const const_twa_graph_ptr& old,
178 twa_graph_ptr& cpy,
179 Trans trans)
180 {
181 transform_copy(old, cpy, trans, old->get_init_state_number());
182 }
183
185 SPOT_API
186 twa_graph_ptr mask_acc_sets(const const_twa_graph_ptr& in,
187 acc_cond::mark_t to_remove);
188
199 SPOT_API
200 twa_graph_ptr mask_keep_states(const const_twa_graph_ptr& in,
201 std::vector<bool>& to_keep,
202 unsigned int init);
203
215 SPOT_API
216 twa_graph_ptr mask_keep_accessible_states(const const_twa_graph_ptr& in,
217 std::vector<bool>& to_keep,
218 unsigned int init,
219 bool drop_univ_branches = false);
220}
Definition: automata.hh:27
twa_graph_ptr mask_acc_sets(const const_twa_graph_ptr &in, acc_cond::mark_t to_remove)
Remove all edges that belong to some given acceptance sets.
twa_graph_ptr mask_keep_accessible_states(const const_twa_graph_ptr &in, std::vector< bool > &to_keep, unsigned int init, bool drop_univ_branches=false)
Keep only the states specified by to_keep that are accessible.
void transform_accessible(const const_twa_graph_ptr &old, twa_graph_ptr &cpy, Trans trans, unsigned int init, bool drop_univ_branches=false)
Clone and mask an automaton.
Definition: mask.hh:52
twa_graph_ptr mask_keep_states(const const_twa_graph_ptr &in, std::vector< bool > &to_keep, unsigned int init)
Keep only the states as specified by to_keep.
void transform_copy(const const_twa_graph_ptr &old, twa_graph_ptr &cpy, Trans trans, unsigned int init)
Copy an automaton and update each edge.
Definition: mask.hh:143
An acceptance mark.
Definition: acc.hh:90

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.9.4