Started refactoring Types with variants.
This commit is contained in:
parent
2654a6ff23
commit
ced1fd0038
@ -1,12 +1,9 @@
|
||||
#ifndef __PLASP__PDDL__TYPE_H
|
||||
#define __PLASP__PDDL__TYPE_H
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <plasp/utils/Parser.h>
|
||||
#include <plasp/pddl/TypePrimitive.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@ -19,45 +16,11 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class Context;
|
||||
|
||||
class Type;
|
||||
using Type = boost::variant<TypePrimitive>;
|
||||
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;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
61
include/plasp/pddl/TypePrimitive.h
Normal file
61
include/plasp/pddl/TypePrimitive.h
Normal 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
|
@ -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<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
|
||||
|
@ -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(),
|
||||
|
@ -1,7 +1,9 @@
|
||||
#include <plasp/pddl/Type.h>
|
||||
#include <plasp/pddl/TypePrimitive.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
#include <plasp/pddl/Context.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_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<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 &primitiveType = boost::get<TypePrimitive>(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<TypePrimitive>(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<const Type *> &Type::parentTypes() const
|
||||
const std::vector<const TypePrimitive *> &TypePrimitive::parentTypes() const
|
||||
{
|
||||
return m_parentTypes;
|
||||
}
|
@ -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<plasp::pddl::TypePrimitive>(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<plasp::pddl::TypePrimitive>(domain.types().find("area")->second);
|
||||
const auto hoist = boost::get<plasp::pddl::TypePrimitive>(domain.types().find("hoist")->second);
|
||||
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 surface = boost::get<plasp::pddl::TypePrimitive>(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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user