Skip to content

Commit

Permalink
lifted some parsing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed May 28, 2024
1 parent bcd5cc5 commit ef014c3
Show file tree
Hide file tree
Showing 31 changed files with 156 additions and 269 deletions.
11 changes: 2 additions & 9 deletions include/loki/details/ast/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ struct Requirements;
struct Types; // : typing
struct Constants;
struct Predicates;
struct DerivedPredicates;
struct Functions;
struct Constraints; // :constraints
struct Structure;
Expand Down Expand Up @@ -915,7 +914,7 @@ struct Action : x3::position_tagged
/* <derived-def> */
struct Axiom : x3::position_tagged
{
Literal literal;
AtomicFormulaSkeleton atomic_formula_skeleton;
GoalDescriptor goal_descriptor;
};

Expand All @@ -937,11 +936,6 @@ struct Predicates : x3::position_tagged
std::vector<AtomicFormulaSkeleton> atomic_formula_skeletons;
};

struct DerivedPredicates : x3::position_tagged
{
std::vector<AtomicFormulaSkeleton> atomic_formula_skeletons;
};

/* <functions-def> */
struct Functions : x3::position_tagged
{
Expand Down Expand Up @@ -974,7 +968,6 @@ struct Domain : x3::position_tagged
boost::optional<Types> types;
boost::optional<Constants> constants;
boost::optional<Predicates> predicates;
boost::optional<DerivedPredicates> derived_predicates;
boost::optional<Functions> functions;
boost::optional<Constraints> constraints;
std::vector<Structure> structures;
Expand Down Expand Up @@ -1214,7 +1207,7 @@ struct Problem : x3::position_tagged
ProblemDomainName domain_name;
boost::optional<Requirements> requirements;
boost::optional<Objects> objects;
boost::optional<DerivedPredicates> derived_predicates;
boost::optional<Predicates> derived_predicates;
boost::optional<Initial> initial;
boost::optional<Goal> goal;
boost::optional<ProblemConstraints> constraints;
Expand Down
7 changes: 3 additions & 4 deletions include/loki/details/pddl/axiom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ namespace loki
class AxiomImpl : public Base<AxiomImpl>
{
private:
// We make parameters explicit although the textual representation has them implicit.
std::string m_derived_predicate_name;
ParameterList m_parameters;
Literal m_literal;
Condition m_condition;

AxiomImpl(size_t identifier, ParameterList parameters, Literal literal, Condition condition);
AxiomImpl(size_t identifier, std::string derived_predicate_name, ParameterList parameters, Condition condition);

// Give access to the constructor.
friend class PDDLFactory<AxiomImpl, Hash<AxiomImpl*>, EqualTo<AxiomImpl*>>;
Expand All @@ -48,8 +47,8 @@ class AxiomImpl : public Base<AxiomImpl>
friend class Base<AxiomImpl>;

public:
const std::string& get_derived_predicate_name() const;
const ParameterList& get_parameters() const;
const Literal& get_literal() const;
const Condition& get_condition() const;
};

Expand Down
1 change: 0 additions & 1 deletion include/loki/details/pddl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ struct Context
ReferencedPDDLObjects references;
// For convenience, to avoid an additional parameter during semantic parsing
Requirements requirements;
std::unordered_set<Predicate> derived_predicates;

Context(PDDLFactories& factories_, PDDLPositionCache& positions_, ScopeStack& scopes_, bool strict_ = false, bool quiet_ = true) :
factories(factories_),
Expand Down
3 changes: 0 additions & 3 deletions include/loki/details/pddl/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class DomainImpl : public Base<DomainImpl>
TypeList m_types;
ObjectList m_constants;
PredicateList m_predicates;
PredicateList m_derived_predicates;
FunctionSkeletonList m_functions;
ActionList m_actions;
AxiomList m_axioms;
Expand All @@ -47,7 +46,6 @@ class DomainImpl : public Base<DomainImpl>
TypeList types,
ObjectList constants,
PredicateList predicates,
PredicateList derived_predicates,
FunctionSkeletonList functions,
ActionList actions,
AxiomList axioms);
Expand All @@ -69,7 +67,6 @@ class DomainImpl : public Base<DomainImpl>
const TypeList& get_types() const;
const ObjectList& get_constants() const;
const PredicateList& get_predicates() const;
const PredicateList& get_derived_predicates() const;
const FunctionSkeletonList& get_functions() const;
const ActionList& get_actions() const;
const AxiomList& get_axioms() const;
Expand Down
52 changes: 17 additions & 35 deletions include/loki/details/pddl/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,37 +143,6 @@ class UnsupportedRequirementError : public std::runtime_error
explicit UnsupportedRequirementError(RequirementEnum requirement, const std::string& error_handler_output);
};

