-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelectric.py
115 lines (80 loc) · 2.3 KB
/
electric.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
from threading import Event
class LinkedEvent( object ):
def __init__( self ):
self._event = Event()
self._links = []
def add_link( self, event ):
if self._links.count( event ) == 0:
self._links.append(event)
def remove_link( self, event ):
self._links.remove(event)
def get_links( self ):
return self._links
def set( self ):
raise NotImplementedError("Abstract method cannot be called")
def clear( self ):
raise NotImplementedError("Abstract method cannot be called")
def wait( self ):
raise NotImplementedError("Abstract method cannot be called")
def is_set( self ):
raise NotImplementedError("Abstract method cannot be called")
def isSet( self ):
raise NotImplementedError("Abstract method cannot be called")
class BranchEvent( LinkedEvent ):
def set( self ):
self._event.set()
for event in self._links:
event.set()
def clear( self ):
self._event.clear()
for event in self._links:
event.clear()
def wait( self ):
self._event.wait()
def is_set( self ):
return self._event.is_set()
def isSet( self ):
return self._event.isSet()
class HubEvent( LinkedEvent ):
def add_link( self, event ):
if isinstance( event, BranchEvent ):
super( HubEvent, self ).add_link( event )
event.add_link( self )
else:
raise TypeError("BranchEvent required")
def wait( self ):
self._event.wait()
def is_set( self ):
return self._event.is_set()
def isSet( self ):
return self._event.isSet()
class ANDHubEvent( HubEvent ):
def set( self ):
all_set = True
for event in self.get_links():
all_set = all_set and event.isSet()
if all_set:
self._event.set()
def clear( self ):
any_clear = False
for event in self.get_links():
any_clear = any_clear or not event.is_set()
if any_clear:
break
if any_clear:
self._event.clear()
class ORHubEvent( HubEvent ):
def set( self ):
any_set = False
for event in self.get_links():
any_set = any_set or event.isSet()
if any_set:
break
if any_set:
self._event.set()
def clear( self ):
all_clear = True
for event in self.get_links():
all_clear = all_clear and not event.is_set()
if all_clear:
self._event.clear()