forked from jar-ben/tamus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_automata.py
146 lines (114 loc) · 5.08 KB
/
fix_automata.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
def fix_automata(file_path, clocks, parameters, discrete_variables, n=1):
r_file = open(file_path, 'r')
w_file = open(file_path[0:-4] + '_fixed.xml', 'w')
for line in r_file:
if "label kind=\"invariant\"" in line or "label kind=\"guard\"" in line:
constraints_start = line.find('>') + 1
constraints_end = line.find('<', constraints_start)
constraints = line[constraints_start: constraints_end]
constraints = constraints.split('&&')
w_file.write(line[:constraints_start])
result = []
for c in constraints:
if '>' in c:
t_start = c.find('>')
if c[t_start+4] == '=':
atomic = c.split('>=')
relation = '>='
else:
atomic = c.split('>')
relation = '>'
clock, threshold, change_order = find_atomics(atomic[0], atomic[1], clocks + discrete_variables,
parameters, n)
if change_order:
relation = '&l' + relation[2:]
c = clock + ' ' + relation + ' ' + threshold
result.append(c)
elif '<' in c:
t_start = c.find('<')
if c[t_start + 4] == '=':
atomic = c.split('<=')
relation = '<='
else:
atomic = c.split('<')
relation = '<'
clock, threshold, change_order = find_atomics(atomic[0], atomic[1], clocks + discrete_variables,
parameters, n)
if change_order:
relation = '&l' + relation[2:]
c = clock + ' ' + relation + ' ' + threshold
result.append(c)
elif '=' in c:
t_start = c.find('=')
if c[t_start + 1] == '=':
atomic = c.split('==')
else:
atomic = c.split('=')
clock, threshold, _ = find_atomics(atomic[0], atomic[1], clocks + discrete_variables, parameters, n)
c1 = clock + ' ' + '<=' + ' ' + threshold
c2 = clock + ' ' + '>=' + ' ' + threshold
result.append(c1)
result.append(c2)
w_file.write('&&'.join(result))
w_file.write(line[constraints_end:])
else:
w_file.write(line)
def find_atomics(left_hand_side, right_hand_side, clocks, parameters, n):
left_hand_side = left_hand_side.split('*')
right_hand_side = right_hand_side.split('*')
change_order = 0
for i in range(len(left_hand_side)):
left_hand_side[i] = left_hand_side[i].strip()
for i in range(len(right_hand_side)):
right_hand_side[i] = right_hand_side[i].strip()
if len(left_hand_side) == 1 and left_hand_side[0] in clocks:
clock = left_hand_side[0]
threshold = right_hand_side
coefficient = 1
elif len(left_hand_side) == 2 and left_hand_side[0] in clocks:
clock = left_hand_side[0]
threshold = right_hand_side
coefficient = int(left_hand_side[1])
elif len(left_hand_side) == 2 and left_hand_side[1] in clocks:
clock = left_hand_side[1]
threshold = right_hand_side
coefficient = int(left_hand_side[0])
elif len(right_hand_side) == 1 and right_hand_side[0] in clocks:
clock = right_hand_side[0]
threshold = left_hand_side
coefficient = 1
change_order = 1
elif len(right_hand_side) == 2 and right_hand_side[0] in clocks:
clock = right_hand_side[0]
threshold = left_hand_side
coefficient = int(right_hand_side[1])
change_order = 1
elif len(right_hand_side) == 2 and right_hand_side[1] in clocks:
clock = right_hand_side[1]
threshold = left_hand_side
coefficient = int(right_hand_side[0])
change_order = 1
else:
if len(left_hand_side) == 1 and left_hand_side[0] not in parameters:
clock = left_hand_side[0]
threshold = right_hand_side
coefficient = 1
else:
clock = right_hand_side[0]
threshold = left_hand_side
coefficient = 1
coefficient_p = 1
if len(threshold) == 1:
if threshold[0] in parameters:
threshold = parameters[threshold[0]]
else:
threshold = threshold[0]
else:
if threshold[0] in parameters:
coefficient_p = int(threshold[1])
threshold = parameters[threshold[0]]
else:
coefficient_p = int(threshold[0])
threshold = parameters[threshold[1]]
threshold = int(threshold) * float(n) * coefficient_p / coefficient
return clock, str(int(threshold)), change_order