Refactored normalization because of recent changes to the pointer usage.

This commit is contained in:
Patrick Lühne 2016-09-04 19:29:05 +02:00
parent 7aa20a5820
commit 6aaf7c039d
30 changed files with 75 additions and 124 deletions

View File

@ -107,12 +107,8 @@ class Expression
virtual Type expressionType() const = 0;
// Normalizes the expression dependent on its type, recursing through all child expressions
//
// Result:
// * a new expression pointer to replace this one if required; this object is then empty
// * nullptr otherwise; the object may or may not have changed
virtual ExpressionPointer normalize() = 0;
virtual ExpressionPointer normalized();
ExpressionPointer negated();
private:
friend void intrusive_ptr_add_ref(Expression *expression);

View File

@ -37,7 +37,7 @@ class At: public ExpressionCRTP<At>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
size_t m_timePoint;

View File

@ -31,7 +31,7 @@ class Binary: public ExpressionCRTP<Derived>
void setArgument(size_t i, ExpressionPointer argument);
const std::array<ExpressionPointer, 2> &arguments() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
std::array<ExpressionPointer, 2> m_arguments;
@ -72,7 +72,7 @@ boost::intrusive_ptr<Derived> Binary<Derived>::parse(Context &context,
template<class Derived>
void Binary<Derived>::setArgument(size_t i, ExpressionPointer expression)
{
BOOST_ASSERT_MSG(i <= 2, "Index out of range");
BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range");
m_arguments[i] = expression;
}
@ -88,22 +88,16 @@ const std::array<ExpressionPointer, 2> &Binary<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Binary<Derived>::normalize()
inline ExpressionPointer Binary<Derived>::normalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_arguments[i] = std::move(normalizedArgument);
m_arguments[i] = m_arguments[i]->normalized();
}
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

@ -23,7 +23,7 @@ class Dummy: public ExpressionCRTP<Dummy>
bool isNormalized() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
private:
bool m_isNormalized = false;

View File

@ -22,9 +22,6 @@ class Either: public NAry<Either>
static const Expression::Type ExpressionType = Expression::Type::Either;
static const std::string Identifier;
public:
ExpressionPointer normalize() override;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

