forked from hst/old-cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scratch.py
312 lines (241 loc) · 8.48 KB
/
scratch.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
sEMPTY = frozenset()
class Event(object):
def __init__(self, name):
self.name = name
TICK = Event("tick")
sTICK = frozenset((TICK,))
TAU = Event("tau")
sTAU = frozenset((TAU,))
class Process(object):
def __init__(self):
self.cached_inits = None
self.cached_after = {}
def inits(self):
if self.cached_inits is None:
self.cached_inits = self._inits()
return self.cached_inits
def after(self, event):
try:
return self.cached_after[event]
except KeyError:
result = self.after(event)
self.cached_after[event] = result
return result
def _inits(self):
pass
def _after(self, event):
pass
class Stop(Process):
def _inits(self):
return sEMPTY
def _after(self, event):
return sEMPTY
STOP = Stop()
sSTOP = frozenset(STOP)
class Skip(Process):
def _inits(self):
return sTICK
def _after(self, event):
if event is TICK:
return sSTOP
else:
return sEMPTY
SKIP = Skip()
sSKIP = frozenset(SKIP)
class Compose(Process):
def __init__(self, left, right):
Process.__init__(self)
self.left = left
self.right = right
self.inits = None
def _inits(self):
if self.inits is None:
l_inits = self.left.inits()
if TICK not in l_inits:
self.inits = l_inits
else:
self.inits = (l_inits - sTICK) | sTAU
return self.inits
def _after(self, event):
result = set()
if event is not TICK:
result.update(Compose(l, self.right)
for l in self.left.after(event))
if event is TAU and TICK in self.left.inits():
result.add(self.right)
return frozenset(result)
class Timeout(Process):
def __init__(self, left, right):
Process.__init__(self)
self.left = left
self.right = right
def _inits(self):
return self.left.inits() | self.right.inits()
def _after(self, event):
result = set()
result.update(Timeout(l, self.right)
for l in self.left.after(event))
if event is TAU:
result.update(Timeout(self.left, r)
for r = self.right.after(event))
else:
result.update(self.right.after(event))
return frozenset(result)
class IntChoice(Process):
def __init__(self, left, right):
Process.__init__(self)
self.left = left
self.right = right
def _inits(self):
return sTAU
def _after(self, event):
if event is sTAU:
return frozenset((self.left, self.right))
else:
return sEMPTY
class ExtChoice(Process):
def __init__(self, left, right):
Process.__init__(self)
self.left = left
self.right = right
def _inits(self):
return self.left.inits() | self.right.inits()
def _after(self, event):
if event is not TAU:
return self.left.after(event) | self.right.after(event)
else:
result = set()
result.update(ExtChoice(l, self.right)
for l in self.left.after(event))
result.update(ExtChoice(self.left, r)
for r in self.right.after(event))
return frozenset(result)
class Interleave(Process):
def __init__(self, left, right):
Process.__init__(self)
self.left = left
self.right = right
def _inits(self):
result = set()
result.update(self.left.inits() - sTICK)
result.update(self.right.inits() - sTICK)
result.update(self.left.inits() & self.right.inits() & sTICK)
return frozenset(result)
def _after(self, event):
result = set()
if event is not TICK:
result.update(Interleave(l, self.right)
for l in self.left.after(event))
result.update(Interleave(self.left, r)
for r in self.right.after(event))
else:
result.update(Interleave(l, r)
for l in self.left.after(event)
for r in self.right.after(event))
class Parallel1(Process):
def __init__(self, left, alpha, right):
Process.__init__(self)
self.left = left
self.alpha = alpha
self.right = right
def _inits(self):
alphatick = self.alpha | sTICK
result = set()
result.update(self.left.inits() - alphatick)
result.update(self.right.inits() - alphatick)
result.update(self.left.inits() & self.right.inits() & alphatick)
return frozenset(result)
def _after(self, event):
alphatick = self.alpha | sTICK
result = set()
if event not in alphatick:
result.update(Parallel1(l, self.alpha, self.right)
for l in self.left.after(event))
result.update(Parallel1(self.left, self.alpha, r)
for r in self.right.after(event))
else:
result.update(Parallel1(l, self.alpha, r)
for l in self.left.after(event)
for r in self.right.after(event))
return frozenset(result)
class Parallel2(Process):
def __init__(self, left, lalpha, ralpha, right):
Process.__init__(self)
self.left = left
self.lalpha = lalpha
self.ralpha = ralpha
self.right = right
def _inits(self):
result = set()
result.update(self.left.inits() & (self.lalpha - self.ralpha + sTAU))
result.update(self.right.inits() & (self.ralpha - self.lalpha + sTAU))
result.update(self.left.inits() &
self.right.inits() &
(self.lalpha & self.ralpha & sTICK))
return frozenset(result)
def _after(self, event):
result = set()
if event in (self.lalpha - self.ralpha + sTAU):
result.update(Parallel2(l, self.lalpha, self.ralpha, self.right)
for l in self.left.after(event))
if event in (self.ralpha - self.lalpha + sTAU):
result.update(Parallel2(self.left, self.lalpha, self.ralpha, r)
for r in self.right.after(event))
if event in (self.lalpha & self.ralpha & sTICK):
result.update(Parallel2(l, self.lalpha, self.ralpha, r)
for l in self.left.after(event)
for r in self.right.after(event))
return frozenset(result)
class Hide(Process):
def __init__(self, process, alpha):
Process.__init__(self)
self.process = process
self.alpha = alpha
def _inits(self):
result = self.process.inits()
if (result & self.alpha) != sEMPTY:
return result - self.alpha + sTAU
else:
return result
def _after(self, event):
result = set()
if event is TAU:
result.update(Hide(p, self.alpha)
for hidden_event in self.alpha
for p in self.process.after(hidden_event))
if event not in self.alpha:
result.update(self.process.after(event))
return frozenset(result)
class Rename(Process):
def __init__(self, process, rel):
Process.__init__(self)
self.process = process
self.rel = rel
def _inits(self):
result = self.process.inits() & frozenset((TICK, TAU))
result.update(self.rel.rimage(self.process.inits()))
return result
def _after(self, event):
if event is TICK or event is TAU:
return self.process.after(event)
else:
result = set()
result.update(Rename(p, self.rel)
for rel_event in self.rel.dimage(event)
for p in self.process.after(rel_event))
return frozenset(result)
class Prefix(Process):
def __init__(self, events, process_func):
Process.__init__(self)
if isinstance(events, Event):
self.events = frozenset(events)
else:
self.events = events
if isinstance(process_func, Process):
self.process_func = lambda event: return process_func
else:
self.process_func = process_func
def _inits(self):
return self.events
def _after(self, event):
return self.process_func(event)