diff options
Diffstat (limited to 'grc/core/Connection.py')
-rw-r--r-- | grc/core/Connection.py | 155 |
1 files changed, 60 insertions, 95 deletions
diff --git a/grc/core/Connection.py b/grc/core/Connection.py index c028d89ddc..01baaaf8fc 100644 --- a/grc/core/Connection.py +++ b/grc/core/Connection.py @@ -17,128 +17,95 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -from . import Constants -from .Element import Element -from .utils import odict +from __future__ import absolute_import + +from .base import Element +from .utils.descriptors import lazy_property class Connection(Element): is_connection = True - def __init__(self, flow_graph, porta, portb): + def __init__(self, parent, source, sink): """ Make a new connection given the parent and 2 ports. Args: flow_graph: the parent of this element - porta: a port (any direction) - portb: a port (any direction) + source: a port (any direction) + sink: a port (any direction) @throws Error cannot make connection Returns: a new connection """ - Element.__init__(self, flow_graph) - source = sink = None - # Separate the source and sink - for port in (porta, portb): - if port.is_source: - source = port - else: - sink = port - if not source: + Element.__init__(self, parent) + + if not source.is_source: + source, sink = sink, source + if not source.is_source: raise ValueError('Connection could not isolate source') - if not sink: + if not sink.is_sink: raise ValueError('Connection could not isolate sink') - busses = len(filter(lambda a: a.get_type() == 'bus', [source, sink])) % 2 - if not busses == 0: - 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 + + self.source_port = source + self.sink_port = 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_bus(self): - return self.get_source().get_type() == self.get_sink().get_type() == 'bus' + def __eq__(self, other): + if not isinstance(other, self.__class__): + return NotImplemented + return self.source_port == other.source_port and self.sink_port == other.sink_port - def validate(self): - """ - Validate the connections. - The ports must match in io size. - """ - """ - Validate the connections. - The ports must match in type. - """ - Element.validate(self) - platform = self.get_parent().get_parent() - source_domain = self.get_source().get_domain() - sink_domain = self.get_sink().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)) - too_many_other_sinks = ( - not platform.domains.get(source_domain, []).get('multiple_sinks', False) and - len(self.get_source().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 - ) - if too_many_other_sinks: - self.add_error_message( - 'Domain "{}" can have only one downstream block'.format(source_domain)) - if too_many_other_sources: - 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() - if source_size != sink_size: - self.add_error_message('Source IO size "{}" does not match sink IO size "{}".'.format(source_size, sink_size)) + def __hash__(self): + return hash((self.source_port, self.sink_port)) + + def __iter__(self): + return iter((self.source_port, self.sink_port)) + + @lazy_property + def source_block(self): + return self.source_port.parent_block + + @lazy_property + def sink_block(self): + return self.sink_port.parent_block - def get_enabled(self): + @lazy_property + def type(self): + return self.source_port.domain, self.sink_port.domain + + @property + def enabled(self): """ Get the enabled state of this connection. 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.enabled and self.sink_block.enabled - ############################# - # Access Ports - ############################# - def get_sink(self): - return self._sink + def validate(self): + """ + Validate the connections. + The ports must match in io size. + """ + Element.validate(self) + platform = self.parent_platform - def get_source(self): - return self._source + if self.type not in platform.connection_templates: + self.add_error_message('No connection known between domains "{}" and "{}"' + ''.format(*self.type)) + + source_size = self.source_port.item_size + sink_size = self.sink_port.item_size + if source_size != sink_size: + self.add_error_message('Source IO size "{}" does not match sink IO size "{}".'.format(source_size, sink_size)) ############################################## # Import/Export Methods @@ -150,9 +117,7 @@ class Connection(Element): Returns: a nested data odict """ - n = odict() - 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() - return n + return ( + self.source_block.name, self.source_port.key, + self.sink_block.name, self.sink_port.key + ) |