@@ -9,25 +9,32 @@ def __init__(self, xml_file : str):
99 self .states = None
1010 self .current_state = ""
1111 self .context = {}
12+ self .saved_state = None
1213 logging .basicConfig (filename = xml_file .split (sep = ".xml" )[0 ] + '.log' ,format = '%(asctime)s - %(levelname)s - %(message)s' , level = logging .DEBUG )
1314
1415 def __CheckConditions (self ,conditions ):
1516 all_conditions_satisfied = True
1617 if (conditions != None ):
1718 _conditions = conditions .conditions
1819 for condition in _conditions :
19- if condition .expression in self .context :
20- func = self .context [condition .expression ]
21- result = None
22- if callable (func ):
23- result = func ()
24- else :
25- result = func
26- if str (result ) != condition .result :
27- all_conditions_satisfied = False
28- break
20+ module ,expression = self .__PrepareExpression (condition .expression )
21+ if module in self .context :
22+ mod = self .context [module ]
23+ try :
24+ func = getattr (mod , expression )
25+ result = None
26+ if callable (func ):
27+ result = func ()
28+ else :
29+ result = func
30+ if str (result ) != condition .result :
31+ all_conditions_satisfied = False
32+ break
33+ except :
34+ logging .error ("Not Found Expression %s in Context" , condition .expression )
35+ all_conditions_satisfied = False
2936 else :
30- logging .error ("No Found Condition Expression %s in Context" , condition . expression )
37+ logging .error ("Not Found Module %s in Context" , module )
3138 all_conditions_satisfied = False
3239 else :
3340 logging .info ("No Condition" )
@@ -38,20 +45,36 @@ def __ExecActions(self,actions):
3845 if (actions != None ):
3946 _actions = actions .actions
4047 for action in _actions :
41- if action .expression in self .context :
42- func = self .context [action .expression ]
43- if callable (func ):
44- #print("Call ",action.expression)
45- func ()
46- else :
47- func
48+ module ,expression = self .__PrepareExpression (action .expression )
49+ if module in self .context :
50+ mod = self .context [module ]
51+ try :
52+ func = getattr (mod , expression )
53+ if callable (func ):
54+ func ()
55+ else :
56+ func
57+ except :
58+ logging .error ("Not Found Expression %s in Context" , action .expression )
59+ all_action_executed = False ;
4860 else :
49- logging .error ("No Found Action Expression %s in Context" , action . expression )
61+ logging .error ("Not Found Module %s in Context" , module )
5062 all_action_executed = False ;
5163 else :
5264 logging .info ("No Action" )
5365 return all_action_executed
5466
67+ def __SaveState (self ):
68+ self .saved_state = [self .current_state , self .context ]
69+
70+ def __RestoreState (self ):
71+ self .current_state = self .saved_state [0 ]
72+ self .context = self .saved_state [1 ]
73+
74+ def __PrepareExpression (self ,expression ):
75+ module_expression = expression .rsplit ('.' ,1 )
76+ return module_expression [0 ],module_expression [1 ]
77+
5578 def get_current_state (self ):
5679 return self .current_state
5780
@@ -63,35 +86,45 @@ def LoadStateMachine(self):
6386 self .states , self .current_state = ReadStateMachineFile (self .xml_file )
6487 logging .info ('State Machine Loaded' )
6588
66- def addVariableToContext (self , module : str , variable : str ):
67- mod = __import__ (module )
68- func = getattr (mod , variable )
69- self .context [module + "." + variable ] = func
89+ def addModuleToContext (self , module : str ):
90+ mod = __import__ (module )
91+ self .context [module ] = mod
7092
7193 def InjectEvent (self , event : str ):
7294 my_state = self .states [self .current_state ]
7395 possible_events = my_state .events
7496 if event in possible_events :
7597 handled_event = possible_events [event ]
98+ ## Save Old State
99+ self .__SaveState ()
76100 ## Preconditions
77101 all_pre_conditions_satisfied = self .__CheckConditions (handled_event .pre_conditions )
78102 if (all_pre_conditions_satisfied ):
103+ logging .debug ("PreConditions satisfied" )
79104 ## Preactions
80105 all_pre_actions_executed = self .__ExecActions (handled_event .pre_actions )
81106 if (all_pre_actions_executed ):
82- ## Save Old State
107+ logging . debug ( "PreActions Executed" )
83108 ## Transition
84109 logging .debug ("Transition %s ------> %s" , self .current_state , handled_event .to_state )
85110 self .current_state = handled_event .to_state
86111 ## Postactions
87112 all_post_actions_executed = self .__ExecActions (handled_event .post_actions )
88113 if (all_post_actions_executed ):
89- None
114+ logging . debug ( "PostActions Executed" )
90115 ## Postconditions
116+ all_post_conditions_satisfied = self .__CheckConditions (handled_event .post_conditions )
117+ if (all_post_conditions_satisfied ):
118+ logging .debug ("PostConditions Satisfied" )
119+ else :
120+ logging .error ("Not all PostConditions satisfied, restore saved state" )
121+ self .__RestoreState ()
91122 else :
92- logging .error ("Not all PostActions Executed" )
123+ logging .error ("Not all PostActions Executed, restore saved state" )
124+ self .__RestoreState ()
93125 else :
94- logging .error ("Not all PretActions Executed" )
126+ logging .error ("Not all PretActions Executed, restore saved state" )
127+ self .__RestoreState ()
95128 else :
96129 logging .error ("Not all PreConditions satisfied" )
97130 else :
0 commit comments