33
44
55class StateMachine :
6+ """
7+ This is a class defines state machine operations.
8+
9+ Attributes:
10+ xml_file (str): The name of the xml that defines the state machine.
11+ states (list): list of possible states of the machine.
12+ current_state (str): the current state of the machine.
13+ context (dict): dictionary that contains the imported module at runtime for state machine operations.
14+ saved_state (list): list of variables for restore state in case of rollback.
15+ """
616 def __init__ (self , xml_file : str ):
17+ """
18+ The constructor for StateMachine class.
19+
20+ Parameters:
21+ xml_file (str): The name of the xml that defines the state machine.
22+ """
723 self .xml_file = xml_file
824 self .states = None
925 self .current_state = ""
@@ -13,6 +29,14 @@ def __init__(self, xml_file: str):
1329 0 ] + '.log' , format = '%(asctime)s - %(levelname)s - %(message)s' , level = logging .DEBUG )
1430
1531 def __CheckConditions (self , conditions ):
32+ """
33+ This Function checks the conditions passed as argument
34+
35+ Parameters:
36+ conditions (list): List of condition to check.
37+ Returns:
38+ all_conditions_satisfied: a boolean that indicates if all conditions are satisfied
39+ """
1640 all_conditions_satisfied = True
1741 if (conditions is not None ):
1842 _conditions = conditions .conditions_list
@@ -43,6 +67,14 @@ def __CheckConditions(self, conditions):
4367 return all_conditions_satisfied
4468
4569 def __ExecActions (self , actions ):
70+ """
71+ This Function executes the actions passed as argument
72+
73+ Parameters:
74+ actions (list): List of actions to execute.
75+ Returns:
76+ all_action_executed: a boolean that indicates if all actions are executed
77+ """
4678 all_action_executed = True
4779 if (actions is not None ):
4880 _actions = actions .actions_list
@@ -67,21 +99,45 @@ def __ExecActions(self, actions):
6799 return all_action_executed
68100
69101 def __SaveState (self ):
102+ """
103+ This Function saves internal state
104+ """
70105 self .saved_state = [self .current_state , self .context ]
71106
72107 def __RestoreState (self ):
108+ """
109+ This Function restores the saved state
110+ """
73111 self .current_state = self .saved_state [0 ]
74112 self .context = self .saved_state [1 ]
75113
76114 @staticmethod
77115 def __PrepareExpression (expression ):
116+ """
117+ This Function split expression in module and expression
118+
119+ Parameters:
120+ expression (str): complete expression.
121+ Returns:
122+ module (str): module string
123+ expression (str): expression string
124+ """
78125 module_expression = expression .rsplit ('.' , 1 )
79126 return module_expression [0 ], module_expression [1 ]
80127
81128 def get_current_state (self ):
129+ """
130+ This Function return current state
131+
132+ Returns:
133+ current_state (str): current state
134+ """
82135 return self .current_state
83136
84137 def LoadStateMachine (self ):
138+ """
139+ This Function load state machine configuration
140+ """
85141 if (self .states is not None ):
86142 logging .error ("State Machine already loaded" )
87143 else :
@@ -90,10 +146,22 @@ def LoadStateMachine(self):
90146 logging .info ('State Machine Loaded' )
91147
92148 def addModuleToContext (self , module : str ):
149+ """
150+ This Function adds a module to the context of state machine
151+
152+ Parameters:
153+ module (str): The module to add.
154+ """
93155 mod = __import__ (module )
94156 self .context [module ] = mod
95157
96158 def InjectEvent (self , event : str ):
159+ """
160+ This Function execute the event injected
161+
162+ Parameters:
163+ event (str): Event injected
164+ """
97165 my_state = self .states [self .current_state ]
98166 possible_events = my_state .events
99167 if event in possible_events :
0 commit comments