-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbuiltins.py
More file actions
518 lines (400 loc) · 11 KB
/
Copy pathbuiltins.py
File metadata and controls
518 lines (400 loc) · 11 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/usr/bin/env python
"""Builtin processes for python-csp. For guard types see csp.guards.
Based on the JCSP PlugNPlay package.
Copyright (C) Sarah Mount, 2009.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A ParTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>'
__date__ = 'May 2010'
import math
import operator
import os
import sys
from csp.guards import Timer
if 'CSP' in os.environ:
if os.environ['CSP'] == 'PROCESSES':
from csp.cspprocess import *
elif os.environ['CSP'] == 'THREADS':
from csp.cspthread import *
else:
from csp.cspprocess import *
else:
from csp.cspprocess import *
del os
# Names exported by this module.
__all__ = ['Sin', 'Cos', 'GenerateFloats',
'Zeroes', 'Id', 'Succ', 'Pred', 'Prefix', 'Delta2',
'Mux2', 'Multiply', 'Clock', 'Printer', 'Pairs',
'Mult', 'Generate', 'FixedDelay', 'Fibonacci',
'Blackhole', 'Sign',
# Processes based on Python operators
'Plus', 'Sub', 'Mul', 'Div', 'FloorDiv', 'Mod',
'Pow', 'LShift', 'RShift', 'Neg', 'Not', 'And',
'Or', 'Nand', 'Nor', 'Xor', 'Land', 'Lor', 'Lnot',
'Lnand', 'Lnor', 'Lxor', 'Eq', 'Ne', 'Geq', 'Leq',
'Gt', 'Lt', 'Is', 'Is_Not']
@forever
def Sin(inchan, outchan):
"""
readset = inchan
writeset = outchan
"""
while True:
outchan.write(math.sin(inchan.read()))
yield
return
@forever
def Cos(inchan, outchan):
"""
readset = inchan
writeset = outchan
"""
while True:
outchan.write(math.cos(inchan.read()))
yield
return
@forever
def GenerateFloats(outchan, epsilon=0.1):
"""
readset =
writeset =
"""
x = 0.0
while True:
outchan.write(x)
x += epsilon
yield
return
@forever
def Zeroes(cout):
"""Writes out a stream of zeroes.
readset =
writeset = cout
"""
while True:
cout.write(0)
yield
return
@forever
def Id(cin, cout):
"""Id is the CSP equivalent of lambda x: x.
readset = cin
writeset = cout
"""
while True:
cout.write(cin.read())
yield
return
@forever
def Succ(cin, cout):
"""Succ is the successor process, which writes out 1 + its input
event.
readset = cin
writeset = cout
"""
while True:
cout.write(cin.read() + 1)
yield
return
@forever
def Pred(cin, cout):
"""Pred is the predecessor process, which writes out 1 - its input
event.
readset = cin
writeset = cout
"""
while True:
cout.write(cin.read() - 1)
yield
return
@forever
def Prefix(cin, cout, prefix_item=None):
"""Prefix a write on L{cout} with the value read from L{cin}.
readset = cin
writeset = cout
@type prefix_item: object
@param prefix_item: prefix value to use before first item read from L{cin}.
"""
pre = prefix_item
while True:
cout.write(pre)
pre = cin.read()
yield
return
@forever
def Delta2(cin, cout1, cout2):
"""Delta2 sends input values down two output channels.
readset = cin
writeset = cout1, cout2
"""
while True:
val = cin.read()
cout1.write(val)
cout2.write(val)
yield
return
@forever
def Mux2(cin1, cin2, cout):
"""Mux2 provides a fair multiplex between two input channels.
readset = cin1, cin2
writeset = cout
"""
# alt = Alt(cin1, cin2)
while True:
cout.write(cin1.read())
cout.write(cin2.read())
# cout.write(alt.pri_select())
yield
return
@forever
def Multiply(cin0, cin1, cout):
"""
readset = cin0, cin1
writeset = cout
"""
while True:
cout.write(cin0.read() * cin1.read())
yield
return
@forever
def Clock(cout, resolution=1):
"""Send None object down output channel every C{resolution}
seconds.
readset =
writeset = cout
"""
timer = Timer()
while True:
timer.sleep(resolution)
cout.write(None)
yield
return
@forever
def Printer(cin, out=sys.stdout):
"""Print all values read from L{cin} to standard out or L{out}.
readset = cin
writeset =
"""
while True:
msg = str(cin.read()) + '\n'
out.write(msg)
yield
return
@forever
def Pairs(cin1, cin2, cout):
"""Read values from L{cin1} and L{cin2} and write their addition
to L{cout}.
readset = cin1, cin2
writeset = cout
"""
while True:
in1 = cin1.read()
in2 = cin2.read()
cout.write(in1 + in2)
yield
return
@forever
def Mult(cin, cout, scale):
"""Scale values read on L{cin} and write to L{cout}.
readset = cin
writeset = cout
"""
while True:
cout.write(cin.read() * scale)
yield
return
@forever
def Generate(cout):
"""Generate successive (+ve) ints and write to L{cout}.
readset =
writeset = cout
"""
counter = 0
while True:
cout.write(counter)
counter += 1
yield
return
@forever
def FixedDelay(cin, cout, delay):
"""Read values from L{cin} and write to L{cout} after a delay of
L{delay} seconds.
readset = cin
writeset = cout
"""
timer = Timer()
while True:
in1 = cin.read()
timer.sleep(delay)
cout.write(in1)
yield
return
@forever
def Fibonacci(cout):
"""Write successive Fibonacci numbers to L{cout}.
readset =
writeset = cout
"""
a_int = b_int = 1
while True:
cout.write(a_int)
a_int, b_int = b_int, a_int + b_int
yield
return
@forever
def Blackhole(cin):
"""Read values from L{cin} and do nothing with them.
readset = cin
writeset =
"""
while True:
cin.read()
yield
return
@forever
def Sign(cin, cout, prefix):
"""Read values from L{cin} and write to L{cout}, prefixed by L{prefix}.
readset = cin
writeset = cout
"""
while True:
val = cin.read()
cout.write(prefix + str(val))
yield
return
### Magic for processes built on Python operators
def _applyunop(unaryop, docstring):
"""Create a process whose output is C{unaryop(cin.read())}.
"""
chandoc = """
readset = cin
writeset = cout
"""
@forever
def _myproc(cin, cout):
while True:
in1 = cin.read()
cout.write(unaryop(in1))
yield
_myproc.__doc__ = docstring + chandoc
return _myproc
def _applybinop(binop, docstring):
"""Create a process whose output is C{binop(cin1.read(), cin2.read())}.
"""
chandoc = """
readset = cin1, cin2
writeset = cout
"""
@forever
def _myproc(cin1, cin2, cout):
while True:
in1 = cin1.read()
in2 = cin2.read()
cout.write(binop(in1, in2))
yield
_myproc.__doc__ = docstring + chandoc
return _myproc
# Numeric operators
Plus = _applybinop(operator.__add__,
"""Writes out the addition of two input events.
""")
Sub = _applybinop(operator.__sub__,
"""Writes out the subtraction of two input events.
""")
Mul = _applybinop(operator.__mul__,
"""Writes out the multiplication of two input events.
""")
Div = _applybinop(operator.__truediv__,
"""Writes out the division of two input events.
""")
FloorDiv = _applybinop(operator.__floordiv__,
"""Writes out the floor div of two input events.
""")
Mod = _applybinop(operator.__mod__,
"""Writes out the modulo of two input events.
""")
Pow = _applybinop(operator.__pow__,
"""Writes out the power of two input events.
""")
LShift = _applybinop(operator.__lshift__,
"""Writes out the left shift of two input events.
""")
RShift = _applybinop(operator.__rshift__,
"""Writes out the right shift of two input events.
""")
Neg = _applyunop(operator.__neg__,
"""Writes out the negation of input events.
""")
# Bitwise operators
Not = _applyunop(operator.__inv__,
"""Writes out the inverse of input events.
""")
And = _applybinop(operator.__and__,
"""Writes out the bitwise and of two input events.
""")
Or = _applybinop(operator.__or__,
"""Writes out the bitwise or of two input events.
""")
Nand = _applybinop(lambda x, y: ~ (x & y),
"""Writes out the bitwise nand of two input events.
""")
Nor = _applybinop(lambda x, y: ~ (x | y),
"""Writes out the bitwise nor of two input events.
""")
Xor = _applybinop(operator.xor,
"""Writes out the bitwise xor of two input events.
""")
# Logical operators
Land = _applybinop(lambda x, y: x and y,
"""Writes out the logical and of two input events.
""")
Lor = _applybinop(lambda x, y: x or y,
"""Writes out the logical or of two input events.
""")
Lnot = _applyunop(operator.__not__,
"""Writes out the logical not of input events.
""")
Lnand = _applybinop(lambda x, y: not (x and y),
"""Writes out the logical nand of two input events.
""")
Lnor = _applybinop(lambda x, y: not (x or y),
"""Writes out the logical nor of two input events.
""")
Lxor = _applybinop(lambda x, y: (x or y) and (not x and y),
"""Writes out the logical xor of two input events.
""")
# Comparison operators
Eq = _applybinop(operator.__eq__,
"""Writes True if two input events are equal (==).
""")
Ne = _applybinop(operator.__ne__,
"""Writes True if two input events are not equal (not ==).
""")
Geq = _applybinop(operator.__ge__,
"""Writes True if 'right' input event is >= 'left'.
""")
Leq = _applybinop(operator.__le__,
"""Writes True if 'right' input event is <= 'left'.
""")
Gt = _applybinop(operator.__gt__,
"""Writes True if 'right' input event is > 'left'.
""")
Lt = _applybinop(operator.__lt__,
"""Writes True if 'right' input event is < 'left'.
""")
Is = _applybinop(lambda x, y: x is y,
"""Writes True if two input events are the same (is).
""")
Is_Not = _applybinop(lambda x, y: not (x is y),
"""Writes True if two input events are not the same (is).
""")