/
Gate.py
39 lines (32 loc) 路 1.09 KB
/
Gate.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
import uuid
class Gate:
def __init__(self, nr_inputs, nr_outputs, type=None) -> None:
self.nr_inputs = nr_inputs
self.nr_outputs = nr_outputs
self.type = type
self.inputs = {}
self.inp_values = []
self.outputs = {}
self.id = str(uuid.uuid4())
def appendInput(self, b):
b.appendRight(self)
self.inputs[len(list(self.inputs.keys()))] = b
def appendOutput(self, b):
self.outputs[len(list(self.outputs.keys()))] = b
def recieve(self, value, x):
# rerun the processing
self.inp_values.append(value)
if len(self.inp_values) < self.nr_inputs:
self.send(value)
def send(self, y):
for x in list(self.outputs.keys()):
self.outputs[x].recieveLeft(y)
def append(self, bus):
self.appendInput(bus)
def toJson(self):
return {
"leftNodes": [self.inputs[x].id for x in self.inputs],
"rightNodes": [self.inputs[x].id for x in self.outputs],
"type": self.type,
"id": self.id
}