How to use the transitions.extensions.GraphMachine function in transitions

To help you get started, we’ve selected a few transitions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jackdbd / design-patterns / state.py View on Github external
def __init__(self, name):
        self.name = name

        # initialize the state machine
        self.machine = GraphMachine(model=self, states=self.states, initial="sleeping")

        # add transitions
        self.machine.add_transition(
            trigger="wake_up", source="sleeping", dest="waiting"
        )
        self.machine.add_transition(
            trigger="start", source="waiting", dest="running", before="display_message"
        )
        self.machine.add_transition(
            trigger="interrupt", source="*", dest="terminated", after="display_warning"
        )
        self.machine.add_transition(
            trigger="random_trigger",
            source="*",
            dest="terminated",
            conditions=["is_valid"],
github mjedmonds / OpenLock / gym_lock / state_machine / multi_lock.py View on Github external
def __init__(self):
        self.states = self._init_states()

        self.machine = Machine(model=self,
                               states=self.states,
                               initial='l0+l1+l2+o-',
                               ignore_invalid_triggers=True,
                               auto_transitions=False)

        # add unlock/lock transition for every lock
        for lock in MultiDoorLockFSM.locks:
            if lock == 'l2':
                locked = [s for s in self.states if lock + '+' in s and 'l0-' in s and 'l1-' in s]
                unlocked = [s for s in self.states if lock + '-' in s and 'l0-' in s and 'l1-' in s]
            else:
                locked = [s for s in self.states if lock + '+' in s]
                unlocked = [s for s in self.states if lock + '-' in s]
            for locked_state, unlocked_state in zip(locked, unlocked):
                    self.machine.add_transition('lock_{}'.format(lock), unlocked_state, locked_state)
                    self.machine.add_transition('unlock_{}'.format(lock), locked_state, unlocked_state)
github impudding / Facebook-chatbot / fsm.py View on Github external
from transitions.extensions import GraphMachine

from utils import send_text_message
from utils import send_image_url


class TocMachine(GraphMachine):
    def __init__(self, **machine_configs):
        self.machine = GraphMachine(
            model=self,
            **machine_configs
        )

    def is_going_to_state1(self, event):
        if event.get("message") and event['message'].get("text"):
            text = event['message']['text']
            return text.lower() == 'romeo and juliet'
        return False

    def is_going_to_state2(self, event):
        if event.get("message") and event['message'].get("text"):
            text = event['message']['text']
            return text.lower() == 'king lear'
github mjedmonds / OpenLock / openlock / scenarios / ordered_lock.py View on Github external
def __init__(self):
        self.states = self._init_states()
        self.initial = "l1+l2+l3+o-"
        # captures lock/door status
        self.machine = Machine(model=self, states=self.states, initial=self.initial)

        # add unlock/lock transition for every lock
        for lock in MultiDoorLockFSM.locks:
            locked = [s for s in self.states if lock + "+" in s and not "_ord" in s]
            unlocked = [s for s in self.states if lock + "-" in s and not "_ord" in s]
            for locked_state, unlocked_state in zip(locked, unlocked):

                # get onto ordered path
                if locked_state == self.initial and lock == "l1":
                    # all locked, door closed, opening l1 gets us onto ordered path
                    unlocked_state = MultiDoorLockFSM.ordered_path[0]

                self.machine.add_transition(
                    "lock_{}".format(lock), unlocked_state, locked_state
                )
                self.machine.add_transition(
github atlassistant / pytlas / pytlas / conversing / agent.py View on Github external
intents = [i for i in self._interpreter.intents if not is_builtin(i)]
        states = [STATE_ASLEEP, STATE_ASK,
                  STATE_FALLBACK, STATE_CANCEL] + intents

        self._available_scopes = build_scopes(
            intents, STATE_CANCEL in self._interpreter.intents)

        MachineKlass = Machine
        kwargs = {}  # Additional arguments to give to the machine

        if self._transitions_graph_path:  # pragma: no cover
            try:
                import pygraphviz # pylint: disable=import-outside-toplevel,unused-import
                from transitions.extensions import GraphMachine

                MachineKlass = GraphMachine
                kwargs['show_conditions'] = True
            except (ImportError, ModuleNotFoundError):
                self._logger.error(
                    'Could not use a GraphMachine, is "pygraphviz" installed?')

        self._machine = MachineKlass(
            model=self,
            states=states,
            send_event=True,
            before_state_change=self._log_transition,
            initial=STATE_ASLEEP,
            **kwargs)

        self._logger.info('Instantiated agent with "%d" states: %s',
                          len(states), ', '.join('"%s"' % s for s in states))
github impudding / Facebook-chatbot / fsm.py View on Github external
def __init__(self, **machine_configs):
        self.machine = GraphMachine(
            model=self,
            **machine_configs
        )