From 97ab22461c4b89324d5fba301914df893cd4350e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 2 Jun 2016 16:06:11 +0200 Subject: [PATCH] Started implementing parsing of action preconditions. --- include/plasp/pddl/Action.h | 43 ++++++++++++++ include/plasp/pddl/Expression.h | 37 ++++++++++++ src/plasp/pddl/Action.cpp | 99 +++++++++++++++++++++++++++++++++ src/plasp/pddl/Expression.cpp | 39 +++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 include/plasp/pddl/Action.h create mode 100644 include/plasp/pddl/Expression.h create mode 100644 src/plasp/pddl/Action.cpp create mode 100644 src/plasp/pddl/Expression.cpp diff --git a/include/plasp/pddl/Action.h b/include/plasp/pddl/Action.h new file mode 100644 index 0000000..d125357 --- /dev/null +++ b/include/plasp/pddl/Action.h @@ -0,0 +1,43 @@ +#ifndef __PLASP__PDDL__ACTION_H +#define __PLASP__PDDL__ACTION_H + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Action +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Action +{ + public: + static Action &parseDeclaration(utils::Parser &parser, Context &context); + + public: + const std::string &name() const; + + private: + Action(std::string name); + + void parsePrecondition(utils::Parser &parser, Context &context); + + std::string m_name; + + std::vector m_parameters; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h new file mode 100644 index 0000000..2a724dc --- /dev/null +++ b/include/plasp/pddl/Expression.h @@ -0,0 +1,37 @@ +#ifndef __PLASP__PDDL__EXPRESSION_H +#define __PLASP__PDDL__EXPRESSION_H + +#include + +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Expression +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Context; + +namespace expression +{ + class And; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +using ExpressionPtr = boost::variant; + +ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &context); + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} + +#endif diff --git a/src/plasp/pddl/Action.cpp b/src/plasp/pddl/Action.cpp new file mode 100644 index 0000000..834aa0d --- /dev/null +++ b/src/plasp/pddl/Action.cpp @@ -0,0 +1,99 @@ +#include + +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Action +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Action::Action(std::string name) +: m_name{name} +{ +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +Action &Action::parseDeclaration(utils::Parser &parser, Context &context) +{ + const auto actionName = parser.parseIdentifier(isIdentifier); + + auto action = std::make_unique(Action(actionName)); + + parser.expect(":parameters"); + + parser.skipWhiteSpace(); + + // Read parameters + while (parser.currentCharacter() != ':') + { + Variable::parseTyped(parser, context, action->m_parameters); + + parser.skipWhiteSpace(); + } + + // Parse preconditions and effects + while (parser.currentCharacter() == ')') + { + parser.expect(":"); + + const auto sectionIdentifier = parser.parseIdentifier(isIdentifier); + + if (sectionIdentifier == "precondition") + action->parsePrecondition(parser, context); + } + + parser.expect(")"); + + // Store new action + context.actions.emplace_back(std::move(action)); + + return *context.actions.back(); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +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; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const Variables &Action::parameters() const +{ + return m_parameters; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp new file mode 100644 index 0000000..a7b2554 --- /dev/null +++ b/src/plasp/pddl/Expression.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include +#include + +namespace plasp +{ +namespace pddl +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Expression +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +ExpressionPtr parsePreconditionExpression(utils::Parser &parser, Context &context) +{ + parser.skipWhiteSpace(); + + parser.expect("("); + + const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); + + ExpressionPtr expression; + + throw utils::ParserException(parser.row(), parser.column(), "Expression of type \"" + expressionIdentifier + "\" not allowed in preference declaration"); + + //if (expressionIdentifier == "and") + // expression = NAry + + parser.expect(")"); +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +}