Skip to content

Commit

Permalink
rename policy builder -> policy factory to be consistent with element…
Browse files Browse the repository at this point in the history
… factory, remove old policy parser
  • Loading branch information
drexlerd committed Oct 16, 2023
1 parent ec6be2c commit 4039ddc
Show file tree
Hide file tree
Showing 37 changed files with 935 additions and 1,177 deletions.
30 changes: 15 additions & 15 deletions api/python/src/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,26 @@ void init_policy(py::module_ &m_policy) {
.def("get_numericals", &Policy::get_numericals, py::return_value_policy::reference)
;

py::class_<PolicyBuilder, std::shared_ptr<PolicyBuilder>>(m_policy, "PolicyBuilder")
py::class_<PolicyFactory, std::shared_ptr<PolicyFactory>>(m_policy, "PolicyFactory")
.def(py::init<>())
.def("add_pos_condition", &PolicyBuilder::add_pos_condition)
.def("add_neg_condition", &PolicyBuilder::add_neg_condition)
.def("add_gt_condition", &PolicyBuilder::add_gt_condition)
.def("add_eq_condition", &PolicyBuilder::add_eq_condition)
.def("add_pos_effect", &PolicyBuilder::add_pos_effect)
.def("add_neg_effect", &PolicyBuilder::add_neg_effect)
.def("add_inc_effect", &PolicyBuilder::add_inc_effect)
.def("add_dec_effect", &PolicyBuilder::add_dec_effect)
.def("add_bot_effect", py::overload_cast<const std::shared_ptr<const Boolean>&>(&PolicyBuilder::add_bot_effect))
.def("add_bot_effect", py::overload_cast<const std::shared_ptr<const Numerical>&>(&PolicyBuilder::add_bot_effect))
.def("add_rule", &PolicyBuilder::add_rule)
.def("add_policy", &PolicyBuilder::add_policy)
.def("add_pos_condition", &PolicyFactory::add_pos_condition)
.def("add_neg_condition", &PolicyFactory::add_neg_condition)
.def("add_gt_condition", &PolicyFactory::add_gt_condition)
.def("add_eq_condition", &PolicyFactory::add_eq_condition)
.def("add_pos_effect", &PolicyFactory::add_pos_effect)
.def("add_neg_effect", &PolicyFactory::add_neg_effect)
.def("add_inc_effect", &PolicyFactory::add_inc_effect)
.def("add_dec_effect", &PolicyFactory::add_dec_effect)
.def("add_bot_effect", py::overload_cast<const std::shared_ptr<const Boolean>&>(&PolicyFactory::add_bot_effect))
.def("add_bot_effect", py::overload_cast<const std::shared_ptr<const Numerical>&>(&PolicyFactory::add_bot_effect))
.def("add_rule", &PolicyFactory::add_rule)
.def("add_policy", &PolicyFactory::add_policy)
;

py::class_<PolicyMinimizer>(m_policy, "PolicyMinimizer")
.def(py::init<>())
.def("minimize", py::overload_cast<const std::shared_ptr<const Policy>&, PolicyBuilder&>(&PolicyMinimizer::minimize, py::const_))
.def("minimize", py::overload_cast<const std::shared_ptr<const Policy>&, const StatePairs&, const StatePairs&, PolicyBuilder&>(&PolicyMinimizer::minimize, py::const_))
.def("minimize", py::overload_cast<const std::shared_ptr<const Policy>&, PolicyFactory&>(&PolicyMinimizer::minimize, py::const_))
.def("minimize", py::overload_cast<const std::shared_ptr<const Policy>&, const StatePairs&, const StatePairs&, PolicyFactory&>(&PolicyMinimizer::minimize, py::const_))
;

py::class_<PolicyReader>(m_policy, "PolicyReader")
Expand Down
24 changes: 12 additions & 12 deletions examples/policy/policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ int main() {
auto vocabulary = construct_vocabulary_info();
auto instance = construct_instance_info(vocabulary);

dlplan::core::SyntacticElementFactory factory(vocabulary);
auto element_factory = std::make_shared<dlplan::core::SyntacticElementFactory>(vocabulary);
// boolean_1 represents whether the hand is empty or not
auto boolean_1 = factory.parse_boolean("b_nullary(arm-empty)");
auto boolean_1 = element_factory->parse_boolean("b_nullary(arm-empty)");
// numerical_1 representes the number of blocks on top of another block
auto numerical_1 = factory.parse_numerical("n_count(r_primitive(on,0,1))");
PolicyBuilder builder;
auto b_pos_condition_0 = builder.add_pos_condition(boolean_1);
auto b_bot_effect_0 = builder.add_bot_effect(boolean_1);
auto n_gt_condition_0 = builder.add_gt_condition(numerical_1);
auto n_dec_effect_0 = builder.add_dec_effect(numerical_1);
auto numerical_1 = element_factory->parse_numerical("n_count(r_primitive(on,0,1))");
PolicyFactory policy_factory(element_factory);
auto b_pos_condition_0 = policy_factory.make_pos_condition(boolean_1);
auto b_bot_effect_0 = policy_factory.make_bot_effect(boolean_1);
auto n_gt_condition_0 = policy_factory.make_gt_condition(numerical_1);
auto n_dec_effect_0 = policy_factory.make_dec_effect(numerical_1);
// rule_1 represents that picking up a block that is on top of another block is good
auto rule_1 = builder.add_rule(
auto rule_1 = policy_factory.make_rule(
{b_pos_condition_0, n_gt_condition_0},
{b_bot_effect_0, n_dec_effect_0}
);
auto policy = builder.add_policy({rule_1});
auto policy = policy_factory.make_policy({rule_1});

const auto& atoms = instance->get_atoms();
const Atom& atom_0 = atoms[0];
Expand All @@ -126,8 +126,8 @@ int main() {
std::cout << policy->compute_repr() << std::endl << std::endl;
std::cout << policy->str() << std::endl << std::endl;

write_to_file("policy.txt", PolicyWriter().write(*policy));
auto policy_in = PolicyReader().read(read_from_file("policy.txt"), builder, factory);
write_to_file("policy.txt", policy->str());
auto policy_in = policy_factory.parse_policy(read_from_file("policy.txt"));

std::cout << "Read policy:" << std::endl;
std::cout << policy_in->compute_repr() << std::endl << std::endl;
Expand Down
112 changes: 46 additions & 66 deletions include/dlplan/policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@
#include <string>
#include <vector>

#include "policy/parsers/common/config.hpp"
#include "core.h"
#include "utils/pimpl.h"


// Forward declarations of this header
namespace dlplan::policy {
class PolicyBuilderImpl;
class PolicyReaderImpl;
class PolicyWriterImpl;
class PolicyFactoryImpl;
class BaseCondition;
class BaseEffect;
class Rule;
class Policy;
class PolicyBuilder;
class PolicyFactory;
}


Expand Down Expand Up @@ -51,7 +50,7 @@ namespace boost::serialization {
void serialize(Archive& ar, dlplan::policy::Policy& t, const unsigned int version);

template <typename Archive>
void serialize(Archive& ar, dlplan::policy::PolicyBuilder& builder, const unsigned int version);
void serialize(Archive& ar, dlplan::policy::PolicyFactory& t, const unsigned int version);
}


Expand Down Expand Up @@ -177,9 +176,9 @@ class Rule {
/// @brief Constructor for serialization.
Rule();

Rule(Conditions&& conditions, Effects&& effects, RuleIndex index);
Rule(const Conditions& conditions, const Effects& effects, RuleIndex index);

friend class PolicyBuilderImpl;
friend class PolicyFactoryImpl;
friend class boost::serialization::access;
template<typename Archive>
friend void boost::serialization::serialize(Archive& ar, Rule& t, const unsigned int version);
Expand Down Expand Up @@ -224,9 +223,9 @@ class Policy {
/// @brief Constructor for serialization.
Policy();

Policy(Rules&& rules, PolicyIndex index);
Policy(const Rules& rules, PolicyIndex index);

friend class PolicyBuilderImpl;
friend class PolicyFactoryImpl;
friend class boost::serialization::access;
template<typename Archive>
friend void boost::serialization::serialize(Archive& ar, Policy& t, const unsigned int version);
Expand Down Expand Up @@ -269,46 +268,61 @@ class Policy {

/// @brief Provides functionality for the syntactically unique creation of
/// conditions, effects, rules, and policies.
class PolicyBuilder {
class PolicyFactory {
private:
dlplan::utils::pimpl<PolicyBuilderImpl> m_pImpl;
dlplan::utils::pimpl<PolicyFactoryImpl> m_pImpl;

/// @brief Constructor for serialization.
PolicyFactory();

friend class boost::serialization::access;
template<typename Archive>
friend void boost::serialization::serialize(Archive& ar, PolicyBuilder& t, const unsigned int version);
friend void boost::serialization::serialize(Archive& ar, PolicyFactory& t, const unsigned int version);

public:
PolicyBuilder();
PolicyBuilder(const PolicyBuilder& other);
PolicyBuilder& operator=(const PolicyBuilder& other);
PolicyBuilder(PolicyBuilder&& other);
PolicyBuilder& operator=(PolicyBuilder&& other);
~PolicyBuilder();
explicit PolicyFactory(std::shared_ptr<core::SyntacticElementFactory> element_factory);
PolicyFactory(const PolicyFactory& other);
PolicyFactory& operator=(const PolicyFactory& other);
PolicyFactory(PolicyFactory&& other);
PolicyFactory& operator=(PolicyFactory&& other);
~PolicyFactory();

/**
* Parses a policy from its textual description
*/
std::shared_ptr<const Policy> parse_policy(
const std::string& description,
const std::string& filename="");

std::shared_ptr<const Policy> parse_policy(
parsers::iterator_type& iter, parsers::iterator_type end,
const std::string& filename="");

/**
* Uniquely adds a condition (resp. effect) and returns it.
*/
std::shared_ptr<const BaseCondition> add_pos_condition(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseCondition> add_neg_condition(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseCondition> add_gt_condition(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseCondition> add_eq_condition(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> add_pos_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> add_neg_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> add_bot_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> add_inc_effect(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> add_dec_effect(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> add_bot_effect(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseCondition> make_pos_condition(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseCondition> make_neg_condition(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseCondition> make_gt_condition(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseCondition> make_eq_condition(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> make_pos_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> make_neg_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> make_bot_effect(const std::shared_ptr<const core::Boolean>& boolean);
std::shared_ptr<const BaseEffect> make_inc_effect(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> make_dec_effect(const std::shared_ptr<const core::Numerical>& numerical);
std::shared_ptr<const BaseEffect> make_bot_effect(const std::shared_ptr<const core::Numerical>& numerical);

/**
* Uniquely adds a rule and returns it.
*/
std::shared_ptr<const Rule> add_rule(
std::shared_ptr<const Rule> make_rule(
Conditions&& conditions,
Effects&& effects);

/**
* Uniquely adds a policy and returns it.
*/
std::shared_ptr<const Policy> add_policy(
std::shared_ptr<const Policy> make_policy(
Rules&& rules);
};

Expand All @@ -326,49 +340,15 @@ class PolicyMinimizer {

std::shared_ptr<const Policy> minimize(
const std::shared_ptr<const Policy>& policy,
PolicyBuilder& builder) const;
PolicyFactory& policy_factory) const;
std::shared_ptr<const Policy> minimize(
const std::shared_ptr<const Policy>& policy,
const StatePairs& true_state_pairs,
const StatePairs& false_state_pairs,
PolicyBuilder& builder) const;
PolicyFactory& policy_factory) const;
};


/// @brief Provides functionality for reading the output of the policy writer
/// into a policy.
class PolicyReader {
private:
dlplan::utils::pimpl<PolicyReaderImpl> m_pImpl;

public:
PolicyReader();
PolicyReader(const PolicyReader& other);
PolicyReader& operator=(const PolicyReader& other);
PolicyReader(PolicyReader&& other);
PolicyReader& operator=(PolicyReader&& other);
~PolicyReader();

std::shared_ptr<const Policy> read(const std::string& data, PolicyBuilder& builder, core::SyntacticElementFactory& factory) const;
};


/// @brief Provides functionality for creating string representation that
/// the reader can translate back into a policy.
class PolicyWriter {
dlplan::utils::pimpl<PolicyWriterImpl> m_pImpl;

public:
PolicyWriter();
PolicyWriter(const PolicyWriter& other);
PolicyWriter& operator=(const PolicyWriter& other);
PolicyWriter(PolicyWriter&& other);
PolicyWriter& operator=(PolicyWriter&& other);
~PolicyWriter();

std::string write(const Policy& policy) const;
};

}

#endif
39 changes: 39 additions & 0 deletions include/dlplan/policy/parsers/common/config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef DLPLAN_INCLUDE_DLPLAN_POLICY_PARSERS_COMMON_CONFIG_HPP_
#define DLPLAN_INCLUDE_DLPLAN_POLICY_PARSERS_COMMON_CONFIG_HPP_

#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
#include <boost/spirit/home/x3/support/utility/error_reporting.hpp>


namespace dlplan::policy::parsers
{
namespace x3 = boost::spirit::x3;

// Our Iterator Type
typedef std::string::const_iterator iterator_type;

// The Phrase Parse Context
typedef
x3::phrase_parse_context<x3::ascii::space_type>::type
phrase_context_type;

// X3 Error Handler Utility
template <typename Iterator>
using error_handler = x3::error_handler<Iterator>;

// tag used to get our error handler from the context
using error_handler_tag = x3::error_handler_tag;

// Our Error Handler
typedef error_handler<iterator_type> error_handler_type;

// Combined Error Handler and Phrase Parse Context
typedef x3::context<
error_handler_tag
, std::reference_wrapper<error_handler_type>
, phrase_context_type>
context_type;
}

#endif
Loading

0 comments on commit 4039ddc

Please sign in to comment.