Implemented predicate parsing and refactored context storage.

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

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;
}
@@ -114,7 +114,7 @@ void Domain::parseSection(utils::Parser &parser)
if (sectionIdentifier == "requirements")
parseRequirementsSection(parser);
else if (sectionIdentifier == "types")
parseTypingSection(parser);
parseTypeSection(parser);
else if (sectionIdentifier == "constants")
skipSection();
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();
// Store types and their parent types
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();
}
@@ -245,7 +248,7 @@ void Domain::parsePredicateSection(utils::Parser &parser)
void Domain::checkConsistency()
{
// 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");
@@ -253,25 +256,19 @@ void Domain::checkConsistency()
}
// 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)
{
// 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");
if (!type->isDeclared())
throw ConsistencyException("Type \"" + type->name() + "\" used but never declared");
});
// Verify that all used predicates have been declared
std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(),
[&](const auto &predicate)
{
if (!predicate.second.isDeclared())
throw ConsistencyException("Predicate \"" + predicate.second.name() + "\" used but never declared");
if (!predicate->isDeclared())
throw ConsistencyException("Predicate \"" + predicate->name() + "\" used but never declared");
});
// 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);
Predicate predicate(predicateName);
auto predicate = std::make_unique<Predicate>(Predicate(predicateName));
// Flag predicate as correctly declared in the types section
predicate.setDeclared();
predicate->setDeclared();
parser.skipWhiteSpace();
// Parse arguments
while (parser.currentCharacter() != ')')
{
predicate.m_arguments.emplace_back(Variable::parse(parser, context));
predicate->m_arguments.emplace_back(Variable::parse(parser, context));
parser.skipWhiteSpace();
@@ -48,11 +48,22 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
if (!parser.advanceIf('-'))
continue;
auto parseType =
[&]() -> TypePtr
{
parser.skipWhiteSpace();
if (parser.currentCharacter() == '(')
return TypePtr(&EitherType::parse(parser, context));
return TypePtr(&PrimitiveType::parse(parser, context));
};
// Parse argument type
const auto &type = TypePrimitive::parse(parser, context);
const auto type = parseType();
// 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)
{
if (!argument.isDirty())
@@ -67,14 +78,16 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
parser.expect<std::string>(")");
const auto predicateArity = predicate.m_arguments.size();
const auto predicateArity = predicate->m_arguments.size();
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>
@@ -12,11 +12,11 @@ namespace pddl
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TypePrimitive
// PrimitiveType
//
////////////////////////////////////////////////////////////////////////////////////////////////////
TypePrimitive::TypePrimitive(std::string name)
PrimitiveType::PrimitiveType(std::string name)
: m_isDirty{false},
m_isDeclared{false},
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();
const auto typeName = parser.parseIdentifier(isIdentifier);
const auto match = context.types.find(typeName);
const auto typeExists = (match != context.types.cend());
const auto match = context.primitiveTypesHashMap.find(typeName);
const auto typeExists = (match != context.primitiveTypesHashMap.cend());
// Return existing primitive types
if (typeExists)
{
auto &type = match->second;
auto &primitiveType = boost::get<TypePrimitive>(type);
auto &type = *match->second;
primitiveType.setDirty();
type.setDirty();
return primitiveType;
return type;
}
const auto insertionResult = context.types.emplace(std::make_pair(typeName, TypePrimitive(typeName)));
auto &type = insertionResult.first->second;
auto &primitiveType = boost::get<TypePrimitive>(type);
// Store new primitive type
context.primitiveTypes.emplace_back(std::make_unique<PrimitiveType>(PrimitiveType(typeName)));
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
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
auto &type = parse(parser, context);
@@ -79,16 +83,14 @@ TypePrimitive &TypePrimitive::parseDeclaration(utils::Parser &parser, Context &c
parentType.setDeclared();
// 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 &childTypePrimitive = boost::get<TypePrimitive>(childType.second);
if (!childTypePrimitive.isDirty())
if (!childType->isDirty())
return;
childTypePrimitive.addParentType(parentType);
childTypePrimitive.setDirty(false);
childType->addParentType(parentType);
childType->setDirty(false);
});
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool TypePrimitive::isDirty() const
bool PrimitiveType::isDirty() const
{
return m_isDirty;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TypePrimitive::setDeclared()
void PrimitiveType::setDeclared()
{
m_isDeclared = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool TypePrimitive::isDeclared() const
bool PrimitiveType::isDeclared() const
{
return m_isDeclared;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &TypePrimitive::name() const
const std::string &PrimitiveType::name() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void TypePrimitive::addParentType(const TypePrimitive &parentType)
void PrimitiveType::addParentType(const PrimitiveType &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;
}

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