From 7aa20a5820809ebed768905cfd32acc62a55403d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Sun, 4 Sep 2016 18:48:46 +0200 Subject: [PATCH] Refactored expressions thanks to intrusive pointers. --- include/plasp/pddl/expressions/At.h | 8 ++-- include/plasp/pddl/expressions/Binary.h | 43 ++++++------------- include/plasp/pddl/expressions/NAry.h | 33 ++++---------- include/plasp/pddl/expressions/Not.h | 8 ++-- include/plasp/pddl/expressions/Predicate.h | 4 +- .../plasp/pddl/expressions/PrimitiveType.h | 4 +- include/plasp/pddl/expressions/Type.h | 2 +- src/plasp/pddl/TranslatorASP.cpp | 6 +-- src/plasp/pddl/expressions/At.cpp | 17 ++------ src/plasp/pddl/expressions/Imply.cpp | 8 ++-- src/plasp/pddl/expressions/Not.cpp | 23 +++------- src/plasp/pddl/expressions/Predicate.cpp | 2 +- src/plasp/pddl/expressions/PrimitiveType.cpp | 2 +- src/plasp/pddl/expressions/Type.cpp | 3 +- tests/TestPDDLNormalization.cpp | 4 +- 15 files changed, 56 insertions(+), 111 deletions(-) diff --git a/include/plasp/pddl/expressions/At.h b/include/plasp/pddl/expressions/At.h index 68457ee..2933597 100644 --- a/include/plasp/pddl/expressions/At.h +++ b/include/plasp/pddl/expressions/At.h @@ -34,17 +34,15 @@ class At: public ExpressionCRTP size_t timePoint() const; - void setArgument(const Expression *argument); - void setArgument(ExpressionPointer &&argument); - const Expression *argument() const; + void setArgument(ExpressionPointer argument); + ExpressionPointer argument() const; ExpressionPointer normalize() override; protected: size_t m_timePoint; - const Expression *m_argument; - ExpressionPointer m_argumentStorage; + ExpressionPointer m_argument; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/Binary.h b/include/plasp/pddl/expressions/Binary.h index 543d935..a91eb93 100644 --- a/include/plasp/pddl/expressions/Binary.h +++ b/include/plasp/pddl/expressions/Binary.h @@ -28,17 +28,13 @@ class Binary: public ExpressionCRTP ExpressionContext &expressionContext, ExpressionParser parseExpression); public: - template - void setArgument(const Expression *argument); - template - void setArgument(ExpressionPointer &&argument); - const std::array &arguments() const; + void setArgument(size_t i, ExpressionPointer argument); + const std::array &arguments() const; ExpressionPointer normalize() override; protected: - std::array m_arguments; - std::array m_argumentStorage; + std::array m_arguments; }; //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -63,8 +59,8 @@ boost::intrusive_ptr Binary::parse(Context &context, // Assume that expression identifier (imply, exists, etc.) is already parsed // Parse arguments of the expression - expression->Binary::setArgument<0>(parseExpression(context, expressionContext)); - expression->Binary::setArgument<1>(parseExpression(context, expressionContext)); + expression->Binary::setArgument(0, parseExpression(context, expressionContext)); + expression->Binary::setArgument(1, parseExpression(context, expressionContext)); parser.expect(")"); @@ -74,31 +70,17 @@ boost::intrusive_ptr Binary::parse(Context &context, //////////////////////////////////////////////////////////////////////////////////////////////////// template -template -void Binary::setArgument(const Expression *expression) +void Binary::setArgument(size_t i, ExpressionPointer expression) { - static_assert(i <= 2, "Index out of range"); + BOOST_ASSERT_MSG(i <= 2, "Index out of range"); - m_argumentStorage[i] = nullptr; m_arguments[i] = expression; } //////////////////////////////////////////////////////////////////////////////////////////////////// template -template -void Binary::setArgument(ExpressionPointer &&expression) -{ - static_assert(i <= 2, "Index out of range"); - - m_argumentStorage[i] = std::move(expression); - m_arguments[i] = m_argumentStorage[i].get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const std::array &Binary::arguments() const +const std::array &Binary::arguments() const { return m_arguments; } @@ -108,18 +90,17 @@ const std::array &Binary::arguments() const template inline ExpressionPointer Binary::normalize() { - for (size_t i = 0; i < m_argumentStorage.size(); i++) + for (size_t i = 0; i < m_arguments.size(); i++) { - BOOST_ASSERT(m_argumentStorage[i]); + BOOST_ASSERT(m_arguments[i]); - auto normalizedArgument = m_argumentStorage[i]->normalize(); + auto normalizedArgument = m_arguments[i]->normalize(); // Replace argument if changed by normalization if (!normalizedArgument) continue; - m_argumentStorage[i] = std::move(normalizedArgument); - m_arguments[i] = m_argumentStorage[i].get(); + m_arguments[i] = std::move(normalizedArgument); } return nullptr; diff --git a/include/plasp/pddl/expressions/NAry.h b/include/plasp/pddl/expressions/NAry.h index cc743c9..c03cf06 100644 --- a/include/plasp/pddl/expressions/NAry.h +++ b/include/plasp/pddl/expressions/NAry.h @@ -28,15 +28,13 @@ class NAry: public ExpressionCRTP ExpressionContext &expressionContext, ExpressionParser parseExpression); public: - void addArgument(const Expression *argument); - void addArgument(ExpressionPointer &&argument); - const std::vector &arguments() const; + void addArgument(ExpressionPointer argument); + const Expressions &arguments() const; ExpressionPointer normalize() override; protected: - std::vector m_arguments; - Expressions m_argumentStorage; + Expressions m_arguments; }; //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -81,7 +79,7 @@ boost::intrusive_ptr NAry::parse(Context &context, //////////////////////////////////////////////////////////////////////////////////////////////////// template -void NAry::addArgument(const Expression *argument) +void NAry::addArgument(ExpressionPointer argument) { if (!argument) return; @@ -92,19 +90,7 @@ void NAry::addArgument(const Expression *argument) //////////////////////////////////////////////////////////////////////////////////////////////////// template -void NAry::addArgument(ExpressionPointer &&argument) -{ - if (!argument) - return; - - m_argumentStorage.emplace_back(std::move(argument)); - m_arguments.emplace_back(m_argumentStorage.back().get()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const std::vector &NAry::arguments() const +const Expressions &NAry::arguments() const { return m_arguments; } @@ -114,18 +100,17 @@ const std::vector &NAry::arguments() const template inline ExpressionPointer NAry::normalize() { - for (size_t i = 0; i < m_argumentStorage.size(); i++) + for (size_t i = 0; i < m_arguments.size(); i++) { - BOOST_ASSERT(m_argumentStorage[i]); + BOOST_ASSERT(m_arguments[i]); - auto normalizedArgument = m_argumentStorage[i]->normalize(); + auto normalizedArgument = m_arguments[i]->normalize(); // Replace argument if changed by normalization if (!normalizedArgument) continue; - m_argumentStorage[i] = std::move(normalizedArgument); - m_arguments[i] = m_argumentStorage[i].get(); + m_arguments[i] = std::move(normalizedArgument); } return nullptr; diff --git a/include/plasp/pddl/expressions/Not.h b/include/plasp/pddl/expressions/Not.h index 5e2d362..ec20037 100644 --- a/include/plasp/pddl/expressions/Not.h +++ b/include/plasp/pddl/expressions/Not.h @@ -29,15 +29,13 @@ class Not: public ExpressionCRTP public: Not(); - void setArgument(const Expression *argument); - void setArgument(ExpressionPointer &&argument); - const Expression *argument() const; + void setArgument(ExpressionPointer argument); + ExpressionPointer argument() const; ExpressionPointer normalize() override; protected: - const Expression *m_argument; - ExpressionPointer m_argumentStorage; + ExpressionPointer m_argument; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/Predicate.h b/include/plasp/pddl/expressions/Predicate.h index 4b8468e..09887f3 100644 --- a/include/plasp/pddl/expressions/Predicate.h +++ b/include/plasp/pddl/expressions/Predicate.h @@ -26,7 +26,7 @@ class Predicate: public ExpressionCRTP public: const std::string &name() const; - const std::vector &arguments() const; + const Expressions &arguments() const; bool isDeclared() const; @@ -40,7 +40,7 @@ class Predicate: public ExpressionCRTP bool m_isDeclared; std::string m_name; - std::vector m_arguments; + Expressions m_arguments; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/PrimitiveType.h b/include/plasp/pddl/expressions/PrimitiveType.h index 4f3f476..45c7525 100644 --- a/include/plasp/pddl/expressions/PrimitiveType.h +++ b/include/plasp/pddl/expressions/PrimitiveType.h @@ -33,7 +33,7 @@ class PrimitiveType: public ExpressionCRTP PrimitiveType(std::string name); const std::string &name() const; - const std::vector &parentTypes() const; + const PrimitiveTypes &parentTypes() const; ExpressionPointer normalize() override; @@ -45,7 +45,7 @@ class PrimitiveType: public ExpressionCRTP std::string m_name; - std::vector m_parentTypes; + PrimitiveTypes m_parentTypes; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/Type.h b/include/plasp/pddl/expressions/Type.h index 54c09cd..0dc228d 100644 --- a/include/plasp/pddl/expressions/Type.h +++ b/include/plasp/pddl/expressions/Type.h @@ -16,7 +16,7 @@ namespace expressions // //////////////////////////////////////////////////////////////////////////////////////////////////// -ExpressionPointer parseExistingPrimitiveType(Context &context, +PrimitiveTypePointer parseExistingPrimitiveType(Context &context, ExpressionContext &expressionContext); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 771eb34..abe7354 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -261,7 +261,7 @@ void TranslatorASP::translateActions() const const auto &andExpression = dynamic_cast(precondition); std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto *argument) + [&](const auto argument) { translateLiteral("precondition", *argument); }); @@ -287,7 +287,7 @@ void TranslatorASP::translateActions() const const auto &andExpression = dynamic_cast(effect); std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto *argument) + [&](const auto argument) { translateLiteral("postcondition", *argument, true); }); @@ -557,7 +557,7 @@ void TranslatorASP::translateGoal() const const auto &andExpression = dynamic_cast(goal); std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto *argument) + [&](const auto argument) { m_outputStream << std::endl << utils::RuleName("goal") << "("; diff --git a/src/plasp/pddl/expressions/At.cpp b/src/plasp/pddl/expressions/At.cpp index 327af08..1005d79 100644 --- a/src/plasp/pddl/expressions/At.cpp +++ b/src/plasp/pddl/expressions/At.cpp @@ -20,23 +20,14 @@ At::At() //////////////////////////////////////////////////////////////////////////////////////////////////// -void At::setArgument(const Expression *argument) +void At::setArgument(ExpressionPointer argument) { - m_argumentStorage = nullptr; m_argument = argument; } //////////////////////////////////////////////////////////////////////////////////////////////////// -void At::setArgument(ExpressionPointer &&argument) -{ - m_argumentStorage = std::move(argument); - m_argument = m_argumentStorage.get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expression *At::argument() const +ExpressionPointer At::argument() const { return m_argument; } @@ -45,9 +36,9 @@ const Expression *At::argument() const ExpressionPointer At::normalize() { - BOOST_ASSERT(m_argumentStorage); + BOOST_ASSERT(m_argument); - auto normalizedArgument = m_argumentStorage->normalize(); + auto normalizedArgument = m_argument->normalize(); // Replace argument if changed by normalization if (normalizedArgument) diff --git a/src/plasp/pddl/expressions/Imply.cpp b/src/plasp/pddl/expressions/Imply.cpp index 965e371..e1ec21e 100644 --- a/src/plasp/pddl/expressions/Imply.cpp +++ b/src/plasp/pddl/expressions/Imply.cpp @@ -23,15 +23,15 @@ const std::string Imply::Identifier = "imply"; ExpressionPointer Imply::normalize() { - BOOST_ASSERT(m_argumentStorage[0]); - BOOST_ASSERT(m_argumentStorage[1]); + BOOST_ASSERT(m_arguments[0]); + BOOST_ASSERT(m_arguments[1]); auto notArgument0 = NotPointer(new Not); - notArgument0->setArgument(std::move(m_argumentStorage[0])); + notArgument0->setArgument(std::move(m_arguments[0])); auto orExpression = OrPointer(new Or); orExpression->addArgument(std::move(notArgument0)); - orExpression->addArgument(std::move(m_argumentStorage[1])); + orExpression->addArgument(std::move(m_arguments[1])); auto normalizedOrExpression = orExpression->normalize(); diff --git a/src/plasp/pddl/expressions/Not.cpp b/src/plasp/pddl/expressions/Not.cpp index 40ed93b..5e994a9 100644 --- a/src/plasp/pddl/expressions/Not.cpp +++ b/src/plasp/pddl/expressions/Not.cpp @@ -20,23 +20,14 @@ Not::Not() //////////////////////////////////////////////////////////////////////////////////////////////////// -void Not::setArgument(const Expression *argument) +void Not::setArgument(ExpressionPointer argument) { - m_argumentStorage = nullptr; m_argument = argument; } //////////////////////////////////////////////////////////////////////////////////////////////////// -void Not::setArgument(ExpressionPointer &&argument) -{ - m_argumentStorage = std::move(argument); - m_argument = m_argumentStorage.get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expression *Not::argument() const +ExpressionPointer Not::argument() const { return m_argument; } @@ -45,14 +36,14 @@ const Expression *Not::argument() const ExpressionPointer Not::normalize() { - BOOST_ASSERT(m_argumentStorage); + BOOST_ASSERT(m_argument); // Remove double negations immediately - if (m_argumentStorage->expressionType() == Expression::Type::Not) + if (m_argument->expressionType() == Expression::Type::Not) { - auto &argument = dynamic_cast(*m_argumentStorage); + auto &argument = dynamic_cast(*m_argument); - auto normalized = std::move(argument.m_argumentStorage); + auto normalized = std::move(argument.m_argument); auto normalizedInner = normalized->normalize(); if (normalizedInner) @@ -61,7 +52,7 @@ ExpressionPointer Not::normalize() return normalized; } - auto normalizedArgument = m_argumentStorage->normalize(); + auto normalizedArgument = m_argument->normalize(); // Replace argument if changed by normalization if (normalizedArgument) diff --git a/src/plasp/pddl/expressions/Predicate.cpp b/src/plasp/pddl/expressions/Predicate.cpp index 7b0b80f..ccfc0f8 100644 --- a/src/plasp/pddl/expressions/Predicate.cpp +++ b/src/plasp/pddl/expressions/Predicate.cpp @@ -163,7 +163,7 @@ const std::string &Predicate::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &Predicate::arguments() const +const Expressions &Predicate::arguments() const { return m_arguments; } diff --git a/src/plasp/pddl/expressions/PrimitiveType.cpp b/src/plasp/pddl/expressions/PrimitiveType.cpp index 9bc88fe..394af70 100644 --- a/src/plasp/pddl/expressions/PrimitiveType.cpp +++ b/src/plasp/pddl/expressions/PrimitiveType.cpp @@ -162,7 +162,7 @@ const std::string &PrimitiveType::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &PrimitiveType::parentTypes() const +const PrimitiveTypes &PrimitiveType::parentTypes() const { return m_parentTypes; } diff --git a/src/plasp/pddl/expressions/Type.cpp b/src/plasp/pddl/expressions/Type.cpp index e4cab3f..606a280 100644 --- a/src/plasp/pddl/expressions/Type.cpp +++ b/src/plasp/pddl/expressions/Type.cpp @@ -17,7 +17,8 @@ namespace expressions // //////////////////////////////////////////////////////////////////////////////////////////////////// -ExpressionPointer parseExistingPrimitiveType(Context &context, ExpressionContext &expressionContext) +PrimitiveTypePointer parseExistingPrimitiveType(Context &context, + ExpressionContext &expressionContext) { return PrimitiveType::parseAndFind(context, expressionContext.domain); } diff --git a/tests/TestPDDLNormalization.cpp b/tests/TestPDDLNormalization.cpp index add8b97..e0ff2f7 100644 --- a/tests/TestPDDLNormalization.cpp +++ b/tests/TestPDDLNormalization.cpp @@ -17,8 +17,8 @@ TEST(PDDLNormalizationTests, Implication) auto d2 = expressions::DummyPointer(new expressions::Dummy); const auto d2p = d2.get(); - i->setArgument<0>(d1); - i->setArgument<1>(d2); + i->setArgument(0, d1); + i->setArgument(1, d2); auto normalized = i->normalize();