Connection.py

Go to the documentation of this file.
00001 """
00002 Copyright 2008 Free Software Foundation, Inc.
00003 This file is part of GNU Radio
00004 
00005 GNU Radio Companion is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 GNU Radio Companion is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00018 """
00019 ##@package grc.elements.Connection
00020 #Flow graph connection.
00021 #A connection exists between 2 ports.
00022 #One port must be input, one output.
00023 #The port decided whether it can have the connection.
00024 #@author Josh Blum
00025 
00026 from grc.elements.Element import Element
00027 from grc.Utils import odict
00028 
00029 class Connection(Element):
00030 
00031         def __init__(self, flow_graph, porta, portb):
00032                 """!
00033                 Make a new connection given the parent and 2 ports.
00034                 @param flow_graph the parent of this element
00035                 @param porta a port (any direction)
00036                 @param portb a port (any direction)
00037                 @throws Error cannot make connection
00038                 @return a new connection
00039                 """
00040                 Element.__init__(self, flow_graph)
00041                 source = sink = None
00042                 #separate the source and sink
00043                 for port in (porta, portb):
00044                         if port.is_source(): source = port
00045                         if port.is_sink(): sink = port
00046                 #verify the source and sink
00047                 assert(source and sink)
00048                 assert(not source.is_full())
00049                 assert(not sink.is_full())
00050                 self._source = source
00051                 self._sink = sink
00052 
00053         def __str__(self): return 'Connection (%s -> %s)'%(self.get_source(), self.get_sink())
00054 
00055         def is_connection(self): return True
00056 
00057         def validate(self):
00058                 """
00059                 Validate the connections.
00060                 The ports must match in type.
00061                 """
00062                 source_type = self.get_source().get_type()
00063                 sink_type = self.get_sink().get_type()
00064                 try: assert source_type == sink_type
00065                 except AssertionError: self._add_error_message('Source type "%s" does not match sink type "%s".'%(source_type, sink_type))
00066 
00067         def get_enabled(self):
00068                 """!
00069                 Get the enabled state of this connection.
00070                 @return true if source and sink blocks are enabled
00071                 """
00072                 return self.get_source().get_parent().get_enabled() and \
00073                         self.get_sink().get_parent().get_enabled()
00074 
00075         #############################
00076         # Access Ports
00077         #############################
00078         def get_sink(self): return self._sink
00079         def get_source(self): return self._source
00080 
00081         ##############################################
00082         ## Import/Export Methods
00083         ##############################################
00084         def export_data(self):
00085                 """
00086                 Export this connection's info.
00087                 @return a nested data odict
00088                 """
00089                 n = odict()
00090                 n['source_block_id'] = self.get_source().get_parent().get_id()
00091                 n['sink_block_id'] = self.get_sink().get_parent().get_id()
00092                 n['source_key'] = self.get_source().get_key()
00093                 n['sink_key'] = self.get_sink().get_key()
00094                 return n

Generated on Sat Aug 23 02:00:11 2008 for GNU Radio Companion by  doxygen 1.5.4