-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
482 lines (389 loc) · 12.6 KB
/
nodes.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
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
import unittest
# from remaining_faults import Fault
# from remaining_faults import Fault
import functools
from time import time
from functools import singledispatch, lru_cache
from typing import List, Type, Union, Any, Dict, Optional, Tuple, Iterable, Iterator, Set, Generator
class Value:
# __slots__ = 'value', 'key'
__init__map = {0: 0, "0": 0, '1': 1, 1: 1, "D": "D", "d": "D",
"D'": "D'", "d'": "D'", "U": "U", "u": "U"}
__key__map = {0: "_zero", 1: "_one", 'U': '_unknown', "D'": "_dprime", 'D': '_d'}
@lru_cache(5)
def __new__(cls, value):
return super(Value, cls).__new__(cls)
def __init__(self, value: Union[str, int]):
try:
self.value = Value.__init__map[value]
except KeyError:
raise Value(f"Cannot be turned into Value: {value}")
self.key = self.__key__map[self.value]
@lru_cache(25)
def __eq__(self, other):
return self is other
@lru_cache(5)
def __invert__(self) -> 'Value':
if self is value_0:
return value_1
elif self is value_1:
return value_0
elif self is value_D:
return value_DP
elif self is value_DP:
return value_D
else:
return value_U
@lru_cache(5)
def __bool__(self):
if self is value_U:
return False
else:
return True
def __str__(self):
return str(self.value)
def __repr__(self):
return repr(self.value)
def __hash__(self):
return hash(self.value)
@functools.cached_property
def propagates_fault(self) -> bool:
if self is value_0 or self is value_1 or self is value_U:
return False
else:
return True
value_1 = Value(1)
value_0 = Value(0)
value_D = Value("D")
value_DP = Value("D'")
value_U = Value("U")
class Gate:
def __subclasscheck__(self, subclass):
return subclass in (AndGate, InputFault, NandGate, OrGate, NorGate, XorGate, XnorGate,
BuffGate, NotGate, FlipFlop)
# __slots__ = 'name', 'type', 'node', 'input_names', 'value', 'value_new', '_zero', \
# '_one', '_unknown', '_dprime', '_d'
def __init__(self, name: str):
self.name = name
self.type: str = "WIRE"
self.node: Optional[Node] = None
self.input_names: List[str] = []
self.value: Value = value_U
self.value_new: Value = value_U
self._zero = False
self._one = False
self._unknown = False
self._dprime = False
self._d = False
def __repr__(self):
return f"{self.name} = {self.value}"
@property
def input_nodes(self) -> List['Node']:
return self.node.input_nodes
@property
def output_nodes(self) -> List['Node']:
return self.node.output_nodes
def update(self):
self.value = self.value_new
def _reset_logic(self):
self._zero = False
self._one = False
self._unknown = False
self._dprime = False
self._d = False
def logic(self) -> Value:
return self.value
@property
def stuck_at(self):
return self.node.stuck_at
class Node:
# __slots__ = 'gate', 'input_nodes', 'output_nodes', 'stuck_at', 'vector_assignment', \
# 'type', 'corridor', 'implication'
def __init__(self, gate: Gate):
gate.node = self
self.gate: Union[Gate, FlipFlop] = gate
self.input_nodes: List[Node, InputFault] = []
self.output_nodes: List[Node, InputFault] = []
self.stuck_at: Optional[Value] = None
self.vector_assignment: Value = value_U
self.type = "INTERM."
self.corridor = False
self.implication = value_U
def __repr__(self):
return self.name
def __str__(self):
return f"{self.type}{self.name} = {self.value}"
def __hash__(self):
return hash(self.name)
def name(self) -> str:
return self.gate.name
@property
def value(self) -> Value:
return self.gate.value
@value.setter
def value(self, val: Value):
self.gate.value = val
@property
def value_new(self) -> Value:
return self.gate.value_new
@value_new.setter
def value_new(self, val: Value):
self.gate.value_new = val
def logic(self):
self.value_new = self.propagate_fault(self.gate.logic(), self.stuck_at)
def update(self):
self.value = self.value_new
def reset(self):
self.gate.value = self.vector_assignment
self.gate.value_new = self.vector_assignment
self.stuck_at = None
for node in self.output_nodes:
node.reset()
def local_reset(self):
self.gate.value = self.vector_assignment
self.gate.value_new = self.vector_assignment
self.stuck_at = None
@property
def propagates_fault(self) -> bool:
"""Returns true if the node propagates a fault"""
return self.value.propagates_fault
@staticmethod
@lru_cache(15)
def propagate_fault(value: Value, stuck_at: Value):
if not stuck_at:
return value
elif stuck_at is value_1:
if value is value_0:
return value_DP
else:
return value
else:
if value is value_1:
return value_D
else:
return value
@property
def fault_propagation_path(self) -> Generator[Set['Node'], None, None]:
frontier = {self}
yield frontier
while (any(node.value.propagates_fault for node in frontier) and
(frontier := {
output_node for node
in frontier
for output_node in node.output_nodes
})
):
yield frontier
# @property
# def propagation_path(self) -> Generator[Set['Node'], None, None]:
# yield (frontier := {self})
# while (frontier := {
# output_node for node
# in frontier
# for output_node in node.output_nodes
# }):
# yield frontier
@functools.cached_property
def propagation_path(self) -> List[Set['Node']]:
result = [frontier := {self}]
while (frontier := {
output_node for node
in frontier
for output_node in node.output_nodes
}):
result.append(frontier)
return result
@functools.cached_property
def outputs_that_are_not_flip_flops(self) -> List['Node']:
return [output_node for output_node in self.output_nodes if not isinstance(output_node.gate, FlipFlop)]
class InputFault(Node):
__slots__ = 'output_nodes', 'genuine_node', 'stuck_at'
def __init__(self, output_node: Node, node: Node, stuck_at: Value):
self.genuine_node = node
self.output_nodes = [output_node]
output_node.input_nodes.remove(node)
output_node.input_nodes.append(self)
self.stuck_at = stuck_at
@property
def name(self):
return "%s FAULT" % self.genuine_node.name
def logic(self):
pass
def update(self):
pass
def reset(self):
self.output_nodes[0].input_nodes.remove(self)
self.output_nodes[0].input_nodes.append(self.genuine_node)
for node in self.output_nodes:
node.reset()
# del self
def local_reset(self):
self.output_nodes[0].input_nodes.remove(self)
self.output_nodes[0].input_nodes.append(self.genuine_node)
@property
def value(self) -> Value:
return self.propagate_fault(self.genuine_node.value, self.stuck_at)
@property
def fault_propagation_path(self) -> Generator[Set['Node'], None, None]:
return self.output_nodes[0].fault_propagation_path
# return self.genuine_node.fault_propagation_path
@property
def propagation_path(self) -> Generator[Set['Node'], None, None]:
return self.output_nodes[0].propagation_path
class AndGate(Gate):
def __init__(self, name):
super().__init__(name)
self.type = "AND"
def logic(self) -> Value:
self._reset_logic()
for node in self.input_nodes:
if node.value is value_0:
return value_0
self.__dict__[node.value.key] = True
return self._logic(self._d, self._dprime, self._unknown)
@staticmethod
@lru_cache(8)
def _logic(_d, _dprime, _unknown):
if _d and _dprime:
return value_0
elif _unknown:
return value_U
elif _d:
return value_D
elif _dprime:
return value_DP
return value_1
class NandGate(AndGate):
def __init__(self, name):
super().__init__(name)
self.type = "NAND"
def logic(self) -> Value:
return ~(super().logic())
class OrGate(Gate):
def __init__(self, name):
super().__init__(name)
self.type = "OR"
def logic(self) -> Value:
self._reset_logic()
for node in self.input_nodes:
if node.value is value_1:
return value_1
self.__dict__[node.value.key] = True
return self._logic(self._d, self._dprime, self._unknown)
@staticmethod
@lru_cache(25)
def _logic(_d, _dprime, _unknown):
if _d and _dprime:
return value_1
elif _unknown:
return value_U
elif _d:
return value_D
elif _dprime:
return value_DP
else:
return value_0
class NorGate(OrGate):
def __init__(self, name):
super().__init__(name)
self.type = "NOR"
def logic(self) -> Value:
return ~(super().logic())
class XorGate(Gate):
def __init__(self, name):
super().__init__(name)
self.type = "XOR"
_xor_map = {
value_1: True,
value_DP: True,
value_D: True
}
def logic(self) -> Value:
self._reset_logic()
d_acceptable = True
dprime_acceptable = True
for node in self.input_nodes:
if self._one and node.value is value_1:
return value_0
elif self._dprime and node.value is value_DP:
dprime_acceptable = False
elif self._dprime and node.value is value_D:
d_acceptable = False
self.__dict__[node.value.key] = True
return self._logic(self._one, self._d, self._dprime, self._unknown,
d_acceptable, dprime_acceptable)
@staticmethod
@lru_cache(64)
def _logic(one, d, dprime, unknown, d_acceptable, dprime_acceptable):
if unknown:
return value_U
if dprime and d:
if one:
return value_0
elif d_acceptable and dprime_acceptable:
return value_1
elif d_acceptable:
return value_D
elif dprime_acceptable:
return value_DP
else:
return value_0
elif dprime:
if one:
return value_D
elif dprime_acceptable:
return value_DP
else:
return value_0
elif d:
if one:
return value_DP
elif d_acceptable:
return value_D
else:
return value_0
else:
return value_0
class XnorGate(XorGate):
def __init__(self, name):
super().__init__(name)
self.type = "XNOR"
def logic(self) -> Value:
return ~(super(XnorGate, self).logic())
class BuffGate(Gate):
def __init__(self, name: str):
super(BuffGate, self).__init__(name)
self.type = "BUFF"
def logic(self) -> Value:
return self.input_nodes[0].value
class NotGate(BuffGate):
def __init__(self, name):
super().__init__(name)
self.type = "NOT"
def logic(self) -> Value:
return ~self.input_nodes[0].value
class FlipFlop(Gate):
def __init__(self, name):
super().__init__(name)
self.type = "DFF"
self.data = value_U
self.data_new = value_U
def logic(self) -> Value:
return self.input_nodes[0].value
@property
def value(self) -> Value:
return self.data
@value.setter
def value(self, val: Value):
self.data_new = val
@property
def value_new(self):
return self.data_new
@value_new.setter
def value_new(self, val: Value):
self.data_new = val
def capture(self):
self.data = self.data_new
def clear(self):
self.data = self.node.vector_assignment
self.data_new = self.node.vector_assignment