Commit f52082bc authored by Denis Poitrenaud's avatar Denis Poitrenaud
Browse files

* src/tgbaalgos/magic.cc: rewrite to externalize the heap and

prepare it to a bit state hashing version.
* src/tgbaalgos/magic.hh: adapt to the new interface of
magic_search and se05_search.
* src/tgbaalgos/se05.cc: new file.
* src/tgbaalgos/Makefile.am: Add it.
* src/tgbatest/ltl2tgba.cc: Add new emptiness check.
* src/tgbatest/emptchk.test: more tests.
* src/tgbatest/dfs.test: new file.
* src/tgbatest/Makefile.am: Add it.
parent 908b6129
2004-11-09 Poitrenaud Denis <denis@src.lip6.fr>
* src/tgbaalgos/magic.cc: rewrite to externalize the heap and
prepare it to a bit state hashing version.
* src/tgbaalgos/magic.hh: adapt to the new interface of
magic_search and se05_search.
* src/tgbaalgos/se05.cc: new file.
* src/tgbaalgos/Makefile.am: Add it.
* src/tgbatest/ltl2tgba.cc: Add new emptiness check.
* src/tgbatest/emptchk.test: more tests.
* src/tgbatest/dfs.test: new file.
* src/tgbatest/Makefile.am: Add it.
2004-11-09 Alexandre Duret-Lutz <adl@src.lip6.fr> 2004-11-09 Alexandre Duret-Lutz <adl@src.lip6.fr>
* src/tgbaalgos/emptiness.cc (print_tgba_run): Output the * src/tgbaalgos/emptiness.cc (print_tgba_run): Output the
......
...@@ -62,6 +62,7 @@ libtgbaalgos_la_SOURCES = \ ...@@ -62,6 +62,7 @@ libtgbaalgos_la_SOURCES = \
replayrun.cc \ replayrun.cc \
rundotdec.cc \ rundotdec.cc \
save.cc \ save.cc \
se05.cc \
stats.cc \ stats.cc \
reductgba_sim.cc \ reductgba_sim.cc \
reductgba_sim_del.cc reductgba_sim_del.cc
......
...@@ -19,163 +19,397 @@ ...@@ -19,163 +19,397 @@
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA. // 02111-1307, USA.
#include <iostream>
#include "misc/hash.hh"
#include <list>
#include <iterator> #include <iterator>
#include <cassert> #include <cassert>
#include "magic.hh" #include "magic.hh"
#include "tgba/bddprint.hh"
namespace spot namespace spot
{ {
magic_search::result::result(magic_search& ms) namespace
: ms_(ms)
{ {
} enum color {WHITE, BLUE, RED};
tgba_run* /// \brief Emptiness checker on spot::tgba automata having at most one
magic_search::result::accepting_run() /// accepting condition (i.e. a TBA).
{ template <typename heap>
tgba_run* run = new tgba_run; class magic_search : public emptiness_check
{
public:
/// \brief Initialize the Magic Search algorithm on the automaton \a a
///
/// \pre The automaton \a a must have at most one accepting
/// condition (i.e. it is a TBA).
magic_search(const tgba *a)
: a(a), all_cond(a->all_acceptance_conditions())
{
assert(a->number_of_acceptance_conditions() <= 1);
}
stack_type::const_reverse_iterator i, e = ms_.stack.rend(); virtual ~magic_search()
tstack_type::const_reverse_iterator ti; {
tgba_run::steps* l = &run->prefix; // 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();
}
}
for (i = ms_.stack.rbegin(), ti = ms_.tstack.rbegin(); i != e; ++i, ++ti) /// \brief Perform a Magic Search.
///
/// \return non null pointer iff the algorithm has found a
/// new accepting path.
///
/// check() can be called several times (until it returns a null
/// pointer) to enumerate all the visited accepting paths. The method
/// visits only a finite set of accepting paths.
virtual emptiness_check_result* check()
{ {
if (i->first.s->compare(ms_.x) == 0) nbn = nbt = 0;
l = &run->cycle; sts = mdp = st_blue.size() + st_red.size();
if (st_red.empty())
{
assert(st_blue.empty());
const state* s0 = a->get_init_state();
++nbn;
h.add_new_state(s0, BLUE);
push(st_blue, s0, bddfalse, bddfalse);
if (dfs_blue())
return new result(*this);
}
else
{
h.pop_notify(st_red.front().s);
delete st_red.front().it;
st_red.pop_front();
if (!st_red.empty() && dfs_red())
return new result(*this);
else
if (dfs_blue())
return new result(*this);
}
return 0;
}
tgba_run::step s = { i->first.s->clone(), ti->first, ti->second }; virtual std::ostream& print_stats(std::ostream &os) const
l->push_back(s); {
os << nbn << " distinct nodes visited" << std::endl;
os << nbt << " transitions explored" << std::endl;
os << mdp << " nodes for the maximal stack depth" << std::endl;
if (!st_red.empty())
{
assert(!st_blue.empty());
os << st_blue.size() + st_red.size() - 1
<< " nodes for the counter example" << std::endl;
}
return os;
} }
return run; 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 followed to reach \a s
/// (false for the first one).
bdd label;
/// The acc set of the transition followed to reach \a s
/// (false for the first one).
bdd acc;
};
magic_search::magic_search(const tgba_tba_proxy* a) typedef std::list<stack_item> stack_type;
: a(a), x(0)
{
}
magic_search::~magic_search() void push(stack_type& st, const state* s,
{ const bdd& label, const bdd& acc)
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;
}
delete x;
// Release all iterators on the stack.
while (!stack.empty())
{ {
delete stack.front().second; ++sts;
stack.pop_front(); if (sts>mdp)
mdp = sts;
tgba_succ_iterator* i = a->succ_iter(s);
i->first();
st.push_front(stack_item(s, i, label, acc));
} }
}
void /// \brief Stack of the blue dfs.
magic_search::push(const state* s, bool m) stack_type st_blue;
{
tgba_succ_iterator* i = a->succ_iter(s); /// \brief Stack of the red dfs.
i->first(); stack_type st_red;
hash_type::iterator hi = h.find(s); /// \brief Map where each visited state is colored
if (hi == h.end()) /// 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 automata to check.
bdd all_cond;
bool dfs_blue()
{ {
magic d = { !m, m }; while (!st_blue.empty())
h[s] = d; {
stack_item& f = st_blue.front();
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();
f.it->next();
typename heap::color_ref c = h.get_color_ref(s_prime);
if (c.is_null())
// Go down the edge (f.s, <label, acc>, s_prime)
{
++nbn;
h.add_new_state(s_prime, BLUE);
push(st_blue, s_prime, label, acc);
}
else // Backtrack the edge (f.s, <label, acc>, s_prime)
{
if (c.get() == BLUE && acc == all_cond)
// the test 'c.get() == BLUE' is added to limit
// the number of runs reported by successive
// calls to the check method. Without this
// functionnality, the test can be ommited.
{
target = f.s;
c.set(RED);
push(st_red, s_prime, label, acc);
if (dfs_red())
return true;
}
}
}
else
// Backtrack the edge
// (predecessor of f.s in st_blue, <f.label, f.acc>, f.s)
{
--sts;
stack_item f_dest(f);
delete f.it;
st_blue.pop_front();
typename heap::color_ref c = h.get_color_ref(f_dest.s);
assert(!c.is_null());
if (c.get() == BLUE && f_dest.acc == all_cond
&& !st_blue.empty())
// the test 'c.get() == BLUE' is added to limit
// the number of runs reported by successive
// calls to the check method. Without this
// functionnality, the test can be ommited.
{
target = st_blue.front().s;
c.set(RED);
push(st_red, f_dest.s, f_dest.label, f_dest.acc);
if (dfs_red())
return true;
}
else
h.pop_notify(f_dest.s);
}
}
return false;
} }
else
bool dfs_red()
{ {
hi->second.seen_without |= !m; assert(!st_red.empty());
hi->second.seen_with |= m; if (target->compare(st_red.front().s) == 0)
if (hi->first != s) return true;
delete s;
s = hi->first; while (!st_red.empty())
{
stack_item& f = st_red.front();
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();
f.it->next();
typename heap::color_ref c = h.get_color_ref(s_prime);
if (c.is_null())
// Notice that this case is taken into account only to
// support successive calls to the check method. Without
// this functionnality, one can check assert(c.is_null()).
// Go down the edge (f.s, <label, acc>, s_prime)
{
++nbn;
h.add_new_state(s_prime, RED);
push(st_red, s_prime, label, acc);
}
else // Go down the edge (f.s, <label, acc>, s_prime)
{
if (c.get() != RED)
{
c.set(RED);
push(st_red, s_prime, label, acc);
if (target->compare(s_prime) == 0)
return true;
}
}
}
else // Backtrack
{
--sts;
h.pop_notify(f.s);
delete f.it;
st_red.pop_front();
}
}
return false;
} }
magic_state ms = { s, m }; class result: public emptiness_check_result
stack.push_front(state_iter_pair(ms, i)); {
} public:
result(magic_search& ms)
: ms_(ms)
{
}
virtual tgba_run* accepting_run()
{
assert(!ms_.st_blue.empty());
assert(!ms_.st_red.empty());
bool tgba_run* run = new tgba_run;
magic_search::has(const state* s, bool m) const
{
hash_type::const_iterator i = h.find(s);
if (i == h.end())
return false;
if (!m && i->second.seen_without)
return true;
if (m && i->second.seen_with)
return true;
return false;
}
emptiness_check_result* typename stack_type::const_reverse_iterator i, j, end;
magic_search::check() tgba_run::steps* l;
{
if (stack.empty()) l = &run->prefix;
// It's a new search.
push(a->get_init_state(), false); i = ms_.st_blue.rbegin();
else end = ms_.st_blue.rend(); --end;
// Remove the transition to the cycle root. j = i; ++j;
tstack.pop_front(); for (; i != end; ++i, ++j)
{
tgba_run::step s = { i->s->clone(), j->label, j->acc };
l->push_back(s);
}
l = &run->cycle;
j = ms_.st_red.rbegin();
tgba_run::step s = { i->s->clone(), j->label, j->acc };
l->push_back(s);
i = j; ++j;
end = ms_.st_red.rend(); --end;
for (; i != end; ++i, ++j)
{
tgba_run::step s = { i->s->clone(), j->label, j->acc };
l->push_back(s);
}
assert(stack.size() == 1 + tstack.size()); return run;
}
private:
magic_search& ms_;
};
while (!stack.empty()) };
class explicit_magic_search_heap
{
public:
class color_ref
{ {
recurse: public:
magic_search::state_iter_pair& p = stack.front(); color_ref(color* c) :p(c)
tgba_succ_iterator* i = p.second; {
const bool magic = p.first.m; }
int get() const
while (!i->done()) {
{ return *p;
const state* s_prime = i->current_state(); }
bdd c = i->current_condition(); void set(color c)
bdd acc = i->current_acceptance_conditions(); {
i->next(); assert(!is_null());
if (magic && 0 == s_prime->compare(x)) *p=c;
{ }
delete s_prime; bool is_null() const
tstack.push_front (tstack_item(c, acc)); {
assert(stack.size() == tstack.size()); return p==0;
return new result(*this); }
} private:
if (!has(s_prime, magic)) color *p;
{ };
push(s_prime, magic);
tstack.push_front (tstack_item(c, acc)); explicit_magic_search_heap()
goto recurse; {
} }
delete s_prime;
} ~explicit_magic_search_heap()
{
const state* s = p.first.s; hash_type::const_iterator s = h.begin();
delete i; while (s != h.end())
stack.pop_front(); {
// Advance the iterator before deleting the "key" pointer.
if (!magic && a->state_is_accepting(s)) const state* ptr = s->first;
{ ++s;
if (!has(s, true)) delete ptr;
{ }
delete x; }
x = s->clone();
push(s, true); color_ref get_color_ref(const state*& s)
continue; {
} hash_type::iterator it = h.find(s);
} if (it==h.end())
if (!stack.empty()) return color_ref(0);
tstack.pop_front(); if (s!=it->first)
} {
delete s;
s = it->first;
}
return color_ref(&(it->second));
}
void add_new_state(const state* s, color c)
{
assert(h.find(s)==h.end());
h.insert(std::make_pair(s, c));
}
assert(tstack.empty()); void pop_notify(const state*)
return 0; {
}
private:
typedef Sgi::hash_map<const state*, color,
state_ptr_hash, state_ptr_equal> hash_type;
hash_type h;
};
} // anonymous
emptiness_check* explicit_magic_search(const tgba *a)
{
return new magic_search<explicit_magic_search_heap>(a);
} }
} }
...@@ -22,107 +22,148 @@ ...@@ -22,107 +22,148 @@
#ifndef SPOT_TGBAALGOS_MAGIC_HH #ifndef SPOT_TGBAALGOS_MAGIC_HH
# define SPOT_TGBAALGOS_MAGIC_HH # define SPOT_TGBAALGOS_MAGIC_HH
#include "misc/hash.hh" #include "tgba/tgba.hh"
#include <list>
#include <utility>
#include <ostream>
#include "tgba/tgbatba.hh"
#include "emptiness.hh" #include "emptiness.hh"
namespace spot namespace spot
{ {
/// \brief Emptiness check on spot::tgba_tba_proxy automata using /// \brief Returns an emptiness check on the spot::tgba automaton \a a.
/// the Magic Search algorithm. ///
/// \pre The automaton \a a must have at most one accepting condition (i.e.
/// it is a TBA).
///
/// The method \a check() of the returned checker can be called several times
/// (until it returns a null pointer) to enumerate all the visited accepting
/// paths. The method visits only a finite set of accepting paths.
///
/// The implemented algorithm is the following.
/// ///
/// This algorithm comes from
/// \verbatim /// \verbatim
/// @InProceedings{ godefroid.93.pstv, /// procedure nested_dfs ()
/// author = {Patrice Godefroid and Gerard .J. Holzmann}, /// begin
/// title = {On the verification of temporal properties}, /// call dfs_blue(s0);
/// booktitle = {Proceedings of the 13th IFIP TC6/WG6.1 International /// end;
/// Symposium on Protocol Specification, Testing, and ///
/// Verification (PSTV'93)}, /// procedure dfs_blue (s)
/// month = {May}, /// begin
/// editor = {Andr{\'e} A. S. Danthine and Guy Leduc /// s.color = blue;
/// and Pierre Wolper}, /// for all t in post(s) do
/// address = {Liege, Belgium}, /// if t.color == white then
/// pages = {109--124}, /// call dfs_blue(t);
/// publisher = {North-Holland}, /// end if;
/// year = {1993}, /// if (the edge (s,t) is accepting) then