From a07019801c7772f3a40283b8f4c916672ab9e393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 2 Jun 2016 16:28:08 +0200 Subject: [PATCH] Refactored expressions to inherit from base class. --- include/plasp/pddl/Action.h | 11 ++++++-- include/plasp/pddl/Context.h | 3 ++ include/plasp/pddl/Domain.h | 3 ++ include/plasp/pddl/Expression.h | 18 +++++++----- src/plasp/pddl/Action.cpp | 49 +++++++++++++++++---------------- src/plasp/pddl/Domain.cpp | 20 +++++++++++++- src/plasp/pddl/Expression.cpp | 7 +++-- 7 files changed, 75 insertions(+), 36 deletions(-) diff --git a/include/plasp/pddl/Action.h b/include/plasp/pddl/Action.h index d125357..4b618d9 100644 --- a/include/plasp/pddl/Action.h +++ b/include/plasp/pddl/Action.h @@ -3,6 +3,7 @@ #include +#include #include #include @@ -25,14 +26,18 @@ class Action public: const std::string &name() const; + const Variables ¶meters() const; + const Expression &precondition() const; + const Expression &effect() const; + private: Action(std::string name); - void parsePrecondition(utils::Parser &parser, Context &context); - std::string m_name; - std::vector m_parameters; + Variables m_parameters; + std::unique_ptr m_precondition; + std::unique_ptr m_effect; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/Context.h b/include/plasp/pddl/Context.h index d6e0188..752157e 100644 --- a/include/plasp/pddl/Context.h +++ b/include/plasp/pddl/Context.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,8 @@ class Context std::vector> predicates; std::unordered_map predicatesHashMap; + + std::vector> actions; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/plasp/pddl/Domain.h b/include/plasp/pddl/Domain.h index 86102bf..087c674 100644 --- a/include/plasp/pddl/Domain.h +++ b/include/plasp/pddl/Domain.h @@ -31,6 +31,7 @@ class Domain const std::vector> &types() const; const std::vector> &constants() const; const std::vector> &predicates() const; + const std::vector> &actions() const; private: Domain(Context &context); @@ -47,6 +48,8 @@ class Domain void parsePredicateSection(utils::Parser &parser); + void parseActionSection(utils::Parser &parser); + void checkConsistency(); Context &m_context; diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h index 2a724dc..df8a5b8 100644 --- a/include/plasp/pddl/Expression.h +++ b/include/plasp/pddl/Expression.h @@ -1,6 +1,8 @@ #ifndef __PLASP__PDDL__EXPRESSION_H #define __PLASP__PDDL__EXPRESSION_H +#include + #include #include @@ -17,17 +19,19 @@ namespace pddl //////////////////////////////////////////////////////////////////////////////////////////////////// class Context; - -namespace expression -{ - class And; -} +class ExpressionVisitor; //////////////////////////////////////////////////////////////////////////////////////////////////// -using ExpressionPtr = boost::variant; +class Expression +{ + public: + virtual void accept(ExpressionVisitor &expressionVisitor) const = 0; +}; -ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &context); +//////////////////////////////////////////////////////////////////////////////////////////////////// + +std::unique_ptr parsePreconditionExpression(utils::Parser &parser, Context &context); //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/plasp/pddl/Action.cpp b/src/plasp/pddl/Action.cpp index 834aa0d..6141f48 100644 --- a/src/plasp/pddl/Action.cpp +++ b/src/plasp/pddl/Action.cpp @@ -31,25 +31,29 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context) parser.expect(":parameters"); - parser.skipWhiteSpace(); + parser.expect("("); // Read parameters - while (parser.currentCharacter() != ':') + while (parser.currentCharacter() != ')') { Variable::parseTyped(parser, context, action->m_parameters); parser.skipWhiteSpace(); } + parser.expect(")"); + // Parse preconditions and effects - while (parser.currentCharacter() == ')') + while (parser.currentCharacter() != ')') { parser.expect(":"); const auto sectionIdentifier = parser.parseIdentifier(isIdentifier); if (sectionIdentifier == "precondition") - action->parsePrecondition(parser, context); + action->m_precondition = parsePreconditionExpression(parser, context); + //else if (sectionIdentifier == "effect") + // action->m_effect = parseEffectExpression(parser, context); } parser.expect(")"); @@ -62,25 +66,6 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context) //////////////////////////////////////////////////////////////////////////////////////////////////// -void Action::parsePrecondition(utils::Parser &parser, Context &context) -{ - parser.skipWhiteSpace(); - - // Check for empty precondition - if (parser.currentCharacter() == '(') - { - // Leave the preconditions empty and return - parser.expect("()"); - parser.expect(")"); - - return; - } - - parser.expect")"); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - const std::string &Action::name() const { return m_name; @@ -95,5 +80,23 @@ const Variables &Action::parameters() const //////////////////////////////////////////////////////////////////////////////////////////////////// +const Expression &Action::precondition() const +{ + BOOST_ASSERT(m_precondition); + + return *m_precondition; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Expression &Action::effect() const +{ + BOOST_ASSERT(m_effect); + + return *m_effect; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + } } diff --git a/src/plasp/pddl/Domain.cpp b/src/plasp/pddl/Domain.cpp index 9957369..7caa6a5 100644 --- a/src/plasp/pddl/Domain.cpp +++ b/src/plasp/pddl/Domain.cpp @@ -88,6 +88,13 @@ const std::vector> &Domain::predicates() const //////////////////////////////////////////////////////////////////////////////////////////////////// +const std::vector> &Domain::actions() const +{ + return m_context.actions; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Domain::parseSection(utils::Parser &parser) { parser.expect("(:"); @@ -131,7 +138,7 @@ void Domain::parseSection(utils::Parser &parser) else if (sectionIdentifier == "constraints") skipSection(); else if (sectionIdentifier == "action") - skipSection(); + parseActionSection(parser); else if (sectionIdentifier == "durative-action") skipSection(); else if (sectionIdentifier == "derived") @@ -273,6 +280,17 @@ void Domain::parsePredicateSection(utils::Parser &parser) //////////////////////////////////////////////////////////////////////////////////////////////////// +void Domain::parseActionSection(utils::Parser &parser) +{ + parser.skipWhiteSpace(); + + Action::parseDeclaration(parser, m_context); + + parser.expect(")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + void Domain::checkConsistency() { // Verify that typing requirement is correctly declared if used diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp index a7b2554..cd1fefe 100644 --- a/src/plasp/pddl/Expression.cpp +++ b/src/plasp/pddl/Expression.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace plasp @@ -15,7 +16,7 @@ namespace pddl // //////////////////////////////////////////////////////////////////////////////////////////////////// -ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &context) +std::unique_ptr parsePreconditionExpression(utils::Parser &parser, Context &context) { parser.skipWhiteSpace(); @@ -23,7 +24,7 @@ ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &contex const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); - ExpressionPtr expression; + std::unique_ptr expression; throw utils::ParserException(parser.row(), parser.column(), "Expression of type \"" + expressionIdentifier + "\" not allowed in preference declaration"); @@ -31,6 +32,8 @@ ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &contex // expression = NAry parser.expect(")"); + + return std::move(expression); } ////////////////////////////////////////////////////////////////////////////////////////////////////