Added separate simplification step to normalize in a single recursion.

This commit is contained in:
Patrick Lühne 2016-09-04 23:30:08 +02:00
parent 97ad4268d7
commit 4d72c20d9b
11 changed files with 128 additions and 16 deletions

View File

@ -109,7 +109,9 @@ class Expression
virtual Type expressionType() const = 0;
virtual ExpressionPointer normalized();
ExpressionPointer normalized();
virtual ExpressionPointer simplified();
virtual ExpressionPointer negationNormalized();
ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0;

View File

@ -37,7 +37,8 @@ class At: public ExpressionCRTP<At>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;

View File

@ -31,7 +31,8 @@ class Binary: public ExpressionCRTP<Derived>
void setArgument(size_t i, ExpressionPointer argument);
const std::array<ExpressionPointer, 2> &arguments() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;
@ -90,7 +91,22 @@ const std::array<ExpressionPointer, 2> &Binary<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::normalized()
inline ExpressionPointer Binary<Derived>::simplified()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->simplified();
}
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::negationNormalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{

View File

@ -24,7 +24,7 @@ class Imply: public Binary<Imply>
static const std::string Identifier;
public:
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -33,7 +33,8 @@ class NAry: public ExpressionCRTP<Derived>
Expressions &arguments();
const Expressions &arguments() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;
@ -120,13 +121,13 @@ Expressions &NAry<Derived>::arguments()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::normalized()
inline ExpressionPointer NAry<Derived>::simplified()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->normalized();
m_arguments[i] = m_arguments[i]->simplified();
}
return this;
@ -134,6 +135,35 @@ inline ExpressionPointer NAry<Derived>::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::negationNormalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
m_arguments[i] = m_arguments[i]->negationNormalized();
}
/*// Unify same-type children
for (size_t i = 0; i < m_arguments.size(); i++)
{
if (m_arguments[i]->expressionType() != Derived::ExpressionType)
continue;
auto &nAryExpression = dynamic_cast<Derived &>(*m_arguments[i]);
m_arguments.reserve(m_arguments.size() + nAryExpression.arguments().size());
m_arguments.emplace_back(nAryExpression.arguments());
m_arguments.erase(const_iterator __position)
}*/
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void NAry<Derived>::print(std::ostream &ostream) const
{

View File

@ -32,7 +32,8 @@ class Not: public ExpressionCRTP<Not>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalized() override;
ExpressionPointer simplified() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;

View File

@ -25,6 +25,20 @@ namespace pddl
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::normalized()
{
return simplified()->negationNormalized();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::simplified()
{
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::negationNormalized()
{
return this;
}

View File

@ -1,5 +1,7 @@
#include <plasp/pddl/expressions/At.h>
#include <plasp/utils/TranslatorException.h>
namespace plasp
{
namespace pddl
@ -34,11 +36,18 @@ ExpressionPointer At::argument() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer At::normalized()
ExpressionPointer At::simplified()
{
throw utils::TranslatorException("“at” predicates currently not supported");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer At::negationNormalized()
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->normalized();
m_argument = m_argument->negationNormalized();
return this;
}

View File

@ -21,13 +21,13 @@ const std::string Imply::Identifier = "imply";
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Imply::normalized()
ExpressionPointer Imply::simplified()
{
BOOST_ASSERT(m_arguments[0]);
BOOST_ASSERT(m_arguments[1]);
m_arguments[0] = m_arguments[0]->normalized();
m_arguments[1] = m_arguments[1]->normalized();
m_arguments[0] = m_arguments[0]->simplified();
m_arguments[1] = m_arguments[1]->simplified();
auto notArgument0 = NotPointer(new Not);
notArgument0->setArgument(std::move(m_arguments[0]));
@ -36,7 +36,7 @@ ExpressionPointer Imply::normalized()
orExpression->addArgument(std::move(notArgument0));
orExpression->addArgument(std::move(m_arguments[1]));
auto normalizedOrExpression = orExpression->normalized();
auto normalizedOrExpression = orExpression->simplified();
if (normalizedOrExpression)
return normalizedOrExpression;

View File

@ -37,7 +37,18 @@ ExpressionPointer Not::argument() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Not::normalized()
ExpressionPointer Not::simplified()
{
BOOST_ASSERT(m_argument);
m_argument = m_argument->simplified();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Not::negationNormalized()
{
BOOST_ASSERT(m_argument);

View File

@ -10,6 +10,34 @@ using namespace plasp::pddl;
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(PDDLNormalizationTests, Simplify)
{
auto n1 = expressions::NotPointer(new expressions::Not);
auto n2 = expressions::NotPointer(new expressions::Not);
auto a = expressions::AndPointer(new expressions::And);
auto o = expressions::OrPointer(new expressions::Or);
auto i = expressions::ImplyPointer(new expressions::Imply);
i->setArgument(0, new expressions::Dummy("a"));
i->setArgument(1, new expressions::Dummy("b"));
o->addArgument(i);
o->addArgument(new expressions::Dummy("c"));
a->addArgument(o);
a->addArgument(new expressions::Dummy("d"));
n2->setArgument(a);
n1->setArgument(n2);
std::stringstream output;
n1->simplified()->print(output);
ASSERT_EQ(output.str(), "(not (not (and (or (or (not (a)) (b)) (c)) (d))))");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(PDDLNormalizationTests, Implication)
{
auto i = expressions::ImplyPointer(new expressions::Imply);