Implemented predicate expressions.
This commit is contained in:
parent
7baf15d9f0
commit
7271a5e52b
@ -33,6 +33,7 @@ class Expression
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables ¶meters);
|
||||
std::unique_ptr<Expression> parseExpression(utils::Parser &parser, Context &context, const Variables ¶meters);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@ -39,7 +39,7 @@ std::unique_ptr<AndExpression> AndExpression::parse(utils::Parser &parser, Conte
|
||||
expression->NAryExpression::parse(parser, context, parameters, parseExpression);
|
||||
|
||||
if (expression->arguments().empty())
|
||||
throw ConsistencyException("Expressions should not be empty");
|
||||
throw ConsistencyException("\"and\" expressions should not be empty");
|
||||
|
||||
return expression;
|
||||
}
|
||||
|
53
include/plasp/pddl/expressions/OrExpression.h
Normal file
53
include/plasp/pddl/expressions/OrExpression.h
Normal 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 ¶meters, 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 ¶meters, 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
|
42
include/plasp/pddl/expressions/PredicateExpression.h
Normal file
42
include/plasp/pddl/expressions/PredicateExpression.h
Normal 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 ¶meters);
|
||||
|
||||
public:
|
||||
void accept(ExpressionVisitor &expressionVisitor) const override;
|
||||
|
||||
private:
|
||||
PredicateExpression() = default;
|
||||
|
||||
std::string m_name;
|
||||
Variables m_arguments;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@ -52,7 +53,8 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
|
||||
if (sectionIdentifier == "precondition")
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/AndExpression.h>
|
||||
#include <plasp/pddl/expressions/OrExpression.h>
|
||||
#include <plasp/pddl/expressions/PredicateExpression.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
@ -16,6 +18,10 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::unique_ptr<Expression> parseExpressionBody(const std::string &expressionIdentifier, utils::Parser &parser, Context &context, const Variables ¶meters);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables ¶meters)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
@ -24,26 +30,24 @@ std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, C
|
||||
|
||||
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;
|
||||
|
||||
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")
|
||||
expression = expressions::AndExpression::parse(parser, context, parameters, parsePreconditionExpression);
|
||||
else if (expressionIdentifier == "or")
|
||||
throwNotAllowed();
|
||||
else if (expressionIdentifier == "not")
|
||||
throwNotAllowed();
|
||||
else if (expressionIdentifier == "exists")
|
||||
throwNotAllowed();
|
||||
else if (expressionIdentifier == "forall")
|
||||
throwUnsupported();
|
||||
else if (expressionIdentifier == "preference")
|
||||
throwUnsupported();
|
||||
else
|
||||
throw utils::ParserException(parser.row(), parser.column(), "Undeclared expression \"" + expressionIdentifier + "\"");
|
||||
expression = parseExpressionBody(expressionIdentifier, parser, context, parameters);
|
||||
|
||||
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 ¶meters)
|
||||
{
|
||||
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 ¶meters)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
#include <plasp/pddl/expressions/AndExpression.h>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
#include <plasp/utils/Parser.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
|
27
src/plasp/pddl/expressions/OrExpression.cpp
Normal file
27
src/plasp/pddl/expressions/OrExpression.cpp
Normal 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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
}
|
46
src/plasp/pddl/expressions/PredicateExpression.cpp
Normal file
46
src/plasp/pddl/expressions/PredicateExpression.cpp
Normal 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 ¶meters)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user