-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPNLab.py
764 lines (603 loc) · 32 KB
/
PNLab.py
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
# -*- coding: utf-8 -*-
'''
@author: Adrián Revuelta Cuauhtli
'''
import sys
import os
import tkMessageBox
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import Tkinter as tk
import ttk
import tkFileDialog
import tkFont
import zipfile
import tempfile
from lxml import etree as ET
from subprocess import call
from PetriNets import PetriNet, PlaceTypes
from GUI.TabManager import TabManager
from GUI.PNEditor import PNEditor
from GUI.AuxDialogs import InputDialog, MoveDialog, SelectItemDialog, PredicateUpdater
from PNLab2PIPE import pnlab2pipe
from PIPE2PNLab import pipe2pnlab
class PNLab(object):
WORKSPACE_WIDTH = 600
WORKSPACE_HEIGHT = 600
EXPLORER_WIDTH = 250
def __init__(self):
super(PNLab, self).__init__()
self.root = tk.Tk()
self.root.wm_title('PNLab')
self.root.protocol("WM_DELETE_WINDOW", self.exit)
#Necessary in order for the children to expand to the real size of the window if resized:
self.root.rowconfigure(1, weight = 1)
self.root.columnconfigure(2, weight = 1)
toolbar_frame = tk.Frame(self.root)
toolbar_frame.grid(row = 0, column = 2, sticky = tk.E)
mode_label = tk.Label(toolbar_frame, text = 'mode: ')
mode_label.grid(row = 0, column = 0, sticky = tk.E)
self.mode_var = tk.StringVar()
mode_combo = ttk.Combobox(toolbar_frame,
values = ['Editor', 'Simulation', 'Execution'],
textvariable = self.mode_var,
state = 'readonly')
self.mode_var.set('Editor')
mode_combo.grid(row = 0, column = 1, sticky = tk.E)
project_frame = tk.Frame(self.root, width = PNLab.EXPLORER_WIDTH)
project_frame.grid(row = 1, column = 0, sticky = tk.NSEW)
project_frame.rowconfigure(0, weight = 1)
sep = ttk.Separator(self.root, orient = tk.VERTICAL)
sep.grid(row = 1, column = 1, sticky = tk.NS)
workspace_frame = tk.Frame(self.root, width = PNLab.WORKSPACE_WIDTH, height = PNLab.WORKSPACE_HEIGHT)
workspace_frame.grid(row = 1, column = 2, sticky = tk.NSEW)
#Necessary in order for the children to expand to the real size of the window if resized:
workspace_frame.rowconfigure(0, weight = 1)
workspace_frame.columnconfigure(0, weight = 1)
self.status_bar = tk.Frame(self.root, height = 20)
self.status_bar.grid(row = 2, columnspan=3, sticky = tk.EW)
self.status_var = tk.StringVar()
self.status_var.set('Ready.')
self.status_label = tk.Label(self.status_bar, textvariable = self.status_var)
self.status_label.grid(row = 0, column = 0, sticky = tk.EW)
self.project_tree = ttk.Treeview(project_frame, height = int((PNLab.WORKSPACE_HEIGHT - 20)/20), selectmode = 'browse')
self.project_tree.heading('#0', text='Project Explorer', anchor=tk.W)
self.project_tree.grid(row = 0, column = 0, sticky = tk.NSEW)
#ysb = ttk.Scrollbar(project_frame, orient='vertical', command=self.project_tree.yview)
xsb = ttk.Scrollbar(project_frame, orient='horizontal', command=self.project_tree.xview)
self.project_tree.configure(xscroll = xsb.set)#, yscroll = ysb.set)
#ysb.grid(row = 0, column = 1, sticky = tk.NS)
xsb.grid(row = 1, column = 0, sticky = tk.EW)
self.folder_img = tk.PhotoImage('folder_img', file = os.path.join(os.path.dirname(__file__), 'GUI', 'img', 'TreeView_Folder.gif'))
self.petri_net_img = tk.PhotoImage('petri_net_img', file = os.path.join(os.path.dirname(__file__), 'GUI', 'img', 'doc.gif'))
self.project_tree.tag_configure('folder', image = self.folder_img)
self.project_tree.tag_configure('petri_net', image = self.petri_net_img)
self.project_tree.insert('', 'end', 'Actions/', text = 'Actions/', tags = ['folder'], open = True)
self.project_tree.insert('', 'end', 'CommActions/', text = 'CommActions/', tags = ['folder'], open = True)
self.project_tree.insert('', 'end', 'Tasks/', text = 'Tasks/', tags = ['folder'], open = True)
self.project_tree.insert('', 'end', 'Environment/', text = 'Environment/', tags = ['folder'], open = True)
self.tab_manager = TabManager(workspace_frame,
width = PNLab.WORKSPACE_WIDTH,
height = PNLab.WORKSPACE_HEIGHT)
self.tab_manager.grid(row = 0, column = 0, sticky = tk.NSEW)
self.tab_manager.bind('<<NotebookTabChanged>>', self._set_string_var)
menubar = tk.Menu(self.root)
file_menu = tk.Menu(menubar, tearoff = False)
file_menu.add_command(label = 'Open', command = self.open)
file_menu.add_command(label="Save", command = self.save, accelerator = 'Ctrl+s')
file_menu.add_command(label="Save As...", command = self.save_as)
file_menu.add_separator()
file_menu.add_command(label="Exit", command = self.exit, accelerator = 'Ctrl+q', foreground = 'red', activeforeground = 'white', activebackground = 'red')
menubar.add_cascade(label = 'File', menu = file_menu)
analysis_menu = tk.Menu(menubar, tearoff = False)
analysis_menu.add_command(label="Set Predicate Init Values", command = self.update_predicates)
analysis_menu.add_command(label="Generate FullPetriNet", command = self.get_full_pn)
analysis_menu.add_command(label = 'ComputeMC', command = self.computeMC)
menubar.add_cascade(label = 'Analysis Tools', menu = analysis_menu)
'''
mode_menu = tk.Menu(menubar, tearoff = False)
mode_menu.add_command(label = 'Editing Mode')
mode_menu.add_command(label = 'Simulation Mode')
mode_menu.add_command(label = 'Execution Mode')
menubar.add_cascade(label = 'Set mode...', menu = mode_menu)
'''
self.root.config(menu = menubar)
self.popped_up_menu = None
self.petri_nets = {}
self.file_path = None
self.folder_menu = tk.Menu(self.root, tearoff = 0)
self.folder_menu.add_command(label = 'Add Petri Net', command = self.create_petri_net)
self.folder_menu.add_command(label = 'Import from Standard PNML', command = self.import_from_PNML)
self.folder_menu.add_command(label = 'Import from PIPE PNML', command = self.import_from_PIPE)
self.petri_net_menu = tk.Menu(self.root, tearoff = 0)
self.petri_net_menu.add_command(label = 'Open', command = self.open_petri_net)
self.petri_net_menu.add_command(label = 'Rename', command = self.rename_petri_net)
self.petri_net_menu.add_command(label = 'Move', command = self.move_petri_net)
self.petri_net_menu.add_command(label = 'Delete', command = self.delete_petri_net)
self.petri_net_menu.add_command(label = 'Export to Standard PNML', command = self.export_to_PNML)
self.petri_net_menu.add_command(label = 'Export to PIPE PNML', command = self.export_to_PIPE)
#MAC OS:
if (self.root.tk.call('tk', 'windowingsystem')=='aqua'):
self.project_tree.tag_bind('folder', '<2>', self.popup_folder_menu)
self.project_tree.tag_bind('folder', '<Control-1>', self.popup_folder_menu)
#Windows / UNIX / Linux:
else:
self.project_tree.tag_bind('folder', '<3>', self.popup_folder_menu)
#MAC OS:
if (self.root.tk.call('tk', 'windowingsystem')=='aqua'):
self.project_tree.tag_bind('petri_net', '<2>', self.popup_petri_net_menu)
self.project_tree.tag_bind('petri_net', '<Control-1>', self.popup_petri_net_menu)
#Windows / UNIX / Linux:
else:
self.project_tree.tag_bind('petri_net', '<3>', self.popup_petri_net_menu)
self.project_tree.tag_bind('petri_net', '<Double-1>', self.open_callback)
self.root.bind('<Button-1>', self._hide_menu)
self.root.bind('<Control-s>', self.save)
self.root.bind('<Control-q>', self.exit)
#######################################################
# TREE WIDGET, PNs AND TABS INTERACTIONS
#######################################################
def _set_string_var(self, event):
try:
tab_id = self.tab_manager.select()
if not tab_id:
return
except:
return
pne = self.tab_manager.widget_dict[tab_id]
self.status_label.configure(textvariable = pne.status_var)
pne.focus_set()
def popup_folder_menu(self, event):
self.clicked_element = self.project_tree.identify('item', event.x, event.y)
self.popped_up_menu = self.folder_menu
self.folder_menu.post(event.x_root, event.y_root)
def popup_petri_net_menu(self, event):
self.clicked_element = self.project_tree.identify('item', event.x, event.y)
self.popped_up_menu = self.petri_net_menu
self.petri_net_menu.post(event.x_root, event.y_root)
def _hide_menu(self, event):
"""Hides a popped-up menu."""
if self.popped_up_menu:
self.popped_up_menu.unpost()
self.popped_up_menu = None
return True
return False
def _adjust_width(self, text, item_id):
font = tkFont.Font()
measure = font.measure(text) + self._find_depth(item_id)*20
current_width = self.project_tree.column('#0', 'minwidth')
if measure > current_width:
self.project_tree.column('#0', minwidth = measure, stretch = True)
def _find_depth(self, item):
count = 0
while item != '':
item = self.project_tree.parent(item)
count += 1
return count
def _add_pne(self, pn, item_id, open_tab = True):
if isinstance(pn, PetriNet):
pne = PNEditor(self.tab_manager, PetriNet = pn)
else:
pne = PNEditor(self.tab_manager, name = pn)
self.petri_nets[item_id] = pne
if open_tab:
self.tab_manager.add(pne, text = pne.name)
self.tab_manager.select(pne)
return pne
def create_petri_net(self):
dialog = InputDialog('Petri Net name',
'Please input a Petri Net name, preferably composed only of alphabetic characters.',
'Name',
entry_length = 25)
dialog.window.transient(self.root)
self.root.wait_window(dialog.window)
if not dialog.value_set:
return
name = dialog.input_var.get()
item_id = self.clicked_element + name
try:
self.project_tree.insert(self.clicked_element, 'end', item_id, text = name, tags = ['petri_net'])
self._adjust_width(name, item_id)
except Exception as e:
tkMessageBox.showerror('ERROR', 'Petri Net could not be inserted in the selected node, possible duplicate name.\n\n' + str(e))
return
self._add_pne(name, item_id)
def open_callback(self, event):
self.clicked_element = self.project_tree.identify('item', event.x, event.y)
self.open_petri_net()
def open_petri_net(self):
pne = self.petri_nets[self.clicked_element]
try:
self.tab_manager.add(pne, text = pne.name)
except:
pass
self.tab_manager.select(pne)
def import_from_PNML(self):
filename = tkFileDialog.askopenfilename(
defaultextension = '.pnml',
filetypes=[('Standard PNML file', '*.pnml'), ('Standard PNML file', '*.pnml.xml')],
title = 'Open Standard PNML file...',
initialdir = os.path.expanduser('~/Desktop')
)
if not filename:
return
item_id = self._import_from_pnml(filename, self.clicked_element)
pne = self.petri_nets[item_id]
self.tab_manager.add(pne, text = pne.name)
self.tab_manager.select(pne)
def _import_from_pnml(self, filename, parent):
try:
petri_nets = PetriNet.from_pnml_file(filename)
except Exception as e:
tkMessageBox.showerror('Error reading PNML file.', 'An error occurred while reading the PNML file.\n\n' + str(e))
return
if len(petri_nets) > 1:
print 'warning: More than 1 petri net read, only 1 loaded.'
try:
pn = petri_nets[0]
except Exception as e:
tkMessageBox.showerror('Error loading PetriNet.', 'An error occurred while loading the PetriNet object.\n\n' + str(e))
name = pn.name
item_id = parent + name
try:
self.project_tree.insert(parent, 'end', item_id, text = name, tags = ['petri_net'])
self._adjust_width(name, item_id)
except Exception as e:
tkMessageBox.showerror('ERROR', 'Petri Net could not be inserted in the selected node, possible duplicate name.\n\n' + str(e))
return
pne = self._add_pne(pn, item_id, False)
pne.edited = False
return item_id
def import_from_PIPE(self):
filename = tkFileDialog.askopenfilename(
defaultextension = '.pnml.xml',
filetypes=[('PNML file', '*.pnml.xml'), ('PNML file', '*.pnml')],
title = 'Open PIPE PNML file...',
initialdir = os.path.expanduser('~/Desktop')
)
if not filename:
return
pn = None
try:
name = os.path.basename(filename)
if name[-5:] == '.pnml':
name = name[:-5]
if name[-9:] == '.pnml.xml':
name = name[:-9]
et = pipe2pnlab.convert(filename)
petri_nets = PetriNet.from_ElementTree(et, name)
except Exception as e:
tkMessageBox.showerror('Error reading PNML file.', 'An error occurred while reading the PNML file.\n\n' + str(e))
return
if len(petri_nets) > 1:
print 'warning: More than 1 petri net read, only 1 loaded.'
try:
pn = petri_nets[0]
except Exception as e:
tkMessageBox.showerror('Error loading PetriNet.', 'An error occurred while loading the PetriNet object.\n\n' + str(e))
parent = self.clicked_element
item_id = parent + name
try:
self.project_tree.insert(parent, 'end', item_id, text = name, tags = ['petri_net'])
self._adjust_width(name, item_id)
except Exception as e:
tkMessageBox.showerror('ERROR', 'Petri Net could not be inserted in the selected node, possible duplicate name.\n\n' + str(e))
return
pne = self._add_pne(pn, item_id, True)
pne.edited = False
def rename_petri_net(self):
old_name = self.project_tree.item(self.clicked_element, 'text')
dialog = InputDialog('Petri Net name',
'Please input a Petri Net name, preferably composed only of alphabetic characters.',
'Name',
value = old_name,
entry_length = 25)
dialog.window.transient(self.root)
self.root.wait_window(dialog.window)
if not dialog.value_set:
return
name = dialog.input_var.get()
parent = self.project_tree.parent(self.clicked_element)
self._move_petri_net(self.clicked_element, parent, parent, old_name, name)
def move_petri_net(self):
destination = self._get_destination(self.clicked_element)
name = self.project_tree.item(self.clicked_element, 'text')
old_parent = self.project_tree.parent(self.clicked_element)
if destination:
self._move_petri_net(self.clicked_element, old_parent, destination, name, name)
def _get_destination(self, item):
dialog = MoveDialog(self.project_tree, item, '')
dialog.window.transient(self.root)
self.root.wait_window(dialog.window)
return dialog.selection
def _move_petri_net(self, old_id, old_parent, parent, old_name, name):
item_id = parent + name
pne = self.petri_nets.pop(old_id)
try:
self.project_tree.insert(parent, 'end', item_id, text = name, tags = ['petri_net'])
self.project_tree.delete(old_id)
self._adjust_width(name, item_id)
pne.name = name
self.petri_nets[item_id] = pne
except Exception as e:
tkMessageBox.showerror('ERROR', 'Item could not be inserted in the selected node, possible duplicate name.\n\nERROR: ' + str(e))
try:
self.project_tree.insert(old_parent, 'end', old_id, text = old_name, tags = ['petri_net'])
except:
pass
self.petri_nets[old_id] = pne
return
try:
self.tab_manager.tab(pne, text = pne.name)
except:
pass
def delete_petri_net(self, item = None):
if not item:
item = self.clicked_element
pne = self.petri_nets.pop(item, None)
try:
self.tab_manager.forget(pne)
except:
pass
self.project_tree.delete(item)
def export_to_PNML(self):
filename = tkFileDialog.asksaveasfilename(
defaultextension = '.pnml',
filetypes=[('Standard PNML file', '*.pnml')],
title = 'Save as Standard PNML file...',
initialdir = os.path.dirname(self.file_path) if self.file_path is not None else os.path.expanduser('~/Desktop'),
initialfile = os.path.basename(self.clicked_element) + '.pnml'
)
if not filename:
return
try:
self.petri_nets[self.clicked_element].petri_net.to_pnml_file(filename)
except Exception as e:
tkMessageBox.showerror('Error saving PNML file.', 'An error occurred while saving the PNML file.\n\n' + str(e))
def export_to_PIPE(self):
filename = tkFileDialog.asksaveasfilename(
defaultextension = '.pnml.xml',
filetypes=[('PIPE PNML file', '*.pnml.xml')],
title = 'Save as PIPE PNML file...',
initialdir = os.path.dirname(self.file_path) if self.file_path is not None else os.path.expanduser('~/Desktop'),
initialfile = os.path.basename(self.clicked_element) + '.pnml.xml'
)
if not filename:
return
try:
et = self.petri_nets[self.clicked_element]._petri_net.to_ElementTree()
et = pnlab2pipe.convert(et)
et.write(filename, encoding = 'utf-8', xml_declaration = True, pretty_print = True)
except Exception as e:
tkMessageBox.showerror('Error saving PNML file.', 'An error occurred while saving the PNML file.\n\n' + str(e))
#######################################################
# FILE MENU ACTIONS
#######################################################
def open(self):
zip_filename = tkFileDialog.askopenfilename(
defaultextension = '.rpnp',
filetypes=[('Robotic Petri Net Plan file', '*.rpnp')],
title = 'Open RPNP file...',
initialdir = os.path.expanduser('~/Desktop')
)
if not zip_filename:
return
items = self.project_tree.get_children('Actions/') \
+ self.project_tree.get_children('CommActions/') \
+ self.project_tree.get_children('Tasks/') \
+ self.project_tree.get_children('Environment/')
for i in items:
self.delete_petri_net(i)
self.file_path = zip_filename
zip_file = zipfile.ZipFile(self.file_path, 'r')
tmp_dir = tempfile.mkdtemp()
for x in zip_file.infolist():
prev_sep = -1
sep_index = x.filename.find('/', 0)
while sep_index > -1:
current_dir = x.filename[:sep_index + 1]
parent = x.filename[:prev_sep + 1]
if not self.project_tree.exists(current_dir):
name = current_dir[current_dir[:-1].rfind('/') + 1:]
self.project_tree.insert(parent, 'end', current_dir, text = name, tags = ['folder'], open = True)
self._adjust_width(name, current_dir)
prev_sep = sep_index
sep_index = x.filename.find('/', sep_index + 1)
if x.filename[-5:] == '.pnml':
last_sep = x.filename.rfind('/') + 1
filename = x.filename[last_sep:]
parent = x.filename[:last_sep]
file_path = os.path.join(tmp_dir, filename)
f = open(file_path, 'w')
data = zip_file.read(x)
f.write(data)
f.close()
self._import_from_pnml(file_path, parent)
os.remove(file_path)
os.rmdir(tmp_dir)
zip_file.close()
self.status_var.set('Opened: ' + self.file_path)
def save(self, event = None):
if not self.file_path:
self.save_as()
return
try:
zip_file = zipfile.ZipFile(self.file_path, "w")
except:
tkMessageBox.showerror('Error opening file.', 'A problem ocurred while opening a file for writing, make sure the file is not open by other program before saving.')
return
tmp_dir = tempfile.mkdtemp()
folders = ('Actions/', 'CommActions/', 'Tasks/', 'Environment/')
for f in folders:
children = self.project_tree.get_children(f)
if not children:
file_path = os.path.join(tmp_dir, f)
os.mkdir(file_path)
zip_file.write(file_path, f)
os.rmdir(file_path)
continue
for current in children:
path = current + '.pnml'
pne = self.petri_nets[current]
file_name = os.path.basename(path)
file_path = os.path.join(tmp_dir, file_name)
pne._petri_net.to_pnml_file(file_path)
zip_file.write(file_path, path)
pne.edited = False
os.remove(file_path)
os.rmdir(tmp_dir)
zip_file.close()
try:
tab_id = self.tab_manager.select()
if not tab_id:
raise Exception()
except:
self.status_label.configure(textvariable = self.status_var)
self.status_var.set('File saved: ' + self.file_path)
return
pne = self.tab_manager.widget_dict[tab_id]
pne.status_var.set('File saved: ' + self.file_path)
def save_as(self):
zip_filename = tkFileDialog.asksaveasfilename(
defaultextension = '.rpnp',
filetypes=[('Robotic Petri Net Plan file', '*.rpnp')],
title = 'Save as RPNP file...',
initialdir = os.path.dirname(self.file_path) if self.file_path is not None else os.path.expanduser('~/Desktop'),
initialfile = os.path.basename(self.file_path) if self.file_path is not None else ''
)
if not zip_filename:
return
self.file_path = zip_filename
self.save()
def exit(self, event = None):
edited = False
for pne in self.petri_nets.itervalues():
if pne.edited:
edited = True
break
if edited:
if not tkMessageBox.askokcancel('Exit without saving?', 'Are you sure you want to quit without saving any changes?', default = tkMessageBox.CANCEL):
return
self.root.destroy()
#######################################################
# ANALYSIS MENU ACTIONS
#######################################################
def update_predicates(self):
folders = ('Actions/', 'CommActions/', 'Tasks/', 'Environment/')
preds = {}
for f in folders:
children = self.project_tree.get_children(f)
for current in children:
pne = self.petri_nets[current]
for p in pne._petri_net.places.itervalues():
if p.type == PlaceTypes.PREDICATE and p.name not in preds:
preds[p.name] = int((p.init_marking > 0) != p._isNegated)
dialog = PredicateUpdater(preds)
dialog.window.transient(self.root)
self.root.wait_window(dialog.window)
if not dialog.value_set:
return False
for f in folders:
children = self.project_tree.get_children(f)
for current in children:
pne = self.petri_nets[current]
for p in pne._petri_net.places.itervalues():
if p.type == PlaceTypes.PREDICATE:
p.init_marking = int((dialog.preds[p.name].get() == 'True') != p._isNegated)
pne._draw_petri_net()
return True
def get_full_pn(self):
dialog = SelectItemDialog(self.project_tree, None, 'Tasks/')
dialog.window.transient(self.root)
self.root.wait_window(dialog.window)
if not dialog.selection:
return
file_location = tkFileDialog.askdirectory(
title = 'Save Full Petri Net in...',
initialdir = os.path.dirname(self.file_path) if self.file_path is not None else os.path.expanduser('~/Desktop'),
parent = self.root,
mustexist = True
)
if not file_location:
return
tmp_dir = tempfile.mkdtemp()
#print tmp_dir
folders = ('Actions/', 'CommActions/', 'Tasks/', 'Environment/')
if not self.update_predicates():
return
root_pred = ET.Element('AvailablePredicates')
predicates_tree = ET.ElementTree(root_pred)
added_predicates = set()
for f in folders:
dir_path = os.path.join(tmp_dir, f)
os.mkdir(dir_path)
root_models = ET.Element('AvailablePetriNetModels')
models_tree = ET.ElementTree(root_models)
children = self.project_tree.get_children(f)
for current in children:
path = current + '.pnml'
pne = self.petri_nets[current]
model = ET.SubElement(root_models, 'PetriNetModel')
tmp = ET.SubElement(model, 'FilePath')
tmp.text = os.path.basename(path)
for p in pne._petri_net.places.itervalues():
if p.type == PlaceTypes.PREDICATE and p.name not in added_predicates:
added_predicates.add(p.name)
predicate = ET.SubElement(root_pred, 'Predicate')
tmp = ET.SubElement(predicate, 'Name')
tmp.text = p.name
tmp = ET.SubElement(predicate, 'InitialMarking')
tmp.text = str(int(p.init_marking != p._isNegated))
tmp = ET.SubElement(predicate, 'Comment')
tmp.text = '...'
if f == 'Actions/':
if p._isRunningCondition:
tmp = ET.SubElement(model, 'RunningCondition')
tmp.text = ('NOT_' if p._isNegated else '') + p.name
if p._isEffect:
tmp = ET.SubElement(model, 'DesiredEffect')
tmp.text = ('NOT_' if p._isNegated else '') + p.name
file_path = os.path.join(tmp_dir, path)
et = pne._petri_net.to_ElementTree()
et = pnlab2pipe.convert(et)
et.write(file_path, encoding = 'utf-8', xml_declaration = True)
models_tree.write(os.path.join(tmp_dir, f, 'AvailableModels.xml'), encoding = 'utf-8', xml_declaration = True, pretty_print=True)
predicates_tree.write(os.path.join(tmp_dir, 'PredicatesList.xml'), encoding = 'utf-8', xml_declaration = True, pretty_print=True)
dir_path = os.path.join(tmp_dir, 'FullPetriNets/')
os.mkdir(dir_path)
path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(path, 'Analysis_tools', 'expandNet')
task = os.path.join(tmp_dir, dialog.selection + '.pnml')
call([path, task])
try:
os.remove('PetriNetFramework.log')
except:
pass
for f in folders:
children = self.project_tree.get_children(f)
for current in children:
path = current + '.pnml'
file_path = os.path.join(tmp_dir, path)
os.remove(file_path)
os.remove(os.path.join(tmp_dir, f, 'AvailableModels.xml'))
dir_path = os.path.join(tmp_dir, f)
os.rmdir(dir_path)
final_filename = os.path.basename(dialog.selection) + '_full.pnml'
call(['mv', os.path.join(tmp_dir, 'FullPetriNets', final_filename), os.path.join(file_location, final_filename + '.xml')])
print 'Moved file to: ' + os.path.join(file_location, final_filename)
os.remove(os.path.join(tmp_dir, 'PredicatesList.xml'))
dir_path = os.path.join(tmp_dir, 'FullPetriNets')
os.rmdir(dir_path)
os.rmdir(tmp_dir)
def computeMC(self):
filename = tkFileDialog.askopenfilename(
title = 'Compute MC in...',
initialdir = os.path.dirname(self.file_path) if self.file_path is not None else os.path.expanduser('~/Desktop')
)
if not filename:
return
path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(path, 'Analysis_tools', 'computeMC')
call([path, '-s', filename])
if __name__ == '__main__':
w = PNLab()
tk.mainloop()