Skip to content

Commit b1406ec

Browse files
authored
Introduced a first read of simple XML sample
1 parent 2da78bb commit b1406ec

6 files changed

Lines changed: 148 additions & 0 deletions

File tree

src/Actions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Actions:
2+
def __init__(self, action : str):
3+
self.action = action
4+
5+
def to_string(self):
6+
result_s = ""
7+
return result_s

src/Conditions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Conditions:
2+
def __init__(self,condition:str):
3+
self.condition = condition
4+
5+
def to_string(self):
6+
result_s = ""
7+
return result_s

src/Event.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Actions
2+
import Conditions
3+
4+
class Event:
5+
def __init__(self, name :str, to_state: str, pre_conditions: Conditions, post_conditions: Conditions, pre_actions: Actions, post_actions: Actions):
6+
self.name = name
7+
self.to_state = to_state
8+
self.pre_conditions = pre_conditions
9+
self.post_conditions = post_conditions
10+
self.pre_actions = pre_actions
11+
self.post_actions = post_actions
12+
13+
def to_string(self):
14+
result_s = "Event: \n"
15+
result_s += "\tName: " + self.name + "\n"
16+
result_s += "\tToState: " + self.to_state + "\n"
17+
result_s += "\tPreConditions: " + self.pre_conditions.to_string() + "\n"
18+
result_s += "\tPostConditions: " + self.post_conditions.to_string() + "\n"
19+
result_s += "\tPreActions: " + self.pre_actions.to_string() + "\n"
20+
result_s += "\tPostActions: " + self.post_actions.to_string() + "\n"
21+
return result_s

src/Main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import argparse
2+
from ReadStateMachine import ReadStateMachineFile
3+
4+
def main():
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument("--xml_file",type=str, help="path to the xml state machine file", required=True)
7+
args = parser.parse_args()
8+
print(args)
9+
10+
states, initial_state = ReadStateMachineFile(args.xml_file)
11+
for item in states.items():
12+
print(item[1].to_string())
13+
print("Initial State: ", initial_state)
14+
15+
16+
if __name__ == "__main__":
17+
main()

src/ReadStateMachine.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

src/State.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Event
2+
3+
class State:
4+
def __init__(self, name: str, events = {}):
5+
self.name = name
6+
self.events = events
7+
8+
def to_string(self):
9+
result_s = "State:\n"
10+
result_s += " Name: " + self.name + "\n"
11+
result_s += " Events: \n"
12+
for event in self.events.items():
13+
result_s += " " + event[1].to_string() + "\n"
14+
return result_s

0 commit comments

Comments
 (0)