From f4f0e07bc11ad1e6c58494a2c06e86b2afc5f63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Tue, 6 Sep 2016 16:57:31 +0200 Subject: [PATCH] Started supporting quantified expressions. --- include/plasp/pddl/Expression.h | 2 + include/plasp/pddl/expressions/Exists.h | 33 +++++ include/plasp/pddl/expressions/ForAll.h | 33 +++++ include/plasp/pddl/expressions/Quantified.h | 140 ++++++++++++++++++++ src/plasp/pddl/Expression.cpp | 14 +- src/plasp/pddl/expressions/Exists.cpp | 25 ++++ src/plasp/pddl/expressions/ForAll.cpp | 25 ++++ 7 files changed, 267 insertions(+), 5 deletions(-) create mode 100644 include/plasp/pddl/expressions/Exists.h create mode 100644 include/plasp/pddl/expressions/ForAll.h create mode 100644 include/plasp/pddl/expressions/Quantified.h create mode 100644 src/plasp/pddl/expressions/Exists.cpp create mode 100644 src/plasp/pddl/expressions/ForAll.cpp diff --git a/include/plasp/pddl/Expression.h b/include/plasp/pddl/Expression.h index 862f63d..5542ccb 100644 --- a/include/plasp/pddl/Expression.h +++ b/include/plasp/pddl/Expression.h @@ -93,6 +93,8 @@ class Expression Constant, Dummy, Either, + Exists, + ForAll, Imply, Not, Or, diff --git a/include/plasp/pddl/expressions/Exists.h b/include/plasp/pddl/expressions/Exists.h new file mode 100644 index 0000000..d795176 --- /dev/null +++ b/include/plasp/pddl/expressions/Exists.h @@ -0,0 +1,33 @@ +#ifndef __PLASP__PDDL__EXPRESSIONS__EXISTS_H +#define __PLASP__PDDL__EXPRESSIONS__EXISTS_H + +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Exists +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +class Exists: public Quantified +{ + 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 new file mode 100644 index 0000000..ee9ea00 --- /dev/null +++ b/include/plasp/pddl/expressions/ForAll.h @@ -0,0 +1,33 @@ +#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 Quantified +{ + public: + static const Expression::Type ExpressionType = Expression::Type::ForAll; + + static const std::string Identifier; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} + +#endif diff --git a/include/plasp/pddl/expressions/Quantified.h b/include/plasp/pddl/expressions/Quantified.h new file mode 100644 index 0000000..ff956ae --- /dev/null +++ b/include/plasp/pddl/expressions/Quantified.h @@ -0,0 +1,140 @@ +#ifndef __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H +#define __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H + +#include +#include +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Quantified +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +class Quantified: public ExpressionCRTP +{ + public: + template + static boost::intrusive_ptr parse(Context &context, + ExpressionContext &expressionContext, ExpressionParser parseExpression); + + public: + void setArgument(ExpressionPointer argument); + ExpressionPointer argument() const; + + ExpressionPointer reduced() override; + ExpressionPointer negationNormalized() override; + + void print(std::ostream &ostream) const override; + + protected: + Variables m_variables; + ExpressionPointer m_argument; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +template +boost::intrusive_ptr Quantified::parse(Context &context, + ExpressionContext &expressionContext, ExpressionParser parseExpression) +{ + auto &parser = context.parser; + + const auto position = parser.position(); + + if (!parser.testAndSkip("(") + || !parser.testIdentifierAndSkip(Derived::Identifier)) + { + parser.seek(position); + return nullptr; + } + + auto expression = boost::intrusive_ptr(new Derived); + + // Parse variable list + parser.expect("("); + Variable::parseTypedDeclarations(context, expressionContext, expression->m_variables); + parser.expect(")"); + + // Parse argument of the expression + expression->Quantified::setArgument(parseExpression(context, expressionContext)); + + parser.expect(")"); + + return expression; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +void Quantified::setArgument(ExpressionPointer expression) +{ + m_argument = expression; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +ExpressionPointer Quantified::argument() const +{ + return m_argument; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline ExpressionPointer Quantified::reduced() +{ + m_argument = m_argument->reduced(); + + return this; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline ExpressionPointer Quantified::negationNormalized() +{ + m_argument = m_argument->negationNormalized(); + + return this; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template +inline void Quantified::print(std::ostream &ostream) const +{ + ostream << "(" << Derived::Identifier << "("; + + for (size_t i = 0; i < m_variables.size(); i++) + { + if (i > 0) + ostream << " "; + + ostream << m_variables[i]->name(); + } + + ostream << ") "; + + m_argument->print(ostream); + + ostream << ")"; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +} + +#endif diff --git a/src/plasp/pddl/Expression.cpp b/src/plasp/pddl/Expression.cpp index 4cc2030..9a07b55 100644 --- a/src/plasp/pddl/Expression.cpp +++ b/src/plasp/pddl/Expression.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -86,14 +88,16 @@ ExpressionPointer parsePreconditionExpression(Context &context, if ((expression = expressions::And::parse(context, expressionContext, parsePreconditionExpression))) return expression; + if ((expression = expressions::ForAll::parse(context, expressionContext, parsePreconditionExpression))) + return expression; + const auto position = parser.position(); parser.expect("("); const auto expressionIdentifierPosition = parser.position(); - if (parser.testIdentifierAndSkip("forall") - || parser.testIdentifierAndSkip("preference")) + if (parser.testIdentifierAndSkip("preference")) { // TODO: refactor parser.seek(expressionIdentifierPosition); @@ -119,6 +123,8 @@ ExpressionPointer parseExpression(Context &context, ExpressionContext &expressio 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))) @@ -132,9 +138,7 @@ ExpressionPointer parseExpression(Context &context, ExpressionContext &expressio const auto expressionIdentifierPosition = parser.position(); - if (parser.testIdentifierAndSkip("exists") - || parser.testIdentifierAndSkip("forall") - || parser.testIdentifierAndSkip("-") + if (parser.testIdentifierAndSkip("-") || parser.testIdentifierAndSkip("=") || parser.testIdentifierAndSkip("*") || parser.testIdentifierAndSkip("+") diff --git a/src/plasp/pddl/expressions/Exists.cpp b/src/plasp/pddl/expressions/Exists.cpp new file mode 100644 index 0000000..5169f5e --- /dev/null +++ b/src/plasp/pddl/expressions/Exists.cpp @@ -0,0 +1,25 @@ +#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 new file mode 100644 index 0000000..566d516 --- /dev/null +++ b/src/plasp/pddl/expressions/ForAll.cpp @@ -0,0 +1,25 @@ +#include + +#include +#include + +namespace plasp +{ +namespace pddl +{ +namespace expressions +{ + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// ForAll +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +const std::string ForAll::Identifier = "forall"; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +} +} +}