hbp_nrp_commons.simulation_lifecycle module

This package defines the simulation lifecycle used in the NRP Backend and Simulation Server components.

class hbp_nrp_commons.simulation_lifecycle.SimulationLifecycle(synchronization_topic: str, initial_state: str = 'created', propagated_destinations: Optional[List[str]] = ['created', 'paused', 'started', 'completed', 'stopped', 'failed'], mqtt_client_id: Optional[str] = None, mqtt_broker_host: str = 'localhost', mqtt_broker_port: int = 1883, mqtt_topics_prefix: str = '', clear_synchronization_topic=False)[source]

Bases: object

Defines the lifecycle of a simulation in terms of state machine defined using the pytransitions package.

A Simulation is created in the created initial state; the initialized trigger makes it transition to paused. The started trigger makes it move to started state, then completed to completed and finally, stopped to the stopped final state. From any running state, the failed trigger will result in the final failed state.

After a transition, state changes may be propagated on the synchronization_topic to other instances of SimulationLifecycle.

See “NRP Backend’s life-cycle state machine” in hbp_nrp_backend docs for more info.

ERROR_STATES: List[str] = ['failed']
FINAL_STATES: List[str] = ['stopped', 'failed']
INITIAL_STATE: str = 'created'
RUNNING_STATES: List[str] = ['created', 'paused', 'started', 'completed']
STATES: List[str] = ['created', 'paused', 'started', 'completed', 'stopped', 'failed']
TRIGGERS: List[str] = ['initialized', 'paused', 'started', 'completed', 'stopped', 'failed']
accept_command(command)[source]

Accepts the given command for the simulation lifecycle.

Any error during the execution of the command, results in a state transition to failed so to perform a cleanup.

Parameters

command – the command that should be activated

Raise

Propagate any exception coming from the execution of the command

Raise

ValueError: command is not valid for the current state

fail(state_change)[source]

Gets called when the simulation fails.

Parameters

state_change – The state change that caused the simulation to fail.

initialize(state_change)[source]

Gets called when the simulation should be initialized.

Parameters

state_change – The state change that caused the simulation to initialize.

static is_error_state(state: str) → bool[source]
static is_final_state(state: str) → bool[source]
static is_initial_state(state: str) → bool[source]
static is_state(state: str) → bool[source]
pause(state_change)[source]

Gets called when the simulation needs to be paused.

Parameters

state_change – The state change that caused the simulation to pause.

static set_silent(state_change)[source]

Specifies that the given state change should not be propagated to other synchronized lifecycles

Parameters

state_change – The state change that should not be propagated

shutdown(_shutdown_event)[source]

Shuts down this simulation lifecycle instance

Parameters

_shutdown_event – The event that caused the shutdown

start(state_change)[source]

Gets called when the simulation needs to be started.

Parameters

state_change – The state change that caused the simulation to start.

stop(state_change)[source]

Gets called when the simulation needs to be stopped; it releases any simulation resource.

Parameters

state_change – The state change that caused the simulation to stop