-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypothesis.py
285 lines (257 loc) · 10.1 KB
/
hypothesis.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
import math
import timeInterval as timeInterval
class OTA(object):
def __init__(self, inputs, states, trans, initState, acceptStates, sinkState):
self.inputs = inputs
self.states = states
self.trans = trans
self.initState = initState
self.acceptStates = acceptStates
self.sinkState = sinkState
def max_time_value(self):
max_time_value = 0
for tran in self.trans:
for c in tran.guards:
if c.max_value == '+':
temp_max_value = int(c.min_value)
else:
temp_max_value = int(c.max_value)
if max_time_value < temp_max_value:
max_time_value = temp_max_value
return max_time_value
def showDiscreteOTA(self):
print("Input: " + str(self.inputs))
print("States: " + str(self.states))
print("InitState: {}".format(self.initState))
print("AcceptStates: {}".format(self.acceptStates))
print("SinkState: {}".format(self.sinkState))
print("Transitions: ")
for t in self.trans:
print(' ' + str(t.tranId), 'S_' + str(t.source), str(t.input), str(t.timePoint), str(t.isReset), 'S_' + str(t.target), end="\n")
def showOTA(self):
print("Input: " + str(self.inputs))
print("States: " + str(self.states))
print("InitState: {}".format(self.initState))
print("AcceptStates: {}".format(self.acceptStates))
print("SinkState: {}".format(self.sinkState))
print("Transitions: ")
for t in self.trans:
print(" " + str(t.tranId), 'S_' + str(t.source), str(t.input), t.showGuards(), str(t.isReset), 'S_' + str(t.target), end="\n")
class DiscreteOTATran(object):
def __init__(self, tranId, source, input, timePoint, isReset, target):
self.tranId = tranId
self.source = source
self.input = input
self.timePoint = timePoint
self.isReset = isReset
self.target = target
class OTATran(object):
def __init__(self, tranId, source, input, guards, isReset, target):
self.tranId = tranId
self.source = source
self.input = input
self.guards = guards
self.isReset = isReset
self.target = target
def isPass(self, ltw):
if ltw.input == self.input:
for guard in self.guards:
if guard.isInInterval(ltw.time):
return True
else:
return False
return False
def showGuards(self):
temp = self.guards[0].show()
for i in range(1, len(self.guards)):
temp = temp + 'U' + self.guards[i].show()
return temp
# build DiscreteOTA (DFA)
def structDiscreteOTA(table, inputs):
inputs = inputs
states = []
initState = None
sinkState = None
acceptStates = []
valueList_name_dict = {}
for s, i in zip(table.S, range(0, len(table.S))):
stateName = i
valueList_name_dict[makeStr(s.valueList)] = stateName
states.append(stateName)
if not s.LRTWs:
initState = stateName
if s.valueList[0][0] == 1:
acceptStates.append(stateName)
if s.valueList[0][0] == -1:
sinkState = stateName
# deal with trans
trans = []
transNum = 0
tableElements = [s for s in table.S] + [r for r in table.R]
source = None
target = None
for r in tableElements:
if not r.LRTWs:
continue
timedWords = [lrtw for lrtw in r.LRTWs]
w = timedWords[:-1]
a = timedWords[len(timedWords) - 1]
for element in tableElements:
if isEqual(w, element.LRTWs):
source = valueList_name_dict[makeStr(element.valueList)]
if isEqual(timedWords, element.LRTWs):
target = valueList_name_dict[makeStr(element.valueList)]
# 确认迁移input
input = a.input
timePoint = a.time
isReset = a.isReset
# 添加新迁移还是添加时间点
needNewTran = True
for tran in trans:
if source == tran.source and input == tran.input and target == tran.target and isReset == tran.isReset:
if timePoint == tran.timePoint:
needNewTran = False
break
if needNewTran:
tempTran = DiscreteOTATran(transNum, source, input, timePoint, isReset, target)
trans.append(tempTran)
transNum = transNum + 1
discreteOTA = OTA(inputs, states, trans, initState, acceptStates, sinkState)
return discreteOTA
# build hypothesis
def structHypothesisOTA(discreteOTA):
inputs = discreteOTA.inputs
states = discreteOTA.states
initState = discreteOTA.initState
acceptStates = discreteOTA.acceptStates
sinkState = discreteOTA.sinkState
# deal with trans
trans = []
for s in discreteOTA.states:
s_dict = {}
for key in discreteOTA.inputs:
s_dict[key] = [0]
for tran in discreteOTA.trans:
if tran.source == s:
for input in discreteOTA.inputs:
if tran.input == input:
tempList = s_dict[input]
if tran.timePoint not in tempList:
tempList.append(tran.timePoint)
s_dict[input] = tempList
for value in s_dict.values():
value.sort()
for tran in discreteOTA.trans:
if tran.source == s:
timePoints = s_dict[tran.input]
guards = []
tw = tran.timePoint
index = timePoints.index(tw)
if index + 1 < len(timePoints):
if isInt(tw) and isInt(timePoints[index + 1]):
tempGuard = timeInterval.Guard("[" + str(tw) + "," + str(timePoints[index + 1]) + ")")
elif isInt(tw) and not isInt(timePoints[index + 1]):
tempGuard = timeInterval.Guard("[" + str(tw) + "," + str(math.modf(timePoints[index + 1])[1]) + "]")
elif not isInt(tw) and isInt(timePoints[index + 1]):
tempGuard = timeInterval.Guard("(" + str(math.modf(tw)[1]) + "," + str(timePoints[index + 1]) + ")")
else:
tempGuard = timeInterval.Guard("(" + str(math.modf(tw)[1]) + "," + str(math.modf(timePoints[index + 1])[1]) + "]")
guards.append(tempGuard)
else:
if isInt(tw):
tempGuard = timeInterval.Guard("[" + str(tw) + ",+)")
else:
tempGuard = timeInterval.Guard("(" + str(math.modf(tw)[1]) + ",+)")
guards.append(tempGuard)
for guard in guards:
tempTran = OTATran(tran.tranId, tran.source, tran.input, [guard], tran.isReset, tran.target)
trans.append(tempTran)
hypothesisOTA = OTA(inputs, states, trans, initState, acceptStates, sinkState)
return hypothesisOTA
# build simple hypothesis - merge guards
def structSimpleHypothesis(hypothesis):
inputs = hypothesis.inputs
states = hypothesis.states
initState = hypothesis.initState
acceptStates = hypothesis.acceptStates
sinkState = hypothesis.sinkState
trans = []
tranNum = 0
for s in hypothesis.states:
for t in hypothesis.states:
for input in inputs:
for reset in [True, False]:
temp = []
for tran in hypothesis.trans:
if tran.source == s and tran.input == input and tran.target == t and tran.isReset == reset:
temp.append(tran)
if temp:
guards = []
for i in temp:
guards += i.guards
guards = simpleGuards(guards)
trans.append(OTATran(tranNum, s, input, guards, reset, t))
tranNum += 1
return OTA(inputs, states, trans, initState, acceptStates, sinkState)
# --------------------------------- auxiliary function ---------------------------------
# valueList改为str
def makeStr(valueList):
temp = []
for v in valueList:
temp.append(v[0])
temp += v[1]
result = ','.join(str(i) for i in temp)
return result
# Determine whether two LRTWs are the same
def isEqual(LRTWs1, LRTWs2):
if len(LRTWs1) != len(LRTWs2):
return False
else:
flag = True
for i in range(len(LRTWs1)):
if LRTWs1[i] != LRTWs2[i]:
flag = False
break
if flag:
return True
else:
return False
# 判断是否整数
def isInt(num):
x, y = math.modf(num)
if x == 0:
return True
else:
return False
# Sort guards
def sortGuards(guards):
for i in range(len(guards) - 1):
for j in range(len(guards) - i - 1):
if guards[j].max_bn > guards[j + 1].max_bn:
guards[j], guards[j + 1] = guards[j + 1], guards[j]
return guards
# Merge guards
def simpleGuards(guards):
if len(guards) == 1 or len(guards) == 0:
return guards
else:
sortedGuards = sortGuards(guards)
result = []
tempGuard = sortedGuards[0]
for i in range(1, len(sortedGuards)):
firstRight = tempGuard.max_bn
secondLeft = sortedGuards[i].min_bn
if float(firstRight.value) == float(secondLeft.value):
if (firstRight.bracket == 1 and secondLeft.bracket == 2) or (firstRight.bracket == 3 and secondLeft.bracket == 4):
left = tempGuard.guard.split(',')[0]
right = sortedGuards[i].guard.split(',')[1]
guard = timeInterval.Guard(left + ',' + right)
tempGuard = guard
elif firstRight.bracket == 1 and secondLeft.bracket == 4:
result.append(tempGuard)
tempGuard = sortedGuards[i]
else:
result.append(tempGuard)
tempGuard = sortedGuards[i]
result.append(tempGuard)
return result