Finished basic support for action parsing.

This commit is contained in:
Patrick Lühne 2016-06-02 22:31:02 +02:00
parent fb5a5a71a5
commit fcde9c9b6b
4 changed files with 121 additions and 16 deletions

View File

@ -0,0 +1,58 @@
#ifndef __PLASP__PDDL__EXPRESSION__NOT_EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION__NOT_EXPRESSION_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Predicate.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NotExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class NotExpression: public Expression
{
public:
template<typename ExpressionParser>
static std::unique_ptr<NotExpression> parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
const Expression &argument();
private:
NotExpression() = default;
std::unique_ptr<Expression> m_argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename ExpressionParser>
std::unique_ptr<NotExpression> NotExpression::parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression)
{
auto expression = std::make_unique<NotExpression>(NotExpression());
parser.skipWhiteSpace();
// Parse argument
expression->m_argument = parseExpression(parser, context, parameters);
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -57,8 +57,6 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context)
action->m_effect = parseEffectExpression(parser, context, action->m_parameters);
}
parser.expect<std::string>(")");
// Store new action
context.actions.emplace_back(std::move(action));

View File

@ -3,6 +3,7 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/AndExpression.h>
#include <plasp/pddl/expressions/NotExpression.h>
#include <plasp/pddl/expressions/OrExpression.h>
#include <plasp/pddl/expressions/PredicateExpression.h>
#include <plasp/utils/ParserException.h>
@ -20,6 +21,7 @@ namespace pddl
std::unique_ptr<Expression> parseExpressionContent(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables &parameters);
std::unique_ptr<Expression> parseEffectBodyExpressionContent(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables &parameters);
std::unique_ptr<Expression> parsePredicateExpression(utils::Parser &parser, Context &context, const Variables &parameters);
////////////////////////////////////////////////////////////////////////////////////////////////////
@ -32,8 +34,6 @@ void throwUnsupported(const utils::Parser &parser, const std::string &expression
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
parser.expect<std::string>("(");
const auto expressionIdentifier = parser.parseIdentifier(isIdentifier);
@ -59,8 +59,6 @@ std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, C
std::unique_ptr<Expression> parseExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
parser.expect<std::string>("(");
const auto expressionIdentifier = parser.parseIdentifier(isIdentifier);
@ -84,8 +82,9 @@ std::unique_ptr<Expression> parseExpressionContent(const std::string &expression
expression = expressions::AndExpression::parse(parser, context, parameters, parseExpression);
else if (expressionIdentifier == "or")
expression = expressions::OrExpression::parse(parser, context, parameters, parseExpression);
else if (expressionIdentifier == "not"
|| expressionIdentifier == "imply"
else if (expressionIdentifier == "not")
expression = expressions::NotExpression::parse(parser, context, parameters, parseExpression);
else if (expressionIdentifier == "imply"
|| expressionIdentifier == "exists"
|| expressionIdentifier == "forall"
|| expressionIdentifier == "-"
@ -125,14 +124,10 @@ std::unique_ptr<Expression> parseExpressionContent(const std::string &expression
std::unique_ptr<Expression> parseEffectExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
parser.expect<std::string>("(");
const auto expressionIdentifier = parser.parseIdentifier(isIdentifier);
std::cout << "Parsing effect expression " << expressionIdentifier << std::endl;
std::unique_ptr<Expression> expression;
if (expressionIdentifier == "and")
@ -154,12 +149,11 @@ std::unique_ptr<Expression> parseEffectExpression(utils::Parser &parser, Context
std::unique_ptr<Expression> parseEffectBodyExpressionContent(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
std::unique_ptr<Expression> expression;
if (expressionIdentifier == "not"
|| expressionIdentifier == "="
if (expressionIdentifier == "not")
expression = expressions::NotExpression::parse(parser, context, parameters, parsePredicateExpression);
else if (expressionIdentifier == "="
|| expressionIdentifier == "assign"
|| expressionIdentifier == "scale-up"
|| expressionIdentifier == "scale-down"
@ -189,5 +183,33 @@ std::unique_ptr<Expression> parseEffectBodyExpressionContent(const std::string &
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parsePredicateExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.expect<std::string>("(");
const auto predicateName = parser.parseIdentifier(isIdentifier);
std::unique_ptr<Expression> expression;
// Check if predicate with that name exists
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
[&](const auto &predicate)
{
return predicate->name() == predicateName;
});
// If predicate exists, parse it
if (match == context.predicates.cend())
throw utils::ParserException(parser.row(), parser.column(), "Unknown predicate \"" + predicateName + "\"");
expression = expressions::PredicateExpression::parse(predicateName, parser, context, parameters);
parser.expect<std::string>(")");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}

View File

@ -0,0 +1,27 @@
#include <plasp/pddl/expressions/NotExpression.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NotExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
void NotExpression::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}