0

Below is my code a.py:

from django.db import models
from django_fsm import transition, FSMIntegerField
from django_fsm import FSMField, transition
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
import django
django.setup()

from django.core.management import call_command
class Order(models.Model):
     STATUS_STARTED = 0
     STATUS_SLOW =1
     STATUS_FAST=2
     STATUS_JUMP=3
     STATUS_CHOICES = (
      (STATUS_STARTED, 'STARTED'),
      (STATUS_SLOW,'SLOW')
      (STATUS_FAST,'FAST')
      (STATUS_JUMP,'JUMP')
       )
product = models.CharField(max_length=200)
status = FSMIntegerField(choices=STATUS_CHOICES, default=STATUS_STARTED, protected=True)

A person STARTED from a point & he is either FAST or SLOW.

@transition(field=status, source=[STATUS_STARTED],  target=STATUS_FAST)
def fast(self):
    print("person run fast")

@transition(field=status, source=[STATUS_STARTED],  target=STATUS_SLOW)
def slow(self):
    print("person run slow ")

Person in FAST state can switch to SLOW state & can JUMP:

@transition(field=status, source=[STATUS_FAST],  target=STATUS_SLOW)
def switch(self)
    print("state switched")

@transition(field=status, source=[STATUS_FAST, STATUS_SLOW],  target=STATUS_JUMP)
def jump(self)
    print("person jumpped")

but I have condition that person who STARTED from the point with SLOW state cannot JUMP.

from FAST state, he can SLOW & JUMP, but not directly from STARTED--> SLOW-->JUMP is possible.

But when i run above code:

     >>person = Order() #input
     >>person.slow() # Input
     >> person run slow # output
     >>person.jump() # Input
     >>person jumpped # output but expected transition error. 

And i found that its because, FSM will maintain the last state alone & as per jump(), from SLOW to JUMP get executed..

Any possibility to define a variable (lets SAY 'X') after @transition function for FAST state & set as X=Y, to indicate person have attained this state. so that when he enters JUMP state, the variable can be verified whether X==Y or not ? if so, how to define it.

1 Answers1

1

finite state machine does not have reccolection of older states ( pushdown automaton employs stack instead and can have that kind of memory)

You still can solve same problem with FSM but with a bit more states:

You will have to have for example a states:

  • for one that started fast and slowed down to go to STARTED_FAST_SLOWED_DOWN
  • oposite one to go to STARTED_SLOW_FASTER_NOW

and you could then have actions that move from following states if possible

iklinac
  • 14,944
  • 4
  • 28
  • 30