-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsTable.py
292 lines (254 loc) · 9.67 KB
/
obsTable.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
import math
from teacher import TQs
from timedWord import TimedWord, DRTW_to_LRTW, LRTW_to_LTW, ResetTimedWord
class ObsTable(object):
def __init__(self, S, R, E):
self.S = S
self.R = R
self.E = E
def show(self):
print("new_E:" + str(len(self.E)))
for e in self.E:
print([ltw.show() for ltw in e])
print("new_S:" + str(len(self.S)))
for s in self.S:
print([lrtw.show() for lrtw in s.LRTWs], s.valueList)
print("new_R:" + str(len(self.R)))
for r in self.R:
print([lrtw.show() for lrtw in r.LRTWs], r.valueList)
class Element(object):
def __init__(self, LRTWs, valueList):
self.LRTWs = LRTWs
self.valueList = valueList # [value,[resetList]]
# init observation table
def initTable(inputs, targetSys, mqNum):
table = ObsTable([], [], [])
# deal with E
table.E.append([])
# deal with S
element, mqNum = fillTableRow([], table, False, targetSys, mqNum)
table.S.append(element)
# deal with R
table, mqNum = extendR(table.S[0], inputs, table, targetSys, mqNum)
return table, mqNum
# Expand into a row according to s (根据s扩充成一行)
def fillTableRow(LRTWs, table, flag, targetSys, mqNum):
# flag判断是否进入Error状态或查询是否有效 - flag为True表示已进入Error状态或无效
if flag:
valueList = []
for e in table.E:
value = [-1, [True for i in range(len(e))]]
valueList.append(value)
element = Element(LRTWs, valueList)
else:
valueList = []
if isValid(LRTWs):
for e in table.E:
LTWs = LRTW_to_LTW(LRTWs) + e
tempLRTWs, tempValue, mqNum = TQs(LTWs, targetSys, mqNum)
if len(e) != 0:
resetList = getResetList(tempLRTWs, e)
value = [tempValue, resetList]
else:
value = [tempValue, []]
valueList.append(value)
else:
for e in table.E:
value = [-1, [True for i in range(len(e))]]
valueList.append(value)
element = Element(LRTWs, valueList)
return element, mqNum
# S adds s and table expands R area (S添加s,扩展R区域)
def extendR(s, inputs, table, targetSys, mqNum):
tableTrace = [s.LRTWs for s in table.S] + [r.LRTWs for r in table.R]
for input in inputs:
LTWs = LRTW_to_LTW(s.LRTWs) + [TimedWord(input, 0)]
LRTWs, tempValue, mqNum = TQs(LTWs, targetSys, mqNum)
if tempValue == -1:
element, mqNum = fillTableRow(LRTWs, table, True, targetSys, mqNum)
else:
element, mqNum = fillTableRow(LRTWs, table, False, targetSys, mqNum)
if element.LRTWs not in tableTrace:
table.R.append(element)
return table, mqNum
# Determine whether table is prepared
def isPrepared(table):
flagClosed, closedMove = isClosed(table)
flagConsistent, consistentAdd = isConsistent(table)
if flagClosed and flagConsistent:
return True
else:
return False
# Determine whether it is closed
def isClosed(table):
closedMove = []
for r in table.R:
flag = False
for s in table.S:
if r.valueList == s.valueList:
flag = True
break
if not flag:
if elementNotInList(r.valueList, closedMove):
closedMove.append(r)
if len(closedMove) > 0:
return False, closedMove
else:
return True, closedMove
# make closed
def makeClosed(table, inputs, closedMove, targetSys, mqNum):
# close_move将其从R移至S
for r in closedMove:
table.S.append(r)
table.R.remove(r)
# 处理R
for i in closedMove:
table, mqNum = extendR(i, inputs, table, targetSys, mqNum)
return table, mqNum
# Determine whether it is consistent
def isConsistent(table):
flag = True
consistentAdd = []
tableElement = [s for s in table.S] + [r for r in table.R]
for i in range(0, len(tableElement) - 1):
for j in range(i + 1, len(tableElement)):
if tableElement[i].valueList == tableElement[j].valueList:
tempElements1 = []
tempElements2 = []
for element in tableElement:
if isPrefix(element.LRTWs, tableElement[i].LRTWs):
tempElements1.append(element)
if isPrefix(element.LRTWs, tableElement[j].LRTWs):
tempElements2.append(element)
for e1 in tempElements1:
for e2 in tempElements2:
newLRTWs1 = deletePrefix(e1.LRTWs, tableElement[i].LRTWs)
newLRTWs2 = deletePrefix(e2.LRTWs, tableElement[j].LRTWs)
if len(newLRTWs1) == len(newLRTWs2) == 1:
if newLRTWs1[0].input == newLRTWs2[0].input and newLRTWs1[0].time == newLRTWs2[0].time:
if newLRTWs1[0].isReset != newLRTWs2[0].isReset:
flag = False
temp = TimedWord(newLRTWs1[0].input, newLRTWs1[0].time)
consistentAdd = [temp]
return flag, consistentAdd
elif e1.valueList != e2.valueList:
flag = False
for k in range(0, len(e1.valueList)):
if e1.valueList[k] != e2.valueList[k]:
newIndex = k
consistentAdd = [TimedWord(newLRTWs1[0].input, newLRTWs1[0].time)] + table.E[newIndex]
return flag, consistentAdd
return flag, consistentAdd
# make consistent
def makeConsistent(table, consistentAdd, targetSys, mqNum):
table.E.append(consistentAdd)
for i in range(0, len(table.S)):
if isValid(table.S[i].LRTWs):
LTWs = LRTW_to_LTW(table.S[i].LRTWs) + consistentAdd
LRTWs, tempValue, mqNum = TQs(LTWs, targetSys, mqNum)
value = [tempValue, getResetList(LRTWs, consistentAdd)]
table.S[i].valueList.append(value)
else:
value = [-1, [True for m in range(len(consistentAdd))]]
table.S[i].valueList.append(value)
for j in range(0, len(table.R)):
if isValid(table.R[j].LRTWs):
LTWs = LRTW_to_LTW(table.R[j].LRTWs) + consistentAdd
LRTWs, tempValue, mqNum = TQs(LTWs, targetSys, mqNum)
value = [tempValue, getResetList(LRTWs, consistentAdd)]
table.R[j].valueList.append(value)
else:
value = [-1, [True for n in range(len(consistentAdd))]]
table.R[j].valueList.append(value)
return table, mqNum
# deal with ctx (添加反例的所有前缀集)
def dealCtx(table, ctx, targetSys, mqNum):
newCtx = DRTW_to_LRTW(ctx)
newCtx = normalize(newCtx)
pref = prefixes(newCtx)
LRTWsList = [s.LRTWs for s in table.S] + [r.LRTWs for r in table.R]
for LRTWs in pref:
needAdd = True
for tempLRTWs in LRTWsList:
if LRTWs == tempLRTWs:
needAdd = False
break
if needAdd:
tempElement, mqNum = fillTableRow(LRTWs, table, False, targetSys, mqNum)
table.R.append(tempElement)
return table, mqNum
# --------------------------------- 辅助函数 ---------------------------------
# time normalization
def normalize(trace):
newTrace = []
for i in trace:
if math.modf(float(i.time))[0] == 0.0:
time = math.modf(float(i.time))[1]
else:
time = math.modf(float(i.time))[1] + 0.5
newTrace.append(ResetTimedWord(i.input, time, i.isReset))
return newTrace
# determine whether LRTWs are valid (LRTWs是否有效)
def isValid(LRTWs):
if not LRTWs:
return True
else:
nowTime = 0
for lrtw in LRTWs:
if lrtw.time >= nowTime:
if lrtw.isReset:
nowTime = 0
else:
nowTime = lrtw.time
else:
return False
return True
# get corresponding resetList in e (获得E中对应的resetList)
def getResetList(LRTWs, e):
restList = []
tempLRTWs = LRTWs[-len(e):]
for i in tempLRTWs:
restList.append(i.isReset)
return restList
# 判断Element的List中是否已有相同valueList的元素,若存在返回False,不存在则返回True
def elementNotInList(valueList, elementList):
if len(elementList) == 0:
return True
else:
flag = True
for i in range(len(elementList)):
if valueList == elementList[i].valueList:
flag = False
break
if flag:
return True
else:
return False
# determine whether pre is the prefix of tws (判断pre是否是tws的前缀)
def isPrefix(tws, pre):
if len(pre) == 0:
return True
else:
if len(tws) < len(pre):
return False
else:
for i in range(0, len(pre)):
if tws[i] == pre[i]:
pass
else:
return False
return True
# remove prefix pre (删除tws的前缀pre)
def deletePrefix(tws, pre):
if len(pre) == 0:
return tws
else:
new_tws = tws[len(pre):]
return new_tws
# prefix set of tws (tws前缀集)
def prefixes(tws):
newPrefixes = []
for i in range(1, len(tws) + 1):
temp_tws = tws[:i]
newPrefixes.append(temp_tws)
return newPrefixes