Refactored Requirement to be a proper class.

This commit is contained in:
Patrick Lühne 2016-05-31 14:11:21 +02:00
parent cf1c66a085
commit 5f763e90fc
5 changed files with 79 additions and 48 deletions

View File

@ -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;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,9 +1,10 @@
#ifndef __PLASP__PDDL__REQUIREMENT_H
#define __PLASP__PDDL__REQUIREMENT_H
#include <plasp/utils/Parser.h>
#include <vector>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
@ -15,39 +16,51 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
struct Requirement
class Requirement;
using Requirements = std::vector<Requirement>;
////////////////////////////////////////////////////////////////////////////////////////////////////
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<Type>;
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;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -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

View File

@ -72,7 +72,14 @@ const RequirementTypeNames requirementTypesToASP = boost::assign::list_of<Requir
////////////////////////////////////////////////////////////////////////////////////////////////////
Requirement::Type Requirement::fromPDDL(utils::Parser &parser)
Requirement::Requirement(Requirement::Type type)
: m_type{type}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Requirement Requirement::parse(utils::Parser &parser)
{
const auto requirementName = parser.parseIdentifier(isIdentifier);
@ -81,14 +88,21 @@ Requirement::Type Requirement::fromPDDL(utils::Parser &parser)
if (match == requirementTypesToPDDL.right.end())
throw utils::ParserException(parser.row(), parser.column(), "Unknown PDDL requirement \"" + requirementName + "\"");
return match->second;
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());

View File

@ -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());