From daa063c3385893b77890f3da4f5bf3dd760e5646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Fri, 3 Jun 2016 17:12:39 +0200 Subject: [PATCH] Big refactoring (build still broken). --- include/plasp/pddl/Constant.h | 60 ------------ include/plasp/pddl/Context.h | 21 ++--- include/plasp/pddl/Domain.h | 9 +- include/plasp/pddl/EitherType.h | 43 --------- include/plasp/pddl/Expression.h | 21 +++++ include/plasp/pddl/Predicate.h | 81 ---------------- include/plasp/pddl/PrimitiveType.h | 59 ------------ include/plasp/pddl/expressions/Constant.h | 87 ++++++++++++++++++ include/plasp/pddl/expressions/Either.h | 55 +++++++++++ include/plasp/pddl/expressions/NAry.h | 4 - include/plasp/pddl/expressions/Not.h | 1 - include/plasp/pddl/expressions/Predicate.h | 15 ++- .../pddl/expressions/PredicateDeclaration.h | 49 ++++++++++ .../plasp/pddl/expressions/PrimitiveType.h | 92 +++++++++++++++++++ include/plasp/pddl/expressions/Reference.h | 76 +++++++++++++++ include/plasp/pddl/{ => expressions}/Type.h | 20 ++-- include/plasp/pddl/expressions/Variable.h | 17 ++-- src/plasp/pddl/Action.cpp | 1 + src/plasp/pddl/Domain.cpp | 22 +++-- src/plasp/pddl/EitherType.cpp | 52 ----------- src/plasp/pddl/Expression.cpp | 14 +-- src/plasp/pddl/{ => expressions}/Constant.cpp | 60 ++++++------ .../pddl/{Type.cpp => expressions/Either.cpp} | 26 +++--- src/plasp/pddl/expressions/Predicate.cpp | 60 +++++++++++- .../PredicateDeclaration.cpp} | 49 +++++----- .../pddl/{ => expressions}/PrimitiveType.cpp | 70 +++++++------- src/plasp/pddl/expressions/Type.cpp | 46 ++++++++++ src/plasp/pddl/expressions/Variable.cpp | 53 ++++++++--- tests/TestPDDLParser.cpp | 18 ++-- 29 files changed, 697 insertions(+), 484 deletions(-) delete mode 100644 include/plasp/pddl/Constant.h delete mode 100644 include/plasp/pddl/EitherType.h delete mode 100644 include/plasp/pddl/Predicate.h delete mode 100644 include/plasp/pddl/PrimitiveType.h create mode 100644 include/plasp/pddl/expressions/Constant.h create mode 100644 include/plasp/pddl/expressions/Either.h create mode 100644 include/plasp/pddl/expressions/PredicateDeclaration.h create mode 100644 include/plasp/pddl/expressions/PrimitiveType.h create mode 100644 include/plasp/pddl/expressions/Reference.h rename include/plasp/pddl/{ => expressions}/Type.h (50%) delete mode 100644 src/plasp/pddl/EitherType.cpp rename src/plasp/pddl/{ => expressions}/Constant.cpp (67%) rename src/plasp/pddl/{Type.cpp => expressions/Either.cpp} (55%) rename src/plasp/pddl/{Predicate.cpp => expressions/PredicateDeclaration.cpp} (57%) rename src/plasp/pddl/{ => expressions}/PrimitiveType.cpp (66%) create mode 100644 src/plasp/pddl/expressions/Type.cpp diff --git a/include/plasp/pddl/Constant.h b/include/plasp/pddl/Constant.h deleted file mode 100644 index 97a1999..0000000 --- a/include/plasp/pddl/Constant.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef __PLASP__PDDL__CONSTANT_H -#define __PLASP__PDDL__CONSTANT_H - -#include -#include - -#include -#include - -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 diff --git a/include/plasp/pddl/Context.h b/include/plasp/pddl/Context.h index 752157e..2aefca7 100644 --- a/include/plasp/pddl/Context.h +++ b/include/plasp/pddl/Context.h @@ -6,9 +6,10 @@ #include #include -#include -#include -#include +#include +#include +#include +#include namespace plasp { @@ -24,16 +25,14 @@ namespace pddl class Context { public: - std::vector> primitiveTypes; - std::unordered_map primitiveTypesHashMap; + expressions::PrimitiveTypes primitiveTypes; + //std::unordered_map primitiveTypesHashMap; - std::vector> eitherTypes; + expressions::Constants constants; + //std::unordered_map constantsHashMap; - std::vector> constants; - std::unordered_map constantsHashMap; - - std::vector> predicates; - std::unordered_map predicatesHashMap; + expressions::PredicateDeclarations predicateDeclarations; + //std::unordered_map predicatesHashMap; std::vector> actions; }; diff --git a/include/plasp/pddl/Domain.h b/include/plasp/pddl/Domain.h index 087c674..2167d49 100644 --- a/include/plasp/pddl/Domain.h +++ b/include/plasp/pddl/Domain.h @@ -4,9 +4,8 @@ #include #include -#include +#include #include -#include #include namespace plasp @@ -28,9 +27,9 @@ class Domain public: const std::string &name() const; const Requirements &requirements() const; - const std::vector> &types() const; - const std::vector> &constants() const; - const std::vector> &predicates() const; + const expressions::PrimitiveTypes &types() const; + const expressions::Constants &constants() const; + const expressions::PredicateDeclarations &predicates() const; const std::vector> &actions() const; private: diff --git a/include/plasp/pddl/EitherType.h b/include/plasp/pddl/EitherType.h deleted file mode 100644 index 6118607..0000000 --- a/include/plasp/pddl/EitherType.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef __PLASP__PDDL__EITHER_TYPE_H -#define __PLASP__PDDL__EITHER_TYPE_H - -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// EitherType -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Context; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class EitherType -{ - public: - static EitherType &parse(utils::Parser &parser, Context &context); - - public: - const std::vector &allowedTypes() const; - - private: - EitherType() = default; - - std::vector m_allowedTypes; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h index 1e48c4d..1e44afe 100644 --- a/include/plasp/pddl/Expression.h +++ b/include/plasp/pddl/Expression.h @@ -30,6 +30,13 @@ namespace expressions class And; using AndPointer = std::unique_ptr; +class Constant; +using ConstantPointer = std::unique_ptr; +using Constants = std::vector; + +class Either; +using EitherPointer = std::unique_ptr; + class Not; using NotPointer = std::unique_ptr; @@ -38,6 +45,20 @@ using OrPointer = std::unique_ptr; class Predicate; using PredicatePointer = std::unique_ptr; +using Predicates = std::vector; + +class PredicateDeclaration; +using PredicateDeclarationPointer = std::unique_ptr; +using PredicateDeclarations = std::vector; + +class PrimitiveType; +using PrimitiveTypePointer = std::unique_ptr; +using PrimitiveTypes = std::vector; + +template +class Reference; +template +using ReferencePointer = std::unique_ptr>; class Variable; using VariablePointer = std::unique_ptr; diff --git a/include/plasp/pddl/Predicate.h b/include/plasp/pddl/Predicate.h deleted file mode 100644 index f6038b7..0000000 --- a/include/plasp/pddl/Predicate.h +++ /dev/null @@ -1,81 +0,0 @@ -#ifndef __PLASP__PDDL__PREDICATE_H -#define __PLASP__PDDL__PREDICATE_H - -#include - -#include -#include - -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 - { - 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 diff --git a/include/plasp/pddl/PrimitiveType.h b/include/plasp/pddl/PrimitiveType.h deleted file mode 100644 index 13a4040..0000000 --- a/include/plasp/pddl/PrimitiveType.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __PLASP__PDDL__PRIMITIVE_TYPE_H -#define __PLASP__PDDL__PRIMITIVE_TYPE_H - -#include -#include - -#include - -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 &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 m_parentTypes; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Constant.h b/include/plasp/pddl/expressions/Constant.h new file mode 100644 index 0000000..d7f9658 --- /dev/null +++ b/include/plasp/pddl/expressions/Constant.h @@ -0,0 +1,87 @@ +#ifndef __PLASP__PDDL__EXPRESSION__CONSTANT_H +#define __PLASP__PDDL__EXPRESSION__CONSTANT_H + +#include +#include +#include +#include + +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 + 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 +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 diff --git a/include/plasp/pddl/expressions/Either.h b/include/plasp/pddl/expressions/Either.h new file mode 100644 index 0000000..7238ec7 --- /dev/null +++ b/include/plasp/pddl/expressions/Either.h @@ -0,0 +1,55 @@ +#ifndef __PLASP__PDDL__EXPRESSION__EITHER_H +#define __PLASP__PDDL__EXPRESSION__EITHER_H + +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Either +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Either: public NAry +{ + public: + template + static EitherPointer parse(utils::Parser &parser, Context &context, + const Variables ¶meters, ExpressionParser parseExpression); + + public: + void accept(ExpressionVisitor &expressionVisitor) const override; + + private: + Either() = default; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +EitherPointer Either::parse(utils::Parser &parser, Context &context, + const Variables ¶meters, ExpressionParser parseExpression) +{ + auto expression = std::make_unique(Either()); + + expression->NAry::parse(parser, context, parameters, parseExpression); + + if (expression->arguments().empty()) + throw ConsistencyException("\"and\" expressions should not be empty"); + + return expression; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} + +#endif diff --git a/include/plasp/pddl/expressions/NAry.h b/include/plasp/pddl/expressions/NAry.h index 86413dd..be33193 100644 --- a/include/plasp/pddl/expressions/NAry.h +++ b/include/plasp/pddl/expressions/NAry.h @@ -1,10 +1,6 @@ #ifndef __PLASP__PDDL__EXPRESSION__N_ARY_H #define __PLASP__PDDL__EXPRESSION__N_ARY_H -#include -#include -#include - #include #include #include diff --git a/include/plasp/pddl/expressions/Not.h b/include/plasp/pddl/expressions/Not.h index 86f814a..03f6594 100644 --- a/include/plasp/pddl/expressions/Not.h +++ b/include/plasp/pddl/expressions/Not.h @@ -2,7 +2,6 @@ #define __PLASP__PDDL__EXPRESSION__NOT_H #include -#include namespace plasp { diff --git a/include/plasp/pddl/expressions/Predicate.h b/include/plasp/pddl/expressions/Predicate.h index f5da265..02436bb 100644 --- a/include/plasp/pddl/expressions/Predicate.h +++ b/include/plasp/pddl/expressions/Predicate.h @@ -2,8 +2,6 @@ #define __PLASP__PDDL__EXPRESSION__PREDICATE_H #include -#include -#include namespace plasp { @@ -27,13 +25,20 @@ class Predicate: public Expression public: void accept(ExpressionVisitor &expressionVisitor) const override; - const std::vector &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 m_arguments; + Expressions m_arguments; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/expressions/PredicateDeclaration.h b/include/plasp/pddl/expressions/PredicateDeclaration.h new file mode 100644 index 0000000..cf664f0 --- /dev/null +++ b/include/plasp/pddl/expressions/PredicateDeclaration.h @@ -0,0 +1,49 @@ +#ifndef __PLASP__PDDL__EXPRESSION__PREDICATE_DECLARATION_H +#define __PLASP__PDDL__EXPRESSION__PREDICATE_DECLARATION_H + +#include + +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 diff --git a/include/plasp/pddl/expressions/PrimitiveType.h b/include/plasp/pddl/expressions/PrimitiveType.h new file mode 100644 index 0000000..5a7bc43 --- /dev/null +++ b/include/plasp/pddl/expressions/PrimitiveType.h @@ -0,0 +1,92 @@ +#ifndef __PLASP__PDDL__EXPRESSION__PRIMITIVE_TYPE_H +#define __PLASP__PDDL__EXPRESSION__PRIMITIVE_TYPE_H + +#include +#include +#include +#include + +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 + 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 &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 m_parentTypes; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +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 diff --git a/include/plasp/pddl/expressions/Reference.h b/include/plasp/pddl/expressions/Reference.h new file mode 100644 index 0000000..ab09b5a --- /dev/null +++ b/include/plasp/pddl/expressions/Reference.h @@ -0,0 +1,76 @@ +#ifndef __PLASP__PDDL__EXPRESSION__REFERENCE_H +#define __PLASP__PDDL__EXPRESSION__REFERENCE_H + +#include +#include +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Reference +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +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 +Reference::Reference() +: m_value{nullptr} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +Reference::Reference(const Type *value) +: m_value{value} +{ + BOOST_ASSERT(m_value != nullptr); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +void Reference::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const +{ + expressionVisitor.visit(*this); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +const Type *Reference::value() const +{ + return m_value; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} + +#endif diff --git a/include/plasp/pddl/Type.h b/include/plasp/pddl/expressions/Type.h similarity index 50% rename from include/plasp/pddl/Type.h rename to include/plasp/pddl/expressions/Type.h index 6f43499..a0ac7d8 100644 --- a/include/plasp/pddl/Type.h +++ b/include/plasp/pddl/expressions/Type.h @@ -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 -#include -#include +#include +#include #include namespace plasp { namespace pddl { +namespace expressions +{ //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -18,16 +20,12 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -class Context; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -using TypePtr = boost::variant; - -TypePtr parseType(utils::Parser &parser, Context &context); +ExpressionPointer parseExistingPrimitiveType(utils::Parser &parser, Context &context, const Variables ¶meters); +//ExpressionPointer parseExistingType(utils::Parser &parser, Context &context, const Variables ¶meters); //////////////////////////////////////////////////////////////////////////////////////////////////// +} } } diff --git a/include/plasp/pddl/expressions/Variable.h b/include/plasp/pddl/expressions/Variable.h index ba38fb9..931f86c 100644 --- a/include/plasp/pddl/expressions/Variable.h +++ b/include/plasp/pddl/expressions/Variable.h @@ -2,7 +2,6 @@ #define __PLASP__PDDL__EXPRESSION__VARIABLE_H #include -#include 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 ¶meters); + + 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; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/Action.cpp b/src/plasp/pddl/Action.cpp index c011298..d353234 100644 --- a/src/plasp/pddl/Action.cpp +++ b/src/plasp/pddl/Action.cpp @@ -4,6 +4,7 @@ #include #include +#include #include namespace plasp diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index c51379f..d15a919 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -4,6 +4,10 @@ #include #include +#include +#include +#include +#include #include namespace plasp @@ -67,23 +71,23 @@ const Requirements &Domain::requirements() const //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector> &Domain::types() const +const expressions::PrimitiveTypes &Domain::types() const { return m_context.primitiveTypes; } //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector> &Domain::constants() const +const expressions::Constants &Domain::constants() const { return m_context.constants; } //////////////////////////////////////////////////////////////////////////////////////////////////// -const std::vector> &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 } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/EitherType.cpp b/src/plasp/pddl/EitherType.cpp deleted file mode 100644 index 42d2d34..0000000 --- a/src/plasp/pddl/EitherType.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include - -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// EitherType -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -EitherType &EitherType::parse(utils::Parser &parser, Context &context) -{ - parser.skipWhiteSpace(); - - auto eitherType = std::make_unique(EitherType()); - - parser.expect("(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(")"); - - return *context.eitherTypes.back(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::vector &EitherType::allowedTypes() const -{ - return m_allowedTypes; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp index 234cdd1..621fa28 100644 --- a/src/plasp/pddl/Expression.cpp +++ b/src/plasp/pddl/Expression.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include 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); diff --git a/src/plasp/pddl/Constant.cpp b/src/plasp/pddl/expressions/Constant.cpp similarity index 67% rename from src/plasp/pddl/Constant.cpp rename to src/plasp/pddl/expressions/Constant.cpp index b8e2a28..7d1ccdc 100644 --- a/src/plasp/pddl/Constant.cpp +++ b/src/plasp/pddl/expressions/Constant.cpp @@ -1,14 +1,17 @@ -#include +#include #include #include -#include +#include +#include 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()); - // 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(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 } } +} diff --git a/src/plasp/pddl/Type.cpp b/src/plasp/pddl/expressions/Either.cpp similarity index 55% rename from src/plasp/pddl/Type.cpp rename to src/plasp/pddl/expressions/Either.cpp index fe1685c..158481d 100644 --- a/src/plasp/pddl/Type.cpp +++ b/src/plasp/pddl/expressions/Either.cpp @@ -1,29 +1,27 @@ -#include +#include -#include +#include 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); } //////////////////////////////////////////////////////////////////////////////////////////////////// } } +} diff --git a/src/plasp/pddl/expressions/Predicate.cpp b/src/plasp/pddl/expressions/Predicate.cpp index a57608a..d3c4368 100644 --- a/src/plasp/pddl/expressions/Predicate.cpp +++ b/src/plasp/pddl/expressions/Predicate.cpp @@ -1,6 +1,11 @@ #include +#include #include +#include +#include +#include +#include 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 ¶meters) { - auto expression = std::make_unique(Predicate()); + auto predicate = std::make_unique(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>(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>(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 &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; } diff --git a/src/plasp/pddl/Predicate.cpp b/src/plasp/pddl/expressions/PredicateDeclaration.cpp similarity index 57% rename from src/plasp/pddl/Predicate.cpp rename to src/plasp/pddl/expressions/PredicateDeclaration.cpp index 0bd7e81..2beae85 100644 --- a/src/plasp/pddl/Predicate.cpp +++ b/src/plasp/pddl/expressions/PredicateDeclaration.cpp @@ -1,36 +1,39 @@ -#include - -#include +#include #include +#include #include +#include +#include +#include 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("("); - const auto predicateName = parser.parseIdentifier(isIdentifier); + auto predicate = std::make_unique(PredicateDeclaration()); - auto predicate = std::make_unique(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(")"); - 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 } } +} diff --git a/src/plasp/pddl/PrimitiveType.cpp b/src/plasp/pddl/expressions/PrimitiveType.cpp similarity index 66% rename from src/plasp/pddl/PrimitiveType.cpp rename to src/plasp/pddl/expressions/PrimitiveType.cpp index 92f8982..7f019ec 100644 --- a/src/plasp/pddl/PrimitiveType.cpp +++ b/src/plasp/pddl/expressions/PrimitiveType.cpp @@ -1,14 +1,16 @@ -#include +#include #include #include -#include +#include 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()); + 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(typeName))); + parser.skipWhiteSpace(); - auto &type = *context.primitiveTypes.back(); + auto type = std::make_unique(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 &PrimitiveType::parentTypes() const } } +} diff --git a/src/plasp/pddl/expressions/Type.cpp b/src/plasp/pddl/expressions/Type.cpp new file mode 100644 index 0000000..2aed578 --- /dev/null +++ b/src/plasp/pddl/expressions/Type.cpp @@ -0,0 +1,46 @@ +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Type +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +ExpressionPointer parseExistingPrimitiveType(utils::Parser &parser, Context &context, const Variables ¶meters) +{ + auto reference = std::make_unique>(PrimitiveType::parseExisting(parser, context.primitiveTypes)); + + return reference; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*ExpressionPointer parseExistingType(utils::Parser &parser, Context &context, const Variables ¶meters) +{ + 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>(PrimitiveType::parseExisting(parser, context)); + + return type; +}*/ + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} diff --git a/src/plasp/pddl/expressions/Variable.cpp b/src/plasp/pddl/expressions/Variable.cpp index 1a725d4..f9c3e3b 100644 --- a/src/plasp/pddl/expressions/Variable.cpp +++ b/src/plasp/pddl/expressions/Variable.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include namespace plasp @@ -36,6 +38,8 @@ VariablePointer Variable::parseDeclaration(utils::Parser &parser) auto variable = std::make_unique(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 ¶meters) { // Parse and store variable itself - variables.emplace_back(parseDeclaration(parser)); + parameters.emplace_back(parseDeclaration(parser)); + + auto ¶meter = 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 ¶meter) + { + 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; } diff --git a/tests/TestPDDLParser.cpp b/tests/TestPDDLParser.cpp index b55dde6..365512c 100644 --- a/tests/TestPDDLParser.cpp +++ b/tests/TestPDDLParser.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -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(on.arguments()[0]->type()); + const auto *onArgument0Type = dynamic_cast(on.arguments()[0]->type()); ASSERT_EQ(onArgument0Type, &block); ASSERT_EQ(on.arguments()[1]->name(), "y"); - const auto onArgument1Type = boost::get(on.arguments()[1]->type()); + const auto onArgument1Type = dynamic_cast(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(on.arguments()[0]->type()); + const auto onArgument0Type = dynamic_cast(on.arguments()[0]->type()); ASSERT_EQ(onArgument0Type, &crate); ASSERT_EQ(on.arguments()[1]->name(), "s"); - const auto onArgument1Type = boost::get(on.arguments()[1]->type()); + const auto onArgument1Type = dynamic_cast(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(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(in.arguments()[0]->type()); + ASSERT_EQ(inArgument0Type->arguments().size(), 2u); + ASSERT_EQ(dynamic_cast(inArgument0Type->arguments()[0].get()), &storearea); + ASSERT_EQ(dynamic_cast(inArgument0Type->arguments()[1].get()), &crate); } catch (const std::exception &e) {