Added debug printing function to facilitate testing expression normalization.

This commit is contained in:
Patrick Lühne 2016-09-04 22:26:17 +02:00
parent c9ecd0c020
commit ed2d64c1c9
23 changed files with 185 additions and 95 deletions

View File

@ -1,6 +1,8 @@
#ifndef __PLASP__PDDL__EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION_H
#include <iosfwd>
#include <boost/intrusive_ptr.hpp>
#include <plasp/utils/Parser.h>
@ -110,6 +112,8 @@ class Expression
virtual ExpressionPointer normalized();
ExpressionPointer negated();
virtual void print(std::ostream &ostream) const = 0;
private:
friend void intrusive_ptr_add_ref(Expression *expression);
friend void intrusive_ptr_release(Expression *expression);

View File

@ -39,6 +39,8 @@ class At: public ExpressionCRTP<At>
ExpressionPointer normalized() override;
void print(std::ostream &ostream) const override;
protected:
size_t m_timePoint;

View File

@ -33,6 +33,8 @@ class Binary: public ExpressionCRTP<Derived>
ExpressionPointer normalized() override;
void print(std::ostream &ostream) const override;
protected:
std::array<ExpressionPointer, 2> m_arguments;
};
@ -102,6 +104,23 @@ inline ExpressionPointer Binary<Derived>::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void Binary<Derived>::print(std::ostream &ostream) const
{
ostream << "(" << Derived::Identifier;
std::for_each(m_arguments.begin(), m_arguments.end(),
[&](auto &argument)
{
ostream << " ";
argument->print(ostream);
});
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -35,6 +35,8 @@ class Constant: public ExpressionCRTP<Constant>
const std::string &name() const;
PrimitiveTypePointer type() const;
void print(std::ostream &ostream) const override;
private:
static ConstantPointer parseDeclaration(Context &context);
static void parseTypedDeclaration(Context &context, Domain &domain, Constants &constants);

View File

@ -21,12 +21,13 @@ class Dummy: public ExpressionCRTP<Dummy>
public:
static const Expression::Type ExpressionType = Expression::Type::Dummy;
bool isNormalized() const;
public:
Dummy(std::string name);
ExpressionPointer normalized() override;
void print(std::ostream &ostream) const override;
private:
bool m_isNormalized = false;
std::string m_name;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -35,6 +35,8 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionPointer normalized() override;
void print(std::ostream &ostream) const override;
protected:
Expressions m_arguments;
};
@ -132,6 +134,23 @@ inline ExpressionPointer NAry<Derived>::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void NAry<Derived>::print(std::ostream &ostream) const
{
ostream << "(" << Derived::Identifier;
std::for_each(m_arguments.begin(), m_arguments.end(),
[&](auto &argument)
{
ostream << " ";
argument->print(ostream);
});
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -34,6 +34,8 @@ class Not: public ExpressionCRTP<Not>
ExpressionPointer normalized() override;
void print(std::ostream &ostream) const override;
protected:
ExpressionPointer m_argument;
};

View File

@ -30,6 +30,8 @@ class Predicate: public ExpressionCRTP<Predicate>
bool isDeclared() const;
void print(std::ostream &ostream) const override;
private:
Predicate();

View File

@ -31,6 +31,8 @@ class PredicateDeclaration: public ExpressionCRTP<PredicateDeclaration>
void normalizeParameterNames();
void print(std::ostream &ostream) const override;
private:
PredicateDeclaration();

View File

@ -35,6 +35,8 @@ class PrimitiveType: public ExpressionCRTP<PrimitiveType>
const std::string &name() const;
const PrimitiveTypes &parentTypes() const;
void print(std::ostream &ostream) const override;
private:
void setDirty(bool isDirty = true);
bool isDirty() const;

View File

@ -27,6 +27,8 @@ class Unsupported: public ExpressionCRTP<Unsupported>
public:
const std::string &type() const;
void print(std::ostream &ostream) const override;
private:
std::string m_type;
};

View File

@ -37,6 +37,8 @@ class Variable: public ExpressionCRTP<Variable>
void setDirty(bool isDirty = true);
bool isDirty() const;
void print(std::ostream &ostream) const override;
private:
static void parseDeclaration(Context &context, Variables &parameters);

View File

