1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2016, 2018-2019 Laboratoire de Recherche et
3 // Développement 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 #include "config.h"
21 #include <ctime>
22 #include <vector>
23 #include <spot/twaalgos/dualize.hh>
24 #include <spot/twaalgos/hoa.hh>
25 #include <spot/twaalgos/iscolored.hh>
26 #include <spot/twaalgos/parity.hh>
27 #include <spot/twaalgos/product.hh>
28 #include <spot/twaalgos/randomgraph.hh>
29 #include <spot/misc/random.hh>
30 #include <spot/twaalgos/complete.hh>
31 #include <spot/twa/twagraph.hh>
32 #include <spot/twa/fwd.hh>
33 #include <spot/twa/acc.hh>
34 #include <spot/misc/trival.hh>
35 #include <utility>
36 #include <string>
37 #include <iostream>
38
39 #define LAST_AUT result.back().first
40 #define LAST_NUM_SETS result.back().second
41 #define NEW_AUT() do { \
42 result.emplace_back(spot::random_graph(6, 0.5, &apf, \
43 current_bdd, 0, 0, 0.5, true), 0); \
44 LAST_NUM_SETS = 0; \
45 /* print_hoa need this */ \
46 LAST_AUT->prop_state_acc(spot::trival::maybe()); \
47 } while (false)
48
49 #define SET_TR(t, value) do { \
50 unsigned value_tmp = value; \
51 if (value_tmp + 1 > LAST_NUM_SETS) \
52 LAST_NUM_SETS = value_tmp + 1; \
53 t.acc.set(value_tmp); \
54 } while (false)
55
56
57 static std::vector<std::pair<spot::twa_graph_ptr, unsigned>>
generate_aut(const spot::bdd_dict_ptr & current_bdd)58 generate_aut(const spot::bdd_dict_ptr& current_bdd)
59 {
60 spot::atomic_prop_set apf = spot::create_atomic_prop_set(3);
61 std::vector<std::pair<spot::twa_graph_ptr, unsigned>> result;
62 // No accset on any transition
63 NEW_AUT();
64 // The same accset on every transitions
65 NEW_AUT();
66 for (auto& t: LAST_AUT->edges())
67 SET_TR(t, 0);
68
69 // All used / First unused / Last unused / First and last unused
70 for (auto incr_ext: { 0, 1 })
71 for (auto used: { 1, 2 })
72 for (auto modulo: { 4, 5, 6 })
73 if (incr_ext + modulo <= 6)
74 {
75 NEW_AUT();
76 unsigned count = 0;
77 for (auto& t: LAST_AUT->edges())
78 if (std::rand() % used == 0)
79 {
80 auto value = ++count % modulo + incr_ext;
81 SET_TR(t, value);
82 }
83 }
84
85 // One-Three in middle not used
86 for (auto i: { 0, 1 })
87 for (auto start: { 1, 2 })
88 for (auto unused: { 1, 2, 3 })
89 {
90 NEW_AUT();
91 auto count = 0;
92 for (auto& t: LAST_AUT->edges())
93 {
94 int val = 0;
95 if (count % (3 + i) < start)
96 val = count % (3 + i);
97 else
98 val = count % (3 + i) + unused;
99 SET_TR(t, val);
100 }
101 }
102
103 // All accset on all transitions
104 for (auto i: { 0, 1 })
105 {
106 NEW_AUT();
107 for (auto& t: LAST_AUT->edges())
108 for (auto acc = 0; acc < 5 + i; ++acc)
109 SET_TR(t, acc);
110 }
111
112 // Some random automata
113 std::vector<std::vector<int>> cont_sets;
114 for (auto i = 0; i <= 6; ++i)
115 {
116 std::vector<int> cont_set;
117 for (auto j = 0; j < i; ++j)
118 cont_set.push_back(j);
119 cont_sets.push_back(cont_set);
120 }
121 for (auto min: { 0, 1 })
122 {
123 for (auto num_sets: { 1, 2, 5, 6 })
124 for (auto i = 0; i < 10; ++i)
125 {
126 NEW_AUT();
127 for (auto& t: LAST_AUT->edges())
128 {
129 auto nb_acc = std::rand() % (num_sets - min + 1) + min;
130 spot::mrandom_shuffle(cont_sets[num_sets].begin(),
131 cont_sets[num_sets].end());
132 for (auto j = 0; j < nb_acc; ++j)
133 SET_TR(t, cont_sets[num_sets][j]);
134 }
135 }
136 for (auto num_sets: {2, 3})
137 for (auto even: {0, 1})
138 if ((num_sets - 1) * 2 + even < 6)
139 {
140 NEW_AUT();
141 for (auto& t: LAST_AUT->edges())
142 {
143 auto nb_acc = std::rand() % (num_sets - min + 1) + min;
144 spot::mrandom_shuffle(cont_sets[num_sets].begin(),
145 cont_sets[num_sets].end());
146 for (auto j = 0; j < nb_acc; ++j)
147 {
148 auto value = cont_sets[num_sets][j] * 2 + even;
149 SET_TR(t, value);
150 }
151 }
152 }
153 }
154 return result;
155 }
156
157 static std::vector<std::tuple<spot::acc_cond::acc_code, bool, bool, unsigned>>
generate_acc()158 generate_acc()
159 {
160 std::vector<std::tuple<spot::acc_cond::acc_code, bool, bool, unsigned>>
161 result;
162 for (auto max: { true, false })
163 for (auto odd: { true, false })
164 for (auto num_sets: { 0, 1, 2, 5, 6 })
165 result.emplace_back(spot::acc_cond::acc_code::parity(max, odd,
166 num_sets), max, odd, num_sets);
167 return result;
168 }
169
is_included(spot::const_twa_graph_ptr left,spot::const_twa_graph_ptr right,bool first_left)170 static bool is_included(spot::const_twa_graph_ptr left,
171 spot::const_twa_graph_ptr right, bool first_left)
172 {
173 auto tmp = spot::dualize(right);
174 auto product = spot::product(left, tmp);
175 if (!product->is_empty())
176 {
177 std::cerr << "======Not included======\n";
178 if (first_left)
179 std::cerr << "======First automaton======\n";
180 else
181 std::cerr << "======Second automaton======\n";
182 spot::print_hoa(std::cerr, left) << '\n';
183 if (first_left)
184 std::cerr << "======Second automaton======\n";
185 else
186 std::cerr << "======First automaton======\n";
187 spot::print_hoa(std::cerr, right) << '\n';
188 if (first_left)
189 std::cerr << "======!Second automaton======\n";
190 else
191 std::cerr << "======!First automaton======\n";
192 spot::print_hoa(std::cerr, tmp) << '\n';
193 if (first_left)
194 std::cerr << "======First X !Second======\n";
195 else
196 std::cerr << "======Second X !First======\n";
197 spot::print_hoa(std::cerr, product) << '\n';
198 return false;
199 }
200 return true;
201 }
202
are_equiv(spot::const_twa_graph_ptr left,spot::const_twa_graph_ptr right)203 static bool are_equiv(spot::const_twa_graph_ptr left,
204 spot::const_twa_graph_ptr right)
205 {
206 return is_included(left, right, true) && is_included(right, left, false);
207 }
208
is_right_parity(spot::const_twa_graph_ptr aut,spot::parity_kind target_kind,spot::parity_style target_style,bool origin_max,bool origin_odd,unsigned num_sets)209 static bool is_right_parity(spot::const_twa_graph_ptr aut,
210 spot::parity_kind target_kind,
211 spot::parity_style target_style,
212 bool origin_max, bool origin_odd, unsigned num_sets)
213 {
214 bool is_max;
215 bool is_odd;
216 if (!aut->acc().is_parity(is_max, is_odd))
217 return false;
218 bool target_max;
219 bool target_odd;
220 if (aut->num_sets() <= 1 || num_sets <= 1
221 || target_kind == spot::parity_kind_any)
222 target_max = is_max;
223 else if (target_kind == spot::parity_kind_max)
224 target_max = true;
225 else if (target_kind == spot::parity_kind_min)
226 target_max = false;
227 else
228 target_max = origin_max;
229 if (aut->num_sets() == 0 || num_sets == 0
230 || target_style == spot::parity_style_any)
231 target_odd = is_odd;
232 else if (target_style == spot::parity_style_odd)
233 target_odd = true;
234 else if (target_style == spot::parity_style_even)
235 target_odd = false;
236 else
237 target_odd = origin_odd;
238 if (!(is_max == target_max && is_odd == target_odd))
239 {
240 std::cerr << "======Wrong accceptance======\n";
241 std::string kind[] = { "max", "min", "same", "any" };
242 std::string style[] = { "odd", "even", "same", "any" };
243 std::cerr << "target: " << kind[target_kind] << ' '
244 << style[target_style]
245 << "\norigin: " << kind[origin_max ? 0 : 1] << ' '
246 << style[origin_odd ? 0 : 1] << ' ' << num_sets
247 << "\nactually: " << kind[is_max ? 0 : 1] << ' '
248 << style[is_odd ? 0 : 1] << ' ' << aut->num_sets()
249 << "\n\n";
250 return false;
251 }
252 return true;
253 }
254
is_almost_colored(spot::const_twa_graph_ptr aut)255 static bool is_almost_colored(spot::const_twa_graph_ptr aut)
256 {
257 for (auto t: aut->edges())
258 if (t.acc.count() > 1)
259 {
260 std::cerr << "======Not colored======\n";
261 spot::print_hoa(std::cerr, aut) << '\n';
262 return false;
263 }
264 return true;
265 }
266
is_colored_printerr(spot::const_twa_graph_ptr aut)267 static bool is_colored_printerr(spot::const_twa_graph_ptr aut)
268 {
269 bool result = is_colored(aut);
270 if (!result)
271 {
272 std::cerr << "======Not colored======\n";
273 spot::print_hoa(std::cerr, aut) << '\n';
274 }
275 return result;
276 }
277
to_parity_kind(bool is_max)278 static spot::parity_kind to_parity_kind(bool is_max)
279 {
280 if (is_max)
281 return spot::parity_kind_max;
282 return spot::parity_kind_min;
283 }
284
to_parity_style(bool is_odd)285 static spot::parity_style to_parity_style(bool is_odd)
286 {
287 if (is_odd)
288 return spot::parity_style_odd;
289 return spot::parity_style_even;
290 }
291
main()292 int main()
293 {
294 auto current_bdd = spot::make_bdd_dict();
295 spot::srand(0);
296 auto parity_kinds =
297 {
298 spot::parity_kind_max,
299 spot::parity_kind_min,
300 spot::parity_kind_same,
301 spot::parity_kind_any,
302 };
303 auto parity_styles =
304 {
305 spot::parity_style_odd,
306 spot::parity_style_even,
307 spot::parity_style_same,
308 spot::parity_style_any,
309 };
310
311 auto acceptance_sets = generate_acc();
312 auto automata_tuples = generate_aut(current_bdd);
313
314 unsigned num_automata = automata_tuples.size();
315 unsigned num_acceptance = acceptance_sets.size();
316
317 std::cerr << "num of automata: " << num_automata << '\n';
318 std::cerr << "num of acceptance expression: " << num_acceptance << '\n';
319
320 for (auto acc_tuple: acceptance_sets)
321 for (auto& aut_tuple: automata_tuples)
322 {
323 auto& aut = aut_tuple.first;
324 auto aut_num_sets = aut_tuple.second;
325
326 auto acc = std::get<0>(acc_tuple);
327 auto is_max = std::get<1>(acc_tuple);
328 auto is_odd = std::get<2>(acc_tuple);
329 auto acc_num_sets = std::get<3>(acc_tuple);
330 if (aut_num_sets <= acc_num_sets)
331 {
332 aut->set_acceptance(acc_num_sets, acc);
333 // Check change_parity
334 for (auto kind: parity_kinds)
335 for (auto style: parity_styles)
336 {
337 auto output = spot::change_parity(aut, kind, style);
338
339 if (!is_right_parity(output, kind, style,
340 is_max, is_odd, acc_num_sets))
341 throw std::runtime_error("change parity: wrong acceptance");
342 if (!are_equiv(aut, output))
343 throw std::runtime_error("change_parity: not equivalent.");
344 if (!is_almost_colored(output))
345 throw std::runtime_error(
346 "change_parity: too many acc on a transition");
347 }
348 // Check colorize_parity
349 for (auto keep_style: { true, false })
350 {
351 auto output = spot::colorize_parity(aut, keep_style);
352 if (!is_colored_printerr(output))
353 throw std::runtime_error("colorize_parity: not colored.");
354 if (!are_equiv(aut, output))
355 throw std::runtime_error("colorize_parity: not equivalent.");
356 auto target_kind = to_parity_kind(is_max);
357 auto target_style = keep_style ? to_parity_style(is_odd)
358 : spot::parity_style_any;
359 if (!is_right_parity(output, target_kind, target_style,
360 is_max, is_odd, acc_num_sets))
361 throw std::runtime_error("change_parity: wrong acceptance.");
362 }
363 // Check cleanup_parity
364 for (auto keep_style: { true, false })
365 {
366 auto output = spot::cleanup_parity(aut, keep_style);
367 if (!is_almost_colored(output))
368 throw std::runtime_error(
369 "cleanup_parity: too many acc on a transition.");
370 if (!are_equiv(aut, output))
371 throw std::runtime_error("cleanup_parity: not equivalent.");
372 auto target_kind = to_parity_kind(is_max);
373 auto target_style = keep_style ? to_parity_style(is_odd)
374 : spot::parity_style_any;
375 if (!is_right_parity(output, target_kind, target_style,
376 is_max, is_odd, acc_num_sets))
377 throw std::runtime_error("cleanup_parity: wrong acceptance.");
378 }
379
380 // Check reduce_parity
381 for (auto colored: { true, false })
382 {
383 auto output = spot::reduce_parity(aut, colored);
384 if (!colored && !is_almost_colored(output))
385 throw std::runtime_error(
386 "reduce_parity: too many acc on a transition.");
387 if (colored && !is_colored_printerr(output))
388 throw std::runtime_error("reduce_parity: not colored.");
389 if (!are_equiv(aut, output))
390 throw std::runtime_error("reduce_parity: not equivalent.");
391 if (!is_right_parity(output, to_parity_kind(is_max),
392 spot::parity_style_any,
393 is_max, is_odd, acc_num_sets))
394 throw std::runtime_error("reduce_parity: wrong acceptance.");
395 }
396 }
397 }
398
399 return 0;
400 }
401