Class State

Class Documentation

class State

Represents a PDDL state as S = <O, Pred, InfPred, Der, Func>

This class is used to model a PDDL state as S = <O, Pred, InfPred, Der, Func>. Where O denotes the objects (i.e., instances), Pred the grounded predicates in the state, InfPred the grounded predicates inferred from the derived predicates, Der the derived predicates resolution graph, and Func the grounded functions. Furthermore, this class provides methods to manipulate and read the state, for example, to add new predicates, instances etc.

Public Functions

inline State()
inline State(const std::unordered_set<plansys2::Instance> &instances, std::unordered_set<plansys2::Function> &functions, const std::unordered_set<plansys2::Predicate> &predicates)
inline State(const std::unordered_set<plansys2::Instance> &instances, std::unordered_set<plansys2::Function> &functions, const std::unordered_set<plansys2::Predicate> &predicates, const plansys2::DerivedResolutionGraph &derived_predicates)
State(const std::unordered_set<plansys2::Instance> &instances, const std::unordered_set<plansys2::Function> &functions, const std::unordered_set<plansys2::Predicate> &predicates, const std::vector<std::tuple<plansys2::Derived, plansys2::Predicate>> &inferred_predicates)
State(const std::unordered_set<plansys2::Instance> &instances, const std::unordered_set<plansys2::Function> &functions, const std::unordered_set<plansys2::Predicate> &predicates, const std::vector<std::tuple<plansys2::Derived, plansys2::Predicate>> &inferred_predicates, const plansys2::DerivedResolutionGraph &derived_predicates)
State(const plansys2_msgs::msg::State &state)
bool operator==(const State &state) const
inline auto &getInstances() const
inline auto &getFunctions() const
inline auto &getPredicates() const
inline auto &getInferredPredicates() const
inline auto &getUnionPredicatesInferredPredicates() const
inline auto &getDerivedPredicates() const
std::vector<std::vector<Derived>> getDerivedPredicatesSCCs() const

Retrieves the strongly connected components (SCCs) of derived predicates.

This method computes the SCCs of the derived predicates using Tarjan’s algorithm, reverses the order of the resulting SCCs, and returns them as a vector of vectors in topological order.

Returns:

A vector of vectors, where each inner vector contains derived predicates that form a strongly connected component.

std::vector<std::vector<Derived>> getDerivedPredicatesSCCs(const std::vector<plansys2_msgs::msg::Node> &root_nodes) const

Computes the strongly connected components (SCCs) of derived predicates.

This function returns the SCCs of the derived predicates graph using Tarjan’s algorithm. If no root nodes are provided, it computes the SCCs for the entire derived predicates graph. If a set of root nodes is provided, it computes the SCCs for the subgraph induced by those nodes. The resulting SCCs are returned in reverse topological order.

Parameters:

root_nodes – A vector of root nodes to define the subgraph for SCC computation. If empty, the entire graph is considered.

Returns:

std::vector<std::vector<Derived>> A vector of SCCs, each represented as a vector of Derived predicates.

std::vector<plansys2::Derived> getDerivedPredicatesDepthFirst() const

Retrieves all derived predicates in the state using a depth-first traversal.

This method returns a vector containing all derived predicates found in the current state. The predicates are collected by traversing the state in a depth-first manner, which may be useful for scenarios where the order of discovery or dependency resolution is important.

Returns:

std::vector<plansys2::Derived> A vector of derived predicates present in the state.

std::vector<plansys2::Derived> getDerivedPredicatesDepthFirst(const std::vector<plansys2_msgs::msg::Node> &root_nodes) const

Retrieves all children derived predicates from root_nodes using a depth-first traversal.

This method traverses the given root nodes in a depth-first manner to collect all derived predicates present in the state. Derived predicates are computed based on the current state and the logical structure of the provided nodes.

Parameters:

root_nodes – A vector of root nodes from which to start the depth-first traversal.

Returns:

A vector containing all derived predicates found during the traversal.

inline auto getInstancesSize() const
inline auto getFunctionsSize() const
inline auto getPredicatesSize() const
inline auto getInferredPredicatesSize() const
inline auto getUnionPredicatesSize() const
inline auto getDerivedPredicatesSize() const
inline size_t getInferredPredicateRefCount(const plansys2::Predicate &predicate) const

Retrieves the reference count for a given inferred predicate.

This function returns the number of references associated with the specified inferred predicate in the internal reference count map. That is, from how many derived predicates the inferred predicates was inferred from.

Parameters:

predicate – The predicate whose reference count is to be retrieved.

Returns:

The reference count of the specified inferred predicate.

size_t getNumberInferredFromDerived(const plansys2::Derived &derived) const

Returns the number of inferred predicates inferred from a given derived predicate.

