def generate_group_of_models(self, constructor_id): if __datasets__.current: # Get the id for the current dataset dataset_id = __datasets__.current.get_id() # We should check that there is a group of connections already for this dataset if not __group_of_group_of_connections__.get_group(dataset_id): # There are not group of connections for this dataset, just generate it print_info('There were no connections for this dataset. Generate them first.') return False # Get the id of the groups of connections these models are related to group_connection = __group_of_group_of_connections__.get_group(dataset_id) if group_connection: group_connection_id = group_connection.get_id() else: print_error('There are no connections for this dataset yet. Please generate them.') # The id of this group of models is the id of the dataset + the id of the model constructor. Because we can have the same connnections modeled by different constructors. #group_of_models_id = str(dataset_id) + '-' + str(__modelsconstructors__.get_default_constructor().get_id()) group_of_models_id = str(dataset_id) + '-' + str(constructor_id) # If we already have a group of models, ask what to do try: group_of_models = self.group_of_models[group_of_models_id] print_warning('There is already a group of models for this dataset. Do you want to delete the current models and create a new one?') answer = raw_input('YES/NO?') if answer == 'YES': # First delete the old models self.delete_group_of_models(group_of_models_id) else: return False except KeyError: # first time. Not to repeat the code, we leave this empty and we do a new try pass # Do we have the group of models for this id? try: group_of_models = self.group_of_models[group_of_models_id] except KeyError: # First time. # Create the group of models group_of_models = Group_of_Models(group_of_models_id) # Set the group of connections they will be using group_of_models.set_group_connection_id(group_connection_id) # Set the dataset id for this group of models group_of_models.set_dataset_id(dataset_id) # Set the model constructor used for all the models group_of_models.set_constructor_id(constructor_id) # Store the model self.group_of_models[group_of_models_id] = group_of_models # Update the dataset to include this group of models __datasets__.current.add_group_of_models(group_of_models_id) # Generate the models group_of_models.generate_models() else: print_error('There is no dataset selected.')
def generate_group_of_models(self): if __datasets__.current: # Get the id for the current dataset dataset_id = __datasets__.current.get_id() # We should check that there is a group of connections already for this dataset if not __group_of_group_of_connections__.get_group(dataset_id): # There are not group of connections for this dataset, just generate it print_info( 'There were no connections for this dataset. Generate them first.' ) return False # Get the id of the groups of connections these models are related to group_connection = __group_of_group_of_connections__.get_group( dataset_id) if group_connection: group_connection_id = group_connection.get_id() else: print_error( 'There are no connections for this dataset yet. Please generate them.' ) # The id of this group of models is the id of the dataset + the id of the model constructor. Because we can have the same connnections modeled by different constructors. group_of_models_id = str(dataset_id) + '-' + str( __modelsconstructors__.get_default_constructor().get_id()) # Do we have the group of models for this id? try: group_of_models = self.group_of_models[group_of_models_id] except KeyError: # First time. # Create the group of models group_of_models = Group_of_Models(group_of_models_id) # Set the group of connections they will be using group_of_models.set_group_connection_id(group_connection_id) # Set the dataset id for this group of models group_of_models.set_dataset_id(dataset_id) # Store the model self.group_of_models[group_of_models_id] = group_of_models # Update the dataset to include this group of models __datasets__.current.add_group_of_models(group_of_models_id) # Generate the models group_of_models.generate_models() else: print_error('There is no dataset selected.')
def get_flow_label(self, model_group_id): """ Returns the label in the first flow on the connections """ # Horrible to get the model group id in a parameter... i know # Get the group of connections id group_of_connections_id = int(model_group_id.split('-')[0]) group_of_connections = __group_of_group_of_connections__.get_group(group_of_connections_id) # Get the flow label. This is horrible and we should not do it, but we need to access the first connection in the list... so just access the dict directly... connection = group_of_connections.connections[self.get_id()] return connection.get_label()
def generate_group_of_models(self): if __datasets__.current: # Get the id for the current dataset dataset_id = __datasets__.current.get_id() # We should check that there is a group of connections already for this dataset if not __group_of_group_of_connections__.get_group(dataset_id): # There are not group of connections for this dataset, just generate it print_info('There were no connections for this dataset. Generate them first.') return False # Get the id of the groups of connections these models are related to group_connection = __group_of_group_of_connections__.get_group(dataset_id) if group_connection: group_connection_id = group_connection.get_id() else: print_error('There are no connections for this dataset yet. Please generate them.') # The id of this group of models is the id of the dataset + the id of the model constructor. Because we can have the same connnections modeled by different constructors. group_of_models_id = str(dataset_id) + '-' + str(__modelsconstructors__.get_default_constructor().get_id()) # Do we have the group of models for this id? try: group_of_models = self.group_of_models[group_of_models_id] except KeyError: # First time. # Create the group of models group_of_models = Group_of_Models(group_of_models_id) # Set the group of connections they will be using group_of_models.set_group_connection_id(group_connection_id) # Set the dataset id for this group of models group_of_models.set_dataset_id(dataset_id) # Store the model self.group_of_models[group_of_models_id] = group_of_models # Update the dataset to include this group of models __datasets__.current.add_group_of_models(group_of_models_id) # Generate the models group_of_models.generate_models() else: print_error('There is no dataset selected.')
def generate_models(self): """ Generate all the individual models. We are related with only one dataset and connection group. """ # Get the group of connections from the id group_of_connections = __group_of_group_of_connections__.get_group(self.get_group_connection_id()) # For each connection for connection in group_of_connections.get_connections(): # Create its model. Remember that the connection id and the model id is the 4-tuple model_id = connection.get_id() new_model = Model(model_id) # Set the constructor for this model. Each model has a specific way of constructing the states new_model.set_constructor(__modelsconstructors__.get_default_constructor()) for flow in connection.get_flows(): new_model.add_flow(flow) self.models[model_id] = new_model
def generate_models(self): """ Generate all the individual models. We are related with only one dataset and connection group. """ # Get the group of connections from the id group_of_connections = __group_of_group_of_connections__.get_group( self.get_group_connection_id()) # For each connection for connection in group_of_connections.get_connections(): # Create its model. Remember that the connection id and the model id is the 4-tuple model_id = connection.get_id() new_model = Model(model_id) # Set the constructor for this model. Each model has a specific way of constructing the states new_model.set_constructor( __modelsconstructors__.get_default_constructor()) for flow in connection.get_flows(): new_model.add_flow(flow) self.models[model_id] = new_model
def generate_models(self): """ Generate all the individual models. We are related with only one dataset and connection group. """ # Get the group of connections from the id group_of_connections = __group_of_group_of_connections__.get_group(self.get_group_connection_id()) # For each connection for connection in group_of_connections.get_connections(): # Create its model. Remember that the connection id and the model id is the 4-tuple model_id = connection.get_id() new_model = Model(model_id) # Set the constructor for this model. Each model has a specific way of constructing the states #new_model.set_constructor(__modelsconstructors__.get_default_constructor()) constructor_id = self.get_constructor_id() new_model.set_constructor(__modelsconstructors__.get_constructor(constructor_id)) for flow in connection.get_flows(): # Try to add the flow if not new_model.add_flow(flow): self.delete_model_by_id(new_model.get_id()) # The flows are not ordered. Delete the truckated models __groupofgroupofmodels__.delete_group_of_models(self.get_id()) return False self.models[model_id] = new_model
def add_label_with_filter(self, group_of_model_id, filter): """ Add a label using the filters to a group of connection ids""" if __datasets__.current: dataset_id_standing = __datasets__.current.get_id() dataset_id = group_of_model_id.split('-')[0] if str(dataset_id_standing) != dataset_id: print_error('You should select the dataset you are going to work in. Not another') return False if not __groupofgroupofmodels__.get_group(group_of_model_id): print_error('That group of models does not exist.') return False print_warning('Remember that a label should represent a unique behavioral model!') # Get the base name of the labels temp_general_name = self.decide_a_label_name("") # Connection id is empty because we are not checking the protos. This is used because we need to assign a label to multiple connections id. if temp_general_name: # The general name of the label is the one every connection will share except the final number that will change. # The general_name_id is the final number that will keep changing general_name = '-'.join(temp_general_name.split('-')[0:-1]) general_name_id = int(temp_general_name.split('-')[-1]) # Get all the connections from the connection model group_of_connections_id = int(group_of_model_id.split('-')[0]) group_of_connections = __group_of_group_of_connections__.get_group(group_of_connections_id) connections = group_of_connections.get_connections() # Construct the filter self.construct_filter(filter) # Check we are using the correct filters for temp_filter in self.filter: if temp_filter[0] != "connid" : print_error('Adding labels with a filter only supports the type of filter connid= and connid!=') return False elif temp_filter[1] != "=" and temp_filter[1] != "!=": print_error('Adding labels with a filter only supports the type of filter connid= and connid!=') return False for connection in connections: connection_id = connection.get_id() has_label = self.check_label_existance(group_of_model_id, connection_id) # Get next label id try: label_id = self.labels[list(self.labels.keys())[-1]].get_id() + 1 except (KeyError, IndexError): label_id = 1 # Obtain the name name = general_name + '-' + str(general_name_id) proto = general_name.split('-')[2] label = Label(label_id) label.set_name(name) label.add_connection(group_of_model_id, connection_id) if self.apply_filter(label): if has_label: #print_error('This connection from this dataset was already assigned the label id {}. We did not change it.'.format(has_label)) continue # Add label id to the model self.add_label_to_model(group_of_model_id, connection_id, name) # add auto note with the label to the model self.add_auto_label_for_connection(group_of_model_id, connection_id, name) self.labels[label_id] = label general_name_id += 1 else: del label else: # This is not necesary, but is a precaution print_error('Aborting the assignment of the label.') return False else: print_error('There is no dataset selected.')
def add_label_with_filter(self, group_of_model_id, filter): """ Add a label using the filters to a group of connection ids""" if __datasets__.current: dataset_id_standing = __datasets__.current.get_id() dataset_id = group_of_model_id.split('-')[0] if str(dataset_id_standing) != dataset_id: print_error( 'You should select the dataset you are going to work in. Not another' ) return False if not __groupofgroupofmodels__.get_group(group_of_model_id): print_error('That group of models does not exist.') return False print_warning( 'Remember that a label should represent a unique behavioral model!' ) # Get the base name of the labels temp_general_name = self.decide_a_label_name( "" ) # Connection id is empty because we are not checking the protos. This is used because we need to assign a label to multiple connections id. if temp_general_name: # The general name of the label is the one every connection will share except the final number that will change. # The general_name_id is the final number that will keep changing general_name = '-'.join(temp_general_name.split('-')[0:-1]) general_name_id = int(temp_general_name.split('-')[-1]) # Get all the connections from the connection model group_of_connections_id = int(group_of_model_id.split('-')[0]) group_of_connections = __group_of_group_of_connections__.get_group( group_of_connections_id) connections = group_of_connections.get_connections() # Construct the filter self.construct_filter(filter) for connection in connections: connection_id = connection.get_id() has_label = self.check_label_existance( group_of_model_id, connection_id) # Get next label id try: label_id = self.labels[list( self.labels.keys())[-1]].get_id() + 1 except (KeyError, IndexError): label_id = 1 # Obtain the name of this connection name = general_name + '-' + str(general_name_id) proto = general_name.split('-')[2] label = Label(label_id) label.set_name(name) label.add_connection(group_of_model_id, connection_id) if self.apply_filter(label, groupofmodelid=group_of_model_id, connectionid=connection_id): if has_label: # his connection from this dataset was already assigned this id. We did not change it continue # Add the label id to the model self.add_label_to_model(group_of_model_id, connection_id, name) # add auto note with the label to the model self.add_auto_label_for_connection( group_of_model_id, connection_id, name) # Update the label list self.labels[label_id] = label general_name_id += 1 else: del label else: # This is not necesary, but is a precaution print_error('Aborting the assignment of the label.') return False else: print_error('There is no dataset selected.')