def __sentence__(self, mutant: mut.Mutant): sentence = list() for state_error in mutant.get_features().get_state_errors(): constraints = mutant.get_features().get_constraints_for(state_error) sentence.append(mutant.get_mutant_space().program.get_file_name() + "::" + str(state_error)) for constraint in constraints: constraint: mut.StateConstraint sentence.append(mutant.get_mutant_space().program.get_file_name() + "::" + str(constraint.get_execution()) + "::" + constraint.get_condition().generate_code(True)) self.sentences.append(TaggedDocument(sentence, str(sentence))) return sentence
def __key__(self, mutant: mut.Mutant): words = set() for state_error in mutant.get_features().get_state_errors(): word = mutant.get_mutant_space().program.get_file_name() word += "::" + str(state_error) words.add(word) constraints = mutant.get_features().get_constraints_for(state_error) for constraint in constraints.get_constraints(): word += mutant.get_mutant_space().program.get_file_name() word += str(constraint.get_execution()) + "::" word += constraint.get_condition().generate_code(True) words.add(word) return str(self.word2int.encodes(words))
def print_propagate_context(mutant: mut.Mutant, filename: str, view=False, max_distance=1): program = mutant.get_mutant_space().get_program() program: cprog.CProgram instance_graph = program.get_instance_graph() instance_graph: cirinst.CirInstanceGraph dependence_graph = program.get_dependence_graph() dependence_graph: cprog.CDependenceGraph location, source_error = None, None for state_error in mutant.get_features().get_state_errors(): state_error: mut.StateError for operand in state_error.get_operands(): if isinstance(operand, cirtree.CirNode): location = operand break if location is not None: source_error = state_error break if location is not None: location: cirtree.CirNode source_error: mut.StateError source_instance = None for instance_node in instance_graph.get_nodes(): instance_node: cirinst.CirInstanceNode for instance_code in instance_node.get_cir_instances_in(): instance_code: cirinst.CirInstanceCode if instance_code.get_cir_source_node() == location: source_instance = instance_code break if source_instance is not None: break if source_instance is not None: dependence_node = dependence_graph.get_node(source_instance) propagate_tree = CDependenceTree.influence_tree(dependence_node, max_distance) di_graph = graphviz.Digraph(name=filename + ".propagate." + str(mutant.id)) propagate_tree.print_graph_visual(di_graph) di_graph.node(str(source_error), label=str(source_error)) di_graph.edge(str(source_error), str(propagate_tree.root), label="seeded_in") di_graph.render(filename=filename + ".propagate." + str(mutant.id), view=view) return
def encode_feature_vectors(self, mutant: mut.Mutant): """ :param mutant: :return: feature vector encoded by doc2vec in average weights """ infection = mutant.get_features() infection: mut.StateInfection program = mutant.get_mutant_space().program program: cpro.CProgram sentence_dict = dict() for state_error, state_constraints in infection.error_infections.items( ): state_error: mut.StateError state_constraints: mut.StateConstraints error_information_dict = ErrorInformationPath.error_information_paths( program, state_error, self.path_distance) for error_information_paths in error_information_dict.values(): for error_information_path in error_information_paths: # encoding error information path word_list, code_list = error_information_path.get_words( self.__corpus__) sentence_dict[str(word_list)] = word_list for state_constraint in state_constraints.get_constraints(): state_constraint: mut.StateConstraint constraint_dependence_dict = ConstraintDependencePath.constraint_dependence_paths( program, state_constraint, self.path_distance, self.optimize, self.sym_depth) for constraint_dependence_paths in constraint_dependence_dict.values( ): for constraint_dependence_path in constraint_dependence_paths: # encoding constraint path word_list, code_list = constraint_dependence_path.get_words( self.__corpus__) sentence_dict[str(word_list)] = word_list self.doc2vec: Doc2Vec feature_vectors = list() for key, sentence in sentence_dict.items(): feature_vector = self.doc2vec.infer_vector(sentence) feature_vectors.append(feature_vector) return self.__average_feature_vectors__(feature_vectors)
def encoding_word_vectors(self, mutant: mut.Mutant): """ :param mutant: :return: set of feature path words as sentences """ infection = mutant.get_features() infection: mut.StateInfection program = mutant.get_mutant_space().program program: cpro.CProgram sentence_dict = dict() for state_error, state_constraints in infection.error_infections.items( ): state_error: mut.StateError state_constraints: mut.StateConstraints error_information_dict = ErrorInformationPath.error_information_paths( program, state_error, self.path_distance) for error_information_paths in error_information_dict.values(): for error_information_path in error_information_paths: # encoding error information path word_list, code_list = error_information_path.get_words( self.__corpus__) sentence_dict[str(word_list)] = word_list for state_constraint in state_constraints.get_constraints(): state_constraint: mut.StateConstraint constraint_dependence_dict = ConstraintDependencePath.constraint_dependence_paths( program, state_constraint, self.path_distance, self.optimize, self.sym_depth) for constraint_dependence_paths in constraint_dependence_dict.values( ): for constraint_dependence_path in constraint_dependence_paths: # encoding constraint path word_list, code_list = constraint_dependence_path.get_words( self.__corpus__) sentence_dict[str(word_list)] = word_list tagged_sentences = list() for key, sentence in sentence_dict.items(): tagged_sentence = TaggedDocument(words=sentence, tags=key) tagged_sentences.append(tagged_sentence) self.tagged_sentences.append(tagged_sentence) return tagged_sentences
def print_infect_context(mutant: mut.Mutant, filename: str, view=False, max_distance=1): program = mutant.get_mutant_space().get_program() program: cprog.CProgram instance_graph = program.get_instance_graph() instance_graph: cirinst.CirInstanceGraph dependence_graph = program.get_dependence_graph() dependence_graph: cprog.CDependenceGraph start_execution = mutant.get_features().get_faulty_execution() start_instances = list() for instance_node in instance_graph.get_nodes(): instance_node: cirinst.CirInstanceNode if instance_node.get_source_execution() == start_execution: start_instances.append(instance_node) di_graph = graphviz.Digraph(name=filename + ".infect." + str(mutant.id)) if len(start_instances) > 0: start_instance = start_instances[random.randint(0, len(start_instances) - 1)] dependence_node = dependence_graph.get_node(start_instance) data_dependence_tree = CDependenceTree.data_dependence_tree(dependence_node, max_distance) data_dependence_tree.print_graph_visual(di_graph) di_graph.render(filename=filename + ".infect." + str(mutant.id), view=view) return
def __paths__(self, mutant: mut.Mutant, max_distance: int): """ :param mutant: :param max_distance: :return: coverage-tree, infection-tree and propagation-tree """ program = mutant.get_mutant_space().get_program() program: cprog.CProgram instance_graph = program.get_instance_graph() instance_graph: cirinst.CirInstanceGraph dependence_graph = program.get_dependence_graph() dependence_graph: cprog.CDependenceGraph paths = dict() start_execution = mutant.get_features().get_faulty_execution() start_instances = list() for instance_node in instance_graph.get_nodes(): instance_node: cirinst.CirInstanceNode if instance_node.get_source_execution() == start_execution: start_instances.append(instance_node) if len(start_instances) > 0: start_instance = start_instances[random.randint(0, len(start_instances) - 1)] dependence_node = dependence_graph.get_node(start_instance) coverage_tree = CDependenceTree.control_dependence_tree(dependence_node, max_distance) infection_tree = CDependenceTree.data_dependence_tree(dependence_node, max_distance) leafs = coverage_tree.get_leafs() for leaf in leafs: leaf: CDependenceTreeNode orig_path = leaf.path_of() path = list() path.append(str(mutant.mutation.get_mutation_operator())) path.append("seeded_in") for orig_object in orig_path: path.append(self.__word__(orig_object)) key = str(path) if key not in paths: paths[key] = path leafs = infection_tree.get_leafs() for leaf in leafs: leaf: CDependenceTreeNode orig_path = leaf.path_of() path = list() path.append(str(mutant.mutation.get_mutation_operator())) path.append("seeded_in") for orig_object in orig_path: path.append(self.__word__(orig_object)) key = str(path) if key not in paths: paths[key] = path for state_error in mutant.get_features().get_state_errors(): state_error: mut.StateError for operand in state_error.get_operands(): if isinstance(operand, cirtree.CirNode): operand: cirtree.CirNode dependence_nodes = list() for dependence_node in dependence_graph.get_nodes(): dependence_node: cprog.CDependenceNode instance = dependence_node.get_instance() if isinstance(instance, cirinst.CirInstanceCode): instance: cirinst.CirInstanceCode if instance.get_cir_source_node() == operand: dependence_nodes.append(dependence_node) if len(dependence_nodes) > 0: dependence_node = dependence_nodes[random.randint(0, len(dependence_nodes) - 1)] propagation_tree = CDependenceTree.influence_tree(dependence_node, max_distance) leafs = propagation_tree.get_leafs() for leaf in leafs: leaf: CDependenceTreeNode orig_path = leaf.path_of() path = list() path.append(str(state_error.get_error_type())) path.append("caused_to") for orig_object in orig_path: path.append(self.__word__(orig_object)) key = str(path) if key not in paths: paths[key] = path break return paths
def __label__(self, mutant: mut.Mutant): mutant.get_labels().define_category(self.prob_threshold) return mutant.get_labels().get_category().value
def print_words_in_paths(mutant: mut.Mutant, filename: str, max_distance=1): filename = filename + ".path." + str(mutant.id) with open(filename, 'w') as writer: program = mutant.get_mutant_space().get_program() program: cprog.CProgram instance_graph = program.get_instance_graph() instance_graph: cirinst.CirInstanceGraph dependence_graph = program.get_dependence_graph() dependence_graph: cprog.CDependenceGraph writer.write("Mutant:\n") writer.write("Class:\t" + str(mutant.mutation.get_mutation_operator()) + "\n") writer.write("Operator:\t" + str(mutant.mutation.get_mutation_operator()) + "\n") writer.write("Location:\t" + get_ast_code(mutant.mutation.get_location(), 128) + "\n") if mutant.mutation.has_parameter(): writer.write("Parameter:\t" + str(mutant.mutation.get_parameter()) + "\n") writer.write("\n#PATHS:\n") location, source_error = None, None for state_error in mutant.get_features().get_state_errors(): state_error: mut.StateError for operand in state_error.get_operands(): if isinstance(operand, cirtree.CirNode): location = operand break if location is not None: source_error = state_error break if location is not None: location: cirtree.CirNode source_error: mut.StateError source_instance = None for instance_node in instance_graph.get_nodes(): instance_node: cirinst.CirInstanceNode for instance_code in instance_node.get_cir_instances_in(): instance_code: cirinst.CirInstanceCode if instance_code.get_cir_source_node() == location: source_instance = instance_code break if source_instance is not None: break if source_instance is not None: dependence_node = dependence_graph.get_node(source_instance) # generate dependence tree here propagation_tree = CDependenceTree.influence_tree(dependence_node, max_distance) for leaf in propagation_tree.get_leafs(): leaf: CDependenceTreeNode path = leaf.path_of() writer.write(str(mutant.mutation.get_mutation_operator()) + "\t") writer.write("seeded_in") for path_node in path: if isinstance(path_node, cprog.CDependenceNode): path_node: cprog.CDependenceNode instance = path_node.get_instance() if isinstance(instance, cirinst.CirInstanceNode): instance: cirinst.CirInstanceNode writer.write("\t" + str(instance.get_source_statement().cir_type)) else: instance: cirinst.CirInstanceCode writer.write("\t" + str(instance.get_cir_source_node().cir_type)) else: path_node: cprog.CDependenceEdge if path_node.dependence_type == cprog.CDependenceType.parent_child_depend: writer.write("\toperand_of") else: writer.write("\t" + str(path_node.dependence_type)) writer.write("\n") writer.write("\nEND_PATHS") return