2016-06-02 16:06:11 +02:00
|
|
|
#include <plasp/pddl/Expression.h>
|
|
|
|
|
|
|
|
#include <plasp/pddl/Context.h>
|
|
|
|
#include <plasp/pddl/Identifier.h>
|
2016-06-03 13:33:05 +02:00
|
|
|
#include <plasp/pddl/expressions/And.h>
|
2016-06-04 17:20:25 +02:00
|
|
|
#include <plasp/pddl/expressions/Imply.h>
|
2016-06-03 13:33:05 +02:00
|
|
|
#include <plasp/pddl/expressions/Not.h>
|
|
|
|
#include <plasp/pddl/expressions/Or.h>
|
|
|
|
#include <plasp/pddl/expressions/Predicate.h>
|
2016-06-03 17:12:39 +02:00
|
|
|
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
|
|
|
#include <plasp/pddl/expressions/Reference.h>
|
2016-06-04 17:47:41 +02:00
|
|
|
#include <plasp/utils/IO.h>
|
2016-06-02 16:06:11 +02:00
|
|
|
#include <plasp/utils/ParserException.h>
|
|
|
|
|
|
|
|
namespace plasp
|
|
|
|
{
|
|
|
|
namespace pddl
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Expression
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer parseExpressionContent(const std::string &expressionIdentifier,
|
2016-06-04 16:24:10 +02:00
|
|
|
Context &context, const expressions::Variables ¶meters);
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer parseEffectBodyExpressionContent(const std::string &expressionIdentifier,
|
2016-06-04 16:24:10 +02:00
|
|
|
Context &context, const expressions::Variables ¶meters);
|
|
|
|
ExpressionPointer parsePredicate(Context &context, const expressions::Variables ¶meters);
|
2016-06-02 21:53:10 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void throwUnsupported(const utils::Parser &parser, const std::string &expressionIdentifier)
|
|
|
|
{
|
2016-06-04 18:21:42 +02:00
|
|
|
throw utils::ParserException(parser, "Expression type \"" + expressionIdentifier + "\" currently unsupported in this context");
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
2016-06-02 18:31:47 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
ExpressionPointer parsePreconditionExpression(Context &context,
|
2016-06-03 13:33:05 +02:00
|
|
|
const expressions::Variables ¶meters)
|
2016-06-02 16:06:11 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>("(");
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
const auto expressionIdentifier = context.parser.parseIdentifier(isIdentifier);
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2016-06-02 17:35:01 +02:00
|
|
|
if (expressionIdentifier == "and")
|
2016-06-03 13:20:52 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
expression = expressions::And::parse(context, parameters,
|
2016-06-03 13:20:52 +02:00
|
|
|
parsePreconditionExpression);
|
|
|
|
}
|
2016-06-02 21:53:10 +02:00
|
|
|
else if (expressionIdentifier == "forall"
|
|
|
|
|| expressionIdentifier == "preference")
|
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
throwUnsupported(context.parser, expressionIdentifier);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
2016-06-02 17:35:01 +02:00
|
|
|
else
|
2016-06-04 16:24:10 +02:00
|
|
|
expression = parseExpressionContent(expressionIdentifier, context, parameters);
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>(")");
|
2016-06-02 18:31:47 +02:00
|
|
|
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
ExpressionPointer parseExpression(Context &context, const expressions::Variables ¶meters)
|
2016-06-02 18:31:47 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>("(");
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
const auto expressionIdentifier = context.parser.parseIdentifier(isIdentifier);
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
auto expression = parseExpressionContent(expressionIdentifier, context, parameters);
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>(")");
|
2016-06-02 16:28:08 +02:00
|
|
|
|
2016-06-02 17:35:01 +02:00
|
|
|
return expression;
|
2016-06-02 16:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer parseExpressionContent(const std::string &expressionIdentifier,
|
2016-06-04 16:24:10 +02:00
|
|
|
Context &context, const expressions::Variables ¶meters)
|
2016-06-02 18:31:47 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.skipWhiteSpace();
|
2016-06-02 18:31:47 +02:00
|
|
|
|
|
|
|
if (expressionIdentifier == "and")
|
2016-06-04 18:12:41 +02:00
|
|
|
return expressions::And::parse(context, parameters, parseExpression);
|
|
|
|
|
|
|
|
if (expressionIdentifier == "or")
|
|
|
|
return expressions::Or::parse(context, parameters, parseExpression);
|
|
|
|
|
|
|
|
if (expressionIdentifier == "not")
|
|
|
|
return expressions::Not::parse(context, parameters, parseExpression);
|
|
|
|
|
|
|
|
if (expressionIdentifier == "imply")
|
|
|
|
return expressions::Imply::parse(context, parameters, parseExpression);
|
|
|
|
|
|
|
|
if (expressionIdentifier == "exists"
|
2016-06-02 18:31:47 +02:00
|
|
|
|| expressionIdentifier == "forall"
|
|
|
|
|| expressionIdentifier == "-"
|
|
|
|
|| expressionIdentifier == "="
|
|
|
|
|| expressionIdentifier == "*"
|
|
|
|
|| expressionIdentifier == "+"
|
|
|
|
|| expressionIdentifier == "-"
|
|
|
|
|| expressionIdentifier == "/"
|
|
|
|
|| expressionIdentifier == ">"
|
|
|
|
|| expressionIdentifier == "<"
|
|
|
|
|| expressionIdentifier == "="
|
|
|
|
|| expressionIdentifier == ">="
|
|
|
|
|| expressionIdentifier == "<=")
|
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
throwUnsupported(context.parser, expressionIdentifier);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 18:12:41 +02:00
|
|
|
// Check if predicate with that name exists
|
|
|
|
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
|
|
|
[&](const auto &predicate)
|
|
|
|
{
|
|
|
|
return predicate->name() == expressionIdentifier;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If predicate exists, parse it
|
|
|
|
if (match != context.predicateDeclarations.cend())
|
|
|
|
return expressions::Predicate::parse(expressionIdentifier, context, parameters);
|
|
|
|
|
|
|
|
throw utils::ParserException(context.parser, "Expression \"" + expressionIdentifier + "\" not allowed in this context");
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
ExpressionPointer parseEffectExpression(Context &context, const expressions::Variables ¶meters)
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>("(");
|
2016-06-02 21:53:10 +02:00
|
|
|
|
2016-06-04 18:28:43 +02:00
|
|
|
const auto expressionIdentifier = context.parser.parseIdentifier(isIdentifier);
|
2016-06-02 21:53:10 +02:00
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 21:53:10 +02:00
|
|
|
|
|
|
|
if (expressionIdentifier == "and")
|
2016-06-04 16:24:10 +02:00
|
|
|
expression = expressions::And::parse(context, parameters, parseEffectExpression);
|
2016-06-02 21:53:10 +02:00
|
|
|
else if (expressionIdentifier == "forall"
|
|
|
|
|| expressionIdentifier == "when")
|
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
throwUnsupported(context.parser, expressionIdentifier);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
else
|
2016-06-04 16:24:10 +02:00
|
|
|
expression = parseEffectBodyExpressionContent(expressionIdentifier, context, parameters);
|
2016-06-02 21:53:10 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>(")");
|
2016-06-02 21:53:10 +02:00
|
|
|
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer parseEffectBodyExpressionContent(const std::string &expressionIdentifier,
|
2016-06-04 16:24:10 +02:00
|
|
|
Context &context, const expressions::Variables ¶meters)
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 21:53:10 +02:00
|
|
|
|
2016-06-02 22:31:02 +02:00
|
|
|
if (expressionIdentifier == "not")
|
2016-06-04 18:12:41 +02:00
|
|
|
return expressions::Not::parse(context, parameters, parsePredicate);
|
|
|
|
|
|
|
|
if (expressionIdentifier == "="
|
2016-06-02 21:53:10 +02:00
|
|
|
|| expressionIdentifier == "assign"
|
|
|
|
|| expressionIdentifier == "scale-up"
|
|
|
|
|| expressionIdentifier == "scale-down"
|
|
|
|
|| expressionIdentifier == "increase"
|
|
|
|
|| expressionIdentifier == "decrease")
|
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
throwUnsupported(context.parser, expressionIdentifier);
|
2016-06-02 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 18:12:41 +02:00
|
|
|
// Check if predicate with that name exists
|
|
|
|
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
|
|
|
[&](const auto &predicate)
|
|
|
|
{
|
|
|
|
return predicate->name() == expressionIdentifier;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If predicate exists, parse it
|
|
|
|
if (match != context.predicateDeclarations.cend())
|
|
|
|
return expressions::Predicate::parse(expressionIdentifier, context, parameters);
|
|
|
|
|
|
|
|
throw utils::ParserException(context.parser, "Expression \"" + expressionIdentifier + "\" not allowed in this context");
|
2016-06-02 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
ExpressionPointer parsePredicate(Context &context, const expressions::Variables ¶meters)
|
2016-06-02 22:31:02 +02:00
|
|
|
{
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>("(");
|
2016-06-02 22:31:02 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
const auto predicateName = context.parser.parseIdentifier(isIdentifier);
|
2016-06-02 22:31:02 +02:00
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 22:31:02 +02:00
|
|
|
|
|
|
|
// Check if predicate with that name exists
|
2016-06-03 17:12:39 +02:00
|
|
|
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
2016-06-02 22:31:02 +02:00
|
|
|
[&](const auto &predicate)
|
|
|
|
{
|
|
|
|
return predicate->name() == predicateName;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If predicate exists, parse it
|
2016-06-03 17:12:39 +02:00
|
|
|
if (match == context.predicateDeclarations.cend())
|
2016-06-04 16:24:10 +02:00
|
|
|
throw utils::ParserException(context.parser, "Unknown predicate \"" + predicateName + "\"");
|
2016-06-02 22:31:02 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
expression = expressions::Predicate::parse(predicateName, context, parameters);
|
2016-06-02 22:31:02 +02:00
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
context.parser.expect<std::string>(")");
|
2016-06-02 22:31:02 +02:00
|
|
|
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-02 16:06:11 +02:00
|
|
|
}
|
|
|
|
}
|