def action_componentclass(self, componentclass, namespace): for src, sink in componentclass.portconnections: full_src = NamespaceAddress.concat(namespace, src) full_sink = NamespaceAddress.concat(namespace, sink) # print 'Adding Port:',full_src # print 'Adding Port:',full_sink self.portconnections.append((full_src, full_sink))
def action_componentclass(self, componentclass, namespace): for src, sink in componentclass.portconnections: full_src = NamespaceAddress.concat(namespace, src) full_sink = NamespaceAddress.concat(namespace, sink) # print 'Adding Port:',full_src # print 'Adding Port:',full_sink self.portconnections.append((full_src, full_sink))
def visit_componentclass(self, componentclass, **kwargs): # @UnusedVariable prefix = componentclass.get_node_addr().get_str_prefix() if prefix == '_': prefix = '' prefix_excludes = ['t'] kwargs = {'prefix': prefix, 'prefix_excludes': prefix_excludes} port_connections = [] for src, sink in componentclass.portconnections: # To calculate the new address of the ports, we take of the 'local' # port address, i.e. the parent address, then add the prefixed # string: src_new = NamespaceAddress.concat( src.get_parent_addr(), NamespaceAddress.concat( componentclass.get_node_addr(), src.get_parent_addr()).get_str_prefix() + self.prefix_variable(src.get_local_name())) sink_new = NamespaceAddress.concat( sink.get_parent_addr(), NamespaceAddress.concat( componentclass.get_node_addr(), sink.get_parent_addr()).get_str_prefix() + self.prefix_variable(sink.get_local_name())) # self.prefix_variable(sink.get_local_name(), **kwargs) ) # print 'Mapping Ports:', src, '->', src_new, '(%s)' % # src.get_local_name(), prefix # print 'Mapping Ports:', sink, '->', sink_new # src_new = NamespaceAddress.concat( # src.get_parent_addr(), src.getstr() ) # sink_new = NamespaceAddress.concat( # sink.get_parent_addr(), sink.getstr() ) port_connections.append((src_new, sink_new)) return componentclass.__class__( name=componentclass.name, parameters=[ p.accept_visitor(self, **kwargs) for p in componentclass.parameters ], analog_ports=[ p.accept_visitor(self, **kwargs) for p in componentclass.analog_ports ], event_ports=[ p.accept_visitor(self, **kwargs) for p in componentclass.event_ports ], dynamicsblock=(componentclass.dynamicsblock.accept_visitor( self, **kwargs) if componentclass.dynamicsblock else None), subnodes=dict([(k, v.accept_visitor(self, **kwargs)) for (k, v) in componentclass.subnodes.iteritems()]), portconnections=port_connections)
def get_node_addr(self): """Get the namespace address of this component""" parent = self.get_parent_model() if not parent: return NamespaceAddress.create_root() else: contained_namespace = invert_dictionary(parent.subnodes)[self] return parent.get_node_addr().get_subns_addr(contained_namespace)
def visit_componentclass(self, componentclass, **kwargs): # @UnusedVariable prefix = componentclass.get_node_addr().get_str_prefix() if prefix == '_': prefix = '' prefix_excludes = ['t'] kwargs = {'prefix': prefix, 'prefix_excludes': prefix_excludes} port_connections = [] for src, sink in componentclass.portconnections: # To calculate the new address of the ports, we take of the 'local' # port address, i.e. the parent address, then add the prefixed # string: src_new = NamespaceAddress.concat( src.get_parent_addr(), NamespaceAddress.concat( componentclass.get_node_addr(), src.get_parent_addr()).get_str_prefix() + self.prefix_variable(src.get_local_name())) sink_new = NamespaceAddress.concat( sink.get_parent_addr(), NamespaceAddress.concat( componentclass.get_node_addr(), sink.get_parent_addr()).get_str_prefix() + self.prefix_variable(sink.get_local_name())) # self.prefix_variable(sink.get_local_name(), **kwargs) ) # print 'Mapping Ports:', src, '->', src_new, '(%s)' % # src.get_local_name(), prefix # print 'Mapping Ports:', sink, '->', sink_new # src_new = NamespaceAddress.concat( # src.get_parent_addr(), src.getstr() ) # sink_new = NamespaceAddress.concat( # sink.get_parent_addr(), sink.getstr() ) port_connections.append((src_new, sink_new)) return componentclass.__class__( name=componentclass.name, parameters=[p.accept_visitor(self, **kwargs) for p in componentclass.parameters], analog_ports=[p.accept_visitor(self, **kwargs) for p in componentclass.analog_ports], event_ports=[p.accept_visitor(self, **kwargs) for p in componentclass.event_ports], dynamicsblock=(componentclass.dynamicsblock.accept_visitor(self, **kwargs) if componentclass.dynamicsblock else None), subnodes=dict([(k, v.accept_visitor(self, **kwargs)) for (k, v) in componentclass.subnodes.iteritems()]), portconnections=port_connections)
def get_subnode(self, addr): """Gets a subnode from this component recursively.""" namespace_addr = NamespaceAddress(addr) # Look up the first name in the namespace if len(namespace_addr.loctuple) == 0: return self local_namespace_ref = namespace_addr.loctuple[0] if local_namespace_ref not in self.subnodes: err = "Attempted to lookup node: %s\n" % local_namespace_ref err += "Doesn't exist in this namespace: %s" % self.subnodes.keys() raise NineMLRuntimeError(err) subnode = self.subnodes[local_namespace_ref] addr_in_subnode = NamespaceAddress(namespace_addr.loctuple[1:]) return subnode.get_subnode(addr=addr_in_subnode)
def get_node_addr(self): """Get the namespace address of this component""" parent = self.get_parent_model() if not parent: return NamespaceAddress.create_root() else: contained_namespace = invert_dictionary(parent.subnodes)[self] return parent.get_node_addr().get_subns_addr(contained_namespace)
def connect_ports(self, src, sink): """Connects the ports of 2 subcomponents. The ports can be specified as ``string`` s or |NamespaceAddresses|. :param src: The source port of one sub-component; this should either an |EventPort| or |AnalogPort|, but it *must* be a send port. :param sink: The sink port of one sub-component; this should either an |EventPort| or |AnalogPort|, but it *must* be either a 'recv' or a 'reduce' port. """ connection = (NamespaceAddress(src), NamespaceAddress(sink)) self._portconnections.append(connection) self._validate_self()
def get_new_regime(self, old_regime_string): """ for example: old_regime_string = ('iaf:subthresholdregime ' 'cobaInhib:cobadefaultregime ' 'cobaExcit:cobadefaultregime') """ # Lets create a dictionary that maps 'NamespaceAddress' to regime name # from the input string: # old_regime_string = 'iaf:subthresholdregime' # 'cobaInhib:cobadefaultregime' # 'cobaExcit:cobadefaultregime' nsstr_regimename = [l.split(':') for l in old_regime_string.split()] ns_regimename = dict([(NamespaceAddress(ns), regime_name) for (ns, regime_name) in nsstr_regimename]) # OK, now lets go through our old componentswithregimes, # and find the regime that was specified. target_regime_tuple = [] for c in self.componentswithregimes: comp_ns = c.get_node_addr() if comp_ns not in ns_regimename: err = ('Looking for a regime in namespace: {}, but not found.' .format(comp_ns)) err += ('\nNamespaces: {}' .format(','.join([str(ns) for ns in ns_regimename.keys()]))) err += '\nSpecified String: {}'.format(old_regime_string) raise NineMLRuntimeError(err) target_regime_name = ns_regimename[comp_ns] regime_map = dict([(r.name, r) for r in c.regimes]) if target_regime_name not in regime_map: err = 'Namespace has no regime named: %s' err += '\nRegimes: %s' % (str(regime_map.keys())) raise NineMLRuntimeError(err) target_regime_tuple.append(regime_map[target_regime_name]) target_regime_tuple = tuple(target_regime_tuple) new_regime = self.old_regime_tuple_to_new_regime_map[ target_regime_tuple] return new_regime
def _action_port(self, port, namespace): port_address = NamespaceAddress.concat(namespace, port.name) if port_address in self.ports: raise NineMLRuntimeError('Duplicated Name for port found: %s' % port_address) self.ports[port_address] = port
def _action_port(self, port, namespace): port_address = NamespaceAddress.concat(namespace, port.name) if port_address in self.ports: raise NineMLRuntimeError("Duplicated Name for port found: %s" % port_address) self.ports[port_address] = port