diff options
Diffstat (limited to 'grc/core')
-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 |
5 files changed, 76 insertions, 66 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 |