hoa.cc 14.3 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 "tl/formula.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
      bool use_implicit_labels;
55
      bool use_state_labels = true;
56
      bdd all_ap;
57
58
59
60
61
62

      // 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;

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

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

94
      void check_det_and_comp(const const_twa_graph_ptr& aut)
95
96
97
      {
	std::string empty;

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

      void number_all_ap()
      {
	bdd all = bddtrue;
171
172
	for (auto& i: sup)
	  all &= bdd_support(i.first);
173
	all_ap = all;
174
175
176
177
178
179
180
181
182

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

183
184
185
	if (use_implicit_labels)
	  return;

186
	for (auto& i: sup)
187
	  {
188
	    bdd cond = i.first;
189
190
	    if (cond == bddtrue)
	      {
191
		i.second = "t";
192
193
194
195
		continue;
	      }
	    if (cond == bddfalse)
	      {
196
		i.second = "f";
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
		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;
	      }
229
	    i.second = s.str();
230
231
232
233
234
235
	  }
      }
    };

  }

236
237
238
239
240
241
242
243
244
  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
    };
245

246
  static std::ostream&
247
  print_hoa(std::ostream& os,
248
		const const_twa_graph_ptr& aut,
249
		const char* opt)
250
  {
251
252
253
    bool newline = true;
    hoa_acceptance acceptance = Hoa_Acceptance_States;
    bool implicit_labels = false;
254
    bool verbose = false;
255
    bool state_labels = false;
256
257
258
259

    if (opt)
      while (*opt)
	{
260
	  switch (char c = *opt++)
261
262
263
264
	    {
	    case 'i':
	      implicit_labels = true;
	      break;
265
266
267
	    case 'k':
	      state_labels = true;
	      break;
268
269
270
271
272
273
274
275
276
277
278
279
	    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;
280
281
282
	    case 'v':
	      verbose = true;
	      break;
283
284
285
	    default:
	      throw std::runtime_error
		(std::string("unknown option for print_hoa(): ") + c);
286
287
	    }
	}
288

289
290
291
292
    // 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();

293
    metadata md(aut, implicit_labels, state_labels);
294

295
    if (acceptance == Hoa_Acceptance_States && !md.has_state_acc)
296
      acceptance = Hoa_Acceptance_Transitions;
297

298
    unsigned num_states = aut->num_states();
299
300
301

    const char nl = newline ? '\n' : ' ';
    os << "HOA: v1" << nl;
302
    auto n = aut->get_named_prop<std::string>("automaton-name");
303
    if (n)
304
      escape_str(os << "name: \"", *n) << '"' << nl;
305
    unsigned nap = md.vap.size();
306
    os << "States: " << num_states << nl
307
       << "Start: " << init << nl
308
       << "AP: " << nap;
309
    auto d = aut->get_dict();
310
    for (auto& i: md.vap)
311
      escape_str(os << " \"", d->bdd_map[i].f.ap_name()) << '"';
312
    os << nl;
313

314
    unsigned num_acc = aut->num_sets();
315
    acc_cond::acc_code acc_c = aut->acc().get_acceptance();
316
    if (aut->acc().is_generalized_buchi())
317
      {
318
	if (aut->acc().is_tt())
319
	  os << "acc-name: all";
320
	else if (aut->acc().is_buchi())
321
322
323
324
	  os << "acc-name: Buchi";
	else
	  os << "acc-name: generalized-Buchi " << num_acc;
	os << nl;
325
      }
326
327
    else if (aut->acc().is_generalized_co_buchi())
      {
328
	if (aut->acc().is_ff())
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	  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;
343
344
345
	    // Force the acceptance to remove any duplicate sets, and
	    // make sure it is correctly ordered.
	    acc_c = acc_cond::acc_code::rabin(r);
346
347
348
349
350
351
352
353
	  }
	else
	  {
	    r = aut->acc().is_streett();
	    assert(r != 0);
	    if (r > 0)
	      {
		os << "acc-name: Streett " << r << nl;
354
355
356
		// Force the acceptance to remove any duplicate sets, and
		// make sure it is correctly ordered.
		acc_c = acc_cond::acc_code::streett(r);
357
358
359
360
361
362
363
364
365
366
	      }
	    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;
367
368
369
370
		    // 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());
371
		  }
372
373
374
375
376
377
378
379
380
381
		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;
		  }
382
383
384
	      }
	  }
      }
385
    os << "Acceptance: " << num_acc << ' ';
386
    os << acc_c;
