Big refactoring (build still broken).

This commit is contained in:
Patrick Lühne 2016-06-03 17:12:39 +02:00
parent 5abf1f8a84
commit daa063c338
29 changed files with 697 additions and 484 deletions

View File

@ -1,60 +0,0 @@
#ifndef __PLASP__PDDL__CONSTANT_H
#define __PLASP__PDDL__CONSTANT_H
#include <string>
#include <vector>
#include <plasp/pddl/Type.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constant
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Context;
////////////////////////////////////////////////////////////////////////////////////////////////////
class Constant
{
public:
static Constant &parse(utils::Parser &parser, Context &context);
static Constant &parseDeclaration(utils::Parser &parser, Context &context);
public:
const std::string &name() const;
const PrimitiveType *type() const;
bool isDeclared() const;
private:
Constant(std::string name);
void setDirty(bool isDirty = true);
bool isDirty() const;
void setDeclared();
void setType(const PrimitiveType *parentType);
bool m_isDirty;
bool m_isDeclared;
std::string m_name;
const PrimitiveType *m_type;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
#endif

View File

@ -6,9 +6,10 @@
#include <vector>
#include <plasp/pddl/Action.h>
#include <plasp/pddl/Constant.h>
#include <plasp/pddl/Predicate.h>
#include <plasp/pddl/Type.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
namespace plasp
{
@ -24,16 +25,14 @@ namespace pddl
class Context
{
public:
std::vector<std::unique_ptr<PrimitiveType>> primitiveTypes;
std::unordered_map<std::string, PrimitiveType *> primitiveTypesHashMap;
expressions::PrimitiveTypes primitiveTypes;
//std::unordered_map<std::string, expressions::PrimitiveType *> primitiveTypesHashMap;
std::vector<std::unique_ptr<EitherType>> eitherTypes;
expressions::Constants constants;
//std::unordered_map<std::string, expressions::Constant *> constantsHashMap;
std::vector<std::unique_ptr<Constant>> constants;
std::unordered_map<std::string, Constant *> constantsHashMap;
std::vector<std::unique_ptr<Predicate>> predicates;
std::unordered_map<PredicateHashMapKey, Predicate *> predicatesHashMap;
expressions::PredicateDeclarations predicateDeclarations;
//std::unordered_map<expressions::PredicateHashMapKey, expressions::Predicate *> predicatesHashMap;
std::vector<std::unique_ptr<Action>> actions;
};

View File

@ -4,9 +4,8 @@
#include <unordered_map>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Predicate.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Requirement.h>
#include <plasp/pddl/Type.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -28,9 +27,9 @@ class Domain
public:
const std::string &name() const;
const Requirements &requirements() const;
const std::vector<std::unique_ptr<PrimitiveType>> &types() const;
const std::vector<std::unique_ptr<Constant>> &constants() const;
const std::vector<std::unique_ptr<Predicate>> &predicates() const;
const expressions::PrimitiveTypes &types() const;
const expressions::Constants &constants() const;
const expressions::PredicateDeclarations &predicates() const;
const std::vector<std::unique_ptr<Action>> &actions() const;
private:

View File

@ -1,43 +0,0 @@
#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

@ -30,6 +30,13 @@ namespace expressions
class And;
using AndPointer = std::unique_ptr<And>;
class Constant;
using ConstantPointer = std::unique_ptr<Constant>;
using Constants = std::vector<ConstantPointer>;
class Either;
using EitherPointer = std::unique_ptr<Either>;
class Not;
using NotPointer = std::unique_ptr<Not>;
@ -38,6 +45,20 @@ using OrPointer = std::unique_ptr<Or>;
class Predicate;
using PredicatePointer = std::unique_ptr<Predicate>;
using Predicates = std::vector<PredicatePointer>;
class PredicateDeclaration;
using PredicateDeclarationPointer = std::unique_ptr<PredicateDeclaration>;
using PredicateDeclarations = std::vector<PredicateDeclarationPointer>;
class PrimitiveType;
using PrimitiveTypePointer = std::unique_ptr<PrimitiveType>;
using PrimitiveTypes = std::vector<PrimitiveTypePointer>;
template<class Type>
class Reference;
template<class Type>
using ReferencePointer = std::unique_ptr<Reference<Type>>;
class Variable;
using VariablePointer = std::unique_ptr<Variable>;

View File

@ -1,81 +0,0 @@
#ifndef __PLASP__PDDL__PREDICATE_H
#define __PLASP__PDDL__PREDICATE_H
#include <boost/functional/hash.hpp>
#include <plasp/pddl/expressions/Variable.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Context;
struct PredicateHashMapKey
{
std::string name;
size_t arity;
bool operator==(const PredicateHashMapKey &other) const
{
return arity == other.arity && name == other.name;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////
class Predicate
{
public:
static Predicate &parseDeclaration(utils::Parser &parser, Context &context);
public:
const std::string &name() const;
const expressions::Variables &arguments() const;
bool isDeclared() const;
private:
Predicate(std::string name);
void setDeclared();
bool m_isDeclared;
std::string m_name;
expressions::Variables m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace std
{
template<>
struct hash<plasp::pddl::PredicateHashMapKey>
{
std::size_t operator()(const plasp::pddl::PredicateHashMapKey &key) const
{
std::size_t seed = 0;
boost::hash_combine(seed, key.name);
boost::hash_combine(seed, key.arity);
return seed;
}
};
}
#endif

View File

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

View File

@ -0,0 +1,87 @@
#ifndef __PLASP__PDDL__EXPRESSION__CONSTANT_H
#define __PLASP__PDDL__EXPRESSION__CONSTANT_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/utils/Parser.h>
#include <plasp/utils/ParserException.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Constant
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Constant: public Expression
{
public:
static ConstantPointer parseDeclaration(utils::Parser &parser, Context &context);
static void parseTypedDeclaration(utils::Parser &parser, Context &context);
template<class Container>
static Constant *parseExisting(utils::Parser &parser, const Container &constants);
// TODO: method for lazy creation if not existing
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const PrimitiveType *type() const;
bool isDeclared() const;
private:
Constant();
void setDirty(bool isDirty = true);
bool isDirty() const;
void setDeclared();
void setType(const PrimitiveType *parentType);
bool m_isDirty;
bool m_isDeclared;
std::string m_name;
const PrimitiveType *m_type;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Container>
Constant *Constant::parseExisting(utils::Parser &parser, const Container &constants)
{
parser.skipWhiteSpace();
const auto constantName = parser.parseIdentifier(isIdentifier);
// TODO: use hash map
const auto match = std::find_if(constants.cbegin(), constants.cend(),
[&](const auto &constant)
{
return constant->name() == constantName;
});
const auto constantExists = (match != constants.cend());
if (!constantExists)
throw utils::ParserException(parser.row(), parser.column(), "Constant \"" + constantName + "\" used but never declared");
return match->get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,55 @@
#ifndef __PLASP__PDDL__EXPRESSION__EITHER_H
#define __PLASP__PDDL__EXPRESSION__EITHER_H
#include <plasp/pddl/expressions/NAry.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Either
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Either: public NAry
{
public:
template<typename ExpressionParser>
static EitherPointer parse(utils::Parser &parser, Context &context,
const Variables &parameters, ExpressionParser parseExpression);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
private:
Either() = default;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename ExpressionParser>
EitherPointer Either::parse(utils::Parser &parser, Context &context,
const Variables &parameters, ExpressionParser parseExpression)
{
auto expression = std::make_unique<Either>(Either());
expression->NAry::parse(parser, context, parameters, parseExpression);
if (expression->arguments().empty())
throw ConsistencyException("\"and\" expressions should not be empty");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -1,10 +1,6 @@
#ifndef __PLASP__PDDL__EXPRESSION__N_ARY_H
#define __PLASP__PDDL__EXPRESSION__N_ARY_H
#include <memory>
#include <string>
#include <vector>
#include <plasp/pddl/ConsistencyException.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Variable.h>

View File

@ -2,7 +2,6 @@
#define __PLASP__PDDL__EXPRESSION__NOT_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Predicate.h>
namespace plasp
{

View File

@ -2,8 +2,6 @@
#define __PLASP__PDDL__EXPRESSION__PREDICATE_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Predicate.h>
#include <plasp/pddl/expressions/Variable.h>
namespace plasp
{
@ -27,13 +25,20 @@ class Predicate: public Expression
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::vector<const Variable *> &arguments() const;
const std::string &name() const;
const Expressions &arguments() const;
bool isDeclared() const;
private:
Predicate() = default;
Predicate();
void setDeclared();
bool m_isDeclared;
std::string m_name;
std::vector<const Variable *> m_arguments;
Expressions m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,49 @@
#ifndef __PLASP__PDDL__EXPRESSION__PREDICATE_DECLARATION_H
#define __PLASP__PDDL__EXPRESSION__PREDICATE_DECLARATION_H
#include <plasp/pddl/Expression.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PredicateDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class PredicateDeclaration: public Expression
{
public:
static void parse(utils::Parser &parser, Context &context);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const Variables &arguments() const;
bool isDeclared() const;
private:
PredicateDeclaration();
void setDeclared();
bool m_isDeclared;
std::string m_name;
Variables m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,92 @@
#ifndef __PLASP__PDDL__EXPRESSION__PRIMITIVE_TYPE_H
#define __PLASP__PDDL__EXPRESSION__PRIMITIVE_TYPE_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/utils/Parser.h>
#include <plasp/utils/ParserException.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PrimitiveType
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class PrimitiveType: public Expression
{
public:
static PrimitiveTypePointer parseDeclaration(utils::Parser &parser, Context &context);
static void parseTypedDeclaration(utils::Parser &parser, Context &context);
template<class Container>
static PrimitiveType *parseExisting(utils::Parser &parser, const Container &primitiveTypes);
// TODO: method for lazy creation if not existing
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
const std::vector<const PrimitiveType *> &parentTypes() const;
bool isDeclared() const;
private:
PrimitiveType();
void setDirty(bool isDirty = true);
bool isDirty() const;
void setDeclared();
void addParentType(const PrimitiveType *parentType);
bool m_isDirty;
bool m_isDeclared;
std::string m_name;
std::vector<const PrimitiveType *> m_parentTypes;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Container>
PrimitiveType *PrimitiveType::parseExisting(utils::Parser &parser,
const Container &primitiveTypes)
{
parser.skipWhiteSpace();
const auto typeName = parser.parseIdentifier(isIdentifier);
// TODO: use hash map
const auto match = std::find_if(primitiveTypes.cbegin(), primitiveTypes.cend(),
[&](const auto &primitiveType)
{
return primitiveType->name() == typeName;
});
const auto typeExists = (match != primitiveTypes.cend());
// Return existing primitive types
if (!typeExists)
throw utils::ParserException(parser.row(), parser.column(), "Primitive type \"" + typeName + "\" used but never declared");
auto *type = match->get();
type->setDirty();
return type;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,76 @@
#ifndef __PLASP__PDDL__EXPRESSION__REFERENCE_H
#define __PLASP__PDDL__EXPRESSION__REFERENCE_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Reference
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
class Reference: public Expression
{
public:
Reference(const Type *value);
void accept(ExpressionVisitor &expressionVisitor) const override;
const Type *value() const;
private:
Reference();
const Type *m_value;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
Reference<Type>::Reference()
: m_value{nullptr}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
Reference<Type>::Reference(const Type *value)
: m_value{value}
{
BOOST_ASSERT(m_value != nullptr);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
void Reference<Type>::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Type>
const Type *Reference<Type>::value() const
{
return m_value;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -1,16 +1,18 @@
#ifndef __PLASP__PDDL__TYPE_H
#define __PLASP__PDDL__TYPE_H
#ifndef __PLASP__PDDL__EXPRESSIONS__TYPE_H
#define __PLASP__PDDL__EXPRESSIONS__TYPE_H
#include <boost/variant.hpp>
#include <plasp/pddl/EitherType.h>
#include <plasp/pddl/PrimitiveType.h>
#include <plasp/pddl/expressions/Either.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -18,16 +20,12 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Context;
////////////////////////////////////////////////////////////////////////////////////////////////////
using TypePtr = boost::variant<const PrimitiveType *, const EitherType *>;
TypePtr parseType(utils::Parser &parser, Context &context);
ExpressionPointer parseExistingPrimitiveType(utils::Parser &parser, Context &context, const Variables &parameters);
//ExpressionPointer parseExistingType(utils::Parser &parser, Context &context, const Variables &parameters);
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -2,7 +2,6 @@
#define __PLASP__PDDL__EXPRESSION__VARIABLE_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Type.h>
namespace plasp
{
@ -21,21 +20,22 @@ class Variable: public Expression
{
public:
static VariablePointer parseDeclaration(utils::Parser &parser);
static void parseTypedDeclaration(utils::Parser &parser, Context &context,
Variables &variables);
static const Variable *parse(utils::Parser &parser, const Variables &variables);
static void parseTypedDeclaration(utils::Parser &parser, Context &context,
Variables &parameters);
static const Variable *parseExisting(utils::Parser &parser, const Variables &variables);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const std::string &name() const;
TypePtr type() const;
const Expression *type() const;
void setDirty(bool isDirty = true);
bool isDirty() const;
void setType(TypePtr type);
void setType(const Expression *type);
private:
Variable();
@ -44,7 +44,10 @@ class Variable: public Expression
std::string m_name;
TypePtr m_type;
const Expression *m_type;
// Stores "either" expression if necessary
ExpressionPointer m_eitherExpression;
};
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -4,6 +4,7 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Type.h>
#include <plasp/utils/ParserException.h>
namespace plasp

View File

@ -4,6 +4,10 @@
#include <plasp/pddl/ConsistencyException.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
#include <plasp/pddl/expressions/Variable.h>
#include <plasp/utils/ParserException.h>
namespace plasp
@ -67,23 +71,23 @@ const Requirements &Domain::requirements() const
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<std::unique_ptr<PrimitiveType>> &Domain::types() const
const expressions::PrimitiveTypes &Domain::types() const
{
return m_context.primitiveTypes;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<std::unique_ptr<Constant>> &Domain::constants() const
const expressions::Constants &Domain::constants() const
{
return m_context.constants;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<std::unique_ptr<Predicate>> &Domain::predicates() const
const expressions::PredicateDeclarations &Domain::predicates() const
{
return m_context.predicates;
return m_context.predicateDeclarations;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -237,7 +241,7 @@ void Domain::parseTypeSection(utils::Parser &parser)
if (parser.currentCharacter() == '(')
throw utils::ParserException(parser.row(), parser.column(), "Only primitive types are allowed in type section");
PrimitiveType::parseDeclaration(parser, m_context);
expressions::PrimitiveType::parseTypedDeclaration(parser, m_context);
parser.skipWhiteSpace();
}
@ -254,7 +258,7 @@ void Domain::parseConstantSection(utils::Parser &parser)
// Store constants
while (parser.currentCharacter() != ')')
{
Constant::parseDeclaration(parser, m_context);
expressions::Constant::parseDeclaration(parser, m_context);
parser.skipWhiteSpace();
}
@ -271,7 +275,7 @@ void Domain::parsePredicateSection(utils::Parser &parser)
// Store predicates and their arguments
while (parser.currentCharacter() != ')')
{
Predicate::parseDeclaration(parser, m_context);
expressions::PredicateDeclaration::parse(parser, m_context);
parser.skipWhiteSpace();
}
@ -322,7 +326,7 @@ void Domain::checkConsistency()
});
// Verify that all used predicates have been declared
std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(),
std::for_each(m_context.predicateDeclarations.cbegin(), m_context.predicateDeclarations.cend(),
[&](const auto &predicate)
{
if (!predicate->isDeclared())
@ -330,6 +334,8 @@ void Domain::checkConsistency()
});
// Verify that all variables have types
// Verify that constants are unique
// Verify that all primitive types are unique
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -1,52 +0,0 @@
#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

@ -6,6 +6,8 @@
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Or.h>
#include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/expressions/Reference.h>
#include <plasp/utils/ParserException.h>
namespace plasp
@ -113,14 +115,14 @@ ExpressionPointer parseExpressionContent(const std::string &expressionIdentifier
else
{
// Check if predicate with that name exists
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
[&](const auto &predicate)
{
return predicate->name() == expressionIdentifier;
});
// If predicate exists, parse it
if (match != context.predicates.cend())
if (match != context.predicateDeclarations.cend())
expression = expressions::Predicate::parse(expressionIdentifier, parser, context, parameters);
else
throw utils::ParserException(parser.row(), parser.column(), "Expression \"" + expressionIdentifier + "\" not allowed in this context");
@ -176,14 +178,14 @@ ExpressionPointer parseEffectBodyExpressionContent(const std::string &expression
else
{
// Check if predicate with that name exists
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
[&](const auto &predicate)
{
return predicate->name() == expressionIdentifier;
});
// If predicate exists, parse it
if (match != context.predicates.cend())
if (match != context.predicateDeclarations.cend())
expression = expressions::Predicate::parse(expressionIdentifier, parser, context, parameters);
else
throw utils::ParserException(parser.row(), parser.column(), "Expression \"" + expressionIdentifier + "\" not allowed in this context");
@ -204,14 +206,14 @@ ExpressionPointer parsePredicate(utils::Parser &parser, Context &context,
ExpressionPointer expression;
// Check if predicate with that name exists
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
[&](const auto &predicate)
{
return predicate->name() == predicateName;
});
// If predicate exists, parse it
if (match == context.predicates.cend())
if (match == context.predicateDeclarations.cend())
throw utils::ParserException(parser.row(), parser.column(), "Unknown predicate \"" + predicateName + "\"");
expression = expressions::Predicate::parse(predicateName, parser, context, parameters);

View File

@ -1,14 +1,17 @@
#include <plasp/pddl/Constant.h>
#include <plasp/pddl/expressions/Constant.h>
#include <algorithm>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -16,66 +19,51 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Constant::Constant(std::string name)
Constant::Constant()
: m_isDirty{false},
m_isDeclared{false},
m_name(name),
m_type{nullptr}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Constant &Constant::parse(utils::Parser &parser, Context &context)
ConstantPointer Constant::parseDeclaration(utils::Parser &parser, Context &context)
{
parser.skipWhiteSpace();
const auto constantName = parser.parseIdentifier(isIdentifier);
const auto match = context.constantsHashMap.find(constantName);
const auto constantExists = (match != context.constantsHashMap.cend());
auto constant = std::make_unique<Constant>(Constant());
// Return existing primitive types
if (constantExists)
{
auto &constant = *match->second;
constant->m_name = parser.parseIdentifier(isIdentifier);
constant.setDirty();
// Flag constant for potentially upcoming type declaration
constant->setDirty();
return constant;
}
// Store new primitive type
context.constants.emplace_back(std::make_unique<Constant>(Constant(constantName)));
auto &constant = *context.constants.back();
// Add a pointer to the primitive type to the hash map
context.constantsHashMap.emplace(std::make_pair(constantName, &constant));
// Flag type for potentially upcoming parent type declaration
constant.setDirty();
// TODO: Store constant in hash map
return constant;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Constant &Constant::parseDeclaration(utils::Parser &parser, Context &context)
void Constant::parseTypedDeclaration(utils::Parser &parser, Context &context)
{
// Parse and store constant
auto &constant = parse(parser, context);
context.constants.emplace_back(parseDeclaration(parser, context));
const auto &constant = context.constants.back();
// Flag constant as correctly declared in the types section
constant.setDeclared();
constant->setDeclared();
parser.skipWhiteSpace();
// Check for typing information
if (!parser.advanceIf('-'))
return constant;
return;
// If existing, parse and store parent type
auto &type = PrimitiveType::parse(parser, context);
auto *type = PrimitiveType::parseExisting(parser, context.primitiveTypes);
// Assign parent type to all types that were previously flagged
std::for_each(context.constants.begin(), context.constants.end(),
@ -84,11 +72,16 @@ Constant &Constant::parseDeclaration(utils::Parser &parser, Context &context)
if (!constant->isDirty())
return;
constant->setType(&type);
constant->setType(type);
constant->setDirty(false);
});
}
return constant;
////////////////////////////////////////////////////////////////////////////////////////////////////
void Constant::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -144,3 +137,4 @@ const PrimitiveType *Constant::type() const
}
}
}

View File

@ -1,29 +1,27 @@
#include <plasp/pddl/Type.h>
#include <plasp/pddl/expressions/Either.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Type
//
////////////////////////////////////////////////////////////////////////////////////////////////////
TypePtr parseType(utils::Parser &parser, Context &context)
namespace expressions
{
parser.skipWhiteSpace();
if (parser.currentCharacter() == '(')
return &EitherType::parse(parser, context);
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Either
//
////////////////////////////////////////////////////////////////////////////////////////////////////
return &PrimitiveType::parse(parser, context);
void Either::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -1,6 +1,11 @@
#include <plasp/pddl/expressions/Predicate.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/Reference.h>
#include <plasp/pddl/expressions/Variable.h>
namespace plasp
{
@ -15,22 +20,46 @@ namespace expressions
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Predicate::Predicate()
: m_isDeclared{false}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
PredicatePointer Predicate::parse(std::string name, utils::Parser &parser,
Context &context, const Variables &parameters)
{
auto expression = std::make_unique<Predicate>(Predicate());
auto predicate = std::make_unique<Predicate>(Predicate());
expression->m_name = name;
predicate->m_name = name;
parser.skipWhiteSpace();
// Parse arguments
while (parser.currentCharacter() != ')')
expression->m_arguments.emplace_back(Variable::parse(parser, parameters));
{
// Parse variables
if (parser.currentCharacter() == '?')
{
const auto *variable = Variable::parseExisting(parser, parameters);
auto variableReference = std::make_unique<Reference<Variable>>(variable);
predicate->m_arguments.emplace_back(std::move(variableReference));
}
// Parse constants
else
{
const auto *constant = Constant::parseExisting(parser, context.constants);
auto constantReference = std::make_unique<Reference<Constant>>(constant);
predicate->m_arguments.emplace_back(std::move(constantReference));
}
parser.skipWhiteSpace();
}
// TODO: check that signature matches one of the declared ones
return expression;
return predicate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -42,7 +71,28 @@ void Predicate::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<const Variable *> &Predicate::arguments() const
void Predicate::setDeclared()
{
m_isDeclared = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Predicate::isDeclared() const
{
return m_isDeclared;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Predicate::name() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Expressions &Predicate::arguments() const
{
return m_arguments;
}

View File

@ -1,36 +1,39 @@
#include <plasp/pddl/Predicate.h>
#include <algorithm>
#include <plasp/pddl/expressions/PredicateDeclaration.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Constant.h>
#include <plasp/pddl/expressions/Reference.h>
#include <plasp/pddl/expressions/Variable.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Predicate
// PredicateDeclaration
//
////////////////////////////////////////////////////////////////////////////////////////////////////
Predicate::Predicate(std::string name)
: m_isDeclared{false},
m_name{name}
PredicateDeclaration::PredicateDeclaration()
: m_isDeclared{false}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
void PredicateDeclaration::parse(utils::Parser &parser, Context &context)
{
parser.expect<std::string>("(");
const auto predicateName = parser.parseIdentifier(isIdentifier);
auto predicate = std::make_unique<PredicateDeclaration>(PredicateDeclaration());
auto predicate = std::make_unique<Predicate>(Predicate(predicateName));
predicate->m_name = parser.parseIdentifier(isIdentifier);
// Flag predicate as correctly declared in the types section
predicate->setDeclared();
@ -47,42 +50,41 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
parser.expect<std::string>(")");
const auto predicateArity = predicate->m_arguments.size();
const PredicateHashMapKey key = {predicateName, predicateArity};
// Store new predicate
context.predicates.emplace_back(std::move(predicate));
// Add a pointer to the predicate to the hash map
context.predicatesHashMap.emplace(std::make_pair(key, context.predicates.back().get()));
return *context.predicates.back();
context.predicateDeclarations.emplace_back(std::move(predicate));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void Predicate::setDeclared()
void PredicateDeclaration::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void PredicateDeclaration::setDeclared()
{
m_isDeclared = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
bool Predicate::isDeclared() const
bool PredicateDeclaration::isDeclared() const
{
return m_isDeclared;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string &Predicate::name() const
const std::string &PredicateDeclaration::name() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const expressions::Variables &Predicate::arguments() const
const Variables &PredicateDeclaration::arguments() const
{
return m_arguments;
}
@ -91,3 +93,4 @@ const expressions::Variables &Predicate::arguments() const
}
}
}

View File

@ -1,14 +1,16 @@
#include <plasp/pddl/PrimitiveType.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
#include <algorithm>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
@ -16,70 +18,66 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
PrimitiveType::PrimitiveType(std::string name)
PrimitiveType::PrimitiveType()
: m_isDirty{false},
m_isDeclared{false},
m_name(name)
m_isDeclared{false}
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
PrimitiveType &PrimitiveType::parse(utils::Parser &parser, Context &context)
PrimitiveTypePointer PrimitiveType::parseDeclaration(utils::Parser &parser, Context &context)
{
parser.skipWhiteSpace();
const auto typeName = parser.parseIdentifier(isIdentifier);
const auto match = context.primitiveTypesHashMap.find(typeName);
const auto typeExists = (match != context.primitiveTypesHashMap.cend());
// Return existing primitive types
if (typeExists)
// TODO: refactor
if (context.primitiveTypes.empty())
{
auto &type = *match->second;
auto object = std::make_unique<PrimitiveType>(PrimitiveType());
object->m_name = "object";
object->setDirty();
object->setDeclared();
type.setDirty();
return type;
context.primitiveTypes.emplace_back(std::move(object));
}
// Store new primitive type
context.primitiveTypes.emplace_back(std::make_unique<PrimitiveType>(PrimitiveType(typeName)));
parser.skipWhiteSpace();
auto &type = *context.primitiveTypes.back();
auto type = std::make_unique<PrimitiveType>(PrimitiveType());
// Add a pointer to the primitive type to the hash map
context.primitiveTypesHashMap.emplace(std::make_pair(typeName, &type));
type->m_name = parser.parseIdentifier(isIdentifier);
// Flag type for potentially upcoming parent type declaration
type.setDirty();
type->setDirty();
// TODO: Store constant in hash map
return type;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
PrimitiveType &PrimitiveType::parseDeclaration(utils::Parser &parser, Context &context)
void PrimitiveType::parseTypedDeclaration(utils::Parser &parser, Context &context)
{
// Parse and store type
auto &type = parse(parser, context);
context.primitiveTypes.emplace_back(parseDeclaration(parser, context));
const auto &type = context.primitiveTypes.back();
// Flag type as correctly declared in the types section
type.setDeclared();
type->setDeclared();
parser.skipWhiteSpace();
// Check for type inheritance
if (!parser.advanceIf('-'))
return type;
return;
// If existing, parse and store parent type
auto &parentType = parse(parser, context);
auto *parentType = parseExisting(parser, context.primitiveTypes);
parentType.setDirty(false);
parentType->setDirty(false);
// Flag parent tpe as correctly declared in the types section
parentType.setDeclared();
parentType->setDeclared();
// Assign parent type to all types that were previously flagged
std::for_each(context.primitiveTypes.begin(), context.primitiveTypes.end(),
@ -88,11 +86,16 @@ PrimitiveType &PrimitiveType::parseDeclaration(utils::Parser &parser, Context &c
if (!childType->isDirty())
return;
childType->addParentType(&parentType);
childType->addParentType(parentType);
childType->setDirty(false);
});
}
return type;
////////////////////////////////////////////////////////////////////////////////////////////////////
void PrimitiveType::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -148,3 +151,4 @@ const std::vector<const PrimitiveType *> &PrimitiveType::parentTypes() const
}
}
}

View File

@ -0,0 +1,46 @@
#include <plasp/pddl/expressions/Type.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/expressions/Reference.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Type
//
////////////////////////////////////////////////////////////////////////////////////////////////////
ExpressionPointer parseExistingPrimitiveType(utils::Parser &parser, Context &context, const Variables &parameters)
{
auto reference = std::make_unique<Reference<PrimitiveType>>(PrimitiveType::parseExisting(parser, context.primitiveTypes));
return reference;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/*ExpressionPointer parseExistingType(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
// Parse either type (always begins with opening parenthesis)
if (parser.currentCharacter() == '(')
return Either::parse(parser, context, parameters, parseExistingPrimitiveType);
// Parse primitive type
auto type = std::make_unique<Reference<PrimitiveType>>(PrimitiveType::parseExisting(parser, context));
return type;
}*/
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -5,6 +5,8 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/Either.h>
#include <plasp/pddl/expressions/Type.h>
#include <plasp/utils/ParserException.h>
namespace plasp
@ -36,6 +38,8 @@ VariablePointer Variable::parseDeclaration(utils::Parser &parser)
auto variable = std::make_unique<Variable>(Variable());
variable->m_name = parser.parseIdentifier(isIdentifier);
// Flag variable for potentially upcoming type declaration
variable->setDirty();
return variable;
@ -43,10 +47,12 @@ VariablePointer Variable::parseDeclaration(utils::Parser &parser)
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Variables &variables)
void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Variables &parameters)
{
// Parse and store variable itself
variables.emplace_back(parseDeclaration(parser));
parameters.emplace_back(parseDeclaration(parser));
auto &parameter = parameters.back();
parser.skipWhiteSpace();
@ -54,24 +60,41 @@ void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Va
if (!parser.advanceIf('-'))
return;
// Parse argument type
const auto type = parseType(parser, context);
// TODO: do not allow nested either expressions
// Set the argument type for all previously flagged arguments
std::for_each(variables.begin(), variables.end(),
[&](auto &variable)
const auto setType =
[&](const auto *type)
{
if (!variable->isDirty())
return;
// Set the argument type for all previously flagged arguments
std::for_each(parameters.begin(), parameters.end(),
[&](auto &parameter)
{
if (!parameter->isDirty())
return;
variable->setType(type);
variable->setDirty(false);
});
parameter->setType(type);
parameter->setDirty(false);
});
};
// Parse argument of "either" type (always begins with opening parenthesis)
if (parser.currentCharacter() == '(')
{
parameter->m_eitherExpression = Either::parse(parser, context, parameters, parseExistingPrimitiveType);
setType(parameter->m_eitherExpression.get());
return;
}
// Parse primitive type
const auto *type = PrimitiveType::parseExisting(parser, context.primitiveTypes);
setType(type);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
const Variable *Variable::parse(utils::Parser &parser, const Variables &variables)
const Variable *Variable::parseExisting(utils::Parser &parser, const Variables &variables)
{
parser.skipWhiteSpace();
@ -107,7 +130,7 @@ const std::string &Variable::name() const
////////////////////////////////////////////////////////////////////////////////////////////////////
TypePtr Variable::type() const
const Expression *Variable::type() const
{
return m_type;
}
@ -128,7 +151,7 @@ bool Variable::isDirty() const
////////////////////////////////////////////////////////////////////////////////////////////////////
void Variable::setType(TypePtr type)
void Variable::setType(const Expression *type)
{
m_type = type;
}

View File

@ -6,6 +6,8 @@
#include <stdexcept>
#include <plasp/pddl/Description.h>
#include <plasp/pddl/expressions/PrimitiveType.h>
#include <plasp/pddl/expressions/Either.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -76,10 +78,10 @@ TEST_F(PDDLParserTests, ParseBlocksWorldDomain)
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());
const auto *onArgument0Type = dynamic_cast<const plasp::pddl::expressions::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());
const auto onArgument1Type = dynamic_cast<const plasp::pddl::expressions::PrimitiveType *>(on.arguments()[1]->type());
ASSERT_EQ(onArgument1Type, &block);
const auto &handempty = *domain.predicates()[3];
@ -139,20 +141,20 @@ TEST_F(PDDLParserTests, ParseStorageDomain)
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());
const auto onArgument0Type = dynamic_cast<const plasp::pddl::expressions::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());
const auto onArgument1Type = dynamic_cast<const plasp::pddl::expressions::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);
const auto inArgument0Type = dynamic_cast<const plasp::pddl::expressions::Either *>(in.arguments()[0]->type());
ASSERT_EQ(inArgument0Type->arguments().size(), 2u);
ASSERT_EQ(dynamic_cast<const plasp::pddl::expressions::PrimitiveType *>(inArgument0Type->arguments()[0].get()), &storearea);
ASSERT_EQ(dynamic_cast<const plasp::pddl::expressions::PrimitiveType *>(inArgument0Type->arguments()[1].get()), &crate);
}
catch (const std::exception &e)
{