Implemented predicate expressions.

This commit is contained in:
Patrick Lühne 2016-06-02 18:31:47 +02:00
parent 7baf15d9f0
commit 7271a5e52b
9 changed files with 261 additions and 14 deletions

View File

@ -33,6 +33,7 @@ class Expression
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters); std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters);
std::unique_ptr<Expression> parseExpression(utils::Parser &parser, Context &context, const Variables &parameters);
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -39,7 +39,7 @@ std::unique_ptr<AndExpression> AndExpression::parse(utils::Parser &parser, Conte
expression->NAryExpression::parse(parser, context, parameters, parseExpression); expression->NAryExpression::parse(parser, context, parameters, parseExpression);
if (expression->arguments().empty()) if (expression->arguments().empty())
throw ConsistencyException("Expressions should not be empty"); throw ConsistencyException("\"and\" expressions should not be empty");
return expression; return expression;
} }

View File

@ -0,0 +1,53 @@
#ifndef __PLASP__PDDL__EXPRESSION__OR_EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION__OR_EXPRESSION_H
#include <plasp/pddl/expressions/NAryExpression.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// OrExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class OrExpression: public NAryExpression
{
public:
template<typename ExpressionParser>
static std::unique_ptr<OrExpression> parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
private:
OrExpression() = default;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename ExpressionParser>
std::unique_ptr<OrExpression> OrExpression::parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression)
{
auto expression = std::make_unique<OrExpression>(OrExpression());
expression->NAryExpression::parse(parser, context, parameters, parseExpression);
if (expression->arguments().empty())
throw ConsistencyException("\"or\" expressions should not be empty");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,42 @@
#ifndef __PLASP__PDDL__EXPRESSION__PREDICATE_EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION__PREDICATE_EXPRESSION_H
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/Predicate.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PredicateExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class PredicateExpression: public Expression
{
public:
static std::unique_ptr<PredicateExpression> parse(std::string name, utils::Parser &parser, Context &context, const Variables &parameters);
public:
void accept(ExpressionVisitor &expressionVisitor) const override;
private:
PredicateExpression() = default;
std::string m_name;
Variables m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -4,6 +4,7 @@
#include <plasp/pddl/Context.h> #include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h> #include <plasp/pddl/Identifier.h>
#include <plasp/utils/ParserException.h>
namespace plasp namespace plasp
{ {
@ -52,7 +53,8 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context)
if (sectionIdentifier == "precondition") if (sectionIdentifier == "precondition")
action->m_precondition = parsePreconditionExpression(parser, context, action->m_parameters); action->m_precondition = parsePreconditionExpression(parser, context, action->m_parameters);
//else if (sectionIdentifier == "effect") else if (sectionIdentifier == "effect")
throw utils::ParserException(parser.row(), parser.column(), "Action effects are currently unsupported");
// action->m_effect = parseEffectExpression(parser, context); // action->m_effect = parseEffectExpression(parser, context);
} }

View File

@ -3,6 +3,8 @@
#include <plasp/pddl/Context.h> #include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h> #include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/AndExpression.h> #include <plasp/pddl/expressions/AndExpression.h>
#include <plasp/pddl/expressions/OrExpression.h>
#include <plasp/pddl/expressions/PredicateExpression.h>
#include <plasp/utils/ParserException.h> #include <plasp/utils/ParserException.h>
namespace plasp namespace plasp
@ -16,6 +18,10 @@ namespace pddl
// //
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parseExpressionBody(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables &parameters);
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters) std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{ {
parser.skipWhiteSpace(); parser.skipWhiteSpace();
@ -24,26 +30,24 @@ std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, C
const auto expressionIdentifier = parser.parseIdentifier(isIdentifier); const auto expressionIdentifier = parser.parseIdentifier(isIdentifier);
std::cout << "Parsing identifier " << expressionIdentifier << std::endl; std::cout << "Parsing precondition expression " << expressionIdentifier << std::endl;
std::unique_ptr<Expression> expression; std::unique_ptr<Expression> expression;
const auto throwNotAllowed = const auto throwUnsupported =
[&]() [&]()
{ {
throw utils::ParserException(parser.row(), parser.column(), "Expression of type \"" + expressionIdentifier + "\" not allowed in preference declaration"); throw utils::ParserException(parser.row(), parser.column(), "Expression type \"" + expressionIdentifier + "\" unsupported");
}; };
if (expressionIdentifier == "and") if (expressionIdentifier == "and")
expression = expressions::AndExpression::parse(parser, context, parameters, parsePreconditionExpression); expression = expressions::AndExpression::parse(parser, context, parameters, parsePreconditionExpression);
else if (expressionIdentifier == "or") else if (expressionIdentifier == "forall")
throwNotAllowed(); throwUnsupported();
else if (expressionIdentifier == "not") else if (expressionIdentifier == "preference")
throwNotAllowed(); throwUnsupported();
else if (expressionIdentifier == "exists")
throwNotAllowed();
else else
throw utils::ParserException(parser.row(), parser.column(), "Undeclared expression \"" + expressionIdentifier + "\""); expression = parseExpressionBody(expressionIdentifier, parser, context, parameters);
parser.expect<std::string>(")"); parser.expect<std::string>(")");
@ -52,5 +56,79 @@ 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);
auto expression = parseExpressionBody(expressionIdentifier, parser, context, parameters);
parser.expect<std::string>(")");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parseExpressionBody(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
std::cout << "Parsing expression " << expressionIdentifier << std::endl;
std::unique_ptr<Expression> expression;
const auto throwUnsupported =
[&]()
{
throw utils::ParserException(parser.row(), parser.column(), "Expression type \"" + expressionIdentifier + "\" unsupported");
};
if (expressionIdentifier == "and")
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"
|| expressionIdentifier == "exists"
|| expressionIdentifier == "forall"
|| expressionIdentifier == "-"
|| expressionIdentifier == "="
|| expressionIdentifier == "*"
|| expressionIdentifier == "+"
|| expressionIdentifier == "-"
|| expressionIdentifier == "/"
|| expressionIdentifier == ">"
|| expressionIdentifier == "<"
|| expressionIdentifier == "="
|| expressionIdentifier == ">="
|| expressionIdentifier == "<=")
{
throwUnsupported();
}
else
{
// 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() == expressionIdentifier;
});
// If predicate exists, parse it
if (match != context.predicates.cend())
expression = expressions::PredicateExpression::parse(expressionIdentifier, parser, context, parameters);
else
throw utils::ParserException(parser.row(), parser.column(), "Undeclared expression \"" + expressionIdentifier + "\"");
}
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
} }
} }

View File

@ -1,8 +1,6 @@
#include <plasp/pddl/expressions/AndExpression.h> #include <plasp/pddl/expressions/AndExpression.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h> #include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/utils/Parser.h>
namespace plasp namespace plasp
{ {

View File

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

View File

@ -0,0 +1,46 @@
#include <plasp/pddl/expressions/PredicateExpression.h>
#include <plasp/pddl/ExpressionVisitor.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PredicateExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<PredicateExpression> PredicateExpression::parse(std::string name, utils::Parser &parser, Context &context, const Variables &parameters)
{
auto expression = std::make_unique<PredicateExpression>(PredicateExpression());
expression->m_name = name;
parser.skipWhiteSpace();
// Parse arguments
while (parser.currentCharacter() != ')')
Variable::parseTyped(parser, context, expression->m_arguments);
std::cout << "Parsed " << expression->m_arguments.size() << " arguments" << std::endl;
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void PredicateExpression::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
{
expressionVisitor.visit(*this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}