An abstract syntax tree-based software defect prediction method

By combining abstract syntax trees, graph convolutional networks, and bidirectional GRU networks, and utilizing attention mechanisms to obtain semantic features of source code, the problem of insufficient capture of syntax and semantic features in traditional methods is solved, and more efficient software defect prediction is achieved.

CN115982037BActive Publication Date: 2025-11-04BEIJING TECH & BUSINESS UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211735838.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-12-30
Publication Date
2025-11-04
Estimated Expiration
2042-12-30

AI Technical Summary

Technical Problem

Traditional software defect prediction methods struggle to effectively capture the syntactic and semantic features of code, resulting in insufficient accuracy in defect prediction.

Method used

We employ an abstract syntax tree-based approach, combining graph convolutional networks (GCN) and bidirectional GRU networks, to obtain semantic features of the source code through node relationship graphs and attention mechanisms, and then use logistic regression algorithm for prediction.

Benefits of technology

It improves the accuracy and efficiency of software defect prediction, enabling more precise location of defective parts in the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115982037B_ABST
    Figure CN115982037B_ABST
Patent Text Reader

Abstract

The application provides a software defect prediction method based on an abstract syntax tree, and belongs to the fields of deep learning and software defect prediction.The source code is converted into the form of an abstract syntax tree, well-defined structure information and semantic information are retained, a graph convolution network (GCN) is used to learn the features of nodes in the syntax tree structure and the information of the network structure, word embedding is used to convert the node sequence of the abstract syntax tree into a text vector, then a BiGRU network is used to learn the semantic relationship of the context directly to extract semantic features, finally, the two types of features obtained are aggregated based on an attention mechanism to obtain the features of the code, which are input into a classifier to predict the defect rate.The application fully utilizes the semantic and syntax features of the code, uses the attention mechanism to give different weights to different variables, reduces noise interference, and improves the accuracy of software defect prediction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of deep learning and software defect prediction, and can help software engineers and testers to allocate resources rationally to locate errors and quickly narrow down the parts of the software codebase most likely to contain defects. Background Technology

[0002] As software systems play a vital role in all sectors of society, software defects have a significant impact on business and people's lives. However, due to the substantial increase in the size and complexity of software codebases, identifying defects in software code has become increasingly difficult. Traditional software defect prediction relies on manually acquired software metrics for classification learning. Machine learning techniques, such as SVM (Support Vector Machine), Random Forest, Naive Bayes, and Logistic Regression, have been widely used to build defect prediction models. These techniques extract numerous features from the software code and input them into common classifiers, such as SVM, Random Forest, Naive Bayes, and Logistic Regression. However, traditional software defect prediction methods use static code metrics as features, which do not truly reflect the syntax and semantics of the code, undoubtedly affecting defect prediction.

[0003] With the development of deep learning, it has been found to perform well in capturing the syntactic and semantic features of source code. Extensive previous experiments have demonstrated that converting code into an abstract syntax tree (AST) effectively preserves its structure. Most mainstream software defect prediction models incorporate this step. The key idea behind these methods is to use the token vectors extracted from the AST to generate semantic feature representations of the source code, and then leverage these features to build a more accurate classification model. However, to obtain even more precise semantic features and improve model accuracy, a method is needed to better utilize the well-defined structural information and rich semantics within the AST. Summary of the Invention

[0004] Based on the representation of abstract syntax trees, this invention provides a method for obtaining semantic features of source code by combining GCN networks and bidirectional GRU networks, and incorporates an attention mechanism to improve model performance.

[0005] The technical solution provided by this invention is as follows:

[0006] A software defect prediction method based on abstract syntax trees includes the following steps:

[0007] 1) Parse the source code into an abstract syntax tree;

[0008] 2) Use a depth-first search algorithm to convert the abstract syntax tree into a text vector. If the size of the abstract syntax tree is n, then a text vector about Y is obtained:

[0009] Y = [Y1, Y2, Y3, ..., Yn] (1)

[0010] 3) Generate a node relationship graph for each code segment of the Abstract Syntax Tree (AST). Each node in the AST has its own features. Let these features form an n×D matrix, and the relationships between the nodes also form an n×n adjacency matrix A.

[0011]

[0012] The graph is represented by G = (S, E), where the elements in S are vertices and the elements in E are edges;

