-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
48 lines (37 loc) · 1.53 KB
/
example.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
"""
Loads example fault trees which have the entire FT in a single file.
"""
import os
from pathlib import Path
from ft_2_quantum_sat.fault_tree import FaultTree
image_folder = 'images/'
def image_name(filepath):
"""
Get a name for the image from the file path (models/ABC/abc.xml -> abc.png).
"""
file_name = os.path.basename(filepath)
return image_folder + file_name[:-4] + '.png'
def analyze_fault_tree(filepath, m=1, method='classical'):
"""
Compute `m` cutsets from the given fault tree (as XML file).
"""
# load the fault tree
print(f"Loading {filepath}... ", end='')
ft = FaultTree.load_from_xml(filepath)
print(f"(has {ft.number_of_nodes()} nodes)")
# vizualize
ft.save_as_image(image_name(filepath))
# compute minimal cutsets
print(f"Computing the {m} smallest minimal cut sets with method = '{method}'...")
cutsets = ft.compute_min_cutsets(m, method)
print(f"{cutsets}\n")
if __name__ == '__main__':
Path(image_folder).mkdir(parents=True, exist_ok=True)
# Analyze FTs (not all FTs can be loaded because they have e.g. this
# <!-- Transfer-In --> thing which the current parser doesn't deal with)
analyze_fault_tree('models/Theatre/theatre.xml', m=2)
analyze_fault_tree('models/Theatre/theatre.xml', m=2, method='grover')
analyze_fault_tree('models/SmallTree/SmallTree.xml', m=2)
analyze_fault_tree('models/BSCU/BSCU.xml', m=3)
analyze_fault_tree('models/Lift/lift.xml', m=5)
analyze_fault_tree('models/Lift/lift.xml', m=5, method='min-sat')