Welcome to StateMachine’s documentation!

Getting Started

The State Machine is used to implement a workflow. Through a transition you change from one state to another. The current state determines the behavior.

Context

The Context serves as an interface between the state machine and the client . It delegates a request from the client to the current state. Depending on the current state, the request results in a different behavior.

The following example shows how to create a context and load a state by its name:

from state_machine.context import Context

context = Context()
context.load_state("RegisteredState")
context.change_state()

State

Each state is modeled as a JSON document with two set of strategies (create_study and change_state) that dictates a set of behaviors.

The existing states need to be passed to the context. Here is an example:

{
    "name" : "RegisteredState",
    "strategies_create_study" : [
        {
            "name" : "expression",
            "value" : "len(kwargs.get('datasets', [])) > 0",
            "state_if_true" : "DatasetState"
        }
    ],
    "strategies_change_state" : [
        {
            "name" : "expression",
            "value" : "len(kwargs.get('datasets', [])) > 0",
            "state_if_true" : "DatasetState"
        }
    ]
}

study_state_machine

study_state_machine package

Subpackages

Submodules

study_state_machine.context module

Implementation of a finite state machine for studies

class study_state_machine.context.Context(available_states, initial_state=None)

Bases: object

Only if an initial state is passed to the constructor, the context of the current state is set. Otherwise, call transition_to() or load_state() with a name of a State, respectively

add_sample(*args, **kwargs)
property available_states
change_state(*args, **kwargs)
create_study(*args, **kwargs)
property current_state
get_state_dict(state_name)
load_state(state_name)

Load state with given name

Parameters

state_name – Name of the state

Raises

StateNotFoundException – If the state is not found

parse_strategies(strategies, *args, **kwargs)

Parse strategies list and return a state to be transitioned to

transition_to(state_name)

Set state as the new current state and set its context

Parameters

state_name – new current state

study_state_machine.errors module

Collection of state machine related exceptions.

All exceptions inherit from StateMachineException

exception study_state_machine.errors.BehaviorNotAllowedException

Bases: study_state_machine.errors.StateMachineException

Exception if a state does not support a behavior

exception study_state_machine.errors.StateMachineException

Bases: Exception

Generic state machine error

exception study_state_machine.errors.StateNotFoundException

Bases: study_state_machine.errors.StateMachineException

Exception if state is not found by its name

study_state_machine.interfaces module

class study_state_machine.interfaces.IState(context=None)

Bases: abc.ABC

Base class for all states.

The state has a reference to the context in order to change into the next state.

property context
class study_state_machine.interfaces.IStudyState(context=None)

Bases: study_state_machine.interfaces.IState

Base class for all study states

add_sample(*args, **kwargs)
change_state(*args, **kwargs)
create_study(*args, **kwargs)

Module contents

Indices and tables