Răsfoiți Sursa

FSM: support "any" state transitions

Closes #344
fireclawthefox 5 ani în urmă
părinte
comite
f21ab509ec
1 a modificat fișierele cu 27 adăugiri și 0 ștergeri
  1. 27 0
      direct/src/fsm/FSM.py

+ 27 - 0
direct/src/fsm/FSM.py

@@ -153,6 +153,12 @@ class FSM(DirectObject):
     # must be approved by some filter function.
     # must be approved by some filter function.
     defaultTransitions = None
     defaultTransitions = None
 
 
+    # An enum class for special states like the DEFAULT or ANY state,
+    # that should be treatened by the FSM in a special way
+    class EnumStates():
+        ANY = 1
+        DEFAULT = 2
+
     def __init__(self, name):
     def __init__(self, name):
         self.fsmLock = RLock()
         self.fsmLock = RLock()
         self._name = name
         self._name = name
@@ -382,6 +388,27 @@ class FSM(DirectObject):
                 # accept it.
                 # accept it.
                 return (request,) + args
                 return (request,) + args
 
 
+            elif FSM.EnumStates.ANY in self.defaultTransitions.get(self.state, []):
+                # Whenever we have a '*' as our to transition, we allow
+                # to transit to any other state
+                return (request,) + args
+
+            elif request in self.defaultTransitions.get(FSM.EnumStates.ANY, []):
+                # If the requested state is in the default transitions
+                # from any state list, we also alow to transit to the
+                # new state
+                return (request,) + args
+
+            elif FSM.EnumStates.ANY in self.defaultTransitions.get(FSM.EnumStates.ANY, []):
+                # This is like we had set the defaultTransitions to None.
+                # Any state can transit to any other state
+                return (request,) + args
+
+            elif request in self.defaultTransitions.get(FSM.EnumStates.DEFAULT, []):
+                # This is the fallback state that we use whenever no
+                # other trnasition was possible
+                return (request,) + args
+
             # If self.defaultTransitions is not None, it is an error
             # If self.defaultTransitions is not None, it is an error
             # to request a direct state transition (capital letter
             # to request a direct state transition (capital letter
             # request) not listed in defaultTransitions and not
             # request) not listed in defaultTransitions and not