@ -1,5 +1,8 @@
#include <plasp/pddl/expressions/And.h>
#include <algorithm>
#include <iostream>
namespace plasp
{
namespace pddl

View File

@ -45,6 +45,17 @@ ExpressionPointer At::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
void At::print(std::ostream &ostream) const
{
ostream << "(at " << m_timePoint << " ";
m_argument->print(ostream);
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -239,6 +239,13 @@ PrimitiveTypePointer Constant::type() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Constant::print(std::ostream &ostream) const
{
ostream << "(" << m_name << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -15,18 +15,16 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Dummy::isNormalized() const
Dummy::Dummy(std::string name)
: m_name{name}
{
return m_isNormalized;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Dummy::normalized()
void Dummy::print(std::ostream &ostream) const
{
m_isNormalized = true;
return this;
ostream << "(" << m_name << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -84,6 +84,17 @@ ExpressionPointer Not::normalized()
////////////////////////////////////////////////////////////////////////////////////////////////////
void Not::print(std::ostream &ostream) const
{
ostream << "(not ";
m_argument->print(ostream);
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -170,6 +170,14 @@ const Expressions &Predicate::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::print(std::ostream &ostream) const
{
// TODO: implement correctly
ostream << "(<predicate>)";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -87,6 +87,14 @@ void PredicateDeclaration::normalizeParameterNames()
////////////////////////////////////////////////////////////////////////////////////////////////////
void PredicateDeclaration::print(std::ostream &ostream) const
{
// TODO: implement correctly
ostream << "(<predicate declaration>)";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -169,6 +169,14 @@ const PrimitiveTypes &PrimitiveType::parentTypes() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void PrimitiveType::print(std::ostream &ostream) const
{
// TODO: implement correctly
ostream << "(<primitive type" << m_name << ">)";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -41,6 +41,14 @@ const std::string &Unsupported::type() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Unsupported::print(std::ostream &ostream) const
{
// TODO: implement correctly
ostream << "(<unsupported expression “" << m_type << "”>)";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -203,6 +203,14 @@ bool Variable::isDirty() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::print(std::ostream &ostream) const
{
// TODO: implement correctly
ostream << "(<variable " << m_name << ">)";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -13,28 +13,14 @@ using namespace plasp::pddl;
TEST(PDDLNormalizationTests, Implication)
{
auto i = expressions::ImplyPointer(new expressions::Imply);
auto d1 = expressions::DummyPointer(new expressions::Dummy);
const auto d1p = d1.get();
auto d2 = expressions::DummyPointer(new expressions::Dummy);
const auto d2p = d2.get();
i->setArgument(0, d1);
i->setArgument(1, d2);
i->setArgument(0, new expressions::Dummy("a"));
i->setArgument(1, new expressions::Dummy("b"));
auto normalized = i->normalized();
std::stringstream output;
i->normalized()->print(output);
ASSERT_EQ(normalized->expressionType(), Expression::Type::Or);
const auto &o = dynamic_cast<expressions::Or &>(*normalized);
ASSERT_EQ(o.arguments()[0]->expressionType(), Expression::Type::Not);
const auto &n = dynamic_cast<const expressions::Not &>(*o.arguments()[0]);
ASSERT_EQ(n.argument(), d1p);
ASSERT_EQ(o.arguments()[1], d2p);
ASSERT_TRUE(d1p->isNormalized());
ASSERT_TRUE(d2p->isNormalized());
ASSERT_EQ(output.str(), "(or (not (a)) (b))");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -43,57 +29,32 @@ TEST(PDDLNormalizationTests, DoubleNegation)
{
auto n1 = expressions::NotPointer(new expressions::Not);
auto n2 = expressions::NotPointer(new expressions::Not);
auto d = expressions::DummyPointer(new expressions::Dummy);
const auto dp = d.get();
n2->setArgument(std::move(d));
n1->setArgument(std::move(n2));
n2->setArgument(new expressions::Dummy("a"));
n1->setArgument(n2);
auto normalized = n1->normalized();
std::stringstream output;
n1->normalized()->print(output);
ASSERT_EQ(normalized.get(), dp);
ASSERT_TRUE(dp->isNormalized());
ASSERT_EQ(output.str(), "(a)");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(PDDLNormalizationTests, DeMorganNegativeConjunction)
{
auto d1 = expressions::DummyPointer(new expressions::Dummy);
const auto d1p = d1.get();
auto d2 = expressions::DummyPointer(new expressions::Dummy);
const auto d2p = d2.get();
auto d3 = expressions::DummyPointer(new expressions::Dummy);
const auto d3p = d3.get();
auto a = expressions::AndPointer(new expressions::And);
a->addArgument(d1);
a->addArgument(d2);
a->addArgument(d3);
a->addArgument(new expressions::Dummy("a"));
a->addArgument(new expressions::Dummy("b"));
a->addArgument(new expressions::Dummy("c"));
auto n = expressions::NotPointer(new expressions::Not);
n->setArgument(a);
auto normalized = n->normalized();
std::stringstream output;
n->normalized()->print(output);
ASSERT_EQ(normalized->expressionType(), Expression::Type::Or);
const auto &o = dynamic_cast<expressions::Or &>(*normalized);
ASSERT_EQ(o.arguments()[0]->expressionType(), Expression::Type::Not);
ASSERT_EQ(o.arguments()[1]->expressionType(), Expression::Type::Not);
ASSERT_EQ(o.arguments()[2]->expressionType(), Expression::Type::Not);
const auto &n1 = dynamic_cast<expressions::Not &>(*o.arguments()[0]);
const auto &n2 = dynamic_cast<expressions::Not &>(*o.arguments()[1]);
const auto &n3 = dynamic_cast<expressions::Not &>(*o.arguments()[2]);
ASSERT_EQ(n1.argument().get(), d1p);
ASSERT_EQ(n2.argument().get(), d2p);
ASSERT_EQ(n3.argument().get(), d3p);
ASSERT_TRUE(d1p->isNormalized());
ASSERT_TRUE(d2p->isNormalized());
ASSERT_TRUE(d3p->isNormalized());
ASSERT_EQ(output.str(), "(or (not (a)) (not (b)) (not (c)))");
}
@ -101,39 +62,39 @@ TEST(PDDLNormalizationTests, DeMorganNegativeConjunction)
TEST(PDDLNormalizationTests, DeMorganNegativeDisjunction)
{
auto d1 = expressions::DummyPointer(new expressions::Dummy);
const auto d1p = d1.get();
auto d2 = expressions::DummyPointer(new expressions::Dummy);
const auto d2p = d2.get();
auto d3 = expressions::DummyPointer(new expressions::Dummy);
const auto d3p = d3.get();
auto o = expressions::OrPointer(new expressions::Or);
o->addArgument(d1);
o->addArgument(d2);
o->addArgument(d3);
auto a = expressions::OrPointer(new expressions::Or);
a->addArgument(new expressions::Dummy("a"));
a->addArgument(new expressions::Dummy("b"));
a->addArgument(new expressions::Dummy("c"));
auto n = expressions::NotPointer(new expressions::Not);
n->setArgument(o);
n->setArgument(a);
auto normalized = n->normalized();
std::stringstream output;
n->normalized()->print(output);
ASSERT_EQ(normalized->expressionType(), Expression::Type::And);
const auto &a = dynamic_cast<expressions::And &>(*normalized);
ASSERT_EQ(a.arguments()[0]->expressionType(), Expression::Type::Not);
ASSERT_EQ(a.arguments()[1]->expressionType(), Expression::Type::Not);
ASSERT_EQ(a.arguments()[2]->expressionType(), Expression::Type::Not);
const auto &n1 = dynamic_cast<expressions::Not &>(*a.arguments()[0]);
const auto &n2 = dynamic_cast<expressions::Not &>(*a.arguments()[1]);
const auto &n3 = dynamic_cast<expressions::Not &>(*a.arguments()[2]);
ASSERT_EQ(n1.argument().get(), d1p);
ASSERT_EQ(n2.argument().get(), d2p);
ASSERT_EQ(n3.argument().get(), d3p);
ASSERT_TRUE(d1p->isNormalized());
ASSERT_TRUE(d2p->isNormalized());
ASSERT_TRUE(d3p->isNormalized());
ASSERT_EQ(output.str(), "(and (not (a)) (not (b)) (not (c)))");
}
////////////////////////////////////////////////////////////////////////////////////////////////////
TEST(PDDLNormalizationTests, DoubleNegationInner)
{
auto n1 = expressions::NotPointer(new expressions::Not);
auto n2 = expressions::NotPointer(new expressions::Not);
auto n3 = expressions::NotPointer(new expressions::Not);
auto a = expressions::AndPointer(new expressions::And);
a->addArgument(new expressions::Dummy("a"));
a->addArgument(new expressions::Dummy("b"));
a->addArgument(new expressions::Dummy("c"));
n3->setArgument(a);
n2->setArgument(n3);
n1->setArgument(n2);
std::stringstream output;
n1->normalized()->print(output);
ASSERT_EQ(output.str(), "(or (not (a)) (not (b)) (not (c)))");
}