Skip to content

Commit

Permalink
separated base into base and baseelement, base will be a general base…
Browse files Browse the repository at this point in the history
… for any object in dlplan
  • Loading branch information
drexlerd committed Nov 22, 2023
1 parent aa93c73 commit 7086170
Show file tree
Hide file tree
Showing 68 changed files with 117 additions and 112 deletions.
95 changes: 50 additions & 45 deletions include/dlplan/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,80 +709,89 @@ class State {
};


/// @brief Represents the abstract base class of an element with functionality
/// for computing string representations and its complexity.
/// @brief Represents the abstract base class of an object
/// with functionality for computing string representations.
template<typename Derived>
class BaseElement {
class Base {
protected:
std::shared_ptr<VocabularyInfo> m_vocabulary_info;
ElementIndex m_index;
/**
* if true then element is evaluated per instance rather than per state.
*/
bool m_is_static;
int m_index;

BaseElement(std::shared_ptr<VocabularyInfo> vocabulary_info, ElementIndex index, bool is_static)
: m_vocabulary_info(vocabulary_info), m_index(index), m_is_static(is_static) { }
Base(int index) : m_index(index) { }

public:
~BaseElement() { }
~Base() { }

bool operator==(const Derived& other) const {
return static_cast<const Derived*>(this)->operator==(other);
bool operator==(const Base& other) const {
return static_cast<const Derived*>(this)->are_equal_impl(static_cast<const Derived&>(other));
}

bool operator<(const Derived& other) const {
bool operator<(const Base& other) const {
return m_index < other.m_index;
}

/// @brief Computes a hash value for this element.
/// @return
size_t hash() const {
/// @brief Computes a hash value for this object.
size_t hash() const {
return static_cast<const Derived*>(this)->hash_impl();
}

/// @brief Compute the canonical string representation of this element.
/// @return The canonical string representation of this element.
/// @brief Compute a canonical string representation of this object.
std::string compute_repr() const {
std::stringstream ss;
std::stringstream ss;
compute_repr(ss);
return ss.str();
}

/// @brief Helper function to compute the canonical string representation of this element.
/// @brief Overload of the output stream insertion operator (operator<<).
friend std::ostream& operator<<(std::ostream& os, const Base& element) {
os << element.str();
return os;
}

/// @brief Compute a canonical string representation of this object.
void compute_repr(std::stringstream& out) const {
static_cast<const Derived*>(this)->compute_repr_impl(out);
}

/// @brief Compute a string representation of this object.
std::string str() const {
return static_cast<const Derived*>(this)->str_impl();
}

/* Getters. */
int get_index() const { return m_index; }
};


template<typename Derived>
class BaseElement : public Base<Derived> {
protected:
std::shared_ptr<VocabularyInfo> m_vocabulary_info;
bool m_is_static;

public:
BaseElement(int index, std::shared_ptr<VocabularyInfo> vocabulary_info, bool is_static)
: Base<Derived>(index), m_vocabulary_info(vocabulary_info), m_is_static(is_static) { }

~BaseElement() { }

/// @brief Returns the complexity of the element
/// measured in the size of the abstract syntax tree.
/// @return An integer that represents the number of element constructors in the represenation.
int compute_complexity() const {
return static_cast<const Derived*>(this)->compute_complexity_impl();
}

/// @brief Computes a time score for evaluating this element relative to other elements.
/// The scoring assumes evaluation that uses caching.
/// @return An integer that represents the score.
int compute_evaluate_time_score() const {
return static_cast<const Derived*>(this)->compute_evaluate_time_score_impl();
}

/// @brief Overload of the output stream insertion operator (operator<<) for the BaseElement class.
/// Outputs a string representation of a BaseElement object to the specified output stream.
/// @param os The output stream to write the string representation to.
/// @param denotation The BaseElement to be represented as a string.
/// @return A reference to the output stream after writing the string representation.
friend std::ostream& operator<<(std::ostream& os, const BaseElement& element) {
os << element.compute_repr();
return os;
// For elements we simply return repr as a string representation.
std::string str_impl() const {
return Base<Derived>::compute_repr();
}

/// @brief Compute a string representation of this element.
/// @return A string representation of this element.
std::string str() const { return compute_repr(); }

ElementIndex get_index() const { return m_index; }
/* Getters. */
std::shared_ptr<VocabularyInfo> get_vocabulary_info() const { return m_vocabulary_info; }
bool is_static() const { return m_is_static; }
};
Expand All @@ -804,8 +813,7 @@ class Concept : public BaseElement<Concept> {
Concept& operator=(Concept&& other);
~Concept();

virtual bool operator==(const Concept& other) const = 0;

virtual bool are_equal_impl(const Concept& other) const = 0;
virtual size_t hash_impl() const = 0;
virtual void compute_repr_impl(std::stringstream& out) const = 0;
virtual int compute_complexity_impl() const = 0;
Expand Down Expand Up @@ -833,8 +841,7 @@ class Role : public BaseElement<Role> {
Role& operator=(Role&& other);
~Role();

virtual bool operator==(const Role& other) const = 0;

virtual bool are_equal_impl(const Role& other) const = 0;
virtual size_t hash_impl() const = 0;
virtual void compute_repr_impl(std::stringstream& out) const = 0;
virtual int compute_complexity_impl() const = 0;
Expand Down Expand Up @@ -862,8 +869,7 @@ class Numerical : public BaseElement<Numerical> {
Numerical& operator=(Numerical&& other);
~Numerical();

virtual bool operator==(const Numerical& other) const = 0;

virtual bool are_equal_impl(const Numerical& other) const = 0;
virtual size_t hash_impl() const = 0;
virtual void compute_repr_impl(std::stringstream& out) const = 0;
virtual int compute_complexity_impl() const = 0;
Expand Down Expand Up @@ -891,8 +897,7 @@ class Boolean : public BaseElement<Boolean> {
Boolean& operator=(Boolean&& other);
~Boolean();

virtual bool operator==(const Boolean& other) const = 0;

virtual bool are_equal_impl(const Boolean& other) const = 0;
virtual size_t hash_impl() const = 0;
virtual void compute_repr_impl(std::stringstream& out) const = 0;
virtual int compute_complexity_impl() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/booleans/empty.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class EmptyBoolean : public Boolean {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Boolean& other) const override {
bool are_equal_impl(const Boolean& other) const override {
if (typeid(*this) == typeid(other)) {
const auto& other_derived = static_cast<const EmptyBoolean<T>&>(other);
return m_is_static == other_derived.m_is_static
Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/booleans/inclusion.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class InclusionBoolean : public Boolean {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Boolean& other) const override {
bool are_equal_impl(const Boolean& other) const override {
if (typeid(*this) == typeid(other)) {
const auto& other_derived = static_cast<const InclusionBoolean<T>&>(other);
return m_is_static == other_derived.m_is_static
Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/booleans/nullary.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class NullaryBoolean : public Boolean {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Boolean& other) const override;
bool are_equal_impl(const Boolean& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/all.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AllConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/and.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class AndConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class BotConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DiffConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/equal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EqualConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/not.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NotConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/one_of.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OneOfConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/or.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class OrConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PrimitiveConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ProjectionConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/some.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SomeConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/subset.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SubsetConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/concepts/top.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TopConcept : public Concept {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Concept& other) const override;
bool are_equal_impl(const Concept& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/numericals/concept_distance.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConceptDistanceNumerical : public Numerical {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Numerical& other) const override;
bool are_equal_impl(const Numerical& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/numericals/count.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CountNumerical : public Numerical {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Numerical& other) const override {
bool are_equal_impl(const Numerical& other) const override {
if (typeid(*this) == typeid(other)) {
const auto& other_derived = static_cast<const CountNumerical&>(other);
return m_is_static == other_derived.m_is_static
Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/numericals/role_distance.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class RoleDistanceNumerical : public Numerical {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Numerical& other) const override;
bool are_equal_impl(const Numerical& other) const override;

size_t hash_impl() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SumConceptDistanceNumerical : public Numerical {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Numerical& other) const override;
bool are_equal_impl(const Numerical& other) const override;

size_t hash_impl() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SumRoleDistanceNumerical : public Numerical {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Numerical& other) const override;
bool are_equal_impl(const Numerical& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/roles/and.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AndRole : public Role {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Role& other) const override;
bool are_equal_impl(const Role& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/roles/compose.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ComposeRole : public Role {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Role& other) const override;
bool are_equal_impl(const Role& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/roles/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DiffRole : public Role {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Role& other) const override;
bool are_equal_impl(const Role& other) const override;

size_t hash_impl() const override;

Expand Down
2 changes: 1 addition & 1 deletion include/dlplan/core/elements/roles/identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IdentityRole : public Role {
friend class dlplan::ReferenceCountedObjectFactory;

public:
bool operator==(const Role& other) const override;
bool are_equal_impl(const Role& other) const override;

size_t hash_impl() const override;

Expand Down
Loading

0 comments on commit 7086170

Please sign in to comment.