complete.cc 3.75 KB
Newer Older
1
// -*- coding: utf-8 -*-
2 3
// Copyright (C) 2013, 2014, 2015 Laboratoire de Recherche et
// Développement de l'Epita.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
//
// 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 "complete.hh"

namespace spot
{
24
  unsigned tgba_complete_here(twa_graph_ptr aut)
25
  {
26 27
    unsigned n = aut->num_states();
    unsigned sink = -1U;
28 29 30 31 32 33

    // UM is a pair (bool, mark).  If the Boolean is false, the
    // acceptance is always satisfiable.  Otherwise, MARK is an
    // example of unsatisfiable mark.
    auto um = aut->get_acceptance().unsat_mark();
    if (!um.first)
34
      {
35 36 37
	// We cannot safely complete an automaton if its
	// acceptance is always satisfiable.
	auto acc = aut->set_buchi();
38
	for (auto& t: aut->transition_vector())
39
	  t.acc = acc;
40
      }
41
    else
42
      {
43 44 45 46 47
	// Loop over the states and seek a state that has only self
	// loops, and that is not accepting.  This will be our sink
	// state.  Note that we do not even have to ensure that the
	// state is complete as we will complete the whole automaton
	// in a second pass.
48
	for (unsigned i = 0; i < n; ++i)
49
	  {
50
	    bool selfloop = true;
51
	    acc_cond::mark_t accsum = 0U;
52 53 54 55 56 57 58 59 60
	    for (auto& t: aut->out(i))
	      {
		if (t.dst != i)	// Not a self-loop
		  {
		    selfloop = false;
		    break;
		  }
		accsum |= t.acc;
	      }
61
	    if (selfloop && !aut->acc().accepting(accsum))
62
	      {
63
		// We have found a sink!
64 65 66
		sink = i;
		break;
	      }
67 68 69
	  }
      }

70 71
    unsigned t = aut->num_transitions();

72
    // Now complete all states (excluding any newly added the sink).
73
    for (unsigned i = 0; i < n; ++i)
74
      {
75
	bdd missingcond = bddtrue;
76
	acc_cond::mark_t acc = 0U;
77
	for (auto& t: aut->out(i))
78
	  {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	    missingcond -= t.cond;
	    // FIXME: This is ugly.
	    //
	    // In case the automaton uses state-based acceptance, we
	    // need to put the new transition in the same set as all
	    // the other.
	    //
	    // In case the automaton uses transition-based acceptance,
	    // it does not matter what acceptance set we put the new
	    // transition into.
	    //
	    // So in both cases, we put the transition in the same
	    // acceptance sets as the last outgoing transition of the
	    // state.
	    acc = t.acc;
94
	  }
95 96
	// If the state has incomplete successors, we need to add a
	// transition to some sink state.
97
	if (missingcond != bddfalse)
98 99 100 101 102
	  {
	    // If we haven't found any sink, simply add one.
	    if (sink == -1U)
	      {
		sink = aut->new_state();
103
		aut->new_transition(sink, sink, bddtrue, um.second);
104 105 106 107 108
	      }
	    // In case the automaton use state-based acceptance, propagate
	    // the acceptance of the first transition to the one we add.
	    aut->new_transition(i, sink, missingcond, acc);
	  }
109
      }
110 111 112 113 114 115 116

    // Get rid of any named property if the automaton changed.
    if (t < aut->num_transitions())
      aut->release_named_properties();
    else
      assert(t == aut->num_transitions());

117 118
    return sink;
  }
119

120
  twa_graph_ptr tgba_complete(const const_twa_ptr& aut)
121
  {
122
    auto res = make_twa_graph(aut, {
123 124 125 126 127
					true, // state based
					true, // inherently_weak
					true, // deterministic
					true, // stutter inv.
				      });
128 129
    tgba_complete_here(res);
    return res;
130 131
  }

132
}