Commits

dr0id  committed 2bf00f1

code cosmetics

  • Participants
  • Parent commits aa7fb67

Comments (0)

Files changed (2)

File trunk/symplehfsm/examples/testablestatemachines/testablestatemachine.py

 from symplehfsm import SympleDictHFSM
 from symplehfsm import BaseHFSMTests
 
-#-------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
 
-#-------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 class Events(object):
     dark = 0
     light = 1
     no_movement = 3
     timeout = 4
 
-#-------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
 class Actions(object):
     def lamp_off(self):
     def start_timer(self):
         print("starting timer")
 
-#-------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
 
-structure = Structure()
+def main():
+    structure = Structure()
 
-#                         state,    parent, initial,            entry,        exit
-structure.add_state("parent",        None, False)
-structure.add_state(  "day",     "parent",  True)
-structure.add_state(  "night",   "parent", False)
-structure.add_state(    "off",    "night",  True)
-structure.add_state(    "moving", "night", False)
-structure.add_state(    "timing", "night", False)
+    #                         state,    parent, initial,            entry,        exit
+    structure.add_state("parent",        None, False)
+    structure.add_state(  "day",     "parent",  True)
+    structure.add_state(  "night",   "parent", False)
+    structure.add_state(    "off",    "night",  True)
+    structure.add_state(    "moving", "night", False)
+    structure.add_state(    "timing", "night", False)
 
-#                handling state,   event, next state,           action,                    guard
-structure.add_trans(   "day",        "dark",  "night")
-structure.add_trans( "night",       "light",    "day", methodcaller("lamp_off"))
-structure.add_trans(   "off",    "movement", "moving", methodcaller("lamp_on"))
-structure.add_trans("moving", "no_movement", "timing", methodcaller("start_timer"))
-structure.add_trans("timing",     "timeout",    "off", methodcaller("lamp_off"))
-structure.add_trans("timing",    "movement", "moving")
+    #                handling state,   event, next state,           action,                    guard
+    structure.add_trans(   "day",        "dark",  "night")
+    structure.add_trans( "night",       "light",    "day", methodcaller("lamp_off"))
+    structure.add_trans(   "off",    "movement", "moving", methodcaller("lamp_on"))
+    structure.add_trans("moving", "no_movement", "timing", methodcaller("start_timer"))
+    structure.add_trans("timing",     "timeout",    "off", methodcaller("lamp_off"))
+    structure.add_trans("timing",    "movement", "moving")
 
-actions = Actions()
+    actions = Actions()
 
-sm = SympleDictHFSM(structure, actions)
-sm.init(False)
+    sm = SympleDictHFSM(structure, actions)
+    sm.init(False)
 
 
-# run a event sequence
-sm.handle_event("dark")
-sm.handle_event("movement")
-sm.handle_event("no_movement")
-sm.handle_event("movement")
-sm.handle_event("no_movement")
-sm.handle_event("timeout")
-sm.handle_event("light")
-sm.handle_event("dark")
+    # run a event sequence
+    sm.handle_event("dark")
+    sm.handle_event("movement")
+    sm.handle_event("no_movement")
+    sm.handle_event("movement")
+    sm.handle_event("no_movement")
+    sm.handle_event("timeout")
+    sm.handle_event("light")
+    sm.handle_event("dark")
 
-#-------------------------------------------------------------------------------
+if __name__ == "__main__":
+    main()
+
+# ------------------------------------------------------------------------------
 # class MyStateMachine(SympleDictHFSM, Events):
 
     # # instantiate the states and tarnsitions only once for all instances of MyStateMahine
         # f = operator.methodcaller('timeout', self)
         # self.handle_event(f, self)
 
-# #-------------------------------------------------------------------------------
+# # ------------------------------------------------------------------------------
 # class MyStateMachineTests(BaseHFSMTests):
 
     # class AEnum(object):
     # AEnum.__setattr__ = None
 
 
