Life Cycle Manager

Manager of ordering- and constraint-based contracts in the simulation

class vivarium.engine.framework.lifecycle.manager.LifeCycleManager[source]

Manages ordering- and constraint-based contracts in the simulation.

property name: str

The name of this component.

property current_state: str

The name of the current life cycle state.

property timings: dict[str, list[float]]
add_phase(phase_name, states, loop=False)[source]

Add a new phase to the lifecycle.

Phases must be added in order.

Return type:

None

Parameters:
  • phase_name (str) – The name of the phase to add. Phase names must be unique.

  • states (list[str]) – The list of names (in order) of the states that make up the life cycle phase. State names must be unique across the entire life cycle.

  • loop (bool) – Whether the life cycle phase states loop.

Raises:

LifeCycleError – If the phase or state names are non-unique.

set_state(state)[source]

Sets the current life cycle state to the provided state.

Return type:

None

Parameters:

state (str) – The name of the state to set.

Raises:
  • LifeCycleError – If the requested state doesn’t exist in the life cycle.

  • InvalidTransitionError – If setting the provided state represents an invalid life cycle transition.

get_state_names(phase)[source]

Gets all states in the phase in their order of execution.

Return type:

list[str]

Parameters:

phase (str) – The name of the phase to retrieve the states for.

Returns:

A list of state names in order of execution.

add_handlers(state_name, handlers)[source]

Registers a set of functions to be called during a life cycle state.

This method does not apply any constraints, rather it is used to build up an execution order for introspection.

Return type:

None

Parameters:
  • state_name (str) – The name of the state to register the handlers for.

  • handlers (list[Callable[[Event], None]]) – A list of functions that will execute during the state.

add_constraint(method, allow_during=(), restrict_during=())[source]

Constrains a function to be executable only during certain states.

Return type:

None

Parameters:
  • method (Callable[[...], Any]) – The method to add constraints to.

  • allow_during (tuple[str, ...] | list[str]) – An optional list of life cycle states in which the provided method is allowed to be called.

  • restrict_during (tuple[str, ...] | list[str]) – An optional list of life cycle states in which the provided method is restricted from being called.

Raises:
  • ValueError – If neither allow_during nor restrict_during are provided, or if both are provided.

  • LifeCycleError – If states provided as arguments are not in the life cycle.

  • ConstraintError – If a lifecycle constraint has already been applied to the provided method.