<s> from SubDomain import * <EOL> from Query import * <EOL> def InputFileKey ( input_file_key , enabled = True ) : <EOL> """<STR_LIT>""" <EOL> def input_file_key_inner ( cls ) : <EOL> cls . input_file_key = input_file_key <EOL> return cls <EOL> return input_file_key_inner <EOL> class Projection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source = "<STR_LIT>" , destination = "<STR_LIT>" , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( Projection , self ) . __init__ ( ) <EOL> if isinstance ( source , str ) : <EOL> self . source = source <EOL> elif isinstance ( source , SubDomain ) : <EOL> self . source = source . subdomain ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> if isinstance ( destination , str ) : <EOL> self . destination = destination <EOL> elif isinstance ( destination , SubDomain ) : <EOL> self . destination = destination . subdomain ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> def relates ( self , source , destination ) : <EOL> """<STR_LIT>""" <EOL> return ( ( ( self . source == source ) and <EOL> ( self . destination == destination ) ) or <EOL> ( ( self . source == destination ) and ( self . destination == source ) ) ) <EOL> def project ( self , subdomain , destination ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def source_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def destination_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> @ classmethod <EOL> def instantiate ( cls , key , source , destination , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( cls , '<STR_LIT>' ) and cls . input_file_key . upper ( ) == key . upper ( ) : <EOL> return cls ( source , destination , ** kwargs ) <EOL> else : <EOL> for s in cls . __subclasses__ ( ) : <EOL> result = s . instantiate ( key , source , destination , ** kwargs ) <EOL> if result is not None : <EOL> return result <EOL> return None <EOL> @ InputFileKey ( "<STR_LIT>" ) <EOL> class IdentityProjection ( Projection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source = "<STR_LIT>" , destination = "<STR_LIT>" , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( IdentityProjection , self ) . __init__ ( source , destination , ** kwargs ) <EOL> def project ( self , subdomain , destination ) : <EOL> """<STR_LIT>""" <EOL> result = SubDomain . instantiate ( destination , subdomain ) <EOL> return result <EOL> @ InputFileKey ( "<STR_LIT>" ) <EOL> class CompositionProjection ( Projection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source = "<STR_LIT>" , destination = "<STR_LIT>" , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( CompositionProjection , self ) . __init__ ( source , destination , <EOL> ** kwargs ) <EOL> if kwargs : <EOL> if '<STR_LIT>' not in kwargs : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> self . _projection_list = kwargs [ "<STR_LIT>" ] <EOL> def project ( self , subdomain , destination ) : <EOL> """<STR_LIT>""" <EOL> sub = subdomain <EOL> if destination == self . destination : <EOL> for proj , src , dest in self . _projection_list : <EOL> sub = proj . project ( sub , dest ) <EOL> else : <EOL> reverse_list = self . _projection_list [ : ] <EOL> reverse_list . reverse ( ) <EOL> for proj , src , dest in reverse_list : <EOL> sub = proj . project ( sub , src ) <EOL> return sub <EOL> def source_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _projection_list [ <NUM_LIT:0> ] . source_ids ( ) <EOL> def destination_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _projection_list [ - <NUM_LIT:1> ] . destination_ids ( ) <EOL> @ InputFileKey ( "<STR_LIT:file>" ) <EOL> class TableProjection ( Projection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source = "<STR_LIT>" , destination = "<STR_LIT>" , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( TableProjection , self ) . __init__ ( source , destination , ** kwargs ) <EOL> if kwargs : <EOL> if '<STR_LIT>' not in kwargs or '<STR_LIT>' not in kwargs or '<STR_LIT>' not in kwargs : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> self . _table = kwargs [ "<STR_LIT>" ] <EOL> self . _source_key = kwargs [ "<STR_LIT>" ] <EOL> self . _destination_key = kwargs [ "<STR_LIT>" ] <EOL> self . _source_dict = dict ( ) <EOL> self . _destination_dict = dict ( ) <EOL> key_lists = self . _table . attributes_by_identifiers ( <EOL> self . _table . identifiers ( ) , <EOL> [ self . _source_key , self . _destination_key ] , <EOL> unique = False ) <EOL> for source , destination in zip ( * key_lists ) : <EOL> if source in self . _source_dict : <EOL> self . _source_dict [ source ] . append ( destination ) <EOL> else : <EOL> self . _source_dict [ source ] = [ destination ] <EOL> if destination in self . _destination_dict : <EOL> self . _destination_dict [ destination ] . append ( source ) <EOL> else : <EOL> self . _destination_dict [ destination ] = [ source ] <EOL> def project ( self , subdomain , destination ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( ) <EOL> if destination == self . destination : <EOL> for domain_id in subdomain : <EOL> keys . extend ( self . _source_dict [ domain_id ] ) <EOL> else : <EOL> for domain_id in subdomain : <EOL> keys . extend ( self . _destination_dict [ domain_id ] ) <EOL> return SubDomain . instantiate ( destination , list ( set ( keys ) ) ) <EOL> def source_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ x for x in self . _source_dict ] <EOL> def destination_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ x for x in self . _destination_dict ] <EOL> @ InputFileKey ( "<STR_LIT>" ) <EOL> class NodeLinkProjection ( Projection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source = "<STR_LIT>" , destination = "<STR_LIT>" , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( NodeLinkProjection , self ) . __init__ ( Nodes ( ) , Links ( ) , ** kwargs ) <EOL> if kwargs : <EOL> if '<STR_LIT>' not in kwargs or '<STR_LIT>' not in kwargs or '<STR_LIT>' not in kwargs : <EOL> raise ValueError ( "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> self . run = kwargs [ '<STR_LIT>' ] <EOL> self . node_policy = kwargs [ '<STR_LIT>' ] <EOL> self . link_policy = kwargs [ '<STR_LIT>' ] <EOL> hardware_info = self . run [ "<STR_LIT>" ] <EOL> self . source_table = self . run . getTable ( hardware_info [ "<STR_LIT>" ] ) <EOL> self . destination_table = self . run . getTable ( <EOL> hardware_info [ "<STR_LIT>" ] ) <EOL> self . coords = hardware_info [ "<STR_LIT>" ] <EOL> self . source_coords = [ hardware_info [ "<STR_LIT>" ] [ coord ] <EOL> for coord in self . coords ] <EOL> self . destination_coords = [ hardware_info [ "<STR_LIT>" ] [ coord ] <EOL> for coord in self . coords ] <EOL> self . node_coord_dict = dict ( ) <EOL> self . coord_node_dict = dict ( ) <EOL> self . link_coord_dict_source = dict ( ) <EOL> self . link_coord_dict_destination = dict ( ) <EOL> self . coord_link_dict_source = dict ( ) <EOL> self . coord_link_dict_destination = dict ( ) <EOL> node_coords , node_ids = self . source_table . _table . group_attributes_by_attributes ( <EOL> self . source_table . _table . identifiers ( ) , self . coords , <EOL> [ self . source_table [ '<STR_LIT>' ] ] , '<STR_LIT>' ) <EOL> for node_coord , node_id in zip ( node_coords , node_ids [ <NUM_LIT:0> ] ) : <EOL> self . node_coord_dict [ int ( node_id ) ] = node_coord <EOL> self . coord_node_dict [ node_coord ] = int ( node_id ) <EOL> coord_len = len ( self . coords ) <EOL> source_index = coord_len <EOL> destination_index = source_index + coord_len <EOL> link_coord_names = list ( ) <EOL> link_coord_names . extend ( self . source_coords ) <EOL> link_coord_names . extend ( self . destination_coords ) <EOL> link_coords , link_ids = self . destination_table . _table . group_attributes_by_attributes ( <EOL> self . destination_table . _table . identifiers ( ) , <EOL> link_coord_names , [ self . destination_table [ '<STR_LIT>' ] ] , '<STR_LIT>' ) <EOL> for link_id , link_tuple in zip ( link_ids [ <NUM_LIT:0> ] , link_coords ) : <EOL> link_id = int ( link_id ) <EOL> source = link_tuple [ <NUM_LIT:0> : source_index ] <EOL> destination = link_tuple [ source_index : destination_index ] <EOL> if link_id not in self . link_coord_dict_source : <EOL> self . link_coord_dict_source [ link_id ] = source <EOL> if link_id not in self . link_coord_dict_destination : <EOL> self . link_coord_dict_destination [ link_id ] = destination <EOL> if source not in self . coord_link_dict_source : <EOL> self . coord_link_dict_source [ source ] = [ link_id ] <EOL> elif link_id not in self . coord_link_dict_source [ source ] : <EOL> self . coord_link_dict_source [ source ] . append ( link_id ) <EOL> if destination not in self . coord_link_dict_destination : <EOL> self . coord_link_dict_destination [ destination ] = [ link_id ] <EOL> elif link_id not in self . coord_link_dict_destination [ destination ] : <EOL> self . coord_link_dict_destination [ destination ] . append ( <EOL> link_id ) <EOL> self . make_dicts ( ) <EOL> def make_dicts ( self ) : <EOL> """<STR_LIT>""" <EOL> self . node_dict = dict ( ) <EOL> self . link_dict = dict ( ) <EOL> for node_id in self . node_coord_dict : <EOL> link_list = list ( ) <EOL> if self . node_policy == '<STR_LIT>' or self . node_policy == '<STR_LIT>' : <EOL> link_list . extend ( self . coord_link_dict_source [ <EOL> self . node_coord_dict [ node_id ] ] ) <EOL> if self . node_policy == '<STR_LIT>' or self . node_policy == '<STR_LIT>' : <EOL> link_list . extend ( self . coord_link_dict_destination [ <EOL> self . node_coord_dict [ node_id ] ] ) <EOL> for link_id in link_list : <EOL> if link_id not in self . link_dict : <EOL> self . link_dict [ link_id ] = [ node_id ] <EOL> elif node_id not in self . link_dict [ link_id ] : <EOL> self . link_dict [ link_id ] . append ( node_id ) <EOL> self . node_dict [ node_id ] = link_list <EOL> def project ( self , subdomain , destination ) : <EOL> """<STR_LIT>""" <EOL> keys = list ( ) <EOL> if destination == self . destination : <EOL> for node_id in subdomain : <EOL> keys . extend ( self . node_dict [ node_id ] ) <EOL> else : <EOL> for link_id in subdomain : <EOL> keys . extend ( self . link_dict [ link_id ] ) <EOL> return SubDomain . instantiate ( destination , list ( set ( keys ) ) ) <EOL> def update_policies ( self , node_policy , link_policy ) : <EOL> """<STR_LIT>""" <EOL> self . node_policy = node_policy <EOL> self . link_policy = link_policy <EOL> self . makedicts ( ) <EOL> def source_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ x for x in self . node_dict ] <EOL> def destination_ids ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ x for x in self . link_dict ] </s>
