コード例 #1
0
    def test_connect_ports(self):
        # Signature: name(self, src, sink)
                # Connects the ports of 2 subcomponents.
                #
                # The ports can be specified as ``string`` s or ``NamespaceAddresses`` es.
                #
                #
                # :param src: The source port of one sub-component; this should either an
                #     event port or analog port, but it *must* be a send port.
                #
                # :param sink: The sink port of one sub-component; this should either an
                #     event port or analog port, but it *must* be either a 'recv' or a
                #     'reduce' port.

        from nineml.abstraction_layer import ComponentClass
        from nineml.abstraction_layer.testing_utils import TestableComponent
        from nineml.exceptions import NineMLRuntimeError

        tIaf = TestableComponent('iaf')
        tCoba = TestableComponent('coba_synapse')

        # Should be fine:
        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()})
        c.connect_ports('iaf.V', 'coba.V')

        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()},
                           portconnections=[('iaf.V', 'coba.V')]
                           )

        # Non existant Ports:
        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()})
        self.assertRaises(
            NineMLRuntimeError,
            c.connect_ports, 'iaf.V1', 'coba.V')
        self.assertRaises(
            NineMLRuntimeError,
            c.connect_ports, 'iaf.V', 'coba.V1')

        self.assertRaises(
            NineMLRuntimeError,
            ComponentClass,
            name='C1',
            subnodes={'iaf': tIaf(), 'coba': tCoba()},
            portconnections=[('iaf.V1', 'coba.V')]
        )

        self.assertRaises(
            NineMLRuntimeError,
            ComponentClass,
            name='C1',
            subnodes={'iaf': tIaf(), 'coba': tCoba()},
            portconnections=[('iaf.V', 'coba.V1')]
        )

        # Connect ports the wronf way around:
        # [Check the wright way around works:]
        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()},
                           portconnections=[('coba.I', 'iaf.ISyn')]
                           )
        # And the wrong way around:
        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()})
        self.assertRaises(
            NineMLRuntimeError,
            c.connect_ports, 'iaf.ISyn.', 'coba.I')
        self.assertRaises(
            NineMLRuntimeError,
            c.connect_ports, 'coba.V', 'iaf.V')

        # Error raised on duplicate port-connection:
        c = ComponentClass(name='C1',
                           subnodes={'iaf': tIaf(), 'coba': tCoba()},
                           )

        c.connect_ports('coba.I', 'iaf.ISyn')
        self.assertRaises(
            NineMLRuntimeError,
            c.connect_ports, 'coba.I', 'iaf.ISyn')