Giter VIP home page Giter VIP logo

transitions's People

Contributors

aforren1 avatar aleneum avatar andsor avatar ankostis avatar apiraino avatar artofhuman avatar busla avatar ipeluffo avatar janlo avatar jodal avatar jsenecal avatar maueki avatar mayowa avatar medecau avatar micahlyle avatar msumulong avatar ndvanforeest avatar nzjrs avatar ollamh avatar potens1 avatar spagh-eddie avatar svdgraaf avatar synss avatar termim avatar thedrow avatar themysteriousx avatar timgates42 avatar tyarkoni avatar v1k45 avatar wtgee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

transitions's Issues

Pickling without Dill for Python 3.4 and higher fails for LockedMachine

======================================================================
ERROR: test_pickle (tests.test_threading.TestTransitions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../transitions/tests/test_threading.py", line 67, in test_pickle
    dump = pickle.dumps(m)
AttributeError: Can't pickle local object 'LockedMachine.__getattribute__.<locals>.locked_method'

Multiple callbacks active at the same time

Currently most of my state logic is contained in on_enter callbacks. Since in some states I have to periodical checks, these callbacks run until the sate finishes.

Now I just noticed that it is possible that the on_enter CB of the following state is activated, while the old callback hasn't finished yet. Is this behaviour desired?

In my case I probably will have to start using mutexes ... 😖

Inheritance sample

In the inheritance sample:

class Matter(Machine):
    def say_hello(self): print "hello, new state!"
    def say_goodbye(self): print "goodbye, old state!"

    def __init__(self):
        states = ['solid', 'liquid', 'gas']
        Machine.__init__(self, states, initial='solid')
        self.add_transition('melt', 'solid', 'liquid')

constructor args are wrong (1st param is model name).
It could be like this:

Machine.__init__(self, states=states, initial='solid')

callback not working when using alternative initialisation pattern 1

I implemented a simple SM using the first pattern described in the alternative initialization patterns. Unfortunately, I cannot get the callbacks to work (e.g. on_enter_<some_state>). I prepared a working and a not working example.

alternative initialisation pattern 1 (not working)

test.py:

#!/usr/bin/env python

from transitions import Machine, State


def test_callback():
    print 'testing callbacks'

if __name__ == '__main__':

    states = ['StateA',
              State(name='StateB', on_enter=['test_callback'])]
    transitions = [{ 'trigger': 'triggerA', 'source': 'StateA', 'dest': 'StateB' }]
    sm = Machine(states=states, transitions=transitions, initial='StateA')

    print sm.state

    sm.triggerA()

    print sm.state

console output:

$ ./test.py 
StateA
Traceback (most recent call last):
  File "./test.py", line 18, in <module>
    sm.triggerA()
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 214, in trigger
    if t.execute(event):
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 134, in execute
    machine.get_state(self.dest).enter(event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 48, in enter
    getattr(event_data.model, oe), event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 447, in callback
    func(*event_data.args, **event_data.kwargs)
TypeError: 'NoneType' object is not callable

normal/default initialisation (working)

test2.py:

#!/usr/bin/env python

from transitions import Machine, State

class TestModel(object):
    def test_callback(self):
        print 'testing callbacks'

if __name__ == '__main__':

    test_model = TestModel()

    states = ['StateA',
              State(name='StateB', on_enter=['test_callback'])]
    transitions = [{ 'trigger': 'triggerA', 'source': 'StateA', 'dest': 'StateB' }]
    sm = Machine(test_model, states=states, transitions=transitions, initial='StateA')

    print test_model.state

    test_model.triggerA()

    print test_model.state

console output:

$ ./test2.py 
StateA
testing callbacks
StateB

Am I missing something or doing something wrong here?

PS: Alternative initialisation pattern 2 works as well.

RuntimeError: maximum recursion depth exceeded

Hi there!
My state machine loops, if there is no work to do. Now, if I leave it running for a while, then at some point, the whole thing crashes with the following error:

RuntimeError: maximum recursion depth exceeded

The long version:

  File "[...]/operator.py", line 196, in _idling_cb
    self.wake_up()
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 214, in trigger
    if t.execute(event):
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 134, in execute
    machine.get_state(self.dest).enter(event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 48, in enter
    getattr(event_data.model, oe), event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 447, in callback
    func(*event_data.args, **event_data.kwargs)
  File "[...]/operator.py", line 207, in _processing_ride_requests_cb
    self.no_ride_requests()
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 214, in trigger
    if t.execute(event):
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 134, in execute
    machine.get_state(self.dest).enter(event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 48, in enter
    getattr(event_data.model, oe), event_data)
  File "/usr/local/lib/python2.7/dist-packages/transitions/core.py", line 447, in callback
    func(*event_data.args, **event_data.kwargs)
  File "[...]/operator.py", line 194, in _idling_cb
    rospy.loginfo(self._node_name + " : new state '" + self.state + "'.")
  File "/usr/lib/python2.7/logging/__init__.py", line 1152, in info
    self._log(INFO, msg, args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
    self.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
    self.emit(record)
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/rosout.py", line 112, in emit
    record.filename, record.lineno, record.funcName)
  File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/impl/rosout.py", line 98, in _rosout
    logger.error("Unable to report rosout: %s\n%s", e, traceback.format_exc())
  File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
    self.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
    self.emit(record)
  File "/usr/lib/python2.7/logging/handlers.py", line 82, in emit
    self.handleError(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 802, in handleError
    None, sys.stderr)
  File "/usr/lib/python2.7/traceback.py", line 125, in print_exception
    print_tb(tb, limit, file)
  File "/usr/lib/python2.7/traceback.py", line 69, in print_tb
    line = linecache.getline(filename, lineno, f.f_globals)
  File "/usr/lib/python2.7/linecache.py", line 14, in getline
    lines = getlines(filename, module_globals)
  File "/usr/lib/python2.7/linecache.py", line 40, in getlines
    return updatecache(filename, module_globals)
RuntimeError: maximum recursion depth exceeded

Any idea what's going wrong or where I should start digging further?

Before and after state change

Hi everybody,

I noticed one thing, "condition" callback is triggered before "before" callback. I expected that "before" is called before "condition"? Does my logic make any sense?

Callables as callbacks

At the moment callbacks are set using strings which reference methods on the model object. This has the advantage that a set of transitions is model agnostic and could be used on multiple state machines, however in some cases it would be useful to pass a callable (like a function) as a callback. I have a use case at the moment where the model class has objects as properties, I want to call methods on them and currently I have to wrap those methods in methods defined on the model class itself. This means I have twice as many functions just to pass the callbacks around. I think it would be relatively straightforward to maintain the current usage and just do a type comparison when the callbacks need to be called, if they are strings look up the attribute on the model, if they are callables call them directly.

callback function if conditions is not satisfied

Hi,

Is there some way that i could define callback function if condition is not satisfied, after callbacks are only triggered if condition is satisfied. Something like after_fail would be cool.

For example, i have a state change from new to active:

        {'trigger': 'new_to_active', 'source': States.NEW, 'dest': States.ACTIVE,
         'before': 'write_insert_statement',
         'conditions': 'is_instruction_successfully_sent_to_controller',
         'after': ['save_current_state',
                   'set_related_instructions_to_active_state']},

In the part of the code that calls this trigger i have

                if not machine.new_to_provisioned():
                    machine.new_to_error()

I would like to put as much logic as i can in my state machine, so the handlers that call this events are totally dumb.

Best,
Domagoj

Optional debugging feature

This library is very useful, thanks for the great work!

I think that adding an optional logging feature, which would dump to stdout/err all transitions as they get triggered, along with the states which they originate from/go to, would be a big convenience.

PyPI Out of Date despite Version

Hi, the package you have on PyPI is listed as version 0.2.9, which is the latest, but hasn't been updated since 2015-11-10. Turns out those small changes with the inspect module on the add_states are important when it comes to multiple inheritance. Took me a while to figure out the code was just out of date. Thanks!

Graph Mixin Support

I've added in the graph as a mixin as per @aleneum's suggestion. Please take a look and see if it works for you.

Adds a graph attribute to the machine which will auto-track the changes and apply the style attribute from the MachineGraphSupport instance according to the styles_attribute defined in the base AGraph class. Currently has three styles: default, active, passive

Example notebook here

Feel free to iterate on the dev-graph branch.

Lock fails for conditional transition

I just stumbled upon a bug which occurs if a list of possible transitions is evaluated in core.Event. This problem occurs because locking core.Transition.execute is not sufficient for the test scenario below:

def test_conditional_access(self):
    self.stuff.heavy_checking = heavy_checking # checking takes 1s and returns False
    self.stuff.machine.add_transition('advance', 'A', 'B', conditions='heavy_checking')
    self.stuff.machine.add_transition('advance', 'A', 'C')
    t = Thread(target=self.stuff.advance)
    t.start()
    time.sleep(0.1)
    logger.info('Check if state transition done...')
    # Thread will release lock before Transition is finished
    self.assertTrue(self.stuff.machine.is_state('C')) 

Cause:

for t in self.transitions[state_name]:
    event.transition = t
    if t.execute(event): # lock is acquired and released here

Execution order will be TransitionAB.execute (Thread), Machine.is_state (Main), TransitionAC.execute (Thread). Update: Since it is a racing condition who gets the lock after TransitionAB it does not fail all the time.

Proposed solution: Lock core.Event.trigger rather than core.Transition.execute.
This requires minor changes to core to be able to subclass Event. Update: It does not. It's is three lines of redundant code though.

If you do not object and/or think this is a bad idea I will fix it in a branch fix-67-threading.

Logger does not display state information

When using the logging functionality with transitions, the state information of the state machine is not displayed on_enter and on_exit. For example, on entering a state stdout displays:

2015-09-01 13:13:41,528 - transitions.core: Exited state %s

This issue is probably due to an incorrect string formatting in calls to the logger, see below:

logger.info("Entered state %s", self.name)

# should be changed to:
logger.info("Entered state %s" % self.name)

Wildcard transitions are not applied on newer states

If one adds a state after a transition with a wildcard source then that transition will not apply on the new state.

In [6]: m=transitions.Machine()

In [7]: m.add_transition('one', '*', 'final')

In [8]: m.add_transition('two', 'initial', 'two')

In [9]: m.states
Out[9]: OrderedDict([('initial', <transitions.core.State object at 0x10c84dcd0>)])

In [10]: m.events
Out[10]:
{'one': <transitions.core.Event at 0x10c84dc90>,
 'to_initial': <transitions.core.Event at 0x10c84dad0>,
 'two': <transitions.core.Event at 0x10c84db10>}

In [11]: m.add_state('two')

In [12]: m.to_two()
[14/Jan/2016 16:39:32] INFO [transitions.core:131] Initiating transition from state initial to state two...
[14/Jan/2016 16:39:32] INFO [transitions.core:58] Exited state initial
[14/Jan/2016 16:39:32] INFO [transitions.core:51] Entered state two
Out[12]: True

In [13]: m.one()
---------------------------------------------------------------------------
MachineError                              Traceback (most recent call last)
<ipython-input-13-81b3745163c6> in <module>()
----> 1 m.one()

/Users/administrator/.virtualenvs/test/lib/python2.7/site-packages/transitions/core.pyc in trigger(self, *args, **kwargs)
    227                 logging.warning(msg)
    228             else:
--> 229                 raise MachineError(msg)
    230         event = EventData(self.machine.current_state, self, self.machine,
    231                           self.machine.model, args=args, kwargs=kwargs)

MachineError: "Can't trigger event one from state two!"

transitions prevents logging module from working

I admit I am new to logging, but even with this simple example, adding the transitions module prevents logging from working:

import transitions
import logging


if __name__ == '__main__':

    # set up a logger
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                        datefmt='%m-%d %H:%M',
                        filename='mylog.log',
                        filemode='w')

    logging.info("test")

if you comment out the import transitions the logging file is created and "test" shows up as expected. When the transitions module is imported, the log file is not created.

Am I making the mistake by incorrectly setting up logging? Or is transitions setting something up for logging that overrides my logging attempts?

States that triggered event

Hi,

I have a question. I have a code like this

        {'trigger': 'new_to_pending', 'source': States.NEW, 'dest': States.PENDING,
         'before': 'write_insert_statement',
         'after': ['save_current_state',
                   'set_related_active_instructions_to_pending_state']},

i write_insert_statement i would like to access source and dest states, if i send send_event=True to initializer i get event_data object but his object only contains initial state. Is it possible to retrieve both states source and dest?

Best,
Domagoj

How to describe one condition func call but different state change in one transition

hi,I have a question when using your awesome FSM, I want the code works like:

state = STATE_A

if condition_test():
    state = STATE_B
else:
    state = STATE_C

I write code like this, but it does work as expected. Becase the trigger: `mycall' will be called two times.

def condition_test():
    return random.randint(0,1) == 0 

transitions = [
    { 
        'trigger': 'mycall', 
        'source': 'STATE_A', 
        'dest': 'STATE_B' ,
        'conditions': 'condition_test'
    },
    { 
        'trigger': 'mycall', 
        'source': 'STATE_A', 
        'dest': 'STATE_C' ,
        'unless': 'condition_test'
    }
]

fsm = Machine(states=states, initial='STATE_A')

#### other code

So please help me, how to achieve one condition func call but different state change in one transition. Thanks verymuch!

Trouble getting started

from transitions import Machine

states = [
    "leaderboard",
    "chapter_teaser"
    "chapter_chosen",
    "adding_to_leaderboard"
]

transitions = [
    { 'trigger': 'advance', 'source': 'leaderboard', 'dest': 'chapter_teaser' },
    { 'trigger': 'advance', 'source': 'chapter_teaser', 'dest': 'chapter_chosen' },
    { 'trigger': 'advance', 'source': 'chapter_chosen', 'dest': 'adding_to_leaderboard' },
    { 'trigger': 'advance', 'source': 'adding_to_leaderboard', 'dest': 'leaderboard' }
]

class LeaderBoardDisplay(object):
    pass

leaderboard = LeaderBoardDisplay()

machine = Machine(leaderboard, states=states, transitions=transitions, initial='leaderboard')

I've been trying to use the state machine above, but can't work out the method to go to the next state.

Also, machine.anything_at_all returns None, instead of an exception.

More powerful transiton?

I found it may be difficult to pass data from trigger to transition's condition. For example, trigger=foo,when bar>5, StateA->StateB, I can't use Machine.foo(bar=6) to excute the transition.

By the way, I think a transition including Event Trigger, Condition, Condition Action, Transition Action is necessary for building compicated FSM.
That kind of transition is described in http://mathworks.com/help/stateflow/ug/transitions.html

pip install fails with transitions 0.2.6

Hi,
Here's the pip output when trying to install last transistions version (0.2.6):

Downloading/unpacking transitions from https://pypi.python.org/packages/source/t/transitions/transitions-0.2.6.tar.gz#md5=c53bdddd361634a91c8b229c936e7e90 (from -r requirements.txt (line 1))
  Downloading transitions-0.2.6.tar.gz
  Running setup.py (path:/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/setup.py) egg_info for package transitions
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/setup.py", line 4, in <module>
        from transitions.version import __version__
      File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/transitions/__init__.py", line 2, in <module>
        from .core import (State, Transition, Event, EventData, Machine, MachineError,
      File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/transitions/core.py", line 8, in     <module>
        from six import string_types
    ImportError: No module named 'six'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/setup.py", line 4, in <module>

    from transitions.version import __version__

  File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/transitions/__init__.py", line 2, in <module>

    from .core import (State, Transition, Event, EventData, Machine, MachineError,

  File "/Users/nico/Dev/beerfactory/python/env-mqtt/build/transitions/transitions/core.py", line 8, in <module>

    from six import string_types

ImportError: No module named 'six'

plotting graphs

I tried the following code to verify that I have a correct state machine :

#!/usr/bin/python
# -*- coding: utf-8 -*-

from fileStateMachine import *
states = ['1', '2', '3']
transitions = [
    {
        'trigger': 'advance',
        'source': '1',
        'dest': '2',
        'conditions': 'valid_data'
        },
    {
        'trigger': 'advance',
        'source': '1',
        'dest': '3',
        'unless': 'valid_data'
        }
]

class MF():
    def valid_data():
        return random.randrange(100) < 50

lump = MF( )

# the actual state machine a file posses.
machine = Machine( lump, states=states, initial='1', transitions=transitions, auto_transitions=False )

#print state machine
graph = None
try:
    graph = machine.get_graph()
except :
    printError('pygraphviz is missing.')
    sys.exit(101)
graph.draw('my_state_diagram.png', prog='dot')
print 'TODO'

which result in the following state diagram:

my_state_diagram

now a bunch of questions arise:

  1. Is there a concept to tell a/multiple State(s) that it is final or has a final property.
  2. Can we pass this state to pygraphviz.
  3. Why is the initial state ignored in the graph, or is it ignored in the machine as well?
  4. Would it be possible to add the condition to the graph, e.g. as an argument to advance -> "advance( valid_data==true )", since IMHO this small state machine is valid.

Many thanks in advance
stvo

State Callback defined in model class does not run

I've created a model class that inherits Machine which contains several on_enter_ callbacks for several states (methods defined/named on_enter_stateName(self)). None of the callbacks are executed when the state is entered. In core.py (lines 369-374) it looks like this method of adding callbacks only works if the model class is not "self".

Hierarchical Concurrent State Machine

Hi,

I have not worked with transitions so far but it looks like a really neat and pythonic approach to state machines. So in that matter its exactly what we need for a project :). My question is if transitions also supports nested and concurrent states. I know it is a bit contradicting to search something 'simple' but also requiring such complex features. But maybe I have just missed that in the docs or you may have some recommendations about how to realise hierarchical concurrent state machines with transitions.

Best regards,
Alex

conditions in ordered_transitions

Hi, it's me again 😄

As far as I understand there is no way at the moment to specify a condition for a single transition in a chain? If you don't mind about the feature I could provide a pull request for the feature.

Improve nested extension

Since my first implementation I got some feedback about the way I have done Hierarchical State Machines in transitions. The current implementation has some issues:

  • underscore separator does not cut it: We have been aware of the fact, that underscore is no perfect solution but it allowed nested states to reuse many of transitions' core functionality (auto transitions and callback addition). However, since underscore can also be used in state naming, there is no way to tell if stateC_alt_state1 is actually a (two times) nested state or not. Even worse could be a situation where the naming of states collide (stateC + alt_state1; stateC_alt + state_1). I haven't even tried what the ambiguity in auto transition would do but to_stateC_alt_state1 would probably do random things.
  • flatten strategy destroys hierarchical information: After flattening, retrieving hierarchical information, especially related to transitions, is bothersome. You cannot rely on the name. This means you have to retrieve the state in question and have a look at the parent (and its parent and so on) to actually know HOW nested the state was. I still think flattening is not a bad approach but especially in combination with the underscore separator it makes things unnecessarily complicated. The blueprint strategy could compensate this a bit and made copying possible but its one of the things that is hard to comprehend and maintain especially if the 'causer' is not available anymore.

I spent some time thinking about it and came to the conclusion that I have to change some things under the hood but also violate the first law of a library developer which is 'One shall not break the API' by switching from underscore to a dot separator. Why choosing the dot:

  • It's already implicitly forbidden in naming because it makes the convenience features of transitions useless: 'my.state' -> to_my.state (Nope) -> on_enter_my.state (Nope)
  • From python's point of view it's suitable to imply nesting: machine.to_stateA.alt.state1 resembles common object.child.property nesting. I have chosen to_stateC.alt rather than to.stateC.alt to stick to the way transition's core work. This is done with a callable FunctionWrapper which contains auto transitions to child states as properties (wrapping happens here).

Setback: The way of returning callbacks with partial is not that easy to extend to allow on_enter_C.alt('callback'). The best I came up with was on_enter_C().alt('callback') without overwriting __attr__ in nested (which I try to avoid). So I have chosen another way which is on_enter/on_exit('C.alt','callback'). Implicit calling of model functions does not work in this case though.

However, the rework brings new features which hopefully make up for it:

  • hierarchical order is kept: No need for blueprints anymore
  • Machines can be embedded and not just copied into another Machine: {name:'stateC', children:another_machine} no longer copies the structure but references the states directly. This allows to keep the states of a machine and dynamical add them to another super state machine. This becomes handy if you subclass States to keep certain informations independently of their current status or if creating a state requires much time and/or resources.
  • Embedding machines allows siblings: {name:'stateC', children:['1',another_machine]}is no longer prohibited. If another_machine also contains a state named 1 a ValueError is raised.
  • Hierarchical delegation of transitions: Previously, parent state transitions had to be copied to its children. Having a transition from stateC means 3 transitions for 3 children and 12 if these children also contain 3 children. Additionally, it makes the trigger unusable for children or leads to unpredictable behaviour if substates 'accidentally' use the same trigger. In this case addition order decides which transition is executed. Not anymore! After the rework trigger events are delegated from children to parents. This means children can intentionally overwrite their parents behaviour.

Ah yeah, I also merged AAGraph and AGraph since AAGraph was already the default class chosen and I haven't found any occasion where AGraph was explicitly used. Actually this is also the reason why #85 fails the coverage test I guess. AGraph._add_nodes and AGraph._add_edges aren't used during testing.

There is still some work to be done (tweaking, documentation and merging the latest changes for instance), that's why I haven't opened a pull request yet. But it has progressed enough to gather some feedback.

So what are your thoughts about that? Is to_stateC.alt.stateD() and on_enter('stateC.alt.stateC','callback') sufficient? Is it okay to break the API and use dots from now on?

trafficlight

Hey everyone,

I really like the transitions package. Before. I always used the Qt StateMachine class and PyQt.
If only a Statemachine is required, transitions is leaner and not such a large dependency.
I have two questions.
I have created a traffic light, which can be turned on and run in the background. To do so the machine is dispatched to a separate thread which keeps calling next. The interpreter is constantly updated on the state of the traffic light via print outs and a user can stop or pause it.
Update: Threads were replaced with coroutines, the interpreter was replaced with a web front end, see example at the end.
The machine serves as a simple illustrations for more complicated machines, handled in the background, e.g. 3D printers.
The two questions are as follows:

  1. Why is False or True returned if the traffic light is paused!? I also get this if I transition a machine from the documentation's code samples.
    Update: This is to notify transition is successful, documentation has been updated.
  2. Is this is the right way to do it, could you give a more elaborate example in the chapter Threadsafe(-ish) State Machine, any other comments!?
    Update: No threads are expensive, use asyncio.
    The code works in Python 3, not 2.7.
'''
Created on Jan 8, 2016
'''

from transitions import LockedHierarchicalMachine as Machine
from time import sleep, time
from threading import Thread, Event

class ThreadClass(Thread):
        def __init__(self,trafficlight):
            super().__init__()
            self.trafficlight=trafficlight
            self.stopevent=Event()  
            self.pauseevent=Event()
        def stop(self):  
            self.stopevent.set()
            self.pauseevent.set()    
        def pause(self):
            self.pauseevent.set()            
        def run(self):
            while(True):
                sleep(self.trafficlight.printstatetimeout()) 
                self.trafficlight.lock.acquire() 
                if self.stopevent.isSet():
                    print("Stop pressed, traffic light stops")
                    self.trafficlight.lock.release()
                    break
                else:
                    if self.pauseevent.isSet() and not self.stopevent.isSet():
                        self.trafficlight.lock.release()
                        print("Pause pressed")
                        self.pauseevent.clear()
                        self.pauseevent.wait()
                        print("Resume or stop pressed")
                        self.pauseevent.clear()
                        self.trafficlight.lock.acquire()
                    else:
                        self.trafficlight.next()
                self.trafficlight.lock.release()          

class Trafficlight(Machine):
    def __init__(self):
        self.timeoutdic = {'run_green': 3, 'run_red': 5, 'run_yellow': 2}
        states=['paused',{'name':'run', 'children':['green','yellow','red']}]
        transitions = [
                       { 'trigger': 'next', 'source': 'run_green', 'dest': 'run_yellow'},
                       { 'trigger': 'next', 'source': 'run_yellow', 'dest': 'run_red'},
                       { 'trigger': 'next', 'source': 'run_red', 'dest': 'run_green'},
                       {'trigger':'pause','source': 'run', 'dest':'paused', 'before':'storestate'},
                       {'trigger':'pause','source':'paused','dest':'run', 'after':'restorestate'},
                       {'trigger':'reset', 'source':'*','dest':'run_green'}
                       ]
        Machine.__init__(self, states=states,transitions=transitions, initial='run_green')
        self.reinstall()
    def reinstall(self):
        self.thread=ThreadClass(self) 
        self.oldtime=None
        self.elapsedtime=None
        self.oldstate=None
    def printstatetimeout(self):
        self.oldtime=time()
        self.oldstate=self.state
        if self.elapsedtime:
            naptime=self.timeoutdic[self.state]-self.elapsedtime
            self.elapsedtime=None
        else:
            naptime=self.timeoutdic[self.state]
        print('Light is '+self.state+' and on for '+str(naptime)+'s')
        return naptime
    def start(self):
        self.thread.start()
    def stop(self):
        print('Traffic light is turned off')
        print('Waiting '+str(self.timeoutdic[self.state])+'s for all processes to finish')
        self.thread.stop()
        sleep(self.timeoutdic[self.state])
        self.reset()
        self.reinstall()
    def storestate(self):
        print('Light is turned off temporarily')
        self.elapsedtime=time()-self.oldtime
        self.thread.pause()
    def restorestate(self):
        self.set_state(self.oldstate)
        self.thread.pause()  

trafficlight = Trafficlight()
trafficlight.start()  
sleep(10)
trafficlight.stop()   
trafficlight.start()  
sleep(11)
trafficlight.pause() 
sleep(5)   # TODO: False returned why!?
# Resumes traffic light, interruption is accounted for see; storestate, restorestate
trafficlight.pause()  

Logger as parameter to use with multiple machines

Use of standard logging is great. However there are use cases where more than one FSM can be instanciated, in which case it is difficult to match log lines with code logic.

I would like to propose an optional logger parameter when instanciating the machine (with default to current global logger). This way user has possibility to pass a logger instance embedding some context and configuration.

Generic condition method

Very good work! I'm curious if I could have a generic condition method where I check the transition source and dest field and return True/False accordingly. I can't seem to find transition's dest from generic condition method. Or may be there's a better way?

First argument passed to the trigger will be vanished

>>from transitions import Machine, State
>>class Model(object):
>>    def on_activate(self, value):
>>        print value
>>model = Model()
>>states = [
>>    'init',
>>    State('active', on_enter='on_activate'),
>>]
>>transitions = [{'trigger': 'activate', 'source': 'init', 'dest': 'active'}]
>>machine = Machine(model=model, states=states, transitions=transitions, initial='init')
>>model.activate('simple argument, not a kwarg')
Traceback (most recent call last):
...
TypeError: on_activate() takes exactly 2 arguments (1 given)

Main scheduler

Thanks for the nice design and implementation.

I have a question for the "main" loop. ( I do not know if the "conditional transition" is the answer to my question or not. So please advise )
Let's use your NarcolepticSuperhero example walk-through. You used Python interactive prompt. That's the main loop. It sits there and waits for an event to occur (such as .wake_up()). When that event occurs, the main loop notifies the state machine of that event (batman.wake_up()). That notification makes the state machine transitions to the next state, AND THEN it comes back to the main loop (Python interactive prompt), and waits again for the next event to occur.

I am seeking a way to implement the Main loop functionality which is not Python interactive prompt. The same functionality is needed: Wait for an event, pass to the state machine so that it can transition to a next state, and completing before_transition(), on_exit(), on_enter(), after_transition(), and THEN comes back to a main loop.

In the source of "transitions", I found no while loop.

If this questions does not sound making sense, probably I have mistaken the design of your implementation.

Thanks,
pb.

How to achieve other conditions except default `conditions' or `unless' in one condition

Hi, dear tyarkoni

I have another question whiling using your FSM, I want to achieve like this: start a task to wget a large file, and then to check is the wget task finished or not once in a while, because result may be three status: wget_ing, wget_failure, wget_success. Pseudocode is like bellow:

def start_wget():

    return 0

def check_wget():

    return random('WGET_RUNNING', 'WGET_SUCCESS', 'WGET_FAILURE')

# WGET_EXEC
# WGET_RUNNING
# WGET_SUCCESS
# WGET_FAILURE

state = 'WGET_EXEC'

# here we do not care
start_wget()

result = check_wget()

if result == 1:
    state = 'WGET_RUNNING'
elif result == -1:
    state = 'WGET_FAILURE'
elif result == 0:
    state = 'WGET_SUCCESS'

And I do not know how to achieve in your FSM, please help me!

transitions = [
    { 
        'trigger': 'mycall', 
        'source': 'WGET_EXEC', 
        'dest': 'WGET_RUNNING' ,
        'conditions': 'start_wget'
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_SUCCESS' ,
        'conditions': 'check_wget' 
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_RUNNING' ,
    },
    { 
        'trigger': 'mycall', 
        'source': 'WGET_RUNNING', 
        'dest': 'WGET_FAILURE' ,
    },
   ... 
]

fsm = Machine(states=states, initial='WGET_EXEC')

Looping / "Running" a Machine

I might be missing a critical concept; I can't find any examples or snippets that show how to do this:

How would I setup a machine to move to the next state until an end state is reached? All of the examples show manual steps from one state to the next. I'd like to "start" the machine and have it automatically transition from one state to the next using conditional transitions until it reaches the "end" state.

Any suggestions are much appreciated. I'm really interested in using this library; it seems to be very clean and lightweight compared to other python implementations of FSM.

Dynamic on_enter/exit_* methods

The documentation says:

# Callbacks can also be added after initialization using 
# the dynamically added on_enter_ and on_exist_ methods.

I haven't looked into this in detail yet but a quick scan doesn't show a route through the code that creates on_enter/on_exit methods for each state after init has run.

Has this ever worked?

>>> from transitions import State, Machine
>>>
>>> class Matter(object):
...     def say_hello(self): print("hello, new state!")
...     def say_goodbye(self): print("goodbye, old state!")
...
>>> lump = Matter()
>>>
>>> states = [
...     State(name='solid', on_exit=['say_goodbye']),
...     'liquid',
...     { 'name': 'gas' }
...     ]
>>>
>>> machine = Machine(lump, states=states)
>>> machine.add_transition('sublimate', 'solid', 'gas')
>>>
>>> lump.on_exit_solid('say_goodbye')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Matter' object has no attribute 'on_exit_solid'
>>>
>>> machine.set_state('solid')
>>> lump.sublimate()

Error on passing data to functions in 'before', 'after' and 'conditions'

The first example in "Passing Data" section in README gives this error:

Traceback (most recent call last):
  File "/Users/harshit/PycharmProjects/DialogFlow/test.py", line 17, in <module>
    lump.melt(temp=45, pressure=1853.68)  # keyword args
  File "/usr/local/lib/python3.5/site-packages/transitions/core.py", line 227, in trigger
    if t.execute(event):
  File "/usr/local/lib/python3.5/site-packages/transitions/core.py", line 145, in execute
    machine.callback(getattr(event_data.model, func), event_data)
  File "/usr/local/lib/python3.5/site-packages/transitions/core.py", line 460, in callback
    func(*event_data.args, **event_data.kwargs)
TypeError: print_pressure() got an unexpected keyword argument 'temp'

on adding transitions the following way
machine.add_transition('melt', 'solid', 'liquid', before='set_environment', after='print_pressure')

Define transitions on states rather than machine?

Would it be possible to change the way transitions are added (or add an additional method for adding them) so that they are added to the source state rather than to the machine? It seems to me that something like:
machine['solid'].add_transition('sublimate', 'gas')
is more immediately comprehensible than:
machine.add_transition('sublimate', 'solid', 'gas')
as it allows the transitions to be conceptually grouped around the source state. I suspect that the wildcard might cause some implementation difficulties, but are there any other issues with this kind of syntax?

state diagrams

At some point we should add the ability to generate state diagrams via GraphViz or NetworkX.

Inheritance sample seems to be incorrect

This example does not seem work as intended:

>>> from transitions import Machine
>>> class Matter(Machine):
...     def say_hello(self): print ("hello, new state!")
...     def say_goodbye(self): print ("goodbye, old state!")
...     def __init__(self):
...         states = ['solid', 'liquid', 'gas']
...         Machine.__init__(self, states, initial='solid')
...         self.add_transition('melt', 'solid', 'liquid')
...
>>> lump = Matter()
['solid', 'liquid', 'gas']
None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in __init__
  File "/Users/adamb/Developer/transitions-bug/env-osx/lib/python3.5/site-packages/transitions/core.py", line 294, in __init__
    self.set_state(self._initial)
  File "/Users/adamb/Developer/transitions-bug/env-osx/lib/python3.5/site-packages/transitions/core.py", line 325, in set_state
    state = self.get_state(state)
  File "/Users/adamb/Developer/transitions-bug/env-osx/lib/python3.5/site-packages/transitions/core.py", line 319, in get_state
    raise ValueError("State '%s' is not a registered state." % state)
ValueError: State 'solid' is not a registered state.

I was going to send a pull, but I'm not sure of the correct behaviour.

The issue is that when invoking Machine.init as a class method, the first parameter from the invocation (self) gets put into the first parameter of Machine.init() (self).

This shifts all the parameters left, so states gets put into model etc.

There are three ways to fix this - either add a second self so that Machine.model is set to self:

...         Machine.__init__(self, self, states, initial='solid')

or, use an explicit keyword for states so that Machine.model is set to None (this version is used in your test suite):

...         Machine.__init__(self, states=states, initial='solid')

This also fixes the example:

...         super(Matter, self).__init__(self, states, initial='solid')

Which one is "correct"? When you are using inheritance, is the model intended to be None, or an instance of itself?

Add info to setup.py info

Adding info in setup.py/pypi.python.org (supported python versions)´whould help to spread the package.

Triggering event inside callback

It looks like we can trigger an event from inside a callback (e.g. on_enter_mystate). But this is probably a source of confusion: the callback code start executing while the FSM is in one state, call the event (FSM might change state and trigger other callbacks), and first callback finish executing in a different FSM state (we can see strange nested on_entering, on_exiting log).

I believe a less surprising behaviour would be to queue event execution while a transition is already executing. Would that make sense?

Allow machine pickle/unpickle

Currently, pickling or unpickling objects with Machine instance fails with TypeError:
example from quick start example:

import pickle
print(pickle.dumps(batman))
TypeError: 'NoneType' object is not callable

This could be useful to be able to save and restore a machine state. I can work on that if you agree.

a generic callback

Hi!

Is there a way to specify a generic callback which will be called whenever the machine changes its state?

on_enter when initialzing machine

Given:

>>> from transitions import Machine
>>> class Practise(object):
...     def on_enter_A(self): print("We've just entered state A!")
... 
>>> test = Practise()

Is there any reason why:

>>> machine = Machine(model=test, states=['A', 'B', 'C'], initial='A')

Doesn't immediately return:

"We've just entered state A!"

As it does when you enter state A subsequent times:

>>> machine.add_transition('repeat', source='A', dest='A')
>>> test.repeat()
We've just entered state A!
True

Thanks.

True or False returned upon transition

As requested in #63, why is True returned if this code is executed!? Is this is a bug!?

from transitions import Machine
class Matter(object):
    pass
lump=Matter()
states=['solid', 'liquid']
machine = Machine(model=lump, states=states, initial='solid')
machine.add_transition('melt', source='solid', dest='liquid')
print(lump.melt())  # lump.melt() returns True

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.