Implemented predicate parsing and refactored context storage.

This commit is contained in:
Patrick Lühne 2016-06-01 14:11:13 +02:00
parent 44482ae438
commit 3c97ced486
14 changed files with 265 additions and 110 deletions

View File

@ -1,6 +1,10 @@
#ifndef __PLASP__PDDL__CONTEXT_H #ifndef __PLASP__PDDL__CONTEXT_H
#define __PLASP__PDDL__CONTEXT_H #define __PLASP__PDDL__CONTEXT_H
#include <memory>
#include <unordered_map>
#include <vector>
#include <plasp/pddl/Predicate.h> #include <plasp/pddl/Predicate.h>
#include <plasp/pddl/Type.h> #include <plasp/pddl/Type.h>
@ -18,8 +22,13 @@ namespace pddl
class Context class Context
{ {
public: public:
TypeHashMap types; std::vector<std::unique_ptr<PrimitiveType>> primitiveTypes;
PredicateHashMap predicates; std::unordered_map<std::string, PrimitiveType *> primitiveTypesHashMap;
std::vector<std::unique_ptr<EitherType>> eitherTypes;
std::vector<std::unique_ptr<Predicate>> predicates;
std::unordered_map<PredicateHashMapKey, Predicate *> predicatesHashMap;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -28,8 +28,8 @@ class Domain
public: public:
const std::string &name() const; const std::string &name() const;
const Requirements &requirements() const; const Requirements &requirements() const;
const TypeHashMap &types() const; const std::vector<std::unique_ptr<PrimitiveType>> &types() const;
const PredicateHashMap &predicates() const; const std::vector<std::unique_ptr<Predicate>> &predicates() const;
private: private:
Domain(Context &context); Domain(Context &context);
@ -40,7 +40,7 @@ class Domain
bool hasRequirement(Requirement::Type requirementType) const; bool hasRequirement(Requirement::Type requirementType) const;
void computeDerivedRequirements(); void computeDerivedRequirements();
void parseTypingSection(utils::Parser &parser); void parseTypeSection(utils::Parser &parser);
void parsePredicateSection(utils::Parser &parser); void parsePredicateSection(utils::Parser &parser);

View File

@ -0,0 +1,43 @@
#ifndef __PLASP__PDDL__EITHER_TYPE_H
#define __PLASP__PDDL__EITHER_TYPE_H
#include <vector>
#include <plasp/pddl/PrimitiveType.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// EitherType
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Context;
////////////////////////////////////////////////////////////////////////////////////////////////////
class EitherType
{
public:
static EitherType &parse(utils::Parser &parser, Context &context);
public:
const std::vector<const PrimitiveType *> &allowedTypes() const;
private:
EitherType() = default;
std::vector<const PrimitiveType *> m_allowedTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -1,7 +1,6 @@
#ifndef __PLASP__PDDL__PREDICATE_H #ifndef __PLASP__PDDL__PREDICATE_H
#define __PLASP__PDDL__PREDICATE_H #define __PLASP__PDDL__PREDICATE_H
#include <unordered_map>
#include <vector> #include <vector>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
@ -33,9 +32,6 @@ struct PredicateHashMapKey
} }
}; };
class Predicate;
using PredicateHashMap = std::unordered_map<PredicateHashMapKey, Predicate>;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
class Predicate class Predicate

View File