[0013] 4) Word embedding process. Using the Gensim library, the text vector is converted into a numeric vector that can be input into the GRU. A mapping dictionary is built, and each text element in the vector is linked to an integer. These integers act as tokens, uniquely identifying each text element in the AST. In this way, each element in the node sequence can be replaced with a numeric symbol while maintaining their order. Using the text vector given in step (2), the corresponding embedding matrix x can finally be obtained.

[0014] x=[x1,x2,x3,...xn] (3)

[0015] For class imbalance issues in the data, the Random Oversampling-SMOTE method is used to handle them.

[0016] 5) Input the data obtained in step 3) into the graph convolutional network (GCN). In the GCN, the node relationship graph and the corresponding nodes are used as input. For each node in the GCN, it is necessary to consider the feature information contained in all its neighbors and itself. Its propagation between layers is as follows:

[0017]

[0018] Where hl is the hidden representation generated at each layer of the GCN network. I is the identity matrix. yes The degree matrix is ​​given by σ, the non-linear activation function is given by wl, the weight matrix of the l-th layer of the neural network is given by bl, and the bias matrix is ​​given by b. The output representation of the GCN network is finally obtained through propagation between layers.

[0019] 6) This is the hidden representation generated by each layer of the BiGRU network, which uses the embedding matrix x obtained in step 4) as input:

[0020]

[0021] BiGRU can be represented as two unidirectional GRU networks. The BiGRU() function is a nonlinear transformation of the input embedding matrix x, which is encoded into the output representation hc of the BiGRU network.

[0022] 7) The output representation of the GCN layer was obtained. The output of the BiGRU network is represented as hc, and then an attention mechanism is used to aggregate the two acquired features to capture the key sentiment features from the source code:

[0023]

[0024]

[0025] Formula (6) is used to calculate the attention coefficient, || represents the vector concatenation operation, which aims to concatenate the two acquired features, and W represents the weight matrix. It is the i-th node in the hidden state. Let be the j-th node in the graph hidden representation, and let a be the non-linear activation function LeakyReLU.

[0026] Therefore, after a series of data processing steps, the final expression of the source code can be obtained as follows:

[0027]

[0028] 8) Input the source code representation *r* into the classifier and use the logistic regression algorithm to obtain software defect predictions. Logistic regression is an algorithm that predicts binary results, yielding both negative and positive outcomes. It is widely used in software defect prediction. It uses cross-entropy as the loss function to calculate the probability of the two labels corresponding to each category. The predicted label for the corresponding category is the label with the higher probability score.

[0029] The technical effects of this invention are:

[0030] This invention addresses the shortcomings of traditional machine learning methods in predicting software defects, particularly the difficulty in capturing the true semantic and syntactic features of code. It employs an Abstract Syntax Tree (BSN) approach to obtain these features. Using Graph Convolutional Networks (GCNs) and Bidirectional GRUs as core models, the BSN is transformed into vector nodes. GCNs then integrate the feature information of each node and its neighbors to obtain its hidden representation. BiGRUs are used to convert numerical vectors obtained through word embeddings into hidden representations of nodes, thereby capturing the relationships between sequences and solving the gradient problems of RNNs (inability to retain information for long periods and backpropagation). The features obtained from both networks are aggregated, and an attention mechanism is used to assign different weights to different features, reflecting their importance. Based on this, the defect rate is predicted by calculating the obtained source code features. Attached Figure Description

[0031] Figure 1 This is a system framework diagram of the present invention;

[0032] Figure 2 This is a simplified node relationship diagram in this invention;

[0033] Figure 3 This is a model diagram of the GRU network in this invention;

[0034] Figure 4 This is a schematic diagram of the prediction results using the present invention and other prediction methods. Detailed Implementation

[0035] The present invention will be further described below with reference to the accompanying drawings and embodiments, but the scope of the invention is not limited in any way.

[0036] This invention, based on the representation of an abstract syntax tree, provides a method for obtaining semantic features of source code by combining GCN and bidirectional GRU networks, and incorporates an attention mechanism to improve model performance. Figure 1 As shown, the software defect prediction method based on abstract syntax trees of this invention includes the following steps:

[0037] 1) Use Python's javalang library to convert the source code into an abstract syntax tree;

[0038] 2) After obtaining the parsed AST, in order to better reflect the hierarchical relationship of the AST nodes, the depth-first search (DFT) algorithm is used to label the elements of the vector. In this way, each AST node becomes an element of the vector. Using this method, the abstract syntax tree is converted into a text vector. If the size of the abstract syntax tree is n, a text vector about Y can be obtained:

[0039] Y = [Y1, Y2, Y3, ..., Yn] (1)

[0040] 3) Generate a node relationship graph for each code segment of the abstract syntax tree. A simplified relationship graph is shown below. Figure 2 As shown, the adjacency matrix is ​​an n-order square matrix, where n is the number of vertices, and S i Sj represents an edge in the node relationship graph. Each node in the AST has its own features, which form an n×D matrix. The relationships between the nodes also form an n×n adjacency matrix A.

[0041]

[0042] The graph is represented by G = (S, E), where the elements in S are vertices and the elements in E are edges.

[0043] 4) Word Embedding Process. The Gensim library is used to convert text vectors into numeric vectors that can be input into the GRU. It learns the topic vector representation of the text hidden layer from the original unstructured text in an unsupervised manner, establishing a mapping dictionary table. Each text element in the vector is linked to an integer, and these integers are used as labels, ensuring that each text element in the AST corresponds to only one numeric symbol. For example, `static` corresponds to 1, and `public` corresponds to 2. In this way, each element in the node sequence is replaced with a numeric symbol while maintaining their order. Using the text vector given in step (2), the corresponding embedding matrix `x` can finally be obtained.

[0044] x=[x1,x2,x3,...xn] (3)

[0045] In software, there are generally more defect-free modules than defective modules. Training directly on the source data can easily bias the model towards the majority, affecting the prediction results. Therefore, the Random Oversampling-SMOTE method is used to deal with the class imbalance problem in the data.

[0046] 5) Input the data obtained in step (3) into the graph convolutional network. In the GCN network, the node relationship graph and the corresponding nodes are used as its inputs, and the output of each layer is used as the input of the next layer. For each node in the GCN network, it is necessary to consider the feature information contained in all its neighbors and itself. Its propagation method between layers is:

[0047]

[0048] Where hl is the hidden representation generated at each layer of the GCN network. I is the identity matrix. yes The degree matrix is ​​given by σ, the non-linear activation function is given by σ, Wl is the weight matrix of the l-th layer of the neural network, and bl is the bias matrix. The output representation of the GCN network is finally obtained through propagation between layers.

[0049] Since A is an unnormalized matrix, multiplying it with the feature matrix will change the distribution of features and miss information about A itself, which may lead to gradient explosion or gradient vanishing. Therefore, A is normalized by adding the identity matrix to matrix A and then multiplying it by the inverse of the degree matrix, finally obtaining a symmetric and normalized matrix.

[0050] hc is the hidden representation generated at each layer of the bidirectional GRU network, which uses the embedding matrix x obtained in step four as input:

[0051]

[0052] BiGRU can be represented as two unidirectional GRU networks. The BiGRU() function is a nonlinear transformation of the input embedding matrix x, which is encoded into the output representation hc of the BiGRU network.

[0053] The BiGRU layer is used to extract deep text features from the input text vector. The BiGRU model consists of a forward GRU and a backward GRU. The GRU model is as follows: Figure 3 As shown, the input is the word vector matrix xt obtained in step (4) and the hidden state ht transmitted from the previous node. -1 The hidden state ht is obtained by updating gate z and resetting gate r to pass to the next node. After BiGRU feature extraction, the relationships between contexts in the text can be learned more fully.

[0054] Finally, the output representation of the GCN layer was obtained. The output of the BiGRU network is represented as hc, and then an attention mechanism is used to aggregate the two acquired features to capture the key sentiment features from the source code:

[0055]

[0056]

[0057] Formula (6) is used to calculate the attention coefficient, || represents the vector concatenation operation, which aims to concatenate the two acquired features, and W represents the weight matrix. It is the i-th node in the hidden state. Let be the j-th node in the graph hidden representation, and let a be the non-linear activation function LeakyReLU.

[0058] Therefore, after a series of data processing steps, the final expression of the source code can be obtained as follows:

[0059]

[0060] 6) Finally, the source code expression *r* is input into the classifier, and the logistic regression algorithm is used to obtain the software defect prediction. Logistic regression is an algorithm that predicts binary results, yielding both negative and positive results, and is widely used in software defect prediction. It uses cross-entropy as the loss function to calculate the probability of the two labels corresponding to each category, and the predicted label for the corresponding category is the label with the higher probability score.

[0061] 7) Use commonly used metrics such as precision, recall, and F1 score to measure the prediction results.

[0062] Precision = TP / (TP + FP) (9)

[0063] Recall = TP / (TP + FN) (10)

[0064]

[0065] TP indicates that an actual defective sample is predicted as defective; TN indicates that an actual non-defective sample is predicted as non-defective; FP indicates that an actual non-defective sample is predicted as defective; and FN indicates that an actual defective sample is predicted as non-defective.

[0066] Precision is the proportion of actual defective samples out of the samples predicted as defective; recall is the proportion of samples predicted as defective out of the actual defective samples; F1 is the harmonic mean of precision and recall. (Reference) Figure 4 AUC is defined as the area under the ROC curve and the coordinate axis. ROC is the Receiver Operational Feature curve, where the x-axis is FP and the y-axis is TP. This area will not exceed 1; the closer the AUC is to 1, the higher the realism of the detection method. This invention uses datasets such as camel, jedit, log4j, lucence, poi, synapse, velocity, and xalan as examples, and compares its experimental results with existing prediction methods such as DT, DBN, CNN, and LSTM. The results from the GCRU (based on a combination of GCN and bidirectional GRU networks) of this invention show that the prediction performance of this invention is superior to other prediction methods.

[0067] It should be noted that the purpose of disclosing the embodiments is to help further understand the present invention. However, those skilled in the art will understand that various substitutions and modifications are possible without departing from the spirit and scope of the present invention and the appended claims. Therefore, the present invention should not be limited to the content disclosed in the embodiments, and the scope of protection of the present invention is defined by the scope of the claims.

Claims

1. A software defect prediction method based on abstract syntax trees, characterized in that, Includes the following steps: 1) Convert the source code into an abstract syntax tree; 2) Use a depth-first search algorithm to convert the abstract syntax tree into a text vector. If the size of the abstract syntax tree is n, then a text vector about Y is obtained: Y=[Y1,Y2,Y3,...,Y n (1) 3) Generate a node relationship graph (AST) for each code segment of the abstract syntax tree. Each node in the AST has its own features. Let the features of these nodes form an n×D matrix, and the relationships between the nodes form an n×n adjacency matrix A. The node relationship graph AST is represented by G = (S, E), where the elements in S are vertices and the elements in E are edges; 4) Convert the text vectors into numeric vectors that can be input into the GRU, build a mapping dictionary table, link each text element in the vector to an integer, and these integers act as tags to uniquely identify each text element in the AST, finally obtaining the corresponding embedding matrix x; x=[x1,x2,x3,...x n ] (3) 5) Using the node relationship graph and corresponding nodes obtained in step 3) as input to the Graph Convolutional Network (GCN), the propagation method between layers for each node in the GCN network is as follows: Where h l The hidden representation generated for each layer of the GCN network. It is the identity matrix. yes The degree matrix, σ is the nonlinear activation function, W l b is the weight matrix of the l-th layer of the neural network. l The bias matrix is ​​used to obtain the output representation of the GCN network through propagation between layers. 6) Use the embedding matrix x obtained in step 4) as the input to the BiGRU network. BiGRU is represented as two unidirectional GRU networks. The BiGRU() function is a nonlinear transformation of the input embedding matrix x, encoding it into the output representation h of the BiGRU network. c ; 7) Use an attention mechanism to aggregate the output representation of the GCN layer. The output of the BiGRU network represents h c Capture key emotional features from the source code: in It is the i-th node in the hidden state. Let be the j-th node in the graph hidden representation, 'a' be the non-linear activation function LeakyReLU, and '||' denote the vector concatenation operation; the final expression of the source code is as follows: 8) Input the expression form r of the source code into the classifier and use the logistic regression algorithm to obtain the software defect prediction.

2. The software defect prediction method based on abstract syntax trees as described in claim 1, characterized in that, Step 1) uses Python's javalang library to parse the source code into an abstract syntax tree.

3. The software defect prediction method based on abstract syntax trees as described in claim 1, characterized in that, Step 4) uses the Gensim library to convert the text vector into a numeric vector that can be input into GRU.

Citation Information

Patent Citations

  • Software source code defect detection method and system, electronic equipment and storage medium

    CN113641586A

  • A Software Defect Localization Method Based on Graph Convolutional Neural Networks

    CN114936158A