From e345fd60fd7e57d64e726e31878bb3ee216e6c9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 20 Jun 2017 01:53:55 +0200 Subject: [PATCH] Reimplemented plasp with PDDL parsing library. --- app/CMakeLists.txt | 2 + app/main.cpp | 18 +- include/plasp/pddl/Action.h | 51 -- include/plasp/pddl/ConsistencyException.h | 57 -- include/plasp/pddl/Context.h | 59 -- include/plasp/pddl/Description.h | 60 -- include/plasp/pddl/Domain.h | 101 ---- include/plasp/pddl/Expression.h | 237 -------- include/plasp/pddl/ExpressionContext.h | 39 -- include/plasp/pddl/IO.h | 45 -- include/plasp/pddl/InitialState.h | 35 -- include/plasp/pddl/Problem.h | 87 --- include/plasp/pddl/Requirement.h | 72 --- include/plasp/pddl/Tokenizer.h | 54 -- include/plasp/pddl/TranslatorASP.h | 12 +- include/plasp/pddl/VariableStack.h | 34 -- include/plasp/pddl/expressions/And.h | 36 -- include/plasp/pddl/expressions/At.h | 112 ---- include/plasp/pddl/expressions/Binary.h | 195 ------- include/plasp/pddl/expressions/Constant.h | 64 --- .../plasp/pddl/expressions/DerivedPredicate.h | 59 -- include/plasp/pddl/expressions/Dummy.h | 39 -- include/plasp/pddl/expressions/Either.h | 33 -- include/plasp/pddl/expressions/Exists.h | 33 -- include/plasp/pddl/expressions/ForAll.h | 36 -- include/plasp/pddl/expressions/Imply.h | 36 -- include/plasp/pddl/expressions/NAry.h | 239 -------- include/plasp/pddl/expressions/Not.h | 85 --- include/plasp/pddl/expressions/Or.h | 36 -- include/plasp/pddl/expressions/Predicate.h | 56 -- .../pddl/expressions/PredicateDeclaration.h | 53 -- .../plasp/pddl/expressions/PrimitiveType.h | 55 -- include/plasp/pddl/expressions/Quantified.h | 250 -------- include/plasp/pddl/expressions/Type.h | 28 - include/plasp/pddl/expressions/Unsupported.h | 42 -- include/plasp/pddl/expressions/Variable.h | 63 --- include/plasp/pddl/expressions/When.h | 67 --- include/plasp/pddl/translation/Effect.h | 79 +++ include/plasp/pddl/translation/Goal.h | 72 +++ include/plasp/pddl/translation/Precondition.h | 99 ++-- include/plasp/pddl/translation/Predicate.h | 111 ++-- include/plasp/pddl/translation/Primitives.h | 129 +++-- include/plasp/pddl/translation/Variables.h | 31 +- src/CMakeLists.txt | 3 + src/plasp/pddl/Action.cpp | 118 ---- src/plasp/pddl/Description.cpp | 220 -------- src/plasp/pddl/Domain.cpp | 451 --------------- src/plasp/pddl/Expression.cpp | 336 ----------- src/plasp/pddl/ExpressionContext.cpp | 54 -- src/plasp/pddl/InitialState.cpp | 90 --- src/plasp/pddl/Problem.cpp | 408 -------------- src/plasp/pddl/Requirement.cpp | 134 ----- src/plasp/pddl/TranslatorASP.cpp | 289 +++------- src/plasp/pddl/VariableStack.cpp | 62 -- src/plasp/pddl/expressions/And.cpp | 49 -- src/plasp/pddl/expressions/At.cpp | 99 ---- src/plasp/pddl/expressions/Constant.cpp | 253 --------- .../pddl/expressions/DerivedPredicate.cpp | 133 ----- src/plasp/pddl/expressions/Dummy.cpp | 34 -- src/plasp/pddl/expressions/Either.cpp | 22 - src/plasp/pddl/expressions/Exists.cpp | 25 - src/plasp/pddl/expressions/ForAll.cpp | 42 -- src/plasp/pddl/expressions/Imply.cpp | 46 -- src/plasp/pddl/expressions/Not.cpp | 134 ----- src/plasp/pddl/expressions/Or.cpp | 61 -- src/plasp/pddl/expressions/Predicate.cpp | 199 ------- .../pddl/expressions/PredicateDeclaration.cpp | 103 ---- src/plasp/pddl/expressions/PrimitiveType.cpp | 184 ------ src/plasp/pddl/expressions/Type.cpp | 30 - src/plasp/pddl/expressions/Unsupported.cpp | 54 -- src/plasp/pddl/expressions/Variable.cpp | 210 ------- src/plasp/pddl/expressions/When.cpp | 22 - tests/CMakeLists.txt | 2 + tests/TestPDDLNormalization.cpp | 15 +- tests/TestPDDLParser.cpp | 532 ------------------ tests/TestPDDLTranslation.cpp | 24 +- 76 files changed, 459 insertions(+), 7080 deletions(-) delete mode 100644 include/plasp/pddl/Action.h delete mode 100644 include/plasp/pddl/ConsistencyException.h delete mode 100644 include/plasp/pddl/Context.h delete mode 100644 include/plasp/pddl/Description.h delete mode 100644 include/plasp/pddl/Domain.h delete mode 100644 include/plasp/pddl/Expression.h delete mode 100644 include/plasp/pddl/ExpressionContext.h delete mode 100644 include/plasp/pddl/IO.h delete mode 100644 include/plasp/pddl/InitialState.h delete mode 100644 include/plasp/pddl/Problem.h delete mode 100644 include/plasp/pddl/Requirement.h delete mode 100644 include/plasp/pddl/Tokenizer.h delete mode 100644 include/plasp/pddl/VariableStack.h delete mode 100644 include/plasp/pddl/expressions/And.h delete mode 100644 include/plasp/pddl/expressions/At.h delete mode 100644 include/plasp/pddl/expressions/Binary.h delete mode 100644 include/plasp/pddl/expressions/Constant.h delete mode 100644 include/plasp/pddl/expressions/DerivedPredicate.h delete mode 100644 include/plasp/pddl/expressions/Dummy.h delete mode 100644 include/plasp/pddl/expressions/Either.h delete mode 100644 include/plasp/pddl/expressions/Exists.h delete mode 100644 include/plasp/pddl/expressions/ForAll.h delete mode 100644 include/plasp/pddl/expressions/Imply.h delete mode 100644 include/plasp/pddl/expressions/NAry.h delete mode 100644 include/plasp/pddl/expressions/Not.h delete mode 100644 include/plasp/pddl/expressions/Or.h delete mode 100644 include/plasp/pddl/expressions/Predicate.h delete mode 100644 include/plasp/pddl/expressions/PredicateDeclaration.h delete mode 100644 include/plasp/pddl/expressions/PrimitiveType.h delete mode 100644 include/plasp/pddl/expressions/Quantified.h delete mode 100644 include/plasp/pddl/expressions/Type.h delete mode 100644 include/plasp/pddl/expressions/Unsupported.h delete mode 100644 include/plasp/pddl/expressions/Variable.h delete mode 100644 include/plasp/pddl/expressions/When.h create mode 100644 include/plasp/pddl/translation/Effect.h create mode 100644 include/plasp/pddl/translation/Goal.h delete mode 100644 src/plasp/pddl/Action.cpp delete mode 100644 src/plasp/pddl/Description.cpp delete mode 100644 src/plasp/pddl/Domain.cpp delete mode 100644 src/plasp/pddl/Expression.cpp delete mode 100644 src/plasp/pddl/ExpressionContext.cpp delete mode 100644 src/plasp/pddl/InitialState.cpp delete mode 100644 src/plasp/pddl/Problem.cpp delete mode 100644 src/plasp/pddl/Requirement.cpp delete mode 100644 src/plasp/pddl/VariableStack.cpp delete mode 100644 src/plasp/pddl/expressions/And.cpp delete mode 100644 src/plasp/pddl/expressions/At.cpp delete mode 100644 src/plasp/pddl/expressions/Constant.cpp delete mode 100644 src/plasp/pddl/expressions/DerivedPredicate.cpp delete mode 100644 src/plasp/pddl/expressions/Dummy.cpp delete mode 100644 src/plasp/pddl/expressions/Either.cpp delete mode 100644 src/plasp/pddl/expressions/Exists.cpp delete mode 100644 src/plasp/pddl/expressions/ForAll.cpp delete mode 100644 src/plasp/pddl/expressions/Imply.cpp delete mode 100644 src/plasp/pddl/expressions/Not.cpp delete mode 100644 src/plasp/pddl/expressions/Or.cpp delete mode 100644 src/plasp/pddl/expressions/Predicate.cpp delete mode 100644 src/plasp/pddl/expressions/PredicateDeclaration.cpp delete mode 100644 src/plasp/pddl/expressions/PrimitiveType.cpp delete mode 100644 src/plasp/pddl/expressions/Type.cpp delete mode 100644 src/plasp/pddl/expressions/Unsupported.cpp delete mode 100644 src/plasp/pddl/expressions/Variable.cpp delete mode 100644 src/plasp/pddl/expressions/When.cpp delete mode 100644 tests/TestPDDLParser.cpp diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 701cd32..5cd4bae 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -7,6 +7,8 @@ set(includes ${Boost_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/lib/tokenize/include + ${PROJECT_SOURCE_DIR}/lib/variant/include + ${PROJECT_SOURCE_DIR}/lib/pddlparse/include ) set(sources diff --git a/app/main.cpp b/app/main.cpp index c3b8973..c10ee6d 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -4,10 +4,14 @@ #include +#include +#include + #include #include +#include +#include #include -#include #include #include #include @@ -148,9 +152,15 @@ int main(int argc, char **argv) if (language == plasp::Language::Type::PDDL) { - auto context = plasp::pddl::Context(std::move(tokenizer), logger); - auto description = plasp::pddl::Description::fromContext(context); - const auto translator = plasp::pddl::TranslatorASP(description, logger.outputStream()); + const auto logWarning = + [&](const auto &location, const auto &warning) + { + logger.log(plasp::output::Priority::Warning, location, warning); + }; + + auto context = pddl::Context(std::move(tokenizer), logWarning); + auto description = pddl::parseDescription(context); + const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream()); translator.translate(); } else if (language == plasp::Language::Type::SAS) diff --git a/include/plasp/pddl/Action.h b/include/plasp/pddl/Action.h deleted file mode 100644 index 1005bd6..0000000 --- a/include/plasp/pddl/Action.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef __PLASP__PDDL__ACTION_H -#define __PLASP__PDDL__ACTION_H - -#include - -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Action -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Action -{ - public: - static void parseDeclaration(Context &context, Domain &domain); - - public: - const std::string &name() const; - - const expressions::Variables ¶meters() const; - const Expression *precondition() const; - const Expression *effect() const; - - void normalize(expressions::DerivedPredicates &derivedPredicates); - - private: - Action() = default; - - std::string m_name; - - expressions::Variables m_parameters; - ExpressionPointer m_precondition; - ExpressionPointer m_effect; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/ConsistencyException.h b/include/plasp/pddl/ConsistencyException.h deleted file mode 100644 index 31941a5..0000000 --- a/include/plasp/pddl/ConsistencyException.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef __PLASP__PDDL__CONSISTENCY_EXCEPTION_H -#define __PLASP__PDDL__CONSISTENCY_EXCEPTION_H - -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// ConsistencyException -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class ConsistencyException: public std::exception -{ - public: - explicit ConsistencyException() - : ConsistencyException("unspecified consistency error") - { - } - - explicit ConsistencyException(const char *message) - : ConsistencyException(static_cast(message)) - { - } - - explicit ConsistencyException(const std::string &message) - : m_message{message} - { - } - - ~ConsistencyException() throw() - { - } - - const char *what() const throw() - { - if (m_message.empty()) - return "unspecified consistency error"; - - return m_message.c_str(); - } - - private: - std::string m_message; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Context.h b/include/plasp/pddl/Context.h deleted file mode 100644 index f7ac062..0000000 --- a/include/plasp/pddl/Context.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __PLASP__PDDL__CONTEXT_H -#define __PLASP__PDDL__CONTEXT_H - -#include -#include -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Context -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Context -{ - public: - Context() = default; - ~Context() = default; - - explicit Context(Tokenizer &&otherTokenizer, output::Logger &otherLogger) - : tokenizer{std::move(otherTokenizer)}, - logger(otherLogger) - { - } - - Context(const Context &other) = delete; - Context &operator=(const Context &other) = delete; - - Context(Context &&other) - : tokenizer(std::move(other.tokenizer)), - logger(other.logger) - { - } - - Context &operator=(Context &&other) = delete; - - constexpr static const char *auxiliaryPrefix() - { - return "__plasp_"; - } - - Tokenizer tokenizer; - output::Logger &logger; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Description.h b/include/plasp/pddl/Description.h deleted file mode 100644 index dd5dce2..0000000 --- a/include/plasp/pddl/Description.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef __PLASP__PDDL__DESCRIPTION_H -#define __PLASP__PDDL__DESCRIPTION_H - -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Description -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Description -{ - public: - static Description fromContext(Context &context); - static Description fromStream(std::istream &istream, Context &context); - static Description fromFile(const std::string &path, Context &context); - static Description fromFiles(const std::vector &paths, Context &context); - - public: - Context &context(); - const Context &context() const; - - const Domain &domain() const; - - bool containsProblem() const; - const Problem &problem() const; - - void normalize(); - - private: - Description(Context &context); - - void parse(); - void findSections(); - - void checkConsistency(); - - Context &m_context; - - tokenize::StreamPosition m_domainPosition; - std::unique_ptr m_domain; - tokenize::StreamPosition m_problemPosition; - std::unique_ptr m_problem; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Domain.h b/include/plasp/pddl/Domain.h deleted file mode 100644 index 7b76957..0000000 --- a/include/plasp/pddl/Domain.h +++ /dev/null @@ -1,101 +0,0 @@ -#ifndef __PLASP__PDDL__DOMAIN_H -#define __PLASP__PDDL__DOMAIN_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Domain -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Domain -{ - public: - Domain(Context &context); - - public: - void findSections(); - void parse(); - - void setName(std::string name); - const std::string &name() const; - - const Requirements &requirements() const; - bool hasRequirement(Requirement::Type requirementType) const; - void checkRequirement(Requirement::Type requirementType); - - expressions::PrimitiveTypes &types(); - const expressions::PrimitiveTypes &types() const; - - expressions::Constants &constants(); - const expressions::Constants &constants() const; - - expressions::PredicateDeclarations &predicates(); - const expressions::PredicateDeclarations &predicates() const; - - std::vector> &actions(); - const std::vector> &actions() const; - - expressions::DerivedPredicates &derivedPredicates(); - const expressions::DerivedPredicates &derivedPredicates() const; - - void checkConsistency(); - - void normalize(); - - private: - void parseSection(); - - void parseRequirementSection(); - void computeDerivedRequirements(); - - void parseTypeSection(); - - void parseConstantSection(); - - void parsePredicateSection(); - - void parseActionSection(); - - Context &m_context; - - std::string m_name; - - tokenize::StreamPosition m_requirementsPosition; - Requirements m_requirements; - - tokenize::StreamPosition m_typesPosition; - expressions::PrimitiveTypes m_types; - - tokenize::StreamPosition m_constantsPosition; - expressions::Constants m_constants; - - tokenize::StreamPosition m_predicatesPosition; - expressions::PredicateDeclarations m_predicates; - - std::vector m_actionPositions; - std::vector> m_actions; - - expressions::DerivedPredicates m_derivedPredicates; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h deleted file mode 100644 index 7536c12..0000000 --- a/include/plasp/pddl/Expression.h +++ /dev/null @@ -1,237 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSION_H -#define __PLASP__PDDL__EXPRESSION_H - -#include -#include - -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Expression -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Context; -class Domain; -class ExpressionContext; -class ExpressionVisitor; -class Problem; - -class Expression; -using ExpressionPointer = boost::intrusive_ptr; -using Expressions = std::vector; - -namespace expressions -{ -class And; -using AndPointer = boost::intrusive_ptr; - -class At; -using AtPointer = boost::intrusive_ptr; - -class Constant; -using ConstantPointer = boost::intrusive_ptr; -using Constants = std::vector; - -class DerivedPredicate; -using DerivedPredicatePointer = boost::intrusive_ptr; -using DerivedPredicates = std::vector; - -class Dummy; -using DummyPointer = boost::intrusive_ptr; - -class Either; -using EitherPointer = boost::intrusive_ptr; - -class Exists; -using ExistsPointer = boost::intrusive_ptr; - -class ForAll; -using ForAllPointer = boost::intrusive_ptr; - -class Imply; -using ImplyPointer = boost::intrusive_ptr; - -class Not; -using NotPointer = boost::intrusive_ptr; - -class Or; -using OrPointer = boost::intrusive_ptr; - -class Predicate; -using PredicatePointer = boost::intrusive_ptr; -using Predicates = std::vector; - -class PredicateDeclaration; -using PredicateDeclarationPointer = boost::intrusive_ptr; -using PredicateDeclarations = std::vector; - -class PrimitiveType; -using PrimitiveTypePointer = boost::intrusive_ptr; -using PrimitiveTypes = std::vector; - -class Quantified; -using QuantifiedPointer = boost::intrusive_ptr; - -template -class Reference; -template -using ReferencePointer = boost::intrusive_ptr>; - -class Unsupported; -using UnsupportedPointer = boost::intrusive_ptr; - -class Variable; -using VariablePointer = boost::intrusive_ptr; -using Variables = std::vector; - -class When; -using WhenPointer = boost::intrusive_ptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Expression -{ - public: - enum class Type - { - And, - At, - Binary, - Constant, - DerivedPredicate, - Dummy, - Either, - Exists, - ForAll, - Imply, - Not, - Or, - PredicateDeclaration, - Predicate, - PrimitiveType, - Reference, - Unsupported, - Variable, - When, - }; - - public: - virtual ~Expression() = default; - - virtual Type expressionType() const = 0; - - template - bool is() const; - template - T &as(); - template - const T &as() const; - - virtual ExpressionPointer copy(); - - // Transform into a normal form as used for the translation to ASP - ExpressionPointer normalized(); - // Reduce the set of used expressions (eliminates implications, for instance) - virtual ExpressionPointer reduced(); - // Transform such that only existential (and no universal) quantifiers are used - virtual ExpressionPointer existentiallyQuantified(); - // Simplify the expression equivalently - virtual ExpressionPointer simplified(); - // Decompose expression into derived predicates (eliminate recursively nested expressions) - virtual ExpressionPointer decomposed(expressions::DerivedPredicates &derivedPredicates); - // Negate the expression - ExpressionPointer negated(); - - virtual void collectParameters(std::set ¶meters); - - virtual void print(std::ostream &ostream) const = 0; - - private: - friend void intrusive_ptr_add_ref(Expression *expression); - friend void intrusive_ptr_release(Expression *expression); - - size_t m_referenceCount = 0; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -bool Expression::is() const -{ - return expressionType() == T::ExpressionType; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -T &Expression::as() -{ - return dynamic_cast(*this); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const T &Expression::as() const -{ - return dynamic_cast(*this); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void intrusive_ptr_add_ref(Expression *expression) -{ - expression->m_referenceCount++; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void intrusive_ptr_release(Expression *expression) -{ - if (--expression->m_referenceCount == 0) - delete expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -class ExpressionCRTP: public Expression -{ - public: - Type expressionType() const override final - { - return Derived::ExpressionType; - } -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseLiteral(Context &context, ExpressionContext &expressionContext); -ExpressionPointer parseAtomicFormula(Context &context, ExpressionContext &expressionContext); - -ExpressionPointer parsePreconditionExpression(Context &context, - ExpressionContext &expressionContext); -ExpressionPointer parseExpression(Context &context, ExpressionContext &expressionContext); - -ExpressionPointer parseEffectExpression(Context &context, - ExpressionContext &expressionContext); - -ExpressionPointer parseConditionalEffectExpression(Context &context, - ExpressionContext &expressionContext); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/ExpressionContext.h b/include/plasp/pddl/ExpressionContext.h deleted file mode 100644 index 983baee..0000000 --- a/include/plasp/pddl/ExpressionContext.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSION_CONTEXT_H -#define __PLASP__PDDL__EXPRESSION_CONTEXT_H - -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// ExpressionContext -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class ExpressionContext -{ - public: - ExpressionContext(Domain &domain); - ExpressionContext(Domain &domain, Problem *problem); - - bool hasRequirement(Requirement::Type requirementType) const; - void checkRequirement(Requirement::Type requirementType) const; - - Domain &domain; - Problem *problem; - - VariableStack variables; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/IO.h b/include/plasp/pddl/IO.h deleted file mode 100644 index a14691d..0000000 --- a/include/plasp/pddl/IO.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef __PLASP__PDDL__IO_H -#define __PLASP__PDDL__IO_H - -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// IO -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void skipSection(Tokenizer &tokenizer) -{ - size_t openParentheses = 1; - - while (true) - { - const auto character = tokenizer.currentCharacter(); - tokenizer.advance(); - - if (character == '(') - openParentheses++; - else if (character == ')') - { - openParentheses--; - - if (openParentheses == 0) - return; - } - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/InitialState.h b/include/plasp/pddl/InitialState.h deleted file mode 100644 index ccb1733..0000000 --- a/include/plasp/pddl/InitialState.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__INITIAL_STATE_H -#define __PLASP__PDDL__EXPRESSIONS__INITIAL_STATE_H - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// InitialState -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class InitialState -{ - public: - static std::unique_ptr parseDeclaration(Context &context, - ExpressionContext &expressionContext); - - public: - const Expressions &facts() const; - - private: - Expressions m_facts; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Problem.h b/include/plasp/pddl/Problem.h deleted file mode 100644 index 0fae87b..0000000 --- a/include/plasp/pddl/Problem.h +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef __PLASP__PDDL__PROBLEM_H -#define __PLASP__PDDL__PROBLEM_H - -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Problem -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Problem -{ - public: - Problem(Context &context, Domain &domain); - - void findSections(); - void parse(); - - Domain &domain(); - const Domain &domain() const; - - const std::string &name() const; - - const Requirements &requirements() const; - bool hasRequirement(Requirement::Type requirementType) const; - void checkRequirement(Requirement::Type requirementType); - - expressions::Constants &objects(); - const expressions::Constants &objects() const; - - InitialState &initialState(); - const InitialState &initialState() const; - - const Expression &goal() const; - - void checkConsistency(); - - void normalize(); - - private: - void parseRequirementSection(); - void computeDerivedRequirements(); - - void parseDomainSection(); - - void parseObjectSection(); - - void parseInitialStateSection(); - - void parseGoalSection(); - - Context &m_context; - Domain &m_domain; - - std::string m_name; - - tokenize::StreamPosition m_domainPosition; - - tokenize::StreamPosition m_requirementsPosition; - Requirements m_requirements; - - tokenize::StreamPosition m_objectsPosition; - expressions::Constants m_objects; - - tokenize::StreamPosition m_initialStatePosition; - std::unique_ptr m_initialState; - - tokenize::StreamPosition m_goalPosition; - ExpressionPointer m_goal; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Requirement.h b/include/plasp/pddl/Requirement.h deleted file mode 100644 index b764be6..0000000 --- a/include/plasp/pddl/Requirement.h +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef __PLASP__PDDL__REQUIREMENT_H -#define __PLASP__PDDL__REQUIREMENT_H - -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Requirement -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Requirement; -using Requirements = std::vector; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Requirement -{ - public: - enum class Type - { - STRIPS, - Typing, - NegativePreconditions, - DisjunctivePreconditions, - Equality, - ExistentialPreconditions, - UniversalPreconditions, - QuantifiedPreconditions, - ConditionalEffects, - Fluents, - NumericFluents, - ObjectFluents, - ADL, - DurativeActions, - DurationInequalities, - ContinuousEffects, - DerivedPredicates, - TimedInitialLiterals, - Preferences, - Constraints, - ActionCosts, - GoalUtilities - }; - - static Requirement parse(Context &context); - - public: - Requirement(Type type); - - Type type() const; - - std::string toPDDL() const; - std::string toASP() const; - - private: - Type m_type; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/Tokenizer.h b/include/plasp/pddl/Tokenizer.h deleted file mode 100644 index dd145d0..0000000 --- a/include/plasp/pddl/Tokenizer.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef __PLASP__PDDL__TOKENIZER_H -#define __PLASP__PDDL__TOKENIZER_H - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Tokenizer -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class PDDLTokenizerPolicy -{ - public: - static char transformCharacter(char c) noexcept - { - return std::tolower(c); - } - - static bool isWhiteSpaceCharacter(char c) - { - return std::iswspace(c); - } - - static bool isBlankCharacter(char c) - { - return std::isblank(c); - } - - static bool isIdentifierCharacter(char c) - { - return c != '?' - && c != '(' - && c != ')' - && c != ';' - && std::isgraph(c); - } -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -using Tokenizer = tokenize::Tokenizer; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/TranslatorASP.h b/include/plasp/pddl/TranslatorASP.h index ef71ac3..37abc4e 100644 --- a/include/plasp/pddl/TranslatorASP.h +++ b/include/plasp/pddl/TranslatorASP.h @@ -1,9 +1,10 @@ #ifndef __PLASP__PDDL__TRANSLATOR_ASP_H #define __PLASP__PDDL__TRANSLATOR_ASP_H -#include +#include -#include +#include +#include namespace plasp { @@ -19,7 +20,7 @@ namespace pddl class TranslatorASP { public: - explicit TranslatorASP(Description &description, output::ColorStream &outputStream); + explicit TranslatorASP(const ::pddl::ast::Description &description, output::ColorStream &outputStream); void translate() const; @@ -27,15 +28,14 @@ class TranslatorASP void translateDomain() const; void translateTypes() const; void translatePredicates() const; - void translateDerivedPredicates() const; void translateActions() const; void translateProblem() const; void translateInitialState() const; void translateGoal() const; - void translateConstants(const std::string &heading, const expressions::Constants &constants) const; + void translateConstants(const std::string &heading, const ::pddl::ast::ConstantDeclarations &constants) const; - Description &m_description; + const ::pddl::ast::Description &m_description; output::ColorStream &m_outputStream; }; diff --git a/include/plasp/pddl/VariableStack.h b/include/plasp/pddl/VariableStack.h deleted file mode 100644 index 452902f..0000000 --- a/include/plasp/pddl/VariableStack.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __PLASP__PDDL__VARIABLE_STACK_H -#define __PLASP__PDDL__VARIABLE_STACK_H - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// VariableStack -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class VariableStack -{ - public: - void push(expressions::Variables *variables); - void pop(); - - expressions::VariablePointer parseAndFind(Context &context); - - private: - std::vector m_variableStack; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} - -#endif diff --git a/include/plasp/pddl/expressions/And.h b/include/plasp/pddl/expressions/And.h deleted file mode 100644 index 748d12e..0000000 --- a/include/plasp/pddl/expressions/And.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__AND_H -#define __PLASP__PDDL__EXPRESSIONS__AND_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// And -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class And: public NAry -{ - public: - static const Expression::Type ExpressionType = Expression::Type::And; - - static const std::string Identifier; - - public: - ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/At.h b/include/plasp/pddl/expressions/At.h deleted file mode 100644 index e884bc0..0000000 --- a/include/plasp/pddl/expressions/At.h +++ /dev/null @@ -1,112 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__AT_H -#define __PLASP__PDDL__EXPRESSIONS__AT_H - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// At -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class At: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::At; - - template - static AtPointer parse(Context &context, ExpressionContext &expressionContext, - ExpressionParser parseExpression); - - static const size_t TimePointStart = std::numeric_limits::max(); - static const size_t TimePointEnd = std::numeric_limits::max() - 1; - - public: - At(); - - ExpressionPointer copy() override; - - size_t timePoint() const; - - void setArgument(ExpressionPointer argument); - ExpressionPointer argument() const; - - ExpressionPointer reduced() override; - ExpressionPointer existentiallyQuantified() override; - ExpressionPointer simplified() override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - protected: - size_t m_timePoint; - - ExpressionPointer m_argument; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -AtPointer At::parse(Context &context, ExpressionContext &expressionContext, - ExpressionParser parseExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip("at")) - { - tokenizer.seek(position); - return nullptr; - } - - size_t timePoint; - - const auto timePointPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("start")) - timePoint = TimePointStart; - else if (tokenizer.testIdentifierAndSkip("end")) - timePoint = TimePointEnd; - else if (tokenizer.probeNumber()) - { - tokenizer.seek(timePointPosition); - timePoint = tokenizer.get(); - } - else - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = AtPointer(new At); - - expression->m_timePoint = timePoint; - - tokenizer.skipWhiteSpace(); - - // Parse argument - expression->setArgument(parseExpression(context, expressionContext)); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Binary.h b/include/plasp/pddl/expressions/Binary.h deleted file mode 100644 index 72ec620..0000000 --- a/include/plasp/pddl/expressions/Binary.h +++ /dev/null @@ -1,195 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__BINARY_H -#define __PLASP__PDDL__EXPRESSIONS__BINARY_H - -#include -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Binary -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -class Binary: public ExpressionCRTP -{ - public: - template - static boost::intrusive_ptr parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression); - - public: - ExpressionPointer copy() override; - - void setArgument(size_t i, ExpressionPointer argument); - std::array &arguments(); - const std::array &arguments() const; - - ExpressionPointer reduced() override; - ExpressionPointer existentiallyQuantified() override; - ExpressionPointer simplified() override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - protected: - std::array m_arguments; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -template -boost::intrusive_ptr Binary::parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip(Derived::Identifier)) - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = boost::intrusive_ptr(new Derived); - - // Parse arguments of the expression - expression->Binary::setArgument(0, parseExpression(context, expressionContext)); - expression->Binary::setArgument(1, parseExpression(context, expressionContext)); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -ExpressionPointer Binary::copy() -{ - auto result = new Derived; - - for (size_t i = 0; i < m_arguments.size(); i++) - result->m_arguments[i] = m_arguments[i]->copy(); - - return result; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -void Binary::setArgument(size_t i, ExpressionPointer expression) -{ - BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range"); - - m_arguments[i] = expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -std::array &Binary::arguments() -{ - return m_arguments; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const std::array &Binary::arguments() const -{ - return m_arguments; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer Binary::reduced() -{ - for (auto &argument : m_arguments) - { - BOOST_ASSERT(argument); - - argument = argument->reduced(); - } - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer Binary::existentiallyQuantified() -{ - for (auto &argument : m_arguments) - { - BOOST_ASSERT(argument); - - argument = argument->existentiallyQuantified(); - } - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer Binary::simplified() -{ - for (auto &argument : m_arguments) - { - BOOST_ASSERT(argument); - - argument = argument->simplified(); - } - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void Binary::collectParameters(std::set ¶meters) -{ - for (const auto &argument : m_arguments) - argument->collectParameters(parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void Binary::print(std::ostream &ostream) const -{ - ostream << "(" << Derived::Identifier; - - std::for_each(m_arguments.begin(), m_arguments.end(), - [&](auto &argument) - { - ostream << " "; - argument->print(ostream); - }); - - ostream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Constant.h b/include/plasp/pddl/expressions/Constant.h deleted file mode 100644 index 89a0284..0000000 --- a/include/plasp/pddl/expressions/Constant.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__CONSTANT_H -#define __PLASP__PDDL__EXPRESSIONS__CONSTANT_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Constant -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Constant: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Constant; - - static void parseTypedDeclaration(Context &context, Domain &domain); - static void parseTypedDeclarations(Context &context, Domain &domain); - static void parseTypedDeclaration(Context &context, Problem &problem); - static void parseTypedDeclarations(Context &context, Problem &problem); - - static ConstantPointer parseAndFind(Context &context, const Domain &domain); - static ConstantPointer parseAndFind(Context &context, const Problem &problem); - - public: - const std::string &name() const; - PrimitiveTypePointer type() const; - - void print(std::ostream &ostream) const override; - - private: - static ConstantPointer parseDeclaration(Context &context); - static void parseTypedDeclaration(Context &context, Domain &domain, Constants &constants); - - static ConstantPointer parseAndFind(const std::string &constantName, const Constants &constants); - - Constant(); - - void setDirty(bool isDirty = true); - bool isDirty() const; - - void setType(PrimitiveTypePointer parentType); - - bool m_isDirty; - - std::string m_name; - - PrimitiveTypePointer m_type; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/DerivedPredicate.h b/include/plasp/pddl/expressions/DerivedPredicate.h deleted file mode 100644 index c143f07..0000000 --- a/include/plasp/pddl/expressions/DerivedPredicate.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__DERIVED_PREDICATE_H -#define __PLASP__PDDL__EXPRESSIONS__DERIVED_PREDICATE_H - -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// DerivedPredicate -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class DerivedPredicate: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::DerivedPredicate; - - // TODO: consider implementing parsing functions for compatibility with older PDDL versions - - public: - explicit DerivedPredicate(size_t id); - - size_t id() const; - - void setPreconditions(std::vector &&preconditions); - const std::vector &preconditions() const; - - const std::set ¶meters() const; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - private: - void collectParameters(); - - size_t m_id; - - // The arguments are interpreted as a disjunction of conjunctions - std::vector m_preconditions; - - std::set m_parameters; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Dummy.h b/include/plasp/pddl/expressions/Dummy.h deleted file mode 100644 index 2905f49..0000000 --- a/include/plasp/pddl/expressions/Dummy.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__DUMMY_H -#define __PLASP__PDDL__EXPRESSIONS__DUMMY_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Dummy -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Dummy: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Dummy; - - public: - Dummy(std::string name); - - void print(std::ostream &ostream) const override; - - private: - std::string m_name; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Either.h b/include/plasp/pddl/expressions/Either.h deleted file mode 100644 index 8e49e76..0000000 --- a/include/plasp/pddl/expressions/Either.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__EITHER_H -#define __PLASP__PDDL__EXPRESSIONS__EITHER_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Either -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Either: public NAry -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Either; - - static const std::string Identifier; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Exists.h b/include/plasp/pddl/expressions/Exists.h deleted file mode 100644 index 815b34f..0000000 --- a/include/plasp/pddl/expressions/Exists.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__EXISTS_H -#define __PLASP__PDDL__EXPRESSIONS__EXISTS_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Exists -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Exists: public QuantifiedCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Exists; - - static const std::string Identifier; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/ForAll.h b/include/plasp/pddl/expressions/ForAll.h deleted file mode 100644 index 1d2f47c..0000000 --- a/include/plasp/pddl/expressions/ForAll.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__FOR_ALL_H -#define __PLASP__PDDL__EXPRESSIONS__FOR_ALL_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// ForAll -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class ForAll: public QuantifiedCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::ForAll; - - static const std::string Identifier; - - public: - ExpressionPointer existentiallyQuantified() override; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Imply.h b/include/plasp/pddl/expressions/Imply.h deleted file mode 100644 index b8c2f1a..0000000 --- a/include/plasp/pddl/expressions/Imply.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__IMPLY_H -#define __PLASP__PDDL__EXPRESSIONS__IMPLY_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Imply -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Imply: public Binary -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Imply; - - static const std::string Identifier; - - public: - ExpressionPointer reduced() override; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/NAry.h b/include/plasp/pddl/expressions/NAry.h deleted file mode 100644 index 5b1d869..0000000 --- a/include/plasp/pddl/expressions/NAry.h +++ /dev/null @@ -1,239 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__N_ARY_H -#define __PLASP__PDDL__EXPRESSIONS__N_ARY_H - -#include -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// NAry -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -class NAry: public ExpressionCRTP -{ - public: - template - static boost::intrusive_ptr parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression); - - public: - ExpressionPointer copy() override; - - void setArgument(size_t i, ExpressionPointer argument); - void addArgument(ExpressionPointer argument); - Expressions &arguments(); - const Expressions &arguments() const; - - ExpressionPointer reduced() override; - ExpressionPointer existentiallyQuantified() override; - ExpressionPointer simplified() override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - protected: - Expressions m_arguments; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -template -boost::intrusive_ptr NAry::parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip(Derived::Identifier)) - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = boost::intrusive_ptr(new Derived); - - tokenizer.skipWhiteSpace(); - - // Parse arguments of the expression - while (tokenizer.currentCharacter() != ')') - { - expression->addArgument(parseExpression(context, expressionContext)); - - tokenizer.skipWhiteSpace(); - } - - if (expression->m_arguments.empty()) - context.logger.log(output::Priority::Warning, tokenizer, "“" + Derived::Identifier + "” expressions should not be empty"); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -ExpressionPointer NAry::copy() -{ - auto result = new Derived; - - result->m_arguments.resize(m_arguments.size()); - - for (size_t i = 0; i < m_arguments.size(); i++) - result->m_arguments[i] = m_arguments[i]->copy(); - - return result; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -void NAry::setArgument(size_t i, ExpressionPointer expression) -{ - BOOST_ASSERT_MSG(i <= m_arguments.size(), "Index out of range"); - - m_arguments[i] = expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -void NAry::addArgument(ExpressionPointer argument) -{ - if (!argument) - return; - - m_arguments.emplace_back(argument); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -const Expressions &NAry::arguments() const -{ - return m_arguments; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -Expressions &NAry::arguments() -{ - return m_arguments; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer NAry::reduced() -{ - for (auto &argument : m_arguments) - { - BOOST_ASSERT(argument); - - argument = argument->reduced(); - } - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer NAry::existentiallyQuantified() -{ - for (auto &argument : m_arguments) - { - BOOST_ASSERT(argument); - - argument = argument->existentiallyQuantified(); - } - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer NAry::simplified() -{ - // Associate same-type children, such as (a && (b && c)) == (a && b && c) - for (size_t i = 0; i < m_arguments.size();) - { - auto &argument = m_arguments[i]; - argument = argument->simplified(); - - if (argument->expressionType() != Derived::ExpressionType) - { - i++; - continue; - } - - auto &nAryExpression = argument->template as(); - - BOOST_ASSERT(!nAryExpression.arguments().empty()); - - // Remove former child by replacing it with the first one of the child - m_arguments[i] = nAryExpression.arguments().front(); - - // Reserve space for new arguments - m_arguments.reserve(m_arguments.size() + nAryExpression.arguments().size() - 1); - - // Copy all but first element - m_arguments.insert(m_arguments.end(), nAryExpression.arguments().begin() + 1, nAryExpression.arguments().end()); - } - - // TODO: recognize tautologies - // TODO: introduce/handle boolean values - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void NAry::collectParameters(std::set ¶meters) -{ - for (const auto &argument : m_arguments) - argument->collectParameters(parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void NAry::print(std::ostream &ostream) const -{ - ostream << "(" << Derived::Identifier; - - for (const auto &argument : m_arguments) - { - ostream << " "; - argument->print(ostream); - } - - ostream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Not.h b/include/plasp/pddl/expressions/Not.h deleted file mode 100644 index a04ed5a..0000000 --- a/include/plasp/pddl/expressions/Not.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__NOT_H -#define __PLASP__PDDL__EXPRESSIONS__NOT_H - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Not -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Not: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Not; - - template - static NotPointer parse(Context &context, ExpressionContext &expressionContext, - ExpressionParser parseExpression); - - public: - Not(); - - ExpressionPointer copy() override; - - void setArgument(ExpressionPointer argument); - ExpressionPointer argument() const; - - ExpressionPointer reduced() override; - ExpressionPointer existentiallyQuantified() override; - ExpressionPointer simplified() override; - ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - protected: - ExpressionPointer m_argument; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -NotPointer Not::parse(Context &context, ExpressionContext &expressionContext, - ExpressionParser parseExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip("not")) - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = NotPointer(new Not); - - tokenizer.skipWhiteSpace(); - - // Parse argument - expression->setArgument(parseExpression(context, expressionContext)); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Or.h b/include/plasp/pddl/expressions/Or.h deleted file mode 100644 index 2d78071..0000000 --- a/include/plasp/pddl/expressions/Or.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__OR_H -#define __PLASP__PDDL__EXPRESSIONS__OR_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Or -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Or: public NAry -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Or; - - static const std::string Identifier; - - public: - ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Predicate.h b/include/plasp/pddl/expressions/Predicate.h deleted file mode 100644 index a9a1c70..0000000 --- a/include/plasp/pddl/expressions/Predicate.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__PREDICATE_H -#define __PLASP__PDDL__EXPRESSIONS__PREDICATE_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Predicate -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Predicate: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Predicate; - - static PredicatePointer parse(Context &context, ExpressionContext &expressionContext); - static PredicatePointer parse(Context &context, const Problem &problem); - - public: - const std::string &name() const; - const Expressions &arguments() const; - - bool isDeclared() const; - - ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - private: - Predicate(); - - void setDeclared(); - - bool m_isDeclared; - - std::string m_name; - Expressions m_arguments; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/PredicateDeclaration.h b/include/plasp/pddl/expressions/PredicateDeclaration.h deleted file mode 100644 index 32b2897..0000000 --- a/include/plasp/pddl/expressions/PredicateDeclaration.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__PREDICATE_DECLARATION_H -#define __PLASP__PDDL__EXPRESSIONS__PREDICATE_DECLARATION_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// PredicateDeclaration -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class PredicateDeclaration: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::PredicateDeclaration; - - static void parse(Context &context, Domain &domain); - - public: - const std::string &name() const; - const Variables ¶meters() const; - - bool isDeclared() const; - - void normalizeParameterNames(); - - void print(std::ostream &ostream) const override; - - private: - PredicateDeclaration(); - - void setDeclared(); - - bool m_isDeclared; - - std::string m_name; - Variables m_parameters; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/PrimitiveType.h b/include/plasp/pddl/expressions/PrimitiveType.h deleted file mode 100644 index f47817f..0000000 --- a/include/plasp/pddl/expressions/PrimitiveType.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__PRIMITIVE_TYPE_H -#define __PLASP__PDDL__EXPRESSIONS__PRIMITIVE_TYPE_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// PrimitiveType -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class PrimitiveType: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::PrimitiveType; - - static void parseDeclaration(Context &context, Domain &domain); - static void parseTypedDeclaration(Context &context, Domain &domain); - - static PrimitiveTypePointer parseAndFind(Context &context, Domain &domain); - - public: - PrimitiveType(); - PrimitiveType(std::string name); - - const std::string &name() const; - const PrimitiveTypes &parentTypes() const; - - void print(std::ostream &ostream) const override; - - private: - void setDirty(bool isDirty = true); - bool isDirty() const; - - bool m_isDirty; - - std::string m_name; - - PrimitiveTypes m_parentTypes; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Quantified.h b/include/plasp/pddl/expressions/Quantified.h deleted file mode 100644 index 8c043e1..0000000 --- a/include/plasp/pddl/expressions/Quantified.h +++ /dev/null @@ -1,250 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H -#define __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H - -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Quantified -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Quantified: public Expression -{ - public: - void setArgument(ExpressionPointer argument); - ExpressionPointer argument() const; - - Variables &variables(); - const Variables &variables() const; - - protected: - Variables m_variables; - ExpressionPointer m_argument; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -class QuantifiedCRTP: public Quantified -{ - public: - template - static boost::intrusive_ptr parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression); - - public: - Type expressionType() const override final - { - return Derived::ExpressionType; - } - - ExpressionPointer copy() override; - - ExpressionPointer reduced() override; - ExpressionPointer existentiallyQuantified() override; - ExpressionPointer simplified() override; - ExpressionPointer decomposed(DerivedPredicates &derivedPredicates) override; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -template -boost::intrusive_ptr QuantifiedCRTP::parse(Context &context, - ExpressionContext &expressionContext, ExpressionParser parseExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip(Derived::Identifier)) - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = boost::intrusive_ptr(new Derived); - - // Parse variable list - tokenizer.expect("("); - Variable::parseTypedDeclarations(context, expressionContext, expression->m_variables); - tokenizer.expect(")"); - - // Push newly parsed variables to the stack - expressionContext.variables.push(&expression->m_variables); - - // Parse argument of the expression - expression->Quantified::setArgument(parseExpression(context, expressionContext)); - - // Clean up variable stack - expressionContext.variables.pop(); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -ExpressionPointer QuantifiedCRTP::copy() -{ - auto result = new Derived; - - result->m_argument = m_argument->copy(); - - return result; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void Quantified::setArgument(ExpressionPointer expression) -{ - m_argument = expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline ExpressionPointer Quantified::argument() const -{ - return m_argument; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline Variables &Quantified::variables() -{ - return m_variables; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline const Variables &Quantified::variables() const -{ - return m_variables; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer QuantifiedCRTP::reduced() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->reduced(); - - // Child quantifiers may not move before this quantifier, the order matters - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer QuantifiedCRTP::existentiallyQuantified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->existentiallyQuantified(); - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer QuantifiedCRTP::simplified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->simplified(); - - // Associate same-type children, such as (forall (?x) (forall (?y) (...))) - if (m_argument->expressionType() != Derived::ExpressionType) - return this; - - auto &quantifiedExpression = m_argument->template as(); - - // Unify variables - m_variables.insert(m_variables.end(), quantifiedExpression.variables().begin(), quantifiedExpression.variables().end()); - - // Move child expression up - m_argument = quantifiedExpression.argument(); - - // TODO: introduce/handle boolean values - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline ExpressionPointer QuantifiedCRTP::decomposed(DerivedPredicates &derivedPredicates) -{ - derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size())); - auto derivedPredicate = derivedPredicates.back(); - - m_argument = m_argument->decomposed(derivedPredicates); - - derivedPredicate->setPreconditions({{this}}); - - return derivedPredicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void QuantifiedCRTP::collectParameters(std::set ¶meters) -{ - m_argument->collectParameters(parameters); - - // Remove bound variables - for (const auto &variable : m_variables) - parameters.erase(variable); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void QuantifiedCRTP::print(std::ostream &ostream) const -{ - ostream << "(" << Derived::Identifier << " ("; - - for (size_t i = 0; i < m_variables.size(); i++) - { - if (i > 0) - ostream << " "; - - m_variables[i]->print(ostream); - } - - ostream << ") "; - - m_argument->print(ostream); - - ostream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Type.h b/include/plasp/pddl/expressions/Type.h deleted file mode 100644 index 0dc228d..0000000 --- a/include/plasp/pddl/expressions/Type.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__TYPE_H -#define __PLASP__PDDL__EXPRESSIONS__TYPE_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Type -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveTypePointer parseExistingPrimitiveType(Context &context, - ExpressionContext &expressionContext); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Unsupported.h b/include/plasp/pddl/expressions/Unsupported.h deleted file mode 100644 index 48e0ef7..0000000 --- a/include/plasp/pddl/expressions/Unsupported.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__UNSUPPORTED_H -#define __PLASP__PDDL__EXPRESSIONS__UNSUPPORTED_H - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Unsupported -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Unsupported: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Unsupported; - - static UnsupportedPointer parse(Context &context); - - public: - const std::string &type() const; - - void print(std::ostream &ostream) const override; - - private: - std::string m_type; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/Variable.h b/include/plasp/pddl/expressions/Variable.h deleted file mode 100644 index 4c76abe..0000000 --- a/include/plasp/pddl/expressions/Variable.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__VARIABLE_H -#define __PLASP__PDDL__EXPRESSIONS__VARIABLE_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Variable -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class Variable: public ExpressionCRTP -{ - public: - static const Expression::Type ExpressionType = Expression::Type::Variable; - - static void parseTypedDeclaration(Context &context, ExpressionContext &expressionContext, - Variables &variables); - static void parseTypedDeclarations(Context &context, ExpressionContext &expressionContext, - Variables &variables); - - public: - Variable(); - Variable(std::string name); - - void setName(std::string name); - const std::string &name() const; - - void setType(ExpressionPointer type); - ExpressionPointer type() const; - - void setDirty(bool isDirty = true); - bool isDirty() const; - - void collectParameters(std::set ¶meters) override; - - void print(std::ostream &ostream) const override; - - private: - static void parseDeclaration(Context &context, Variables ¶meters); - - private: - bool m_isDirty; - - std::string m_name; - - ExpressionPointer m_type; -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/expressions/When.h b/include/plasp/pddl/expressions/When.h deleted file mode 100644 index e7f9254..0000000 --- a/include/plasp/pddl/expressions/When.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef __PLASP__PDDL__EXPRESSIONS__WHEN_H -#define __PLASP__PDDL__EXPRESSIONS__WHEN_H - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// When -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -class When: public Binary -{ - public: - static const Expression::Type ExpressionType = Expression::Type::When; - - static const std::string Identifier; - - template - static WhenPointer parse(Context &context, ExpressionContext &expressionContext, - ConditionExpressionParser parseConditionExpression, - ImplicationExpressionParser parseImplicationExpression); -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -WhenPointer When::parse(Context &context, ExpressionContext &expressionContext, - ConditionExpressionParser parseConditionExpression, - ImplicationExpressionParser parseImplicationExpression) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(") - || !tokenizer.testIdentifierAndSkip(Identifier)) - { - tokenizer.seek(position); - return nullptr; - } - - auto expression = WhenPointer(new When); - - // Parse arguments of the expression - expression->setArgument(0, parseConditionExpression(context, expressionContext)); - expression->setArgument(1, parseImplicationExpression(context, expressionContext)); - - tokenizer.expect(")"); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} - -#endif diff --git a/include/plasp/pddl/translation/Effect.h b/include/plasp/pddl/translation/Effect.h new file mode 100644 index 0000000..0bdae6e --- /dev/null +++ b/include/plasp/pddl/translation/Effect.h @@ -0,0 +1,79 @@ +#ifndef __PLASP__PDDL__TRANSLATION__EFFECT_H +#define __PLASP__PDDL__TRANSLATION__EFFECT_H + +#include + +#include +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Effect +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline void translateEffect(output::ColorStream &outputStream, const ::pddl::ast::Effect &effect, const std::string &objectType, PrintObjectName printObjectName) +{ + const auto handleUnsupported = + [](const auto &) + { + throw output::TranslatorException("only “and” expressions and (negated) predicates supported as action effects currently"); + }; + + const auto handlePredicate = + [&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true) + { + outputStream << std::endl << output::Function("postcondition") << "("; + printObjectName(); + outputStream + << ", " << output::Keyword("effect") << "(" + << output::Reserved("unconditional") << ")" + << ", "; + translatePredicateToVariable(outputStream, *predicate, isPositive); + outputStream << ") :- " << output::Function(objectType.c_str()) << "("; + printObjectName(); + outputStream << ")."; + }; + + const auto handleAtomicFormula = + [&](const ::pddl::ast::AtomicFormula &atomicFormula) + { + atomicFormula.match(handlePredicate, handleUnsupported); + }; + + const auto handleNot = + [&](const ::pddl::ast::NotPointer<::pddl::ast::Effect> ¬_) + { + if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) + handleUnsupported(not_); + + const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + + handlePredicate(predicate, false); + }; + + const auto handleAnd = + [&](const ::pddl::ast::AndPointer<::pddl::ast::Effect> &and_) + { + for (const auto &argument : and_->arguments) + translateEffect(outputStream, argument, objectType, printObjectName); + }; + + effect.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/include/plasp/pddl/translation/Goal.h b/include/plasp/pddl/translation/Goal.h new file mode 100644 index 0000000..45094fa --- /dev/null +++ b/include/plasp/pddl/translation/Goal.h @@ -0,0 +1,72 @@ +#ifndef __PLASP__PDDL__TRANSLATION__GOAL_H +#define __PLASP__PDDL__TRANSLATION__GOAL_H + +#include + +#include +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Goal +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline void translateGoal(output::ColorStream &outputStream, const ::pddl::ast::Goal &goal) +{ + const auto handleUnsupported = + [](const auto &) + { + throw output::TranslatorException("only “and” expressions and (negated) predicates supported as goals currently"); + }; + + const auto handlePredicate = + [&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true) + { + outputStream << std::endl << output::Function("goal") << "("; + // TODO: assert that goal is variable-free + translatePredicateToVariable(outputStream, *predicate, isPositive); + outputStream << ")."; + }; + + const auto handleAtomicFormula = + [&](const ::pddl::ast::AtomicFormula &atomicFormula) + { + atomicFormula.match(handlePredicate, handleUnsupported); + }; + + const auto handleNot = + [&](const ::pddl::ast::NotPointer<::pddl::ast::Goal> ¬_) + { + if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) + handleUnsupported(not_); + + const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + + handlePredicate(predicate, false); + }; + + const auto handleAnd = + [&](const ::pddl::ast::AndPointer<::pddl::ast::Goal> &and_) + { + for (const auto &argument : and_->arguments) + translateGoal(outputStream, argument); + }; + + goal.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/include/plasp/pddl/translation/Precondition.h b/include/plasp/pddl/translation/Precondition.h index a826d63..95c2912 100644 --- a/include/plasp/pddl/translation/Precondition.h +++ b/include/plasp/pddl/translation/Precondition.h @@ -1,16 +1,18 @@ #ifndef __PLASP__PDDL__TRANSLATION__PRECONDITION_H #define __PLASP__PDDL__TRANSLATION__PRECONDITION_H +#include + #include -#include +#include + +#include #include namespace plasp { namespace pddl { -namespace translation -{ //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -18,57 +20,56 @@ namespace translation // //////////////////////////////////////////////////////////////////////////////////////////////////// -template -void translatePreconditionDisjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, const std::vector &preconditionDisjunction); -template -void translatePreconditionConjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expressions &preconditionConjunction); -template -void translatePrecondition(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expression &precondition); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void translatePreconditionDisjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, const std::vector &preconditionDisjunction) +template +inline void translatePrecondition(output::ColorStream &outputStream, const ::pddl::ast::Precondition &precondition, const std::string &objectType, PrintObjectName printObjectName) { - for (size_t i = 0; i < preconditionDisjunction.size(); i++) - translatePreconditionConjunction(outputStream, objectType, printObjectName, i, preconditionDisjunction[i]); + const auto handleUnsupported = + [](const auto &) + { + throw output::TranslatorException("only “and” expressions and (negated) predicates supported as action preconditions currently"); + }; + + const auto handlePredicate = + [&](const ::pddl::ast::PredicatePointer &predicate, bool isPositive = true) + { + outputStream << std::endl << output::Function("precondition") << "("; + printObjectName(); + outputStream << ", "; + translatePredicateToVariable(outputStream, *predicate, isPositive); + outputStream << ") :- " << output::Function(objectType.c_str()) << "("; + printObjectName(); + outputStream << ")."; + }; + + const auto handleAtomicFormula = + [&](const ::pddl::ast::AtomicFormula &atomicFormula) + { + atomicFormula.match(handlePredicate, handleUnsupported); + }; + + const auto handleNot = + [&](const ::pddl::ast::NotPointer<::pddl::ast::Precondition> ¬_) + { + if (!not_->argument.is<::pddl::ast::AtomicFormula>() || !not_->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) + handleUnsupported(not_); + + const auto &predicate = not_->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + + handlePredicate(predicate, false); + }; + + const auto handleAnd = + [&](const ::pddl::ast::AndPointer<::pddl::ast::Precondition> &and_) + { + for (const auto &argument : and_->arguments) + translatePrecondition(outputStream, argument, objectType, printObjectName); + }; + + precondition.match(handleAtomicFormula, handleNot, handleAnd, handleUnsupported); } //////////////////////////////////////////////////////////////////////////////////////////////////// -template -inline void translatePreconditionConjunction(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expressions &preconditionConjunction) -{ - for (size_t i = 0; i < preconditionConjunction.size(); i++) - translatePrecondition(outputStream, objectType, printObjectName, disjunctionID, *preconditionConjunction[i]); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -template -inline void translatePrecondition(output::ColorStream &outputStream, const std::string &objectType, PrintObjectName printObjectName, size_t disjunctionID, const Expression &precondition) -{ - outputStream << std::endl << output::Function("precondition") << "("; - - printObjectName(outputStream); - - outputStream - << ", " << output::Keyword("disjunct") - << "(" << output::Number(disjunctionID) - << "), "; - - translateLiteral(outputStream, precondition); - - outputStream << ") :- " << output::Function(objectType.c_str()) << "("; - - printObjectName(outputStream); - - outputStream << ")."; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} } } diff --git a/include/plasp/pddl/translation/Predicate.h b/include/plasp/pddl/translation/Predicate.h index 67dac5a..f35b7ac 100644 --- a/include/plasp/pddl/translation/Predicate.h +++ b/include/plasp/pddl/translation/Predicate.h @@ -1,20 +1,18 @@ #ifndef __PLASP__PDDL__TRANSLATION__PREDICATE_H #define __PLASP__PDDL__TRANSLATION__PREDICATE_H +#include +#include + #include -#include -#include -#include -#include -#include + +#include #include namespace plasp { namespace pddl { -namespace translation -{ //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -22,44 +20,46 @@ namespace translation // //////////////////////////////////////////////////////////////////////////////////////////////////// -void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate); -void translateDerivedPredicate(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate); -void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration); -void printDerivedPredicateName(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate); +void translatePredicate(output::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate); +void translatePredicateDeclaration(output::ColorStream &outputStream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration); //////////////////////////////////////////////////////////////////////////////////////////////////// -inline void translatePredicate(output::ColorStream &outputStream, const expressions::Predicate &predicate) +inline void translatePredicate(output::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate) { - const auto &arguments = predicate.arguments(); + const auto &arguments = predicate.arguments; if (arguments.empty()) { - outputStream << output::String(predicate.name().c_str()); - + outputStream << *predicate.declaration; return; } - outputStream << "(" << output::String(predicate.name().c_str()); + outputStream << "(" << *predicate.declaration; for (const auto &argument : arguments) { outputStream << ", "; - if (argument->is()) - { - const auto &constant = argument->as(); + const auto handleConstant = + [&](const ::pddl::ast::ConstantPointer &constant) + { + outputStream << output::Keyword("constant") << "(" << *constant << ")"; + }; - outputStream << output::Keyword("constant") << "(" << output::String(constant.name().c_str()) << ")"; - } - else if (argument->is()) - { - const auto &variable = argument->as(); + const auto handleVariable = + [&](const ::pddl::ast::VariablePointer &variable) + { + outputStream << *variable; + }; - outputStream << output::Variable(variable.name().c_str()); - } - else - throw output::TranslatorException("only variables and constants supported in predicates currently"); + const auto handleUnsupported = + [&](const auto &) + { + throw output::TranslatorException("only variables and constants supported in predicates currently"); + }; + + argument.match(handleConstant, handleVariable, handleUnsupported); } outputStream << ")"; @@ -67,64 +67,41 @@ inline void translatePredicate(output::ColorStream &outputStream, const expressi //////////////////////////////////////////////////////////////////////////////////////////////////// -inline void translateDerivedPredicate(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate) -{ - const auto ¶meters = derivedPredicate.parameters(); - const auto id = derivedPredicate.id(); - - if (parameters.empty()) - { - outputStream << output::Number(id); - return; - } - - outputStream << "(" << output::Number(id); - - for (const auto ¶meter : parameters) - outputStream << ", " << output::Variable(parameter->name().c_str()); - - outputStream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void printPredicateName(output::ColorStream &outputStream, const expressions::PredicateDeclaration &predicateDeclaration) +inline void translatePredicateDeclaration(output::ColorStream &outputStream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration) { outputStream << output::Keyword("variable") << "("; - if (predicateDeclaration.parameters().empty()) + if (predicateDeclaration.parameters.empty()) { - outputStream << output::String(predicateDeclaration.name().c_str()) << ")"; + outputStream << predicateDeclaration << ")"; return; } - outputStream << "(" << output::String(predicateDeclaration.name().c_str()); - translation::translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters()); + outputStream << "(" << predicateDeclaration; + translateVariablesForRuleHead(outputStream, predicateDeclaration.parameters); outputStream << "))"; } //////////////////////////////////////////////////////////////////////////////////////////////////// -inline void printDerivedPredicateName(output::ColorStream &outputStream, const expressions::DerivedPredicate &derivedPredicate) +void translatePredicateToVariable(output::ColorStream &outputStream, const ::pddl::ast::Predicate &predicate, bool isPositive = true) { - outputStream << output::Keyword("derivedVariable") << "("; + outputStream << output::Keyword("variable") << "("; + translatePredicate(outputStream, predicate); + outputStream << "), " << output::Keyword("value") << "("; + translatePredicate(outputStream, predicate); + outputStream << ", "; - const auto id = derivedPredicate.id(); + if (isPositive) + outputStream << output::Boolean("true"); + else + outputStream << output::Boolean("false"); - if (derivedPredicate.parameters().empty()) - { - outputStream << output::Number(id) << ")"; - return; - } - - outputStream << "(" << output::Number(id); - translation::translateVariablesForRuleHead(outputStream, derivedPredicate.parameters()); - outputStream << "))"; + outputStream << ")"; } //////////////////////////////////////////////////////////////////////////////////////////////////// -} } } diff --git a/include/plasp/pddl/translation/Primitives.h b/include/plasp/pddl/translation/Primitives.h index 24d3dbb..ae10470 100644 --- a/include/plasp/pddl/translation/Primitives.h +++ b/include/plasp/pddl/translation/Primitives.h @@ -1,19 +1,15 @@ #ifndef __PLASP__PDDL__TRANSLATION__PRIMITIVES_H #define __PLASP__PDDL__TRANSLATION__PRIMITIVES_H +#include + #include #include -#include -#include -#include -#include namespace plasp { namespace pddl { -namespace translation -{ //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -21,56 +17,91 @@ namespace translation // //////////////////////////////////////////////////////////////////////////////////////////////////// -void translateLiteral(output::ColorStream &outputStream, const Expression &literal); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -inline void translateLiteral(output::ColorStream &outputStream, const Expression &literal) +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::ConstantDeclaration &constantDeclaration) { - // Translate single predicate - if (literal.is()) - { - const auto &predicate = literal.as(); + assert(!constantDeclaration.name.empty()); - outputStream << output::Keyword("variable") << "("; - translation::translatePredicate(outputStream, predicate); - outputStream << "), " << output::Keyword("value") << "("; - translation::translatePredicate(outputStream, predicate); - outputStream << ", " << output::Boolean("true") << ")"; - } - // Assuming that "not" expression may only contain a predicate - else if (literal.is()) - { - const auto ¬Expression = literal.as(); - - if (notExpression.argument()->expressionType() != Expression::Type::Predicate) - throw output::TranslatorException("only negations of primitive predicates supported as literals currently"); - - const auto &predicate = notExpression.argument()->as(); - - outputStream << output::Keyword("variable") << "("; - translation::translatePredicate(outputStream, predicate); - outputStream << "), " << output::Keyword("value") << "("; - translation::translatePredicate(outputStream, predicate); - outputStream << ", " << output::Boolean("false") << ")"; - } - else if (literal.is()) - { - const auto &derivedPredicate = literal.as(); - - outputStream << output::Keyword("derivedVariable") << "("; - translation::translateDerivedPredicate(outputStream, derivedPredicate); - outputStream << "), " << output::Keyword("value") << "("; - translation::translateDerivedPredicate(outputStream, derivedPredicate); - outputStream << ", " << output::Boolean("true") << ")"; - } - else - throw output::TranslatorException("only primitive predicates and their negations supported as literals currently"); + return (stream << output::String(constantDeclaration.name.c_str())); } //////////////////////////////////////////////////////////////////////////////////////////////////// +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::Constant &constant) +{ + assert(constant.declaration != nullptr); + + return (stream << *constant.declaration); } + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::PrimitiveTypeDeclaration &primitiveTypeDeclaration) +{ + assert(!primitiveTypeDeclaration.name.empty()); + + return (stream << output::String(primitiveTypeDeclaration.name.c_str())); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::PrimitiveType &primitiveType) +{ + assert(primitiveType.declaration != nullptr); + + return (stream << *primitiveType.declaration); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline output::ColorStream &operator<<(output::ColorStream &stream, ::pddl::ast::VariableDeclaration &variableDeclaration) +{ + assert(!variableDeclaration.name.empty()); + assert(std::isalpha(variableDeclaration.name[0])); + + if (!std::isupper(variableDeclaration.name[0])) + variableDeclaration.name[0] = std::toupper(variableDeclaration.name[0]); + + return (stream + << output::Format({output::Color::Green, output::FontWeight::Bold}) + << variableDeclaration.name + << output::ResetFormat()); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline output::ColorStream &operator<<(output::ColorStream &stream, ::pddl::ast::Variable &variable) +{ + assert(variable.declaration != nullptr); + + return (stream << *variable.declaration); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// TODO: move to appropriate header +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::Action &action) +{ + return (stream << output::String(action.name.c_str())); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// TODO: move to appropriate header +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::PredicateDeclaration &predicateDeclaration) +{ + return (stream << output::String(predicateDeclaration.name.c_str())); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +// TODO: move to appropriate header +inline output::ColorStream &operator<<(output::ColorStream &stream, const ::pddl::ast::Predicate &predicate) +{ + return (stream << *predicate.declaration); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/include/plasp/pddl/translation/Variables.h b/include/plasp/pddl/translation/Variables.h index 5696aac..e39e085 100644 --- a/include/plasp/pddl/translation/Variables.h +++ b/include/plasp/pddl/translation/Variables.h @@ -1,6 +1,9 @@ #ifndef __PLASP__PDDL__TRANSLATION__VARIABLES_H #define __PLASP__PDDL__TRANSLATION__VARIABLES_H +#include +#include + #include #include @@ -8,8 +11,6 @@ namespace plasp { namespace pddl { -namespace translation -{ //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -30,12 +31,8 @@ inline void translateVariablesForRuleHead(output::ColorStream &outputStream, con if (variables.empty()) return; - for (auto i = variables.cbegin(); i != variables.cend(); i++) - { - const auto &variable = **i; - - outputStream << ", " << output::Variable(variable.name().c_str()); - } + for (const auto &variable : variables) + outputStream << ", " << *variable; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -48,28 +45,25 @@ inline void translateVariablesForRuleBody(output::ColorStream &outputStream, con outputStream << " :- "; - for (auto i = variables.cbegin(); i != variables.cend(); i++) + for (const auto &variable : variables) { - const auto &variable = **i; - - if (i != variables.cbegin()) + if (variable.get() != variables.begin()->get()) outputStream << ", "; - if (variable.type() != nullptr) + if (variable->type) { - if (variable.type()->expressionType() != Expression::Type::PrimitiveType) + if (!variable->type.value().template is<::pddl::ast::PrimitiveTypePointer>()) throw output::TranslatorException("only primitive types supported currently"); - const auto &type = variable.type()->template as(); + const auto &type = variable->type.value().template get<::pddl::ast::PrimitiveTypePointer>(); outputStream << output::Function("has") << "(" - << output::Variable(variable.name().c_str()) << ", " - << output::Keyword("type") << "(" << output::String(type.name().c_str()) << "))"; + << *variable << ", " << output::Keyword("type") << "(" << *type << "))"; } else { outputStream << output::Function("has") << "(" - << output::Variable(variable.name().c_str()) << ", " + << *variable << ", " << output::Keyword("type") << "(" << output::String("object") << "))"; } } @@ -77,7 +71,6 @@ inline void translateVariablesForRuleBody(output::ColorStream &outputStream, con //////////////////////////////////////////////////////////////////////////////////////////////////// -} } } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d8d299d..64040b0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,8 @@ set(includes ${Boost_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/lib/tokenize/include + ${PROJECT_SOURCE_DIR}/lib/variant/include + ${PROJECT_SOURCE_DIR}/lib/pddlparse/include ) set(sources @@ -59,6 +61,7 @@ set(sources set(libraries ${Boost_LIBRARIES} tokenize + pddlparse pthread ) diff --git a/src/plasp/pddl/Action.cpp b/src/plasp/pddl/Action.cpp deleted file mode 100644 index 23f7bb1..0000000 --- a/src/plasp/pddl/Action.cpp +++ /dev/null @@ -1,118 +0,0 @@ -#include - -#include - -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Action -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Action::parseDeclaration(Context &context, Domain &domain) -{ - auto &tokenizer = context.tokenizer; - - auto action = std::make_unique(Action()); - - action->m_name = tokenizer.getIdentifier(); - - tokenizer.expect(":parameters"); - tokenizer.expect("("); - - ExpressionContext expressionContext(domain); - expressionContext.variables.push(&action->m_parameters); - - // Read parameters - expressions::Variable::parseTypedDeclarations(context, expressionContext, action->m_parameters); - - tokenizer.expect(")"); - - // Parse preconditions and effects - while (!tokenizer.testAndReturn(')')) - { - tokenizer.expect(":"); - - if (tokenizer.testIdentifierAndSkip("precondition")) - action->m_precondition = parsePreconditionExpression(context, expressionContext); - else if (tokenizer.testIdentifierAndSkip("effect")) - action->m_effect = parseEffectExpression(context, expressionContext); - - tokenizer.skipWhiteSpace(); - } - - // Store new action - expressionContext.domain.actions().emplace_back(std::move(action)); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Action::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::Variables &Action::parameters() const -{ - return m_parameters; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expression *Action::precondition() const -{ - BOOST_ASSERT(m_precondition); - - return m_precondition.get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expression *Action::effect() const -{ - BOOST_ASSERT(m_effect); - - return m_effect.get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Action::normalize(expressions::DerivedPredicates &derivedPredicates) -{ - // Normalize preconditions and effects - m_precondition = m_precondition->normalized()->decomposed(derivedPredicates); - { - m_precondition->print(std::cout); - - // TODO: refactor - std::set parameters; - m_precondition->collectParameters(parameters); - } - - m_effect = m_effect->normalized(); - { - // TODO: refactor - std::set parameters; - m_effect->collectParameters(parameters); - } - - // Normalize parameter names - for (size_t i = 0; i < m_parameters.size(); i++) - m_parameters[i]->setName("X" + std::to_string(i)); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Description.cpp b/src/plasp/pddl/Description.cpp deleted file mode 100644 index 24ac528..0000000 --- a/src/plasp/pddl/Description.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include - -#include -#include - -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Description -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Description::Description(Context &context) -: m_context(context), - m_domainPosition{tokenize::InvalidStreamPosition}, - m_domain{std::make_unique(Domain(m_context))}, - m_problemPosition{tokenize::InvalidStreamPosition} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Description Description::fromContext(Context &context) -{ - Description description(context); - - description.parse(); - - return description; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Description Description::fromStream(std::istream &istream, Context &context) -{ - Description description(context); - - description.m_context.tokenizer.read("std::cin", istream); - description.parse(); - - return description; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Description Description::fromFile(const std::string &path, Context &context) -{ - Description description(context); - - description.m_context.tokenizer.read(path); - description.parse(); - - return description; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Description Description::fromFiles(const std::vector &paths, Context &context) -{ - BOOST_ASSERT(!paths.empty()); - - // TODO: handle dirty context objects (for instance, reused context objects in unit tests) - - Description description(context); - - std::for_each(paths.cbegin(), paths.cend(), - [&](const auto &path) - { - description.m_context.tokenizer.read(path); - }); - - description.parse(); - - return description; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Context &Description::context() -{ - return m_context; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Context &Description::context() const -{ - return m_context; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Domain &Description::domain() const -{ - BOOST_ASSERT(m_domain); - - return *m_domain; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Description::containsProblem() const -{ - return m_problem.get() != nullptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Problem &Description::problem() const -{ - BOOST_ASSERT(m_problem); - - return *m_problem; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Description::parse() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.removeComments(";", "\n", false); - - // First, determine the locations of domain and problem - findSections(); - - if (m_domainPosition == tokenize::InvalidStreamPosition) - throw ConsistencyException("no PDDL domain specified"); - - tokenizer.seek(m_domainPosition); - m_domain->parse(); - - if (m_problemPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_problemPosition); - m_problem->parse(); - } - - checkConsistency(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Description::findSections() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.skipWhiteSpace(); - - while (!tokenizer.atEnd()) - { - const auto position = tokenizer.position(); - - tokenizer.expect("("); - tokenizer.expect("define"); - tokenizer.expect("("); - - if (tokenizer.testAndSkip("domain")) - { - if (m_domainPosition != tokenize::InvalidStreamPosition) - throw tokenize::TokenizerException(tokenizer, "PDDL description may not contain two domains"); - - m_domainPosition = position; - - tokenizer.seek(position); - m_domain->findSections(); - } - else if (tokenizer.testAndSkip("problem")) - { - if (m_problemPosition != tokenize::InvalidStreamPosition) - throw tokenize::TokenizerException(tokenizer, "PDDL description may currently not contain two problems"); - - m_problem = std::make_unique(Problem(m_context, *m_domain)); - - m_problemPosition = position; - - tokenizer.seek(position); - m_problem->findSections(); - } - else - { - const auto sectionIdentifier = tokenizer.get(); - throw tokenize::TokenizerException(tokenizer, "unknown PDDL section “" + sectionIdentifier + "”"); - } - - tokenizer.skipWhiteSpace(); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Description::checkConsistency() -{ - m_domain->checkConsistency(); - m_problem->checkConsistency(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Description::normalize() -{ - m_domain->normalize(); - - if (m_problem) - m_problem->normalize(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp deleted file mode 100644 index 525294f..0000000 --- a/src/plasp/pddl/Domain.cpp +++ /dev/null @@ -1,451 +0,0 @@ -#include - -#include - -#include -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Domain -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Domain::Domain(Context &context) -: m_context(context), - m_requirementsPosition{tokenize::InvalidStreamPosition}, - m_typesPosition{tokenize::InvalidStreamPosition}, - m_constantsPosition{tokenize::InvalidStreamPosition}, - m_predicatesPosition{tokenize::InvalidStreamPosition} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::findSections() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect("define"); - tokenizer.expect("("); - tokenizer.expect("domain"); - - m_name = tokenizer.getIdentifier(); - - tokenizer.expect(")"); - - const auto setSectionPosition = - [&](const std::string §ionName, auto §ionPosition, const auto value, bool unique = false) - { - if (unique && sectionPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(value); - throw tokenize::TokenizerException(tokenizer, "only one “:" + sectionName + "” section allowed"); - } - - sectionPosition = value; - }; - - tokenizer.skipWhiteSpace(); - - // Find sections - while (tokenizer.currentCharacter() != ')') - { - const auto position = tokenizer.position(); - - tokenizer.expect("("); - tokenizer.expect(":"); - - const auto sectionIdentifierPosition = tokenizer.position(); - - // Save the tokenizer position of the individual sections for later parsing - if (tokenizer.testIdentifierAndSkip("requirements")) - setSectionPosition("requirements", m_requirementsPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("types")) - setSectionPosition("types", m_typesPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("constants")) - setSectionPosition("constants", m_constantsPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("predicates")) - setSectionPosition("predicates", m_predicatesPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("action")) - { - m_actionPositions.emplace_back(-1); - setSectionPosition("action", m_actionPositions.back(), position); - } - else if (tokenizer.testIdentifierAndSkip("functions") - || tokenizer.testIdentifierAndSkip("constraints") - || tokenizer.testIdentifierAndSkip("durative-action") - || tokenizer.testIdentifierAndSkip("derived")) - { - tokenizer.seek(sectionIdentifierPosition); - - const auto sectionIdentifier = tokenizer.getIdentifier(); - - m_context.logger.log(output::Priority::Warning, tokenizer, "section type “" + sectionIdentifier + "” currently unsupported"); - - tokenizer.seek(sectionIdentifierPosition); - } - else - { - const auto sectionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - throw tokenize::TokenizerException(tokenizer, "unknown domain section “" + sectionIdentifier + "”"); - } - - // Skip section for now and parse it later - skipSection(tokenizer); - - tokenizer.skipWhiteSpace(); - } - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parse() -{ - auto &tokenizer = m_context.tokenizer; - - if (m_requirementsPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_requirementsPosition); - parseRequirementSection(); - } - - if (m_typesPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_typesPosition); - parseTypeSection(); - } - - if (m_constantsPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_constantsPosition); - parseConstantSection(); - } - - if (m_predicatesPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_predicatesPosition); - parsePredicateSection(); - } - - for (size_t i = 0; i < m_actionPositions.size(); i++) - if (m_actionPositions[i] != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_actionPositions[i]); - parseActionSection(); - } - - computeDerivedRequirements(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::setName(std::string name) -{ - m_name = name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Domain::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Requirements &Domain::requirements() const -{ - return m_requirements; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::PrimitiveTypes &Domain::types() -{ - return m_types; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::PrimitiveTypes &Domain::types() const -{ - return m_types; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::Constants &Domain::constants() -{ - return m_constants; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::Constants &Domain::constants() const -{ - return m_constants; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::PredicateDeclarations &Domain::predicates() -{ - return m_predicates; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::PredicateDeclarations &Domain::predicates() const -{ - return m_predicates; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -std::vector> &Domain::actions() -{ - return m_actions; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::vector> &Domain::actions() const -{ - return m_actions; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::DerivedPredicates &Domain::derivedPredicates() -{ - return m_derivedPredicates; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::DerivedPredicates &Domain::derivedPredicates() const -{ - return m_derivedPredicates; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parseRequirementSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("requirements"); - - while (tokenizer.currentCharacter() != ')') - { - tokenizer.expect(":"); - - m_requirements.emplace_back(Requirement::parse(m_context)); - - tokenizer.skipWhiteSpace(); - } - - // TODO: do this check only once the problem is parsed - // If no requirements are specified, assume STRIPS - if (m_requirements.empty()) - m_requirements.emplace_back(Requirement::Type::STRIPS); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Domain::hasRequirement(Requirement::Type requirementType) const -{ - const auto match = std::find_if(m_requirements.cbegin(), m_requirements.cend(), - [&](const auto &requirement) - { - return requirement.type() == requirementType; - }); - - return match != m_requirements.cend(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::checkRequirement(Requirement::Type requirementType) -{ - if (hasRequirement(requirementType)) - return; - - m_context.logger.log(output::Priority::Warning, m_context.tokenizer, "requirement “" + Requirement(requirementType).toPDDL() + "” used but never declared"); - - m_requirements.push_back(requirementType); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::computeDerivedRequirements() -{ - const auto addRequirementUnique = - [&](const auto requirement) - { - if (hasRequirement(requirement)) - return; - - m_requirements.push_back(Requirement(requirement)); - }; - - if (hasRequirement(Requirement::Type::ADL)) - { - addRequirementUnique(Requirement::Type::STRIPS); - addRequirementUnique(Requirement::Type::Typing); - addRequirementUnique(Requirement::Type::NegativePreconditions); - addRequirementUnique(Requirement::Type::DisjunctivePreconditions); - addRequirementUnique(Requirement::Type::Equality); - addRequirementUnique(Requirement::Type::QuantifiedPreconditions); - addRequirementUnique(Requirement::Type::ConditionalEffects); - } - - if (hasRequirement(Requirement::Type::QuantifiedPreconditions)) - { - addRequirementUnique(Requirement::Type::ExistentialPreconditions); - addRequirementUnique(Requirement::Type::UniversalPreconditions); - } - - if (hasRequirement(Requirement::Type::Fluents)) - { - addRequirementUnique(Requirement::Type::NumericFluents); - addRequirementUnique(Requirement::Type::ObjectFluents); - } - - if (hasRequirement(Requirement::Type::TimedInitialLiterals)) - addRequirementUnique(Requirement::Type::DurativeActions); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parseTypeSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("types"); - - checkRequirement(Requirement::Type::Typing); - - tokenizer.skipWhiteSpace(); - - // Store types and their parent types - while (tokenizer.currentCharacter() != ')') - { - if (tokenizer.currentCharacter() == '(') - throw tokenize::TokenizerException(tokenizer, "only primitive types are allowed in type section"); - - expressions::PrimitiveType::parseTypedDeclaration(m_context, *this); - - tokenizer.skipWhiteSpace(); - } - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parseConstantSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("constants"); - - // Store constants - expressions::Constant::parseTypedDeclarations(m_context, *this); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parsePredicateSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("predicates"); - - tokenizer.skipWhiteSpace(); - - // Store predicates and their arguments - while (tokenizer.currentCharacter() != ')') - { - expressions::PredicateDeclaration::parse(m_context, *this); - - tokenizer.skipWhiteSpace(); - } - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::parseActionSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("action"); - - Action::parseDeclaration(m_context, *this); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::checkConsistency() -{ - // Verify that constants are unique - // Verify that all primitive types are unique - // Check for case-sensitivity issues -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Domain::normalize() -{ - std::for_each(m_predicates.begin(), m_predicates.end(), - [](auto &predicate) - { - predicate->normalizeParameterNames(); - }); - - std::for_each(m_actions.begin(), m_actions.end(), - [&](auto &action) - { - action->normalize(m_derivedPredicates); - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp deleted file mode 100644 index 42dd6f7..0000000 --- a/src/plasp/pddl/Expression.cpp +++ /dev/null @@ -1,336 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Expression -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::copy() -{ - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::normalized() -{ - return reduced()->simplified()->existentiallyQuantified()->simplified(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::reduced() -{ - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::existentiallyQuantified() -{ - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::simplified() -{ - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::decomposed(expressions::DerivedPredicates &) -{ - throw output::TranslatorException("expression cannot be decomposed (not normalized)"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Expression::negated() -{ - if (is()) - return as().argument(); - - auto notExpression = expressions::NotPointer(new expressions::Not); - notExpression->setArgument(this); - - return notExpression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -// TODO: implement better (visitor pattern?) -void Expression::collectParameters(std::set &) -{ - throw output::TranslatorException("expression parameters could not be collected (expression not normalized)"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext); -ExpressionPointer parsePredicate(Context &context, ExpressionContext &expressionContext); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parsePreconditionExpression(Context &context, - ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - ExpressionPointer expression; - - if ((expression = expressions::And::parse(context, expressionContext, parsePreconditionExpression)) - || (expression = expressions::ForAll::parse(context, expressionContext, parsePreconditionExpression))) - { - return expression; - } - - const auto position = tokenizer.position(); - - tokenizer.expect("("); - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("preference")) - { - // TODO: refactor - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(position); - return parseExpression(context, expressionContext); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseExpression(Context &context, ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - ExpressionPointer expression; - - if ((expression = expressions::And::parse(context, expressionContext, parseExpression)) - || (expression = expressions::Or::parse(context, expressionContext, parseExpression)) - || (expression = expressions::Exists::parse(context, expressionContext, parseExpression)) - || (expression = expressions::ForAll::parse(context, expressionContext, parseExpression)) - || (expression = expressions::Not::parse(context, expressionContext, parseExpression)) - || (expression = expressions::Imply::parse(context, expressionContext, parseExpression)) - || (expression = expressions::Predicate::parse(context, expressionContext))) - { - return expression; - } - - const auto position = tokenizer.position(); - - tokenizer.expect("("); - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("-") - || tokenizer.testIdentifierAndSkip("=") - || tokenizer.testIdentifierAndSkip("*") - || tokenizer.testIdentifierAndSkip("+") - || tokenizer.testIdentifierAndSkip("-") - || tokenizer.testIdentifierAndSkip("/") - || tokenizer.testIdentifierAndSkip(">") - || tokenizer.testIdentifierAndSkip("<") - || tokenizer.testIdentifierAndSkip("=") - || tokenizer.testIdentifierAndSkip(">=") - || tokenizer.testIdentifierAndSkip("<=")) - { - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - throw tokenize::TokenizerException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in this context"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseEffectExpression(Context &context, ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - ExpressionPointer expression; - - if ((expression = expressions::And::parse(context, expressionContext, parseEffectExpression)) - || (expression = expressions::ForAll::parse(context, expressionContext, parseEffectExpression)) - || (expression = expressions::When::parse(context, expressionContext, parseExpression, parseConditionalEffectExpression))) - { - return expression; - } - - const auto position = tokenizer.position(); - - tokenizer.expect("("); - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("when")) - { - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(position); - return parseEffectBodyExpression(context, expressionContext); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - ExpressionPointer expression; - - if ((expression = expressions::Not::parse(context, expressionContext, parsePredicate)) - || (expression = expressions::Predicate::parse(context, expressionContext))) - { - return expression; - } - - const auto position = tokenizer.position(); - - tokenizer.expect("("); - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("=") - || tokenizer.testIdentifierAndSkip("assign") - || tokenizer.testIdentifierAndSkip("scale-up") - || tokenizer.testIdentifierAndSkip("scale-down") - || tokenizer.testIdentifierAndSkip("increase") - || tokenizer.testIdentifierAndSkip("decrease")) - { - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - throw tokenize::TokenizerException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in this context"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseConditionalEffectExpression(Context &context, ExpressionContext &expressionContext) -{ - ExpressionPointer expression; - - if ((expression = expressions::And::parse(context, expressionContext, parseEffectBodyExpression))) - return expression; - - return parseEffectBodyExpression(context, expressionContext); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parsePredicate(Context &context, ExpressionContext &expressionContext) -{ - ExpressionPointer expression; - - if ((expression = expressions::Predicate::parse(context, expressionContext))) - return expression; - - throw tokenize::TokenizerException(context.tokenizer, "expected predicate"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseLiteral(Context &context, ExpressionContext &expressionContext) -{ - ExpressionPointer expression; - - if ((expression = parseAtomicFormula(context, expressionContext)) - || (expression = expressions::Not::parse(context, expressionContext, parseAtomicFormula))) - { - return expression; - } - - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer parseAtomicFormula(Context &context, ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - ExpressionPointer expression; - - if ((expression = expressions::Predicate::parse(context, expressionContext))) - return expression; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(")) - return nullptr; - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("=")) - { - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(position); - return nullptr; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/ExpressionContext.cpp b/src/plasp/pddl/ExpressionContext.cpp deleted file mode 100644 index 25b2275..0000000 --- a/src/plasp/pddl/ExpressionContext.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// ExpressionContext -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionContext::ExpressionContext(Domain &domain) -: domain(domain), - problem(nullptr) -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionContext::ExpressionContext(Domain &domain, Problem *problem) -: domain(domain), - problem{problem} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool ExpressionContext::hasRequirement(Requirement::Type requirementType) const -{ - if (problem != nullptr) - return problem->hasRequirement(requirementType); - - return domain.hasRequirement(requirementType); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void ExpressionContext::checkRequirement(Requirement::Type requirementType) const -{ - if (problem != nullptr) - problem->checkRequirement(requirementType); - else - domain.checkRequirement(requirementType); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/InitialState.cpp b/src/plasp/pddl/InitialState.cpp deleted file mode 100644 index 9fc6dd8..0000000 --- a/src/plasp/pddl/InitialState.cpp +++ /dev/null @@ -1,90 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// InitialState -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -std::unique_ptr InitialState::parseDeclaration(Context &context, - ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - auto initialState = std::make_unique(InitialState()); - - const auto parseInitialStateElement = - [&]() -> ExpressionPointer - { - ExpressionPointer expression; - - // TODO: do not allow negative initial state literals - if ((expression = parseLiteral(context, expressionContext)) - || (expression = expressions::At::parse(context, expressionContext, parseLiteral))) - { - return expression; - } - - const auto position = tokenizer.position(); - - tokenizer.expect("("); - - const auto expressionIdentifierPosition = tokenizer.position(); - - if (tokenizer.testIdentifierAndSkip("=")) - { - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - return expressions::Unsupported::parse(context); - } - - tokenizer.seek(expressionIdentifierPosition); - const auto expressionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - throw tokenize::TokenizerException(tokenizer, "expression type “" + expressionIdentifier + "” unknown or not allowed in this context"); - }; - - tokenizer.skipWhiteSpace(); - - while (tokenizer.currentCharacter() != ')') - { - ExpressionPointer expression; - - if ((expression = parseInitialStateElement())) - initialState->m_facts.emplace_back(std::move(expression)); - - tokenizer.skipWhiteSpace(); - } - - return initialState; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expressions &InitialState::facts() const -{ - return m_facts; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Problem.cpp b/src/plasp/pddl/Problem.cpp deleted file mode 100644 index e40a544..0000000 --- a/src/plasp/pddl/Problem.cpp +++ /dev/null @@ -1,408 +0,0 @@ -#include - -#include - -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Problem -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Problem::Problem(Context &context, Domain &domain) -: m_context(context), - m_domain(domain), - m_domainPosition{tokenize::InvalidStreamPosition}, - m_requirementsPosition{tokenize::InvalidStreamPosition}, - m_objectsPosition{tokenize::InvalidStreamPosition}, - m_initialStatePosition{tokenize::InvalidStreamPosition}, - m_goalPosition{tokenize::InvalidStreamPosition} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::findSections() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect("define"); - tokenizer.expect("("); - tokenizer.expect("problem"); - - m_name = tokenizer.getIdentifier(); - - tokenizer.expect(")"); - - const auto setSectionPosition = - [&](const std::string §ionName, auto §ionPosition, const auto value, bool unique = false) - { - if (unique && sectionPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(value); - throw tokenize::TokenizerException(tokenizer, "only one “:" + sectionName + "” section allowed"); - } - - sectionPosition = value; - }; - - tokenizer.skipWhiteSpace(); - - while (tokenizer.currentCharacter() != ')') - { - const auto position = tokenizer.position(); - - tokenizer.expect("("); - tokenizer.expect(":"); - - const auto sectionIdentifierPosition = tokenizer.position(); - - // TODO: check order of the sections - if (tokenizer.testIdentifierAndSkip("domain")) - setSectionPosition("domain", m_domainPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("requirements")) - setSectionPosition("requirements", m_requirementsPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("objects")) - setSectionPosition("objects", m_objectsPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("init")) - setSectionPosition("init", m_initialStatePosition, position, true); - else if (tokenizer.testIdentifierAndSkip("goal")) - setSectionPosition("goal", m_goalPosition, position, true); - else if (tokenizer.testIdentifierAndSkip("constraints") - || tokenizer.testIdentifierAndSkip("metric") - || tokenizer.testIdentifierAndSkip("length")) - { - tokenizer.seek(sectionIdentifierPosition); - - const auto sectionIdentifier = tokenizer.getIdentifier(); - - m_context.logger.log(output::Priority::Warning, tokenizer, "section type “" + sectionIdentifier + "” currently unsupported"); - - tokenizer.seek(sectionIdentifierPosition); - } - else - { - const auto sectionIdentifier = tokenizer.getIdentifier(); - - tokenizer.seek(position); - throw tokenize::TokenizerException(tokenizer, "unknown problem section “" + sectionIdentifier + "”"); - } - - // Skip section for now and parse it later - skipSection(tokenizer); - - tokenizer.skipWhiteSpace(); - } - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parse() -{ - auto &tokenizer = m_context.tokenizer; - - if (m_domainPosition == tokenize::InvalidStreamPosition) - throw ConsistencyException("problem description does not specify the corresponding domain"); - - tokenizer.seek(m_domainPosition); - parseDomainSection(); - - if (m_requirementsPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_requirementsPosition); - parseRequirementSection(); - } - - if (m_objectsPosition != tokenize::InvalidStreamPosition) - { - tokenizer.seek(m_objectsPosition); - parseObjectSection(); - } - - if (m_initialStatePosition == tokenize::InvalidStreamPosition) - throw ConsistencyException("problem description does not specify an initial state"); - - tokenizer.seek(m_initialStatePosition); - parseInitialStateSection(); - - if (m_goalPosition == tokenize::InvalidStreamPosition) - throw ConsistencyException("problem description does not specify a goal"); - - tokenizer.seek(m_goalPosition); - parseGoalSection(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Domain &Problem::domain() -{ - return m_domain; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Domain &Problem::domain() const -{ - return m_domain; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Problem::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Requirements &Problem::requirements() const -{ - return m_requirements; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::Constants &Problem::objects() -{ - return m_objects; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const expressions::Constants &Problem::objects() const -{ - return m_objects; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parseDomainSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("domain"); - - tokenizer.skipWhiteSpace(); - - const auto domainName = tokenizer.getIdentifier(); - - if (m_domain.name() != domainName) - throw tokenize::TokenizerException(tokenizer, "domains do not match (“" + m_domain.name() + "” and “" + domainName + "”)"); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parseRequirementSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("requirements"); - - tokenizer.skipWhiteSpace(); - - while (tokenizer.currentCharacter() != ')') - { - tokenizer.expect(":"); - - m_requirements.emplace_back(Requirement::parse(m_context)); - - tokenizer.skipWhiteSpace(); - } - - // TODO: do this check only once the domain is parsed - // If no requirements are specified, assume STRIPS - if (m_requirements.empty()) - m_requirements.emplace_back(Requirement::Type::STRIPS); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Problem::hasRequirement(Requirement::Type requirementType) const -{ - const auto match = std::find_if(m_requirements.cbegin(), m_requirements.cend(), - [&](const auto &requirement) - { - return requirement.type() == requirementType; - }); - - if (match != m_requirements.cend()) - return true; - - return m_domain.hasRequirement(requirementType); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::checkRequirement(Requirement::Type requirementType) -{ - if (hasRequirement(requirementType)) - return; - - m_context.logger.log(output::Priority::Warning, m_context.tokenizer, "requirement “" + Requirement(requirementType).toPDDL() + "” used but never declared"); - - m_requirements.push_back(requirementType); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::computeDerivedRequirements() -{ - const auto addRequirementUnique = - [&](const auto requirement) - { - if (hasRequirement(requirement)) - return; - - m_requirements.push_back(Requirement(requirement)); - }; - - if (hasRequirement(Requirement::Type::ADL)) - { - addRequirementUnique(Requirement::Type::STRIPS); - addRequirementUnique(Requirement::Type::Typing); - addRequirementUnique(Requirement::Type::NegativePreconditions); - addRequirementUnique(Requirement::Type::DisjunctivePreconditions); - addRequirementUnique(Requirement::Type::Equality); - addRequirementUnique(Requirement::Type::QuantifiedPreconditions); - addRequirementUnique(Requirement::Type::ConditionalEffects); - } - - if (hasRequirement(Requirement::Type::QuantifiedPreconditions)) - { - addRequirementUnique(Requirement::Type::ExistentialPreconditions); - addRequirementUnique(Requirement::Type::UniversalPreconditions); - } - - if (hasRequirement(Requirement::Type::Fluents)) - { - addRequirementUnique(Requirement::Type::NumericFluents); - addRequirementUnique(Requirement::Type::ObjectFluents); - } - - if (hasRequirement(Requirement::Type::TimedInitialLiterals)) - addRequirementUnique(Requirement::Type::DurativeActions); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parseObjectSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("objects"); - - tokenizer.skipWhiteSpace(); - - // Store constants - expressions::Constant::parseTypedDeclarations(m_context, *this); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parseInitialStateSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("init"); - - ExpressionContext expressionContext(m_domain, this); - - m_initialState = InitialState::parseDeclaration(m_context, expressionContext); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::parseGoalSection() -{ - auto &tokenizer = m_context.tokenizer; - - tokenizer.expect("("); - tokenizer.expect(":"); - tokenizer.expect("goal"); - - ExpressionContext expressionContext(m_domain, this); - - m_goal = parsePreconditionExpression(m_context, expressionContext); - - tokenizer.expect(")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -InitialState &Problem::initialState() -{ - BOOST_ASSERT(m_initialState); - - return *m_initialState; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const InitialState &Problem::initialState() const -{ - BOOST_ASSERT(m_initialState); - - return *m_initialState; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expression &Problem::goal() const -{ - BOOST_ASSERT(m_goal); - - return *m_goal; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::checkConsistency() -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Problem::normalize() -{ - BOOST_ASSERT(m_goal); - - // TODO: normalize objects and initial state - - m_goal = m_goal->normalized(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/Requirement.cpp b/src/plasp/pddl/Requirement.cpp deleted file mode 100644 index e26ea35..0000000 --- a/src/plasp/pddl/Requirement.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include - -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Requirement -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -using RequirementTypeNames = boost::bimap; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const RequirementTypeNames requirementTypesToPDDL = boost::assign::list_of - (Requirement::Type::STRIPS, "strips") - (Requirement::Type::Typing, "typing") - (Requirement::Type::NegativePreconditions, "negative-preconditions") - (Requirement::Type::DisjunctivePreconditions, "disjunctive-preconditions") - (Requirement::Type::Equality, "equality") - (Requirement::Type::ExistentialPreconditions, "existential-preconditions") - (Requirement::Type::UniversalPreconditions, "universal-preconditions") - (Requirement::Type::QuantifiedPreconditions, "quantified-preconditions") - (Requirement::Type::ConditionalEffects, "conditional-effects") - (Requirement::Type::Fluents, "fluents") - (Requirement::Type::NumericFluents, "numeric-fluents") - (Requirement::Type::ObjectFluents, "object-fluents") - (Requirement::Type::ADL, "adl") - (Requirement::Type::DurativeActions, "durative-actions") - (Requirement::Type::DurationInequalities, "duration-inequalities") - (Requirement::Type::ContinuousEffects, "continuous-effects") - (Requirement::Type::DerivedPredicates, "derived-predicates") - (Requirement::Type::TimedInitialLiterals, "timed-initial-literals") - (Requirement::Type::Preferences, "preferences") - (Requirement::Type::Constraints, "constraints") - (Requirement::Type::ActionCosts, "action-costs") - (Requirement::Type::GoalUtilities, "goal-utilities"); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const RequirementTypeNames requirementTypesToASP = boost::assign::list_of - (Requirement::Type::STRIPS, "strips") - (Requirement::Type::Typing, "typing") - (Requirement::Type::NegativePreconditions, "negativePreconditions") - (Requirement::Type::DisjunctivePreconditions, "disjunctivePreconditions") - (Requirement::Type::Equality, "equality") - (Requirement::Type::ExistentialPreconditions, "existentialPreconditions") - (Requirement::Type::UniversalPreconditions, "universalPreconditions") - (Requirement::Type::QuantifiedPreconditions, "quantifiedPreconditions") - (Requirement::Type::ConditionalEffects, "conditionalEffects") - (Requirement::Type::Fluents, "fluents") - (Requirement::Type::NumericFluents, "numericFluents") - (Requirement::Type::ObjectFluents, "objectFluents") - (Requirement::Type::ADL, "adl") - (Requirement::Type::DurativeActions, "durativeActions") - (Requirement::Type::DurationInequalities, "durationInequalities") - (Requirement::Type::ContinuousEffects, "continuousEffects") - (Requirement::Type::DerivedPredicates, "derivedPredicates") - (Requirement::Type::TimedInitialLiterals, "timedInitialLiterals") - (Requirement::Type::Preferences, "preferences") - (Requirement::Type::Constraints, "constraints") - (Requirement::Type::ActionCosts, "actionCosts") - (Requirement::Type::GoalUtilities, "goalUtilities"); - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Requirement::Requirement(Requirement::Type type) -: m_type{type} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Requirement Requirement::parse(Context &context) -{ - auto &tokenizer = context.tokenizer; - - const auto requirementName = tokenizer.getIdentifier(); - - const auto match = requirementTypesToPDDL.right.find(requirementName); - - if (match == requirementTypesToPDDL.right.end()) - throw tokenize::TokenizerException(tokenizer, "unknown PDDL requirement “" + requirementName + "”"); - - const auto requirementType = match->second; - - if (requirementType == Requirement::Type::GoalUtilities) - context.logger.log(output::Priority::Warning, tokenizer, "requirement “goal-utilities” is not part of the PDDL 3.1 specification"); - - return Requirement(match->second); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Requirement::Type Requirement::type() const -{ - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -std::string Requirement::toPDDL() const -{ - const auto match = requirementTypesToPDDL.left.find(m_type); - - BOOST_ASSERT(match != requirementTypesToPDDL.left.end()); - - return match->second; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -std::string Requirement::toASP() const -{ - const auto match = requirementTypesToASP.left.find(m_type); - - BOOST_ASSERT(match != requirementTypesToASP.left.end()); - - return match->second; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/TranslatorASP.cpp b/src/plasp/pddl/TranslatorASP.cpp index 1fab18b..2896fd8 100644 --- a/src/plasp/pddl/TranslatorASP.cpp +++ b/src/plasp/pddl/TranslatorASP.cpp @@ -2,11 +2,13 @@ #include +#include + #include #include -#include -#include -#include + +#include +#include #include #include #include @@ -23,11 +25,10 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -TranslatorASP::TranslatorASP(Description &description, output::ColorStream &outputStream) -: m_description(description), +TranslatorASP::TranslatorASP(const ::pddl::ast::Description &description, output::ColorStream &outputStream) +: m_description{description}, m_outputStream(outputStream) { - m_description.normalize(); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -36,7 +37,7 @@ void TranslatorASP::translate() const { translateDomain(); - if (m_description.containsProblem()) + if (m_description.problem) { m_outputStream << std::endl; translateProblem(); @@ -49,35 +50,28 @@ void TranslatorASP::translateDomain() const { m_outputStream << output::Heading1("domain"); - const auto &domain = m_description.domain(); + const auto &domain = m_description.domain; // Types m_outputStream << std::endl; translateTypes(); // Constants - if (!domain.constants().empty()) + if (!domain->constants.empty()) { m_outputStream << std::endl; - translateConstants("constants", domain.constants()); + translateConstants("constants", domain->constants); } // Predicates - if (!domain.predicates().empty()) + if (!domain->predicates.empty()) { m_outputStream << std::endl; translatePredicates(); } - // Derived predicates - if (!domain.derivedPredicates().empty()) - { - m_outputStream << std::endl; - translateDerivedPredicates(); - } - // Actions - if (!domain.actions().empty()) + if (!domain->actions.empty()) { m_outputStream << std::endl; translateActions(); @@ -92,7 +86,7 @@ void TranslatorASP::translateTypes() const m_outputStream << std::endl; - const auto &types = m_description.domain().types(); + const auto &types = m_description.domain->types; if (types.empty()) { @@ -108,18 +102,18 @@ void TranslatorASP::translateTypes() const m_outputStream << output::Function("type") << "(" << output::Keyword("type") << "(" - << output::String(type->name().c_str()) + << *type << "))." << std::endl; - const auto &parentTypes = type->parentTypes(); + const auto &parentTypes = type->parentTypes; std::for_each(parentTypes.cbegin(), parentTypes.cend(), [&](const auto &parentType) { m_outputStream << output::Function("inherits") << "(" << output::Keyword("type") - << "(" << output::String(type->name().c_str()) << "), " << output::Keyword("type") - << "(" << output::String(parentType->name().c_str()) << "))." << std::endl; + << "(" << *type << "), " << output::Keyword("type") + << "(" << *parentType << "))." << std::endl; }); } @@ -143,17 +137,17 @@ void TranslatorASP::translatePredicates() const { m_outputStream << output::Heading2("variables"); - const auto &predicates = m_description.domain().predicates(); + const auto &predicates = m_description.domain->predicates; for (const auto &predicate : predicates) { m_outputStream << std::endl << output::Function("variable") << "("; - translation::printPredicateName(m_outputStream, *predicate); + translatePredicateDeclaration(m_outputStream, *predicate); m_outputStream << ")"; - translation::translateVariablesForRuleBody(m_outputStream, predicate->parameters()); + translateVariablesForRuleBody(m_outputStream, predicate->parameters); m_outputStream << "."; } @@ -173,160 +167,47 @@ void TranslatorASP::translatePredicates() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void TranslatorASP::translateDerivedPredicates() const -{ - m_outputStream << output::Heading2("derived predicates"); - - const auto &derivedPredicates = m_description.domain().derivedPredicates(); - - for (const auto &derivedPredicate : derivedPredicates) - { - const auto printObjectName = - [&](auto &outputStream) - { - translation::printDerivedPredicateName(outputStream, *derivedPredicate); - }; - - m_outputStream << std::endl << output::Function("derivedVariable") << "("; - - printObjectName(m_outputStream); - - m_outputStream << ")"; - - translation::translateVariablesForRuleBody(m_outputStream, derivedPredicate->parameters()); - - m_outputStream << "."; - - translation::translatePreconditionDisjunction(m_outputStream, "derivedVariable", printObjectName, derivedPredicate->preconditions()); - - m_outputStream << std::endl; - } - - m_outputStream - << std::endl - << output::Function("contains") << "(" - << output::Keyword("derivedVariable") << "(" << output::Variable("X") << "), " - << output::Keyword("value") << "(" << output::Variable("X") << ", " << output::Variable("B") << ")) :- " - << output::Function("derivedVariable") << "(" << output::Keyword("derivedVariable") << "(" << output::Variable("X") << ")), " - << output::Function("boolean") << "(" << output::Variable("B") << ")." - << std::endl; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - void TranslatorASP::translateActions() const { m_outputStream << output::Heading2("actions"); - const auto &actions = m_description.domain().actions(); - - const auto printActionName = - [&](const auto &action) - { - m_outputStream << output::Keyword("action") << "("; - - if (action.parameters().empty()) - { - m_outputStream << output::String(action.name().c_str()) << ")"; - - return; - } - - m_outputStream << "(" << output::String(action.name().c_str()); - translation::translateVariablesForRuleHead(m_outputStream, action.parameters()); - m_outputStream << "))"; - }; + const auto &actions = m_description.domain->actions; for (const auto &action : actions) { - const auto translateExpression = - [&](const auto &ruleHead, const auto &literal, bool enumerateEffects = false) + const auto printActionName = + [&]() { - m_outputStream << std::endl << output::Function(ruleHead) << "("; + m_outputStream << output::Keyword("action") << "("; - printActionName(*action); + if (action->parameters.empty()) + { + m_outputStream << *action << ")"; + return; + } - // TODO: implement conditional effects - if (enumerateEffects) - m_outputStream << ", " << output::Keyword("effect") << "(" << output::Reserved("unconditional") << ")"; - - m_outputStream << ", "; - - translation::translateLiteral(m_outputStream, literal); - - m_outputStream << ") :- " << output::Function("action") << "("; - - printActionName(*action); - - m_outputStream << ")."; + m_outputStream << "(" << *action; + translateVariablesForRuleHead(m_outputStream, action->parameters); + m_outputStream << "))"; }; m_outputStream << std::endl; // Name m_outputStream << output::Function("action") << "("; - printActionName(*action); + printActionName(); m_outputStream << ")"; - translation::translateVariablesForRuleBody(m_outputStream, action->parameters()); + translateVariablesForRuleBody(m_outputStream, action->parameters); m_outputStream << "."; // Precondition - if (action->precondition()) - { - const auto &precondition = *action->precondition(); + if (action->precondition) + translatePrecondition(m_outputStream, action->precondition.value(), "action", printActionName); - switch (precondition.expressionType()) - { - case Expression::Type::And: - { - const auto &andExpression = precondition.as(); - - std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto argument) - { - translateExpression("precondition", *argument); - }); - - break; - } - case Expression::Type::Predicate: - case Expression::Type::Not: - case Expression::Type::DerivedPredicate: - { - translateExpression("precondition", precondition); - break; - } - default: - throw output::TranslatorException("only “and” expressions and (negated) predicates supported as action preconditions currently (" + std::to_string((int)precondition.expressionType()) + ")"); - } - } - - // Effect - if (action->effect()) - { - const auto &effect = *action->effect(); - - if (effect.is() || effect.is()) - { - translateExpression("postcondition", effect, true); - } - // Assuming a conjunction - else - { - if (effect.expressionType() != Expression::Type::And) - throw output::TranslatorException("only “and” expressions and (negated) predicates supported as action effects currently"); - - const auto &andExpression = effect.as(); - - std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto argument) - { - translateExpression("postcondition", *argument, true); - }); - } - } + if (action->effect) + translateEffect(m_outputStream, action->effect.value(), "action", printActionName); m_outputStream << std::endl; } @@ -334,7 +215,7 @@ void TranslatorASP::translateActions() const //////////////////////////////////////////////////////////////////////////////////////////////////// -void TranslatorASP::translateConstants(const std::string &heading, const expressions::Constants &constants) const +void TranslatorASP::translateConstants(const std::string &heading, const ::pddl::ast::ConstantDeclarations &constants) const { m_outputStream << output::Heading2(heading.c_str()); @@ -343,21 +224,26 @@ void TranslatorASP::translateConstants(const std::string &heading, const express m_outputStream << std::endl << output::Function("constant") << "(" << output::Keyword("constant") << "(" - << output::String(constant->name().c_str()) + << *constant << "))." << std::endl; - const auto type = constant->type(); + const auto &type = constant->type; - if (type != nullptr) + if (type) { + if (!type.value().is<::pddl::ast::PrimitiveTypePointer>()) + throw output::TranslatorException("only primitive types supported currently"); + + const auto &primitveType = type.value().get<::pddl::ast::PrimitiveTypePointer>(); + m_outputStream << output::Function("has") << "(" - << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " - << output::Keyword("type") << "(" << output::String(type->name().c_str()) << "))." << std::endl; + << output::Keyword("constant") << "(" << *constant << "), " + << output::Keyword("type") << "(" << *primitveType << "))." << std::endl; } else { m_outputStream << output::Function("has") << "(" - << output::Keyword("constant") << "(" << output::String(constant->name().c_str()) << "), " + << output::Keyword("constant") << "(" << *constant << "), " << output::Keyword("type") << "(" << output::String("object") << "))." << std::endl; } } @@ -367,17 +253,17 @@ void TranslatorASP::translateConstants(const std::string &heading, const express void TranslatorASP::translateProblem() const { - BOOST_ASSERT(m_description.containsProblem()); + assert(m_description.problem); m_outputStream << output::Heading1("problem"); - const auto &problem = m_description.problem(); + const auto &problem = m_description.problem.value(); // Objects - if (!problem.objects().empty()) + if (!problem->objects.empty()) { m_outputStream << std::endl; - translateConstants("objects", problem.objects()); + translateConstants("objects", problem->objects); } // Initial State @@ -385,42 +271,45 @@ void TranslatorASP::translateProblem() const translateInitialState(); // Goal - m_outputStream << std::endl; - translateGoal(); + if (problem->goal) + { + m_outputStream << std::endl; + translateGoal(); + } } //////////////////////////////////////////////////////////////////////////////////////////////////// void TranslatorASP::translateInitialState() const { - BOOST_ASSERT(m_description.containsProblem()); + assert(m_description.problem); m_outputStream << output::Heading2("initial state"); - const auto &initialStateFacts = m_description.problem().initialState().facts(); + const auto &facts = m_description.problem.value()->initialState.facts; - for (const auto &fact : initialStateFacts) + for (const auto &fact : facts) { m_outputStream << std::endl << output::Function("initialState") << "("; // Translate single predicate - if (fact->is()) + if (fact.is<::pddl::ast::AtomicFormula>() && fact.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) { - const auto &predicate = fact->as(); + const auto &predicate = fact.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); - m_outputStream << output::Keyword("variable") << "("; - translation::translatePredicate(m_outputStream, predicate); - m_outputStream << "), " << output::Keyword("value") << "("; - translation::translatePredicate(m_outputStream, predicate); - m_outputStream << ", " << output::Boolean("true") << ")"; + translatePredicateToVariable(m_outputStream, *predicate, true); } // Assuming that "not" expression may only contain a predicate - else if (fact->is()) + else if (fact.is<::pddl::ast::NotPointer<::pddl::ast::Fact>>()) { - const auto ¬Expression = fact->as(); + const auto ¬Expression = fact.get<::pddl::ast::NotPointer<::pddl::ast::Fact>>(); - if (notExpression.argument()->expressionType() != Expression::Type::Predicate) + if (!notExpression->argument.is<::pddl::ast::AtomicFormula>() || !notExpression->argument.get<::pddl::ast::AtomicFormula>().is<::pddl::ast::PredicatePointer>()) throw output::TranslatorException("only negations of simple predicates supported in initial state currently"); + + const auto &predicate = notExpression->argument.get<::pddl::ast::AtomicFormula>().get<::pddl::ast::PredicatePointer>(); + + translatePredicateToVariable(m_outputStream, *predicate, false); } else throw output::TranslatorException("only predicates and their negations supported in initial state currently"); @@ -445,36 +334,14 @@ void TranslatorASP::translateInitialState() const void TranslatorASP::translateGoal() const { - BOOST_ASSERT(m_description.containsProblem()); + assert(m_description.problem); + assert(m_description.problem.value()->goal); m_outputStream << output::Heading2("goal"); - const auto &goal = m_description.problem().goal(); - - if (goal.is() || goal.is()) - { - m_outputStream << std::endl << output::Function("goal") << "("; - - translation::translateLiteral(m_outputStream, goal); - - m_outputStream << ")."; - } - else if (goal.is()) - { - const auto &andExpression = goal.as(); - - std::for_each(andExpression.arguments().cbegin(), andExpression.arguments().cend(), - [&](const auto argument) - { - m_outputStream << std::endl << output::Function("goal") << "("; - - translation::translateLiteral(m_outputStream, *argument); - - m_outputStream << ")."; - }); - } - else - throw output::TranslatorException("only single predicates, their negations, and conjunctions are currently supported in the goal"); + const auto &goal = m_description.problem.value()->goal.value(); + // TODO: refactor + ::plasp::pddl::translateGoal(m_outputStream, goal); m_outputStream << std::endl; } diff --git a/src/plasp/pddl/VariableStack.cpp b/src/plasp/pddl/VariableStack.cpp deleted file mode 100644 index aedb4ff..0000000 --- a/src/plasp/pddl/VariableStack.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include - -#include - -namespace plasp -{ -namespace pddl -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// VariableStack -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void VariableStack::push(expressions::Variables *variables) -{ - m_variableStack.push_back(variables); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void VariableStack::pop() -{ - m_variableStack.pop_back(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -expressions::VariablePointer VariableStack::parseAndFind(plasp::pddl::Context &context) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - tokenizer.expect("?"); - - const auto variableName = tokenizer.getIdentifier(); - - for (auto i = m_variableStack.crbegin(); i != m_variableStack.crend(); i++) - { - const auto *variables = *i; - - BOOST_ASSERT(variables); - - const auto match = std::find_if(variables->crbegin(), variables->crend(), - [&](const auto &variable) - { - return variable->name() == variableName; - }); - - if (match != variables->crend()) - return match->get(); - } - - throw tokenize::TokenizerException(tokenizer, "variable “" + variableName + "” used but never declared"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} diff --git a/src/plasp/pddl/expressions/And.cpp b/src/plasp/pddl/expressions/And.cpp deleted file mode 100644 index df7dfcf..0000000 --- a/src/plasp/pddl/expressions/And.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include - -#include -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// And -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string And::Identifier = "and"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer And::decomposed(DerivedPredicates &derivedPredicates) -{ - BOOST_ASSERT(!m_arguments.empty()); - - derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size())); - auto derivedPredicate = derivedPredicates.back(); - - for (auto &argument : m_arguments) - argument = argument->decomposed(derivedPredicates); - - std::vector preconditions; - preconditions.emplace_back(std::move(m_arguments)); - - // Move this expression’s arguments to the derived predicate - derivedPredicate->setPreconditions(std::move(preconditions)); - - return derivedPredicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/At.cpp b/src/plasp/pddl/expressions/At.cpp deleted file mode 100644 index c3d1f84..0000000 --- a/src/plasp/pddl/expressions/At.cpp +++ /dev/null @@ -1,99 +0,0 @@ -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// At -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -At::At() -: m_argument{nullptr} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer At::copy() -{ - auto result = new At; - - result->m_argument = m_argument->copy(); - - return result; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void At::setArgument(ExpressionPointer argument) -{ - m_argument = argument; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer At::argument() const -{ - return m_argument; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer At::reduced() -{ - throw output::TranslatorException("reducing “at” predicates currently not supported"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer At::existentiallyQuantified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->existentiallyQuantified(); - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer At::simplified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->simplified(); - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void At::collectParameters(std::set ¶meters) -{ - m_argument->collectParameters(parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void At::print(std::ostream &ostream) const -{ - ostream << "(at " << m_timePoint << " "; - - m_argument->print(ostream); - - ostream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Constant.cpp b/src/plasp/pddl/expressions/Constant.cpp deleted file mode 100644 index 2ac99cc..0000000 --- a/src/plasp/pddl/expressions/Constant.cpp +++ /dev/null @@ -1,253 +0,0 @@ -#include - -#include - -#include - -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Constant -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Constant::Constant() -: m_isDirty{false}, - m_type{nullptr} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ConstantPointer Constant::parseDeclaration(Context &context) -{ - context.tokenizer.skipWhiteSpace(); - - auto constant = ConstantPointer(new Constant); - - constant->m_name = context.tokenizer.getIdentifier(); - - BOOST_ASSERT(constant->m_name != "-"); - - // Flag constant for potentially upcoming type declaration - constant->setDirty(); - - return constant; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::parseTypedDeclaration(Context &context, Domain &domain) -{ - parseTypedDeclaration(context, domain, domain.constants()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::parseTypedDeclaration(Context &context, Problem &problem) -{ - parseTypedDeclaration(context, problem.domain(), problem.objects()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::parseTypedDeclaration(Context &context, Domain &domain, Constants &constants) -{ - // Parse and store constant - constants.emplace_back(parseDeclaration(context)); - - context.tokenizer.skipWhiteSpace(); - - // Check for typing information - if (!context.tokenizer.testAndSkip('-')) - return; - - // If existing, parse and store parent type - auto type = PrimitiveType::parseAndFind(context, domain); - - // Assign parent type to all types that were previously flagged - std::for_each(constants.begin(), constants.end(), - [&](auto &constant) - { - if (!constant->isDirty()) - return; - - constant->setType(type); - constant->setDirty(false); - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::parseTypedDeclarations(Context &context, Domain &domain) -{ - auto &tokenizer = context.tokenizer; - - while (tokenizer.currentCharacter() != ')') - { - parseTypedDeclaration(context, domain); - - tokenizer.skipWhiteSpace(); - } - - if (domain.constants().empty()) - return; - - // Check correct use of typing requirement - const auto typingUsed = (domain.constants().back()->type() != nullptr); - - // If types are given, check that typing is a requirement - if (typingUsed) - domain.checkRequirement(Requirement::Type::Typing); - // If no types are given, check that typing is not a requirement - else if (domain.hasRequirement(Requirement::Type::Typing)) - throw tokenize::TokenizerException(tokenizer, "constant has undeclared type"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::parseTypedDeclarations(Context &context, Problem &problem) -{ - auto &tokenizer = context.tokenizer; - - while (tokenizer.currentCharacter() != ')') - { - parseTypedDeclaration(context, problem); - - tokenizer.skipWhiteSpace(); - } - - if (problem.objects().empty()) - return; - - // Check correct use of typing requirement - const auto typingUsed = (problem.objects().back()->type() != nullptr); - - // If types are given, check that typing is a requirement - if (typingUsed) - problem.checkRequirement(Requirement::Type::Typing); - // If no types are given, check that typing is not a requirement - else if (problem.hasRequirement(Requirement::Type::Typing)) - throw tokenize::TokenizerException(tokenizer, "constant has undeclared type"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ConstantPointer Constant::parseAndFind(Context &context, const Domain &domain) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - const auto constantName = tokenizer.getIdentifier(); - - auto constant = parseAndFind(constantName, domain.constants()); - - if (constant != nullptr) - return constant; - - throw tokenize::TokenizerException(tokenizer, "constant “" + constantName + "” used but never declared"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ConstantPointer Constant::parseAndFind(Context &context, const Problem &problem) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - const auto constantName = tokenizer.getIdentifier(); - - auto constant = parseAndFind(constantName, problem.domain().constants()); - - if (constant) - return constant; - - constant = parseAndFind(constantName, problem.objects()); - - if (constant) - return constant; - - throw tokenize::TokenizerException(tokenizer, "constant “" + constantName + "” used but never declared"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ConstantPointer Constant::parseAndFind(const std::string &constantName, const Constants &constants) -{ - 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) - return nullptr; - - return match->get(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::setDirty(bool isDirty) -{ - m_isDirty = isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Constant::isDirty() const -{ - return m_isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Constant::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::setType(PrimitiveTypePointer type) -{ - m_type = type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveTypePointer Constant::type() const -{ - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Constant::print(std::ostream &ostream) const -{ - ostream << "(" << m_name << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/DerivedPredicate.cpp b/src/plasp/pddl/expressions/DerivedPredicate.cpp deleted file mode 100644 index 02e7e63..0000000 --- a/src/plasp/pddl/expressions/DerivedPredicate.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// DerivedPredicate -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -DerivedPredicate::DerivedPredicate(size_t id) -: m_id{id} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -size_t DerivedPredicate::id() const -{ - return m_id; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void DerivedPredicate::setPreconditions(std::vector &&preconditions) -{ - m_preconditions = std::move(preconditions); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::vector &DerivedPredicate::preconditions() const -{ - return m_preconditions; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void DerivedPredicate::collectParameters() -{ - for (const auto &conjunction : m_preconditions) - for (const auto &precondition : conjunction) - precondition->collectParameters(m_parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::set &DerivedPredicate::parameters() const -{ - return m_parameters; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void DerivedPredicate::collectParameters(std::set ¶meters) -{ - for (auto &conjunction : m_preconditions) - for (auto &precondition : conjunction) - { - BOOST_ASSERT(precondition.get() != this); - precondition->collectParameters(m_parameters); - } - - // Copy in order not to interfere with potentially bound variables in parent expressions - parameters = m_parameters; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void DerivedPredicate::print(std::ostream &ostream) const -{ - ostream << "(:derived " << m_id << " "; - - BOOST_ASSERT(m_preconditions.size() > 0); - - const auto printConjunction = - [&ostream](const auto &conjunction) - { - if (conjunction.size() == 0) - { - conjunction.front()->print(ostream); - return; - } - - ostream << "(and"; - - for (const auto &precondition : conjunction) - { - ostream << " "; - precondition->print(ostream); - } - - ostream << ")"; - }; - - if (m_preconditions.size() == 1) - { - const auto &conjunction = m_preconditions.front(); - - BOOST_ASSERT(conjunction.size() > 0); - - printConjunction(conjunction); - - ostream << ")"; - - return; - } - - ostream << " (or"; - - for (const auto conjunction : m_preconditions) - { - ostream << " "; - - printConjunction(conjunction); - } - - ostream << "))"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Dummy.cpp b/src/plasp/pddl/expressions/Dummy.cpp deleted file mode 100644 index fec67d5..0000000 --- a/src/plasp/pddl/expressions/Dummy.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Dummy -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Dummy::Dummy(std::string name) -: m_name{name} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Dummy::print(std::ostream &ostream) const -{ - ostream << "(" << m_name << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Either.cpp b/src/plasp/pddl/expressions/Either.cpp deleted file mode 100644 index 8f795ec..0000000 --- a/src/plasp/pddl/expressions/Either.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Either -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string Either::Identifier = "either"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Exists.cpp b/src/plasp/pddl/expressions/Exists.cpp deleted file mode 100644 index 5169f5e..0000000 --- a/src/plasp/pddl/expressions/Exists.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Exists -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string Exists::Identifier = "exists"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/ForAll.cpp b/src/plasp/pddl/expressions/ForAll.cpp deleted file mode 100644 index 53fa1fc..0000000 --- a/src/plasp/pddl/expressions/ForAll.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// ForAll -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string ForAll::Identifier = "forall"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer ForAll::existentiallyQuantified() -{ - auto existsExpression = ExistsPointer(new Exists()); - auto notExpression = NotPointer(new Not()); - - notExpression->setArgument(m_argument->existentiallyQuantified()); - existsExpression->setArgument(notExpression); - existsExpression->variables() = std::move(m_variables); - - return existsExpression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Imply.cpp b/src/plasp/pddl/expressions/Imply.cpp deleted file mode 100644 index 0e55591..0000000 --- a/src/plasp/pddl/expressions/Imply.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include - -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Imply -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -// TODO: make static character string literal -const std::string Imply::Identifier = "imply"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Imply::reduced() -{ - BOOST_ASSERT(m_arguments[0]); - BOOST_ASSERT(m_arguments[1]); - - m_arguments[0] = m_arguments[0]->reduced(); - m_arguments[1] = m_arguments[1]->reduced(); - - auto notArgument0 = NotPointer(new Not); - notArgument0->setArgument(m_arguments[0]); - - auto orExpression = OrPointer(new Or); - orExpression->addArgument(notArgument0); - orExpression->addArgument(m_arguments[1]); - - return orExpression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Not.cpp b/src/plasp/pddl/expressions/Not.cpp deleted file mode 100644 index 126ce05..0000000 --- a/src/plasp/pddl/expressions/Not.cpp +++ /dev/null @@ -1,134 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Not -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Not::Not() -: m_argument{nullptr} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::copy() -{ - auto result = new Not; - - result->m_argument = m_argument->copy(); - - return result; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Not::setArgument(ExpressionPointer argument) -{ - m_argument = argument; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::argument() const -{ - return m_argument; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::reduced() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->reduced(); - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::existentiallyQuantified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->existentiallyQuantified(); - - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::simplified() -{ - BOOST_ASSERT(m_argument); - - m_argument = m_argument->simplified(); - - if (!m_argument->is()) - return this; - - // Remove double negations - const auto ¬Expression = m_argument->as(); - - return notExpression.argument(); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Not::collectParameters(std::set ¶meters) -{ - m_argument->collectParameters(parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Not::decomposed(DerivedPredicates &derivedPredicates) -{ - m_argument = m_argument->decomposed(derivedPredicates); - - // Predicates and derived predicates can be directly negated - if (m_argument->is() || m_argument->is()) - return this; - - derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size())); - auto derivedPredicate = derivedPredicates.back(); - - // Move this expression’s arguments to the derived predicate - derivedPredicate->setPreconditions({{this}}); - - return derivedPredicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Not::print(std::ostream &ostream) const -{ - ostream << "(not "; - - m_argument->print(ostream); - - ostream << ")"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Or.cpp b/src/plasp/pddl/expressions/Or.cpp deleted file mode 100644 index 3b090f3..0000000 --- a/src/plasp/pddl/expressions/Or.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include - -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Or -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string Or::Identifier = "or"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Or::decomposed(DerivedPredicates &derivedPredicates) -{ - BOOST_ASSERT(!m_arguments.empty()); - - derivedPredicates.emplace_back(new DerivedPredicate(derivedPredicates.size())); - auto derivedPredicate = derivedPredicates.back(); - - std::vector preconditions; - - for (auto &argument : m_arguments) - { - Expressions conjunction; - - // “and” expressions can directly be inlined into the derived predicate - if (argument->is()) - { - conjunction = std::move(argument->as().arguments()); - - for (auto &argument : conjunction) - argument = argument->decomposed(derivedPredicates); - } - else - conjunction.emplace_back(argument->decomposed(derivedPredicates)); - - // Move this expression’s arguments to the derived predicate - preconditions.emplace_back(std::move(conjunction)); - } - - derivedPredicate->setPreconditions(std::move(preconditions)); - - return derivedPredicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Predicate.cpp b/src/plasp/pddl/expressions/Predicate.cpp deleted file mode 100644 index bddcebc..0000000 --- a/src/plasp/pddl/expressions/Predicate.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Predicate -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Predicate::Predicate() -: m_isDeclared{false} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PredicatePointer Predicate::parse(Context &context, ExpressionContext &expressionContext) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(")) - { - tokenizer.seek(position); - return nullptr; - } - - const auto predicateName = tokenizer.getIdentifier(); - const auto &predicates = expressionContext.domain.predicates(); - - const auto matchingPredicate = std::find_if(predicates.cbegin(), predicates.cend(), - [&](const auto &predicate) - { - return predicate->name() == predicateName; - }); - - if (matchingPredicate == predicates.cend()) - { - tokenizer.seek(position); - return nullptr; - } - - auto predicate = PredicatePointer(new Predicate); - - predicate->m_name = predicateName; - - tokenizer.skipWhiteSpace(); - - // Parse arguments - while (tokenizer.currentCharacter() != ')') - { - // Parse variables - if (tokenizer.currentCharacter() == '?') - { - const auto variable = expressionContext.variables.parseAndFind(context); - predicate->m_arguments.emplace_back(variable); - } - // Parse constants - else - { - const auto constant = (expressionContext.problem == nullptr) - ? Constant::parseAndFind(context, expressionContext.domain) - : Constant::parseAndFind(context, *expressionContext.problem); - predicate->m_arguments.emplace_back(constant); - } - - tokenizer.skipWhiteSpace(); - } - - // TODO: check that signature matches one of the declared ones - - tokenizer.expect(")"); - - return predicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PredicatePointer Predicate::parse(Context &context, const Problem &problem) -{ - auto &tokenizer = context.tokenizer; - - const auto position = tokenizer.position(); - - if (!tokenizer.testAndSkip("(")) - { - tokenizer.seek(position); - return nullptr; - } - - const auto predicateName = tokenizer.getIdentifier(); - const auto &predicates = problem.domain().predicates(); - - const auto matchingPredicate = std::find_if(predicates.cbegin(), predicates.cend(), - [&](const auto &predicate) - { - return predicate->name() == predicateName; - }); - - if (matchingPredicate == predicates.cend()) - { - tokenizer.seek(position); - return nullptr; - } - - auto predicate = PredicatePointer(new Predicate); - - predicate->m_name = predicateName; - - tokenizer.skipWhiteSpace(); - - while (tokenizer.currentCharacter() != ')') - { - if (tokenizer.currentCharacter() == '?') - throw tokenize::TokenizerException(tokenizer, "variables not allowed in this context"); - - // Parse objects and constants - const auto constant = Constant::parseAndFind(context, problem); - predicate->m_arguments.emplace_back(constant); - } - - // TODO: check that signature matches one of the declared ones - - tokenizer.expect(")"); - - return predicate; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Predicate::setDeclared() -{ - m_isDeclared = true; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Predicate::isDeclared() const -{ - return m_isDeclared; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Predicate::decomposed(DerivedPredicates &) -{ - // Predicates cannot be further decomposed - return this; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Predicate::collectParameters(std::set ¶meters) -{ - for (const auto &argument : m_arguments) - argument->collectParameters(parameters); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Predicate::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Expressions &Predicate::arguments() const -{ - return m_arguments; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Predicate::print(std::ostream &ostream) const -{ - // TODO: implement correctly - ostream << "()"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/PredicateDeclaration.cpp b/src/plasp/pddl/expressions/PredicateDeclaration.cpp deleted file mode 100644 index 779e95b..0000000 --- a/src/plasp/pddl/expressions/PredicateDeclaration.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include - -#include -#include -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// PredicateDeclaration -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PredicateDeclaration::PredicateDeclaration() -: m_isDeclared{false} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PredicateDeclaration::parse(Context &context, Domain &domain) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.expect("("); - - auto predicate = PredicateDeclarationPointer(new PredicateDeclaration); - - predicate->m_name = tokenizer.getIdentifier(); - - // Flag predicate as correctly declared in the types section - predicate->setDeclared(); - - tokenizer.skipWhiteSpace(); - - ExpressionContext expressionContext(domain); - expressionContext.variables.push(&predicate->m_parameters); - - // Parse parameters - Variable::parseTypedDeclarations(context, expressionContext, predicate->m_parameters); - - tokenizer.expect(")"); - - domain.predicates().emplace_back(std::move(predicate)); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PredicateDeclaration::setDeclared() -{ - m_isDeclared = true; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool PredicateDeclaration::isDeclared() const -{ - return m_isDeclared; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &PredicateDeclaration::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const Variables &PredicateDeclaration::parameters() const -{ - return m_parameters; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PredicateDeclaration::normalizeParameterNames() -{ - for (size_t i = 0; i < m_parameters.size(); i++) - m_parameters[i]->setName("X" + std::to_string(i)); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PredicateDeclaration::print(std::ostream &ostream) const -{ - // TODO: implement correctly - ostream << "()"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/PrimitiveType.cpp b/src/plasp/pddl/expressions/PrimitiveType.cpp deleted file mode 100644 index 310ee31..0000000 --- a/src/plasp/pddl/expressions/PrimitiveType.cpp +++ /dev/null @@ -1,184 +0,0 @@ -#include - -#include - -#include - -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// PrimitiveType -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveType::PrimitiveType() -: m_isDirty{true} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveType::PrimitiveType(std::string name) -: m_isDirty{true}, - m_name{name} -{ - BOOST_ASSERT(!m_name.empty()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PrimitiveType::parseDeclaration(Context &context, Domain &domain) -{ - auto &types = domain.types(); - - context.tokenizer.skipWhiteSpace(); - - const auto typeName = context.tokenizer.getIdentifier(); - - const auto match = std::find_if(types.cbegin(), types.cend(), - [&](const auto &primitiveType) - { - return primitiveType->name() == typeName; - }); - - // Return existing primitive type - if (match != types.cend()) - { - auto *type = match->get(); - - type->setDirty(); - - return; - } - - types.emplace_back(PrimitiveTypePointer(new PrimitiveType(typeName))); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PrimitiveType::parseTypedDeclaration(Context &context, Domain &domain) -{ - auto &types = domain.types(); - - // Parse and store type - parseDeclaration(context, domain); - - context.tokenizer.skipWhiteSpace(); - - // Check for type inheritance - if (!context.tokenizer.testAndSkip('-')) - return; - - domain.checkRequirement(Requirement::Type::Typing); - - // If existing, parse and store parent type - auto parentType = parseAndFind(context, domain); - - parentType->setDirty(false); - - // Assign parent type to all types that were previously flagged - std::for_each(types.begin(), types.end(), - [&](auto &childType) - { - if (!childType->isDirty()) - return; - - childType->m_parentTypes.push_back(parentType); - childType->setDirty(false); - }); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveTypePointer PrimitiveType::parseAndFind(Context &context, Domain &domain) -{ - auto &tokenizer = context.tokenizer; - - auto &types = domain.types(); - - tokenizer.skipWhiteSpace(); - - const auto typeName = tokenizer.getIdentifier(); - - if (typeName.empty()) - throw tokenize::TokenizerException(tokenizer, "no type supplied"); - - const auto match = std::find_if(types.cbegin(), types.cend(), - [&](const auto &primitiveType) - { - return primitiveType->name() == typeName; - }); - - if (match == types.cend()) - { - // Only "object" is allowed as an implicit type - if (typeName == "object" || typeName == "objects") - { - context.logger.log(output::Priority::Warning, tokenizer, "primitive type “" + typeName + "” should be declared"); - types.emplace_back(PrimitiveTypePointer(new PrimitiveType(typeName))); - } - else - throw tokenize::TokenizerException(tokenizer, "type “" + typeName + "” used but never declared"); - - return types.back().get(); - } - - auto *type = match->get(); - type->setDirty(); - - return type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PrimitiveType::setDirty(bool isDirty) -{ - m_isDirty = isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool PrimitiveType::isDirty() const -{ - return m_isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &PrimitiveType::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const PrimitiveTypes &PrimitiveType::parentTypes() const -{ - return m_parentTypes; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void PrimitiveType::print(std::ostream &ostream) const -{ - // TODO: implement correctly - ostream << "()"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Type.cpp b/src/plasp/pddl/expressions/Type.cpp deleted file mode 100644 index 606a280..0000000 --- a/src/plasp/pddl/expressions/Type.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include - -#include -#include -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Type -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -PrimitiveTypePointer parseExistingPrimitiveType(Context &context, - ExpressionContext &expressionContext) -{ - return PrimitiveType::parseAndFind(context, expressionContext.domain); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Unsupported.cpp b/src/plasp/pddl/expressions/Unsupported.cpp deleted file mode 100644 index 70e68f5..0000000 --- a/src/plasp/pddl/expressions/Unsupported.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Unsupported -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -UnsupportedPointer Unsupported::parse(Context &context) -{ - auto &tokenizer = context.tokenizer; - - auto expression = UnsupportedPointer(new Unsupported); - - tokenizer.expect("("); - - expression->m_type = tokenizer.getIdentifier(); - - context.logger.log(output::Priority::Warning, tokenizer, "expression type “" + expression->m_type + "” currently unsupported in this context"); - - skipSection(tokenizer); - - return expression; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Unsupported::type() const -{ - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Unsupported::print(std::ostream &ostream) const -{ - // TODO: implement correctly - ostream << "()"; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/Variable.cpp b/src/plasp/pddl/expressions/Variable.cpp deleted file mode 100644 index 74c7cbb..0000000 --- a/src/plasp/pddl/expressions/Variable.cpp +++ /dev/null @@ -1,210 +0,0 @@ -#include - -#include - -#include - -#include -#include -#include -#include -#include -#include - -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// Variable -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Variable::Variable() -: m_isDirty{false} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -Variable::Variable(std::string name) -: m_isDirty{false}, - m_name{name} -{ -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::parseDeclaration(Context &context, Variables ¶meters) -{ - auto &tokenizer = context.tokenizer; - - tokenizer.skipWhiteSpace(); - - tokenizer.expect("?"); - - auto variable = VariablePointer(new Variable); - - variable->m_name = tokenizer.getIdentifier(); - - // Check if variable of that name already exists in the current scope - const auto match = std::find_if(parameters.cbegin(), parameters.cend(), - [&](const auto ¶meter) - { - return parameter->name() == variable->m_name; - }); - - if (match != parameters.cend()) - throw tokenize::TokenizerException(tokenizer, "variable “" + variable->m_name + "” already declared in this scope"); - - // Flag variable for potentially upcoming type declaration - variable->setDirty(); - - parameters.emplace_back(variable); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::parseTypedDeclaration(Context &context, ExpressionContext &expressionContext, - Variables &variables) -{ - auto &tokenizer = context.tokenizer; - - // Parse and store variable itself - parseDeclaration(context, variables); - - auto variable = variables.back(); - - tokenizer.skipWhiteSpace(); - - // Check if the variable has a type declaration - if (!tokenizer.testAndSkip('-')) - return; - - const auto setType = - [&](ExpressionPointer type) - { - // Set the argument type for all previously flagged arguments - for (auto &variable : variables) - { - if (!variable->isDirty()) - return; - - variable->setType(type); - variable->setDirty(false); - } - }; - - tokenizer.skipWhiteSpace(); - - // Parse argument if it has "either" type (always begins with opening parenthesis) - variable->m_type = Either::parse(context, expressionContext, parseExistingPrimitiveType); - - // Else, try parsing it as a primitive type - if (!variable->m_type) - variable->m_type = PrimitiveType::parseAndFind(context, expressionContext.domain); - - setType(variable->m_type); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::parseTypedDeclarations(Context &context, ExpressionContext &expressionContext, - Variables &variables) -{ - auto &tokenizer = context.tokenizer; - - while (tokenizer.currentCharacter() != ')') - { - parseTypedDeclaration(context, expressionContext, variables); - - tokenizer.skipWhiteSpace(); - } - - if (variables.empty()) - return; - - // Check correct use of typing requirement - const auto typingUsed = (variables.back()->type() != nullptr); - - // If types are given, check that typing is a requirement - if (typingUsed) - expressionContext.checkRequirement(Requirement::Type::Typing); - // If no types are given, check that typing is not a requirement - else if (expressionContext.hasRequirement(Requirement::Type::Typing)) - throw tokenize::TokenizerException(tokenizer, "variable has undeclared type"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::setName(std::string name) -{ - m_name = name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string &Variable::name() const -{ - return m_name; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::setType(ExpressionPointer type) -{ - m_type = type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -ExpressionPointer Variable::type() const -{ - return m_type; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::setDirty(bool isDirty) -{ - m_isDirty = isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -bool Variable::isDirty() const -{ - return m_isDirty; -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::collectParameters(std::set ¶meters) -{ - parameters.emplace(this); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -void Variable::print(std::ostream &ostream) const -{ - ostream << "?" << m_name; - - if (m_type) - { - ostream << " - "; - m_type->print(ostream); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/src/plasp/pddl/expressions/When.cpp b/src/plasp/pddl/expressions/When.cpp deleted file mode 100644 index d739a83..0000000 --- a/src/plasp/pddl/expressions/When.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include - -namespace plasp -{ -namespace pddl -{ -namespace expressions -{ - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// -// When -// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -const std::string When::Identifier = "when"; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -} -} -} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 089ad57..fcfdcad 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,6 +7,8 @@ set(includes ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/lib/catch/single_include ${PROJECT_SOURCE_DIR}/lib/tokenize/include + ${PROJECT_SOURCE_DIR}/lib/variant/include + ${PROJECT_SOURCE_DIR}/lib/pddlparse/include ) set(libraries diff --git a/tests/TestPDDLNormalization.cpp b/tests/TestPDDLNormalization.cpp index 43c6c7b..6e8e1da 100644 --- a/tests/TestPDDLNormalization.cpp +++ b/tests/TestPDDLNormalization.cpp @@ -1,19 +1,8 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace plasp::pddl; - //////////////////////////////////////////////////////////////////////////////////////////////////// -TEST_CASE("[PDDL normalization] PDDL expressions are correctly reduced", "[PDDL normalization]") +/*TEST_CASE("[PDDL normalization] PDDL expressions are correctly reduced", "[PDDL normalization]") { auto n1 = expressions::NotPointer(new expressions::Not); auto n2 = expressions::NotPointer(new expressions::Not); @@ -205,4 +194,4 @@ TEST_CASE("[PDDL normalization] “for all” expressions are correctly replaced f1->normalized()->print(output); CHECK(output.str() == "(exists (?x ?y) (not (exists (?z ?u ?v ?w) (not (a)))))"); -} +}*/ diff --git a/tests/TestPDDLParser.cpp b/tests/TestPDDLParser.cpp deleted file mode 100644 index 0d03ddf..0000000 --- a/tests/TestPDDLParser.cpp +++ /dev/null @@ -1,532 +0,0 @@ -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -using namespace plasp::pddl; - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] The Blocks World domain is parsed correctly", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - const auto description = Description::fromFile("data/blocksworld-domain.pddl", context); - - REQUIRE_NOTHROW(description.domain()); - - const auto &domain = description.domain(); - - // Name - CHECK(domain.name() == "blocks"); - - // Requirements - REQUIRE(domain.requirements().size() == 2u); - CHECK(domain.requirements()[0].type() == Requirement::Type::STRIPS); - CHECK(domain.requirements()[1].type() == Requirement::Type::Typing); - - // Types - REQUIRE(domain.types().size() == 1u); - - const auto &block = *domain.types()[0]; - - CHECK(block.name() == "block"); - REQUIRE(block.parentTypes().size() == 0u); - - // Predicates - REQUIRE(domain.predicates().size() == 5u); - - const auto &on = *domain.predicates()[0]; - - CHECK(on.name() == "on"); - REQUIRE(on.parameters().size() == 2u); - CHECK(on.parameters()[0]->name() == "x"); - const auto &onArgument0Type = on.parameters()[0]->type()->as(); - CHECK(&onArgument0Type == &block); - CHECK(on.parameters()[1]->name() == "y"); - const auto &onArgument1Type = on.parameters()[1]->type()->as(); - CHECK(&onArgument1Type == &block); - - const auto &handempty = *domain.predicates()[3]; - - CHECK(handempty.name() == "handempty"); - CHECK(handempty.parameters().empty()); - - // Actions - REQUIRE(domain.actions().size() == 4u); - - const auto &pickUp = *domain.actions()[0]; - - CHECK(pickUp.name() == "pick-up"); - REQUIRE(pickUp.parameters().size() == 1u); - CHECK(pickUp.parameters()[0]->name() == "x"); - CHECK(pickUp.parameters()[0]->type() == &block); - - const auto &pickUpPre = pickUp.precondition()->as(); - REQUIRE(pickUpPre.arguments().size() == 3u); - const auto &pickUpPre0 = pickUpPre.arguments()[0]->as(); - CHECK(pickUpPre0.name() == "clear"); - REQUIRE(pickUpPre0.arguments().size() == 1u); - const auto &pickUpPre00 = pickUpPre0.arguments()[0]->as(); - CHECK(pickUpPre00.name() == "x"); - CHECK(pickUpPre00.type() == &block); - CHECK(&pickUpPre00 == pickUp.parameters()[0].get()); - const auto &pickUpPre2 = pickUpPre.arguments()[2]->as(); - CHECK(pickUpPre2.name() == "handempty"); - CHECK(pickUpPre2.arguments().empty()); - - const auto &pickUpEff = pickUp.effect()->as(); - REQUIRE(pickUpEff.arguments().size() == 4u); - const auto &pickUpEff0 = pickUpEff.arguments()[0]->as(); - const auto &pickUpEff00 = pickUpEff0.argument()->as(); - CHECK(pickUpEff00.name() == "ontable"); - REQUIRE(pickUpEff00.arguments().size() == 1u); - const auto &pickUpEff000 = pickUpEff00.arguments()[0]->as(); - CHECK(pickUpEff000.name() == "x"); - CHECK(pickUpEff000.type() == &block); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] A Blocks World problem is parsed correctly", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - const auto description = Description::fromFiles({"data/blocksworld-domain.pddl", "data/blocksworld-problem.pddl"}, context); - - REQUIRE_NOTHROW(description.problem()); - - const auto &problem = description.problem(); - - // Name - CHECK(problem.name() == "blocks-4-0"); - CHECK(problem.domain().name() == "blocks"); - - // Requirements - // TODO: compute domain vs. problem requirements correctly and check them - - // Objects - REQUIRE(problem.objects().size() == 4u); - - CHECK(problem.objects()[0]->name() == "d"); - REQUIRE(problem.objects()[0]->type() != nullptr); - CHECK(problem.objects()[0]->type()->name() == "block"); - CHECK(problem.objects()[3]->name() == "c"); - REQUIRE(problem.objects()[3]->type() != nullptr); - CHECK(problem.objects()[3]->type()->name() == "block"); - - // Initial State - const auto &facts = problem.initialState().facts(); - - REQUIRE(facts.size() == 9u); - const auto &fact0 = facts[0].get()->as(); - CHECK(fact0.name() == "clear"); - REQUIRE(fact0.arguments().size() == 1u); - const auto &fact00 = fact0.arguments()[0]->as(); - CHECK(fact00.name() == "c"); - REQUIRE(fact00.type() != nullptr); - CHECK(fact00.type()->name() == "block"); - const auto &fact8 = facts[8].get()->as(); - CHECK(fact8.name() == "handempty"); - REQUIRE(fact8.arguments().size() == 0u); - - // Goal - const auto &goal = problem.goal().as(); - - REQUIRE(goal.arguments().size() == 3u); - const auto &goal0 = goal.arguments()[0]->as(); - CHECK(goal0.name() == "on"); - REQUIRE(goal0.arguments().size() == 2u); - const auto &goal00 = goal0.arguments()[0]->as(); - CHECK(goal00.name() == "d"); - const auto &goal01 = goal0.arguments()[1]->as(); - CHECK(goal01.name() == "c"); - const auto &goal2 = goal.arguments()[2]->as(); - CHECK(goal2.name() == "on"); - REQUIRE(goal2.arguments().size() == 2u); - const auto &goal20 = goal2.arguments()[0]->as(); - CHECK(goal20.name() == "b"); - const auto &goal21 = goal2.arguments()[1]->as(); - CHECK(goal21.name() == "a"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] The Storage domain is parsed correctly", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - const auto description = plasp::pddl::Description::fromFile("data/storage-domain.pddl", context); - - REQUIRE_NOTHROW(description.domain()); - - const auto &domain = description.domain(); - - // Name - CHECK(domain.name() == "storage-propositional"); - - // Requirements - REQUIRE(domain.requirements().size() == 1u); - CHECK(domain.requirements()[0].type() == Requirement::Type::Typing); - - // Types - REQUIRE(domain.types().size() == 10u); - - const auto &hoist = *domain.types()[0]; - const auto &surface = *domain.types()[1]; - const auto &area = *domain.types()[3]; - const auto &object = *domain.types()[4]; - const auto &storearea = *domain.types()[7]; - const auto &crate = *domain.types()[9]; - - const auto &hoistParents = hoist.parentTypes(); - REQUIRE(hoistParents.size() == 1u); - CHECK(std::find(hoistParents.cbegin(), hoistParents.cend(), &object) != hoistParents.cend()); - - const auto &areaParents = area.parentTypes(); - REQUIRE(areaParents.size() == 2u); - CHECK(std::find(areaParents.cbegin(), areaParents.cend(), &object) != areaParents.cend()); - CHECK(std::find(areaParents.cbegin(), areaParents.cend(), &surface) != areaParents.cend()); - - // Predicates - REQUIRE(domain.predicates().size() == 8u); - - const auto &on = *domain.predicates()[5]; - - CHECK(on.name() == "on"); - REQUIRE(on.parameters().size() == 2u); - CHECK(on.parameters()[0]->name() == "c"); - const auto &onArgument0Type = on.parameters()[0]->type()->as(); - CHECK(&onArgument0Type == &crate); - CHECK(on.parameters()[1]->name() == "s"); - const auto &onArgument1Type = on.parameters()[1]->type()->as(); - CHECK(&onArgument1Type == &storearea); - - const auto &in = *domain.predicates()[1]; - CHECK(in.name() == "in"); - REQUIRE(in.parameters().size() == 2u); - CHECK(in.parameters()[0]->name() == "x"); - const auto &inArgument0Type = in.parameters()[0]->type()->as(); - REQUIRE(inArgument0Type.arguments().size() == 2u); - const auto &inArgument0Type0 = inArgument0Type.arguments()[0]->as(); - CHECK(&inArgument0Type0 == &storearea); - const auto &inArgument0Type1 = inArgument0Type.arguments()[1]->as(); - CHECK(&inArgument0Type1 == &crate); - - // Actions - REQUIRE(domain.actions().size() == 5u); - - const auto &drop = *domain.actions()[1]; - - CHECK(drop.name() == "drop"); - REQUIRE(drop.parameters().size() == 5u); - CHECK(drop.parameters()[3]->name() == "a2"); - CHECK(drop.parameters()[3]->type() == &area); - - const auto &dropPre = drop.precondition()->as(); - REQUIRE(dropPre.arguments().size() == 5u); - const auto &dropPre2 = dropPre.arguments()[2]->as(); - CHECK(dropPre2.name() == "lifting"); - REQUIRE(dropPre2.arguments().size() == 2u); - const auto &dropPre21 = dropPre2.arguments()[1]->as(); - CHECK(dropPre21.name() == "c"); - CHECK(dropPre21.type() == &crate); - - const auto &dropEff = drop.effect()->as(); - REQUIRE(dropEff.arguments().size() == 5u); - const auto &dropEff2 = dropEff.arguments()[2]->as(); - const auto &dropEff20 = dropEff2.argument()->as(); - CHECK(dropEff20.name() == "clear"); - REQUIRE(dropEff20.arguments().size() == 1u); - const auto &dropEff200 = dropEff20.arguments()[0]->as(); - CHECK(dropEff200.name() == "a1"); - CHECK(dropEff200.type() == &storearea); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] A Storage problem is parsed correctly", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - const auto description = Description::fromFiles({"data/storage-domain.pddl", "data/storage-problem.pddl"}, context); - - REQUIRE_NOTHROW(description.problem()); - - const auto &problem = description.problem(); - - // Name - CHECK(problem.name() == "storage-1"); - CHECK(problem.domain().name() == "storage-propositional"); - - // Requirements - // TODO: compute domain vs. problem requirements correctly and check them - - // Objects - REQUIRE(problem.objects().size() == 7u); - - CHECK(problem.objects()[0]->name() == "depot0-1-1"); - REQUIRE(problem.objects()[0]->type() != nullptr); - CHECK(problem.objects()[0]->type()->name() == "storearea"); - CHECK(problem.objects()[6]->name() == "loadarea"); - REQUIRE(problem.objects()[6]->type() != nullptr); - CHECK(problem.objects()[6]->type()->name() == "transitarea"); - - // Initial State - const auto &facts = problem.initialState().facts(); - - REQUIRE(facts.size() == 10u); - const auto &fact0 = facts[0].get()->as(); - CHECK(fact0.name() == "in"); - REQUIRE(fact0.arguments().size() == 2u); - const auto &fact01 = fact0.arguments()[1]->as(); - CHECK(fact01.name() == "depot0"); - REQUIRE(fact01.type() != nullptr); - CHECK(fact01.type()->name() == "depot"); - const auto &fact9 = facts[9].get()->as(); - CHECK(fact9.name() == "available"); - REQUIRE(fact9.arguments().size() == 1u); - const auto &fact90 = fact9.arguments()[0]->as(); - CHECK(fact90.name() == "hoist0"); - REQUIRE(fact90.type() != nullptr); - CHECK(fact90.type()->name() == "hoist"); - - // Goal - const auto &goal = problem.goal().as(); - - REQUIRE(goal.arguments().size() == 1u); - const auto &goal0 = goal.arguments()[0]->as(); - CHECK(goal0.name() == "in"); - REQUIRE(goal0.arguments().size() == 2u); - const auto &goal00 = goal0.arguments()[0]->as(); - CHECK(goal00.name() == "crate0"); - const auto &goal01 = goal0.arguments()[1]->as(); - CHECK(goal01.name() == "depot0"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] Constants are parsed correctly", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - const auto description = Description::fromFile("data/woodworking-domain.pddl", context); - - REQUIRE_NOTHROW(description.domain()); - - const auto &domain = description.domain(); - - // Name - CHECK(domain.name() == "woodworking"); - - // Types - const auto &acolour = *domain.types()[0]; - const auto &surface = *domain.types()[4]; - const auto &treatmentstatus = *domain.types()[5]; - - // Constants - REQUIRE(domain.constants().size() == 8u); - CHECK(domain.constants()[0]->type() == &surface); - CHECK(domain.constants()[2]->type() == &surface); - CHECK(domain.constants()[3]->type() == &treatmentstatus); - CHECK(domain.constants()[6]->type() == &treatmentstatus); - CHECK(domain.constants()[7]->type() == &acolour); - - // TODO: add test with constants in predicates -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] White spaces are ignored", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - CHECK_NOTHROW(Description::fromFile("data/test-cases/white-space.pddl", context)); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] Missing or unmatching domain descriptions are detected", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - SECTION("") - { - CHECK_THROWS_AS(Description::fromFile("data/blocksworld-problem.pddl", context), ConsistencyException); - } - SECTION("") - { - CHECK_THROWS_AS(Description::fromFiles({"data/blocksworld-problem.pddl", "data/storage-domain.pddl"}, context), tokenize::TokenizerException); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] Common PDDL syntax errors are detected", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - SECTION("") - { - CHECK_NOTHROW(Description::fromFile("data/pddl-syntax/domain-valid.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expressions-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expressions-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expressions-3.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expression-name-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expression-name-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-expression-name-3.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-3.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-4.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-5.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-6.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-7.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-parentheses-8.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-3.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-4.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-5.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-6.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-section-name-7.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-types-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-types-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-types-3.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-types-4.pddl", context)); - } - - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-variables-1.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-variables-2.pddl", context)); - } - SECTION("") - { - CHECK_THROWS(Description::fromFile("data/pddl-syntax/domain-variables-3.pddl", context)); - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - -TEST_CASE("[PDDL parser] Former issues are fixed", "[PDDL parser]") -{ - plasp::output::Logger logger; - Context context(Tokenizer(), logger); - - SECTION("white space issues with constants and parsing unsupported sections") - { - CHECK_NOTHROW(Description::fromFile("data/issues/issue-1.pddl", context)); - } - - SECTION("white space issues with empty n-ary predicates") - { - CHECK_NOTHROW(Description::fromFile("data/issues/issue-2.pddl", context)); - } - - SECTION("comments are correctly ignored") - { - CHECK_NOTHROW(Description::fromFile("data/issues/issue-3.pddl", context)); - } -} diff --git a/tests/TestPDDLTranslation.cpp b/tests/TestPDDLTranslation.cpp index a7f981e..aa817b2 100644 --- a/tests/TestPDDLTranslation.cpp +++ b/tests/TestPDDLTranslation.cpp @@ -3,31 +3,37 @@ #include #include -#include +#include +#include + +#include #include -using namespace plasp::pddl; - boost::iostreams::stream nullStream((boost::iostreams::null_sink())); +const pddl::Context::WarningCallback ignoreWarnings = [](const auto &, const auto &){}; //////////////////////////////////////////////////////////////////////////////////////////////////// TEST_CASE("[PDDL translation] Former issues are fixed", "[PDDL translation]") { - plasp::output::Logger logger; - Context context(Tokenizer(), logger); + // TODO: refactor + plasp::output::Logger logger(nullStream, nullStream); + pddl::Tokenizer tokenizer; + pddl::Context context(std::move(tokenizer), ignoreWarnings); SECTION("translating domains without typing information works") { - auto description = Description::fromFile("data/issues/issue-4.pddl", context); - const auto translator = TranslatorASP(description, description.context().logger.outputStream()); + context.tokenizer.read("data/issues/issue-4.pddl"); + auto description = pddl::parseDescription(context); + const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream()); CHECK_NOTHROW(translator.translate()); } SECTION("translating the simple blocks world domain works") { - auto description = Description::fromFile("data/issues/issue-5.pddl", context); - const auto translator = TranslatorASP(description, description.context().logger.outputStream()); + context.tokenizer.read("data/issues/issue-5.pddl"); + auto description = pddl::parseDescription(context); + const auto translator = plasp::pddl::TranslatorASP(std::move(description), logger.outputStream()); CHECK_NOTHROW(translator.translate()); } }