-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPort.py
More file actions
148 lines (122 loc) · 4.39 KB
/
Copy pathPort.py
File metadata and controls
148 lines (122 loc) · 4.39 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
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
class Port(object):
def __init__(self, name="port", node=None, defaultValue=None):
"""
:param name:
:param node:
:param defaultValue:
Args:
edges ([]): is a list of ports that this port is connected to
dirty (bool): if the value of this Port has been updated then the node is set to dirty
"""
self.name = name
self.node = node
self._value = defaultValue # maybe have this as a property, that if isConnected then is queries the connected port
self.defaultValue = defaultValue
self.edges = []
self.dirty = False
@property
def value(self):
return self._value
@value.setter
def value(self, val):
self._value = val
self.setDirty()
def isConnected(self):
if len(self.edges):
return True
return False
def isSource(self):
pass
def isDestination(self):
pass
def addEdge(self, port):
self.edges.append(port)
def setDirty(self):
"""
Sets the port and the ports node to be dirty
"""
self.dirty = True
self.node.dirty = True
def connect(self, destPort):
"""
Create a connection between the current port and a destination port
Args:
destPort(Port): The port that this port will be connecting to.
Returns:
bool: False if the connection failed, true if the connection was made successfully
"""
if destPort.isConnected():
return False
self.addEdge(destPort)
destPort.addEdge(self)
# as the port has been connected, the connected node has to be updated
destPort.setDirty()
self.setDirty()
return True
def disconnect(self, discPort=None):
"""
Disconnect this port from another port. If no port is given, then disconnects all connection to this port
As each edge(connection) has two ports and each port stores the port it is connected to, we have to remove the each port
from storing one another
Args:
discPort (Port): The port we will be disconnecting from
"""
if discPort is None:
for port in self.edges:
port.disconnect(self)
port.setDirty()
self.edges = []
else:
for port in self.edges:
if port == discPort:
self.edges.remove(port)
port.disconnect(self)
port.setDirty()
class ContainerPort(Port):
def __init__(self, name="port", node=None, defaultValue=None):
super(ContainerPort, self).__init__(name, node, defaultValue)
self.internalEdges = []
def addEdge(self, port):
if port.node in self.node.internalNodes:
self.internalEdges.append(port)
#self.value = port.value
else:
self.edges.append(port)
def isConnected(self):
if len(self.edges):
return True
return False
def connect(self, destPort):
"""
Create a connection between the current port and a destination port
Args:
destPort(Port): The port that this port will be connecting to.
Returns:
bool: False if the connection failed, true if the connection was made successfully
"""
if destPort.isConnected():
return False
self.addEdge(destPort)
destPort.addEdge(self)
# as the port has been connected, the connected node has to be updated
destPort.setDirty()
return True
def disconnect(self, discPort=None):
"""
Disconnect this port from another port. If no port is given, then disconnects all connection to this port
As each edge(connection) has two ports and each port stores the port it is connected to, we have to remove the each port
from storing one another
Args:
discPort (Port): The port we will be disconnecting from
"""
if discPort is None:
for port in self.edges:
port.disconnect(self)
port.setDirty()
self.edges = []
else:
for port in self.edges:
if port == discPort:
self.edges.remove(port)
port.disconnect(self)
port.setDirty()