Started refactoring Types with variants.

This commit is contained in:
Patrick Lühne 2016-06-01 01:29:46 +02:00
parent 2654a6ff23
commit ced1fd0038
6 changed files with 113 additions and 83 deletions

View File

@ -1,12 +1,9 @@
#ifndef __PLASP__PDDL__TYPE_H #ifndef __PLASP__PDDL__TYPE_H
#define __PLASP__PDDL__TYPE_H #define __PLASP__PDDL__TYPE_H
#include <functional> #include <boost/variant.hpp>
#include <string>
#include <unordered_map>
#include <vector>
#include <plasp/utils/Parser.h> #include <plasp/pddl/TypePrimitive.h>
namespace plasp namespace plasp
{ {
@ -19,45 +16,11 @@ namespace pddl
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
class Context; using Type = boost::variant<TypePrimitive>;
class Type;
using TypeHashMap = std::unordered_map<std::string, Type>; using TypeHashMap = std::unordered_map<std::string, Type>;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
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<const Type *> &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<const Type *> m_parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }

View File

@ -0,0 +1,61 @@
#ifndef __PLASP__PDDL__TYPE_PRIMITIVE_H
#define __PLASP__PDDL__TYPE_PRIMITIVE_H
#include <functional>
#include <string>
#include <unordered_map>
#include <vector>
#include <plasp/utils/Parser.h>
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<const TypePrimitive *> &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<const TypePrimitive *> m_parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -215,7 +215,7 @@ void Domain::parseTypingSection(utils::Parser &parser)
// Store types and their parent types // Store types and their parent types
while (parser.currentCharacter() != ')') while (parser.currentCharacter() != ')')
{ {
Type::parseDeclaration(parser, m_context); TypePrimitive::parseDeclaration(parser, m_context);
parser.skipWhiteSpace(); parser.skipWhiteSpace();
} }
@ -256,8 +256,14 @@ void Domain::checkConsistency()
std::for_each(m_context.types.cbegin(), m_context.types.cend(), std::for_each(m_context.types.cbegin(), m_context.types.cend(),
[&](const auto &type) [&](const auto &type)
{ {
if (!type.second.isDeclared()) // TODO: refactor without typeinfo
throw ConsistencyException("Type \"" + type.second.name() + "\" used but never declared"); if (type.second.type() != boost::typeindex::type_id<TypePrimitive>())
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

View File

@ -49,7 +49,7 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
continue; continue;
// Parse argument type // 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 // 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(),

View File

@ -1,7 +1,9 @@
#include <plasp/pddl/Type.h> #include <plasp/pddl/TypePrimitive.h>
#include <algorithm> #include <algorithm>
#include <boost/variant.hpp>
#include <plasp/pddl/Context.h> #include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h> #include <plasp/pddl/Identifier.h>
@ -12,11 +14,11 @@ namespace pddl
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Type // TypePrimitive
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Type::Type(std::string name) TypePrimitive::TypePrimitive(std::string name)
: m_isDirty{false}, : m_isDirty{false},
m_isDeclared{false}, m_isDeclared{false},
m_name(name) 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(); parser.skipWhiteSpace();
@ -36,24 +38,26 @@ Type &Type::parse(utils::Parser &parser, Context &context)
if (typeExists) if (typeExists)
{ {
auto &type = match->second; auto &type = match->second;
auto &primitiveType = boost::get<TypePrimitive>(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 &type = insertionResult.first->second;
auto &primitiveType = boost::get<TypePrimitive>(type);
// Flag type for potentially upcoming parent type declaration // 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 // Parse and store type
auto &type = parse(parser, context); 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(), std::for_each(context.types.begin(), context.types.end(),
[&](auto &childType) [&](auto &childType)
{ {
if (!childType.second.isDirty()) auto &childTypePrimitive = boost::get<TypePrimitive>(childType.second);
if (!childTypePrimitive.isDirty())
return; return;
childType.second.addParentType(parentType); childTypePrimitive.addParentType(parentType);
childType.second.setDirty(false); childTypePrimitive.setDirty(false);
}); });
return type; 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; m_isDirty = isDirty;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool Type::isDirty() const bool TypePrimitive::isDirty() const
{ {
return m_isDirty; return m_isDirty;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Type::setDeclared() void TypePrimitive::setDeclared()
{ {
m_isDeclared = true; m_isDeclared = true;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
bool Type::isDeclared() const bool TypePrimitive::isDeclared() const
{ {
return m_isDeclared; return m_isDeclared;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Type::name() const const std::string &TypePrimitive::name() const
{ {
return m_name; return m_name;
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
void Type::addParentType(const Type &parentType) void TypePrimitive::addParentType(const TypePrimitive &parentType)
{ {
m_parentTypes.emplace_back(&parentType); m_parentTypes.emplace_back(&parentType);
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<const Type *> &Type::parentTypes() const const std::vector<const TypePrimitive *> &TypePrimitive::parentTypes() const
{ {
return m_parentTypes; return m_parentTypes;
} }

View File

@ -56,11 +56,10 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain)
ASSERT_EQ(domain.types().size(), 1u); ASSERT_EQ(domain.types().size(), 1u);
const auto block = domain.types().find("block"); const auto block = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("block")->second);
ASSERT_NE(block, domain.types().cend());
ASSERT_EQ(block->second.name(), "block"); ASSERT_EQ(block.name(), "block");
ASSERT_EQ(block->second.parentTypes().size(), 0u); ASSERT_EQ(block.parentTypes().size(), 0u);
ASSERT_EQ(domain.predicates().size(), 5u); ASSERT_EQ(domain.predicates().size(), 5u);
@ -89,25 +88,20 @@ TEST_F(PDDLParserTests, ParseStorageDomain)
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 = domain.types().find("area"); const auto area = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("area")->second);
ASSERT_NE(area, domain.types().cend()); const auto hoist = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("hoist")->second);
const auto hoist = domain.types().find("hoist"); const auto object = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("object")->second);
ASSERT_NE(hoist, domain.types().cend()); const auto storearea = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("storearea")->second);
const auto object = domain.types().find("object"); const auto surface = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("surface")->second);
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 &hoistParents = hoist->second.parentTypes(); const auto &hoistParents = hoist.parentTypes();
ASSERT_EQ(hoistParents.size(), 1u); 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_EQ(areaParents.size(), 2u);
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object->second) != areaParents.cend()); ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend());
ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface->second) != areaParents.cend()); ASSERT_TRUE(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend());
} }
catch (const std::exception &e) catch (const std::exception &e)
{ {