def test_get_str_prefix(self): # Signature: name(self, join_char='_') # Returns the same as ``getstr``, but prepends the ``join_char`` to # the end of the string, so that the string can be used to prefix # variables. # # :param join_char: The character used to join the levels in the address. # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.' ) self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i')).get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.' ) self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i'), NSA.create_root(),).get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.' ) self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_str_prefix('/'), 'a/b/c/d/e/f/g/h/i/' )
def test_getstr(self): # Signature: name(self, join_char='_') # Returns the namespace address as a string. # # :param join_char: The character used to join the levels in the address. # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').getstr('.'), 'a.b.c.d.e.f.g.h.i' ) self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA( 'a.b.c.d.e.f.g.h.i')).getstr('.'), 'a.b.c.d.e.f.g.h.i' ) self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA( 'a.b.c.d.e.f.g.h.i'), NSA.create_root(),).getstr('.'), 'a.b.c.d.e.f.g.h.i' ) self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').getstr('/'), 'a/b/c/d/e/f/g/h/i' )
def test_get_str_prefix(self): # Signature: name(self, join_char='_') # Returns the same as ``getstr``, but prepends the ``join_char`` to # the end of the string, so that the string can be used to prefix # variables. # # :param join_char: The character used to join the levels in the address. # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.') self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i')).get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.') self.assertEqual( NSA.concat( NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i'), NSA.create_root(), ).get_str_prefix('.'), 'a.b.c.d.e.f.g.h.i.') self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_str_prefix('/'), 'a/b/c/d/e/f/g/h/i/')
def test_getstr(self): # Signature: name(self, join_char='_') # Returns the namespace address as a string. # # :param join_char: The character used to join the levels in the address. # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').getstr('.'), 'a.b.c.d.e.f.g.h.i') self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i')).getstr('.'), 'a.b.c.d.e.f.g.h.i') self.assertEqual( NSA.concat( NSA.create_root(), NSA.create_root(), NSA('a.b.c.d.e.f.g.h.i'), NSA.create_root(), ).getstr('.'), 'a.b.c.d.e.f.g.h.i') self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').getstr('/'), 'a/b/c/d/e/f/g/h/i')
def test_create_root(self): # Signature: name(cls) # Returns a empty (root) namespace address # # # >>> nineml.abstraction_layer.NamespaceAddress.create_root() # NameSpaceAddress: '//' # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual(NSA.create_root().loctuple, ())
def test_get_local_name(self): # Signature: name(self) # Returns the local reference; i.e. the last field in the # address, as a ``string`` # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual(NSA('a.b.c.d.e.f.g.h.i').get_local_name(), 'i') self.assertEqual(NSA('a.b.lastname').get_local_name(), 'lastname') self.assertRaises( NineMLRuntimeError, NSA.create_root().get_local_name, )
def test_get_fully_qualified_port_connections(self): # Signature: name(self) # Used by the flattening code. # # This method returns a d list of tuples of the # the fully-qualified port connections # from nineml.abstraction_layer.component.componentqueryer import ComponentClassQueryer # Signature: name(self) # Get the namespace address of this component d = ComponentClass( name='D', aliases=['A:=1', 'B:=2'], analog_ports=[AnalogSendPort('A'), AnalogSendPort('B')]) e = ComponentClass(name='E', analog_ports=[AnalogReceivePort('C')]) f = ComponentClass(name='F', analog_ports=[AnalogReceivePort('D')]) g = ComponentClass(name='G', analog_ports=[AnalogReceivePort('E')]) b = ComponentClass(name='B', subnodes={ 'd': d, 'e': e }, portconnections=[('d.A', 'e.C')]) c = ComponentClass(name='C', aliases=['G:=-1'], analog_ports=[AnalogSendPort('G')], subnodes={ 'f': f, 'g': g }, portconnections=[('G', 'f.D')]) a = ComponentClass(name='A', subnodes={ 'b': b, 'c': c }, analog_ports=[AnalogReceivePort('F')], portconnections=[('b.d.A', 'F')]) bNew = a.get_subnode('b') cNew = a.get_subnode('c') # dNew = a.get_subnode('b.d') # eNew = a.get_subnode('b.e') # fNew = a.get_subnode('c.f') # gNew = a.get_subnode('c.g') self.assertEquals(list(a.query.get_fully_qualified_port_connections()), [(NamespaceAddress('b.d.A'), NamespaceAddress('F'))]) self.assertEquals( list(bNew.query.get_fully_qualified_port_connections()), [(NamespaceAddress('b.d.A'), NamespaceAddress('b.e.C'))]) self.assertEquals( list(cNew.query.get_fully_qualified_port_connections()), [(NamespaceAddress('c.G'), NamespaceAddress('c.f.D'))])
def test_get_local_name(self): # Signature: name(self) # Returns the local reference; i.e. the last field in the # address, as a ``string`` # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_local_name(), 'i') self.assertEqual( NSA('a.b.lastname').get_local_name(), 'lastname') self.assertRaises( NineMLRuntimeError, NSA.create_root().get_local_name, )
def test_get_node_addr(self): # Signature: name(self) # Get the namespace address of this component from nineml.abstraction_layer import ComponentClass from nineml.abstraction_layer import NamespaceAddress d = ComponentClass(name='D',) e = ComponentClass(name='E') f = ComponentClass(name='F') g = ComponentClass(name='G') b = ComponentClass(name='B', subnodes={'d': d, 'e': e}) c = ComponentClass(name='C', subnodes={'f': f, 'g': g}) a = ComponentClass(name='A', subnodes={'b': b, 'c': c}) # Construction of the objects causes cloning to happen: # Therefore we test by looking up and checking that there # are the correct component names: bNew = a.get_subnode('b') cNew = a.get_subnode('c') dNew = a.get_subnode('b.d') eNew = a.get_subnode('b.e') fNew = a.get_subnode('c.f') gNew = a.get_subnode('c.g') self.assertEquals(a.get_node_addr(), NamespaceAddress.create_root()) self.assertEquals(bNew.get_node_addr(), NamespaceAddress('b')) self.assertEquals(cNew.get_node_addr(), NamespaceAddress('c')) self.assertEquals(dNew.get_node_addr(), NamespaceAddress('b.d')) self.assertEquals(eNew.get_node_addr(), NamespaceAddress('b.e')) self.assertEquals(fNew.get_node_addr(), NamespaceAddress('c.f')) self.assertEquals(gNew.get_node_addr(), NamespaceAddress('c.g')) self.assertEquals(a.name, 'A') self.assertEquals(bNew.name, 'B') self.assertEquals(cNew.name, 'C') self.assertEquals(dNew.name, 'D') self.assertEquals(eNew.name, 'E') self.assertEquals(fNew.name, 'F') self.assertEquals(gNew.name, 'G')
def test_concat(self): # Signature: name(cls, *args) # Concatenates all the Namespace Addresses. # # This method take all the arguments supplied, converts each one into a # namespace object, then, produces a new namespace object which is the # concatentation of all the arugements namespaces. # # For example: # # >>> NamespaceAddress.concat('first.second','third.forth','fifth.sixth') # NameSpaceAddress: '/first/second/third/forth/fifth/sixth' # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual(NSA.concat(NSA('a.b.c'), NSA('d.e.f'), NSA('g.h.i')), NSA('a.b.c.d.e.f.g.h.i')) self.assertEqual( NSA.concat(NSA.create_root(), NSA('a.b.c'), NSA.create_root()), NSA('a.b.c')) self.assertEqual(NSA.concat(NSA.create_root(), NSA.create_root()), NSA.create_root())
def test_get_parent_addr(self): # Signature: name(self) # Return the address of an namespace higher # # >>> a = NamespaceAddress('level1.level2.level3') # >>> a # NameSpaceAddress: '/level1/level2/level3/' # >>> a.get_parent_addr() # NameSpaceAddress: '/level1/level2/' self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_parent_addr(), NSA('a.b.c.d.e.f.g.h')) self.assertEqual(NSA('a.b.lastname').get_parent_addr(), NSA('a.b')) self.assertRaises( NineMLRuntimeError, NSA.create_root().get_local_name, )
def test_concat(self): # Signature: name(cls, *args) # Concatenates all the Namespace Addresses. # # This method take all the arguments supplied, converts each one into a # namespace object, then, produces a new namespace object which is the # concatentation of all the arugements namespaces. # # For example: # # >>> NamespaceAddress.concat('first.second','third.forth','fifth.sixth') # NameSpaceAddress: '/first/second/third/forth/fifth/sixth' # from nineml.abstraction_layer.component.namespaceaddress import NamespaceAddress self.assertEqual( NSA.concat(NSA('a.b.c'), NSA('d.e.f'), NSA('g.h.i')), NSA('a.b.c.d.e.f.g.h.i')) self.assertEqual( NSA.concat(NSA.create_root(), NSA('a.b.c'), NSA.create_root()), NSA('a.b.c') ) self.assertEqual( NSA.concat(NSA.create_root(), NSA.create_root()), NSA.create_root() )
def test_get_parent_addr(self): # Signature: name(self) # Return the address of an namespace higher # # >>> a = NamespaceAddress('level1.level2.level3') # >>> a # NameSpaceAddress: '/level1/level2/level3/' # >>> a.get_parent_addr() # NameSpaceAddress: '/level1/level2/' self.assertEqual( NSA('a.b.c.d.e.f.g.h.i').get_parent_addr(), NSA('a.b.c.d.e.f.g.h') ) self.assertEqual( NSA('a.b.lastname').get_parent_addr(), NSA('a.b') ) self.assertRaises( NineMLRuntimeError, NSA.create_root().get_local_name, )
def test_get_node_addr(self): # Signature: name(self) # Get the namespace address of this component d = ComponentClass(name='D', ) e = ComponentClass(name='E') f = ComponentClass(name='F') g = ComponentClass(name='G') b = ComponentClass(name='B', subnodes={'d': d, 'e': e}) c = ComponentClass(name='C', subnodes={'f': f, 'g': g}) a = ComponentClass(name='A', subnodes={'b': b, 'c': c}) # Construction of the objects causes cloning to happen: # Therefore we test by looking up and checking that there # are the correct component names: bNew = a.get_subnode('b') cNew = a.get_subnode('c') dNew = a.get_subnode('b.d') eNew = a.get_subnode('b.e') fNew = a.get_subnode('c.f') gNew = a.get_subnode('c.g') self.assertEquals(a.get_node_addr(), NamespaceAddress.create_root()) self.assertEquals(bNew.get_node_addr(), NamespaceAddress('b')) self.assertEquals(cNew.get_node_addr(), NamespaceAddress('c')) self.assertEquals(dNew.get_node_addr(), NamespaceAddress('b.d')) self.assertEquals(eNew.get_node_addr(), NamespaceAddress('b.e')) self.assertEquals(fNew.get_node_addr(), NamespaceAddress('c.f')) self.assertEquals(gNew.get_node_addr(), NamespaceAddress('c.g')) self.assertEquals(a.name, 'A') self.assertEquals(bNew.name, 'B') self.assertEquals(cNew.name, 'C') self.assertEquals(dNew.name, 'D') self.assertEquals(eNew.name, 'E') self.assertEquals(fNew.name, 'F') self.assertEquals(gNew.name, 'G')
def test_insert_subnode(self): # Signature: name(self, subnode, namespace) # Insert a subnode into this component # # # :param subnode: An object of type ``ComponentClass``. # :param namespace: A `string` specifying the name of the component in # this components namespace. # # :raises: ``NineMLRuntimeException`` if there is already a subcomponent at # the same namespace location # # .. note:: # # This method will clone the subnode. d = ComponentClass(name='D') e = ComponentClass(name='E') f = ComponentClass(name='F') g = ComponentClass(name='G') b = ComponentClass(name='B') b.insert_subnode(namespace='d', subnode=d) b.insert_subnode(namespace='e', subnode=e) c = ComponentClass(name='C') c.insert_subnode(namespace='f', subnode=f) c.insert_subnode(namespace='g', subnode=g) a = ComponentClass(name='A') a.insert_subnode(namespace='b', subnode=b) a.insert_subnode(namespace='c', subnode=c) # Construction of the objects causes cloning to happen: # Therefore we test by looking up and checking that there # are the correct component names: bNew = a.get_subnode('b') cNew = a.get_subnode('c') dNew = a.get_subnode('b.d') eNew = a.get_subnode('b.e') fNew = a.get_subnode('c.f') gNew = a.get_subnode('c.g') self.assertEquals(a.get_node_addr(), NamespaceAddress.create_root()) self.assertEquals(bNew.get_node_addr(), NamespaceAddress('b')) self.assertEquals(cNew.get_node_addr(), NamespaceAddress('c')) self.assertEquals(dNew.get_node_addr(), NamespaceAddress('b.d')) self.assertEquals(eNew.get_node_addr(), NamespaceAddress('b.e')) self.assertEquals(fNew.get_node_addr(), NamespaceAddress('c.f')) self.assertEquals(gNew.get_node_addr(), NamespaceAddress('c.g')) self.assertEquals(a.name, 'A') self.assertEquals(bNew.name, 'B') self.assertEquals(cNew.name, 'C') self.assertEquals(dNew.name, 'D') self.assertEquals(eNew.name, 'E') self.assertEquals(fNew.name, 'F') self.assertEquals(gNew.name, 'G') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'x') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.X') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.b.') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.b.X') # Adding to the same namespace twice: d1 = ComponentClass(name='D1') d2 = ComponentClass(name='D2') a = ComponentClass(name='B') a.insert_subnode(namespace='d', subnode=d1) self.assertRaises(NineMLRuntimeError, a.insert_subnode, namespace='d', subnode=d2)
def test_insert_subnode(self): # Signature: name(self, subnode, namespace) # Insert a subnode into this component # # # :param subnode: An object of type ``ComponentClass``. # :param namespace: A `string` specifying the name of the component in # this components namespace. # # :raises: ``NineMLRuntimeException`` if there is already a subcomponent at # the same namespace location # # .. note:: # # This method will clone the subnode. d = ComponentClass(name='D') e = ComponentClass(name='E') f = ComponentClass(name='F') g = ComponentClass(name='G') b = ComponentClass(name='B') b.insert_subnode(namespace='d', subnode=d) b.insert_subnode(namespace='e', subnode=e) c = ComponentClass(name='C') c.insert_subnode(namespace='f', subnode=f) c.insert_subnode(namespace='g', subnode=g) a = ComponentClass(name='A') a.insert_subnode(namespace='b', subnode=b) a.insert_subnode(namespace='c', subnode=c) # Construction of the objects causes cloning to happen: # Therefore we test by looking up and checking that there # are the correct component names: bNew = a.get_subnode('b') cNew = a.get_subnode('c') dNew = a.get_subnode('b.d') eNew = a.get_subnode('b.e') fNew = a.get_subnode('c.f') gNew = a.get_subnode('c.g') self.assertEquals(a.get_node_addr(), NamespaceAddress.create_root()) self.assertEquals(bNew.get_node_addr(), NamespaceAddress('b')) self.assertEquals(cNew.get_node_addr(), NamespaceAddress('c')) self.assertEquals(dNew.get_node_addr(), NamespaceAddress('b.d')) self.assertEquals(eNew.get_node_addr(), NamespaceAddress('b.e')) self.assertEquals(fNew.get_node_addr(), NamespaceAddress('c.f')) self.assertEquals(gNew.get_node_addr(), NamespaceAddress('c.g')) self.assertEquals(a.name, 'A') self.assertEquals(bNew.name, 'B') self.assertEquals(cNew.name, 'C') self.assertEquals(dNew.name, 'D') self.assertEquals(eNew.name, 'E') self.assertEquals(fNew.name, 'F') self.assertEquals(gNew.name, 'G') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'x') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.X') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.b.') self.assertRaises(NineMLRuntimeError, a.get_subnode, 'a.b.X') # Adding to the same namespace twice: d1 = ComponentClass(name='D1') d2 = ComponentClass(name='D2') a = ComponentClass(name='B') a.insert_subnode(namespace='d', subnode=d1) self.assertRaises( NineMLRuntimeError, a.insert_subnode, namespace='d', subnode=d2)