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:
objectDefines the lifecycle of a simulation in terms of state machine defined using the pytransitions package.
A Simulation is created in the
createdinitial state; theinitializedtrigger makes it transition topaused. Thestartedtrigger makes it move tostartedstate, thencompletedtocompletedand finally,stoppedto thestoppedfinal state. From any running state, thefailedtrigger will result in the finalfailedstate.After a transition, state changes may be propagated on the
synchronization_topicto other instances ofSimulationLifecycle.See “NRP Backend’s life-cycle state machine” in
hbp_nrp_backenddocs 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.
-
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
-