tau03.cc 12.8 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// 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 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.

//#define TRACE

#ifdef TRACE
#include <iostream>
#include "tgba/bddprint.hh"
#endif

#include <cassert>
#include <list>
#include <iterator>
#include "misc/hash.hh"
#include "tgba/tgba.hh"
#include "emptiness.hh"
#include "magic.hh"
#include "tau03.hh"

namespace spot
{
  namespace
  {
    enum color {WHITE, CYAN, BLUE};

    /// \brief Emptiness checker on spot::tgba automata having at most one
    /// accepting condition (i.e. a TBA).
    template <typename heap>
    class tau03_search : public emptiness_check
    {
    public:
      /// \brief Initialize the search algorithm on the automaton \a a
      tau03_search(const tgba *a, size_t size)
        : h(size), a(a), all_cond(a->all_acceptance_conditions())
      {
        assert(a->number_of_acceptance_conditions() > 0);
      }

      virtual ~tau03_search()
      {
        // Release all iterators on the stacks.
        while (!st_blue.empty())
          {
            h.pop_notify(st_blue.front().s);
            delete st_blue.front().it;
            st_blue.pop_front();
          }
        while (!st_red.empty())
          {
            h.pop_notify(st_red.front().s);
            delete st_red.front().it;
            st_red.pop_front();
          }
      }

      /// \brief Perform a Magic Search.
      ///
      /// \return non null pointer iff the algorithm has found an
      /// accepting path.
      virtual emptiness_check_result* check()
      {
        if (!st_red.empty())
          {
            assert(!st_blue.empty());
            return 0;
          }
        assert(st_blue.empty());
        nbn = nbt = 0;
        sts = mdp = 0;
        const state* s0 = a->get_init_state();
        ++nbn;
        h.add_new_state(s0, CYAN);
        push(st_blue, s0, bddfalse, bddfalse);
        if (dfs_blue())
          return new emptiness_check_result;
        return 0;
      }

      virtual std::ostream& print_stats(std::ostream &os) const
      {
        os << nbn << " distinct nodes visited" << std::endl;
        os << nbt << " transitions explored" << std::endl;
        os << mdp << " nodes for the maximal stack depth" << std::endl;
        return os;
      }

    private:
      /// \brief counters for statistics (number of distinct nodes, of
      /// transitions and maximal stacks size.
      int nbn, nbt, mdp, sts;

      struct stack_item
      {
        stack_item(const state* n, tgba_succ_iterator* i, bdd l, bdd a)
          : s(n), it(i), label(l), acc(a) {};
        /// The visited state.
        const state* s;
        /// Design the next successor of \a s which has to be visited.
        tgba_succ_iterator* it;
        /// The label of the transition traversed to reach \a s
        /// (false for the first one).
        bdd label;
        /// The acceptance set of the transition traversed to reach \a s
        /// (false for the first one).
        bdd acc;
      };

      typedef std::list<stack_item> stack_type;

      void push(stack_type& st, const state* s,
                        const bdd& label, const bdd& acc)
      {
        ++sts;
        if (sts>mdp)
          mdp = sts;
        tgba_succ_iterator* i = a->succ_iter(s);
        i->first();
        st.push_front(stack_item(s, i, label, acc));
      }

      /// \brief Stack of the blue dfs.
      stack_type st_blue;

      /// \brief Stack of the red dfs.
      stack_type st_red;

      /// \brief Map where each visited state is colored
      /// by the last dfs visiting it.
      heap h;

      /// State targeted by the red dfs.
      const state* target;

      /// The automata to check.
      const tgba* a;

      /// The unique accepting condition of the automaton \a a.
      bdd all_cond;

