hsm.core.State

class State(name)

Bases: object

Represents a state in a state machine.

enter and exit handlers are called whenever a state is entered or exited respectively. These action names are reserved only for this purpose.

It is encouraged to extend this class to encapsulate a state behavior, similarly to the State Pattern.

Parameters:

name (str) – Human readable state name

Example Usage:

# Extending State to encapsulate state-related behavior.
class Running(State):
    def on_enter(self, event):
        print('Running state entered')

    def on_jump(self, event):
        print('Jumping')
# An alternative option to add handlers is by calling :func:`add_handler`.
# A handler may be any function as long as it takes `state` and `event` args.
def my_handler(state, event):
    print('my handler')

running = State('running')
running.add_handler('event', my_handler)

Methods

add_handler

Add a new event callback.

add_transition

Add a transition from self to target_state

is_active

is_substate

Check whether the state is a substate of self.

ros_subscribe

Attributes

root

Get the root state in a states hierarchy.

add_handler(events, func, prepend=False)

Add a new event callback.

Parameters:
  • trigger (str) – name of triggering event

  • func (callable) – callback function

add_transition(events, target_state, condition=None, action=None, before=None, after=None)

Add a transition from self to target_state

All callbacks take two arguments - state and event. See parameters description for details.

It is possible to create conditional if/elif/else-like logic for transitions. To do so, add many same transition rules with different condition callbacks. First met condition will trigger a transition, if no condition is met, no transition is performed.

Parameters:
  • target_state (State, None) – Target state. If None, then it’s an internal transition

  • events (Iterable of Hashable) – List of events that trigger the transition

  • condition (Callable) –

    Condition callback - if returns True transition may be initiated.

    condition callback takes two arguments:

    • state: Leaf state before transition

    • event: Event that triggered the transition

  • action (Callable) –

    Action callback that is called during the transition after all states have been left but before the new one is entered.

    action callback takes two arguments:

    • state: Leaf state before transition

    • event: Event that triggered the transition

  • before (Callable) –

    Action callback that is called right before the transition.

    before callback takes two arguments:

    • state: Leaf state before transition

    • event: Event that triggered the transition

  • after (Callable) –

    Action callback that is called just after the transition

    after callback takes two arguments:

    • state: Leaf state after transition

    • event: Event that triggered the transition

is_substate(state)

Check whether the state is a substate of self.

Also self is considered a substate of self.

Parameters:

state (State) – State to verify

Returns:

True if state is a substate of self, False otherwise

Return type:

bool

property root

Get the root state in a states hierarchy.

Returns:

Root state in the states hierarchy

Return type:

State