* src/ltlvisit/equals.cc, src/ltlvisit/equals.hh: New files.

* src/ltlvisit/Makefile.am (libltlvisit_a_SOURCES): Add equals.hh
and equals.cc.
* src/ltltest/equals.cc, src/ltltest/equals.test: New files.
* src/ltltest/Makefile.am (check_PROGRAMS): Add equals.
(equals_SOURCES): New variable.
(TESTS): Add equals.test.
This commit is contained in:
Alexandre Duret-Lutz 2003-04-16 12:30:21 +00:00
parent eec66e6d07
commit 7425f4a91e
8 changed files with 299 additions and 3 deletions

View file

@ -6,4 +6,6 @@ libltlvisit_a_SOURCES = \
dotty.cc \
dotty.hh \
dump.cc \
dump.hh
dump.hh \
equals.cc \
equals.hh

140
src/ltlvisit/equals.cc Normal file
View file

@ -0,0 +1,140 @@
#include <vector>
#include "equals.hh"
#include "ltlast/allnodes.hh"
namespace spot
{
namespace ltl
{
equals_visitor::equals_visitor(const formulae* f)
: f_(f), result_(false)
{
}
equals_visitor::~equals_visitor()
{
}
bool
equals_visitor::result() const
{
return result_;
}
void
equals_visitor::visit(const atomic_prop* ap)
{
const atomic_prop* p = dynamic_cast<const atomic_prop*>(f_);
if (p && p->name() == ap->name())
result_ = true;
}
void
equals_visitor::visit(const constant* c)
{
const constant* p = dynamic_cast<const constant*>(f_);
if (p && p->val() == c->val())
result_ = true;
}
void
equals_visitor::visit(const unop* uo)
{
const unop* p = dynamic_cast<const unop*>(f_);
if (!p || p->op() != uo->op())
return;
f_ = p->child();
uo->child()->accept(*this);
}
void
equals_visitor::visit(const binop* bo)
{
const binop* p = dynamic_cast<const binop*>(f_);
if (!p || p->op() != bo->op())
return;
// The current visitor will descend the left branch.
// Build a second visitor for the right branch.
equals_visitor v2(p->second());
f_ = p->first();
bo->first()->accept(*this);
if (result_ == false)
return;
bo->second()->accept(v2);
result_ = v2.result();
}
void
equals_visitor::visit(const multop* m)
{
const multop* p = dynamic_cast<const multop*>(f_);
if (!p || p->op() != m->op())
return;
// This check is a bit more complicated than other checks
// because And(a, b, c) is equal to And(c, a, b, a).
unsigned m_size = m->size();
unsigned p_size = p->size();
std::vector<bool> p_seen(p_size, false);
for (unsigned nf = 0; nf < m_size; ++nf)
{
unsigned np;
const formulae* mnth = m->nth(nf);
for (np = 0; np < p_size; ++np)
{
if (equals(p->nth(np), mnth))
{
p_seen[np] = true;
break;
}
}
// We we haven't found mnth in any child of p, then
// the two formulaes aren't equal.
if (np == p_size)
return;
}
// At this point, we have found all children of m' in children
// of `p'. That doesn't means that both formula are equal.
// Condider m = And(a, b, c) against p = And(c, d, a, b).
// We should now check if any unmarked (accodring to p_seen)
// child of `p' has an counterpart in `m'. Because `m' might
// contain duplicate children, its faster to test that
// unmarked children of `p' have a counterpart in marked children
// of `p'.
for (unsigned np = 0; np < p_size; ++np)
{
// Consider only unmarked children.
if (p_seen[np])
continue;
// Compare with marked children.
unsigned np2;
const formulae *pnth = p->nth(np);
for (np2 = 0; np2 < p_size; ++np2)
if (p_seen[np2] && equals(p->nth(np2), pnth))
break;
// No match? Too bad.
if (np2 == p_size)
return;
}
// The two formulaes match.
result_ = true;
}
bool
equals(const formulae* f1, const formulae* f2)
{
equals_visitor v(f1);
f2->accept(v);
return v.result();
}
}
}

34
src/ltlvisit/equals.hh Normal file
View file

@ -0,0 +1,34 @@
#include "ltlast/formulae.hh"
#include "ltlast/visitor.hh"
namespace spot
{
namespace ltl
{
// This visitor is public, because it's convenient
// to derive from it and override part of its methods.
class equals_visitor : public const_visitor
{
public:
equals_visitor(const formulae* f);
virtual ~equals_visitor();
// Return true iff the visitor has visited a
// formulae which is equal to `f'.
bool result() const;
void visit(const atomic_prop* ap);
void visit(const unop* uo);
void visit(const binop* bo);
void visit(const multop* bo);
void visit(const constant* c);
private:
const formulae* f_;
bool result_;
};
bool equals(const formulae* f1, const formulae* f2);
}
}