@@ -9,25 +9,32 @@ def __init__(self, xml_file : str):
9
9
self .states = None
10
10
self .current_state = ""
11
11
self .context = {}
12
+ self .saved_state = None
12
13
logging .basicConfig (filename = xml_file .split (sep = ".xml" )[0 ] + '.log' ,format = '%(asctime)s - %(levelname)s - %(message)s' , level = logging .DEBUG )
13
14
14
15
def __CheckConditions (self ,conditions ):
15
16
all_conditions_satisfied = True
16
17
if (conditions != None ):
17
18
_conditions = conditions .conditions
18
19
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
29
36
else :
30
- logging .error ("No Found Condition Expression %s in Context" , condition . expression )
37
+ logging .error ("Not Found Module %s in Context" , module )
31
38
all_conditions_satisfied = False
32
39
else :
33
40
logging .info ("No Condition" )
@@ -38,20 +45,36 @@ def __ExecActions(self,actions):
38
45
if (actions != None ):
39
46
_actions = actions .actions
40
47
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 ;
48
60
else :
49
- logging .error ("No Found Action Expression %s in Context" , action . expression )
61
+ logging .error ("Not Found Module %s in Context" , module )
50
62
all_action_executed = False ;
51
63
else :
52
64
logging .info ("No Action" )
53
65
return all_action_executed
54
66
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
+
55
78
def get_current_state (self ):
56
79
return self .current_state
57
80
@@ -63,35 +86,45 @@ def LoadStateMachine(self):
63
86
self .states , self .current_state = ReadStateMachineFile (self .xml_file )
64
87
logging .info ('State Machine Loaded' )
65
88
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
70
92
71
93
def InjectEvent (self , event : str ):
72
94
my_state = self .states [self .current_state ]
73
95
possible_events = my_state .events
74
96
if event in possible_events :
75
97
handled_event = possible_events [event ]
98
+ ## Save Old State
99
+ self .__SaveState ()
76
100
## Preconditions
77
101
all_pre_conditions_satisfied = self .__CheckConditions (handled_event .pre_conditions )
78
102
if (all_pre_conditions_satisfied ):
103
+ logging .debug ("PreConditions satisfied" )
79
104
## Preactions
80
105
all_pre_actions_executed = self .__ExecActions (handled_event .pre_actions )
81
106
if (all_pre_actions_executed ):
82
- ## Save Old State
107
+ logging . debug ( "PreActions Executed" )
83
108
## Transition
84
109
logging .debug ("Transition %s ------> %s" , self .current_state , handled_event .to_state )
85
110
self .current_state = handled_event .to_state
86
111
## Postactions
87
112
all_post_actions_executed = self .__ExecActions (handled_event .post_actions )
88
113
if (all_post_actions_executed ):
89
- None
114
+ logging . debug ( "PostActions Executed" )
90
115
## 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 ()
91
122
else :
92
- logging .error ("Not all PostActions Executed" )
123
+ logging .error ("Not all PostActions Executed, restore saved state" )
124
+ self .__RestoreState ()
93
125
else :
94
- logging .error ("Not all PretActions Executed" )
126
+ logging .error ("Not all PretActions Executed, restore saved state" )
127
+ self .__RestoreState ()
95
128
else :
96
129
logging .error ("Not all PreConditions satisfied" )
97
130
else :
0 commit comments