Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statemachines point to newest instance for their state variables #334

Closed
KellyP5 opened this issue Jan 26, 2023 · 3 comments · Fixed by #335
Closed

Statemachines point to newest instance for their state variables #334

KellyP5 opened this issue Jan 26, 2023 · 3 comments · Fixed by #335

Comments

@KellyP5
Copy link

KellyP5 commented Jan 26, 2023

I am not sure this was solved, if I create multiple instances of a state machine -- the newest statemachines class variables will serve as the single point of truth for all state machines.

from statemachine import State
from statemachine import StateMachine
class TestStateMachine(StateMachine):
    # States
    st_1 = State("One", initial=True)
    st_2 = State("Two")
    st_3 = State("Three")
    # Transitions
    tr_change = (
        st_1.to(st_2, cond="two")
        | st_2.to(st_3, cond="three") 
        | st_3.to(st_1, cond="one")
    )
    def __init__(self, name="unnamed"):
        # variables
        self.sm_name = name
        self.one = False
        self.two = True
        self.three = False
        super().__init__()
        
s1 = TestStateMachine()
s2 = TestStateMachine()
s1.current_state
State('One', id='st_1', value='st_1', initial=True, final=False)
s2.current_state
State('One', id='st_1', value='st_1', initial=True, final=False)
s1.tr_change()
s1.current_state
State('Two', id='st_2', value='st_2', initial=False, final=False)
s2.current_state
State('One', id='st_1', value='st_1', initial=True, final=False)
s2.two = False
s2
TestStateMachine(model=Model(state=st_1), state_field='state', current_state='st_1')
s2.tr_change()

statemachine.exceptions.TransitionNotAllowed: Can't tr_change when in One.

s1.three = True
s1.three
True
s2.three
False
s1.tr_change() # this shouldn't throw an error because s1.three is now true

statemachine.exceptions.TransitionNotAllowed: Can't tr_change when in Two.

s2.three = True # When I change the variable on s2, it then allows me to transition the state of s1
s1.tr_change()
s1.current_state 
State('Two', id='st_2', value='st_2', initial=False, final=False)

Originally posted by @KellyP5 in #330 (comment)

@KellyP5
Copy link
Author

KellyP5 commented Jan 26, 2023

Simpler and more straightforward version

s1 = TestStateMachine("s1")
s2 = TestStateMachine("s2")
s1.current_state
State('One', id='st_1', value='st_1', initial=True, final=False)
s2.current_state
State('One', id='st_1', value='st_1', initial=True, final=False)
s1.one
False
s1.two
True
s1.three
False
s2.one
False
s2.two
True
s2.three
False
s2.two = False
s1.tr_change()

Traceback (most recent call last):
statemachine.exceptions.TransitionNotAllowed: Can't tr_change when in One.

s2.two = True
s1.tr_change() # No error

@fgmacedo
Copy link
Owner

Hi @KellyP5 , thanks for reporting this. I was now able to reproduce the issue. I'll try to find where the bug is and release a bug fix.

@fgmacedo
Copy link
Owner

fgmacedo commented Jan 27, 2023

I've released a 1.0.3 version today with this bugfix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants