C++ YASMIN (Yet Another State MachINe)
Loading...
Searching...
No Matches
yasmin::Concurrence Class Reference

Runs a series of states in parallel. More...

#include <concurrence.hpp>

Inheritance diagram for yasmin::Concurrence:
Collaboration diagram for yasmin::Concurrence:

Public Member Functions

 Concurrence (const StateMap &states, const std::string &default_outcome, const OutcomeMap &outcome_map, const ParameterMappingsMap &parameter_mappings={})
 Shared pointer type for Concurrence.
void set_parameter_mappings (const std::string &state_name, const ParameterMappings &parameter_mappings)
 Sets parameter mappings for a child state.
const ParameterMappingsMap & get_parameter_mappings () const noexcept
 Returns the parameter mappings for this concurrence state.
void configure () override
 Configures this concurrence state and all of its children.
std::string execute (Blackboard::SharedPtr blackboard) override
 Executes the state's specific logic.
void cancel_state () override
 Cancels the current state execution.
const StateMap & get_states () const noexcept
 Returns the map of states managed by this concurrence state.
const OutcomeMap & get_outcome_map () const noexcept
 Returns the outcome map for this concurrence state.
const std::string & get_default_outcome () const noexcept
 Returns the default outcome for this concurrence state.
std::string to_string () const override
 Converts the state to a string representation.
Public Member Functions inherited from yasmin::State
 State (const Outcomes &outcomes)
 Shared pointer type for State.
virtual ~State ()
 Virtual destructor.
Blackboard::SharedPtr get_parameters_blackboard () const
 Gets the private parameter blackboard of this state.
bool is_idle () const noexcept
 Checks if the state is idle.
bool is_running () const noexcept
 Checks if the state is currently running.
bool is_canceled () const noexcept
 Checks if the state has been canceled.
bool is_completed () const noexcept
 Checks if the state has completed execution.
std::string operator() (Blackboard::SharedPtr blackboard)
 Executes the state and returns the outcome.
Outcomes const & get_outcomes () const noexcept
 Gets the set of possible outcomes for this state.
void set_description (const std::string &description)
 Sets the description for this state.
const std::string & get_description () const
 Gets the description of this state.
void set_outcome_description (const std::string &outcome, const std::string &description)
 Sets a human-readable description for an outcome.
const std::string & get_outcome_description (const std::string &outcome) const
 Gets the human-readable description for an outcome.
const std::unordered_map< std::string, std::string > & get_outcome_descriptions () const
 Gets all outcome descriptions.
void add_input_key (const BlackboardKeyInfo &key_info)
 Adds an input key to the state's metadata.
void add_input_key (const std::string &key_name)
 Adds an input key with a name only.
void add_input_key (const std::string &key_name, const std::string &description)
 Adds an input key with a name and description.
template<typename T>
void add_input_key (const std::string &key_name, const std::string &description, T default_value)
 Adds an input key with a name, default value and description.
void add_output_key (const BlackboardKeyInfo &key_info)
 Adds an output key to the state's metadata.
void add_output_key (const std::string &key_name)
 Adds an output key with a name only.
void add_output_key (const std::string &key_name, const std::string &description)
 Adds an output key with a name and description.
const std::vector< BlackboardKeyInfo > & get_input_keys () const
 Gets the input keys metadata.
const std::vector< BlackboardKeyInfo > & get_output_keys () const
 Gets the output keys metadata.
void declare_parameter (const BlackboardKeyInfo &parameter_info)
 Declares a parameter in the state metadata.
void declare_parameter (const std::string &parameter_name)
 Declares a parameter with a name only.
void declare_parameter (const std::string &parameter_name, const std::string &description)
 Declares a parameter with a name and description.
template<typename T>
void declare_parameter (const std::string &parameter_name, const std::string &description, T default_value)
 Declares a parameter with a name, description, and default value.
bool has_parameter (const std::string &parameter_name) const
 Checks if a parameter exists in the state-local parameter storage.
template<typename T>
T get_parameter (const std::string &parameter_name) const
 Gets a parameter from the state-local parameter storage.
template<typename T>
void set_parameter (const std::string &parameter_name, T value)
 Sets a parameter in the state-local parameter storage.
bool is_parameter_declared (const std::string &parameter_name) const
 Checks whether a parameter was declared in the state metadata.
void copy_parameter_from (const State &source_state, const std::string &source_parameter_name, const std::string &target_parameter_name)
 Copies a parameter value from another state.
const std::vector< BlackboardKeyInfo > & get_parameters () const
 Gets the declared parameter metadata.
const StateMetadata & get_metadata () const
 Gets the complete state metadata.

Protected Attributes

const StateMap states
 The states to run concurrently (name -> state).
const std::string default_outcome
 Default outcome.
OutcomeMap outcome_map
StateOutcomeMap intermediate_outcome_map
 Stores the intermediate outcomes of the concurrent states.
Outcomes possible_outcomes
 The set of possible outcomes.
ParameterMappingsMap parameter_mappings
 Per-child parameter mappings applied during configure().
