Implemented And expressions.

This commit is contained in:
Patrick Lühne 2016-06-02 17:35:01 +02:00
parent d070b5be9e
commit 7baf15d9f0
9 changed files with 206 additions and 9 deletions

View File

@ -5,6 +5,7 @@
#include <boost/variant.hpp>
#include <plasp/pddl/Variable.h>
#include <plasp/utils/Parser.h>
namespace plasp
@ -31,7 +32,7 @@ class Expression
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context);
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters);
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -21,7 +21,7 @@ class Expression;
class ExpressionVisitor
{
public:
virtual void apply(const Expression &expression)
virtual void visit(const Expression &expression)
{
expression.accept(*this);
}

View File

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

View File

@ -0,0 +1,63 @@
#ifndef __PLASP__PDDL__EXPRESSION__N_ARY_EXPRESSION_H
#define __PLASP__PDDL__EXPRESSION__N_ARY_EXPRESSION_H
#include <memory>
#include <string>
#include <vector>
#include <plasp/pddl/ConsistencyException.h>
#include <plasp/pddl/Expression.h>
#include <plasp/utils/Parser.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NAryExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class NAryExpression: public Expression
{
public:
const std::vector<std::unique_ptr<Expression>> &arguments() const;
protected:
template<typename ExpressionParser>
void parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression);
private:
std::vector<std::unique_ptr<Expression>> m_arguments;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename ExpressionParser>
void NAryExpression::parse(utils::Parser &parser, Context &context, const Variables &parameters, ExpressionParser parseExpression)
{
std::cout << "Parsing n-ary predicate" << std::endl;
parser.skipWhiteSpace();
// Assume that expression identifier (and, or, etc.) is already parsed
// Parse arguments of the expression
while (parser.currentCharacter() != ')')
{
m_arguments.emplace_back(parseExpression(parser, context, parameters));
parser.skipWhiteSpace();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -6,6 +6,9 @@ file(GLOB core_headers "../include/plasp/*.h")
file(GLOB pddl_sources "plasp/pddl/*.cpp")
file(GLOB pddl_headers "../include/plasp/pddl/*.h")
file(GLOB pddl_expressions_sources "plasp/pddl/expressions/*.cpp")
file(GLOB pddl_expressions_headers "../include/plasp/pddl/expressions/*.h")
file(GLOB sas_sources "plasp/sas/*.cpp")
file(GLOB sas_headers "../include/plasp/sas/*.h")
@ -27,6 +30,9 @@ set(sources
${pddl_sources}
${pddl_headers}
${pddl_expressions_sources}
${pddl_expressions_headers}
${sas_sources}
${sas_headers}

View File

@ -51,7 +51,7 @@ Action &Action::parseDeclaration(utils::Parser &parser, Context &context)
const auto sectionIdentifier = parser.parseIdentifier(isIdentifier);
if (sectionIdentifier == "precondition")
action->m_precondition = parsePreconditionExpression(parser, context);
action->m_precondition = parsePreconditionExpression(parser, context, action->m_parameters);
//else if (sectionIdentifier == "effect")
// action->m_effect = parseEffectExpression(parser, context);
}

View File

@ -2,7 +2,7 @@
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Identifier.h>
#include <plasp/pddl/expressions/NAryExpression.h>
#include <plasp/pddl/expressions/AndExpression.h>
#include <plasp/utils/ParserException.h>
namespace plasp
@ -16,7 +16,7 @@ namespace pddl
//
////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context)
std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, Context &context, const Variables &parameters)
{
parser.skipWhiteSpace();
@ -24,16 +24,30 @@ std::unique_ptr<Expression> parsePreconditionExpression(utils::Parser &parser, C
const auto expressionIdentifier = parser.parseIdentifier(isIdentifier);
std::cout << "Parsing identifier " << expressionIdentifier << std::endl;
std::unique_ptr<Expression> expression;
throw utils::ParserException(parser.row(), parser.column(), "Expression of type \"" + expressionIdentifier + "\" not allowed in preference declaration");
const auto throwNotAllowed =
[&]()
{
throw utils::ParserException(parser.row(), parser.column(), "Expression of type \"" + expressionIdentifier + "\" not allowed in preference declaration");
};
//if (expressionIdentifier == "and")
// expression = NAry
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
throw utils::ParserException(parser.row(), parser.column(), "Undeclared expression \"" + expressionIdentifier + "\"");
parser.expect<std::string>(")");
return std::move(expression);
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////

View File

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

View File

@ -0,0 +1,31 @@
#include <plasp/pddl/expressions/NAryExpression.h>
#include <algorithm>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/ExpressionVisitor.h>
#include <plasp/pddl/Identifier.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// NAryExpression
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<std::unique_ptr<Expression>> &NAryExpression::arguments() const
{
return m_arguments;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}