bfssteps.hh 3.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright (C) 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 2 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 Spot; see the file COPYING.  If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.

#ifndef SPOT_TGBAALGOS_BFSSTEPS_HH
# define SPOT_TGBAALGOS_BFSSTEPS_HH

25
26
27
#include <map>
#include <deque>
#include <utility>
28
#include "emptiness.hh"
29
#include "tgba/tgba.hh"
30
31
32

namespace spot
{
33
34
35
36
37
38
  /// \brief Make a BFS in a spot::tgba to compute a tgba_run::steps.
  /// \ingroup tgba_misc
  ///
  /// This class should be used to compute the shortest path
  /// between a state of a spot::tgba and the first transition or
  /// state that matches some conditions.
39
40
41
42
43
44
45
46
47
48
49
50
  class bfs_steps
  {
  public:
    bfs_steps(const tgba* a);
    virtual ~bfs_steps();
    const state* search(const state* start, tgba_run::steps& l);
    virtual const state* filter(const state* s) = 0;
    virtual bool match(tgba_run::step& step, const state* dest) = 0;
  protected:
    const tgba* a_;
  };

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  template<typename T>
  class bfs_steps_with_path_conditions
  {
  public:
    bfs_steps_with_path_conditions(const tgba* a)
      : a_(a)
    {
    }

    virtual
    ~bfs_steps_with_path_conditions()
    {
    }

    virtual const state* filter(const state* s) = 0;
    virtual bool match(tgba_run::step& step, const state* dest,
		       const T& src_cond, T& dest_cond) = 0;

    const state*
    search(const state* start, tgba_run::steps& l)
    {
      // Records backlinks to parent state during the BFS.
      // (This also stores the propositions of this link.)
      std::map<const state*, tgba_run::step,
	state_ptr_less_than> father;
      // BFS queue.
      std::deque<std::pair<const state*, T> > todo;
      // Initial state.
      todo.push_back(start);

      while (!todo.empty())
	{
	  const state* src = todo.front().first;
	  T cond = todo.front().second;
	  todo.pop_front();
	  tgba_succ_iterator* i = a_->succ_iter(src);
	  for (i->first(); !i->done(); i->next())
	    {
	      const state* dest = filter(i->current_state());

	      if (!dest)
		continue;

	      bdd cond = i->current_condition();
	      bdd acc = i->current_acceptance_conditions();
	      tgba_run::step s = { src, cond, acc };

	      std::pair<const state*, T> next;
	      next.first = src;

	      if (match(s, dest, cond, next.second))
		{
		  // Found it!

		  tgba_run::steps p;
		  for (;;)
		    {
		      tgba_run::step tmp = s;
		      tmp.s = tmp.s->clone();
		      p.push_front(tmp);
		      if (s.s == start)
			break;
		      s = father[s.s];
		    }

		  l.splice(l.end(), p);
		  delete i;
		  return dest;
		}

	      // Common case: record backlinks and continue BFS
	      // for unvisited states.
	      if (father.find(dest) == father.end())
		{
		  todo.push_back(next);
		  father[dest] = s;
		}
	    }
	  delete i;
	}
      return 0;

    }

  protected:
    const tgba* a_;
  };

139
140
141
}

#endif // SPOT_TGBAALGOS_BFSSTEPS_HH