Ejemplo n.º 1
0
    def read_component(cls, filename, component_name=None):
        """Reads a single |COMPONENTCLASS| object from a filename.

        :param filename: The name of the file.
        :param component_name: If the file contains more than one
            ComponentClass definition, this parameter must be provided as a
            ``string`` specifying which component to return, otherwise a
            NineMLRuntimeException will be raised.
        :rtype: Returns a |COMPONENTCLASS| object.
        """

        xml_node_filename_map = {}
        root = cls._load_nested_xml(filename=filename,
                                   xml_node_filename_map=xml_node_filename_map)

        loader = cls.loader(xmlroot=root,
                            xml_node_filename_map=xml_node_filename_map)

        if component_name == None:
            key_func = lambda c: loader.component_srcs[c] == filename
            return filter_expect_single(loader.components, key_func)

        else:
            key_func = lambda c: c.name == component_name
            return filter_expect_single(loader.components, key_func)
Ejemplo n.º 2
0
    def rename_port(cls, component, old_port_name, new_port_name):
        """ Renames a port in a component """
        if not component.is_flat():
            raise NineMLRuntimeError('rename_port() on non-flat component')

        # Find the old port:
        port = filter_expect_single(component.analog_ports,
                                    lambda ap: ap.name == old_port_name)
        port._name = new_port_name
Ejemplo n.º 3
0
    def remap_port_to_parameter(cls, component, port_name):
        """ Renames a port in a component """
        if not component.is_flat():
            raise NineMLRuntimeError('rename_port_to_parameter() on non-flat component')

        # Find the old port:
        port = filter_expect_single(component.analog_ports,
                                    lambda ap: ap.name == port_name)
        component._analog_ports.remove(port)

        # Add a new parameter:
        component._parameters.append(Parameter(port_name))
Ejemplo n.º 4
0
    def close_analog_port(cls, component, port_name, value="0"):
        """Closes an incoming analog port by assigning its value to 0"""

        if not component.is_flat():
            raise NineMLRuntimeError('close_analog_port() on non-flat component')

        # Subsitute the value in:
        component.accept_visitor(ExpandPortDefinition(port_name, value))

        # Remove it from the list of ports:
        port = filter_expect_single(component.analog_ports,
                                    lambda ap: ap.name == port_name)
        component._analog_ports.remove(port)
Ejemplo n.º 5
0
    def close_analog_port(cls, component, port_name, value="0"):
        """Closes an incoming analog port by assigning its value to 0"""

        if not component.is_flat():
            raise NineMLRuntimeError('close_analog_port() on non-flat '
                                     'component')

        # Subsitute the value in:
        component.accept_visitor(ExpandPortDefinition(port_name, value))

        # Remove it from the list of ports:
        port = filter_expect_single(component.analog_ports,
                                    lambda ap: ap.name == port_name)
        if isinstance(port, AnalogSendPort):
            component._analog_send_ports.pop(port_name)
        elif isinstance(port, AnalogReceivePort):
            component._analog_receive_ports.pop(port_name)
        elif isinstance(port, AnalogReducePort):
            component._analog_reduce_ports.pop(port_name)
        else:
            raise TypeError("Expected an analog port")
Ejemplo n.º 6
0
 def load_subnode(self, subnode):
     namespace = subnode.get('namespace')
     component = filter_expect_single(self.components,
                                    lambda c: c.name == subnode.get('node'))
     return namespace, component
Ejemplo n.º 7
0
    def test_filter_expect_single(self):
        # Signature: name(lst, func=None, error_func=None)
                # Find a single element matching a predicate in a list.
                #
                # This is a syntactic-sugar function ``_filter`` and ``expect_single``
                # in a single call.
                #
                #  Returns::
                #
                #      expect_single( _filter(lst, func), error_func )
                #
                #
                #  This is useful when we want to find an item in a sequence with a certain
                #  property, and expect there to be only one.
                #
                #  Examples:
                #
                #  >>> find_smith = lambda s: s.split()[-1] == 'Smith'
                # >>> filter_expect_single( ['John Smith','Tim Jones'], func=find_smith )  #doctest: +NORMALIZE_WHITESPACE
                #  'John Smith'
        # from nineml.utility import filter_expect_single

        from nineml.utility import filter_expect_single
        from nineml.exceptions import NineMLRuntimeError

        find_smith = lambda s: s.split()[-1] == 'Smith'
        self.assertEqual(
            filter_expect_single(['John Smith', 'Tim Jones'], func=find_smith),
            'John Smith'
        )

        self.assertEqual(
            filter_expect_single(['Rob Black', 'John Smith', 'Tim Jones'], func=find_smith),
            'John Smith'
        )
        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, ['Rob Black', 'Tim Jones'], func=find_smith,
        )

        # No Objects:
        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [], func=lambda x: None,
        )

        # Only a single None
        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [None], func=lambda x: x == None,
        )

        # Duplicate objects:
        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [None, None], func=lambda x: x == None,
        )

        # Duplicate objects:
        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [False, False], func=lambda x: x == None,
        )

        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [True, True], func=lambda x: x == None,
        )

        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, [1, 2, 3, 4, 5, 4, 3, 2, 1], func=lambda x: x == 2,
        )

        self.assertRaises(
            NineMLRuntimeError,
            filter_expect_single, ['1', '2', '3', '4', '3', '2', '1'], func=lambda x: x == '2',
        )

        self.assertEqual(
            True,
            filter_expect_single(['1', '2', '3', '4', '5', True], func=lambda x: x is True)
        )
        # Good Cases
        self.assertEqual(
            2,
            filter_expect_single([1, 2, 3, 4, 5], func=lambda x: x == 2)
        )
        self.assertEqual(
            '2',
            filter_expect_single(['1', '2', '3', '4', '5'], func=lambda x: x == '2')
        )
Ejemplo n.º 8
0
    def regime(self, name=None,):
        """Find a regime in the component by name"""
        assert isinstance(name, basestring)

        return filter_expect_single(self.component.regimes,
                                    lambda r: r.name == name)
Ejemplo n.º 9
0
def get_on_event_channel(on_event, component):
    port = filter_expect_single( component.event_ports, lambda ep:ep.name==on_event.src_port_name)
    return port.channel_