def regenerate(self, markov_model_id):
     """ Regenerate the markvov chain """
     try:
         markov_model = self.get_markov_model(int(markov_model_id))
     except KeyError:
         print_error('No such markov model id')
         return False
     label = __group_of_labels__.get_label_by_id(
         markov_model.get_label_id())
     connections = label.get_connections_complete()
     # Get all the group of models and connections names
     state = ""
     for group_of_model_id in connections:
         # Get all the connections
         for conn in connections[group_of_model_id]:
             # Get the model group
             group = __groupofgroupofmodels__.get_group(group_of_model_id)
             # Get the model
             model = group.get_model(conn)
             # Get each state
             state += model.get_state() + '#'
     # Delete the last #
     state = state[:-1]
     # Store the state
     markov_model.set_state(state)
     # Store the connections
     markov_model.set_connections(connections)
     # Create the MM itself
     markov_model.create(markov_model.get_state())
     print_info('Markov model {} regenerated.'.format(markov_model_id))
 def regenerate(self, markov_model_id):
     """ Regenerate the markvov chain """
     try:
         markov_model = self.get_markov_model(int(markov_model_id))
     except KeyError:
         print_error('No such markov model id')
         return False
     label = __group_of_labels__.get_label_by_id(markov_model.get_label_id())
     connections = label.get_connections_complete()
     # Get all the group of models and connections names
     state = ""
     for group_of_model_id in connections:
         # Get all the connections
         for conn in connections[group_of_model_id]:
             # Get the model group
             group = __groupofgroupofmodels__.get_group(group_of_model_id)
             # Get the model
             model = group.get_model(conn)
             # Get each state
             state += model.get_state() + '#'
     # Delete the last #
     state = state[:-1]
     # Store the state
     markov_model.set_state(state)
     # Store the connections
     markov_model.set_connections(connections)
     # Create the MM itself
     markov_model.create(markov_model.get_state())
     print_info('Markov model {} regenerated.'.format(markov_model_id))
 def add_label(self, group_of_model_id, connection_id):
     """ Add a label """
     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
         has_label = self.check_label_existance(group_of_model_id,
                                                connection_id)
         if has_label:
             print_error(
                 'This connection from this dataset was already assigned the label id {}'
                 .format(has_label))
         else:
             print_warning(
                 'Remember that a label should represent a unique behavioral model!'
             )
             try:
                 label_id = self.labels[list(
                     self.labels.keys())[-1]].get_id() + 1
             except (KeyError, IndexError):
                 label_id = 1
             name = self.decide_a_label_name(connection_id)
             if name:
                 previous_label = self.search_label_name(name,
                                                         verbose=False,
                                                         exact=1)
                 if previous_label:
                     label = self.get_label(name)
                     label.add_connection(group_of_model_id, connection_id)
                 else:
                     label = Label(label_id)
                     label.set_name(name)
                     label.add_connection(group_of_model_id, connection_id)
                     self.labels[label_id] = label
                 # 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)
             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 get_the_model_of_a_connection(self, group_of_model_id, connection_id):
     """ Given a connection_id and group of model id, get the model """
     # Get the note id, group_id and group
     dataset_id = int(group_of_model_id.split('-')[0])
     dataset = __datasets__.get_dataset(dataset_id)
     group = __groupofgroupofmodels__.get_group(group_of_model_id)
     try:
         if group.has_model(connection_id):
             model = group.get_model(connection_id)
             return model
     except AttributeError:
         print_error('The connection does not have a model. Probably deleted.')
         return False
 def get_the_model_of_a_connection(self, connection_id):
     """ Given a connection_id and current dataset, get the model """
     if __datasets__.current:
         # Get the note id, group_id and group
         dataset = __datasets__.get_dataset(__datasets__.current.get_id())
         group_of_models = dataset.get_group_of_models()
         for group_id in group_of_models: # Usually is only one group...
             group = __groupofgroupofmodels__.get_group(group_id)
             if group.has_model(connection_id):
                 model = group.get_model(connection_id)
                 return model
     else:
         print_error('There is no dataset selected.')
 def get_the_model_of_a_connection(self, group_of_model_id, connection_id):
     """ Given a connection_id and group of model id, get the model """
     # Get the note id, group_id and group
     dataset_id = int(group_of_model_id.split('-')[0])
     dataset = __datasets__.get_dataset(dataset_id)
     group = __groupofgroupofmodels__.get_group(group_of_model_id)
     try:
         if group.has_model(connection_id):
             model = group.get_model(connection_id)
             return model
     except AttributeError:
         print_error(
             'The connection does not have a model. Probably deleted.')
         return False
 def create_new_model(self, label_name, number_of_flows):
     """ Given a label name create a new markov chain object"""
     # Get the label object
     label_to_model = __group_of_labels__.get_label(label_name)
     if label_to_model:
         # Create a new markov chain object
         ## Get the new id
         try:
             mm_id = self.markov_models[list(
                 self.markov_models.keys())[-1]].get_id() + 1
         except (KeyError, IndexError):
             mm_id = 1
         markov_model = Markov_Model(mm_id)
         # Store the label id
         markov_model.set_label_id(label_to_model.get_id())
         state = ""
         # Get all the connections in the label
         connections = label_to_model.get_connections_complete()
         # Get all the group of models and connections names
         for group_of_model_id in connections:
             # Get all the connections
             for conn in connections[group_of_model_id]:
                 # Get the model group
                 group = __groupofgroupofmodels__.get_group(
                     group_of_model_id)
                 # Get the model
                 model = group.get_model(conn)
                 # Get each state
                 state += model.get_state() + '#'
         # Delete the last #
         state = state[:-1]
         # Store the state
         markov_model.set_state(state)
         # Store the connections
         markov_model.set_connections(connections)
         # Create the MM itself
         markov_model.create(markov_model.get_state())
         # Generate the self probability
         prob = markov_model.compute_probability(markov_model.get_state())
         markov_model.set_self_probability(prob)
         # Store
         self.markov_models[mm_id] = markov_model
         print_info('New model created with id {}'.format(
             markov_model.get_id()))
     else:
         print_error('No label with that name')
 def create_new_model(self, label_name, number_of_flows):
     """ Given a label name create a new markov chain object"""
     # Get the label object
     label_to_model = __group_of_labels__.get_label(label_name)
     if label_to_model:
         # Create a new markov chain object
         ## Get the new id
         try:
             mm_id = self.markov_models[list(self.markov_models.keys())[-1]].get_id() + 1
         except (KeyError, IndexError):
             mm_id = 1
         markov_model = Markov_Model(mm_id)
         # Store the label id
         markov_model.set_label_id(label_to_model.get_id())
         state = ""
         # Get all the connections in the label
         connections = label_to_model.get_connections_complete()
         # Get all the group of models and connections names
         for group_of_model_id in connections:
             # Get all the connections
             for conn in connections[group_of_model_id]:
                 # Get the model group
                 group = __groupofgroupofmodels__.get_group(group_of_model_id)
                 # Get the model
                 model = group.get_model(conn)
                 # Get each state
                 #state += model.get_state() + '#'
                 state += model.get_state() 
         # Delete the last #
         #state = state[:-1]
         # Store the state
         markov_model.set_state(state)
         # Store the connections
         markov_model.set_connections(connections)
         # Create the MM itself
         markov_model.create(markov_model.get_state())
         # Generate the self probability
         prob = markov_model.compute_probability(markov_model.get_state())
         markov_model.set_self_probability(prob)
         # Store
         self.markov_models[mm_id] = markov_model
         print_info('New model created with id {}'.format(markov_model.get_id()))
     else:
         print_error('No label with that name')
 def add_label(self, group_of_model_id, connection_id):
     """ Add a label """
     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
         has_label = self.check_label_existance(group_of_model_id, connection_id)
         if has_label:
             print_error('This connection from this dataset was already assigned the label id {}'.format(has_label))
         else:
             print_warning('Remember that a label should represent a unique behavioral model!')
             try:
                 label_id = self.labels[list(self.labels.keys())[-1]].get_id() + 1
             except (KeyError, IndexError):
                 label_id = 1
             name = self.decide_a_label_name(connection_id)
             if name:
                 previous_label = self.search_label_name(name, verbose=False, exact=1)
                 if previous_label:
                     label = self.get_label(name)
                     label.add_connection(group_of_model_id, connection_id)
                 else:
                     label = Label(label_id)
                     label.set_name(name)
                     label.add_connection(group_of_model_id, connection_id)
                     self.labels[label_id] = label
                 # 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)
             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)
             # 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.')