diff --git a/include/plasp/pddl/Domain.h b/include/plasp/pddl/Domain.h index b7bf001..a882cce 100644 --- a/include/plasp/pddl/Domain.h +++ b/include/plasp/pddl/Domain.h @@ -26,7 +26,7 @@ class Domain public: const std::string &name() const; - const Requirement::Types &requirements() const; + const Requirements &requirements() const; const TypeHashMap &types() const; private: @@ -35,7 +35,7 @@ class Domain void parseSection(utils::Parser &parser); void parseRequirementsSection(utils::Parser &parser); - bool hasRequirement(Requirement::Type requirement) const; + bool hasRequirement(Requirement::Type requirementType) const; void computeDerivedRequirements(); void parseTypingSection(utils::Parser &parser); @@ -45,7 +45,7 @@ class Domain Context &m_context; std::string m_name; - Requirement::Types m_requirements; + Requirements m_requirements; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/Requirement.h b/include/plasp/pddl/Requirement.h index 730e211..99ca03e 100644 --- a/include/plasp/pddl/Requirement.h +++ b/include/plasp/pddl/Requirement.h @@ -1,9 +1,10 @@ #ifndef __PLASP__PDDL__REQUIREMENT_H #define __PLASP__PDDL__REQUIREMENT_H -#include #include +#include + namespace plasp { namespace pddl @@ -15,39 +16,51 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -struct Requirement +class Requirement; +using Requirements = std::vector; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Requirement { - enum class Type - { - STRIPS, - Typing, - NegativePreconditions, - DisjunctivePreconditions, - Equality, - ExistentialPreconditions, - UniversalPreconditions, - QuantifiedPreconditions, - ConditionalEffects, - Fluents, - NumericFluents, - ObjectFluents, - ADL, - DurativeActions, - DurationInequalities, - ContinuousEffects, - DerivedPredicates, - TimedInitialLiterals, - Preferences, - Constraints, - ActionCosts - }; + public: + enum class Type + { + STRIPS, + Typing, + NegativePreconditions, + DisjunctivePreconditions, + Equality, + ExistentialPreconditions, + UniversalPreconditions, + QuantifiedPreconditions, + ConditionalEffects, + Fluents, + NumericFluents, + ObjectFluents, + ADL, + DurativeActions, + DurationInequalities, + ContinuousEffects, + DerivedPredicates, + TimedInitialLiterals, + Preferences, + Constraints, + ActionCosts + }; - using Types = std::vector; + static Requirement parse(utils::Parser &parser); - static Requirement::Type fromPDDL(utils::Parser &parser); + public: + Requirement(Type type); - static void toPDDL(std::ostream &ostream, Requirement::Type requirementType); - static void toASP(std::ostream &ostream, Requirement::Type requirementType); + Type type() const; + + void printAsPDDL(std::ostream &ostream) const; + void printAsASP(std::ostream &ostream) const; + + private: + Type m_type; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index 09fb06c..69f889d 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -60,7 +60,7 @@ const std::string &Domain::name() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const Requirement::Types &Domain::requirements() const +const Requirements &Domain::requirements() const { return m_requirements; } @@ -134,7 +134,7 @@ void Domain::parseRequirementsSection(utils::Parser &parser) if (parser.currentCharacter() == ':') parser.advance(); - m_requirements.emplace_back(Requirement::fromPDDL(parser)); + m_requirements.emplace_back(Requirement::parse(parser)); } if (m_requirements.empty()) @@ -145,9 +145,13 @@ void Domain::parseRequirementsSection(utils::Parser &parser) //////////////////////////////////////////////////////////////////////////////////////////////////// -bool Domain::hasRequirement(Requirement::Type requirement) const +bool Domain::hasRequirement(Requirement::Type requirementType) const { - const auto match = std::find(m_requirements.cbegin(), m_requirements.cend(), requirement); + const auto match = std::find_if(m_requirements.cbegin(), m_requirements.cend(), + [&](const auto &requirement) + { + return requirement.type() == requirementType; + }); return match != m_requirements.cend(); } @@ -162,7 +166,7 @@ void Domain::computeDerivedRequirements() if (hasRequirement(requirement)) return; - m_requirements.push_back(requirement); + m_requirements.push_back(Requirement(requirement)); }; // If no requirements are specified, assume STRIPS @@ -220,7 +224,7 @@ void Domain::checkConsistency() { throw ConsistencyException("Domain contains typing information but does not declare typing requirement"); - m_requirements.push_back(Requirement::Type::Typing); + m_requirements.push_back(Requirement(Requirement::Type::Typing)); } // Verify that all used types have been declared diff --git a/src/plasp/pddl/Requirement.cpp b/src/plasp/pddl/Requirement.cpp index c7b2ad3..95e4616 100644 --- a/src/plasp/pddl/Requirement.cpp +++ b/src/plasp/pddl/Requirement.cpp @@ -72,7 +72,14 @@ const RequirementTypeNames requirementTypesToASP = boost::assign::list_ofsecond; + return Requirement(match->second); } //////////////////////////////////////////////////////////////////////////////////////////////////// -void Requirement::toPDDL(std::ostream &ostream, Requirement::Type requirementType) +Requirement::Type Requirement::type() const { - const auto match = requirementTypesToPDDL.left.find(requirementType); + return m_type; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +void Requirement::printAsPDDL(std::ostream &ostream) const +{ + const auto match = requirementTypesToPDDL.left.find(m_type); BOOST_ASSERT(match != requirementTypesToPDDL.left.end()); @@ -97,9 +111,9 @@ void Requirement::toPDDL(std::ostream &ostream, Requirement::Type requirementTyp //////////////////////////////////////////////////////////////////////////////////////////////////// -void Requirement::toASP(std::ostream &ostream, Requirement::Type requirementType) +void Requirement::printAsASP(std::ostream &ostream) const { - const auto match = requirementTypesToASP.left.find(requirementType); + const auto match = requirementTypesToASP.left.find(m_type); BOOST_ASSERT(match != requirementTypesToASP.left.end()); diff --git a/tests/TestPDDLParser.cpp b/tests/TestPDDLParser.cpp index 6e0a87d..419e1eb 100644 --- a/tests/TestPDDLParser.cpp +++ b/tests/TestPDDLParser.cpp @@ -51,8 +51,8 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain) ASSERT_EQ(domain.name(), "BLOCKS"); ASSERT_EQ(domain.requirements().size(), 2u); - ASSERT_EQ(domain.requirements()[0], plasp::pddl::Requirement::Type::STRIPS); - ASSERT_EQ(domain.requirements()[1], plasp::pddl::Requirement::Type::Typing); + ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::STRIPS); + ASSERT_EQ(domain.requirements()[1].type(), plasp::pddl::Requirement::Type::Typing); ASSERT_EQ(domain.types().size(), 1u); @@ -83,7 +83,7 @@ TEST_F(PDDLParserTests, ParseStorageDomain) ASSERT_EQ(domain.name(), "Storage-Propositional"); ASSERT_EQ(domain.requirements().size(), 1u); - ASSERT_EQ(domain.requirements()[0], plasp::pddl::Requirement::Type::Typing); + ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::Typing); const auto area = domain.types().find("area"); ASSERT_NE(area, domain.types().cend());