Started supporting quantified expressions.

This commit is contained in:
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