-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
420 lines (359 loc) · 18.1 KB
/
Copy pathmodel.py
File metadata and controls
420 lines (359 loc) · 18.1 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from utils.dbm_engine import generate_and_save_engine_json
from utils.dbm_engine import dbm_to_string_constraints as convert_dbm_to_constraints
import pprint
import re
import math
class AutomatonModel:
def __init__(self):
self.clear()
def clear(self):
"""Réinitialise complètement le modèle à son état vide (nouveau fichier)."""
self.data = {
"omit_ui_data": False,
"locations": {},
"init": "",
"transitions": [],
"actions": [],
"clocks": [],
"variables": {
"definition": {
"define": [],
"typedef": {
"structure": {},
"alias": {}
}
},
"init_variables": [],
"update_functions": {},
"constraints": {}
}
}
self.loc_counter = 0
self.trans_counter = 0
def add_location(self, x, y):
"""Crée l'entrée dans le buffer et retourne l'ID"""
loc_id = f"l{self.loc_counter}"
if self.loc_counter == 0:
self.data["init"] = loc_id
self.loc_counter += 1
self.data["locations"][loc_id] = {
"node_pos": {"x": x, "y": y},
"invariants": [], # Stocke la liste des conditions d'invariants
}
return loc_id
def add_transition(self, source_id, target_id, nails_pos=None):
"""Ajoute une transition dans le modèle"""
trans_id = f"t{self.trans_counter}"
self.trans_counter += 1
transition = {
"id": trans_id,
"source": source_id,
"target": target_id,
"nails": nails_pos or [],
}
self.data["transitions"].append(transition)
return trans_id
def get_transition(self, trans_id):
for t in self.data["transitions"]:
if t.get("id") == trans_id:
return t
return None
def change_transition_endpoint(self, trans_id, new_source, new_target):
"""Modifie les points de départ ou d'arrivée d'une transition."""
t = self.get_transition(trans_id)
if t:
t["source"] = new_source
t["target"] = new_target
def add_action(self, action_name):
"""Ajoute une action si elle n'existe pas déjà (ignore les doublons)."""
if action_name and action_name not in self.data["actions"]:
self.data["actions"].append(action_name)
def add_clock(self, clock_name):
"""Ajoute une horloge si elle n'existe pas déjà (ignore les doublons)."""
if clock_name and clock_name not in self.data["clocks"]:
self.data["clocks"].append(clock_name)
def update_transition_action(self, trans_id, action_name):
"""Met à jour l'action associée à une transition."""
t = self.get_transition(trans_id)
if t:
t["action"] = action_name if action_name != "Aucune" else ""
def update_node_position(self, node_id, x, y):
"""Met à jour les coordonnées d'une localité après un déplacement."""
if node_id in self.data["locations"]:
self.data["locations"][node_id]["node_pos"] = {"x": x, "y": y}
def update_nail_position(self, trans_id, nail_index, x, y):
"""Met à jour les coordonnées d'un clou spécifique sur une transition après déplacement."""
t = self.get_transition(trans_id)
if t:
if 0 <= nail_index < len(t["nails"]):
t["nails"][nail_index] = (x, y)
def add_node_invariant(
self, node_id, clock, operator, target_type="value", target_value="0", offset=0
):
"""Ajoute une condition d'invariant pour une horloge sur une localité."""
if node_id in self.data["locations"]:
if "invariants" not in self.data["locations"][node_id]:
self.data["locations"][node_id]["invariants"] = []
invariants = self.data["locations"][node_id]["invariants"]
new_inv = {
"clock": clock,
"operator": operator,
"type": target_type,
"value": target_value,
}
if target_type == "clock":
new_inv["offset"] = offset
invariants.append(new_inv)
def remove_node_invariant(self, node_id, index):
"""Supprime un invariant spécifique d'une localité via son index."""
if (
node_id in self.data["locations"]
and "invariants" in self.data["locations"][node_id]
):
if 0 <= index < len(self.data["locations"][node_id]["invariants"]):
self.data["locations"][node_id]["invariants"].pop(index)
def add_transition_guard(
self,
trans_id,
clock,
operator,
target_type="value",
target_value="0",
offset=0
):
"""Ajoute une condition de garde pour une horloge sur une transition."""
t = self.get_transition(trans_id)
if t:
if "guards" not in t: t["guards"] = []
new_guard = {
"clock": clock,
"operator": operator,
"type": target_type,
"value": target_value,
}
if target_type == "clock": new_guard["offset"] = offset
t["guards"].append(new_guard)
def remove_transition_guard(self, trans_id, index):
"""Supprime une garde spécifique d'une transition via son index."""
t = self.get_transition(trans_id)
if t:
if "guards" in t and 0 <= index < len(t["guards"]):
t["guards"].pop(index)
def remove_transition(self, trans_id):
"""Supprime une transition de la liste du modèle."""
t = self.get_transition(trans_id)
if t:
self.data["transitions"].remove(t)
def set_initial_state(self, loc_id):
"""Définit la localité initiale si elle existe."""
if loc_id in self.data["locations"]:
self.data["init"] = loc_id
def update_variables(self, new_variables_data):
"""Met à jour l'ensemble des données liées aux variables depuis l'éditeur de données."""
self.data["variables"] = new_variables_data
def remove_node(self, node_id):
"""Supprime une localité. (Les transitions sont déjà nettoyées par le contrôleur)."""
if node_id in self.data["locations"]:
del self.data["locations"][node_id]
if self.data.get("init") == node_id:
# Assigner une nouvelle localité initiale par défaut s'il en reste, pour éviter un champ vide
self.data["init"] = next(iter(self.data["locations"].keys())) if self.data["locations"] else ""
# generation du json
def export_to_json(self, filepath):
"""Délègue la compilation DBM au script tiers, puis réordonne le JSON pour mettre 'variables' à la fin."""
# 1. Sauvegarder le fichier initialement via le script existant
result = generate_and_save_engine_json(self.data, filepath)
# 2. Post-traitement : Forcer la clé "variables" à être la toute dernière dans le fichier généré
try:
import json
with open(filepath, 'r', encoding='utf-8') as f:
saved_data = json.load(f)
if "variables" in saved_data:
# Retirer et réinsérer la clé la place automatiquement à la fin du dictionnaire (Python 3.7+)
vars_content = saved_data.pop("variables")
saved_data["variables"] = vars_content
with open(filepath, 'w', encoding='utf-8') as f:
# Réécriture propre du JSON avec variables à la fin
json.dump(saved_data, f, indent=4, ensure_ascii=False)
except Exception as e:
print(f"[Model] Erreur lors de la réorganisation du JSON : {e}")
return result
# chargement du json pour reconstruire le dictionnaire "data" du model
def load_from_json_data(self, json_data):
"""
Reconstruit le dictionnaire de données interne au format strict du Modèle.
Élimine toutes les matrices DBM pour ne conserver que les dictionnaires UI.
"""
# 1. Initialisation de la structure pure attendue par l'UI et le Contrôleur
self.trans_counter = 0
# Détection d'un fichier externe (sans attributs de position UI)
meta_keys = {"clocks", "actions", "init", "locations", "transitions", "variables"}
has_any_pos = False
for key, value in json_data.items():
if key not in meta_keys and isinstance(value, dict):
if "node_pos" in value:
has_any_pos = True
break
self.data = {
"omit_ui_data": not has_any_pos,
"actions": json_data.get("actions", []),
"clocks": json_data.get("clocks", []),
"init": json_data.get("init", ""),
"locations": {},
"transitions": [],
"variables": json_data.get("variables", {
"definition": {
"define": [],
"typedef": {
"structure": {},
"alias": {}
}
},
"init_variables": [],
"update_functions": {},
"constraints": {}
})
}
# Ajout des données
# Génération de la map des horloges (indexation décalée de 1 car index 0 = x0)
clock_map = {name: i + 1 for i, name in enumerate(self.data["clocks"])}
def parse_to_dict(c_str):
"""Convertit une contrainte texte (DBM) en dictionnaire lisible par l'UI"""
if isinstance(c_str, dict): return c_str
clean_str = str(c_str).replace(" ", "")
match = re.match(r"^([a-zA-Z0-9_]+)(?:-([a-zA-Z0-9_]+))?([<>=!]+)(-?\d+)$", clean_str)
if match:
c1, c2, op, val = match.groups()
if c2 and c2 not in ["x0", "0"]:
return {"clock": c1, "operator": op, "type": "clock", "value": c2, "offset": int(val)}
else:
return {"clock": c1, "operator": op, "type": "value", "value": val}
return {"clock": str(c_str), "operator": "<=", "type": "value", "value": "0"}
def safe_pos(pos_dict):
"""Extrait et garantit que les coordonnées (x,y) sont de type float, sinon renvoie 0.0"""
if not isinstance(pos_dict, dict): return {"x": 0.0, "y": 0.0}
try: x = float(pos_dict.get("x", 0.0))
except (ValueError, TypeError): x = 0.0
try: y = float(pos_dict.get("y", 0.0))
except (ValueError, TypeError): y = 0.0
return {"x": x, "y": y}
# 2. Parcours du JSON pour extraire et traduire les données
for key, value in json_data.items():
if key not in meta_keys and isinstance(value, dict):
# Traduction immédiate de la matrice d'invariant en dictionnaires UI
raw_inv = value.get("invariant", [])
invariants_textuels = [parse_to_dict(c) for c in convert_dbm_to_constraints(raw_inv, clock_map)] if raw_inv else []
# Stockage exclusif des données nettoyées
self.data["locations"][key] = {
"invariants": invariants_textuels,
"node_pos": safe_pos(value.get("node_pos")),
"name_pos": safe_pos(value.get("name_pos")),
"invariant_pos": safe_pos(value.get("invariant_pos"))
}
# Traitement des transitions sortantes de cette localité
transitions_du_noeud = value.get("transitions", [])
layouts_des_transitions = value.get("transitions_layout", [])
for idx, t in enumerate(transitions_du_noeud):
# Traduction immédiate de la matrice de garde en dictionnaires UI
raw_guard = t[1]
gardes_textuelles = [parse_to_dict(c) for c in convert_dbm_to_constraints(raw_guard, clock_map)] if raw_guard else []
raw_nails = layouts_des_transitions[idx] if idx < len(layouts_des_transitions) else []
clean_nails = []
if isinstance(raw_nails, list):
for n in raw_nails:
try:
if isinstance(n, dict): # Format Cosmos
clean_nails.append([float(n.get("x", 0.0)), float(n.get("y", 0.0))])
elif isinstance(n, (list, tuple)) and len(n) >= 2: # Format liste strict
clean_nails.append([float(n[0]), float(n[1])])
except (ValueError, TypeError):
pass
# Reconstruction de la transition selon la structure exacte du modèle
trans_id = f"t{self.trans_counter}"
self.trans_counter += 1
transition_dict = {
"id": trans_id,
"guards": gardes_textuelles,
"nails": clean_nails,
"resets": t[2] if t[2] else [],
"source": key,
"target": t[3],
"action": t[0] if t[0] else ""
}
self.data["transitions"].append(transition_dict)
# 3. Synchronisation du compteur de localités du modèle
self.loc_counter = len(self.data["locations"])
# --- Disposition automatique (Disposition Circulaire) ---
# Assigne des positions temporaires si le fichier d'origine n'en contient pas
if self.data.get("omit_ui_data", False):
loc_keys = list(self.data["locations"].keys())
num_locs = len(loc_keys)
if num_locs > 0:
radius = max(150, num_locs * 50)
center_x, center_y = 400.0, 300.0
for i, loc_id in enumerate(loc_keys):
angle = 2 * math.pi * i / num_locs
self.data["locations"][loc_id]["node_pos"] = {
"x": round(center_x + radius * math.cos(angle), 2),
"y": round(center_y + radius * math.sin(angle), 2)
}
def modify_clock(self, old_name, new_name):
"""Modifie le nom d'une horloge partout où elle est utilisée."""
if old_name not in self.data["clocks"]: return
if new_name in self.data["clocks"]: return # Eviter d'écraser
idx = self.data["clocks"].index(old_name)
self.data["clocks"][idx] = new_name
for loc in self.data["locations"].values():
for inv in loc.get("invariants", []):
if inv.get("clock") == old_name:
inv["clock"] = new_name
if inv.get("type") == "clock" and inv.get("value") == old_name:
inv["value"] = new_name
for t in self.data["transitions"]:
for guard in t.get("guards", []):
if guard.get("clock") == old_name:
guard["clock"] = new_name
if guard.get("type") == "clock" and guard.get("value") == old_name:
guard["value"] = new_name
if "resets" in t:
t["resets"] = [new_name if r == old_name else r for r in t["resets"]]
def delete_clock(self, clock_name):
"""Supprime une horloge et toutes les contraintes qui l'utilisent."""
if clock_name not in self.data["clocks"]: return
self.data["clocks"].remove(clock_name)
for loc in self.data["locations"].values():
if "invariants" in loc:
loc["invariants"] = [inv for inv in loc["invariants"]
if inv.get("clock") != clock_name and not (inv.get("type") == "clock" and inv.get("value") == clock_name)]
for t in self.data["transitions"]:
if "guards" in t:
t["guards"] = [guard for guard in t["guards"]
if guard.get("clock") != clock_name and not (guard.get("type") == "clock" and guard.get("value") == clock_name)]
if "resets" in t and clock_name in t["resets"]:
t["resets"].remove(clock_name)
def modify_action(self, old_name, new_name):
"""Modifie le nom d'une action partout où elle est utilisée."""
if old_name not in self.data["actions"]: return
if new_name in self.data["actions"]: return
idx = self.data["actions"].index(old_name)
self.data["actions"][idx] = new_name
for t in self.data["transitions"]:
if t.get("action") == old_name:
t["action"] = new_name
vars_data = self.data.get("variables", {})
if "update_functions" in vars_data and old_name in vars_data["update_functions"]:
vars_data["update_functions"][new_name] = vars_data["update_functions"].pop(old_name)
if "constraints" in vars_data and old_name in vars_data["constraints"]:
vars_data["constraints"][new_name] = vars_data["constraints"].pop(old_name)
def delete_action(self, action_name):
"""Supprime une action et ses données additionnelles (update-functions et contraintes)."""
if action_name not in self.data["actions"]: return
self.data["actions"].remove(action_name)
for t in self.data["transitions"]:
if t.get("action") == action_name:
t["action"] = ""
vars_data = self.data.get("variables", {})
if "update_functions" in vars_data and action_name in vars_data["update_functions"]:
del vars_data["update_functions"][action_name]
if "constraints" in vars_data and action_name in vars_data["constraints"]:
del vars_data["constraints"][action_name]