/* Compatibility errors */
class MismatchedPredicateTermListError : public SemanticParserError
{
public:
MismatchedPredicateTermListError(const Predicate& predicate, const TermList& term_list, const std::string& error_handler_output);
};

class MismatchedFunctionSkeletonTermListError : public SemanticParserError
{
public:
MismatchedFunctionSkeletonTermListError(const FunctionSkeleton& function_skeleton, const TermList& term_list, const std::string& error_handler_output);
};

class IncompatibleObjectToVariableError : public SemanticParserError
{
public:
IncompatibleObjectToVariableError(const Object& object, const Variable& variable, const std::string& error_handler_output);
};

class UnexpectedDerivedPredicateInEffect : public SemanticParserError
{
public:
UnexpectedDerivedPredicateInEffect(const std::string& name, const std::string& error_handler_output);
};

class ExpectedDerivedPredicate : public SemanticParserError
{
public:
ExpectedDerivedPredicate(const std::string& name, const std::string& error_handler_output);
};

/* Object */
class UnusedObjectError : public SemanticParserError
{
Expand Down Expand Up @@ -210,16 +179,29 @@ class IllformedFunctionDefinitionMultipleValues : public SemanticParserError
};

/* Compatibility errors */
class MismatchedDomainError : public SemanticParserError

class IncompatibleArityError : public SemanticParserError
{
public:
MismatchedDomainError(const Domain& domain, const std::string& domain_name, const std::string& error_handler_output);
IncompatibleArityError(const size_t arity_1, const size_t arity_2, const std::string& error_handler_output);
};

class MismatchedPredicateObjectListError : public SemanticParserError
class IncompatibleVariableGroundingError : public SemanticParserError
{
public:
MismatchedPredicateObjectListError(const Predicate& predicate, const ObjectList& object_list, const std::string& error_handler_output);
IncompatibleVariableGroundingError(const Object& object, const Variable& variable, const std::string& error_handler_output);
};

class IncompatibleParameterTypesError : public SemanticParserError
{
public:
IncompatibleParameterTypesError(const Parameter& specialized_parameter, const Parameter& generalized_parameter, const std::string& error_handler_output);
};

class MismatchedDomainError : public SemanticParserError
{
public:
MismatchedDomainError(const Domain& domain, const std::string& domain_name, const std::string& error_handler_output);
};

class NegativeCostError : public SemanticParserError
Expand Down
6 changes: 2 additions & 4 deletions include/loki/details/pddl/factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ class PDDLFactories
return actions.get_or_create<ActionImpl>(std::move(name), std::move(original_arity), std::move(parameters), std::move(condition), std::move(effect));
}

Axiom get_or_create_axiom(ParameterList parameters, Literal literal, Condition condition)
Axiom get_or_create_axiom(std::string derived_predicate_name, ParameterList parameters, Condition condition)
{
return axioms.get_or_create<AxiomImpl>(std::move(parameters), std::move(literal), std::move(condition));
return axioms.get_or_create<AxiomImpl>(std::move(derived_predicate_name), std::move(parameters), std::move(condition));
}

