@@ -7,6 +7,7 @@ def __init__(self, xml_file : str):
77 self .xml_file = xml_file
88 self .states = None
99 self .current_state = ""
10+ self .context = {}
1011
1112 def get_current_state (self ):
1213 return self .current_state
@@ -17,18 +18,44 @@ def LoadStateMachine(self):
1718 else :
1819 self .xml_file
1920 self .states , self .current_state = ReadStateMachineFile (self .xml_file )
21+
22+ def addVariableToContext (self , module : str , variable : str ):
23+ mod = __import__ (module )
24+ func = getattr (mod , variable )
25+ self .context [module + "." + variable ] = func
2026
2127 def InjectEvent (self , event : str ):
2228 my_state = self .states [self .current_state ]
23- possible_events = my_state .get_events ()
29+ possible_events = my_state .events
2430 if event in possible_events :
2531 handled_event = possible_events [event ]
2632 ## Preconditions
27- ## Preactions
28- print ("Transition " , self .current_state , " ------> " , handled_event .get_to_state ())
29- self .current_state = handled_event .get_to_state ()
30- ## Postactions
31- ## Postconditions
33+ all_pre_conditions_satisfied = True
34+ if (handled_event .pre_conditions != None ):
35+ pre_conditions = handled_event .pre_conditions .conditions
36+ for condition in pre_conditions :
37+ if condition .expression in self .context :
38+ func = self .context [condition .expression ]
39+ result = None
40+ if callable (func ):
41+ result = func ()
42+ else :
43+ result = func
44+ if str (result ) != condition .result :
45+ all_pre_conditions_satisfied = False
46+ break
47+ else :
48+ print ("No Precondition" )
49+
50+ if (all_pre_conditions_satisfied ):
51+ ## Preactions
52+ ## Transition
53+ print ("Transition " , self .current_state , " ------> " , handled_event .to_state )
54+ self .current_state = handled_event .to_state
55+ ## Postactions
56+ ## Postconditions
57+ else :
58+ print ("Not all PreConditions satisfied" )
3259 else :
3360 print ("Not a possible event" )
3461
0 commit comments