387
    os << nl;
388
    os << "properties:";
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    // 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;
      };
406
407
408
409
410
411
412
    // We do not support alternating automata so far, and it's
    // probable that nobody cares about the "no-univ-branch"
    // properties.  The "univ-branch" properties seems more important
    // to announce that the automaton might not be parsable by tools
    // that do not support alternating automata.
    if (verbose)
      prop(" no-univ-branch");
413
    implicit_labels = md.use_implicit_labels;
414
    state_labels = md.use_state_labels;
415
    if (implicit_labels)
416
      prop(" implicit-labels");
417
418
    else if (state_labels)
      prop(" state-labels explicit-labels");
419
    else
420
      prop(" trans-labels explicit-labels");
421
    if (acceptance == Hoa_Acceptance_States)
422
      prop(" state-acc");
423
    else if (acceptance == Hoa_Acceptance_Transitions)
424
      prop(" trans-acc");
425
426
    if (md.is_colored)
      prop(" colored");
427
    if (md.is_complete)
428
      prop(" complete");
429
    if (md.is_deterministic)
430
      prop(" deterministic");
431
432
433
434
435
436
    // Deterministic automata are also unambiguous, so writing both
    // properties seems redundant.  People working on unambiguous
    // automata are usually concerned about non-deterministic
    // unambiguous automata.  So do not mention "unambiguous"
    // in the case of deterministic automata.
    if (aut->prop_unambiguous() && (verbose || !md.is_deterministic))
437
      prop(" unambiguous");
438
439
    assert(!(aut->prop_stutter_invariant() && aut->prop_stutter_sensitive()));
    if (aut->prop_stutter_invariant())
440
      prop(" stutter-invariant");
441
    if (aut->prop_stutter_sensitive())
442
      prop(" stutter-sensitive");
443
444
445
    if (aut->prop_terminal())
      prop(" terminal");
    if (aut->prop_weak() && (verbose || !aut->prop_terminal()))
446
447
      prop(" weak");
    if (aut->prop_inherently_weak() && (verbose || !aut->prop_weak()))
448
      prop(" inherently-weak");
449
    os << nl;
450
451
452
453
454
455
456
457
458
459
460
461

    // 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);
      }

462
    os << "--BODY--" << nl;
463
    auto sn = aut->get_named_prop<std::vector<std::string>>("state-names");
464
465
    for (unsigned i = 0; i < num_states; ++i)
      {
466
467
	hoa_acceptance this_acc = acceptance;
	if (this_acc == Hoa_Acceptance_Mixed)
468
	  this_acc = (md.common_acc[i] ?
469
		      Hoa_Acceptance_States : Hoa_Acceptance_Transitions);
470

471
472
473
474
475
476
477
478
479
480
481
482
483
484
	os << "State: ";
	if (state_labels)
	  {
	    bool output = false;
	    for (auto& t: aut->out(i))
	      {
		os << '[' << md.sup[t.cond] << "] ";
		output = true;
		break;
	      }
	    if (!output)
	      os << "[f] ";
	  }
	os << i;
485
486
	if (sn && i < sn->size() && !(*sn)[i].empty())
	  os << " \"" << (*sn)[i] << '"';
487
488
489
490
491
492
493
494
495
496
	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);
	  }
497
498
	os << nl;

499
	if (!implicit_labels && !state_labels)
500
	  {
501

502
503
504
505
506
507
508
509
	    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;
	      }
	  }
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
	else if (state_labels)
	  {
	    unsigned n = 0;
	    for (auto& t: aut->out(i))
	      {
		os << t.dst;
		if (this_acc == Hoa_Acceptance_Transitions)
		  {
		    md.emit_acc(os, aut, t.acc);
		    os << nl;
		  }
		else
		  {
		    ++n;
		    os << (((n & 15) && t.next_succ) ? ' ' : nl);
		  }
	      }
	  }
528
	else
529
	  {
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
	    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);
		  }
	      }
573
574
575
576
577
578
579
	  }
      }
    os << "--END--";		// No newline.  Let the caller decide.
    return os;
  }

  std::ostream&
580
  print_hoa(std::ostream& os,
581
		const const_twa_ptr& aut,
582
		const char* opt)
583
  {
584

585
    auto a = std::dynamic_pointer_cast<const twa_graph>(aut);
586
    if (!a)
587
      a = make_twa_graph(aut, twa::prop_set::all());
588

589
    return print_hoa(os, a, opt);
590
591
592
  }

}