OptimizationMetric get_or_create_optimization_metric(OptimizationMetricEnum metric, FunctionExpression function_expression)
Expand All @@ -284,7 +284,6 @@ class PDDLFactories
TypeList types,
ObjectList constants,
PredicateList predicates,
PredicateList derived_predicates,
FunctionSkeletonList functions,
ActionList actions,
AxiomList axioms)
Expand All @@ -294,7 +293,6 @@ class PDDLFactories
std::move(types),
std::move(constants),
std::move(predicates),
std::move(derived_predicates),
std::move(functions),
std::move(actions),
std::move(axioms));
Expand Down
4 changes: 4 additions & 0 deletions include/loki/details/pddl/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ParameterImpl : public Base<ParameterImpl>
const TypeList& get_bases() const;
};

/// @brief Return true iff specialized_parameter is a specialized version of generalized_parameter
/// by checking whether the types are compatible
extern bool is_specialized_parameter(const Parameter& specialized_parameter, const Parameter& generalized_parameter);

}

#endif
3 changes: 0 additions & 3 deletions include/loki/details/pddl/scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Scope
Bindings<FunctionSkeleton> m_function_skeletons;
Bindings<Variable> m_variables;
Bindings<Predicate> m_predicates;
Bindings<Predicate> m_derived_predicates;

public:
Scope(const PDDLErrorHandler& error_handler, const Scope* parent_scope = nullptr);
Expand All @@ -82,15 +81,13 @@ class Scope
std::optional<BindingSearchResult<FunctionSkeleton>> get_function_skeleton(const std::string& name) const;
std::optional<BindingSearchResult<Variable>> get_variable(const std::string& name) const;
std::optional<BindingSearchResult<Predicate>> get_predicate(const std::string& name) const;
std::optional<BindingSearchResult<Predicate>> get_derived_predicate(const std::string& name) const;

/// @brief Insert a binding.
void insert_type(const std::string& name, const Type& type, const std::optional<Position>& position);
void insert_object(const std::string& name, const Object& object, const std::optional<Position>& position);
void insert_function_skeleton(const std::string& name, const FunctionSkeleton& function_skeleton, const std::optional<Position>& position);
void insert_variable(const std::string& name, const Variable& variable, const std::optional<Position>& position);
void insert_predicate(const std::string& name, const Predicate& predicate, const std::optional<Position>& position);
void insert_derived_predicate(const std::string& name, const Predicate& derived_predicate, const std::optional<Position>& position);

/// @brief Get the error handler to print an error message.
const PDDLErrorHandler& get_error_handler() const;
Expand Down
3 changes: 3 additions & 0 deletions include/loki/details/pddl/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class TypeImpl : public Base<TypeImpl>
/// @brief Collects all types from a hierarchy.
extern TypeSet collect_types_from_hierarchy(const TypeList& types);

/// @brief Return true iff type is a subtype of or equal to one of the types in parent_types
extern bool is_subtype_or_equal(const Type& type, const TypeList& parent_types);

}

#endif
5 changes: 2 additions & 3 deletions src/ast/ast_adapted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,16 @@ BOOST_FUSION_ADAPT_STRUCT(loki::ast::ActionSymbol, name)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::ActionBody, precondition_goal_descriptor, effect)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Action, action_symbol, typed_list_of_variables, action_body)

BOOST_FUSION_ADAPT_STRUCT(loki::ast::Axiom, literal, goal_descriptor)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Axiom, atomic_formula_skeleton, goal_descriptor)

BOOST_FUSION_ADAPT_STRUCT(loki::ast::DomainName, name)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Requirements, requirements)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Types, typed_list_of_names)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Constants, typed_list_of_names)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Predicates, atomic_formula_skeletons)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::DerivedPredicates, atomic_formula_skeletons)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Functions, function_types_list_of_atomic_function_skeletons)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Constraints, constraint_goal_descriptor)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Domain, domain_name, requirements, types, constants, predicates, derived_predicates, functions, constraints, structures)
BOOST_FUSION_ADAPT_STRUCT(loki::ast::Domain, domain_name, requirements, types, constants, predicates, functions, constraints, structures)

