Skip to content

Commit

Permalink
added function expression comparison goal descriptor of :numeric-flue…
Browse files Browse the repository at this point in the history
…nts requirement
  • Loading branch information
drexlerd committed Jan 2, 2025
1 parent a5abd96 commit 5a18eb3
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 17 deletions.
47 changes: 43 additions & 4 deletions include/loki/details/pddl/conditions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define LOKI_INCLUDE_LOKI_PDDL_CONDITIONS_HPP_

#include "loki/details/pddl/declarations.hpp"
#include "loki/details/pddl/function_expressions.hpp"

#include <string>

Expand Down Expand Up @@ -213,15 +214,52 @@ class ConditionForallImpl
auto identifiable_members() const { return std::forward_as_tuple(std::as_const(m_parameters), std::as_const(m_condition)); }
};

class ConditionFunctionExpressionComparisonImpl
{
private:
size_t m_index;
BinaryComparatorEnum m_binary_comparator;
FunctionExpression m_function_expression_left;
FunctionExpression m_function_expression_right;

ConditionFunctionExpressionComparisonImpl(size_t index,
BinaryComparatorEnum binary_comparator,
FunctionExpression function_expression_left,
FunctionExpression function_expression_right);

// Give access to the constructor.
template<typename T, typename Hash, typename EqualTo>
friend class SegmentedRepository;

public:
// moveable but not copyable
ConditionFunctionExpressionComparisonImpl(const ConditionFunctionExpressionComparisonImpl& other) = delete;
ConditionFunctionExpressionComparisonImpl& operator=(const ConditionFunctionExpressionComparisonImpl& other) = delete;
ConditionFunctionExpressionComparisonImpl(ConditionFunctionExpressionComparisonImpl&& other) = default;
ConditionFunctionExpressionComparisonImpl& operator=(ConditionFunctionExpressionComparisonImpl&& other) = default;

size_t get_index() const;
const BinaryComparatorEnum& get_binary_comparator() const;
const FunctionExpression& get_function_expression_left() const;
const FunctionExpression& get_function_expression_right() const;

auto identifiable_members() const
{
return std::forward_as_tuple(std::as_const(m_binary_comparator), std::as_const(m_function_expression_left), std::as_const(m_function_expression_right));
}
};

/* Condition */
using ConditionVariant = std::
variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall, ConditionFunctionExpressionComparison>;

class ConditionImpl
{
private:
size_t m_index;
std::variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall> m_condition;
ConditionVariant m_condition;

ConditionImpl(size_t index,
std::variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall> condition);
ConditionImpl(size_t index, ConditionVariant condition);

// Give access to the constructor.
template<typename T, typename Hash, typename EqualTo>
Expand All @@ -235,7 +273,7 @@ class ConditionImpl
ConditionImpl& operator=(ConditionImpl&& other) = default;

size_t get_index() const;
const std::variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall>& get_condition() const;
const ConditionVariant& get_condition() const;

auto identifiable_members() const { return std::forward_as_tuple(std::as_const(m_condition)); }
};
Expand All @@ -247,6 +285,7 @@ extern std::ostream& operator<<(std::ostream& out, const ConditionNotImpl& eleme
extern std::ostream& operator<<(std::ostream& out, const ConditionImplyImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionExistsImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionForallImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionFunctionExpressionComparisonImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionImpl& element);

}
Expand Down
2 changes: 2 additions & 0 deletions include/loki/details/pddl/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class ConditionExistsImpl;
using ConditionExists = const ConditionExistsImpl*;
class ConditionForallImpl;
using ConditionForall = const ConditionForallImpl*;
class ConditionFunctionExpressionComparisonImpl;
using ConditionFunctionExpressionComparison = const ConditionFunctionExpressionComparisonImpl*;
class ConditionImpl;
using Condition = const ConditionImpl*;
using ConditionList = std::vector<Condition>;
Expand Down
12 changes: 12 additions & 0 deletions include/loki/details/pddl/function_expressions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@