Protected Attributes inherited from yasmin::State
Outcomes outcomes
 The possible outcomes of this state.

Private Member Functions

void apply_parameter_mappings (const std::string &state_name, const State::SharedPtr &state)
 Applies this container's parameter mappings to a direct child.

Static Private Member Functions

static Outcomes generate_possible_outcomes (const OutcomeMap &outcome_map, const std::string &default_outcome)
 Helper function to generate a set of possible outcomes from an outcome map.

Private Attributes

std::mutex intermediate_outcome_mutex
 Mutex for intermediate outcome map.
std::atomic_bool configured {false}
 Flag to indicate if this concurrence state has been configured.

Detailed Description

Runs a series of states in parallel.

The Concurrence class runs a set of states concurrently, waiting for the termination of each, and then returns a single output according to a provided rule map, or a default outcome if no rule is satisfied.

Constructor & Destructor Documentation

◆ Concurrence()

Concurrence::Concurrence ( const StateMap & states,
const std::string & default_outcome,
const OutcomeMap & outcome_map,
const ParameterMappingsMap & parameter_mappings = {} )

Shared pointer type for Concurrence.

Constructs a State with a set of possible outcomes.

Parameters
statesA map of state names to states that will run concurrently.
default_outcomeThe default outcome to return if no outcome map rules are satisfied.
outcome_mapA map of outcome names to requirements for achieving that outcome.

Member Function Documentation

◆ apply_parameter_mappings()

void Concurrence::apply_parameter_mappings ( const std::string & state_name,
const State::SharedPtr & state )
private

Applies this container's parameter mappings to a direct child.

Parameters
state_nameThe child state name.
stateThe child state instance.

◆ cancel_state()

void Concurrence::cancel_state ( )
overridevirtual

Cancels the current state execution.

This method sets the canceled flag to true and logs the action.

Reimplemented from yasmin::State.

◆ configure()

void Concurrence::configure ( )
overridevirtual

Configures this concurrence state and all of its children.

Reimplemented from yasmin::State.

◆ execute()

std::string Concurrence::execute ( Blackboard::SharedPtr blackboard)
overridevirtual

Executes the state's specific logic.

Parameters
blackboardA shared pointer to the Blackboard to use during execution.
Returns
A string representing the outcome of the execution.

This method is intended to be overridden by derived classes to provide specific execution logic.

Reimplemented from yasmin::State.

◆ generate_possible_outcomes()

Outcomes Concurrence::generate_possible_outcomes ( const OutcomeMap & outcome_map,
const std::string & default_outcome )
staticprivate

Helper function to generate a set of possible outcomes from an outcome map.

Parameters
outcome_map
default_outcome
Returns
The set of possible outcomes

◆ get_default_outcome()

const std::string & Concurrence::get_default_outcome ( ) const
noexcept

Returns the default outcome for this concurrence state.

Returns
The default outcome as a string.

◆ get_outcome_map()

const OutcomeMap & Concurrence::get_outcome_map ( ) const
noexcept

Returns the outcome map for this concurrence state.

Returns
A map of outcome names to their requirements.

◆ get_parameter_mappings()

const ParameterMappingsMap & Concurrence::get_parameter_mappings ( ) const
noexcept

Returns the parameter mappings for this concurrence state.

Returns
A constant reference to the parameter mappings.

◆ get_states()

const StateMap & Concurrence::get_states ( ) const
noexcept

Returns the map of states managed by this concurrence state.

Returns
A map of state names to states.

◆ set_parameter_mappings()

void Concurrence::set_parameter_mappings ( const std::string & state_name,
const ParameterMappings & parameter_mappings )

Sets parameter mappings for a child state.

Parameters
state_nameThe child state name.
parameter_mappingsMapping entries child_parameter -> parent_parameter.

◆ to_string()

std::string Concurrence::to_string ( ) const
overridevirtual

Converts the state to a string representation.

Returns
A string representation of the state.

Reimplemented from yasmin::State.

Member Data Documentation

◆ configured

std::atomic_bool yasmin::Concurrence::configured {false}
private

Flag to indicate if this concurrence state has been configured.

◆ default_outcome

const std::string yasmin::Concurrence::default_outcome
protected

Default outcome.

◆ intermediate_outcome_map

StateOutcomeMap yasmin::Concurrence::intermediate_outcome_map
protected

Stores the intermediate outcomes of the concurrent states.

◆ intermediate_outcome_mutex

std::mutex yasmin::Concurrence::intermediate_outcome_mutex
private

Mutex for intermediate outcome map.

◆ outcome_map

OutcomeMap yasmin::Concurrence::outcome_map
protected

Specifies which combination of state outputs should produce a given overall output

◆ parameter_mappings

ParameterMappingsMap yasmin::Concurrence::parameter_mappings
protected

Per-child parameter mappings applied during configure().

◆ possible_outcomes

Outcomes yasmin::Concurrence::possible_outcomes
protected

The set of possible outcomes.

◆ states

const StateMap yasmin::Concurrence::states
protected

The states to run concurrently (name -> state).


The documentation for this class was generated from the following files: