A software defect prediction method and system

By using abstract syntax trees and row attribute graphs in software defect prediction, combined with Transformer encoders and graph convolutional networks, a comprehensive representation vector of code lines is generated, which solves the problem of computational complexity and lack of semantic information caused by the large number of AST nodes. , achieving more accurate software defect prediction.

CN118672594BActive Publication Date: 2024-10-29SHANDONG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411170015.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-26
Publication Date
2024-10-29
Estimated Expiration
2044-08-26

AI Technical Summary

Technical Problem

When existing software defect prediction methods utilize the syntax and semantic information of source code, there are problems such as high computational complexity caused by the large number of AST nodes, lack of explicit representation of semantic information of control flow data flow, and difficulty in detecting fine-grained defects.

Method used

Abstract syntax tree (AST) is used to build multi-label nodes, combined with row attribute graphs, Transformer encoder and multi-label contrastive learning model are used to generate syntax representation vectors, semantic dependencies are learned through graph convolution network, and adaptive weight fusion and soft The attention mechanism generates comprehensive representation vectors of code lines for software defect prediction.

Benefits of technology

It achieves a more complete representation of source code information, improves the accuracy and performance of defect prediction, can more accurately detect hidden defects, and reduces computational complexity and memory usage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118672594B_ABST
    Figure CN118672594B_ABST
Patent Text Reader

Abstract

The present invention belongs to the technical field of software defect prediction, and provides a software defect prediction method and system, which parses the source code file contained in the software system into an abstract syntax tree, extracts feature nodes and label nodes therefrom, extracts the control flow and data flow dependencies between code lines, and constructs a line attribute graph; generates a syntax representation vector of the feature node, and calculates the multi-label contrast learning loss value, and then gradually generates a file representation vector; uses the attention weights generated in the process of generating the obtained file representation vector to perform a software defect prediction task at the code line level, and uses the obtained file representation vector to perform defect prediction at the file level. The present invention can comprehensively mine the grammatical semantic information of the source code, more completely represent the information contained in the source code, and make more accurate predictions on modules with hidden defects.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of software defect prediction, and in particular relates to a software defect prediction method and system. Background Art

[0002] The existing methods for dealing with defect prediction tasks can be divided into machine learning-based methods and deep learning-based methods. Among them, machine learning-based methods usually use the features of traditional hand-crafted code as input for defect prediction, while deep learning-based methods usually use deep learning models to mine the grammatical semantic information of the source code itself for defect prediction. Traditional manual labeling often relies on expert experience, has certain subjective biases, and can only capture limited information. Many researchers use the Abstract Syntax Tree (AST) of software source code to learn grammatical structure information, or build graph structures to learn the semantic information of the code to complete the defect prediction task.

[0003] However, these methods have some limitations, such as the large number of nodes in AST, which increases the computational complexity of neural networks, and the lack of explicit representation of semantic information such as control flow and data flow in AST. In addition, in the defect prediction task, there is still a lack of methods that can fully utilize syntactic and semantic information to effectively detect fine-grained defects in source code. Summary of the invention

[0004] In order to solve the above problems, the present invention proposes a software defect prediction method and system, which can comprehensively mine the syntax and semantic information of the source code, more completely represent the information contained in the source code, and make more accurate predictions on modules with hidden defects.

[0005] According to some embodiments, the present invention adopts the following technical solutions:

[0006] A first aspect of the present invention provides a software defect prediction method, comprising the following steps:

[0007] Parse the source code files contained in the software system into an abstract syntax tree, extract feature nodes and label nodes from them, and generate initial feature node representation vectors and multi-hot encodings of label nodes respectively;

[0008] Parse the source code files included in the software system into code attribute graph files, extract the control flow and data flow dependencies between code lines, and construct a line attribute graph;

[0009] Using the obtained initial feature node representation vector and the multi-hot encoding of the label node, a learning model combining the Transformer encoder and multi-label contrastive learning is constructed to generate the grammatical representation vector of the feature node and calculate the multi-label contrastive learning loss value;

[0010] Using the obtained grammatical representation vectors of feature nodes, a soft attention mechanism is used to weightedly fuse the representation vectors of feature nodes located in the same code line to generate the grammatical representation vector of the code line.

[0011] Using the obtained grammatical representation vector of the code line and the constructed line attribute graph, a graph convolutional network is used to learn the semantic dependency between source code lines and generate the semantic representation vector of the code line.

[0012] Adopting the adaptive weight fusion method, the grammatical representation vector and semantic representation vector of the obtained code line are fused to generate a comprehensive representation vector of the code line;

[0013] The comprehensive representation vectors of all code lines in the same code file are used to generate a file representation vector by weighted fusion using a soft attention mechanism.

[0014] The attention weights generated during the generation of the obtained file representation vectors are used to perform software defect prediction tasks at the code line level, and the obtained file representation vectors are used to perform file-level defect prediction.

[0015] As an optional implementation, the following steps are also included: calculating the defect prediction loss value output by the softmax function, and based on the obtained multi-label comparative learning loss value and the defect prediction loss value, adjusting the parameters in the model using the gradient back propagation algorithm to optimize the defect prediction accuracy.

[0016] As an optional implementation method, after the model training is completed, data that did not participate in the training process is used for testing, the model test results are compared with the actual situation, and the model is optimized and adjusted, so as to continuously optimize the weight data in the model and continuously improve the accuracy of software defect prediction.

[0017] As an optional implementation, the source code file included in the software system is parsed into an abstract syntax tree, and the process of extracting feature nodes and label nodes therefrom includes:

[0018] The source code files contained in the software system are parsed into an abstract syntax tree, and the leaf nodes are used as feature nodes to learn the representation vectors of the features of the source code. The non-leaf nodes representing the variable grammar rules are mapped to label nodes of the feature nodes.

[0019] As an optional implementation method, the process of constructing a row attribute graph includes: defining the row attribute graph in combination with a data flow graph and a program dependency graph, the data flow graph is a set of directed edges constructed by executing control conditions between corresponding lines of code in the corresponding source code and program lines, and the program dependency graph is a set of directed edges generated by corresponding lines of code in the corresponding source code and data dependencies and control dependencies of predicates on variable values. The combination of the two is the row attribute graph.

[0020] As an optional implementation, the process of constructing a learning model combining a Transformer encoder and multi-label contrastive learning, generating a grammatical representation vector of a feature node, and calculating a multi-label contrastive learning loss value includes:

[0021] Use the encoder structure in the Transformer framework to learn the relationship between feature nodes;

[0022] For instance feature nodes in the dataset, a candidate set of instance pairs is defined, and the contrastive learning loss value of each instance pair is calculated.

[0023] As an optional implementation, the obtained grammatical representation vector of the code line and the constructed line attribute graph are used to adopt a graph convolutional network to learn the semantic dependency relationship between source code lines and generate a semantic representation vector of the code line;

[0024] Adopting the adaptive weight fusion method, the grammatical representation vector and semantic representation vector of the obtained code line are fused to generate a comprehensive representation vector of the code line;

[0025] Using the comprehensive representation vectors of all code lines in the same code file, the soft attention mechanism is used for weighted fusion. The process of generating the file representation vector includes:

[0026] Based on the obtained grammatical representation vector of the feature node, the node in the row attribute graph is the code line, and the soft attention mechanism is used to aggregate the feature node vector in each line to obtain the grammatical representation vector of the code line;

[0027] Based on the obtained grammatical representation vector of the code line, the line attribute graph is learned using a graph convolutional network in the spatial domain to generate a semantic representation vector of the code line and obtain a feature embedding matrix of the source code file.

[0028] Based on the obtained grammatical representation vector of the code line and the obtained semantic representation vector of the code line, the two vector representations are fused by an adaptive weight method, wherein the two vector representations are multiplied by their respective weight coefficients, and the weight coefficients of the two are model learnable parameters and the sum of the two is kept to 1, so as to obtain a comprehensive representation vector of the code line;

[0029] A soft attention mechanism is used to merge the comprehensive representation vectors of all code lines in the same file to obtain the file representation vector.

[0030] As an optional implementation, the attention weights generated in the process of generating the obtained file representation vector are used to perform a software defect prediction task at the code line level. The process of performing file-level defect prediction using the obtained file representation vector includes using a multi-layer perceptron as a classification layer to predict the score of the file's defects. If the final score exceeds a set value, the file is considered to have defects.

[0031] According to whether the file has defects, set the corresponding label and use cross entropy as the loss function for discrimination;

[0032] The multi-label comparison learning loss value and the discriminant loss function are combined to perform software defect prediction.

[0033] A second aspect of the present invention provides a software defect prediction system, comprising:

[0034] The multi-label construction module is used to parse the source code files contained in the software system into an abstract syntax tree, extract feature nodes and label nodes from them, and generate the initial feature node representation vector and multi-hot encoding of the label node respectively;

[0035] A line attribute graph construction module is used to parse the source code files included in the software system into code attribute graph files, extract the control flow and data flow dependencies between code lines, and construct a line attribute graph;

[0036] The multi-label contrastive learning module is used to construct a learning model combining the Transformer encoder and multi-label contrastive learning by using the obtained initial feature node representation vector and the multi-hot encoding of the label node, generate the grammatical representation vector of the feature node, and calculate the multi-label contrastive learning loss value;

[0037] The code line grammatical representation learning module is used to use the obtained grammatical representation vectors of feature nodes and adopt a soft attention mechanism to weightedly fuse the feature node representation vectors located in the same code line to generate the grammatical representation vector of the code line;

[0038] The code line semantic representation module is used to use the obtained grammatical representation vector of the code line and the constructed line attribute graph, adopt a graph convolutional network, learn the semantic dependency relationship between source code lines, and generate a semantic representation vector of the code line;

[0039] A code line comprehensive representation module is used to fuse the obtained grammatical representation vector and semantic representation vector of the code line by using an adaptive weight fusion method to generate a code line comprehensive representation vector;

[0040] The file representation module is used to generate a file representation vector by using the comprehensive representation vectors of all code lines in the same code file and performing weighted fusion using a soft attention mechanism;

[0041] The defect prediction module is used to use the attention weights generated in the process of generating the obtained file representation vector to perform software defect prediction tasks at the code line level, and use the obtained file representation vector to perform file-level defect prediction.

[0042] According to a third aspect of the present invention, a computer-readable storage medium is provided for storing computer instructions, wherein when the computer instructions are executed by a processor, the steps in the above method are completed.

[0043] According to a fourth aspect of the present invention, an electronic device is provided, comprising a memory and a processor, and computer instructions stored in the memory and executed on the processor, wherein the steps in the above method are completed when the computer instructions are executed by the processor.

[0044] Compared with the prior art, the present invention has the following beneficial effects:

[0045] (1) The present invention innovatively proposes to construct multi-label nodes based on the Abstract Syntax Tree (AST), extract the AST into feature nodes and their corresponding label nodes, and learn the grammatical structure information as label information to alleviate the problems of large memory usage and information propagation delay caused by the large number of nodes in the AST. In addition, the present invention proposes to combine the Line Property Graph (LPG) based on the dependency relationship between code lines to comprehensively and comprehensively learn the syntax and semantic information of the code.

[0046] (2) The present invention uses the obtained feature nodes and label nodes to construct a learning model based on the combination of Transformer encoder and multi-label contrastive learning to generate a grammatical representation vector of the feature node; uses a soft attention mechanism to weightedly fuse the feature node representation vectors located in the same code line to generate a grammatical representation vector of the code line; then, based on the grammatical representation vector of the code line and the constructed LPG, uses a graph convolutional neural network to mine the semantic dependency between source code lines to generate a semantic representation vector of the code line, and then uses an adaptive weight fusion method to generate a comprehensive representation vector of the code line; finally, based on the obtained comprehensive representation vector of all code lines in the same code file, a file representation vector is generated by weighted fusion using a soft attention mechanism, thereby improving the performance of software defect prediction.

[0047] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.

[0049] Figure 1 is a schematic diagram of a comparison of software defect predictions at the code line level in an embodiment;

[0050] Figure 2 is a schematic diagram of a comparison of file-level software defect predictions in an embodiment;

[0051] FIG3 is an overall flow chart of a software defect prediction method provided by an embodiment;

[0052] Figure 4 The present invention is a schematic diagram of the structure of a software defect prediction system provided by an embodiment. DETAILED DESCRIPTION

[0053] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.

[0054] It should be noted that the following detailed descriptions are all illustrative and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.

[0055] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprising" and / or "including" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.

[0056] In the absence of conflict, the embodiments in this application and the features in the embodiments may be combined with each other.

[0057] Embodiment 1

[0058] A software defect prediction method based on multi-label contrastive learning and row attribute graph learning, such as Figure 3 As shown, the following steps are included:

[0059] Step S1. Parse the source code file of the software system into an Abstract Syntax Tree (AST), and based on the fact that the keywords, grammatical structure and grammatical rules of the code are located in the non-leaf nodes of the AST, and the information such as the identifiers and constants customized by the developer are located in the leaf nodes of the AST, this embodiment proposes to use the leaf nodes as feature nodes for learning the representation vector of the features of the source code, and at the same time map the non-leaf nodes representing the grammatical information corresponding to each feature node (that is, all ancestor nodes of the feature node) to the label nodes of the feature node for subsequent calculation of the loss of multi-label contrastive learning, so that the representation vector of the feature node reflects its grammatical structure information, and feature nodes with similar grammatical structure information have more similar representations in the feature space.

[0060] Specifically, for a Dataset of feature nodes , the corresponding construction of multi-label nodes (feature nodes and label nodes) is defined as follows:

[0061] ;

[0062] in, Indicates feature nodes; Indicates The initial vector representation of feature nodes, which will be obtained through the word2vec method; Represents the embedding dimension of the feature node; represents the label representation vector obtained by multi-hot encoding, where is the total number of label nodes. In this embodiment, a total of 132 node types defined in javaparser are used as label nodes, namely .

[0063] Step S2. Based on the learning of AST in step S1 and the inspiration of code property graph, this embodiment combines the control flow graph (CFG) and the program dependency graph (PDG) to define a line property graph (LPG) based on the dependency relationship between code lines.

[0064] Among them, the data flow graph CFG is defined as , Corresponding to a line of code in the source code, represents the directed edges constructed by executing control conditions between program lines; PDG is defined as , Corresponding to a line of code in the source code, the node of the program dependency graph PDG is the same as the node of the data flow graph CFG, Represents directed edges arising from data dependencies and control dependencies of predicates on variable values.

[0065] Therefore, the construction of LPG combines the data flow graph CFG of the source code and the program dependency graph PDG:

[0066] ;

[0067] .

[0068] Step S3. Based on the information obtained in step S1, The feature embedding matrix of feature nodes And label representation vector, this embodiment proposes to build a learning model that combines Transformer encoder and multi-label contrastive learning to mine it.

[0069] Step S301. Use the encoder structure in the Transformer framework to learn the relationship between feature nodes. The specific calculation formula is as follows:

[0070] ;

[0071] ;

[0072] ;

[0073] In the above formula Indicates The calculation result of the attention head is: Indicates the number of attention heads; , is a learnable weight matrix, are query vector, key vector and value vector respectively, is the dimension of the attention head; The above result is then input into a fully connected forward propagation network, which includes two linear transformations and applies the ReLU activation function between the two linear transformations.

[0074] ;

[0075] in, and is a learnable weight matrix, and represents the bias parameter, Indicates taking the maximum value, thus obtaining the grammatical representation vector matrix of the feature node: .

[0076] Step S302. In the data set In , for a specific instance feature node s, we define the candidate set of instance pairs as For each instance pair The contrastive learning loss value The calculation is as follows:

[0077] ;

[0078] ;

[0079] in, represents the Euclidean distance, is the temperature parameter for comparative learning, which is set to 1 in this embodiment; Represents an instance pair The corresponding labels indicate the similarity of the vectors; Represents an instance pair The grammatical representation vector of the feature node; Represents an instance pair Label representation vector obtained by multi-hot encoding; dynamic coefficient Is Normalized; Dataset The loss function Defined as the sum of the loss values ​​of all instance pairs .

[0080] Step S4: Based on the LPG obtained in step S2, this embodiment uses a spatial domain graph convolutional network (GCN) to mine the semantic dependency information of the program in the LPG.

[0081] Step S401. Based on the grammatical representation vector of the feature node obtained in step S301, since the node in LPG is a code line, in this embodiment, the grammatical representation vector of the feature node is first divided according to the code line to which it belongs, and the grammatical representation vector of the feature node is obtained. The first feature node The feature embedding matrix of the line code line is .

[0082] Step S402. Define a global learnable attention vector To calculate the No. The attention weight of feature nodes , and then perform weighted fusion on the feature node grammar representation vector to obtain the first Line code line syntax representation vector ,in, Indicates the number of feature nodes contained in each row, Indicates No. The grammatical representation vector of feature nodes.

[0083] Step S403. Based on the grammatical representation vector of the code line obtained in step S402, GCN is used to learn LPG to generate a semantic representation vector of the code line. The specific calculation is as follows:

[0084] ;

[0085] in, is a learnable weight matrix, is the bias parameter, Indicates the number of GCN aggregations; Indicates Lines of code (i.e., the first nodes) through The semantic representation vector generated after layer GCN learning; represents the hidden layer dimension of the graph neural network, Indicates the first The neighbors of a node; is the sum of the square roots of the node degrees (i.e. ); is the activation function. In this invention, the number of GCN layers is set to , the final output feature dimension is For a source code file containing N lines of code, the feature embedding matrix of its semantic representation vector is .

[0086] Step S404. Based on the grammatical representation vector of the code line obtained in step S402 and the semantic representation vector of the code line obtained in step S403, this embodiment uses an adaptive weighting method to fuse the two vector representations. ,in, and is a learnable parameter of the model and the sum of the two remains 1 to obtain a comprehensive representation vector of the code line.

[0087] Step S405. In order to complete the defect prediction at the file level, it is necessary to obtain the representation vector of the entire source code file. This embodiment uses the soft-attention mechanism to merge the comprehensive representation vectors of all code lines in the same file. Specifically, another global learnable attention vector is defined To calculate the Attention weights for lines of code , and then perform weighted fusion on the comprehensive representation vectors of the code lines to obtain the file representation vector .

[0088] Step S5. The code line level defect prediction task is regarded as a sorting task, which is based on the As the Risk score implementation for each line of code.

[0089] For the file-level defect prediction task, the model will set up a multi-layer perceptron as a classification layer to predict the score of defects in the file. The calculation process is: ,in, represents the learnable weight matrix, represents the bias parameter; Represents the sigmoid function. If the final score exceeds 0.5, the file is considered to have defects, and if the score does not exceed 0.5, the file is considered to be clean (i.e., there are no defects).

[0090] Step S6. In this embodiment, cross entropy is used as the loss function for model discrimination. If y is the true category distribution, for a total number of samples , where the number of defective samples and normal samples are and , then the cross entropy loss, that is, the loss function of model discrimination The definition is as follows:

[0091] ;

[0092] in, and Denote defect category weight and normal category weight respectively , Indicates that the label is The number of samples; Indicates that the true label of the document is defective, and vice versa; Indicates that the model predicts the file label as defective and vice versa.

[0093] Step S7. The loss function used in this embodiment combines the multi-label contrastive learning loss calculated in step S302 and the cross entropy loss in step S6: ,in is the weight coefficient of the loss function, is the loss function for model discrimination, i.e., the cross entropy loss in step S6, For the dataset The loss function is the multi-label contrast learning loss calculated in step S302. The software defects are predicted for the test samples and the results are compared with the actual usage. Figure 1 and Figure 2 The number of first false positive rows and F1 value are used as evaluation indicators for defect prediction. The comparison results are as follows: Figure 1 and Figure 2 As shown, the method proposed in this embodiment is significantly better than the existing deep row defect prediction method and row defect prediction method in terms of the evaluation index of the number of first false alarm rows. In terms of the evaluation index of F1 value, the method proposed in this embodiment is significantly better than the existing deep row defect prediction method, row defect prediction method, defect prediction method based on convolutional neural network model and defect prediction method based on bidirectional long short-term memory network model.

[0094] based on Figure 1 and Figure 2 It can be seen from the results in that the performance of the software defect prediction proposed in this embodiment is better than other methods.

[0095] Embodiment 2

[0096] like Figure 4 As shown, this embodiment provides a software defect prediction system based on multi-label contrast learning and row attribute graph learning, including:

[0097] (1) A multi-label construction module, which is used to parse the source code file of the software system into AST, extract feature nodes and label nodes from it, and use word2vec and multi-hot encoding methods to generate the initial representation vector of the feature node and the multi-hot encoding of the label node respectively;

[0098] (2) A line property graph construction module, which is used to parse the source code files of the software system into code property graph files and extract the control flow and data flow dependencies between code lines to construct LPG;

[0099] (3) A multi-label contrastive learning module, which uses the obtained initial feature node representation vector and the multi-hot encoding of the label node to construct a learning model combining the Transformer encoder and multi-label contrastive learning, generate the grammatical representation vector of the feature node, and calculate the multi-label contrastive learning loss value;

[0100] (4) A graph representation learning module, which is used to use a soft attention mechanism to weightedly fuse the grammatical representation vectors of feature nodes located in the same code line to generate a grammatical representation vector of the code line; then, the obtained grammatical representation vector of the code line and the constructed LPG are used to learn the semantic dependency between source code lines using a graph convolutional neural network to generate a semantic representation vector of the code line; then, the obtained grammatical representation vector and semantic representation vector of the code line are used; then, an adaptive weighted fusion method is used to generate a comprehensive representation vector of the code line; finally, the comprehensive representation vector of all code lines in the same code file is obtained, and a file representation vector is generated using a soft attention mechanism weighted fusion method;

[0101] (5) Defect prediction module, which is used to perform defect prediction tasks at the code line level using the attention weights generated during the generation of the obtained file representation vector; using the obtained file representation vector, a multi-layer perceptron (MLP) and a softmax function are used to perform file-level defect prediction, and the defect prediction loss value output by the softmax function is calculated; then, based on the obtained multi-label comparative learning loss value and defect prediction loss value, the gradient back propagation algorithm is used to adjust the parameters in the model to continuously optimize the accuracy of defect prediction;

[0102] (6) Feedback update module: After the model training is completed, it outputs the recommended results of the experimental sample set, compares them with the actual situation, and feeds back to update the underlying data information, thereby continuously optimizing the data weight value in the model and continuously improving the accuracy of software defect prediction.

[0103] In some embodiments, the graph representation learning module further comprises:

[0104] The code line grammatical representation learning module is used to use the obtained grammatical representation vectors of feature nodes and adopt a soft attention mechanism to weightedly fuse the feature node representation vectors located in the same code line to generate the grammatical representation vector of the code line;

[0105] The code line semantic representation module is used to use the obtained grammatical representation vector of the code line and the constructed line attribute graph, adopt a graph convolutional network, learn the semantic dependency relationship between source code lines, and generate a semantic representation vector of the code line;

[0106] A code line comprehensive representation module is used to fuse the obtained grammatical representation vector and semantic representation vector of the code line by using an adaptive weight fusion method to generate a code line comprehensive representation vector;

[0107] The file representation module is used to utilize the comprehensive representation vectors of all code lines in the same code file and use the soft attention mechanism for weighted fusion to generate a file representation vector.

[0108] In another embodiment, a software defect prediction system based on multi-label contrastive learning and row attribute graph learning further includes:

[0109] The preprocessing module is used to preprocess the source code data of massive software systems, including data cleaning and normalization.

[0110] In another embodiment, a computer readable storage medium is provided on which a computer program is stored, and when the program is executed by a processor, the following is implemented: Figure 3 The steps in the software defect prediction method are shown.

[0111] In another embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, Figure 3 The steps in the software defect prediction method are shown.

[0112] It should be understood by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of an all-hardware embodiment, an all-software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of one or more computer-usable storage media (including but not limited to disk storage, CD - ROM , optical storage, etc.).

[0113] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0114] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1A function specified in one or more boxes.

[0115] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0116] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made by those skilled in the art within the spirit and principle of the present invention without creative labor shall be included in the protection scope of the present invention.

Claims

1. A software defect prediction method, Its characteristics are: The following steps are involved: Parse the source code files contained in the software system into an abstract syntax tree, extract feature nodes and label nodes from them, and generate initial feature node representation vectors and multi-hot encodings of label nodes respectively; Parse the source code files included in the software system into code attribute graph files, extract the control flow and data flow dependencies between code lines, and construct a line attribute graph; Using the obtained initial feature node representation vector and the multi-hot encoding of the label node, a learning model combining the Transformer encoder and multi-label contrastive learning is constructed to generate the grammatical representation vector of the feature node and calculate the multi-label contrastive learning loss value; Using the obtained grammatical representation vectors of feature nodes, a soft attention mechanism is used to weightedly fuse the representation vectors of feature nodes located in the same code line to generate the grammatical representation vector of the code line. Using the obtained grammatical representation vector of the code line and the constructed line attribute graph, a graph convolutional network is used to learn the semantic dependency between source code lines and generate the semantic representation vector of the code line. Adopting the adaptive weight fusion method, the grammatical representation vector and semantic representation vector of the obtained code line are fused to generate a comprehensive representation vector of the code line; The comprehensive representation vectors of all code lines in the same code file are used to generate a file representation vector by weighted fusion using a soft attention mechanism. The attention weights generated during the generation of the obtained file representation vectors are used to perform software defect prediction tasks at the code line level, and the obtained file representation vectors are used to perform file-level defect prediction.

2. A software defect prediction method as claimed in claim 1, Its characteristics are: The following steps are also included: The defect prediction loss value output by the softmax function is calculated. Based on the obtained multi-label contrast learning loss value and defect prediction loss value, the gradient back propagation algorithm is used to adjust the parameters in the model to optimize the defect prediction accuracy.

3. A software defect prediction method as claimed in claim 1 or 2, Its characteristics are: The following steps are also included: After the model training is completed, it is tested using data that did not participate in the training process. The model test results are compared with the actual situation, and the model is optimized and adjusted to continuously optimize the weight data in the model.

4. A software defect prediction method as claimed in claim 1, Its characteristics are: The process of parsing the source code files contained in the software system into an abstract syntax tree and extracting feature nodes and label nodes from it includes: The source code files contained in the software system are parsed into an abstract syntax tree, and the leaf nodes are used as feature nodes to learn the representation vectors of the features of the source code. The non-leaf nodes representing the variable grammar rules are mapped to label nodes of the feature nodes.

5. A software defect prediction method as claimed in claim 1, Its characteristics are: The process of constructing a row attribute graph includes: defining the row attribute graph in combination with a data flow graph and a program dependency graph, wherein the data flow graph is a set of directed edges constructed by executing control conditions between corresponding lines of code in the corresponding source code and program lines, and the program dependency graph is a set of directed edges generated by corresponding lines of code in the corresponding source code and data dependencies and control dependencies of predicates on variable values. The combination of the two is the row attribute graph.

6. A software defect prediction method as claimed in claim 1, Its characteristics are: The process of building a learning model combining Transformer encoder and multi-label contrastive learning, generating grammatical representation vectors of feature nodes, and calculating the multi-label contrastive learning loss value includes: Use the encoder structure in the Transformer framework to learn the relationship between feature nodes; For instance feature nodes in the dataset, a candidate set of instance pairs is defined, and the contrastive learning loss value of each instance pair is calculated.

7. A software defect prediction method as claimed in claim 1, Its characteristics are: Using the obtained grammatical representation vector of the code line and the constructed line attribute graph, a graph convolutional network is used to learn the semantic dependency between source code lines and generate the semantic representation vector of the code line. Adopting the adaptive weight fusion method, the grammatical representation vector and semantic representation vector of the obtained code line are fused to generate a comprehensive representation vector of the code line; Using the comprehensive representation vectors of all code lines in the same code file, the soft attention mechanism is used for weighted fusion. The process of generating the file representation vector includes: Based on the obtained grammatical representation vector of the feature node, the node in the row attribute graph is the code line, and the soft attention mechanism is used to aggregate the feature node vector in each line to obtain the grammatical representation vector of the code line; Based on the obtained grammatical representation vector of the code line, the line attribute graph is learned using a graph convolutional network in the spatial domain to generate a semantic representation vector of the code line and obtain the feature embedding matrix of the source code file; Based on the obtained grammatical representation vector of the code line and the obtained semantic representation vector of the code line, the two vector representations are fused by an adaptive weight method, wherein the two vector representations are multiplied by their respective weight coefficients, and the weight coefficients of the two are model learnable parameters and the sum of the two is kept to 1, so as to obtain a comprehensive representation vector of the code line; A soft attention mechanism is used to merge the comprehensive representation vectors of all code lines in the same file to obtain the file representation vector.

8. A software defect prediction method as claimed in claim 1, Its characteristics are: The attention weights generated in the process of generating the obtained file representation vector are used to perform the software defect prediction task at the code line level. The process of performing the defect prediction at the file level using the obtained file representation vector includes using a multi-layer perceptron as a classification layer to predict the score of the file's defects. If the final score exceeds the set value, the file is considered to have defects. According to whether the file has defects, set the corresponding label and use cross entropy as the loss function for discrimination; The multi-label comparison learning loss value and the discriminant loss function are combined to perform software defect prediction.

9. A software defect prediction system, Its characteristics include: The multi-label construction module is used to parse the source code files contained in the software system into an abstract syntax tree, extract feature nodes and label nodes from them, and generate the initial feature node representation vector and multi-hot encoding of the label node respectively; A line attribute graph construction module is used to parse the source code files included in the software system into code attribute graph files, extract the control flow and data flow dependencies between code lines, and construct a line attribute graph; The multi-label contrastive learning module is used to construct a learning model combining the Transformer encoder and multi-label contrastive learning by using the obtained initial feature node representation vector and the multi-hot encoding of the label node, generate the grammatical representation vector of the feature node, and calculate the multi-label contrastive learning loss value; The code line grammatical representation learning module is used to use the obtained grammatical representation vectors of feature nodes and adopt a soft attention mechanism to weightedly fuse the feature node representation vectors located in the same code line to generate the grammatical representation vector of the code line; The code line semantic representation module is used to use the obtained grammatical representation vector of the code line and the constructed line attribute graph, adopt a graph convolutional network, learn the semantic dependency relationship between source code lines, and generate a semantic representation vector of the code line; A code line comprehensive representation module is used to fuse the obtained grammatical representation vector and semantic representation vector of the code line by using an adaptive weight fusion method to generate a code line comprehensive representation vector; The file representation module is used to generate a file representation vector by using the comprehensive representation vectors of all code lines in the same code file and performing weighted fusion using a soft attention mechanism; The defect prediction module is used to use the attention weights generated in the process of generating the obtained file representation vector to perform software defect prediction tasks at the code line level, and use the obtained file representation vector to perform file-level defect prediction.

10. An electronic device, Its characteristics are: The method comprises a memory and a processor, and computer instructions stored in the memory and executed on the processor, wherein when the computer instructions are executed by the processor, the steps in the method according to any one of claims 1 to 8 are completed.

Citation Information

Patent Citations

  • Software defect prediction method based on graph convolutional neural network and terminal

    CN113608747A

  • Contrast learning-based source code vulnerability detection method and system

    CN115033890A