-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.py
99 lines (81 loc) · 3.14 KB
/
node.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
class Node():
def __init__(self, name, successors, predecessors, inputs, outputs, max_input, max_output):
self.name = name
self.successors = successors
self.predecessors = predecessors
self.inputs = inputs #input ports
self.outputs = outputs #output ports
self.flag = False # dirty flag
self.max_input = max_input
self.max_output = max_output
def __init__(self, name, successors=[], predecessors=[], inputs=[], outputs=[], max_input = -1, max_output = -1):
self.name = name
self.successors = successors
self.predecessors = predecessors
self.inputs = inputs #input ports
self.outputs = outputs #output ports
self.flag = False # dirty flag
self.max_input = max_input
self.max_output = max_output
def check_dirty(self):
# 输入是否改变
return self.flag
def serialize(self):
return {'name': self.name,
'successors': self.successors,
'predecessors': self.predecessors,
'inputs': self.inputs,
'outputs': self.outputs,
'max_input': self.max_input,
'max_output': self.max_output}
def deserialize(self, node_dict):
de_node = Node(node_dict['name'],
node_dict['successors'],
node_dict['predecessors'],
node_dict['inputs'],
node_dict['outputs'],
node_dict['max_input'],
node_dict['max_output'])
return de_node
def connect(self, port):
if port.type == 'input':
self.inputs.append(port)
else:
self.outputs.append(port)
def disconnect(self, port):
if port.type == 'input':
if port in self.inputs:
self.inputs.remove((port))
else:
if port in self.outputs:
self.outputs.remove((port))
def check_illegal(self):
if self.max_input == -1 or len(self.inputs) <= self.max_input:
if self.max_output == -1 or len(self.outputs) <= self.max_output:
for p in self.inputs:
if p.check_illegal():
return True
return False
return True
class AdderNode(Node):
pass
def add(self):
result = 0
input_num = len(self.inputs)
output_num = len(self.outputs)
if input_num >= 1 and output_num == 1:
for port in self.inputs:
result += port.value
print(self.name + ': ' + str(result))
self.outputs[0].update(result)
else:
print('add error')
class SquareNode(Node):
pass
def square(self):
if len(self.inputs) == 1 and len(self.outputs) == 1:
result = self.inputs[0].value * self.inputs[0].value
print(self.name + ': ' + str(result))
self.outputs[0].update(result)
else:
print('square error')