diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2016-06-03 16:37:05 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2016-06-09 14:49:16 +0200 |
commit | 6375ebf0eb2b619e1a31ec8b8babc3ad0f968dd2 (patch) | |
tree | 489414ef1f7097e47f483661ffada43ec3bb1506 | |
parent | 963773b800655f2902998aedce8d46605d54e60f (diff) |
grc-refactor: Connections
-rw-r--r-- | grc/core/Connection.py | 106 | ||||
-rw-r--r-- | grc/core/FlowGraph.py | 4 | ||||
-rw-r--r-- | grc/core/Port.py | 6 | ||||
-rw-r--r-- | grc/core/generator/Generator.py | 20 | ||||
-rw-r--r-- | grc/core/generator/flow_graph.tmpl | 6 | ||||
-rw-r--r-- | grc/gui/ActionHandler.py | 8 | ||||
-rw-r--r-- | grc/gui/Connection.py | 24 | ||||
-rw-r--r-- | grc/gui/FlowGraph.py | 2 |
8 files changed, 93 insertions, 83 deletions
diff --git a/grc/core/Connection.py b/grc/core/Connection.py index 9ae99debe7..a15fe6a2e8 100644 --- a/grc/core/Connection.py +++ b/grc/core/Connection.py @@ -45,6 +45,21 @@ class Connection(Element): a new connection """ Element.__init__(self, flow_graph) + + source, sink = self._get_sink_source(porta, portb) + + self.source_port = source + self.sink_port = sink + + # Ensure that this connection (source -> sink) is unique + for connection in flow_graph.connections: + if connection.source_port is source and connection.sink_port is sink: + raise LookupError('This connection between source and sink is not unique.') + + self._make_bus_connect() + + @staticmethod + def _get_sink_source(porta, portb): source = sink = None # Separate the source and sink for port in (porta, portb): @@ -56,42 +71,18 @@ class Connection(Element): raise ValueError('Connection could not isolate source') if not sink: raise ValueError('Connection could not isolate sink') - - if (source.get_type() == 'bus') != (sink.get_type() == 'bus'): - raise ValueError('busses must get with busses') - - if not len(source.get_associated_ports()) == len(sink.get_associated_ports()): - raise ValueError('port connections must have same cardinality') - # Ensure that this connection (source -> sink) is unique - for connection in flow_graph.connections: - if connection.get_source() is source and connection.get_sink() is sink: - raise LookupError('This connection between source and sink is not unique.') - self._source = source - self._sink = sink - if source.get_type() == 'bus': - - sources = source.get_associated_ports() - sinks = sink.get_associated_ports() - - for i in range(len(sources)): - try: - flow_graph.connect(sources[i], sinks[i]) - except: - pass + return source, sink def __str__(self): return 'Connection (\n\t{}\n\t\t{}\n\t{}\n\t\t{}\n)'.format( - self.get_source().get_parent(), - self.get_source(), - self.get_sink().get_parent(), - self.get_sink(), + self.source_block, self.source_port, self.sink_block, self.sink_port, ) def is_msg(self): - return self.get_source().get_type() == self.get_sink().get_type() == 'msg' + return self.source_port.get_type() == self.sink_port.get_type() == 'msg' def is_bus(self): - return self.get_source().get_type() == self.get_sink().get_type() == 'bus' + return self.source_port.get_type() == self.sink_port.get_type() == 'bus' def validate(self): """ @@ -104,18 +95,20 @@ class Connection(Element): """ Element.validate(self) platform = self.get_parent().get_parent() - source_domain = self.get_source().get_domain() - sink_domain = self.get_sink().get_domain() + + source_domain = self.source_port.get_domain() + sink_domain = self.sink_port.get_domain() + if (source_domain, sink_domain) not in platform.connection_templates: self.add_error_message('No connection known for domains "{}", "{}"'.format( - source_domain, sink_domain)) + source_domain, sink_domain)) too_many_other_sinks = ( not platform.domains.get(source_domain, []).get('multiple_sinks', False) and - len(self.get_source().get_enabled_connections()) > 1 + len(self.source_port.get_enabled_connections()) > 1 ) too_many_other_sources = ( not platform.domains.get(sink_domain, []).get('multiple_sources', False) and - len(self.get_sink().get_enabled_connections()) > 1 + len(self.sink_port.get_enabled_connections()) > 1 ) if too_many_other_sinks: self.add_error_message( @@ -124,8 +117,8 @@ class Connection(Element): self.add_error_message( 'Domain "{}" can have only one upstream block'.format(sink_domain)) - source_size = Constants.TYPE_TO_SIZEOF[self.get_source().get_type()] * self.get_source().get_vlen() - sink_size = Constants.TYPE_TO_SIZEOF[self.get_sink().get_type()] * self.get_sink().get_vlen() + source_size = Constants.TYPE_TO_SIZEOF[self.source_port.get_type()] * self.source_port.get_vlen() + sink_size = Constants.TYPE_TO_SIZEOF[self.sink_port.get_type()] * self.sink_port.get_vlen() if source_size != sink_size: self.add_error_message('Source IO size "{}" does not match sink IO size "{}".'.format(source_size, sink_size)) @@ -136,17 +129,15 @@ class Connection(Element): Returns: true if source and sink blocks are enabled """ - return self.get_source().get_parent().get_enabled() and \ - self.get_sink().get_parent().get_enabled() + return self.source_block.get_enabled() and self.sink_block.get_enabled() - ############################# - # Access Ports - ############################# - def get_sink(self): - return self._sink + @property + def source_block(self): + return self.source_port.get_parent() - def get_source(self): - return self._source + @property + def sink_block(self): + return self.sink_port.get_parent() ############################################## # Import/Export Methods @@ -159,8 +150,27 @@ class Connection(Element): a nested data odict """ n = collections.OrderedDict() - n['source_block_id'] = self.get_source().get_parent().get_id() - n['sink_block_id'] = self.get_sink().get_parent().get_id() - n['source_key'] = self.get_source().get_key() - n['sink_key'] = self.get_sink().get_key() + n['source_block_id'] = self.source_block.get_id() + n['sink_block_id'] = self.sink_block.get_id() + n['source_key'] = self.source_port.get_key() + n['sink_key'] = self.sink_port.get_key() return n + + def _make_bus_connect(self): + source, sink = self.source_port, self.sink_port + + if (source.get_type() == 'bus') != (sink.get_type() == 'bus'): + raise ValueError('busses must get with busses') + + if not len(source.get_associated_ports()) == len(sink.get_associated_ports()): + raise ValueError('port connections must have same cardinality') + + if source.get_type() == 'bus': + sources = source.get_associated_ports() + sinks = sink.get_associated_ports() + + for i in range(len(sources)): + try: + self.get_parent().connect(sources[i], sinks[i]) + except: + pass diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py index b0f52dbe74..a17d820539 100644 --- a/grc/core/FlowGraph.py +++ b/grc/core/FlowGraph.py @@ -342,7 +342,7 @@ class FlowGraph(Element): elif element in self.connections: if element.is_bus(): - for port in element.get_source().get_associated_ports(): + for port in element.source_port.get_associated_ports(): for connection in port.get_connections(): self.remove_element(connection) self.connections.remove(element) @@ -516,7 +516,7 @@ class FlowGraph(Element): for j in range(len(source.get_connections())): sink.append( - source.get_connections()[j].get_sink()) + source.get_connections()[j].sink_port) for elt in source.get_connections(): self.remove_element(elt) for j in sink: diff --git a/grc/core/Port.py b/grc/core/Port.py index 34edb8d0b4..b0753910e6 100644 --- a/grc/core/Port.py +++ b/grc/core/Port.py @@ -33,7 +33,7 @@ def _get_source_from_virtual_sink_port(vsp): """ try: return _get_source_from_virtual_source_port( - vsp.get_enabled_connections()[0].get_source()) + vsp.get_enabled_connections()[0].source_port) except: raise Exception('Could not resolve source for virtual sink port {}'.format(vsp)) @@ -71,7 +71,7 @@ def _get_sink_from_virtual_source_port(vsp): try: # Could have many connections, but use first return _get_sink_from_virtual_sink_port( - vsp.get_enabled_connections()[0].get_sink()) + vsp.get_enabled_connections()[0].sink_port) except: raise Exception('Could not resolve source for virtual source port {}'.format(vsp)) @@ -377,7 +377,7 @@ class Port(Element): a list of connection objects """ connections = self.get_parent().get_parent().connections - connections = [c for c in connections if c.get_source() is self or c.get_sink() is self] + connections = [c for c in connections if c.source_port is self or c.sink_port is self] return connections def get_enabled_connections(self): diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py index c27e926c79..97729b3ada 100644 --- a/grc/core/generator/Generator.py +++ b/grc/core/generator/Generator.py @@ -167,14 +167,14 @@ class TopBlockGenerator(object): # Filter out virtual sink connections def cf(c): - return not (c.is_bus() or c.is_msg() or c.get_sink().get_parent().is_virtual_sink()) + return not (c.is_bus() or c.is_msg() or c.sink_block.is_virtual_sink()) connections = [con for con in fg.get_enabled_connections() if cf(con)] # Get the virtual blocks and resolve their connections - virtual = [c for c in connections if c.get_source().get_parent().is_virtual_source()] + virtual = [c for c in connections if c.source_block.is_virtual_source()] for connection in virtual: - source = connection.get_source().resolve_virtual_source() - sink = connection.get_sink() + source = connection.source.resolve_virtual_source() + sink = connection.sink_port resolved = fg.get_parent().Connection(flow_graph=fg, porta=source, portb=sink) connections.append(resolved) # Remove the virtual connection @@ -189,19 +189,19 @@ class TopBlockGenerator(object): for block in bypassed_blocks: # Get the upstream connection (off of the sink ports) # Use *connections* not get_connections() - source_connection = [c for c in connections if c.get_sink() == block.get_sinks()[0]] + source_connection = [c for c in connections if c.sink_port == block.get_sinks()[0]] # The source connection should never have more than one element. assert (len(source_connection) == 1) # Get the source of the connection. - source_port = source_connection[0].get_source() + source_port = source_connection[0].source_port # Loop through all the downstream connections - for sink in (c for c in connections if c.get_source() == block.get_sources()[0]): + for sink in (c for c in connections if c.source_port == block.get_sources()[0]): if not sink.get_enabled(): # Ignore disabled connections continue - sink_port = sink.get_sink() + sink_port = sink.sink_port connection = fg.get_parent().Connection(flow_graph=fg, porta=source_port, portb=sink_port) connections.append(connection) # Remove this sink connection @@ -211,8 +211,8 @@ class TopBlockGenerator(object): # List of connections where each endpoint is enabled (sorted by domains, block names) connections.sort(key=lambda c: ( - c.get_source().get_domain(), c.get_sink().get_domain(), - c.get_source().get_parent().get_id(), c.get_sink().get_parent().get_id() + c.source_port.get_domain(), c.sink_port.get_domain(), + c.source_block.get_id(), c.sink_block.get_id() )) connection_templates = fg.get_parent().connection_templates diff --git a/grc/core/generator/flow_graph.tmpl b/grc/core/generator/flow_graph.tmpl index ecdb89390e..21bcb601a2 100644 --- a/grc/core/generator/flow_graph.tmpl +++ b/grc/core/generator/flow_graph.tmpl @@ -205,7 +205,7 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) $DIVIDER #end if #for $msg in $msgs - $(msg.get_source().get_parent().get_id())_msgq_out = $(msg.get_sink().get_parent().get_id())_msgq_in = gr.msg_queue(2) + $(msg.source_block.get_id())_msgq_out = $(msg.sink_block.get_id())_msgq_in = gr.msg_queue(2) #end for ######################################################## ##Create Blocks @@ -260,8 +260,8 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), [$(', '.join($size_strs))]) $DIVIDER #end if #for $con in $connections - #set global $source = $con.get_source() - #set global $sink = $con.get_sink() + #set global $source = $con.source_port + #set global $sink = $con.sink_port #include source=$connection_templates[($source.get_domain(), $sink.get_domain())] #end for diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py index 9c3e9246d5..3c6b57b482 100644 --- a/grc/gui/ActionHandler.py +++ b/grc/gui/ActionHandler.py @@ -240,15 +240,15 @@ class ActionHandler: for connection in block.connections: # Get id of connected blocks - source_id = connection.get_source().get_parent().get_id() - sink_id = connection.get_sink().get_parent().get_id() + source_id = connection.source_block.get_id() + sink_id = connection.sink_block.get_id() # If connected block is not in the list of selected blocks create a pad for it if flow_graph.get_block(source_id) not in flow_graph.get_selected_blocks(): - pads.append({'key': connection.get_sink().get_key(), 'coord': connection.get_source().get_coordinate(), 'block_id' : block.get_id(), 'direction': 'source'}) + pads.append({'key': connection.sink_port.get_key(), 'coord': connection.source_port.get_coordinate(), 'block_id' : block.get_id(), 'direction': 'source'}) if flow_graph.get_block(sink_id) not in flow_graph.get_selected_blocks(): - pads.append({'key': connection.get_source().get_key(), 'coord': connection.get_sink().get_coordinate(), 'block_id' : block.get_id(), 'direction': 'sink'}) + pads.append({'key': connection.source_port.get_key(), 'coord': connection.sink_port.get_coordinate(), 'block_id' : block.get_id(), 'direction': 'sink'}) # Copy the selected blocks and paste them into a new page diff --git a/grc/gui/Connection.py b/grc/gui/Connection.py index 8953ca0183..3af6badaa0 100644 --- a/grc/gui/Connection.py +++ b/grc/gui/Connection.py @@ -75,20 +75,20 @@ class Connection(Element, _Connection): self._source_coor = None #get the source coordinate try: - connector_length = self.get_source().get_connector_length() + connector_length = self.source_port.get_connector_length() except: return - self.x1, self.y1 = Utils.get_rotated_coordinate((connector_length, 0), self.get_source().get_rotation()) + self.x1, self.y1 = Utils.get_rotated_coordinate((connector_length, 0), self.source_port.get_rotation()) #get the sink coordinate - connector_length = self.get_sink().get_connector_length() + CONNECTOR_ARROW_HEIGHT - self.x2, self.y2 = Utils.get_rotated_coordinate((-connector_length, 0), self.get_sink().get_rotation()) + connector_length = self.sink_port.get_connector_length() + CONNECTOR_ARROW_HEIGHT + self.x2, self.y2 = Utils.get_rotated_coordinate((-connector_length, 0), self.sink_port.get_rotation()) #build the arrow self.arrow = [(0, 0), - Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, -CONNECTOR_ARROW_BASE/2), self.get_sink().get_rotation()), - Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, CONNECTOR_ARROW_BASE/2), self.get_sink().get_rotation()), + Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, -CONNECTOR_ARROW_BASE/2), self.sink_port.get_rotation()), + Utils.get_rotated_coordinate((-CONNECTOR_ARROW_HEIGHT, CONNECTOR_ARROW_BASE/2), self.sink_port.get_rotation()), ] - source_domain = self.get_source().get_domain() - sink_domain = self.get_sink().get_domain() + source_domain = self.source_port.get_domain() + sink_domain = self.sink_port.get_domain() # self.line_attributes[0] = 2 if source_domain != sink_domain else 0 # self.line_attributes[1] = Gdk.LINE_DOUBLE_DASH \ # if not source_domain == sink_domain == GR_MESSAGE_DOMAIN \ @@ -105,12 +105,12 @@ class Connection(Element, _Connection): """Calculate coordinates.""" self.clear() #FIXME do i want this here? #source connector - source = self.get_source() + source = self.source_port X, Y = source.get_connector_coordinate() x1, y1 = self.x1 + X, self.y1 + Y self.add_line((x1, y1), (X, Y)) #sink connector - sink = self.get_sink() + sink = self.sink_port X, Y = sink.get_connector_coordinate() x2, y2 = self.x2 + X, self.y2 + Y self.add_line((x2, y2), (X, Y)) @@ -149,8 +149,8 @@ class Connection(Element, _Connection): """ Draw the connection. """ - sink = self.get_sink() - source = self.get_source() + sink = self.sink_port + source = self.source_port #check for changes if self._sink_rot != sink.get_rotation() or self._source_rot != source.get_rotation(): self.create_shapes() elif self._sink_coor != sink.get_coordinate() or self._source_coor != source.get_coordinate(): diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py index 8f35222d42..37a233f825 100644 --- a/grc/gui/FlowGraph.py +++ b/grc/gui/FlowGraph.py @@ -184,7 +184,7 @@ class FlowGraph(Element, _Flowgraph): y_min = min(y, y_min) #get connections between selected blocks connections = list(filter( - lambda c: c.get_source().get_parent() in blocks and c.get_sink().get_parent() in blocks, + lambda c: c.source_block in blocks and c.sink_block in blocks, self.connections, )) clipboard = ( |