diff --git a/api/python/src/dlplan/policy/__init__.pyi b/api/python/src/dlplan/policy/__init__.pyi index ecf406af..ac731213 100644 --- a/api/python/src/dlplan/policy/__init__.pyi +++ b/api/python/src/dlplan/policy/__init__.pyi @@ -5,59 +5,48 @@ from ..core import State, DenotationsCaches, Boolean, Numerical, Concept, Role, class NamedBoolean(): - def __repr__(self) -> str: ... def __str__(self) -> str: ... def get_key(self) -> str: ... - def get_boolean(self) -> Boolean: ... + def get_element(self) -> Boolean: ... class NamedNumerical(): - def __repr__(self) -> str: ... def __str__(self) -> str: ... def get_key(self) -> str: ... - def get_numerical(self) -> Numerical: ... + def get_element(self) -> Numerical: ... class NamedConcept(): - def __repr__(self) -> str: ... def __str__(self) -> str: ... def get_key(self) -> str: ... - def get_concept(self) -> Concept: ... + def get_element(self) -> Concept: ... class NamedRole(): - def __repr__(self) -> str: ... def __str__(self) -> str: ... def get_key(self) -> str: ... - def get_role(self) -> Role: ... + def get_element(self) -> Role: ... class BaseCondition: - def __repr__(self) -> str: ... def __str__(self) -> str: ... @overload def evaluate(self, state: State) -> bool: ... @overload def evaluate(self, state: State, caches: DenotationsCaches) -> bool: ... def get_index(self) -> int: ... - def get_boolean(self) -> Union[None, Boolean]: ... - def get_numerical(self) -> Union[None, Numerical]: ... class BaseEffect: - def __repr__(self) -> str: ... def __str__(self) -> str: ... @overload def evaluate(self, source_state: State, target_state: State) -> bool: ... @overload def evaluate(self, source_state: State, target_state: State, caches: DenotationsCaches) -> bool: ... def get_index(self) -> int: ... - def get_boolean(self) -> Union[None, Boolean]: ... - def get_numerical(self) -> Union[None, Numerical]: ... class Rule: - def __repr__(self) -> str: ... def __str__(self) -> str: ... @overload def evaluate_conditions(self, state: State) -> bool: ... @@ -73,7 +62,6 @@ class Rule: class Policy: - def __repr__(self) -> str: ... def __str__(self) -> str: ... @overload def evaluate(self, source_state: State, target_state: State) -> Union[None, Rule]: ... diff --git a/api/python/src/policy.cpp b/api/python/src/policy.cpp index 05179bd9..f420062c 100644 --- a/api/python/src/policy.cpp +++ b/api/python/src/policy.cpp @@ -16,56 +16,45 @@ using namespace dlplan; void init_policy(py::module_ &m_policy) { py::class_>(m_policy, "NamedBoolean") - .def("__repr__", &policy::NamedBoolean::compute_repr) - .def("__str__", &policy::NamedBoolean::str) + .def("__str__", py::overload_cast<>(&policy::NamedBoolean::str, py::const_)) .def("get_key", &policy::NamedBoolean::get_key) - .def("get_boolean", &policy::NamedBoolean::get_boolean) + .def("get_element", &policy::NamedBoolean::get_element) ; py::class_>(m_policy, "NamedNumerical") - .def("__repr__", &policy::NamedNumerical::compute_repr) - .def("__str__", &policy::NamedNumerical::str) + .def("__str__", py::overload_cast<>(&policy::NamedNumerical::str, py::const_)) .def("get_key", &policy::NamedNumerical::get_key) - .def("get_numerical", &policy::NamedNumerical::get_numerical) + .def("get_element", &policy::NamedNumerical::get_element) ; py::class_>(m_policy, "NamedConcept") - .def("__repr__", &policy::NamedConcept::compute_repr) - .def("__str__", &policy::NamedConcept::str) + .def("__str__", py::overload_cast<>(&policy::NamedConcept::str, py::const_)) .def("get_key", &policy::NamedConcept::get_key) - .def("get_concept", &policy::NamedConcept::get_concept) + .def("get_element", &policy::NamedConcept::get_element) ; py::class_>(m_policy, "NamedRole") - .def("__repr__", &policy::NamedRole::compute_repr) - .def("__str__", &policy::NamedRole::str) + .def("__str__", py::overload_cast<>(&policy::NamedRole::str, py::const_)) .def("get_key", &policy::NamedRole::get_key) - .def("get_role", &policy::NamedRole::get_role) + .def("get_element", &policy::NamedRole::get_element) ; py::class_>(m_policy, "BaseCondition") - .def("__repr__", &policy::BaseCondition::compute_repr) - .def("__str__", &policy::BaseCondition::str) + .def("__str__", py::overload_cast<>(&policy::BaseCondition::str, py::const_)) .def("evaluate", py::overload_cast(&policy::BaseCondition::evaluate, py::const_)) .def("evaluate", py::overload_cast(&policy::BaseCondition::evaluate, py::const_)) .def("get_index", &policy::BaseCondition::get_index) - .def("get_boolean", &policy::BaseCondition::get_boolean) - .def("get_numerical", &policy::BaseCondition::get_numerical) ; py::class_>(m_policy, "BaseEffect") - .def("__repr__", &policy::BaseEffect::compute_repr) - .def("__str__", &policy::BaseEffect::str) + .def("__str__", py::overload_cast<>(&policy::BaseEffect::str, py::const_)) .def("evaluate", py::overload_cast(&policy::BaseEffect::evaluate, py::const_)) .def("evaluate", py::overload_cast(&policy::BaseEffect::evaluate, py::const_)) .def("get_index", &policy::BaseEffect::get_index) - .def("get_boolean", &policy::BaseEffect::get_boolean) - .def("get_numerical", &policy::BaseEffect::get_numerical) ; py::class_>(m_policy, "Rule") - .def("__repr__", &policy::Rule::compute_repr) - .def("__str__", &policy::Rule::str) + .def("__str__", py::overload_cast<>(&policy::Rule::str, py::const_)) .def("evaluate_conditions", py::overload_cast(&policy::Rule::evaluate_conditions, py::const_)) .def("evaluate_conditions", py::overload_cast(&policy::Rule::evaluate_conditions, py::const_)) .def("evaluate_effects", py::overload_cast(&policy::Rule::evaluate_effects, py::const_)) @@ -76,8 +65,7 @@ void init_policy(py::module_ &m_policy) { ; py::class_>(m_policy, "Policy") - .def("__repr__", &policy::Policy::compute_repr) - .def("__str__", &policy::Policy::str) + .def("__str__", py::overload_cast<>(&policy::Policy::str, py::const_)) .def("evaluate", py::overload_cast(&policy::Policy::evaluate, py::const_)) .def("evaluate", py::overload_cast(&policy::Policy::evaluate, py::const_)) .def("evaluate_conditions", py::overload_cast(&policy::Policy::evaluate_conditions, py::const_)) diff --git a/examples/policy/policy.cpp b/examples/policy/policy.cpp index 26605c4a..f9ac20ca 100644 --- a/examples/policy/policy.cpp +++ b/examples/policy/policy.cpp @@ -99,7 +99,6 @@ int main() { assert(!policy->evaluate(state_2, state_0, caches)); assert(!policy->evaluate(state_2, state_1, caches)); - std::cout << policy->compute_repr() << std::endl << std::endl; std::cout << policy->str() << std::endl << std::endl; std::cout << "Parsing policy:" << std::endl; @@ -110,7 +109,6 @@ int main() { "(:rule (:conditions (:c_b_pos b0) (:c_n_gt n0)) (:effects (:e_b_bot b0) (:e_n_dec n0)))\n" ")"; auto policy_in = policy_factory.parse_policy(policy_str); - std::cout << policy_in->compute_repr() << std::endl << std::endl; std::cout << policy_in->str() << std::endl << std::endl; return 0; diff --git a/include/dlplan/common/base.h b/include/dlplan/common/base.h index 0d2d8e07..930cfaf6 100644 --- a/include/dlplan/common/base.h +++ b/include/dlplan/common/base.h @@ -43,7 +43,7 @@ class Base { /// @brief Compute a string representation of this object. void str(std::stringstream& out) const { - return static_cast(this)->str_impl(out); + static_cast(this)->str_impl(out); } /// @brief Compute a string representation of this object. diff --git a/include/dlplan/policy.h b/include/dlplan/policy.h index e690f483..f0f9ef18 100644 --- a/include/dlplan/policy.h +++ b/include/dlplan/policy.h @@ -9,6 +9,7 @@ #include #include +#include "common/base.h" #include "common/parsers/config.hpp" #include "core.h" #include "utils/pimpl.h" @@ -16,16 +17,14 @@ // Forward declarations of this header namespace dlplan::policy { -class NamedBoolean; -class NamedNumerical; -class NamedConcept; -class NamedRole; class PolicyFactoryImpl; class BaseCondition; class BaseEffect; class Rule; class Policy; class PolicyFactory; +class BaseConditionVisitor; +class BaseEffectVisitor; } @@ -44,18 +43,13 @@ struct ScoreCompare { const std::shared_ptr& l, const std::shared_ptr& r) const { if (l->compute_evaluate_time_score() == r->compute_evaluate_time_score()) { - return l->compute_repr() < r->compute_repr(); + return l->get_index() < r->get_index(); } return l->compute_evaluate_time_score() < r->compute_evaluate_time_score(); } }; -using Booleans = std::set, ScoreCompare>; -using Numericals = std::set, ScoreCompare>; -using Concepts = std::set, ScoreCompare>; -using Roles = std::set, ScoreCompare>; -using Conditions = std::set, ScoreCompare>; -using Effects = std::set, ScoreCompare>; + using Rules = std::set, ScoreCompare>; using Policies = std::set, ScoreCompare>; @@ -65,215 +59,103 @@ using StatePairs = std::vector; /// @brief Wrappers around core elements to add an additional key /// that can potentially add some more human readable meaning. -class NamedBoolean { -private: - int m_identifier; - std::string m_key; - std::shared_ptr m_boolean; - - NamedBoolean(int identifier, const std::string& key, std::shared_ptr boolean); - - template - friend class dlplan::ReferenceCountedObjectFactory; - -public: - NamedBoolean(const NamedBoolean& other); - NamedBoolean& operator=(const NamedBoolean& other); - NamedBoolean(NamedBoolean&& other); - NamedBoolean& operator=(NamedBoolean&& other); - ~NamedBoolean(); - - bool operator==(const NamedBoolean& other) const; - bool operator<(const NamedBoolean& other) const; - - size_t hash() const; - - int compute_evaluate_time_score() const; - std::string compute_repr() const; - std::string str() const; - - const std::string& get_key() const; - std::shared_ptr get_boolean() const; -}; - - -class NamedNumerical { +template +class NamedElement : public Base> { private: - int m_identifier; std::string m_key; - std::shared_ptr m_numerical; + std::shared_ptr m_element; - NamedNumerical(int identifier, const std::string& key, std::shared_ptr numerical); + NamedElement(int identifier, const std::string& key, std::shared_ptr element) + : Base>(identifier), m_key(key), m_element(element) { } template friend class dlplan::ReferenceCountedObjectFactory; public: - NamedNumerical(const NamedNumerical& other); - NamedNumerical& operator=(const NamedNumerical& other); - NamedNumerical(NamedNumerical&& other); - NamedNumerical& operator=(NamedNumerical&& other); - ~NamedNumerical(); - - bool operator==(const NamedNumerical& other) const; - bool operator<(const NamedNumerical& other) const; - - size_t hash() const; - - int compute_evaluate_time_score() const; - std::string compute_repr() const; - std::string str() const; - - const std::string& get_key() const; - std::shared_ptr get_numerical() const; -}; - - -class NamedConcept { -private: - int m_identifier; - std::string m_key; - std::shared_ptr m_concept; - - NamedConcept(int identifier, const std::string& key, std::shared_ptr concept); - - template - friend class dlplan::ReferenceCountedObjectFactory; - -public: - NamedConcept(const NamedConcept& other); - NamedConcept& operator=(const NamedConcept& other); - NamedConcept(NamedConcept&& other); - NamedConcept& operator=(NamedConcept&& other); - ~NamedConcept(); - - bool operator==(const NamedConcept& other) const; - bool operator<(const NamedConcept& other) const; - - size_t hash() const; + NamedElement(const NamedElement& other) = default; + NamedElement& operator=(const NamedElement& other) = default; + NamedElement(NamedElement&& other) = default; + NamedElement& operator=(NamedElement&& other) = default; + ~NamedElement() = default; + + bool are_equal_impl(const NamedElement& other) const { + if (this != &other) { + return (m_key == other.m_key) + && (m_element == other.m_element); + } + return true; + } + size_t hash_impl() const { return hash_combine(m_key, m_element); } + void str_impl(std::stringstream& out) const { out << "(" << m_key << " \"" << m_element->str() << "\")"; } - int compute_evaluate_time_score() const; - std::string compute_repr() const; - std::string str() const; + int compute_evaluate_time_score() const { return m_element->compute_evaluate_time_score(); } - const std::string& get_key() const; - std::shared_ptr get_concept() const; + const std::string& get_key() const { return m_key; } + const std::shared_ptr& get_element() const { return m_element; } }; -class NamedRole { -private: - int m_identifier; - std::string m_key; - std::shared_ptr m_role; - - NamedRole(int identifier, const std::string& key, std::shared_ptr role); - - template - friend class dlplan::ReferenceCountedObjectFactory; - -public: - NamedRole(const NamedRole& other); - NamedRole& operator=(const NamedRole& other); - NamedRole(NamedRole&& other); - NamedRole& operator=(NamedRole&& other); - ~NamedRole(); - - bool operator==(const NamedRole& other) const; - bool operator<(const NamedRole& other) const; - - size_t hash() const; - - int compute_evaluate_time_score() const; - std::string compute_repr() const; - std::string str() const; - - const std::string& get_key() const; - std::shared_ptr get_role() const; -}; +using NamedBoolean = NamedElement; +using NamedNumerical = NamedElement; +using NamedConcept = NamedElement; +using NamedRole = NamedElement; +using Booleans = std::set, ScoreCompare>; +using Numericals = std::set, ScoreCompare>; +using Concepts = std::set, ScoreCompare>; +using Roles = std::set, ScoreCompare>; /// @brief Represents the abstract base class of a feature condition and /// provides functionality to access its underlying data and for /// the evaluation on a state. -class BaseCondition { +class BaseCondition : public Base { protected: - int m_identifier; - explicit BaseCondition(int identifier); - template - friend class dlplan::ReferenceCountedObjectFactory; - public: virtual ~BaseCondition(); - virtual bool operator==(const BaseCondition& other) const = 0; - bool operator<(const BaseCondition& other) const; - - virtual size_t hash() const = 0; + virtual bool are_equal_impl(const BaseCondition& other) const = 0; + virtual size_t hash_impl() const = 0; + virtual void str_impl(std::stringstream& out) const = 0; + virtual int compute_evaluate_time_score() const = 0; virtual bool evaluate(const core::State& source_state) const = 0; virtual bool evaluate(const core::State& source_state, core::DenotationsCaches& caches) const = 0; - - virtual std::string compute_repr() const = 0; - virtual std::string str() const = 0; - - /// @brief Computes a time score for evaluating this condition relative to other conditions. - /// The scoring assumes evaluation that uses caching. - /// @return An integer that represents the score. - virtual int compute_evaluate_time_score() const = 0; - - int get_index() const; - virtual std::shared_ptr get_boolean() const = 0; - virtual std::shared_ptr get_numerical() const = 0; + virtual void accept(BaseConditionVisitor& visitor) const = 0; }; /// @brief Represents the abstract base class of a feature effect and /// provides functionality to access its underlying data and for /// the evaluation on a pair of states. -class BaseEffect { +class BaseEffect : public Base { protected: - int m_identifier; - explicit BaseEffect(int identifier); - template - friend class dlplan::ReferenceCountedObjectFactory; - public: virtual ~BaseEffect(); - virtual bool operator==(const BaseEffect& other) const = 0; - bool operator<(const BaseEffect& other) const; - - virtual size_t hash() const = 0; + virtual bool are_equal_impl(const BaseEffect& other) const = 0; + virtual size_t hash_impl() const = 0; + virtual void str_impl(std::stringstream& out) const = 0; + virtual int compute_evaluate_time_score() const = 0; virtual bool evaluate(const core::State& source_state, const core::State& target_state) const = 0; virtual bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const = 0; - - virtual std::string compute_repr() const = 0; - virtual std::string str() const = 0; - - /// @brief Computes a time score for evaluating this effect relative to other effects. - /// The scoring assumes evaluation that uses caching. - /// @return An integer that represents the score. - virtual int compute_evaluate_time_score() const = 0; - - int get_index() const; - virtual std::shared_ptr get_boolean() const = 0; - virtual std::shared_ptr get_numerical() const = 0; + virtual void accept(BaseEffectVisitor& visitor) const = 0; }; +using Conditions = std::set, ScoreCompare>; +using Effects = std::set, ScoreCompare>; + /// @brief Implements a policy rule of the form C->E with where C is a set of /// feature conditions and E is a set of feature effects. Provides /// functionality to access its underlying data and for the evaluation /// on a pair of states. -class Rule { +class Rule : public Base { private: - int m_identifier; Conditions m_conditions; Effects m_effects; @@ -289,25 +171,16 @@ class Rule { Rule& operator=(Rule&& other); ~Rule(); - bool operator==(const Rule& other) const; - bool operator<(const Rule& other) const; + bool are_equal_impl(const Rule& other) const; + size_t hash_impl() const; + void str_impl(std::stringstream& out) const; + int compute_evaluate_time_score() const; bool evaluate_conditions(const core::State& source_state) const; bool evaluate_conditions(const core::State& source_state, core::DenotationsCaches& caches) const; bool evaluate_effects(const core::State& source_state, const core::State& target_state) const; bool evaluate_effects(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const; - std::string compute_repr() const; - std::string str() const; - - size_t hash() const; - - /// @brief Computes a time score for evaluating this rule relative to other rules. - /// The scoring assumes evaluation that uses caching. - /// @return An integer that represents the score. - int compute_evaluate_time_score() const; - - int get_index() const; const Conditions& get_conditions() const; const Effects& get_effects() const; }; @@ -317,9 +190,8 @@ class Rule { /// Boolean and a set of numerical features. Provides functionality /// to access its underlying data and for the evaluation on a pair of /// states. -class Policy { +class Policy : public Base { private: - int m_identifier; Booleans m_booleans; Numericals m_numericals; Rules m_rules; @@ -336,10 +208,11 @@ class Policy { Policy& operator=(Policy&& other); ~Policy(); - bool operator==(const Policy& other) const; - bool operator<(const Policy& other) const; + bool are_equal_impl(const Policy& other) const; + size_t hash_impl() const; + void str_impl(std::stringstream& out) const; + int compute_evaluate_time_score() const; - size_t hash() const; /** * Approach 1: naive approach to evaluate (s,s') @@ -355,15 +228,6 @@ class Policy { std::shared_ptr evaluate_effects(const core::State& source_state, const core::State& target_state, const std::vector>& rules) const; std::shared_ptr evaluate_effects(const core::State& source_state, const core::State& target_state, const std::vector>& rules, core::DenotationsCaches& caches) const; - std::string compute_repr() const; - std::string str() const; - - /// @brief Computes a time score for evaluating this condition relative to other conditions. - /// The scoring assumes evaluation that uses caching. - /// @return An integer that represents the score. - int compute_evaluate_time_score() const; - - int get_index() const; const Booleans& get_booleans() const; const Numericals& get_numericals() const; const Rules& get_rules() const; diff --git a/include/dlplan/policy/condition.h b/include/dlplan/policy/condition.h index d44e8e43..ceca03d8 100644 --- a/include/dlplan/policy/condition.h +++ b/include/dlplan/policy/condition.h @@ -17,34 +17,26 @@ class ReferenceCountedObjectFactory; namespace dlplan::policy { -class BooleanCondition : public BaseCondition { +template +class NamedElementCondition : public BaseCondition { protected: - const std::shared_ptr m_boolean; + std::shared_ptr m_named_element; - BooleanCondition(int identifier, std::shared_ptr boolean); + NamedElementCondition(int identifier, const std::shared_ptr& named_element) + : BaseCondition(identifier), m_named_element(named_element) { } - int compute_evaluate_time_score() const override; - - std::shared_ptr get_boolean() const override; - - std::shared_ptr get_numerical() const override; -}; - - -class NumericalCondition : public BaseCondition { -protected: - const std::shared_ptr m_numerical; - - NumericalCondition(int identifier, std::shared_ptr numerical); +public: + virtual ~NamedElementCondition() = default; - int compute_evaluate_time_score() const override; + int compute_evaluate_time_score() const override { + return m_named_element->compute_evaluate_time_score(); + } - std::shared_ptr get_boolean() const override; - std::shared_ptr get_numerical() const override; + std::shared_ptr get_named_element() const { return m_named_element; } }; -class PositiveBooleanCondition : public BooleanCondition { +class PositiveBooleanCondition : public NamedElementCondition, public std::enable_shared_from_this { private: PositiveBooleanCondition(int identifier, std::shared_ptr boolean); @@ -52,18 +44,17 @@ class PositiveBooleanCondition : public BooleanCondition { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseCondition& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseCondition& other) const override; + void str_impl(std::stringstream& out) const override; + size_t hash_impl() const override; bool evaluate(const core::State& source_state) const override; bool evaluate(const core::State& source_state, core::DenotationsCaches& caches) const override; - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseConditionVisitor& visitor) const override; }; -class NegativeBooleanCondition : public BooleanCondition { +class NegativeBooleanCondition : public NamedElementCondition, public std::enable_shared_from_this { private: NegativeBooleanCondition(int identifier, std::shared_ptr boolean); @@ -71,18 +62,17 @@ class NegativeBooleanCondition : public BooleanCondition { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseCondition& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseCondition& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state) const override; bool evaluate(const core::State& source_state, core::DenotationsCaches& caches) const override; - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseConditionVisitor& visitor) const override; }; -class EqualNumericalCondition : public NumericalCondition { +class EqualNumericalCondition : public NamedElementCondition, public std::enable_shared_from_this { private: EqualNumericalCondition(int identifier, std::shared_ptr numerical); @@ -90,18 +80,17 @@ class EqualNumericalCondition : public NumericalCondition { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseCondition& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseCondition& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state) const override; bool evaluate(const core::State& source_state, core::DenotationsCaches& caches) const override; - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseConditionVisitor& visitor) const override; }; -class GreaterNumericalCondition : public NumericalCondition { +class GreaterNumericalCondition : public NamedElementCondition, public std::enable_shared_from_this { private: GreaterNumericalCondition(int identifier, std::shared_ptr numerical); @@ -109,15 +98,23 @@ class GreaterNumericalCondition : public NumericalCondition { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseCondition& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseCondition& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state) const override; bool evaluate(const core::State& source_state, core::DenotationsCaches& caches) const override; - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseConditionVisitor& visitor) const override; +}; + +/// @brief Defines an interface for visiting conditions. +class BaseConditionVisitor { +public: + virtual void visit(const std::shared_ptr& condition) = 0; + virtual void visit(const std::shared_ptr& condition) = 0; + virtual void visit(const std::shared_ptr& condition) = 0; + virtual void visit(const std::shared_ptr& condition) = 0; }; } diff --git a/include/dlplan/policy/effect.h b/include/dlplan/policy/effect.h index 1772a75d..e8e4be7c 100644 --- a/include/dlplan/policy/effect.h +++ b/include/dlplan/policy/effect.h @@ -15,33 +15,29 @@ class ReferenceCountedObjectFactory; namespace dlplan::policy { -class BooleanEffect : public BaseEffect { +template +class NamedElementEffect : public BaseEffect { protected: - std::shared_ptr m_boolean; + std::shared_ptr m_named_element; - BooleanEffect(int identifier, std::shared_ptr boolean); + NamedElementEffect(int identifier, const std::shared_ptr& named_element) + : BaseEffect(identifier), m_named_element(named_element) { } - int compute_evaluate_time_score() const override; - - std::shared_ptr get_boolean() const override; - std::shared_ptr get_numerical() const override; -}; - - -class NumericalEffect : public BaseEffect { -protected: - std::shared_ptr m_numerical; + template + friend class dlplan::ReferenceCountedObjectFactory; - NumericalEffect(int identifier, std::shared_ptr numerical); +public: + virtual ~NamedElementEffect() = default; - int compute_evaluate_time_score() const override; + int compute_evaluate_time_score() const override { + return m_named_element->compute_evaluate_time_score(); + } - std::shared_ptr get_boolean() const override; - std::shared_ptr get_numerical() const override; + std::shared_ptr get_named_element() const { return m_named_element; } }; -class PositiveBooleanEffect : public BooleanEffect { +class PositiveBooleanEffect : public NamedElementEffect, public std::enable_shared_from_this { private: PositiveBooleanEffect(int identifier, std::shared_ptr boolean); @@ -49,18 +45,16 @@ class PositiveBooleanEffect : public BooleanEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; - - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseEffectVisitor& visitor) const override; }; -class NegativeBooleanEffect : public BooleanEffect { +class NegativeBooleanEffect : public NamedElementEffect, public std::enable_shared_from_this { private: NegativeBooleanEffect(int identifier, std::shared_ptr boolean); @@ -68,18 +62,16 @@ class NegativeBooleanEffect : public BooleanEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; - - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseEffectVisitor& visitor) const override; }; -class UnchangedBooleanEffect : public BooleanEffect { +class UnchangedBooleanEffect : public NamedElementEffect, public std::enable_shared_from_this { private: UnchangedBooleanEffect(int identifier, std::shared_ptr boolean); @@ -87,18 +79,16 @@ class UnchangedBooleanEffect : public BooleanEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; - - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseEffectVisitor& visitor) const override; }; -class IncrementNumericalEffect : public NumericalEffect { +class IncrementNumericalEffect : public NamedElementEffect, public std::enable_shared_from_this { private: IncrementNumericalEffect(int identifier, std::shared_ptr numerical); @@ -106,18 +96,16 @@ class IncrementNumericalEffect : public NumericalEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; - - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseEffectVisitor& visitor) const override; }; -class DecrementNumericalEffect : public NumericalEffect { +class DecrementNumericalEffect : public NamedElementEffect, public std::enable_shared_from_this { private: DecrementNumericalEffect(int identifier, std::shared_ptr numerical); @@ -125,18 +113,16 @@ class DecrementNumericalEffect : public NumericalEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; - - std::string compute_repr() const override; - std::string str() const override; + void accept(BaseEffectVisitor& visitor) const override; }; -class UnchangedNumericalEffect : public NumericalEffect { +class UnchangedNumericalEffect : public NamedElementEffect, public std::enable_shared_from_this { private: UnchangedNumericalEffect(int identifier, std::shared_ptr numerical); @@ -144,15 +130,24 @@ class UnchangedNumericalEffect : public NumericalEffect { friend class dlplan::ReferenceCountedObjectFactory; public: - bool operator==(const BaseEffect& other) const override; - - size_t hash() const override; + bool are_equal_impl(const BaseEffect& other) const override; + size_t hash_impl() const override; + void str_impl(std::stringstream& out) const override; bool evaluate(const core::State& source_state, const core::State& target_state) const override; bool evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const override; + void accept(BaseEffectVisitor& visitor) const override; +}; - std::string compute_repr() const override; - std::string str() const override; +/// @brief Defines an interface for visiting effects. +class BaseEffectVisitor { +public: + virtual void visit(const std::shared_ptr& effect) = 0; + virtual void visit(const std::shared_ptr& effect) = 0; + virtual void visit(const std::shared_ptr& effect) = 0; + virtual void visit(const std::shared_ptr& effect) = 0; + virtual void visit(const std::shared_ptr& effect) = 0; + virtual void visit(const std::shared_ptr& effect) = 0; }; } diff --git a/src/policy/CMakeLists.txt b/src/policy/CMakeLists.txt index 4c61df08..b74e88f9 100644 --- a/src/policy/CMakeLists.txt +++ b/src/policy/CMakeLists.txt @@ -1,16 +1,12 @@ add_library(dlplanpolicy STATIC) target_sources(dlplanpolicy PRIVATE - boolean.cpp - concept.cpp condition.cpp effect.cpp - numerical.cpp policy_factory.cpp policy_impl.cpp policy_minimizer.cpp policy.cpp - role.cpp rule.cpp parsers/syntactic/parser_instantiations.cpp parsers/semantic/context.cpp diff --git a/src/policy/boolean.cpp b/src/policy/boolean.cpp deleted file mode 100644 index 9751896f..00000000 --- a/src/policy/boolean.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../../include/dlplan/policy.h" - -#include "../../include/dlplan/core.h" -#include "../../include/dlplan/utils/hash.h" - -#include - - -namespace dlplan::policy { -NamedBoolean::NamedBoolean(int identifier, const std::string& key, std::shared_ptr boolean) - : m_identifier(identifier), m_key(key), m_boolean(boolean) { } - -NamedBoolean::NamedBoolean(const NamedBoolean& other) = default; - -NamedBoolean& NamedBoolean::operator=(const NamedBoolean& other) = default; - -NamedBoolean::NamedBoolean(NamedBoolean&& other) = default; - -NamedBoolean& NamedBoolean::operator=(NamedBoolean&& other) = default; - -NamedBoolean::~NamedBoolean() = default; - -bool NamedBoolean::operator==(const NamedBoolean& other) const { - if (this != &other) { - return m_key == other.m_key - && m_boolean == other.m_boolean; - } - return true; -} - -bool NamedBoolean::operator<(const NamedBoolean& other) const { - return m_identifier < other.m_identifier; -} - -size_t NamedBoolean::hash() const { - return hash_combine(m_key, m_boolean); -} - -int NamedBoolean::compute_evaluate_time_score() const { - return m_boolean->compute_evaluate_time_score(); -} - -std::string NamedBoolean::compute_repr() const { - return get_key(); -} - -std::string NamedBoolean::str() const { - return get_key(); -} - -const std::string& NamedBoolean::get_key() const { - return m_key; -} - -std::shared_ptr NamedBoolean::get_boolean() const { - return m_boolean; -} - -} diff --git a/src/policy/concept.cpp b/src/policy/concept.cpp deleted file mode 100644 index 4c01687b..00000000 --- a/src/policy/concept.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../../include/dlplan/policy.h" - -#include "../../include/dlplan/core.h" -#include "../../include/dlplan/utils/hash.h" - -#include - - -namespace dlplan::policy { -NamedConcept::NamedConcept(int identifier, const std::string& key, std::shared_ptr concept) - : m_identifier(identifier), m_key(key), m_concept(concept) { } - -NamedConcept::NamedConcept(const NamedConcept& other) = default; - -NamedConcept& NamedConcept::operator=(const NamedConcept& other) = default; - -NamedConcept::NamedConcept(NamedConcept&& other) = default; - -NamedConcept& NamedConcept::operator=(NamedConcept&& other) = default; - -NamedConcept::~NamedConcept() = default; - -bool NamedConcept::operator==(const NamedConcept& other) const { - if (this != &other) { - return m_key == other.m_key - && m_concept == other.m_concept; - } - return true; -} - -bool NamedConcept::operator<(const NamedConcept& other) const { - return m_identifier < other.m_identifier; -} - -size_t NamedConcept::hash() const { - return hash_combine(m_key, m_concept); -} - -int NamedConcept::compute_evaluate_time_score() const { - return m_concept->compute_evaluate_time_score(); -} - -std::string NamedConcept::compute_repr() const { - return get_key(); -} - -std::string NamedConcept::str() const { - return get_key(); -} - -const std::string& NamedConcept::get_key() const { - return m_key; -} - -std::shared_ptr NamedConcept::get_concept() const { - return m_concept; -} - -} diff --git a/src/policy/condition.cpp b/src/policy/condition.cpp index 5bf25c6b..80b357c4 100644 --- a/src/policy/condition.cpp +++ b/src/policy/condition.cpp @@ -9,175 +9,144 @@ using namespace dlplan; namespace dlplan::policy { -BooleanCondition::BooleanCondition(int identifier, std::shared_ptr boolean) - : BaseCondition(identifier), m_boolean(boolean) { } - -int BooleanCondition::compute_evaluate_time_score() const { - return m_boolean->compute_evaluate_time_score(); -} - -std::shared_ptr BooleanCondition::get_boolean() const { - return m_boolean; -} - -std::shared_ptr BooleanCondition::get_numerical() const { - return nullptr; -} - - -NumericalCondition::NumericalCondition(int identifier, std::shared_ptr numerical) - : BaseCondition(identifier), m_numerical(numerical) { } - -int NumericalCondition::compute_evaluate_time_score() const { - return m_numerical->compute_evaluate_time_score(); -} - -std::shared_ptr NumericalCondition::get_boolean() const { - return nullptr; -} - -std::shared_ptr NumericalCondition::get_numerical() const { - return m_numerical; -} - PositiveBooleanCondition::PositiveBooleanCondition(int identifier, std::shared_ptr boolean) - : BooleanCondition(identifier, boolean) { } + : NamedElementCondition(identifier, boolean) { } -bool PositiveBooleanCondition::operator==(const BaseCondition& other) const { +bool PositiveBooleanCondition::are_equal_impl(const BaseCondition& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_boolean == other_derived.m_boolean; + return m_named_element == other_derived.m_named_element; } return false; } -size_t PositiveBooleanCondition::hash() const { - return hash_combine(m_boolean); +size_t PositiveBooleanCondition::hash_impl() const { + return hash_combine(m_named_element); } bool PositiveBooleanCondition::evaluate(const core::State& source_state) const { - return m_boolean->get_boolean()->evaluate(source_state); + return m_named_element->get_element()->evaluate(source_state); } bool PositiveBooleanCondition::evaluate(const core::State& source_state, core::DenotationsCaches& caches) const { - return m_boolean->get_boolean()->evaluate(source_state, caches); + return m_named_element->get_element()->evaluate(source_state, caches); } -std::string PositiveBooleanCondition::compute_repr() const { - return "(:c_b_pos \"" + m_boolean->get_boolean()->str() + "\")"; +void PositiveBooleanCondition::str_impl(std::stringstream& out) const { + out << "(:c_b_pos " + m_named_element->get_key() + ")"; } -std::string PositiveBooleanCondition::str() const { - return "(:c_b_pos " + m_boolean->get_key() + ")"; +void PositiveBooleanCondition::accept(BaseConditionVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } NegativeBooleanCondition::NegativeBooleanCondition(int identifier, std::shared_ptr boolean) - : BooleanCondition(identifier, boolean) { } + : NamedElementCondition(identifier, boolean) { } -bool NegativeBooleanCondition::operator==(const BaseCondition& other) const { +bool NegativeBooleanCondition::are_equal_impl(const BaseCondition& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_boolean == other_derived.m_boolean; + return m_named_element == other_derived.m_named_element; } return false; } -size_t NegativeBooleanCondition::hash() const { - return hash_combine(m_boolean); +size_t NegativeBooleanCondition::hash_impl() const { + return hash_combine(m_named_element); } bool NegativeBooleanCondition::evaluate(const core::State& source_state) const { - return !m_boolean->get_boolean()->evaluate(source_state); + return !m_named_element->get_element()->evaluate(source_state); } bool NegativeBooleanCondition::evaluate(const core::State& source_state, core::DenotationsCaches& caches) const { - return !m_boolean->get_boolean()->evaluate(source_state, caches); + return !m_named_element->get_element()->evaluate(source_state, caches); } -std::string NegativeBooleanCondition::compute_repr() const { - return "(:c_b_neg \"" + m_boolean->get_boolean()->str() + "\")"; +void NegativeBooleanCondition::str_impl(std::stringstream& out) const { + out << "(:c_b_neg " + m_named_element->get_key() + ")"; } -std::string NegativeBooleanCondition::str() const { - return "(:c_b_neg " + m_boolean->get_key() + ")"; +void NegativeBooleanCondition::accept(BaseConditionVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } EqualNumericalCondition::EqualNumericalCondition(int identifier, std::shared_ptr numerical) - : NumericalCondition(identifier, numerical) { } + : NamedElementCondition(identifier, numerical) { } -bool EqualNumericalCondition::operator==(const BaseCondition& other) const { +bool EqualNumericalCondition::are_equal_impl(const BaseCondition& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_numerical == other_derived.m_numerical; + return m_named_element == other_derived.m_named_element; } return false; } -size_t EqualNumericalCondition::hash() const { - return hash_combine(m_numerical); +size_t EqualNumericalCondition::hash_impl() const { + return hash_combine(m_named_element); } bool EqualNumericalCondition::evaluate(const core::State& source_state) const { - int eval = m_numerical->get_numerical()->evaluate(source_state); + int eval = m_named_element->get_element()->evaluate(source_state); if (eval == INF) return false; return eval == 0; } bool EqualNumericalCondition::evaluate(const core::State& source_state, core::DenotationsCaches& caches) const { - int eval = m_numerical->get_numerical()->evaluate(source_state, caches); + int eval = m_named_element->get_element()->evaluate(source_state, caches); if (eval == INF) return false; return eval == 0; } -std::string EqualNumericalCondition::compute_repr() const { - return "(:c_n_eq \"" + m_numerical->get_numerical()->str() + "\")"; +void EqualNumericalCondition::str_impl(std::stringstream& out) const { + out << "(:c_n_eq " + m_named_element->get_key() + ")"; } -std::string EqualNumericalCondition::str() const { - return "(:c_n_eq " + m_numerical->get_key() + ")"; +void EqualNumericalCondition::accept(BaseConditionVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } GreaterNumericalCondition::GreaterNumericalCondition(int identifier, std::shared_ptr numerical) - : NumericalCondition(identifier, numerical) { } + : NamedElementCondition(identifier, numerical) { } -bool GreaterNumericalCondition::operator==(const BaseCondition& other) const { +bool GreaterNumericalCondition::are_equal_impl(const BaseCondition& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_numerical == other_derived.m_numerical; + return m_named_element == other_derived.m_named_element; } return false; } -size_t GreaterNumericalCondition::hash() const { - return hash_combine(m_numerical); +size_t GreaterNumericalCondition::hash_impl() const { + return hash_combine(m_named_element); } bool GreaterNumericalCondition::evaluate(const core::State& source_state) const { - int eval = m_numerical->get_numerical()->evaluate(source_state); + int eval = m_named_element->get_element()->evaluate(source_state); if (eval == INF) return false; return eval > 0; } bool GreaterNumericalCondition::evaluate(const core::State& source_state, core::DenotationsCaches& caches) const { - int eval = m_numerical->get_numerical()->evaluate(source_state, caches); + int eval = m_named_element->get_element()->evaluate(source_state, caches); if (eval == INF) return false; return eval > 0; } -std::string GreaterNumericalCondition::compute_repr() const { - return "(:c_n_gt \"" + m_numerical->get_numerical()->str() + "\")"; +void GreaterNumericalCondition::str_impl(std::stringstream& out) const { + out << "(:c_n_gt " + m_named_element->get_key() + ")"; } -std::string GreaterNumericalCondition::str() const { - return "(:c_n_gt " + m_numerical->get_key() + ")"; +void GreaterNumericalCondition::accept(BaseConditionVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } } diff --git a/src/policy/effect.cpp b/src/policy/effect.cpp index 48f1bd02..fe7b72e0 100644 --- a/src/policy/effect.cpp +++ b/src/policy/effect.cpp @@ -10,251 +10,220 @@ using namespace dlplan; namespace dlplan::policy { -BooleanEffect::BooleanEffect(int identifier, std::shared_ptr boolean) - : BaseEffect(identifier), m_boolean(boolean) { } - -int BooleanEffect::compute_evaluate_time_score() const { - return m_boolean->compute_evaluate_time_score(); -} - -std::shared_ptr BooleanEffect::get_boolean() const { - return m_boolean; -} - -std::shared_ptr BooleanEffect::get_numerical() const { - return nullptr; -} - - -NumericalEffect::NumericalEffect(int identifier, std::shared_ptr numerical) - : BaseEffect(identifier), m_numerical(numerical) { } - -int NumericalEffect::compute_evaluate_time_score() const { - return m_numerical->compute_evaluate_time_score(); -} - -std::shared_ptr NumericalEffect::get_boolean() const { - return nullptr; -} - -std::shared_ptr NumericalEffect::get_numerical() const { - return m_numerical; -} - PositiveBooleanEffect::PositiveBooleanEffect(int identifier, std::shared_ptr boolean) - : BooleanEffect(identifier, boolean) {} + : NamedElementEffect(identifier, boolean) {} -bool PositiveBooleanEffect::operator==(const BaseEffect& other) const { +bool PositiveBooleanEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_boolean == other_derived.m_boolean; + return m_named_element == other_derived.m_named_element; } return false; } -size_t PositiveBooleanEffect::hash() const { - return hash_combine(m_boolean); +size_t PositiveBooleanEffect::hash_impl() const { + return hash_combine(m_named_element); } bool PositiveBooleanEffect::evaluate(const core::State&, const core::State& target_state) const { - return m_boolean->get_boolean()->evaluate(target_state); + return m_named_element->get_element()->evaluate(target_state); } bool PositiveBooleanEffect::evaluate(const core::State&, const core::State& target_state, core::DenotationsCaches& caches) const { - return m_boolean->get_boolean()->evaluate(target_state, caches); + return m_named_element->get_element()->evaluate(target_state, caches); } -std::string PositiveBooleanEffect::compute_repr() const{ - return "(:e_b_pos \"" + m_boolean->get_boolean()->str() + "\")"; +void PositiveBooleanEffect::str_impl(std::stringstream& out) const { + out << "(:e_b_pos " + m_named_element->get_key() + ")"; } -std::string PositiveBooleanEffect::str() const { - return "(:e_b_pos " + m_boolean->get_key() + ")"; +void PositiveBooleanEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } NegativeBooleanEffect::NegativeBooleanEffect(int identifier, std::shared_ptr boolean) - : BooleanEffect(identifier, boolean) {} + : NamedElementEffect(identifier, boolean) {} -bool NegativeBooleanEffect::operator==(const BaseEffect& other) const { +bool NegativeBooleanEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_boolean == other_derived.m_boolean; + return m_named_element == other_derived.m_named_element; } return false; } -size_t NegativeBooleanEffect::hash() const { - return hash_combine(m_boolean); +size_t NegativeBooleanEffect::hash_impl() const { + return hash_combine(m_named_element); } bool NegativeBooleanEffect::evaluate(const core::State&, const core::State& target_state) const { - return !m_boolean->get_boolean()->evaluate(target_state); + return !m_named_element->get_element()->evaluate(target_state); } bool NegativeBooleanEffect::evaluate(const core::State&, const core::State& target_state, core::DenotationsCaches& caches) const { - return !m_boolean->get_boolean()->evaluate(target_state, caches); + return !m_named_element->get_element()->evaluate(target_state, caches); } -std::string NegativeBooleanEffect::compute_repr() const{ - return "(:e_b_neg \"" + m_boolean->get_boolean()->str() + "\")"; +void NegativeBooleanEffect::str_impl(std::stringstream& out) const { + out << "(:e_b_neg " + m_named_element->get_key() + ")"; } -std::string NegativeBooleanEffect::str() const { - return "(:e_b_neg " + m_boolean->get_key() + ")"; +void NegativeBooleanEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } UnchangedBooleanEffect::UnchangedBooleanEffect(int identifier, std::shared_ptr boolean) - : BooleanEffect(identifier, boolean) {} + : NamedElementEffect(identifier, boolean) {} -bool UnchangedBooleanEffect::operator==(const BaseEffect& other) const { +bool UnchangedBooleanEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_boolean == other_derived.m_boolean; + return m_named_element == other_derived.m_named_element; } return false; } -size_t UnchangedBooleanEffect::hash() const { - return hash_combine(m_boolean); +size_t UnchangedBooleanEffect::hash_impl() const { + return hash_combine(m_named_element); } bool UnchangedBooleanEffect::evaluate(const core::State& source_state, const core::State& target_state) const { - return m_boolean->get_boolean()->evaluate(source_state) == m_boolean->get_boolean()->evaluate(target_state); + return m_named_element->get_element()->evaluate(source_state) == m_named_element->get_element()->evaluate(target_state); } bool UnchangedBooleanEffect::evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const { - return m_boolean->get_boolean()->evaluate(source_state, caches) == m_boolean->get_boolean()->evaluate(target_state, caches); + return m_named_element->get_element()->evaluate(source_state, caches) == m_named_element->get_element()->evaluate(target_state, caches); } -std::string UnchangedBooleanEffect::compute_repr() const{ - return "(:e_b_bot \"" + m_boolean->get_boolean()->str() + "\")"; +void UnchangedBooleanEffect::str_impl(std::stringstream& out) const { + out << "(:e_b_bot " + m_named_element->get_key() + ")"; } -std::string UnchangedBooleanEffect::str() const { - return "(:e_b_bot " + m_boolean->get_key() + ")"; +void UnchangedBooleanEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } IncrementNumericalEffect::IncrementNumericalEffect(int identifier, std::shared_ptr numerical) - : NumericalEffect(identifier, numerical) {} + : NamedElementEffect(identifier, numerical) {} -bool IncrementNumericalEffect::operator==(const BaseEffect& other) const { +bool IncrementNumericalEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_numerical == other_derived.m_numerical; + return m_named_element == other_derived.m_named_element; } return false; } -size_t IncrementNumericalEffect::hash() const { - return hash_combine(m_numerical); +size_t IncrementNumericalEffect::hash_impl() const { + return hash_combine(m_named_element); } bool IncrementNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state) const { - int source_eval = m_numerical->get_numerical()->evaluate(source_state); - int target_eval = m_numerical->get_numerical()->evaluate(target_state); + int source_eval = m_named_element->get_element()->evaluate(source_state); + int target_eval = m_named_element->get_element()->evaluate(target_state); if (source_eval == INF) return false; if (target_eval == INF) return false; return source_eval < target_eval; } bool IncrementNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const { - int source_eval = m_numerical->get_numerical()->evaluate(source_state, caches); - int target_eval = m_numerical->get_numerical()->evaluate(target_state, caches); + int source_eval = m_named_element->get_element()->evaluate(source_state, caches); + int target_eval = m_named_element->get_element()->evaluate(target_state, caches); if (source_eval == INF) return false; if (target_eval == INF) return false; return source_eval < target_eval; } -std::string IncrementNumericalEffect::compute_repr() const{ - return "(:e_n_inc \"" + m_numerical->get_numerical()->str() + "\")"; +void IncrementNumericalEffect::str_impl(std::stringstream& out) const { + out << "(:e_n_inc " + m_named_element->get_key() + ")"; } -std::string IncrementNumericalEffect::str() const { - return "(:e_n_inc " + m_numerical->get_key() + ")"; +void IncrementNumericalEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } DecrementNumericalEffect::DecrementNumericalEffect(int identifier, std::shared_ptr numerical) - : NumericalEffect(identifier, numerical) {} + : NamedElementEffect(identifier, numerical) {} -bool DecrementNumericalEffect::operator==(const BaseEffect& other) const { +bool DecrementNumericalEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_numerical == other_derived.m_numerical; + return m_named_element == other_derived.m_named_element; } return false; } -size_t DecrementNumericalEffect::hash() const { - return hash_combine(m_numerical); +size_t DecrementNumericalEffect::hash_impl() const { + return hash_combine(m_named_element); } bool DecrementNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state) const { - int source_eval = m_numerical->get_numerical()->evaluate(source_state); - int target_eval = m_numerical->get_numerical()->evaluate(target_state); + int source_eval = m_named_element->get_element()->evaluate(source_state); + int target_eval = m_named_element->get_element()->evaluate(target_state); if (source_eval == INF) return false; if (target_eval == INF) return false; return source_eval > target_eval; } bool DecrementNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const { - int source_eval = m_numerical->get_numerical()->evaluate(source_state, caches); - int target_eval = m_numerical->get_numerical()->evaluate(target_state, caches); + int source_eval = m_named_element->get_element()->evaluate(source_state, caches); + int target_eval = m_named_element->get_element()->evaluate(target_state, caches); if (source_eval == INF) return false; if (target_eval == INF) return false; return source_eval > target_eval; } -std::string DecrementNumericalEffect::compute_repr() const{ - return "(:e_n_dec \"" + m_numerical->get_numerical()->str() + "\")"; +void DecrementNumericalEffect::str_impl(std::stringstream& out) const { + out << "(:e_n_dec " + m_named_element->get_key() + ")"; } -std::string DecrementNumericalEffect::str() const { - return "(:e_n_dec " + m_numerical->get_key() + ")"; +void DecrementNumericalEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } UnchangedNumericalEffect::UnchangedNumericalEffect(int identifier, std::shared_ptr numerical) - : NumericalEffect(identifier, numerical) {} + : NamedElementEffect(identifier, numerical) {} -bool UnchangedNumericalEffect::operator==(const BaseEffect& other) const { +bool UnchangedNumericalEffect::are_equal_impl(const BaseEffect& other) const { if (typeid(*this) == typeid(other)) { if (this == &other) return true; const auto& other_derived = static_cast(other); - return m_numerical == other_derived.m_numerical; + return m_named_element == other_derived.m_named_element; } return false; } -size_t UnchangedNumericalEffect::hash() const { - return hash_combine(m_numerical); +size_t UnchangedNumericalEffect::hash_impl() const { + return hash_combine(m_named_element); } bool UnchangedNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state) const { - return m_numerical->get_numerical()->evaluate(source_state) == m_numerical->get_numerical()->evaluate(target_state); + return m_named_element->get_element()->evaluate(source_state) == m_named_element->get_element()->evaluate(target_state); } bool UnchangedNumericalEffect::evaluate(const core::State& source_state, const core::State& target_state, core::DenotationsCaches& caches) const { - int source_eval = m_numerical->get_numerical()->evaluate(source_state, caches); - int target_eval = m_numerical->get_numerical()->evaluate(target_state, caches); + int source_eval = m_named_element->get_element()->evaluate(source_state, caches); + int target_eval = m_named_element->get_element()->evaluate(target_state, caches); return source_eval == target_eval; } -std::string UnchangedNumericalEffect::compute_repr() const{ - return "(:e_n_bot \"" + m_numerical->get_numerical()->str() + "\")"; +void UnchangedNumericalEffect::str_impl(std::stringstream& out) const { + out << "(:e_n_bot " + m_named_element->get_key() + ")"; } -std::string UnchangedNumericalEffect::str() const { - return "(:e_n_bot " + m_numerical->get_key() + ")"; +void UnchangedNumericalEffect::accept(BaseEffectVisitor& visitor) const { + visitor.visit(this->shared_from_this()); } } diff --git a/src/policy/numerical.cpp b/src/policy/numerical.cpp deleted file mode 100644 index 55512782..00000000 --- a/src/policy/numerical.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "../../include/dlplan/policy.h" - -#include "../../include/dlplan/core.h" -#include "../../include/dlplan/utils/hash.h" - -#include - - -namespace dlplan::policy { -NamedNumerical::NamedNumerical(int identifier, const std::string& key, std::shared_ptr numerical) - : m_identifier(identifier), m_key(key), m_numerical(numerical) { } - -NamedNumerical::NamedNumerical(const NamedNumerical& other) = default; - -NamedNumerical& NamedNumerical::operator=(const NamedNumerical& other) = default; - -NamedNumerical::NamedNumerical(NamedNumerical&& other) = default; - -NamedNumerical& NamedNumerical::operator=(NamedNumerical&& other) = default; - -NamedNumerical::~NamedNumerical() = default; - -bool NamedNumerical::operator==(const NamedNumerical& other) const { - if (this != &other) { - return m_key == other.m_key - && m_numerical == other.m_numerical; - } - return true; -} - -bool NamedNumerical::operator<(const NamedNumerical& other) const { - return m_identifier < other.m_identifier; -} - -size_t NamedNumerical::hash() const { - return hash_combine(m_key, m_numerical); -} - -int NamedNumerical::compute_evaluate_time_score() const { - return m_numerical->compute_evaluate_time_score(); -} - -std::string NamedNumerical::compute_repr() const { - return get_key(); -} - -std::string NamedNumerical::str() const { - return get_key(); -} - -const std::string& NamedNumerical::get_key() const { - return m_key; -} - -std::shared_ptr NamedNumerical::get_numerical() const { - return m_numerical; -} - -} diff --git a/src/policy/policy.cpp b/src/policy/policy.cpp index b8326889..4c67741e 100644 --- a/src/policy/policy.cpp +++ b/src/policy/policy.cpp @@ -14,33 +14,15 @@ using namespace dlplan; namespace dlplan::policy { -BaseCondition::BaseCondition(int identifier) : m_identifier(identifier) { } +BaseCondition::BaseCondition(int identifier) : Base(identifier) { } BaseCondition::~BaseCondition() = default; -bool BaseCondition::operator<(const BaseCondition& other) const { - return m_identifier < other.m_identifier; -} - - -int BaseCondition::get_index() const { - return m_identifier; -} - -BaseEffect::BaseEffect(int identifier) : m_identifier(identifier) { } +BaseEffect::BaseEffect(int identifier) : Base(identifier) { } BaseEffect::~BaseEffect() = default; -bool BaseEffect::operator<(const BaseEffect& other) const { - return m_identifier < other.m_identifier; -} - - -int BaseEffect::get_index() const { - return m_identifier; -} - PolicyFactory::PolicyFactory(std::shared_ptr element_factory) : m_pImpl(PolicyFactoryImpl(element_factory)) { } diff --git a/src/policy/policy_factory.cpp b/src/policy/policy_factory.cpp index 83bb1b1c..bd1ea408 100644 --- a/src/policy/policy_factory.cpp +++ b/src/policy/policy_factory.cpp @@ -63,7 +63,7 @@ std::shared_ptr PolicyFactoryImpl::parse_policy( std::shared_ptr PolicyFactoryImpl::make_boolean(const std::string& key, const std::shared_ptr& boolean) { auto result = m_cache.get_or_create(key, boolean); - if (!result.created && (result.object->get_boolean() != boolean)) { + if (!result.created && (result.object->get_element() != boolean)) { throw std::runtime_error("Failed to make boolean because a different boolean with the same key already exists."); } return result.object; @@ -71,7 +71,7 @@ std::shared_ptr PolicyFactoryImpl::make_boolean(const std::s std::shared_ptr PolicyFactoryImpl::make_numerical(const std::string& key, const std::shared_ptr& numerical) { auto result = m_cache.get_or_create(key, numerical); - if (!result.created && (result.object->get_numerical() != numerical)) { + if (!result.created && (result.object->get_element() != numerical)) { throw std::runtime_error("Failed to make numerical because a different numerical with the same key already exists."); } return result.object; @@ -79,7 +79,7 @@ std::shared_ptr PolicyFactoryImpl::make_numerical(const st std::shared_ptr PolicyFactoryImpl::make_concept(const std::string& key, const std::shared_ptr& concept_) { auto result = m_cache.get_or_create(key, concept_); - if (!result.created && (result.object->get_concept() != concept_)) { + if (!result.created && (result.object->get_element() != concept_)) { throw std::runtime_error("Failed to make concept because a different concept with the same key already exists."); } return result.object; @@ -87,7 +87,7 @@ std::shared_ptr PolicyFactoryImpl::make_concept(const std::s std::shared_ptr PolicyFactoryImpl::make_role(const std::string& key, const std::shared_ptr& role) { auto result = m_cache.get_or_create(key, role); - if (!result.created && (result.object->get_role() != role)) { + if (!result.created && (result.object->get_element() != role)) { throw std::runtime_error("Failed to make role because a different role with the same key already exists."); } return result.object; diff --git a/src/policy/policy_impl.cpp b/src/policy/policy_impl.cpp index 458f8ca4..78f524cf 100644 --- a/src/policy/policy_impl.cpp +++ b/src/policy/policy_impl.cpp @@ -2,6 +2,8 @@ #include "../../include/dlplan/core.h" #include "../../include/dlplan/utils/hash.h" +#include "../../include/dlplan/policy/condition.h" +#include "../../include/dlplan/policy/effect.h" #include #include @@ -9,29 +11,76 @@ namespace dlplan::policy { +struct InsertNamedElementFromCondition : public BaseConditionVisitor { + Booleans& booleans; + Numericals& numericals; + + InsertNamedElementFromCondition(Booleans& booleans_, Numericals& numericals_) + : booleans(booleans_), numericals(numericals_) { } + + void visit(const std::shared_ptr& condition) override { + booleans.insert(condition->get_named_element()); + } + + void visit(const std::shared_ptr& condition) override { + booleans.insert(condition->get_named_element()); + } + + void visit(const std::shared_ptr& condition) override { + numericals.insert(condition->get_named_element()); + } + + void visit(const std::shared_ptr& condition) override { + numericals.insert(condition->get_named_element()); + } +}; + + +struct InsertNamedElementFromEffect : public BaseEffectVisitor { + Booleans& booleans; + Numericals& numericals; + + InsertNamedElementFromEffect(Booleans& booleans_, Numericals& numericals_) + : booleans(booleans_), numericals(numericals_) { } + + void visit(const std::shared_ptr& effect) override { + booleans.insert(effect->get_named_element()); + } + + void visit(const std::shared_ptr& effect) override { + booleans.insert(effect->get_named_element()); + } + + void visit(const std::shared_ptr& effect) override { + booleans.insert(effect->get_named_element()); + } + + void visit(const std::shared_ptr& effect) override { + numericals.insert(effect->get_named_element()); + } + + void visit(const std::shared_ptr& effect) override { + numericals.insert(effect->get_named_element()); + } + + void visit(const std::shared_ptr& effect) override { + numericals.insert(effect->get_named_element()); + } + +}; + + Policy::Policy(int identifier, const Rules& rules) - : m_identifier(identifier), m_rules(rules) { + : Base(identifier), m_rules(rules) { // Retrieve boolean and numericals from the rules. + InsertNamedElementFromCondition condition_visitor(m_booleans, m_numericals); + InsertNamedElementFromEffect effect_visitor(m_booleans, m_numericals); for (const auto& rule : m_rules) { for (const auto& condition : rule->get_conditions()) { - const auto boolean = condition->get_boolean(); - if (boolean) { - m_booleans.insert(boolean); - } - const auto numerical = condition->get_numerical(); - if (numerical) { - m_numericals.insert(numerical); - } + condition->accept(condition_visitor); } for (const auto& effect : rule->get_effects()) { - const auto boolean = effect->get_boolean(); - if (boolean) { - m_booleans.insert(boolean); - } - const auto numerical = effect->get_numerical(); - if (numerical) { - m_numericals.insert(numerical); - } + effect->accept(effect_visitor); } } } @@ -46,7 +95,7 @@ Policy& Policy::operator=(Policy&& other) = default; Policy::~Policy() = default; -bool Policy::operator==(const Policy& other) const { +bool Policy::are_equal_impl(const Policy& other) const { if (this != &other) { return m_booleans == other.m_booleans && m_numericals == other.m_numericals @@ -54,11 +103,8 @@ bool Policy::operator==(const Policy& other) const { } return true; } -bool Policy::operator<(const Policy& other) const { - return m_identifier < other.m_identifier; -} -size_t Policy::hash() const { +size_t Policy::hash_impl() const { return hash_combine( hash_set(m_booleans), hash_set(m_numericals), @@ -121,40 +167,24 @@ std::shared_ptr Policy::evaluate_effects(const core::State& source_s return nullptr; } - -std::string Policy::compute_repr() const { - // Canonical representation - std::stringstream ss; - ss << "(:policy\n"; - std::vector> sorted_rules(m_rules.begin(), m_rules.end()); - std::sort(sorted_rules.begin(), sorted_rules.end(), [](const auto& r1, const auto& r2){ return r1->compute_repr() < r2->compute_repr(); }); - for (const auto& r : sorted_rules) { - ss << r->compute_repr() << "\n"; - } - ss << ")"; - return ss.str(); -} - -std::string Policy::str() const { - std::stringstream ss; - ss << "(:policy\n"; - ss << "(:booleans "; +void Policy::str_impl(std::stringstream& out) const { + out << "(:policy\n"; + out << "(:booleans "; for (const auto& boolean : m_booleans) { - ss << "(" << boolean->get_key() << " \"" << boolean->get_boolean()->str() << "\")"; - if (boolean != *m_booleans.rbegin()) ss << " "; + out << "(" << boolean->get_key() << " \"" << boolean->get_element()->str() << "\")"; + if (boolean != *m_booleans.rbegin()) out << " "; } - ss << ")\n"; - ss << "(:numericals "; + out << ")\n"; + out << "(:numericals "; for (const auto& numerical : m_numericals) { - ss << "(" << numerical->get_key() << " \"" << numerical->get_numerical()->str() << "\")"; - if (numerical != *m_numericals.rbegin()) ss << " "; + out << "(" << numerical->get_key() << " \"" << numerical->get_element()->str() << "\")"; + if (numerical != *m_numericals.rbegin()) out << " "; } - ss << ")\n"; + out << ")\n"; for (const auto& rule : m_rules) { - ss << rule->str() << "\n"; + out << rule->str() << "\n"; } - ss << ")"; - return ss.str(); + out << ")"; } int Policy::compute_evaluate_time_score() const { @@ -165,10 +195,6 @@ int Policy::compute_evaluate_time_score() const { return score; } -int Policy::get_index() const { - return m_identifier; -} - const Booleans& Policy::get_booleans() const { return m_booleans; } diff --git a/src/policy/role.cpp b/src/policy/role.cpp deleted file mode 100644 index b896bc3b..00000000 --- a/src/policy/role.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "../../include/dlplan/policy.h" - -#include "../../include/dlplan/core.h" -#include "../../include/dlplan/utils/hash.h" - -#include -#include -#include -#include -#include - -#include - - -namespace dlplan::policy { -NamedRole::NamedRole(int identifier, const std::string& key, std::shared_ptr role) - : m_identifier(identifier), m_key(key), m_role(role) { } - -NamedRole::NamedRole(const NamedRole& other) = default; - -NamedRole& NamedRole::operator=(const NamedRole& other) = default; - -NamedRole::NamedRole(NamedRole&& other) = default; - -NamedRole& NamedRole::operator=(NamedRole&& other) = default; - -NamedRole::~NamedRole() = default; - -bool NamedRole::operator==(const NamedRole& other) const { - if (this != &other) { - return m_key == other.m_key - && m_role == other.m_role; - } - return true; -} - -bool NamedRole::operator<(const NamedRole& other) const { - return m_identifier < other.m_identifier; -} - -size_t NamedRole::hash() const { - return hash_combine(m_key, m_role); -} - -int NamedRole::compute_evaluate_time_score() const { - return m_role->compute_evaluate_time_score(); -} - -std::string NamedRole::compute_repr() const { - return get_key(); -} - -std::string NamedRole::str() const { - return get_key(); -} - -const std::string& NamedRole::get_key() const { - return m_key; -} - -std::shared_ptr NamedRole::get_role() const { - return m_role; -} - -} diff --git a/src/policy/rule.cpp b/src/policy/rule.cpp index 9632bd3b..64fc87b6 100644 --- a/src/policy/rule.cpp +++ b/src/policy/rule.cpp @@ -8,7 +8,7 @@ namespace dlplan::policy { Rule::Rule(int identifier, const Conditions& conditions, const Effects& effects) - : m_identifier(identifier), m_conditions(conditions), m_effects(effects) { + : Base(identifier), m_conditions(conditions), m_effects(effects) { } Rule::Rule(const Rule& other) = default; @@ -21,18 +21,15 @@ Rule& Rule::operator=(Rule&& other) = default; Rule::~Rule() = default; -bool Rule::operator==(const Rule& other) const { +bool Rule::are_equal_impl(const Rule& other) const { if (this != &other) { return m_conditions == other.m_conditions && m_effects == other.m_effects; } return true; } -bool Rule::operator<(const Rule& other) const { - return m_identifier < other.m_identifier; -} -size_t Rule::hash() const { +size_t Rule::hash_impl() const { return hash_combine( hash_set(m_conditions), hash_set(m_effects)); @@ -66,54 +63,23 @@ bool Rule::evaluate_effects(const core::State& source_state, const core::State& return true; } -std::string Rule::compute_repr() const { - std::stringstream ss; - ss << "(:rule (:conditions "; - // sort conditions by repr to obtain canonical representation - std::vector> sorted_conditions(m_conditions.begin(), m_conditions.end()); - std::sort(sorted_conditions.begin(), sorted_conditions.end(), [&](const auto& l, const auto& r){ - return l->compute_repr() < r->compute_repr(); - }); - for (const auto& c : sorted_conditions) { - ss << c->compute_repr(); - if (c != *sorted_conditions.rbegin()) { - ss << " "; - } - } - ss << ") (:effects "; - // sort conditions by repr to obtain canonical representation - std::vector> sorted_effects(m_effects.begin(), m_effects.end()); - std::sort(sorted_effects.begin(), sorted_effects.end(), [&](const auto& l, const auto& r){ - return l->compute_repr() < r->compute_repr(); - }); - for (const auto& e : sorted_effects) { - ss << e->compute_repr(); - if (e != *sorted_effects.rbegin()) { - ss << " "; - } - } - ss << "))"; - return ss.str(); -} -std::string Rule::str() const { - std::stringstream ss; - ss << "(:rule (:conditions "; +void Rule::str_impl(std::stringstream& out) const { + out << "(:rule (:conditions "; for (const auto& c : m_conditions) { - ss << c->str(); + out << c->str(); if (c != *m_conditions.rbegin()) { - ss << " "; + out << " "; } } - ss << ") (:effects "; + out << ") (:effects "; for (const auto& e : m_effects) { - ss << e->str(); + out << e->str(); if (e != *m_effects.rbegin()) { - ss << " "; + out << " "; } } - ss << "))"; - return ss.str(); + out << "))"; } int Rule::compute_evaluate_time_score() const { @@ -127,10 +93,6 @@ int Rule::compute_evaluate_time_score() const { return score; } -int Rule::get_index() const { - return m_identifier; -} - const Conditions& Rule::get_conditions() const { return m_conditions; } diff --git a/tests/policy/policy_factory.cpp b/tests/policy/policy_factory.cpp index 9deb79cf..db4028e2 100644 --- a/tests/policy/policy_factory.cpp +++ b/tests/policy/policy_factory.cpp @@ -33,9 +33,11 @@ TEST(DLPTests, PolicyBuilderTest) { // E.g. canonicity policy_factory.make_rule({c_b_pos_2}, {e_b_neg_1}); auto policy = policy_factory.make_policy({policy_factory.make_rule({c_b_pos_2}, {e_b_neg_1})}); - EXPECT_EQ(policy->compute_repr(), + EXPECT_EQ(policy->str(), "(:policy\n" - "(:rule (:conditions (:c_b_pos \"b_empty(c_primitive(package,0))\")) (:effects (:e_b_neg \"b_empty(r_primitive(at,0,1))\")))\n" + "(:booleans (b1 \"b_empty(c_primitive(package,0))\") (b0 \"b_empty(r_primitive(at,0,1))\"))\n" + "(:numericals )\n" + "(:rule (:conditions (:c_b_pos b1)) (:effects (:e_b_neg b0)))\n" ")" ); } diff --git a/tests/policy/policy_minimizer.cpp b/tests/policy/policy_minimizer.cpp index 72b2a89a..bb373b07 100644 --- a/tests/policy/policy_minimizer.cpp +++ b/tests/policy/policy_minimizer.cpp @@ -35,10 +35,10 @@ TEST(DLPTests, StructuralMinimization) { auto minimized_policy = PolicyMinimizer().minimize(input_policy, policy_factory); auto result_policy = policy_factory.parse_policy(minimized_policy_textual); std::cout << "Input policy:" << std::endl - << input_policy->compute_repr() << std::endl << std::endl + << input_policy->str() << std::endl << std::endl << "Minimized policy:" << std::endl - << minimized_policy->compute_repr() << std::endl; - EXPECT_EQ(minimized_policy->compute_repr(), result_policy->compute_repr()); + << minimized_policy->str() << std::endl; + EXPECT_EQ(minimized_policy->str(), result_policy->str()); } @@ -66,10 +66,10 @@ TEST(DLPTests, StructuralMinimization2) { auto minimized_policy = PolicyMinimizer().minimize(input_policy, policy_factory); auto result_policy = policy_factory.parse_policy(minimized_policy_textual); std::cout << "Input policy:" << std::endl - << input_policy->compute_repr() << std::endl << std::endl + << input_policy->str() << std::endl << std::endl << "Minimized policy:" << std::endl - << minimized_policy->compute_repr() << std::endl; - EXPECT_EQ(minimized_policy->compute_repr(), result_policy->compute_repr()); + << minimized_policy->str() << std::endl; + EXPECT_EQ(minimized_policy->str(), result_policy->str()); } @@ -98,10 +98,10 @@ TEST(DLPTests, StructuralMinimization3) { auto minimized_policy = PolicyMinimizer().minimize(input_policy, policy_factory); auto result_policy = policy_factory.parse_policy(minimized_policy_textual); std::cout << "Input policy:" << std::endl - << input_policy->compute_repr() << std::endl << std::endl + << input_policy->str() << std::endl << std::endl << "Minimized policy:" << std::endl - << minimized_policy->compute_repr() << std::endl; - EXPECT_EQ(minimized_policy->compute_repr(), result_policy->compute_repr()); + << minimized_policy->str() << std::endl; + EXPECT_EQ(minimized_policy->str(), result_policy->str()); } @@ -128,10 +128,10 @@ TEST(DLPTests, StructuralMinimization4) { auto minimized_policy = PolicyMinimizer().minimize(input_policy, policy_factory); auto result_policy = policy_factory.parse_policy(minimized_policy_textual); std::cout << "Input policy:" << std::endl - << input_policy->compute_repr() << std::endl << std::endl + << input_policy->str() << std::endl << std::endl << "Minimized policy:" << std::endl - << minimized_policy->compute_repr() << std::endl; - EXPECT_EQ(minimized_policy->compute_repr(), result_policy->compute_repr()); + << minimized_policy->str() << std::endl; + EXPECT_EQ(minimized_policy->str(), result_policy->str()); } @@ -180,10 +180,10 @@ TEST(DLPTests, EmpiricalMinimization) { auto minimized_policy = PolicyMinimizer().minimize(input_policy, true_state_pairs, false_state_pairs, policy_factory); auto result_policy = policy_factory.parse_policy(minimized_policy_textual); std::cout << "Input policy:" << std::endl - << input_policy->compute_repr() << std::endl << std::endl + << input_policy->str() << std::endl << std::endl << "Minimized policy:" << std::endl - << minimized_policy->compute_repr() << std::endl; - EXPECT_EQ(minimized_policy->compute_repr(), result_policy->compute_repr()); + << minimized_policy->str() << std::endl; + EXPECT_EQ(minimized_policy->str(), result_policy->str()); } }