1+ from Conditions import Conditions
2+ from Actions import Actions
3+ from Event import Event
4+ from State import State
5+ import xml .etree .ElementTree as ET
6+
7+
8+ def ReadStateMachineFile (xml_file : str ):
9+ states = {}
10+ initial_state = ""
11+ tree = ET .parse (xml_file )
12+ root = tree .getroot ()
13+ for states_child in root :
14+ #print(states_child.tag, states_child.attrib)
15+ # States Element
16+ if states_child .tag == "State" :
17+ #print("State element")
18+ # State Element
19+ state_name = ""
20+ events = {}
21+ for state_child in states_child :
22+ #print(state_child.tag, state_child.attrib)
23+ if state_child .tag == "Event" :
24+ #print("Event element")
25+ # State->Event Element
26+ event_name = ""
27+ to_state = ""
28+ pre_conditions = Conditions ("" ) # dummy
29+ post_conditions = Conditions ("" ) # dummy
30+ pre_actions = Actions ("" ) #dummy
31+ post_actions = Actions ("" ) #dummy
32+
33+ for event_child in state_child :
34+
35+ #print(event_child.tag, event_child.attrib)
36+ if event_child .tag == "Name" :
37+ #print("Name element = ", event_child.text)
38+ # State->Event->Name Element
39+ event_name = event_child .text
40+ elif event_child .tag == "ToState" :
41+ #print("ToState element = ", event_child.text)
42+ # State->Event->ToState Element
43+ to_state = event_child .text
44+ elif event_child .tag == "PreConditions" :
45+ # State->Event->PreConditions Element
46+ for preconditions_child in event_child :
47+ #print(preconditions_child.text)
48+ # State->Event->PreConditions->Condition Element
49+ None
50+ elif event_child .tag == "PostConditions" :
51+ # State->Event->PostConditions Element
52+ for postconditions_child in event_child :
53+ #print(postconditions_child.text)
54+ # State->Event->PostConditions->Condition Element
55+ None
56+ elif event_child .tag == "PreActions" :
57+ # State->Event->PreActions Element
58+ for preactions_child in event_child :
59+ #print(preactions_child.text)
60+ # State->Event->PreActions->Action Element
61+ None
62+ elif event_child .tag == "PostActions" :
63+ # State->Event->PostActions Element
64+ for postactions_child in event_child :
65+ #print(postactions_child.text)
66+ # State->Event->PostActions->Action Element
67+ None
68+ events [event_name ] = Event (event_name ,to_state ,pre_conditions ,post_conditions ,pre_actions ,post_actions )
69+ #print(event.to_string())
70+ elif state_child .tag == "Name" :
71+ #print("Name element = ", state_child.text)
72+ # State->Name Element
73+ state_name = state_child .text
74+
75+ states [state_name ] = State (state_name , events )
76+
77+ elif states_child .tag == "Initial_State" :
78+ #print ("Initial_State element = ", states_child.text)
79+ # Initial_State Element
80+ initial_state = states_child .text
81+
82+ return states , initial_state
0 commit comments