@ -28,10 +28,11 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
void setArgument(size_t i, ExpressionPointer argument);
void addArgument(ExpressionPointer argument);
const Expressions &arguments() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
Expressions m_arguments;
@ -78,6 +79,16 @@ boost::intrusive_ptr<Derived> NAry<Derived>::parse(Context &context,
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::setArgument(size_t i, ExpressionPointer expression)
{
BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range");
m_arguments[i] = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void NAry<Derived>::addArgument(ExpressionPointer argument)
{
@ -98,22 +109,16 @@ const Expressions &NAry<Derived>::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer NAry<Derived>::normalize()
inline ExpressionPointer NAry<Derived>::normalized()
{
for (size_t i = 0; i < m_arguments.size(); i++)
{
BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_arguments[i]->normalize();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_arguments[i] = std::move(normalizedArgument);
m_arguments[i] = m_arguments[i]->normalized();
}
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -32,7 +32,7 @@ class Not: public ExpressionCRTP<Not>
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer normalize() override;
ExpressionPointer normalized() override;
protected:
ExpressionPointer m_argument;

View File

@ -30,8 +30,6 @@ class Predicate: public ExpressionCRTP<Predicate>
bool isDeclared() const;
ExpressionPointer normalize() override;
private:
Predicate();

View File

@ -29,7 +29,7 @@ class PredicateDeclaration: public ExpressionCRTP<PredicateDeclaration>
bool isDeclared() const;
ExpressionPointer normalize() override;
void normalizeParameterNames();
private:
PredicateDeclaration();

View File

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

View File

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

View File

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

View File

@ -90,8 +90,8 @@ const Expression *Action::effect() const
void Action::normalize()
{
// Normalize preconditions and effects
m_precondition->normalize();
m_effect->normalize();
m_precondition = m_precondition->normalized();
m_effect = m_effect->normalized();
// Normalize parameter names
for (size_t i = 0; i < m_parameters.size(); i++)

View File

@ -420,7 +420,7 @@ void Domain::normalize()
std::for_each(m_predicates.begin(), m_predicates.end(),
[](auto &predicate)
{
predicate->normalize();
predicate->normalizeParameterNames();
});
std::for_each(m_actions.begin(), m_actions.end(),

View File

@ -24,6 +24,30 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::normalized()
{
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Expression::negated()
{
if (expressionType() == Type::Not)
{
auto &notExpression = dynamic_cast<expressions::Not &>(*this);
return notExpression.argument();
}
auto notExpression = expressions::NotPointer(new expressions::Not);
notExpression->setArgument(this);
return notExpression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext);
ExpressionPointer parsePredicate(Context &context, ExpressionContext &expressionContext);

View File

@ -401,7 +401,7 @@ void Problem::normalize()
// TODO: normalize objects and initial state
m_goal->normalize();
m_goal = m_goal->normalized();
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -34,17 +34,13 @@ ExpressionPointer At::argument() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer At::normalize()
ExpressionPointer At::normalized()
{
BOOST_ASSERT(m_argument);
auto normalizedArgument = m_argument->normalize();
m_argument = m_argument->normalized();
// Replace argument if changed by normalization
if (normalizedArgument)
setArgument(std::move(normalizedArgument));
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -239,13 +239,6 @@ PrimitiveTypePointer Constant::type() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Constant::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -22,11 +22,11 @@ bool Dummy::isNormalized() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Dummy::normalize()
ExpressionPointer Dummy::normalized()
{
m_isNormalized = true;
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -17,13 +17,6 @@ const std::string Either::Identifier = "either";
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Either::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

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

View File

@ -34,7 +34,7 @@ ExpressionPointer Not::argument() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Not::normalize()
ExpressionPointer Not::normalized()
{
BOOST_ASSERT(m_argument);
@ -43,22 +43,12 @@ ExpressionPointer Not::normalize()
{
auto &argument = dynamic_cast<Not &>(*m_argument);
auto normalized = std::move(argument.m_argument);
auto normalizedInner = normalized->normalize();
if (normalizedInner)
return normalizedInner;
return normalized;
return argument.m_argument->normalized();
}
auto normalizedArgument = m_argument->normalize();
m_argument = m_argument->normalized();
// Replace argument if changed by normalization
if (normalizedArgument)
setArgument(std::move(normalizedArgument));
return nullptr;
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -170,13 +170,6 @@ const Expressions &Predicate::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Predicate::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -79,12 +79,10 @@ const Variables &PredicateDeclaration::arguments() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer PredicateDeclaration::normalize()
void PredicateDeclaration::normalizeParameterNames()
{
for (size_t i = 0; i < m_parameters.size(); i++)
m_parameters[i]->setName("X" + std::to_string(i));
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -169,13 +169,6 @@ const PrimitiveTypes &PrimitiveType::parentTypes() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer PrimitiveType::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -41,13 +41,6 @@ const std::string &Unsupported::type() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Unsupported::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -203,13 +203,6 @@ bool Variable::isDirty() const
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer Variable::normalize()
{
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -20,7 +20,7 @@ TEST(PDDLNormalizationTests, Implication)
i->setArgument(0, d1);
i->setArgument(1, d2);
auto normalized = i->normalize();
auto normalized = i->normalized();
ASSERT_EQ(normalized->expressionType(), Expression::Type::Or);
@ -48,7 +48,7 @@ TEST(PDDLNormalizationTests, DoubleNegation)
n2->setArgument(std::move(d));
n1->setArgument(std::move(n2));
auto normalized = n1->normalize();
auto normalized = n1->normalized();
ASSERT_EQ(normalized.get(), dp);
ASSERT_TRUE(dp->isNormalized());