Started supporting quantified expressions.

This commit is contained in:
Patrick Lühne 2016-09-06 16:57:31 +02:00
parent ca6664feb2
commit f4f0e07bc1
7 changed files with 267 additions and 5 deletions

View File

@ -93,6 +93,8 @@ class Expression
Constant,
Dummy,
Either,
Exists,
ForAll,
Imply,
Not,
Or,

View File

@ -0,0 +1,33 @@
#ifndef __PLASP__PDDL__EXPRESSIONS__EXISTS_H
#define __PLASP__PDDL__EXPRESSIONS__EXISTS_H
#include <plasp/pddl/expressions/Quantified.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Exists
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class Exists: public Quantified<Exists>
{
public:
static const Expression::Type ExpressionType = Expression::Type::Exists;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,33 @@
#ifndef __PLASP__PDDL__EXPRESSIONS__FOR_ALL_H
#define __PLASP__PDDL__EXPRESSIONS__FOR_ALL_H
#include <plasp/pddl/expressions/Quantified.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ForAll
//
////////////////////////////////////////////////////////////////////////////////////////////////////
class ForAll: public Quantified<ForAll>
{
public:
static const Expression::Type ExpressionType = Expression::Type::ForAll;
static const std::string Identifier;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -0,0 +1,140 @@
#ifndef __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H
#define __PLASP__PDDL__EXPRESSIONS__QUANTIFIED_H
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Variable.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Quantified
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class Quantified: public ExpressionCRTP<Derived>
{
public:
template<typename ExpressionParser>
static boost::intrusive_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
public:
void setArgument(ExpressionPointer argument);
ExpressionPointer argument() const;
ExpressionPointer reduced() override;
ExpressionPointer negationNormalized() override;
void print(std::ostream &ostream) const override;
protected:
Variables m_variables;
ExpressionPointer m_argument;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
template<typename ExpressionParser>
boost::intrusive_ptr<Derived> Quantified<Derived>::parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression)
{
auto &parser = context.parser;
const auto position = parser.position();
if (!parser.testAndSkip<std::string>("(")
|| !parser.testIdentifierAndSkip(Derived::Identifier))
{
parser.seek(position);
return nullptr;
}
auto expression = boost::intrusive_ptr<Derived>(new Derived);
// Parse variable list
parser.expect<std::string>("(");
Variable::parseTypedDeclarations(context, expressionContext, expression->m_variables);
parser.expect<std::string>(")");
// Parse argument of the expression
expression->Quantified<Derived>::setArgument(parseExpression(context, expressionContext));
parser.expect<std::string>(")");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
void Quantified<Derived>::setArgument(ExpressionPointer expression)
{
m_argument = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
ExpressionPointer Quantified<Derived>::argument() const
{
return m_argument;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::reduced()
{
m_argument = m_argument->reduced();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline ExpressionPointer Quantified<Derived>::negationNormalized()
{
m_argument = m_argument->negationNormalized();
return this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
inline void Quantified<Derived>::print(std::ostream &ostream) const
{
ostream << "(" << Derived::Identifier << "(";
for (size_t i = 0; i < m_variables.size(); i++)
{
if (i > 0)
ostream << " ";
ostream << m_variables[i]->name();
}
ostream << ") ";
m_argument->print(ostream);
ostream << ")";
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif

View File

@ -5,6 +5,8 @@
#include <plasp/pddl/ExpressionContext.h>
#include <plasp/pddl/IO.h>
#include <plasp/pddl/expressions/And.h>
#include <plasp/pddl/expressions/Exists.h>
#include <plasp/pddl/expressions/ForAll.h>
#include <plasp/pddl/expressions/Imply.h>
#include <plasp/pddl/expressions/Not.h>
#include <plasp/pddl/expressions/Or.h>
@ -86,14 +88,16 @@ ExpressionPointer parsePreconditionExpression(Context &context,
if ((expression = expressions::And::parse(context, expressionContext, parsePreconditionExpression)))
return expression;
if ((expression = expressions::ForAll::parse(context, expressionContext, parsePreconditionExpression)))
return expression;
const auto position = parser.position();
parser.expect<std::string>("(");
const auto expressionIdentifierPosition = parser.position();
if (parser.testIdentifierAndSkip("forall")
|| parser.testIdentifierAndSkip("preference"))
if (parser.testIdentifierAndSkip("preference"))
{
// TODO: refactor
parser.seek(expressionIdentifierPosition);
@ -119,6 +123,8 @@ ExpressionPointer parseExpression(Context &context, ExpressionContext &expressio
if ((expression = expressions::And::parse(context, expressionContext, parseExpression))
|| (expression = expressions::Or::parse(context, expressionContext, parseExpression))
|| (expression = expressions::Exists::parse(context, expressionContext, parseExpression))
|| (expression = expressions::ForAll::parse(context, expressionContext, parseExpression))
|| (expression = expressions::Not::parse(context, expressionContext, parseExpression))
|| (expression = expressions::Imply::parse(context, expressionContext, parseExpression))
|| (expression = expressions::Predicate::parse(context, expressionContext)))
@ -132,9 +138,7 @@ ExpressionPointer parseExpression(Context &context, ExpressionContext &expressio
const auto expressionIdentifierPosition = parser.position();
if (parser.testIdentifierAndSkip("exists")
|| parser.testIdentifierAndSkip("forall")
|| parser.testIdentifierAndSkip("-")
if (parser.testIdentifierAndSkip("-")
|| parser.testIdentifierAndSkip("=")
|| parser.testIdentifierAndSkip("*")
|| parser.testIdentifierAndSkip("+")

View File

@ -0,0 +1,25 @@
#include <plasp/pddl/expressions/Exists.h>
#include <algorithm>
#include <iostream>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Exists
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string Exists::Identifier = "exists";
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}

View File

@ -0,0 +1,25 @@
#include <plasp/pddl/expressions/ForAll.h>
#include <algorithm>
#include <iostream>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ForAll
//
////////////////////////////////////////////////////////////////////////////////////////////////////
const std::string ForAll::Identifier = "forall";
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}