fysom icon indicating copy to clipboard operation
fysom copied to clipboard

FysomGlobal does not honor '=' destination

Open moznuy opened this issue 6 years ago • 1 comments

Small snippet to illustrate:

from fysom import FysomGlobal, FysomGlobalMixin

class Model(FysomGlobalMixin, object):
    GSM = FysomGlobal(
        events=[('test', ('green', 'not_important'), '=')],
        initial='green',
        state_field='state'
    )

    def __init__(self):
        self.state = None
        super(Model, self).__init__()


obj = Model()
print(obj.current)  # 'green'
obj.test()
print(obj.current)  # '='

I tried to add 2 events with the same name:

events=[('test', 'green', 'green')],
events=[('test', 'not_important', 'not_important'],

but the latter overrides the former, which do not correspond with Fysom behaviour:

from fysom import Fysom
fsm = Fysom(
    events=[
        ('change', 'green', 'red'),
        ('test', 'green', 'green'),
        ('test', 'red', 'red')],
    initial='green')

print(fsm.current) # green
fsm.test()
print(fsm.current) # green
fsm.change()
print(fsm.current) # red
fsm.test()
print(fsm.current) # red

and

from fysom import FysomGlobal, FysomGlobalMixin, Fysom

class Model(FysomGlobalMixin, object):
    GSM = FysomGlobal(
        events=[
            ('change', 'green', 'red'),
            ('test', 'green', 'green'),
            ('test', 'red', 'red')],
        initial='green',
        state_field='state'
    )

    def __init__(self):
        self.state = None
        super(Model, self).__init__()


obj = Model()
print(obj.current)  # 'green'
obj.test()          # 'fysom.FysomError: event test inappropriate in current state green'
print(obj.current)
obj.change()
print(obj.current)
obj.test()
print(obj.current)

So now I forced to create to different events and fire them based on state which defeats the point of using FSM.

moznuy avatar Nov 14 '19 12:11 moznuy

The issue isn't just related to the "=' construct. The _map structure for FysomGlobal does not permit the case in Fysom where the same event can trigger multiple destination, source pairs. moznuy's example with two events with the same name is exactly the problem I have. As he says, this is very different behavior from Fysom.

frank-pet avatar Dec 06 '23 13:12 frank-pet