namespace loki
{
enum class BinaryComparatorEnum
{
GREATER, ///< ">"
LESS, ///< "<"
EQUAL, ///< "="
GREATER_EQUAL, ///< ">="
LESS_EQUAL, ///< "<="
};

extern std::unordered_map<BinaryComparatorEnum, std::string> binary_comparator_enum_to_string;
extern const std::string& to_string(BinaryComparatorEnum binary_comparator);

enum class BinaryOperatorEnum
{
MUL,
Expand Down
8 changes: 8 additions & 0 deletions include/loki/details/pddl/repositories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ using ConditionNotRepository = SegmentedPDDLRepository<ConditionNotImpl>;
using ConditionImplyRepository = SegmentedPDDLRepository<ConditionImplyImpl>;
using ConditionExistsRepository = SegmentedPDDLRepository<ConditionExistsImpl>;
using ConditionForallRepository = SegmentedPDDLRepository<ConditionForallImpl>;
using ConditionFunctionExpressionComparisonRepository = SegmentedPDDLRepository<ConditionFunctionExpressionComparisonImpl>;
using ConditionRepository = SegmentedPDDLRepository<ConditionImpl>;
using EffectLiteralRepository = SegmentedPDDLRepository<EffectLiteralImpl>;
using EffectAndRepository = SegmentedPDDLRepository<EffectAndImpl>;
Expand Down Expand Up @@ -114,6 +115,7 @@ using PDDLTypeToRepository =
boost::hana::pair<boost::hana::type<ConditionImplyImpl>, ConditionImplyRepository>,
boost::hana::pair<boost::hana::type<ConditionExistsImpl>, ConditionExistsRepository>,
boost::hana::pair<boost::hana::type<ConditionForallImpl>, ConditionForallRepository>,
boost::hana::pair<boost::hana::type<ConditionFunctionExpressionComparisonImpl>, ConditionFunctionExpressionComparisonRepository>,
boost::hana::pair<boost::hana::type<ConditionImpl>, ConditionRepository>,
boost::hana::pair<boost::hana::type<EffectLiteralImpl>, EffectLiteralRepository>,
boost::hana::pair<boost::hana::type<EffectAndImpl>, EffectAndRepository>,
Expand Down Expand Up @@ -205,6 +207,10 @@ class PDDLRepositories

ConditionForall get_or_create_condition_forall(ParameterList parameters, Condition condition);

ConditionFunctionExpressionComparison get_or_create_condition_function_expression_comparison(BinaryComparatorEnum binary_comparator,
FunctionExpression function_expression_left,
FunctionExpression function_expression_right);

Condition get_or_create_condition(ConditionLiteral condition);

Condition get_or_create_condition(ConditionAnd condition);
Expand All @@ -219,6 +225,8 @@ class PDDLRepositories

Condition get_or_create_condition(ConditionForall condition);

Condition get_or_create_condition(ConditionFunctionExpressionComparison condition);

EffectLiteral get_or_create_effect_literal(Literal literal);

EffectAnd get_or_create_effect_and(EffectList effects);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/parser_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ const auto binary_comparator_equal_def = lit('=') > x3::attr(ast::BinaryComparat
const auto binary_comparator_greater_equal_def = lit(">=") > x3::attr(ast::BinaryComparatorGreaterEqual {});
const auto binary_comparator_less_equal_def = lit("<=") > x3::attr(ast::BinaryComparatorLessEqual {});
const auto binary_comparator_def =
binary_comparator_greater | binary_comparator_less | binary_comparator_equal | binary_comparator_greater_equal | binary_comparator_less_equal;
binary_comparator_greater_equal | binary_comparator_less_equal | binary_comparator_greater | binary_comparator_less | binary_comparator_equal;

const auto function_head_def = ((lit('(') >> function_symbol > *term) > lit(')')) | (function_symbol > x3::attr(std::vector<ast::Term> {}));
const auto function_expression_def = function_expression_binary_op | function_expression_minus | function_expression_head | function_expression_number;
Expand All @@ -353,7 +353,7 @@ const auto goal_descriptor_not_def = (lit('(') >> keyword_lit("not")) > goal_des
const auto goal_descriptor_imply_def = (lit('(') >> keyword_lit("imply")) > goal_descriptor > goal_descriptor > lit(')');
const auto goal_descriptor_exists_def = (lit('(') >> keyword_lit("exists")) > lit('(') > typed_list_of_variables > lit(')') > goal_descriptor > lit(')');
const auto goal_descriptor_forall_def = (lit('(') >> keyword_lit("forall")) > lit('(') > typed_list_of_variables > lit(')') > goal_descriptor > lit(')');
const auto goal_descriptor_function_comparison_def = (lit('(') >> binary_comparator) >> function_expression > function_expression > lit(')');
const auto goal_descriptor_function_comparison_def = (lit('(') >> binary_comparator) > function_expression > function_expression > lit(')');

const auto constraint_goal_descriptor_def = constraint_goal_descriptor_and | constraint_goal_descriptor_forall | constraint_goal_descriptor_at_end
| constraint_goal_descriptor_always | constraint_goal_descriptor_sometime | constraint_goal_descriptor_within
Expand Down
38 changes: 28 additions & 10 deletions src/pddl/conditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,33 @@ const ParameterList& ConditionForallImpl::get_parameters() const { return m_para

const Condition& ConditionForallImpl::get_condition() const { return m_condition; }

/* Condition */
ConditionImpl::ConditionImpl(
size_t index,
std::variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall> condition) :
/* ConditionFunctionExpressionComparison */

ConditionFunctionExpressionComparisonImpl::ConditionFunctionExpressionComparisonImpl(size_t index,
BinaryComparatorEnum binary_comparator,
FunctionExpression function_expression_left,
FunctionExpression function_expression_right) :
m_index(index),
m_condition(std::move(condition))
m_binary_comparator(binary_comparator),
m_function_expression_left(function_expression_left),
m_function_expression_right(function_expression_right)
{
}

size_t ConditionFunctionExpressionComparisonImpl::get_index() const { return m_index; }

const BinaryComparatorEnum& ConditionFunctionExpressionComparisonImpl::get_binary_comparator() const { return m_binary_comparator; }

const FunctionExpression& ConditionFunctionExpressionComparisonImpl::get_function_expression_left() const { return m_function_expression_left; }

const FunctionExpression& ConditionFunctionExpressionComparisonImpl::get_function_expression_right() const { return m_function_expression_right; }

/* Condition */
ConditionImpl::ConditionImpl(size_t index, ConditionVariant condition) : m_index(index), m_condition(std::move(condition)) {}

size_t ConditionImpl::get_index() const { return m_index; }

const std::variant<ConditionLiteral, ConditionAnd, ConditionOr, ConditionNot, ConditionImply, ConditionExists, ConditionForall>&
ConditionImpl::get_condition() const
{
return m_condition;
}
const ConditionVariant& ConditionImpl::get_condition() const { return m_condition; }

std::ostream& operator<<(std::ostream& out, const ConditionLiteralImpl& element)
{
Expand Down Expand Up @@ -160,6 +171,13 @@ std::ostream& operator<<(std::ostream& out, const ConditionForallImpl& element)
return out;
}

std::ostream& operator<<(std::ostream& out, const ConditionFunctionExpressionComparisonImpl& element)
{
auto formatter = PDDLFormatter();
formatter.write(element, out);
return out;
}

std::ostream& operator<<(std::ostream& out, const ConditionImpl& element)
{
auto formatter = PDDLFormatter();
Expand Down
9 changes: 9 additions & 0 deletions src/pddl/formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ void PDDLFormatter::write(const ConditionForallImpl& element, std::ostream& out)
out << ")";
}

void PDDLFormatter::write(const ConditionFunctionExpressionComparisonImpl& element, std::ostream& out)
{
out << "(" << to_string(element.get_binary_comparator()) << " ";
write(*element.get_function_expression_left(), out);
out << " ";
write(*element.get_function_expression_right(), out);
out << ")";
}

void PDDLFormatter::write(const ConditionImpl& element, std::ostream& out)
{
std::visit([this, &out](const auto& arg) { this->write(*arg, out); }, element.get_condition());
Expand Down
1 change: 1 addition & 0 deletions src/pddl/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PDDLFormatter
void write(const ConditionImplyImpl& element, std::ostream& out);
void write(const ConditionExistsImpl& element, std::ostream& out);
void write(const ConditionForallImpl& element, std::ostream& out);
void write(const ConditionFunctionExpressionComparisonImpl& element, std::ostream& out);
void write(const ConditionImpl& element, std::ostream& out);
void write(const DomainImpl& element, std::ostream& out);
void write(const EffectLiteralImpl& element, std::ostream& out);
Expand Down
14 changes: 14 additions & 0 deletions src/pddl/function_expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@
namespace loki
{

std::unordered_map<BinaryComparatorEnum, std::string> binary_comparator_enum_to_string = {
{ BinaryComparatorEnum::GREATER, ">" }, //
{ BinaryComparatorEnum::LESS, "<" }, //
{ BinaryComparatorEnum::EQUAL, "=" }, //
{ BinaryComparatorEnum::GREATER_EQUAL, ">=" }, //
{ BinaryComparatorEnum::LESS_EQUAL, "<=" },
};

const std::string& to_string(BinaryComparatorEnum binary_comparator)
{
assert(binary_comparator_enum_to_string.count(binary_comparator));
return binary_comparator_enum_to_string.at(binary_comparator);
}

std::unordered_map<BinaryOperatorEnum, std::string> binary_operator_enum_to_string = {
{ BinaryOperatorEnum::MUL, "*" },
{ BinaryOperatorEnum::PLUS, "+" },
Expand Down
11 changes: 10 additions & 1 deletion src/pddl/parser/conditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "conditions.hpp"

#include "error_handling.hpp"
#include "functions.hpp"
#include "literal.hpp"
#include "loki/details/ast/printer.hpp"
#include "loki/details/pddl/conditions.hpp"
Expand Down Expand Up @@ -141,7 +142,15 @@ Condition parse(const ast::GoalDescriptorFunctionComparison& node, Context& cont
{
throw UndefinedRequirementError(RequirementEnum::NUMERIC_FLUENTS, context.scopes.top().get_error_handler()(node, ""));
}
throw NotImplementedError("parse(const ast::GoalDescriptorFunctionComparison& node, Context& context)");
context.references.untrack(RequirementEnum::NUMERIC_FLUENTS);

auto binary_comparator = boost::apply_visitor(BinaryComparatorVisitor(), node.binary_comparator);
auto function_expression_left = parse(node.function_expression_left, context);
auto function_expression_right = parse(node.function_expression_right, context);
auto condition = context.factories.get_or_create_condition(
context.factories.get_or_create_condition_function_expression_comparison(binary_comparator, function_expression_left, function_expression_right));
context.positions.push_back(condition, node);
return condition;
}

Condition parse(const ast::ConstraintGoalDescriptor& node, Context& context) { return boost::apply_visitor(ConditionVisitor(context), node); }
Expand Down
11 changes: 11 additions & 0 deletions src/pddl/parser/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
namespace loki
{

/* BinaryComparatorEnum */
BinaryComparatorEnum BinaryComparatorVisitor::operator()(const ast::BinaryComparatorGreater&) const { return BinaryComparatorEnum::GREATER; }

BinaryComparatorEnum BinaryComparatorVisitor::operator()(const ast::BinaryComparatorLess&) const { return BinaryComparatorEnum::LESS; }

BinaryComparatorEnum BinaryComparatorVisitor::operator()(const ast::BinaryComparatorEqual&) const { return BinaryComparatorEnum::EQUAL; }

BinaryComparatorEnum BinaryComparatorVisitor::operator()(const ast::BinaryComparatorGreaterEqual&) const { return BinaryComparatorEnum::GREATER_EQUAL; }

BinaryComparatorEnum BinaryComparatorVisitor::operator()(const ast::BinaryComparatorLessEqual&) const { return BinaryComparatorEnum::LESS_EQUAL; }

/* MultiOperator */
MultiOperatorEnum MultiOperatorVisitor::operator()(const ast::MultiOperatorMul&) const { return MultiOperatorEnum::MUL; }

Expand Down
10 changes: 10 additions & 0 deletions src/pddl/parser/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@

namespace loki
{
/* BinaryComparator */
struct BinaryComparatorVisitor : boost::static_visitor<BinaryComparatorEnum>
{
BinaryComparatorEnum operator()(const ast::BinaryComparatorGreater& node) const;
BinaryComparatorEnum operator()(const ast::BinaryComparatorLess& node) const;
BinaryComparatorEnum operator()(const ast::BinaryComparatorEqual& node) const;
BinaryComparatorEnum operator()(const ast::BinaryComparatorGreaterEqual& node) const;
BinaryComparatorEnum operator()(const ast::BinaryComparatorLessEqual& node) const;
};

/* MultiOperator */
struct MultiOperatorVisitor : boost::static_visitor<MultiOperatorEnum>
{
Expand Down
14 changes: 14 additions & 0 deletions src/pddl/repositories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ PDDLTypeToRepository create_default_pddl_type_to_repository()
boost::hana::make_pair(boost::hana::type_c<ConditionImplyImpl>, ConditionImplyRepository {}),
boost::hana::make_pair(boost::hana::type_c<ConditionExistsImpl>, ConditionExistsRepository {}),
boost::hana::make_pair(boost::hana::type_c<ConditionForallImpl>, ConditionForallRepository {}),
boost::hana::make_pair(boost::hana::type_c<ConditionFunctionExpressionComparisonImpl>, ConditionFunctionExpressionComparisonRepository {}),
boost::hana::make_pair(boost::hana::type_c<ConditionImpl>, ConditionRepository {}),
boost::hana::make_pair(boost::hana::type_c<EffectLiteralImpl>, EffectLiteralRepository {}),
boost::hana::make_pair(boost::hana::type_c<EffectAndImpl>, EffectAndRepository {}),
Expand Down Expand Up @@ -235,6 +236,14 @@ ConditionForall PDDLRepositories::get_or_create_condition_forall(ParameterList p
return boost::hana::at_key(m_repositories, boost::hana::type<ConditionForallImpl> {}).get_or_create(std::move(parameters), std::move(condition));
}

ConditionFunctionExpressionComparison PDDLRepositories::get_or_create_condition_function_expression_comparison(BinaryComparatorEnum binary_comparator,
FunctionExpression function_expression_left,
FunctionExpression function_expression_right)
{
return boost::hana::at_key(m_repositories, boost::hana::type<ConditionFunctionExpressionComparisonImpl> {})
.get_or_create(std::move(binary_comparator), std::move(function_expression_left), std::move(function_expression_right));
}

Condition PDDLRepositories::get_or_create_condition(ConditionLiteral condition)
{
return boost::hana::at_key(m_repositories, boost::hana::type<ConditionImpl> {}).get_or_create(condition);
Expand Down Expand Up @@ -270,6 +279,11 @@ Condition PDDLRepositories::get_or_create_condition(ConditionForall condition)
return boost::hana::at_key(m_repositories, boost::hana::type<ConditionImpl> {}).get_or_create(condition);
}

Condition PDDLRepositories::get_or_create_condition(ConditionFunctionExpressionComparison condition)
{
return boost::hana::at_key(m_repositories, boost::hana::type<ConditionImpl> {}).get_or_create(condition);
}

EffectLiteral PDDLRepositories::get_or_create_effect_literal(Literal literal)
{
return boost::hana::at_key(m_repositories, boost::hana::type<EffectLiteralImpl> {}).get_or_create(std::move(literal));
Expand Down

0 comments on commit 5a18eb3

Please sign in to comment.