-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraiter_xml.py
More file actions
128 lines (90 loc) · 4.06 KB
/
traiter_xml.py
File metadata and controls
128 lines (90 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import xml.etree.ElementTree as ET
class Automaton(): #on définit la classe automate, utile pour le prétraitement des données
def __init__(self):
self.states = {} # Dictionnaire d'étatss
self.transitions = [] # List de transitions
self.initial_state = None
def add_state(self, state_id, name, x, y, invariant = None):
self.states[state_id] = {'name': name, 'x': x, 'y': y, 'invariant' : invariant}
def set_initial_state(self, state_id):
self.initial_state = state_id
def add_transition(self, source, target, guard=None, sync = None, assignement = None):
self.transitions.append({
'source': source,
'target': target,
'guard': guard,
'sync' : sync,
'assignement' : assignement
})
def gestion_modele(nom_doc):
tree = ET.parse(nom_doc)
root = tree.getroot()
L_model = []
# On sépare selon chaque modèle différent
for template in root.findall('template'):
new_automaton = Automaton()
L_model.append(new_automaton)
# Parse states (locations)
for location in template.findall('location'):
state_id = location.get('id')
name = location.find('name').text if location.find('name') is not None else state_id
x = location.get('x') if location.get('x') is not None else 0
y = location.get('y') if location.get('y') is not None else 0
"""invariant = location.find('kind') if label_element.text else None"""
L_model[-1].add_state(state_id, name, x, y, invariant= None)
# Trouver l'état initial
init = template.find('init')
if init is not None:
L_model[-1].set_initial_state(init.get('ref'))
# trouver les transitions
for transition in template.findall('transition'):
source = transition.find('source').get('ref')
target = transition.find('target').get('ref')
# Rajout des infos utils aux transitions
for label_element in transition.findall('label'):
if label_element.get('kind') == 'guard':
guard = label_element.text
else :
guard = None
if label_element.get('kind') == 'synchronisation':
sync = label_element.text
else:
sync = None
if label_element.get('kind') == 'assignement':
assignement = label_element.text
else :
assignement = None
L_model[-1].add_transition(source, target, guard, sync, assignement)
return L_model
def to_graph(model): #transforme un de nos modèles en un graphe représenté par dictionnaire d'adjacence
d_adj = {}
for id, state in model.states.items():
d_adj[id] = []
for transition in model.transitions:
d_adj[transition["source"]].append(transition["target"])
return d_adj
def to_sommet(l_cycle): #transforme un graphe sans toute la grosse structure pour rendre lisible le résultat du projet
new_list_zeno = []
for cycle in l_cycle:
invariant = 0
new_cycle = []
for sommet in cycle:
if invariant:
invariant = 1-invariant
else :
new_cycle.append(sommet['name'])
invariant = 1 - invariant
new_list_zeno.append(new_cycle)
return new_list_zeno
"""
print("States:")
for state_id, state in automaton.states.items():
print(f"ID: {state_id}, Name: {state['name']}, Position: ({state['x']}, {state['y']})")
print("\nInitial State:")
for model in L_model:
print(model.initial_state)
for model in L_model:
print("\nTransitions:")
for transition in model.transitions:
print(f"From {transition['source']} to {transition['target']} with guard '{transition['guard']}'")
"""