@ -1,5 +1,5 @@
#ifndef __PLASP__PDDL__TYPE_PRIMITIVE_H #ifndef __PLASP__PDDL__PRIMITIVE_TYPE_H
#define __PLASP__PDDL__TYPE_PRIMITIVE_H #define __PLASP__PDDL__PRIMITIVE_TYPE_H
#include <string> #include <string>
#include <vector> #include <vector>
@ -13,7 +13,7 @@ namespace pddl
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// TypePrimitive // PrimitiveType
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -21,34 +21,34 @@ class Context;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
class TypePrimitive class PrimitiveType
{ {
public: public:
static TypePrimitive &parse(utils::Parser &parser, Context &context); static PrimitiveType &parse(utils::Parser &parser, Context &context);
static TypePrimitive &parseDeclaration(utils::Parser &parser, Context &context); static PrimitiveType &parseDeclaration(utils::Parser &parser, Context &context);
public: public:
const std::string &name() const; const std::string &name() const;
const std::vector<const TypePrimitive *> &parentTypes() const; const std::vector<const PrimitiveType *> &parentTypes() const;
bool isDeclared() const; bool isDeclared() const;
private: private:
TypePrimitive(std::string name); PrimitiveType(std::string name);
void setDirty(bool isDirty = true); void setDirty(bool isDirty = true);
bool isDirty() const; bool isDirty() const;
void setDeclared(); void setDeclared();
void addParentType(const TypePrimitive &parentType); void addParentType(const PrimitiveType &parentType);
bool m_isDirty; bool m_isDirty;
bool m_isDeclared; bool m_isDeclared;
std::string m_name; std::string m_name;
std::vector<const TypePrimitive *> m_parentTypes; std::vector<const PrimitiveType *> m_parentTypes;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,11 +1,10 @@
#ifndef __PLASP__PDDL__TYPE_H #ifndef __PLASP__PDDL__TYPE_H
#define __PLASP__PDDL__TYPE_H #define __PLASP__PDDL__TYPE_H
#include <unordered_map>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <plasp/pddl/TypePrimitive.h> #include <plasp/pddl/EitherType.h>
#include <plasp/pddl/PrimitiveType.h>
namespace plasp namespace plasp
{ {
@ -18,8 +17,7 @@ namespace pddl
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
using Type = boost::variant<TypePrimitive>; using TypePtr = boost::variant<const PrimitiveType *, const EitherType *>;
using TypeHashMap = std::unordered_map<std::string, Type>;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -31,12 +31,12 @@ class Variable
public: public:
const std::string &name() const; const std::string &name() const;
const Type &type() const; TypePtr type() const;
void setDirty(bool isDirty = true); void setDirty(bool isDirty = true);
bool isDirty() const; bool isDirty() const;
void setType(const Type &type); void setType(TypePtr type);
private: private:
Variable(std::string name); Variable(std::string name);
@ -45,7 +45,7 @@ class Variable
std::string m_name; std::string m_name;
const Type *m_type; TypePtr m_type;
}; };
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -67,14 +67,14 @@ const Requirements &Domain::requirements() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const TypeHashMap &Domain::types() const const std::vector<std::unique_ptr<PrimitiveType>> &Domain::types() const
{ {
return m_context.types; return m_context.primitiveTypes;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const PredicateHashMap &Domain::predicates() const const std::vector<std::unique_ptr<Predicate>> &Domain::predicates() const
{ {
return m_context.predicates; return m_context.predicates;
} }
@ -114,7 +114,7 @@ void Domain::parseSection(utils::Parser &parser)
if (sectionIdentifier == "requirements") if (sectionIdentifier == "requirements")
parseRequirementsSection(parser); parseRequirementsSection(parser);
else if (sectionIdentifier == "types") else if (sectionIdentifier == "types")
parseTypingSection(parser); parseTypeSection(parser);
else if (sectionIdentifier == "constants") else if (sectionIdentifier == "constants")
skipSection(); skipSection();
else if (sectionIdentifier == "predicates") else if (sectionIdentifier == "predicates")
@ -208,14 +208,17 @@ void Domain::computeDerivedRequirements()
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Domain::parseTypingSection(utils::Parser &parser) void Domain::parseTypeSection(utils::Parser &parser)
{ {
parser.skipWhiteSpace(); parser.skipWhiteSpace();
// Store types and their parent types // Store types and their parent types
while (parser.currentCharacter() != ')') while (parser.currentCharacter() != ')')
{ {
TypePrimitive::parseDeclaration(parser, m_context); if (parser.currentCharacter() == '(')
throw utils::ParserException(parser.row(), parser.column(), "Only primitive types are allowed in type section");
PrimitiveType::parseDeclaration(parser, m_context);
parser.skipWhiteSpace(); parser.skipWhiteSpace();
} }
@ -245,7 +248,7 @@ void Domain::parsePredicateSection(utils::Parser &parser)
void Domain::checkConsistency() void Domain::checkConsistency()
{ {
// Verify that typing requirement is correctly declared if used // Verify that typing requirement is correctly declared if used
if (!m_context.types.empty() && !hasRequirement(Requirement::Type::Typing)) if (!m_context.primitiveTypes.empty() && !hasRequirement(Requirement::Type::Typing))
{ {
throw ConsistencyException("Domain contains typing information but does not declare typing requirement"); throw ConsistencyException("Domain contains typing information but does not declare typing requirement");
@ -253,25 +256,19 @@ void Domain::checkConsistency()
} }
// Verify that all used types have been declared // Verify that all used types have been declared
std::for_each(m_context.types.cbegin(), m_context.types.cend(), std::for_each(m_context.primitiveTypes.cbegin(), m_context.primitiveTypes.cend(),
[&](const auto &type) [&](const auto &type)
{ {
// TODO: refactor without typeinfo if (!type->isDeclared())
if (type.second.type() != boost::typeindex::type_id<TypePrimitive>()) throw ConsistencyException("Type \"" + type->name() + "\" used but never declared");
return;
const auto &typePrimitive = boost::get<TypePrimitive>(type.second);
if (!typePrimitive.isDeclared())
throw ConsistencyException("Type \"" + typePrimitive.name() + "\" used but never declared");
}); });
// Verify that all used predicates have been declared // Verify that all used predicates have been declared
std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(), std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(),
[&](const auto &predicate) [&](const auto &predicate)
{ {
if (!predicate.second.isDeclared()) if (!predicate->isDeclared())
throw ConsistencyException("Predicate \"" + predicate.second.name() + "\" used but never declared"); throw ConsistencyException("Predicate \"" + predicate->name() + "\" used but never declared");
}); });
// Verify that all variables have types // Verify that all variables have types

View File

@ -0,0 +1,52 @@
#include <plasp/pddl/EitherType.h>
#include <algorithm>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// EitherType
//
////////////////////////////////////////////////////////////////////////////////////////////////////
EitherType &EitherType::parse(utils::Parser &parser, Context &context)
{
parser.skipWhiteSpace();
auto eitherType = std::make_unique<EitherType>(EitherType());
parser.expect<std::string>("(either");
parser.skipWhiteSpace();
while (parser.currentCharacter() != ')')
{
eitherType->m_allowedTypes.push_back(&PrimitiveType::parse(parser, context));
parser.skipWhiteSpace();
}
context.eitherTypes.emplace_back(std::move(eitherType));
parser.expect<std::string>(")");
return *context.eitherTypes.back();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<const PrimitiveType *> &EitherType::allowedTypes() const
{
return m_allowedTypes;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -30,17 +30,17 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
const auto predicateName = parser.parseIdentifier(isIdentifier); const auto predicateName = parser.parseIdentifier(isIdentifier);
Predicate predicate(predicateName); auto predicate = std::make_unique<Predicate>(Predicate(predicateName));
// Flag predicate as correctly declared in the types section // Flag predicate as correctly declared in the types section
predicate.setDeclared(); predicate->setDeclared();
parser.skipWhiteSpace(); parser.skipWhiteSpace();
// Parse arguments // Parse arguments
while (parser.currentCharacter() != ')') while (parser.currentCharacter() != ')')
{ {
predicate.m_arguments.emplace_back(Variable::parse(parser, context)); predicate->m_arguments.emplace_back(Variable::parse(parser, context));
parser.skipWhiteSpace(); parser.skipWhiteSpace();
@ -48,11 +48,22 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
if (!parser.advanceIf('-')) if (!parser.advanceIf('-'))
continue; continue;
auto parseType =
[&]() -> TypePtr
{
parser.skipWhiteSpace();
if (parser.currentCharacter() == '(')
return TypePtr(&EitherType::parse(parser, context));
return TypePtr(&PrimitiveType::parse(parser, context));
};
// Parse argument type // Parse argument type
const auto &type = TypePrimitive::parse(parser, context); const auto type = parseType();
// Set the argument type for all previously flagged arguments // Set the argument type for all previously flagged arguments
std::for_each(predicate.m_arguments.begin(), predicate.m_arguments.end(), std::for_each(predicate->m_arguments.begin(), predicate->m_arguments.end(),
[&](auto &argument) [&](auto &argument)
{ {
if (!argument.isDirty()) if (!argument.isDirty())
@ -67,14 +78,16 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
parser.expect<std::string>(")"); parser.expect<std::string>(")");
const auto predicateArity = predicate.m_arguments.size(); const auto predicateArity = predicate->m_arguments.size();
const PredicateHashMapKey key = {predicateName, predicateArity}; const PredicateHashMapKey key = {predicateName, predicateArity};
const auto insertionResult = context.predicates.emplace(std::make_pair(key, std::move(predicate))); // Store new predicate
context.predicates.emplace_back(std::move(predicate));
std::cout << "Emplaced " << insertionResult.first->second.name() << std::endl; // Add a pointer to the predicate to the hash map
context.predicatesHashMap.emplace(std::make_pair(key, context.predicates.back().get()));
return insertionResult.first->second; return *context.predicates.back();
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,4 +1,4 @@
#include <plasp/pddl/TypePrimitive.h> #include <plasp/pddl/PrimitiveType.h>
#include <algorithm> #include <algorithm>
@ -12,11 +12,11 @@ namespace pddl
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// TypePrimitive // PrimitiveType
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
TypePrimitive::TypePrimitive(std::string name) PrimitiveType::PrimitiveType(std::string name)
: m_isDirty{false}, : m_isDirty{false},
m_isDeclared{false}, m_isDeclared{false},
m_name(name) m_name(name)
@ -25,37 +25,41 @@ TypePrimitive::TypePrimitive(std::string name)
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
TypePrimitive &TypePrimitive::parse(utils::Parser &parser, Context &context) PrimitiveType &PrimitiveType::parse(utils::Parser &parser, Context &context)
{ {
parser.skipWhiteSpace(); parser.skipWhiteSpace();
const auto typeName = parser.parseIdentifier(isIdentifier); const auto typeName = parser.parseIdentifier(isIdentifier);
const auto match = context.types.find(typeName); const auto match = context.primitiveTypesHashMap.find(typeName);
const auto typeExists = (match != context.types.cend()); const auto typeExists = (match != context.primitiveTypesHashMap.cend());
// Return existing primitive types
if (typeExists) if (typeExists)
{ {
auto &type = match->second; auto &type = *match->second;
auto &primitiveType = boost::get<TypePrimitive>(type);
primitiveType.setDirty(); type.setDirty();
return primitiveType; return type;
} }
const auto insertionResult = context.types.emplace(std::make_pair(typeName, TypePrimitive(typeName))); // Store new primitive type
auto &type = insertionResult.first->second; context.primitiveTypes.emplace_back(std::make_unique<PrimitiveType>(PrimitiveType(typeName)));
auto &primitiveType = boost::get<TypePrimitive>(type);
auto &type = *context.primitiveTypes.back();
// Add a pointer to the primitive type to the hash map
context.primitiveTypesHashMap.emplace(std::make_pair(typeName, &type));
// Flag type for potentially upcoming parent type declaration // Flag type for potentially upcoming parent type declaration
primitiveType.setDirty(); type.setDirty();
return primitiveType; return type;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
TypePrimitive &TypePrimitive::parseDeclaration(utils::Parser &parser, Context &context) PrimitiveType &PrimitiveType::parseDeclaration(utils::Parser &parser, Context &context)
{ {
// Parse and store type // Parse and store type
auto &type = parse(parser, context); auto &type = parse(parser, context);
@ -79,16 +83,14 @@ TypePrimitive &TypePrimitive::parseDeclaration(utils::Parser &parser, Context &c
parentType.setDeclared(); parentType.setDeclared();
// Assign parent type to all types that were previously flagged // Assign parent type to all types that were previously flagged
std::for_each(context.types.begin(), context.types.end(), std::for_each(context.primitiveTypes.begin(), context.primitiveTypes.end(),
[&](auto &childType) [&](auto &childType)
{ {
auto &childTypePrimitive = boost::get<TypePrimitive>(childType.second); if (!childType->isDirty())
if (!childTypePrimitive.isDirty())
return; return;
childTypePrimitive.addParentType(parentType); childType->addParentType(parentType);
childTypePrimitive.setDirty(false); childType->setDirty(false);
}); });
return type; return type;
@ -96,49 +98,49 @@ TypePrimitive &TypePrimitive::parseDeclaration(utils::Parser &parser, Context &c
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TypePrimitive::setDirty(bool isDirty) void PrimitiveType::setDirty(bool isDirty)
{ {
m_isDirty = isDirty; m_isDirty = isDirty;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool TypePrimitive::isDirty() const bool PrimitiveType::isDirty() const
{ {
return m_isDirty; return m_isDirty;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TypePrimitive::setDeclared() void PrimitiveType::setDeclared()
{ {
m_isDeclared = true; m_isDeclared = true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool TypePrimitive::isDeclared() const bool PrimitiveType::isDeclared() const
{ {
return m_isDeclared; return m_isDeclared;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &TypePrimitive::name() const const std::string &PrimitiveType::name() const
{ {
return m_name; return m_name;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void TypePrimitive::addParentType(const TypePrimitive &parentType) void PrimitiveType::addParentType(const PrimitiveType &parentType)
{ {
m_parentTypes.emplace_back(&parentType); m_parentTypes.emplace_back(&parentType);
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<const TypePrimitive *> &TypePrimitive::parentTypes() const const std::vector<const PrimitiveType *> &PrimitiveType::parentTypes() const
{ {
return m_parentTypes; return m_parentTypes;
} }

View File

@ -61,18 +61,16 @@ const std::string &Variable::name() const
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::setType(const Type &type) void Variable::setType(TypePtr type)
{ {
m_type = &type; m_type = type;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const Type &Variable::type() const TypePtr Variable::type() const
{ {
BOOST_ASSERT(m_type != nullptr); return m_type;
return *m_type;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -48,22 +48,40 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain)
const auto &domain = description.domain(); const auto &domain = description.domain();
// Name
ASSERT_EQ(domain.name(), "BLOCKS"); ASSERT_EQ(domain.name(), "BLOCKS");
// Requirements
ASSERT_EQ(domain.requirements().size(), 2u); ASSERT_EQ(domain.requirements().size(), 2u);
ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::STRIPS); 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.requirements()[1].type(), plasp::pddl::Requirement::Type::Typing);
// Types
ASSERT_EQ(domain.types().size(), 1u); ASSERT_EQ(domain.types().size(), 1u);
const auto block = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("block")->second); const auto &block = *domain.types()[0];
ASSERT_EQ(block.name(), "block"); ASSERT_EQ(block.name(), "block");
ASSERT_EQ(block.parentTypes().size(), 0u); ASSERT_EQ(block.parentTypes().size(), 0u);
// Predicates
ASSERT_EQ(domain.predicates().size(), 5u); ASSERT_EQ(domain.predicates().size(), 5u);
const auto on = domain.predicates().find({"on", 2}); const auto &on = *domain.predicates()[0];
ASSERT_EQ(on.name(), "on");
ASSERT_EQ(on.arguments().size(), 2u);
ASSERT_EQ(on.arguments()[0].name(), "x");
const auto onArgument0Type = boost::get<const plasp::pddl::PrimitiveType *>(on.arguments()[0].type());
ASSERT_EQ(onArgument0Type, &block);
ASSERT_EQ(on.arguments()[1].name(), "y");
const auto onArgument1Type = boost::get<const plasp::pddl::PrimitiveType *>(on.arguments()[1].type());
ASSERT_EQ(onArgument1Type, &block);
const auto &handempty = *domain.predicates()[3];
ASSERT_EQ(handempty.name(), "handempty");
ASSERT_TRUE(handempty.arguments().empty());
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {
@ -83,16 +101,22 @@ TEST_F(PDDLParserTests, ParseStorageDomain)
const auto &domain = description.domain(); const auto &domain = description.domain();
// Name
ASSERT_EQ(domain.name(), "Storage-Propositional"); ASSERT_EQ(domain.name(), "Storage-Propositional");
// Requirements
ASSERT_EQ(domain.requirements().size(), 1u); ASSERT_EQ(domain.requirements().size(), 1u);
ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::Typing); ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::Typing);
const auto area = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("area")->second); // Types
const auto hoist = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("hoist")->second); ASSERT_EQ(domain.types().size(), 10u);
const auto object = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("object")->second);
const auto storearea = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("storearea")->second); const auto &hoist = *domain.types()[0];
const auto surface = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("surface")->second); const auto &surface = *domain.types()[1];
const auto &area = *domain.types()[3];
const auto &object = *domain.types()[4];
const auto &storearea = *domain.types()[7];
const auto &crate = *domain.types()[9];
const auto &hoistParents = hoist.parentTypes(); const auto &hoistParents = hoist.parentTypes();
ASSERT_EQ(hoistParents.size(), 1u); ASSERT_EQ(hoistParents.size(), 1u);
@ -102,6 +126,29 @@ TEST_F(PDDLParserTests, ParseStorageDomain)
ASSERT_EQ(areaParents.size(), 2u); ASSERT_EQ(areaParents.size(), 2u);
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend()); ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend());
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend()); ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend());
// Predicates
ASSERT_EQ(domain.predicates().size(), 8u);
const auto &on = *domain.predicates()[5];
ASSERT_EQ(on.name(), "on");
ASSERT_EQ(on.arguments().size(), 2u);
ASSERT_EQ(on.arguments()[0].name(), "c");
const auto onArgument0Type = boost::get<const plasp::pddl::PrimitiveType *>(on.arguments()[0].type());
ASSERT_EQ(onArgument0Type, &crate);
ASSERT_EQ(on.arguments()[1].name(), "s");
const auto onArgument1Type = boost::get<const plasp::pddl::PrimitiveType *>(on.arguments()[1].type());
ASSERT_EQ(onArgument1Type, &storearea);
const auto &in = *domain.predicates()[1];
ASSERT_EQ(in.name(), "in");
ASSERT_EQ(in.arguments().size(), 2u);
ASSERT_EQ(in.arguments()[0].name(), "x");
const auto inArgument0Type = boost::get<const plasp::pddl::EitherType *>(in.arguments()[0].type());
ASSERT_EQ(inArgument0Type->allowedTypes().size(), 2u);
ASSERT_EQ(inArgument0Type->allowedTypes()[0], &storearea);
ASSERT_EQ(inArgument0Type->allowedTypes()[1], &crate);
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {

View File

@ -1,32 +1,32 @@
(define (domain Storage-Propositional) (define (domain Storage-Propositional)
(:requirements :typing) (:requirements :typing)
(:types hoist surface place area - object (:types hoist surface place area - object
container depot - place container depot - place
storearea transitarea - area storearea transitarea - area
area crate - surface) area crate - surface)
(:predicates (clear ?s - storearea) (:predicates (clear ?s - storearea)
(in ?x - (either storearea crate) ?p - place) (in ?x - (either storearea crate) ?p - place)
(available ?h - hoist) (available ?h - hoist)
(lifting ?h - hoist ?c - crate) (lifting ?h - hoist ?c - crate)
(at ?h - hoist ?a - area) (at ?h - hoist ?a - area)
(on ?c - crate ?s - storearea) (on ?c - crate ?s - storearea)
(connected ?a1 ?a2 - area) (connected ?a1 ?a2 - area)
(compatible ?c1 ?c2 - crate)) (compatible ?c1 ?c2 - crate))
(:action lift (:action lift
:parameters (?h - hoist ?c - crate ?a1 - storearea ?a2 - area ?p - place) :parameters (?h - hoist ?c - crate ?a1 - storearea ?a2 - area ?p - place)
:precondition (and (connected ?a1 ?a2) (at ?h ?a2) (available ?h) :precondition (and (connected ?a1 ?a2) (at ?h ?a2) (available ?h)
(on ?c ?a1) (in ?a1 ?p)) (on ?c ?a1) (in ?a1 ?p))
:effect (and (not (on ?c ?a1)) (clear ?a1) :effect (and (not (on ?c ?a1)) (clear ?a1)
(not (available ?h)) (lifting ?h ?c) (not (in ?c ?p)))) (not (available ?h)) (lifting ?h ?c) (not (in ?c ?p))))
(:action drop (:action drop
:parameters (?h - hoist ?c - crate ?a1 - storearea ?a2 - area ?p - place) :parameters (?h - hoist ?c - crate ?a1 - storearea ?a2 - area ?p - place)
:precondition (and (connected ?a1 ?a2) (at ?h ?a2) (lifting ?h ?c) :precondition (and (connected ?a1 ?a2) (at ?h ?a2) (lifting ?h ?c)
(clear ?a1) (in ?a1 ?p)) (clear ?a1) (in ?a1 ?p))
:effect (and (not (lifting ?h ?c)) (available ?h) :effect (and (not (lifting ?h ?c)) (available ?h)
(not (clear ?a1)) (on ?c ?a1) (in ?c ?p))) (not (clear ?a1)) (on ?c ?a1) (in ?c ?p)))
(:action move (:action move
:parameters (?h - hoist ?from ?to - storearea) :parameters (?h - hoist ?from ?to - storearea)