2016-06-02 16:06:11 +02:00
|
|
|
#include <plasp/pddl/Expression.h>
|
|
|
|
|
2016-12-02 01:16:22 +01:00
|
|
|
#include <plasp/output/TranslatorException.h>
|
2016-06-02 16:06:11 +02:00
|
|
|
#include <plasp/pddl/Context.h>
|
2016-06-07 13:26:19 +02:00
|
|
|
#include <plasp/pddl/Domain.h>
|
|
|
|
#include <plasp/pddl/ExpressionContext.h>
|
2016-06-09 01:25:18 +02:00
|
|
|
#include <plasp/pddl/IO.h>
|
2016-06-03 13:33:05 +02:00
|
|
|
#include <plasp/pddl/expressions/And.h>
|
2016-09-06 16:57:31 +02:00
|
|
|
#include <plasp/pddl/expressions/Exists.h>
|
|
|
|
#include <plasp/pddl/expressions/ForAll.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>
|
2016-06-14 01:31:22 +02:00
|
|
|
#include <plasp/pddl/expressions/Unsupported.h>
|
2016-09-08 15:56:30 +02:00
|
|
|
#include <plasp/pddl/expressions/When.h>
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
#include <tokenize/TokenizerException.h>
|
2017-05-09 15:05:59 +02:00
|
|
|
|
2016-06-02 16:06:11 +02:00
|
|
|
namespace plasp
|
|
|
|
{
|
|
|
|
namespace pddl
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Expression
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-08 03:42:32 +02:00
|
|
|
ExpressionPointer Expression::copy()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-04 19:29:05 +02:00
|
|
|
ExpressionPointer Expression::normalized()
|
2016-09-04 23:30:08 +02:00
|
|
|
{
|
2016-12-07 01:56:06 +01:00
|
|
|
return reduced()->simplified()->existentiallyQuantified()->simplified();
|
2016-09-04 23:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-05 00:06:09 +02:00
|
|
|
ExpressionPointer Expression::reduced()
|
2016-09-04 23:30:08 +02:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-07 01:56:06 +01:00
|
|
|
ExpressionPointer Expression::existentiallyQuantified()
|
2016-09-04 19:29:05 +02:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:34:26 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-05 00:37:43 +02:00
|
|
|
ExpressionPointer Expression::simplified()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-05-09 13:00:44 +02:00
|
|
|
ExpressionPointer Expression::decomposed(expressions::DerivedPredicates &)
|
2016-09-09 16:42:16 +02:00
|
|
|
{
|
2016-12-07 01:56:06 +01:00
|
|
|
throw output::TranslatorException("expression cannot be decomposed (not normalized)");
|
2016-09-09 16:42:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-04 19:29:05 +02:00
|
|
|
ExpressionPointer Expression::negated()
|
|
|
|
{
|
2016-12-07 02:11:54 +01:00
|
|
|
if (is<expressions::Not>())
|
2016-12-07 02:29:48 +01:00
|
|
|
return as<expressions::Not>().argument();
|
2016-09-04 19:29:05 +02:00
|
|
|
|
|
|
|
auto notExpression = expressions::NotPointer(new expressions::Not);
|
|
|
|
notExpression->setArgument(this);
|
|
|
|
|
|
|
|
return notExpression;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-12-07 01:56:06 +01:00
|
|
|
// TODO: implement better (visitor pattern?)
|
|
|
|
void Expression::collectParameters(std::set<expressions::VariablePointer> &)
|
|
|
|
{
|
|
|
|
throw output::TranslatorException("expression parameters could not be collected (expression not normalized)");
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext);
|
2016-06-07 13:26:19 +02:00
|
|
|
ExpressionPointer parsePredicate(Context &context, ExpressionContext &expressionContext);
|
2016-06-02 21:53:10 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-04 16:24:10 +02:00
|
|
|
ExpressionPointer parsePreconditionExpression(Context &context,
|
2016-06-07 13:26:19 +02:00
|
|
|
ExpressionContext &expressionContext)
|
2016-06-02 16:06:11 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
auto &tokenizer = context.tokenizer;
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.skipWhiteSpace();
|
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-09-06 22:36:48 +02:00
|
|
|
if ((expression = expressions::And::parse(context, expressionContext, parsePreconditionExpression))
|
|
|
|
|| (expression = expressions::ForAll::parse(context, expressionContext, parsePreconditionExpression)))
|
|
|
|
{
|
2016-09-06 16:57:31 +02:00
|
|
|
return expression;
|
2016-09-06 22:36:48 +02:00
|
|
|
}
|
2016-09-06 16:57:31 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto position = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.expect<std::string>("(");
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto expressionIdentifierPosition = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (tokenizer.testIdentifierAndSkip("preference"))
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2016-06-09 17:51:10 +02:00
|
|
|
// TODO: refactor
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-14 01:31:22 +02:00
|
|
|
return expressions::Unsupported::parse(context);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-09 17:51:10 +02:00
|
|
|
return parseExpression(context, expressionContext);
|
2016-06-02 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-07 13:26:19 +02:00
|
|
|
ExpressionPointer parseExpression(Context &context, ExpressionContext &expressionContext)
|
2016-06-02 18:31:47 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
auto &tokenizer = context.tokenizer;
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.skipWhiteSpace();
|
2016-06-02 18:31:47 +02:00
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
if ((expression = expressions::And::parse(context, expressionContext, parseExpression))
|
|
|
|
|| (expression = expressions::Or::parse(context, expressionContext, parseExpression))
|
2016-09-06 16:57:31 +02:00
|
|
|
|| (expression = expressions::Exists::parse(context, expressionContext, parseExpression))
|
|
|
|
|| (expression = expressions::ForAll::parse(context, expressionContext, parseExpression))
|
2016-06-09 17:51:10 +02:00
|
|
|
|| (expression = expressions::Not::parse(context, expressionContext, parseExpression))
|
|
|
|
|| (expression = expressions::Imply::parse(context, expressionContext, parseExpression))
|
|
|
|
|| (expression = expressions::Predicate::parse(context, expressionContext)))
|
|
|
|
{
|
|
|
|
return expression;
|
|
|
|
}
|
2016-06-02 16:06:11 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto position = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.expect<std::string>("(");
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto expressionIdentifierPosition = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (tokenizer.testIdentifierAndSkip("-")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("=")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("*")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("+")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("-")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("/")
|
|
|
|
|| tokenizer.testIdentifierAndSkip(">")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("<")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("=")
|
|
|
|
|| tokenizer.testIdentifierAndSkip(">=")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("<="))
|
2016-06-02 18:31:47 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-14 01:31:22 +02:00
|
|
|
return expressions::Unsupported::parse(context);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-04 18:12:41 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
|
|
|
throw tokenize::TokenizerException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in this context");
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-07 13:26:19 +02:00
|
|
|
ExpressionPointer parseEffectExpression(Context &context, ExpressionContext &expressionContext)
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
auto &tokenizer = context.tokenizer;
|
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-09-06 22:36:48 +02:00
|
|
|
if ((expression = expressions::And::parse(context, expressionContext, parseEffectExpression))
|
2016-09-08 15:56:30 +02:00
|
|
|
|| (expression = expressions::ForAll::parse(context, expressionContext, parseEffectExpression))
|
|
|
|
|| (expression = expressions::When::parse(context, expressionContext, parseExpression, parseConditionalEffectExpression)))
|
2016-09-06 22:36:48 +02:00
|
|
|
{
|
2016-06-09 17:51:10 +02:00
|
|
|
return expression;
|
2016-09-06 22:36:48 +02:00
|
|
|
}
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto position = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.expect<std::string>("(");
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto expressionIdentifierPosition = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (tokenizer.testIdentifierAndSkip("when"))
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-14 01:31:22 +02:00
|
|
|
return expressions::Unsupported::parse(context);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-09 17:51:10 +02:00
|
|
|
return parseEffectBodyExpression(context, expressionContext);
|
2016-06-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
ExpressionPointer parseEffectBodyExpression(Context &context, ExpressionContext &expressionContext)
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
auto &tokenizer = context.tokenizer;
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2016-06-03 13:20:52 +02:00
|
|
|
ExpressionPointer expression;
|
2016-06-02 21:53:10 +02:00
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
if ((expression = expressions::Not::parse(context, expressionContext, parsePredicate))
|
|
|
|
|| (expression = expressions::Predicate::parse(context, expressionContext)))
|
|
|
|
{
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto position = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.expect<std::string>("(");
|
2016-06-04 18:12:41 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto expressionIdentifierPosition = tokenizer.position();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (tokenizer.testIdentifierAndSkip("=")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("assign")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("scale-up")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("scale-down")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("increase")
|
|
|
|
|| tokenizer.testIdentifierAndSkip("decrease"))
|
2016-06-02 21:53:10 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-09 17:51:10 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-14 01:31:22 +02:00
|
|
|
return expressions::Unsupported::parse(context);
|
2016-06-02 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-07 13:26:19 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
|
|
|
throw tokenize::TokenizerException(tokenizer.location(), "expression type “" + expressionIdentifier + "” unknown or not allowed in this context");
|
2016-06-02 18:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-09-08 15:56:30 +02:00
|
|
|
ExpressionPointer parseConditionalEffectExpression(Context &context, ExpressionContext &expressionContext)
|
|
|
|
{
|
|
|
|
ExpressionPointer expression;
|
|
|
|
|
|
|
|
if ((expression = expressions::And::parse(context, expressionContext, parseEffectBodyExpression)))
|
|
|
|
return expression;
|
|
|
|
|
|
|
|
return parseEffectBodyExpression(context, expressionContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-07 13:26:19 +02:00
|
|
|
ExpressionPointer parsePredicate(Context &context, ExpressionContext &expressionContext)
|
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
|
|
|
|
2016-06-09 17:51:10 +02:00
|
|
|
if ((expression = expressions::Predicate::parse(context, expressionContext)))
|
|
|
|
return expression;
|
2016-06-02 22:31:02 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
throw tokenize::TokenizerException(context.tokenizer.location(), "expected predicate");
|
2016-06-02 22:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-13 19:02:15 +02:00
|
|
|
ExpressionPointer parseLiteral(Context &context, ExpressionContext &expressionContext)
|
|
|
|
{
|
|
|
|
ExpressionPointer expression;
|
|
|
|
|
|
|
|
if ((expression = parseAtomicFormula(context, expressionContext))
|
|
|
|
|| (expression = expressions::Not::parse(context, expressionContext, parseAtomicFormula)))
|
|
|
|
{
|
|
|
|
return expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ExpressionPointer parseAtomicFormula(Context &context, ExpressionContext &expressionContext)
|
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
auto &tokenizer = context.tokenizer;
|
2016-06-13 19:02:15 +02:00
|
|
|
|
|
|
|
ExpressionPointer expression;
|
|
|
|
|
|
|
|
if ((expression = expressions::Predicate::parse(context, expressionContext)))
|
|
|
|
return expression;
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto position = tokenizer.position();
|
2016-06-13 19:02:15 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (!tokenizer.testAndSkip<std::string>("("))
|
2016-06-14 01:31:22 +02:00
|
|
|
return nullptr;
|
2016-06-13 19:02:15 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
const auto expressionIdentifierPosition = tokenizer.position();
|
2016-06-13 19:02:15 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
if (tokenizer.testIdentifierAndSkip("="))
|
2016-06-13 19:02:15 +02:00
|
|
|
{
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(expressionIdentifierPosition);
|
|
|
|
const auto expressionIdentifier = tokenizer.getIdentifier();
|
2016-06-13 19:02:15 +02:00
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-14 01:31:22 +02:00
|
|
|
return expressions::Unsupported::parse(context);
|
2016-06-13 19:02:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 14:17:57 +02:00
|
|
|
tokenizer.seek(position);
|
2016-06-13 19:02:15 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-02 16:06:11 +02:00
|
|
|
}
|
|
|
|
}
|