Parameters:

derived – The derived predicate whose inferred predicates count is to be retrieved.

Returns:

The number of inferred predicates generated from the specified derived predicate.

inline bool addInstance(const plansys2::Instance &instance)
inline bool addInstance(plansys2::Instance &&instance)
inline bool addPredicate(const plansys2::Predicate &predicate)
inline bool addPredicate(plansys2::Predicate &&predicate)
bool addInferredPredicate(const plansys2::Derived &derived, const plansys2::Predicate &predicate)
bool addInferredPredicate(const plansys2::Derived &derived, plansys2::Predicate &&predicate)
inline bool addFunction(const plansys2::Function &function)
inline bool addFunction(plansys2::Function &&function)
inline void removeInstance(const std::unordered_set<plansys2::Instance>::const_iterator it)
inline bool removeInstance(plansys2::Instance instance)
inline auto removePredicate(const std::unordered_set<plansys2::Predicate>::const_iterator it)
inline bool removePredicate(const plansys2::Predicate &predicate)
inline bool removePredicate(plansys2::Predicate &&predicate)
inline auto removeFunction(const std::unordered_set<plansys2::Function>::const_iterator it)
inline bool removeFunction(const plansys2::Function &function)
inline bool removeFunction(plansys2::Function &&function)
inline bool hasInstance(const plansys2::Instance &instance)
inline bool hasInstance(plansys2::Instance &&instance)
inline bool hasPredicate(const std::string &predicate_str)
inline bool hasPredicate(const plansys2::Predicate &predicate)
inline bool hasPredicate(plansys2::Predicate &&predicate)
inline bool hasInferredPredicate(const std::string &predicate_str)
inline bool hasInferredPredicate(const plansys2::Predicate &predicate)
inline bool hasInferredPredicate(plansys2::Predicate &&predicate)
inline bool hasFunction(const plansys2::Function &function)
inline bool hasFunction(plansys2::Function &&function)
inline auto getInstance(const plansys2::Instance &instance) const
inline auto getInstance(plansys2::Instance &&instance) const
inline auto getPredicate(const plansys2::Predicate &predicate) const
inline auto getPredicate(plansys2::Predicate &&predicate) const
inline auto getFunction(const plansys2::Function &function) const
inline auto getFunction(plansys2::Function &&function) const
inline void setDerivedPredicates(const plansys2::DerivedResolutionGraph derived_predicates)
inline bool hasDerivedPredicates()
inline void reserveInferredPredicates(size_t size)
void clearPredicates()
void clearState()
void resetInferredPredicates()
void initInferredPredicates()
bool ungroundSingleDerivedPredicate(const plansys2::Derived &derived)

Attempts to unground a single derived predicate.

This method attempts to unground the inferred predicates inferred from the given derived predicate. First, this method decreases the reference count of the inferred predicates, and if the reference count reaches zero, it ungrounds the inferred predicate.

Parameters:

derived – The derived predicate to be ungrounded.

Returns:

true if the ungrounding was successful, false otherwise.

std::unordered_set<plansys2::Derived> ungroundDerivedPredicate(const plansys2::Derived &derived)

Attempts to unground the given derived predicate and all its children.

This method attempts to unground the inferred predicates inferred from the given derived predicate and all its children. It leverages the ungroundSingleDerivedPredicate method to unground each individual derived predicate. It returns an unorded set containing all the derived predicates that were ungrounded.

Parameters:

derived – The parent derived predicate to start ungrouding from.

Returns:

std::unordered_set<plansys2::Derived> Set of ungrounded derived predicates.

plansys2_msgs::msg::State getAsMsg()

Converts the current state to a ROS 2 message.

This method serializes the internal representation of the state into a plansys2_msgs::msg::State message, which can be published or transmitted over ROS 2 communication channels.

Returns:

A plansys2_msgs::msg::State message representing the current state.

void addActionsAndPruneDerived(const std::vector<plansys2::ActionVariant> &actions)

Adds actions to the derived resolution graph and prunes the graph.

This function takes a vector of ActionVariant objects, adds them to the derived resolution graph, and prunes the graph to contain only nodes that the actions depend on.

Parameters:

actions – A vector of ActionVariant objects to be added to the derived resolution graph.

void pruneDerivedPredicatesToActions(const std::vector<plansys2::ActionVariant> &actions)

Prunes the derived resolution graph to contain only nodes that the actions depend on.

This function analyzes the provided list of actions and prunes any derived predicates from the state that are not referenced or required by those actions. This helps to optimize the state representation by keeping only relevant derived predicates.

Parameters:

actions – A vector of ActionVariant objects representing the actions whose required derived predicates should be retained in the state.

Friends

friend struct std::hash< State >