      bool dfs_blue()
      {
        while (!st_blue.empty())
          {
            stack_item& f = st_blue.front();
#ifdef TRACE
            std::cout << "DFS_BLUE treats: "
                      << a->format_state(f.s) << std::endl;
#endif
            if (!f.it->done())
              {
                ++nbt;
                const state *s_prime = f.it->current_state();
                bdd label = f.it->current_condition();
                bdd acc = f.it->current_acceptance_conditions();
#ifdef TRACE
                std::cout << "  Visit the successor: "
                          << a->format_state(s_prime) << std::endl;
#endif
                f.it->next();
                typename heap::color_ref c = h.get_color_ref(s_prime);
                if (c.is_white())
                // Go down the edge (f.s, <label, acc>, s_prime)
                  {
                    ++nbn;
#ifdef TRACE
                    std::cout << "  It is white, go down" << std::endl;
#endif
                    h.add_new_state(s_prime, CYAN);
                    push(st_blue, s_prime, label, acc);
                  }
                else // Backtrack the edge (f.s, <label, acc>, s_prime)
                  {
#ifdef TRACE
                      std::cout << "  It is cyan or blue, pop it"
                                << std::endl;
#endif
                      h.pop_notify(s_prime);
                  }
              }
            else
            // Backtrack the edge
            //        (predecessor of f.s in st_blue, <f.label, f.acc>, f.s)
              {
#ifdef TRACE
                std::cout << "  All the successors have been visited"
                          << ", rescan this successors"
                          << std::endl;
#endif
                typename heap::color_ref c = h.get_color_ref(f.s);
                assert(!c.is_white());
                tgba_succ_iterator* i = a->succ_iter(f.s);
                for (i->first(); !i->done(); i->next())
                  {
                    ++nbt;
                    const state *s_prime = i->current_state();
                    bdd label = i->current_condition();
                    bdd acc = i->current_acceptance_conditions();
                    typename heap::color_ref c_prime = h.get_color_ref(s_prime);
                    assert(!c_prime.is_white());
                    bdd acu = acc | c.get_acc();
#ifdef TRACE
                    std::cout << "DFS_BLUE rescanning from: "
                              << a->format_state(f.s) << std::endl;
                    std::cout << "  tests "
                              << a->format_state(s_prime) << std::endl;
                    std::cout << "  if ";
                    bdd_print_acc(std::cout, a->get_dict(), acu);
                    std::cout << std::endl;
                    std::cout << "  is not included in ";
                    bdd_print_acc(std::cout, a->get_dict(), c_prime.get_acc());
                    std::cout << std::endl;
#endif
                    if ((c_prime.get_acc() & acu) != acu)
                      {
#ifdef TRACE
                        std::cout << "  that is true, starts a red dfs with "
                                  << a->format_state(s_prime)
                                  << "  propagating: ";
                        bdd_print_acc(std::cout, a->get_dict(), acu);
                        std::cout << std::endl;
#endif
                        target = f.s;
                        c_prime.cumulate_acc(acu);
                        push(st_red, s_prime, label, acc);
                        if (target->compare(s_prime) == 0 &&
                            c_prime.get_acc() == all_cond)
                          {
                            delete i;
                            return true;
                          }
                        if (dfs_red(acu) || c.get_acc() == all_cond)
                          {
                            delete i;
                            return true;
                          }
                     }
                  }
                delete i;
                c.set_color(BLUE);
                delete f.it;
                --sts;
                h.pop_notify(f.s);
                st_blue.pop_front();
              }
          }
        return false;
      }

      bool dfs_red(const bdd& acu)
      {
        assert(!st_red.empty());

        while (!st_red.empty())
          {
            stack_item& f = st_red.front();
#ifdef TRACE
            std::cout << "DFS_RED treats: "
                      << a->format_state(f.s) << std::endl;
#endif
            if (!f.it->done()) // Go down
              {
                ++nbt;
                const state *s_prime = f.it->current_state();
                bdd label = f.it->current_condition();
                bdd acc = f.it->current_acceptance_conditions();
#ifdef TRACE
                std::cout << "  Visit the successor: "
                          << a->format_state(s_prime) << std::endl;
#endif
                f.it->next();
                typename heap::color_ref c_prime = h.get_color_ref(s_prime);
                if (!c_prime.is_white() &&
                    (c_prime.get_acc() & acu) != acu)
                  {
#ifdef TRACE
                        std::cout << "  It is cyan or blue and propagation "
                                  << "is nedded, go down"
                                  << std::endl;
#endif
                    c_prime.cumulate_acc(acu);
                    push(st_red, s_prime, label, acc);
                    if (target->compare(s_prime) == 0  &&
                            c_prime.get_acc() == all_cond)
                      return true;
                  }
                  else
                    {
#ifdef TRACE
                      std::cout << "  It is white or no propagation is needed "
                                << ", pop it"
                                << std::endl;
#endif
                      if (c_prime.is_white())
                        delete s_prime;
                      h.pop_notify(s_prime);
                    }
              }
            else // Backtrack
              {
#ifdef TRACE
                std::cout << "  All the successors have been visited"
                          << ", pop it" << std::endl;
#endif
                --sts;
                h.pop_notify(f.s);
                delete f.it;
                st_red.pop_front();
              }
          }
        return false;
      }

    };

    class explicit_tau03_search_heap
    {
    public:
      class color_ref
      {
      public:
        color_ref(color* c, bdd* a) :p(c), acc(a)
          {
          }
        color get_color() const
          {
            return *p;
          }
        void set_color(color c)
          {
            assert(!is_white());
            *p = c;
          }
        const bdd& get_acc() const
          {
            assert(!is_white());
            return *acc;
          }
        void cumulate_acc(const bdd& a)
          {
            assert(!is_white());
            *acc |= a;
          }
        bool is_white() const
          {
            return p == 0;
          }
      private:
        color *p;
        bdd* acc;
      };

      explicit_tau03_search_heap(size_t)
        {
        }

      ~explicit_tau03_search_heap()
        {
          hash_type::const_iterator s = h.begin();
          while (s != h.end())
            {
              // Advance the iterator before deleting the "key" pointer.
              const state* ptr = s->first;
              ++s;
              delete ptr;
            }
        }

      color_ref get_color_ref(const state*& s)
        {
          hash_type::iterator it = h.find(s);
          if (it==h.end())
            return color_ref(0, 0);
          if (s!=it->first)
            {
              delete s;
              s = it->first;
            }
          return color_ref(&(it->second.first), &(it->second.second));
        }

      void add_new_state(const state* s, color c)
        {
          assert(h.find(s)==h.end());
          h.insert(std::make_pair(s, std::make_pair(c, bddfalse)));
        }

      void pop_notify(const state*)
        {
        }

    private:

      typedef Sgi::hash_map<const state*, std::pair<color, bdd>,
                state_ptr_hash, state_ptr_equal> hash_type;
      hash_type h;
    };

  } // anonymous

  emptiness_check* explicit_tau03_search(const tgba *a)
  {
    return new tau03_search<explicit_tau03_search_heap>(a, 0);
  }

}