-    # #--- transition tests ---#
+    # # -- transition tests -- #
     # def setUp(self):
         # self.actions = MyStateMachineTests.RecordingActions()
         # self.state_machine = MyStateMachine(self.actions)
         # v = self.TestVector("timing + timeout -> lamp off: off", MyStateMachine.state_timing, self.state_machine.timeout, MyStateMachine.state_off, [self.AEnum.LAMPOFF])
         # self.prove_one_transition(self.state_machine, self.actions, v)
 
-# #-------------------------------------------------------------------------------
+# # ------------------------------------------------------------------------------
 
     # def test_sequence(self):
         # self.prove_transition_sequence("rout1",
                                         # self.state_machine,
                                         # self.actions)
 
-# #-------------------------------------------------------------------------------
+# # ------------------------------------------------------------------------------
 
 
 

File trunk/symplehfsm/symplehfsm.py

     """
     BaseState from which all hirarchical states should inherit.
     :Note: The state itself is 'stateless'.
-    
+
     :Parameters:
         name : string
             name of this state
             Reference to the parent state, for the root state use None
             (only one state has None as parent since there is only
             one root)
-    
+
     """
     class InitialStateAlreadySetError(Exception):
         """Exception is raised if initial state is already set."""
                 children, raises InitialNotSetError otherwise
             - if each child of a state has the parent attribute set to that
                 state, raises WrongParentError otherwise
+
         ..todo: move check_consistency to Structure class?
         """
         if self.initial is None and len(self.children) > 0:
 class Transition(object):
     """
     This class holds the data needed for a transition.
-    
+
     Represents the transition between (composite) states (just the arrow
     in the state chart).
     The transition itself is 'stateless'.
             This should be a methodcaller object or a function
             behaving like a methodcaller. Such a function would
             have following signature (return value is ignored)::
-            
+
                 def f(actions)
 
             A function behaving like a methodcaller looks like
             this::
-            
+
                   f = lambda actions: actions.any_method_of_actions()
 
             :Note: only the function knows which function to call on the actions object.
         guard : methodcaller
             a methodaller of a function that behaves like a methodcaller
             returning a boolean, its signature is::
-            
+
                 guard(actions) -> bool
 
             If True is returned, then the transition will be followed,
     """
     This is the class holding the state machine structure, e.g. the number
     of states and their relationship (hierarchy) and its transitions in between them.
-    
+
     Ths is also the code that is shared by many instances of the same statemachine.
-    
+
     :Parameters:
         name : string
             Optional name for this instance of this class.
     .. todo:: should transition.action be able to return something to the caller?
 
     Base state machine logic. It implements the state transition logic.
-    
+
     :Parameters:
         structure : Structure
             The state machine structure of states and transitions
             The object implementing the actions interface to be used by the state machine.
         name : string
             Optional, default: None. This name will be used for logging and printing.
-    
+
     """
 
     class ReentrantEventException(Exception):
         if __debug__:
             logger.debug(str.format("{0}: INIT done, current state: {1}", self, node))
         self._current_state = node
-        
+
         # set up the right event handling method bo be used
         if use_optimization and self._structure.is_optimized:
             self.handle_event = self._handle_event_optimized
     def _handle_event_optimized(self, event):
         """
         The event handling method used when the structure is optimized.
-        
+
         .. todo:: how to remove those checks? use queue to make those check unneeded??
         """
         if self._currently_handling_event:
         """
         A TestVector is basically the data container needed to test one
         transition.
-        
+
         :Parameters:
             title : string
                 Description of this TestVector
             expected_actions : list
                 list of expected actions to be compared with the
                 captured actions
-        
+
         """
 
         def __init__(self,
                                 captured: {1}".format( \
                                 ", ".join(test_vector.expected_actions), \
                                 ", ".join(resulting_actions.captured_actions)))
-                                
+
         for idx, expected_action in enumerate(test_vector.expected_actions):
             action = resulting_actions.captured_actions[idx]
             if action != expected_action: