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; virtual Type expressionType() const = 0;
// Normalizes the expression dependent on its type, recursing through all child expressions virtual ExpressionPointer normalized();
// ExpressionPointer negated();
// 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;
private: private:
friend void intrusive_ptr_add_ref(Expression *expression); friend void intrusive_ptr_add_ref(Expression *expression);

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ class Dummy: public ExpressionCRTP<Dummy>
bool isNormalized() const; bool isNormalized() const;
ExpressionPointer normalize() override; ExpressionPointer normalized() override;
private: private:
bool m_isNormalized = false; 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 Expression::Type ExpressionType = Expression::Type::Either;
static const std::string Identifier; 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; static const std::string Identifier;
public: public:
ExpressionPointer normalize() override; ExpressionPointer normalized() override;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -28,10 +28,11 @@ class NAry: public ExpressionCRTP<Derived>
ExpressionContext &expressionContext, ExpressionParser parseExpression); ExpressionContext &expressionContext, ExpressionParser parseExpression);
public: public:
void setArgument(size_t i, ExpressionPointer argument);
void addArgument(ExpressionPointer argument); void addArgument(ExpressionPointer argument);
const Expressions &arguments() const; const Expressions &arguments() const;
ExpressionPointer normalize() override; ExpressionPointer normalized() override;
protected: protected:
Expressions m_arguments; 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> template<class Derived>
void NAry<Derived>::addArgument(ExpressionPointer argument) void NAry<Derived>::addArgument(ExpressionPointer argument)
{ {
@ -98,22 +109,16 @@ const Expressions &NAry<Derived>::arguments() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived> template<class Derived>
inline ExpressionPointer NAry<Derived>::normalize() inline ExpressionPointer NAry<Derived>::normalized()
{ {
for (size_t i = 0; i < m_arguments.size(); i++) for (size_t i = 0; i < m_arguments.size(); i++)
{ {
BOOST_ASSERT(m_arguments[i]); BOOST_ASSERT(m_arguments[i]);
auto normalizedArgument = m_arguments[i]->normalize(); m_arguments[i] = m_arguments[i]->normalized();
// Replace argument if changed by normalization
if (!normalizedArgument)
continue;
m_arguments[i] = std::move(normalizedArgument);
} }
return nullptr; return this;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -90,8 +90,8 @@ const Expression *Action::effect() const
void Action::normalize() void Action::normalize()
{ {
// Normalize preconditions and effects // Normalize preconditions and effects
m_precondition->normalize(); m_precondition = m_precondition->normalized();
m_effect->normalize(); m_effect = m_effect->normalized();
// Normalize parameter names // Normalize parameter names
for (size_t i = 0; i < m_parameters.size(); i++) 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(), std::for_each(m_predicates.begin(), m_predicates.end(),
[](auto &predicate) [](auto &predicate)
{ {
predicate->normalize(); predicate->normalizeParameterNames();
}); });
std::for_each(m_actions.begin(), m_actions.end(), 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 parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext);
ExpressionPointer parsePredicate(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 // 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); BOOST_ASSERT(m_argument);
auto normalizedArgument = m_argument->normalize(); m_argument = m_argument->normalized();
// Replace argument if changed by normalization return this;
if (normalizedArgument)
setArgument(std::move(normalizedArgument));
return nullptr;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

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; 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[0]);
BOOST_ASSERT(m_arguments[1]); BOOST_ASSERT(m_arguments[1]);
m_arguments[0] = m_arguments[0]->normalized();
m_arguments[1] = m_arguments[1]->normalized();
auto notArgument0 = NotPointer(new Not); auto notArgument0 = NotPointer(new Not);
notArgument0->setArgument(std::move(m_arguments[0])); notArgument0->setArgument(m_arguments[0]);
auto orExpression = OrPointer(new Or); auto orExpression = OrPointer(new Or);
orExpression->addArgument(std::move(notArgument0)); orExpression->addArgument(notArgument0);
orExpression->addArgument(std::move(m_arguments[1])); orExpression->addArgument(m_arguments[1]);
auto normalizedOrExpression = orExpression->normalize(); auto normalizedOrExpression = orExpression->normalized();
if (normalizedOrExpression) if (normalizedOrExpression)
return normalizedOrExpression; return normalizedOrExpression;

View File

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

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++) for (size_t i = 0; i < m_parameters.size(); i++)
m_parameters[i]->setName("X" + std::to_string(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(0, d1);
i->setArgument(1, d2); i->setArgument(1, d2);
auto normalized = i->normalize(); auto normalized = i->normalized();
ASSERT_EQ(normalized->expressionType(), Expression::Type::Or); ASSERT_EQ(normalized->expressionType(), Expression::Type::Or);
@ -48,7 +48,7 @@ TEST(PDDLNormalizationTests, DoubleNegation)
n2->setArgument(std::move(d)); n2->setArgument(std::move(d));
n1->setArgument(std::move(n2)); n1->setArgument(std::move(n2));
auto normalized = n1->normalize(); auto normalized = n1->normalized();
ASSERT_EQ(normalized.get(), dp); ASSERT_EQ(normalized.get(), dp);
ASSERT_TRUE(dp->isNormalized()); ASSERT_TRUE(dp->isNormalized());