From ced1fd00388cd01390811b8ddc1eb60bb6b4ef8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Wed, 1 Jun 2016 01:29:46 +0200 Subject: [PATCH] Started refactoring Types with variants. --- include/plasp/pddl/Type.h | 43 +------------ include/plasp/pddl/TypePrimitive.h | 61 +++++++++++++++++++ src/plasp/pddl/Domain.cpp | 12 +++- src/plasp/pddl/Predicate.cpp | 2 +- .../pddl/{Type.cpp => TypePrimitive.cpp} | 46 ++++++++------ tests/TestPDDLParser.cpp | 32 ++++------ 6 files changed, 113 insertions(+), 83 deletions(-) create mode 100644 include/plasp/pddl/TypePrimitive.h rename src/plasp/pddl/{Type.cpp => TypePrimitive.cpp} (71%) diff --git a/include/plasp/pddl/Type.h b/include/plasp/pddl/Type.h index ca3dd9f..9456c76 100644 --- a/include/plasp/pddl/Type.h +++ b/include/plasp/pddl/Type.h @@ -1,12 +1,9 @@ #ifndef __PLASP__PDDL__TYPE_H #define __PLASP__PDDL__TYPE_H -#include -#include -#include -#include +#include -#include +#include namespace plasp { @@ -19,45 +16,11 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -class Context; - -class Type; +using Type = boost::variant; using TypeHashMap = std::unordered_map; //////////////////////////////////////////////////////////////////////////////////////////////////// -class Type -{ - public: - static Type &parse(utils::Parser &parser, Context &context); - static Type &parseDeclaration(utils::Parser &parser, Context &context); - - public: - const std::string &name() const; - const std::vector &parentTypes() const; - - bool isDeclared() const; - - private: - Type(std::string name); - - void setDirty(bool isDirty = true); - bool isDirty() const; - - void setDeclared(); - - void addParentType(const Type &parentType); - - bool m_isDirty; - bool m_isDeclared; - - std::string m_name; - - std::vector m_parentTypes; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - } } diff --git a/include/plasp/pddl/TypePrimitive.h b/include/plasp/pddl/TypePrimitive.h new file mode 100644 index 0000000..d98a4e0 --- /dev/null +++ b/include/plasp/pddl/TypePrimitive.h @@ -0,0 +1,61 @@ +#ifndef __PLASP__PDDL__TYPE_PRIMITIVE_H +#define __PLASP__PDDL__TYPE_PRIMITIVE_H + +#include +#include +#include +#include + +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// TypePrimitive +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Context; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class TypePrimitive +{ + public: + static TypePrimitive &parse(utils::Parser &parser, Context &context); + static TypePrimitive &parseDeclaration(utils::Parser &parser, Context &context); + + public: + const std::string &name() const; + const std::vector &parentTypes() const; + + bool isDeclared() const; + + private: + TypePrimitive(std::string name); + + void setDirty(bool isDirty = true); + bool isDirty() const; + + void setDeclared(); + + void addParentType(const TypePrimitive &parentType); + + bool m_isDirty; + bool m_isDeclared; + + std::string m_name; + + std::vector m_parentTypes; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index 86f2350..611a613 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -215,7 +215,7 @@ void Domain::parseTypingSection(utils::Parser &parser) // Store types and their parent types while (parser.currentCharacter() != ')') { - Type::parseDeclaration(parser, m_context); + TypePrimitive::parseDeclaration(parser, m_context); parser.skipWhiteSpace(); } @@ -256,8 +256,14 @@ void Domain::checkConsistency() std::for_each(m_context.types.cbegin(), m_context.types.cend(), [&](const auto &type) { - if (!type.second.isDeclared()) - throw ConsistencyException("Type \"" + type.second.name() + "\" used but never declared"); + // TODO: refactor without typeinfo + if (type.second.type() != boost::typeindex::type_id()) + return; + + const auto &typePrimitive = boost::get(type.second); + + if (!typePrimitive.isDeclared()) + throw ConsistencyException("Type \"" + typePrimitive.name() + "\" used but never declared"); }); // Verify that all used predicates have been declared diff --git a/src/plasp/pddl/Predicate.cpp b/src/plasp/pddl/Predicate.cpp index a394850..7372fc6 100644 --- a/src/plasp/pddl/Predicate.cpp +++ b/src/plasp/pddl/Predicate.cpp @@ -49,7 +49,7 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context) continue; // Parse argument type - const auto &type = Type::parse(parser, context); + const auto &type = TypePrimitive::parse(parser, context); // Set the argument type for all previously flagged arguments std::for_each(predicate.m_arguments.begin(), predicate.m_arguments.end(), diff --git a/src/plasp/pddl/Type.cpp b/src/plasp/pddl/TypePrimitive.cpp similarity index 71% rename from src/plasp/pddl/Type.cpp rename to src/plasp/pddl/TypePrimitive.cpp index fdc36f4..a5ed203 100644 --- a/src/plasp/pddl/Type.cpp +++ b/src/plasp/pddl/TypePrimitive.cpp @@ -1,7 +1,9 @@ -#include +#include #include +#include + #include #include @@ -12,11 +14,11 @@ namespace pddl //////////////////////////////////////////////////////////////////////////////////////////////////// // -// Type +// TypePrimitive // //////////////////////////////////////////////////////////////////////////////////////////////////// -Type::Type(std::string name) +TypePrimitive::TypePrimitive(std::string name) : m_isDirty{false}, m_isDeclared{false}, m_name(name) @@ -25,7 +27,7 @@ Type::Type(std::string name) //////////////////////////////////////////////////////////////////////////////////////////////////// -Type &Type::parse(utils::Parser &parser, Context &context) +TypePrimitive &TypePrimitive::parse(utils::Parser &parser, Context &context) { parser.skipWhiteSpace(); @@ -36,24 +38,26 @@ Type &Type::parse(utils::Parser &parser, Context &context) if (typeExists) { auto &type = match->second; + auto &primitiveType = boost::get(type); - type.setDirty(); + primitiveType.setDirty(); - return type; + return primitiveType; } - const auto insertionResult = context.types.emplace(std::make_pair(typeName, Type(typeName))); + const auto insertionResult = context.types.emplace(std::make_pair(typeName, TypePrimitive(typeName))); auto &type = insertionResult.first->second; + auto &primitiveType = boost::get(type); // Flag type for potentially upcoming parent type declaration - type.setDirty(); + primitiveType.setDirty(); - return type; + return primitiveType; } //////////////////////////////////////////////////////////////////////////////////////////////////// -Type &Type::parseDeclaration(utils::Parser &parser, Context &context) +TypePrimitive &TypePrimitive::parseDeclaration(utils::Parser &parser, Context &context) { // Parse and store type auto &type = parse(parser, context); @@ -80,11 +84,13 @@ Type &Type::parseDeclaration(utils::Parser &parser, Context &context) std::for_each(context.types.begin(), context.types.end(), [&](auto &childType) { - if (!childType.second.isDirty()) + auto &childTypePrimitive = boost::get(childType.second); + + if (!childTypePrimitive.isDirty()) return; - childType.second.addParentType(parentType); - childType.second.setDirty(false); + childTypePrimitive.addParentType(parentType); + childTypePrimitive.setDirty(false); }); return type; @@ -92,49 +98,49 @@ Type &Type::parseDeclaration(utils::Parser &parser, Context &context) //////////////////////////////////////////////////////////////////////////////////////////////////// -void Type::setDirty(bool isDirty) +void TypePrimitive::setDirty(bool isDirty) { m_isDirty = isDirty; } //////////////////////////////////////////////////////////////////////////////////////////////////// -bool Type::isDirty() const +bool TypePrimitive::isDirty() const { return m_isDirty; } //////////////////////////////////////////////////////////////////////////////////////////////////// -void Type::setDeclared() +void TypePrimitive::setDeclared() { m_isDeclared = true; } //////////////////////////////////////////////////////////////////////////////////////////////////// -bool Type::isDeclared() const +bool TypePrimitive::isDeclared() const { return m_isDeclared; } //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::string &Type::name() const +const std::string &TypePrimitive::name() const { return m_name; } //////////////////////////////////////////////////////////////////////////////////////////////////// -void Type::addParentType(const Type &parentType) +void TypePrimitive::addParentType(const TypePrimitive &parentType) { m_parentTypes.emplace_back(&parentType); } //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector &Type::parentTypes() const +const std::vector &TypePrimitive::parentTypes() const { return m_parentTypes; } diff --git a/tests/TestPDDLParser.cpp b/tests/TestPDDLParser.cpp index ae76db5..554719b 100644 --- a/tests/TestPDDLParser.cpp +++ b/tests/TestPDDLParser.cpp @@ -56,11 +56,10 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain) ASSERT_EQ(domain.types().size(), 1u); - const auto block = domain.types().find("block"); - ASSERT_NE(block, domain.types().cend()); + const auto block = boost::get(domain.types().find("block")->second); - ASSERT_EQ(block->second.name(), "block"); - ASSERT_EQ(block->second.parentTypes().size(), 0u); + ASSERT_EQ(block.name(), "block"); + ASSERT_EQ(block.parentTypes().size(), 0u); ASSERT_EQ(domain.predicates().size(), 5u); @@ -89,25 +88,20 @@ TEST_F(PDDLParserTests, ParseStorageDomain) ASSERT_EQ(domain.requirements().size(), 1u); ASSERT_EQ(domain.requirements()[0].type(), plasp::pddl::Requirement::Type::Typing); - const auto area = domain.types().find("area"); - ASSERT_NE(area, domain.types().cend()); - const auto hoist = domain.types().find("hoist"); - ASSERT_NE(hoist, domain.types().cend()); - const auto object = domain.types().find("object"); - ASSERT_NE(object, domain.types().cend()); - const auto storearea = domain.types().find("storearea"); - ASSERT_NE(storearea, domain.types().cend()); - const auto surface= domain.types().find("surface"); - ASSERT_NE(surface, domain.types().cend()); + const auto area = boost::get(domain.types().find("area")->second); + const auto hoist = boost::get(domain.types().find("hoist")->second); + const auto object = boost::get(domain.types().find("object")->second); + const auto storearea = boost::get(domain.types().find("storearea")->second); + const auto surface = boost::get(domain.types().find("surface")->second); - const auto &hoistParents = hoist->second.parentTypes(); + const auto &hoistParents = hoist.parentTypes(); ASSERT_EQ(hoistParents.size(), 1u); - ASSERT_TRUE(std::find(hoistParents.cbegin(), hoistParents.cend(), &object->second) != hoistParents.cend()); + ASSERT_TRUE(std::find(hoistParents.cbegin(), hoistParents.cend(), &object) != hoistParents.cend()); - const auto &areaParents = area->second.parentTypes(); + const auto &areaParents = area.parentTypes(); ASSERT_EQ(areaParents.size(), 2u); - ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object->second) != areaParents.cend()); - ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface->second) != areaParents.cend()); + ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend()); + ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend()); } catch (const std::exception &e) {