-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSignal.py
More file actions
105 lines (95 loc) · 3.09 KB
/
Signal.py
File metadata and controls
105 lines (95 loc) · 3.09 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
from pythonfrp import Globals
from pythonfrp import Errors
from pythonfrp.Types import signalType, eventValueType
class EventValue:
def __init__(self, value = None):
self._type = eventValueType
self.value = value
def __add__(self, e):
Errors.checkEvent(e, "event addition")
# print("Event merge: " + str(self) + " " + str(e))
if self.value is None:
return e
else:
return self
def occurs(self):
return self.value is not None
def __str__(self):
if self.value is None:
return "<No Event>"
return "<" + str(self.value) + ">"
noEvent = EventValue()
class Signal:
def __init__(self):
self._type = signalType
class Lift0(Signal):
def __init__(self, v):
Signal.__init__(self)
self.v = v
def now(self):
return self.v
def __str__(self):
return str(self.v)
class Lift(Signal):
def __init__(self,name, f, args):
Signal.__init__(self)
self.f = f
self.name = name
self.args=args
#print("Lift const: " + str(self.args))
#print("Lift const, arg0.now: " + repr(self.args[0].now()))
#print("Lift const, arg1.now: " + repr(self.args[1].now()))
def now(self):
#print("Lift, arg0.now: " + repr(self.args[0].now()))
#print("Lift, arg1.now: " + repr(self.args[1].now()))
ea = list(map(lambda a: a.now() , self.args)) # Why is this giving me a Lift0
# for the frist argument instead of its value?
# Why is the value a Lift0
#print("Lift: " + str(ea))
#print("eval" + self.name + " " + str(ea) + " = " + str(self.f(*ea)))
return self.f(*ea)
# Cached Signal that inherits Signal
# Baisically is just a time stamp
class CachedSignal(Signal):
def __init__(self, s):
Signal.__init__(self)
self.cachedValue = 0
self.time = -1
#print("cache " + repr(s))
self.s = s
# if not isinstance(s, Lift): # Could also be an observer
# die()
def now(self):
if self.time is not Globals.currentTime:
self.cachedValue = self.s.now()
self.time = Globals.currentTime
return self.cachedValue
def cache(s):
if isinstance(s, Lift0) or isinstance(s, StateMachine):
return s
return CachedSignal(s)
# A State Machine signal
class StateMachine(Signal):
def __init__(self, s0, i, f):
#print("s0 = " + repr(s0) + " i = " + repr(i) + " f = " + repr(f))
Signal.__init__(self)
self.f = f
self.i = i
self.time = -1
# This is here to avoid brain damage,
# self.value is set in self.f()
self.value = None
s0(self)
def now(self):
#Caching the value
#We don't want to recalculate more than once each step
if self.time is not Globals.currentTime:
self.f(self)
self.time = Globals.currentTime
return self.value
class Observer(Signal):
def __init__(self, f):
Signal.__init__(self)
self.f = f
def now(self):
return self.f(self)