/**
* Problem
Expand Down
12 changes: 1 addition & 11 deletions src/ast/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ typedef x3::rule<RequirementsClass, ast::Requirements> requirements_type;
typedef x3::rule<TypesClass, ast::Types> types_type;
typedef x3::rule<ConstantsClass, ast::Constants> constants_type;
typedef x3::rule<PredicatesClass, ast::Predicates> predicates_type;
typedef x3::rule<DerivedPredicatesClass, ast::DerivedPredicates> derived_predicates_type;
typedef x3::rule<FunctionsClass, ast::Functions> functions_type;
typedef x3::rule<ConstraintsClass, ast::Constraints> constraints_type;
typedef x3::rule<StructureClass, ast::Structure> structure_type;
Expand Down Expand Up @@ -570,15 +569,7 @@ BOOST_SPIRIT_DECLARE(effect_type,
action_type,
axiom_type)

BOOST_SPIRIT_DECLARE(domain_name_type,
requirements_type,
types_type,
constants_type,
predicates_type,
derived_predicates_type,
functions_type,
constraints_type,
structure_type)
BOOST_SPIRIT_DECLARE(domain_name_type, requirements_type, types_type, constants_type, predicates_type, functions_type, constraints_type, structure_type)

BOOST_SPIRIT_DECLARE(basic_function_term_type)

Expand Down Expand Up @@ -777,7 +768,6 @@ parser::requirements_type const& requirements();
parser::types_type const& types();
parser::constants_type const& constants();
parser::predicates_type const& predicates();
parser::derived_predicates_type const& derived_predicates();
parser::functions_type const& functions();
parser::constraints_type const& constraints();
parser::structure_type const& structure();
Expand Down
27 changes: 5 additions & 22 deletions src/ast/parser_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ requirements_type const requirements = "requirements";
types_type const types = "types";
constants_type const constants = "constants";
predicates_type const predicates = "predicates";
derived_predicates_type const derived_predicates = "derived_predicates";
functions_type const functions = "functions";
constraints_type const constraints = "constraints";
structure_type const structure = "structure";
Expand Down Expand Up @@ -415,7 +414,7 @@ const auto action_body_def = -(keyword_lit(":precondition") > ((lit('(') >> lit(
const auto action_def = (lit('(') >> keyword_lit(":action")) > action_symbol > keyword_lit(":parameters") > lit('(') > typed_list_of_variables > lit(')')
> action_body >> lit(')');

const auto axiom_def = (lit('(') >> keyword_lit(":derived")) > literal > goal_descriptor > lit(')');
const auto axiom_def = (lit('(') >> keyword_lit(":derived")) > atomic_formula_skeleton > goal_descriptor > lit(')');

const auto define_keyword_def = keyword_lit("define");
const auto domain_keyword_def = keyword_lit("domain");
Expand All @@ -424,13 +423,12 @@ const auto requirements_def = lit('(') >> keyword_lit(":requirements") > *requir
const auto types_def = (lit('(') >> keyword_lit(":types") > typed_list_of_names) > lit(')');
const auto constants_def = (lit('(') >> keyword_lit(":constants") > typed_list_of_names) > lit(')');
const auto predicates_def = (lit('(') >> keyword_lit(":predicates") > *atomic_formula_skeleton) > lit(')');
const auto derived_predicates_def = (lit('(') >> keyword_lit(":derived-predicates") > *atomic_formula_skeleton) > lit(')');
const auto functions_def = (lit('(') >> keyword_lit(":functions") > *function_typed_list_of_atomic_function_skeletons) > lit(')');
const auto constraints_def = (lit('(') >> keyword_lit(":constraints")) > constraint_goal_descriptor > lit(')');
const auto structure_def = action | axiom;

const auto domain_def = lit('(') > define_keyword > domain_name > -requirements > -types > -constants > -predicates > -derived_predicates > -functions
> -constraints > *structure > lit(')');
const auto domain_def = lit('(') > define_keyword > domain_name > -requirements > -types > -constants > -predicates > -functions > -constraints > *structure
> lit(')');

/**
* Problem
Expand Down Expand Up @@ -489,7 +487,7 @@ const auto goal_def = (lit('(') >> keyword_lit(":goal")) > precondition_goal_des
const auto problem_constraints_def = (lit('(') >> keyword_lit(":constraints")) > preference_constraint_goal_descriptor > lit(')');
const auto metric_specification_def = (lit('(') >> keyword_lit(":metric")) > (metric_specification_total_cost | metric_specification_general) > lit(')');

const auto problem_def = lit('(') > define_keyword > problem_name > problem_domain_name > -requirements > -objects > -derived_predicates > -initial > -goal
const auto problem_def = lit('(') > define_keyword > problem_name > problem_domain_name > -requirements > -objects > -predicates > -initial > -goal
> -problem_constraints > -metric_specification > -(*axiom) > lit(')');

/**
Expand Down Expand Up @@ -609,18 +607,7 @@ BOOST_SPIRIT_DEFINE(effect,
action,
axiom)

BOOST_SPIRIT_DEFINE(define_keyword,
domain_keyword,
domain_name,
requirements,
types,
constants,
predicates,
derived_predicates,
functions,
constraints,
structure,
domain)
BOOST_SPIRIT_DEFINE(define_keyword, domain_keyword, domain_name, requirements, types, constants, predicates, functions, constraints, structure, domain)

/**
* Problem
Expand Down Expand Up @@ -733,9 +720,6 @@ struct RequirementAdlClass : x3::annotate_on_success
struct RequirementDurativeActionsClass : x3::annotate_on_success
{
};
struct RequirementDerivedPredicatesClass : x3::annotate_on_success
{
};
struct RequirementTimedInitialLiteralsClass : x3::annotate_on_success
{
};
Expand Down Expand Up @@ -1372,7 +1356,6 @@ parser::requirements_type const& requirements() { return parser::requirements; }
parser::types_type const& types() { return parser::types; }
parser::constants_type const& constants() { return parser::constants; }
parser::predicates_type const& predicates() { return parser::predicates; }
parser::derived_predicates_type const& derived_predicates() { return parser::derived_predicates; }
parser::functions_type const& functions() { return parser::functions; }
parser::constraints_type const& constraints() { return parser::constraints; }
parser::structure_type const& structure() { return parser::structure; }
Expand Down
1 change: 0 additions & 1 deletion src/ast/parser_instantiations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ BOOST_SPIRIT_INSTANTIATE(requirements_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(types_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(constants_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(predicates_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(derived_predicates_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(functions_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(constraints_type, iterator_type, context_type)
BOOST_SPIRIT_INSTANTIATE(structure_type, iterator_type, context_type)
Expand Down
2 changes: 1 addition & 1 deletion src/ast/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ std::string parse_text(const ast::Action& node, const FormattingOptions& options
std::string parse_text(const ast::Axiom& node, const FormattingOptions& options)
{
std::stringstream ss;
ss << std::string(options.indent, ' ') << "(:derived " << parse_text(node.literal, options) << "\n";
ss << std::string(options.indent, ' ') << "(:derived " << parse_text(node.atomic_formula_skeleton, options) << "\n";
FormattingOptions nested_options { options.indent + options.add_indent, options.add_indent };
ss << std::string(nested_options.indent, ' ') << parse_text(node.goal_descriptor, options) << ")\n";
return ss.str();
Expand Down
Loading

0 comments on commit ef014c3

Please sign in to comment.