hoa.cc 12.4 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2011, 2012, 2014, 2015 Laboratoire de Recherche et
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Developpement de l'Epita (LRDE).
// Copyright (C) 2003, 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie.
//
// 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 <ostream>
#include <sstream>
25
#include <cstring>
26
#include <map>
27
28
#include "twa/twa.hh"
#include "twa/twagraph.hh"
29
#include "hoa.hh"
30
31
32
33
#include "reachiter.hh"
#include "misc/escape.hh"
#include "misc/bddlt.hh"
#include "misc/minato.hh"
34
#include "twa/formula2bdd.hh"
35
#include "ltlast/atomic_prop.hh"
36
37
38
39
40

namespace spot
{
  namespace
  {
41
    struct metadata final
42
43
44
45
46
47
48
49
    {
      // Assign a number to each atomic proposition.
      typedef std::map<int, unsigned> ap_map;
      ap_map ap;
      typedef std::vector<int> vap_t;
      vap_t vap;

      std::vector<bool> common_acc;
50
      bool has_state_acc;
51
52
      bool is_complete;
      bool is_deterministic;
53
      bool is_colored;
54
55
      bool use_implicit_labels;
      bdd all_ap;
56
57
58
59
60
61

      // Label support: the set of all conditions occurring in the
      // automaton.
      typedef std::map<bdd, std::string, bdd_less_than> sup_map;
      sup_map sup;

62
      metadata(const const_twa_graph_ptr& aut, bool implicit)
63
      {
64
	check_det_and_comp(aut);
65
	use_implicit_labels = implicit && is_deterministic && is_complete;
66
67
68
69
	number_all_ap();
      }

      std::ostream&
70
      emit_acc(std::ostream& os,
71
	       const const_twa_graph_ptr& aut,
72
	       acc_cond::mark_t b)
73
74
      {
	// FIXME: We could use a cache for this.
75
	if (b == 0U)
76
77
78
	  return os;
	os << " {";
	bool notfirst = false;
79
	for (auto v: aut->acc().sets(b))
80
81
82
83
84
	  {
	    if (notfirst)
	      os << ' ';
	    else
	      notfirst = true;
85
	    os << v;
86
87
88
89
90
	  }
	os << '}';
	return os;
      }

91
      void check_det_and_comp(const const_twa_graph_ptr& aut)
92
93
94
      {
	std::string empty;

95
96
97
98
	unsigned ns = aut->num_states();
	bool deterministic = true;
	bool complete = true;
	bool state_acc = true;
99
100
	bool nodeadend = true;
	bool colored = aut->acc().num_sets() >= 1;
101
	for (unsigned src = 0; src < ns; ++src)
102
103
104
	  {
	    bdd sum = bddfalse;
	    bdd available = bddtrue;
105
106
107
	    bool st_acc = true;
	    bool notfirst = false;
	    acc_cond::mark_t prev = 0U;
108
	    bool has_succ = false;
109
	    for (auto& t: aut->out(src))
110
	      {
111
112
113
		if (complete)
		  sum |= t.cond;
		if (deterministic)
114
		  {
115
116
		    if (!bdd_implies(t.cond, available))
		      deterministic = false;
117
		    else
118
		      available -= t.cond;
119
		  }
120
		sup.insert(std::make_pair(t.cond, empty));
121
122
		if (st_acc)
		  {
123
		    if (notfirst && prev != t.acc)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
124
125
126
		      {
			st_acc = false;
		      }
127
		    else
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
128
129
		      {
			notfirst = true;
130
			prev = t.acc;
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
131
		      }
132
		  }
133
134
135
136
137
138
139
		if (colored)
		  {
		    auto a = t.acc;
		    if (!a || a.remove_some(1))
		      colored = false;
		  }
		has_succ = true;
140
	      }
141
	    nodeadend &= has_succ;
142
143
	    if (complete)
	      complete &= sum == bddtrue;
144
145
146
	    common_acc.push_back(st_acc);
	    state_acc &= st_acc;
	  }
147
148
149
	is_deterministic = deterministic;
	is_complete = complete;
	has_state_acc = state_acc;
150
151
152
153
	// If the automaton has state-based acceptance and contain
	// some states without successors do not declare it as
	// colored.
	is_colored = colored && (!has_state_acc || nodeadend);
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
154
155
156
157
	// If the automaton declares that it is deterministic or
	// state-based, make sure that it really is.
	assert(!aut->is_deterministic() || deterministic);
	assert(!aut->has_state_based_acc() || state_acc);
158
159
160
161
162
      }

      void number_all_ap()
      {
	bdd all = bddtrue;
163
164
	for (auto& i: sup)
	  all &= bdd_support(i.first);
165
	all_ap = all;
166
167
168
169
170
171
172
173
174

	while (all != bddtrue)
	  {
	    int v = bdd_var(all);
	    all = bdd_high(all);
	    ap.insert(std::make_pair(v, vap.size()));
	    vap.push_back(v);
	  }

175
176
177
	if (use_implicit_labels)
	  return;

178
	for (auto& i: sup)
179
	  {
180
	    bdd cond = i.first;
181
182
	    if (cond == bddtrue)
	      {
183
		i.second = "t";
184
185
186
187
		continue;
	      }
	    if (cond == bddfalse)
	      {
188
		i.second = "f";
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
		continue;
	      }
	    std::ostringstream s;
	    bool notfirstor = false;

	    minato_isop isop(cond);
	    bdd cube;
	    while ((cube = isop.next()) != bddfalse)
	      {
		if (notfirstor)
		  s << " | ";
		bool notfirstand = false;
		while (cube != bddtrue)
		  {
		    if (notfirstand)
		      s << '&';
		    else
		      notfirstand = true;
		    bdd h = bdd_high(cube);
		    if (h == bddfalse)
		      {
			s << '!' << ap[bdd_var(cube)];
			cube = bdd_low(cube);
		      }
		    else
		      {
			s << ap[bdd_var(cube)];
			cube = h;
		      }
		  }
		notfirstor = true;
	      }
221
	    i.second = s.str();
222
223
224
225
226
227
	  }
      }
    };

  }

228
229
230
231
232
233
234
235
236
  enum hoa_acceptance
    {
      Hoa_Acceptance_States,	/// state-based acceptance if
				/// (globally) possible
				/// transition-based acceptance
				/// otherwise.
      Hoa_Acceptance_Transitions, /// transition-based acceptance globally
      Hoa_Acceptance_Mixed    /// mix state-based and transition-based
    };
237

238
  static std::ostream&
239
  hoa_reachable(std::ostream& os,
240
		const const_twa_graph_ptr& aut,
241
		const char* opt)
242
  {
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
    bool newline = true;
    hoa_acceptance acceptance = Hoa_Acceptance_States;
    bool implicit_labels = false;

    if (opt)
      while (*opt)
	{
	  switch (*opt++)
	    {
	    case 'i':
	      implicit_labels = true;
	      break;
	    case 'l':
	      newline = false;
	      break;
	    case 'm':
	      acceptance = Hoa_Acceptance_Mixed;
	      break;
	    case 's':
	      acceptance = Hoa_Acceptance_States;
	      break;
	    case 't':
	      acceptance = Hoa_Acceptance_Transitions;
	      break;
	    }
	}
269

270
271
272
273
    // Calling get_init_state_number() may add a state to empty
    // automata, so it has to be done first.
    unsigned init = aut->get_init_state_number();

274
    metadata md(aut, implicit_labels);
275

276
    if (acceptance == Hoa_Acceptance_States && !md.has_state_acc)
277
      acceptance = Hoa_Acceptance_Transitions;
278

279
    unsigned num_states = aut->num_states();
280
281
282

    const char nl = newline ? '\n' : ' ';
    os << "HOA: v1" << nl;
283
    auto n = aut->get_named_prop<std::string>("automaton-name");
284
    if (n)
285
      escape_str(os << "name: \"", *n) << '"' << nl;
286
    unsigned nap = md.vap.size();
287
    os << "States: " << num_states << nl
288
       << "Start: " << init << nl
289
       << "AP: " << nap;
290
    auto d = aut->get_dict();
291
    for (auto& i: md.vap)
292
      {
293
	auto f = ltl::is_atomic_prop(d->bdd_map[i].f);
294
295
296
	assert(f);
	escape_str(os << " \"", f->name()) << '"';
      }
297
    os << nl;
298

299
    unsigned num_acc = aut->acc().num_sets();
300
    acc_cond::acc_code acc_c = aut->acc().get_acceptance();
301
    if (aut->acc().is_generalized_buchi())
302
      {
303
	if (aut->acc().is_true())
304
	  os << "acc-name: all";
305
	else if (aut->acc().is_buchi())
306
307
308
309
	  os << "acc-name: Buchi";
	else
	  os << "acc-name: generalized-Buchi " << num_acc;
	os << nl;
310
      }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    else if (aut->acc().is_generalized_co_buchi())
      {
	if (aut->acc().is_false())
	  os << "acc-name: none";
	else if (aut->acc().is_co_buchi())
	  os << "acc-name: co-Buchi";
	else
	  os << "acc-name: generalized-co-Buchi " << num_acc;
	os << nl;
      }
    else
      {
	int r = aut->acc().is_rabin();
	assert(r != 0);
	if (r > 0)
	  {
	    os << "acc-name: Rabin " << r << nl;
328
329
330
	    // Force the acceptance to remove any duplicate sets, and
	    // make sure it is correctly ordered.
	    acc_c = acc_cond::acc_code::rabin(r);
331
332
333
334
335
336
337
338
	  }
	else
	  {
	    r = aut->acc().is_streett();
	    assert(r != 0);
	    if (r > 0)
	      {
		os << "acc-name: Streett " << r << nl;
339
340
341
		// Force the acceptance to remove any duplicate sets, and
		// make sure it is correctly ordered.
		acc_c = acc_cond::acc_code::streett(r);
342
343
344
345
346
347
348
349
350
351
	      }
	    else
	      {
		std::vector<unsigned> pairs;
		if (aut->acc().is_generalized_rabin(pairs))
		  {
		    os << "acc-name: generalized-Rabin " << pairs.size();
		    for (auto p: pairs)
		      os << ' ' << p;
		    os << nl;
352
353
354
355
		    // Force the acceptance to remove any duplicate
		    // sets, and make sure it is correctly ordered.
		    acc_c = acc_cond::acc_code::generalized_rabin(pairs.begin(),
								  pairs.end());
356
		  }
357
358
359
360
361
362
363
364
365
366
		else
		  {
		    bool max = false;
		    bool odd = false;
		    if (aut->acc().is_parity(max, odd))
		      os << "acc-name: parity "
			 << (max ? "max " : "min ")
			 << (odd ? "odd " : "even ")
			 << num_acc << nl;
		  }
367
368
369
	      }
	  }
      }
370
    os << "Acceptance: " << num_acc << ' ';
371
    os << acc_c;
372
    os << nl;
373
    os << "properties:";
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
    // Make sure the property line is not too large,
    // otherwise our test cases do not fit in 80 columns...
    unsigned prop_len = 60;
    auto prop = [&](const char* str)
      {
	if (newline)
	  {
	    auto l = strlen(str);
	    if (prop_len < l)
	      {
		prop_len = 60;
		os << "\nproperties:";
	      }
	    prop_len -= l;
	  }
	os << str;
      };
391
392
    implicit_labels = md.use_implicit_labels;
    if (implicit_labels)
393
      prop(" implicit-labels");
394
    else
395
      prop(" trans-labels explicit-labels");
396
    if (acceptance == Hoa_Acceptance_States)
397
      prop(" state-acc");
398
    else if (acceptance == Hoa_Acceptance_Transitions)
399
      prop(" trans-acc");
400
401
    if (md.is_colored)
      prop(" colored");
402
    if (md.is_complete)
403
      prop(" complete");
404
    if (md.is_deterministic)
405
      prop(" deterministic");
406
407
    if (aut->is_unambiguous())
      prop(" unambiguous");
408
    assert(!(aut->is_stutter_invariant() && aut->is_stutter_sensitive()));
409
410
    if (aut->is_stutter_invariant())
      prop(" stutter-invariant");
411
412
    if (aut->is_stutter_sensitive())
      prop(" stutter-sensitive");
413
414
    if (aut->is_inherently_weak())
      prop(" inherently-weak");
415
    os << nl;
416
417
418
419
420
421
422
423
424
425
426
427

    // If we want to output implicit labels, we have to
    // fill a vector with all destinations in order.
    std::vector<unsigned> out;
    std::vector<acc_cond::mark_t> outm;
    if (implicit_labels)
      {
	out.resize(1UL << nap);
	if (acceptance != Hoa_Acceptance_States)
	  outm.resize(1UL << nap);
      }

428
    os << "--BODY--" << nl;
429
    auto sn = aut->get_named_prop<std::vector<std::string>>("state-names");
430
431
    for (unsigned i = 0; i < num_states; ++i)
      {
432
433
	hoa_acceptance this_acc = acceptance;
	if (this_acc == Hoa_Acceptance_Mixed)
434
	  this_acc = (md.common_acc[i] ?
435
		      Hoa_Acceptance_States : Hoa_Acceptance_Transitions);
436
437

	os << "State: " << i;
438
439
	if (sn && i < sn->size() && !(*sn)[i].empty())
	  os << " \"" << (*sn)[i] << '"';
440
441
442
443
444
445
446
447
448
449
	if (this_acc == Hoa_Acceptance_States)
	  {
	    acc_cond::mark_t acc = 0U;
	    for (auto& t: aut->out(i))
	      {
		acc = t.acc;
		break;
	      }
	    md.emit_acc(os, aut, acc);
	  }
450
451
	os << nl;

452
453
454
455
456
457
458
459
460
461
462
	if (!implicit_labels)
	  {
	    for (auto& t: aut->out(i))
	      {
		os << '[' << md.sup[t.cond] << "] " << t.dst;
		if (this_acc == Hoa_Acceptance_Transitions)
		  md.emit_acc(os, aut, t.acc);
		os << nl;
	      }
	  }
	else
463
	  {
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
	    for (auto& t: aut->out(i))
	      {
		bdd cond = t.cond;
		while (cond != bddfalse)
		  {
		    bdd one = bdd_satoneset(cond, md.all_ap, bddfalse);
		    cond -= one;
		    unsigned level = 1;
		    unsigned pos = 0U;
		    while (one != bddtrue)
		      {
			bdd h = bdd_high(one);
			if (h == bddfalse)
			  {
			    one = bdd_low(one);
			  }
			else
			  {
			    pos |= level;
			    one = h;
			  }
			level <<= 1;
		      }
		    out[pos] = t.dst;
		    if (this_acc != Hoa_Acceptance_States)
		      outm[pos] = t.acc;
		  }
	      }
	    unsigned n = out.size();
	    for (unsigned i = 0; i < n;)
	      {
		os << out[i];
		if (this_acc != Hoa_Acceptance_States)
		  {
		    md.emit_acc(os, aut, outm[i]) << nl;
		    ++i;
		  }
		else
		  {
		    ++i;
		    os << (((i & 15) && i < n) ? ' ' : nl);
		  }
	      }
507
508
509
510
511
512
513
	  }
      }
    os << "--END--";		// No newline.  Let the caller decide.
    return os;
  }

  std::ostream&
514
  hoa_reachable(std::ostream& os,
515
		const const_twa_ptr& aut,
516
		const char* opt)
517
  {
518

519
    auto a = std::dynamic_pointer_cast<const twa_graph>(aut);
520
    if (!a)
521
      a = make_twa_graph(aut, twa::prop_set::all());
522

523
    return hoa_reachable(os, a, opt);
524
525
526
  }

}