A cross-platform code transplant quality assessment method, system and device

By using a joint neural network of code attribute diagram and GCN/GAT, the code structure and semantic features after cross-platform code transplantation are extracted, and the problem of inaccurate evaluation in the existing technology is solved, and a more efficient and accurate code quality evaluation is achieved.

CN119065713BActive Publication Date: 2025-03-04OCEAN UNIV OF CHINA
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411562077.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-05
Publication Date
2025-03-04
Estimated Expiration
2044-11-05

AI Technical Summary

Technical Problem

The existing cross-platform code transplant quality evaluation methods focus too much on the surface similarity of the code, ignore the logical relationship and semantic structure of the code, resulting in inaccurate evaluation.

Method used

A joint neural network combining code attribute graph (CPG) with GCN and GAT is used to extract the structure and semantic features of the code through graph convolution networks and graph attention networks to generate quality evaluation scores.

Benefits of technology

It improves the accuracy and authenticity of code transplant quality evaluation, can more effectively capture the deep syntax and semantic structure of the code, and reduces noise and bias in the evaluation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119065713B_ABST
    Figure CN119065713B_ABST
Patent Text Reader

Abstract

The present invention relates to a method, system and device for evaluating the quality of cross-platform code transplantation, and relates to the field of computers. The method comprises obtaining a data set of source code and cross-platform transplanted code, using an open source static analysis tool Joern to analyze the source code and the cross-platform transplanted code, and generating a code property graph; using a networkx library to read CPG to construct a code undirected graph, using a community discovery algorithm based on betweenness and simplifying the undirected graph, and converting it into an adjacency matrix after removing redundant information; inputting the converted adjacency matrix of the source code and the cross-platform transplanted code into a joint neural network of GCN and GAT to obtain a node vector of the graph; using principal component analysis to convert the node vector of the graph into a graph feature vector, and finally obtaining a quality evaluation score through an activation function; the present invention also provides a system and device for running the method. The present invention realizes accurate evaluation of the quality of the cross-platform transplanted code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of computer cross-platform code transplantation quality assessment, and in particular to a cross-platform code transplantation quality assessment method, system and device. Background Art

[0002] With the development of modern computer technology and the Internet, cross-platform and cross-language code translation technology has made many advances and optimizations, making code transplantation and migration more efficient and accurate. However, it also brings about the problem of inaccurate code quality assessment. Cross-platform code transplantation is the process of migrating an application or software from one platform (such as an operating system or hardware architecture) to another. Among them, code transplantation involves the process of converting code from one programming language to another programming language, which can be a statement-level translation (such as Python to Java) or a function-level translation (such as converting code that implements a specific function from one language to another). Whether the translated code can replace the original code in terms of functionality and stability needs to be evaluated to ensure the quality of code transplantation.

[0003] Therefore, the quality assessment method after code transplantation has been studied by more and more scholars. The quality assessment after code translation plays a key role in the success of the development team and the project. High-quality code transplantation can ensure the functional correctness of the code, have good performance and efficiency, and have good readability and maintainability. The existing code translation evaluation method is mainly based on the n-gram matching metric for evaluation, which leads to excessive focus on the surface similarity of the code and ignores the substantive content and logical relationship of the code. In terms of code evaluation, there are mainly code quality evaluation methods such as BLEU and CodeBLEU. They were originally designed to evaluate natural languages. When used for code quality evaluation, they will ignore important grammatical and semantic features of the code. As a language with strong logic, the difference in grammar and semantics has a great impact on the code. The above evaluation methods all pay too much attention to the surface similarity of the code and ignore the logical relationship and semantic similarity of the translated code. Therefore, a cross-platform code transplantation quality evaluation method that considers the logical structure and grammatical characteristics of the code is needed to evaluate the quality of the transplanted code and improve the authenticity and accuracy of the code quality evaluation. Summary of the invention

[0004] In order to solve the above technical problems, the present invention provides a cross-platform code transplantation quality assessment method and system, which utilizes the code attribute graph to reflect the structural relationship, dependency relationship, data flow and other characteristics of the code, and combines the joint neural network of GCN and GAT to realize the quality assessment of the code after cross-platform transplantation.

[0005] To achieve the above object, the technical solution of the present invention is as follows:

[0006] A cross-platform code transplantation quality assessment method, the method comprising the following steps:

[0007] (1) Obtain basic data, using open source data sets, to obtain source code and cross-platform ported code data sets, where the source code data set and the cross-platform ported code data set are in one-to-one correspondence;

[0008] (2) Use the open source static analysis tool Joern to analyze the source code and the cross-platform ported code to generate a code property graph;

[0009] (3) Use the networkx library to read the CPG code to build an undirected graph, use the betweenness-based community discovery algorithm to simplify the undirected graph, and convert it into an adjacency matrix after removing redundant information;

[0010] (4) Input the adjacency matrix of the source code and the cross-platform ported code after conversion in steps (2) and (3) into the joint neural network of the graph convolutional network (GCN) and the graph attention network (GAT) to obtain the node vector of the graph;

[0011] (5) The node vector of the graph is converted into a graph feature vector using principal component analysis, and finally a quality assessment score is obtained through an activation function; the joint neural network has two inputs, two neural networks, and the weights of the two neural networks are not shared, so it can process differential data, that is, it can process source code and code after cross-platform porting.

[0012] As a preferred implementation, the steps of using a betweenness-based community discovery algorithm to prune redundant edges from the code undirected graph in step (3) are as follows:

[0013] The first step is to read the code property graph (CPG) and use the networkx library to process the CPG to obtain the code undirected graph ;in, represents the code property graph, represents the set of vertices in the graph, Represents the set of edges in a graph;

[0014] Step 2: For each edge in the code undirected graph, calculate its betweenness centrality, which represents the number of shortest paths through the edge; find the code undirected graph Delete the edge with the highest betweenness centrality from the code undirected graph to obtain a new code undirected graph ;

[0015] The definition of betweenness centrality is as follows, where Indicates passing through node of The number of shortest paths, express The number of shortest paths. The betweenness centrality reflects the importance of node v as a "bridge"; ;

[0016] The third step is to use the depth traversal algorithm to determine whether the code undirected graph is connected. If it is connected, repeat steps (2) and (3). If it is not connected, replace the code undirected graph. The output is an adjacency matrix ;

[0017] As a preferred implementation, the specific steps of extracting the node vector of the source code graph through GAT in step (4) are as follows:

[0018] The first step is to use the adjacency matrix of the source code as , the feature matrix H is represented by the feature vector of each node, N is the number of nodes, and F is the feature dimension; the feature matrix H is represented as:

[0019] ,

[0020] Node connection adjacency matrix It is expressed as: ;

[0021] The feature matrix Through a learnable weight matrix Perform a linear transformation: ,in is the transformed feature dimension;

[0022] Step 2: For any pair of adjacent nodes and , calculate the attention coefficient between them : First, concatenate the features of node i and node j ;

[0023] Calculate the attention score using a learnable vector a:

[0024] ( ),in ;

[0025] The attention coefficients are normalized using the softmax function to obtain the weights αij:

[0026] ,in Is a node The set of neighbor nodes of

[0027] Step 3: Use the calculated attention weights to perform weighted summation on the features of neighboring nodes and calculate the new representation h of node i i ′′ : , where σ is the activation function ReLU.

[0028] As a preferred implementation, the specific steps of extracting the node vector of the source code graph through GCN are as follows:

[0029] Step 1: The adjacency matrix of the cross-platform ported code is B. Self-loops are added to the adjacency matrix, that is, the diagonal elements are set to 1. This ensures that each node can consider its own characteristics;

[0030] ; The updated adjacency matrix is ,in is the identity matrix;

[0031] Normalize the adjacency matrix: , where D is the degree matrix, D[i][j] represents the degree of node i;

[0032] In the second step, each node has a feature vector, and the features are combined into a feature matrix X, where the rows of X represent nodes and the columns represent features. In GCN, the feature vectors of the nodes are convolved through the adjacency matrix. The calculation of a graph convolution layer can be expressed as:

[0033] ,in It is The node feature matrix of the layer, is the learnable weight matrix and σ is the activation function ReLU.

[0034] Step 3: Extract features through two layers of GCN convolution operations. In the first layer of the network, the code adjacency matrix of the cross-platform ported code is input, and the feature graph extracted by the full convolution layer is used as the node feature. The first layer of the convolution module transfers information. After mapping the nodes to the latent space, the second layer of the GCN module is used to achieve more efficient graph convolution on the aggregated nodes. The final model is achieved by fusing the output node vectors of the two layers of the network.

[0035] Finally, we get the embedding representation of each node (i.e., node vector), which can capture the characteristics of the structure and attributes of the node in the graph. By training the GCN model and using the above steps and the adjacency matrix, we get the embedding vector of each node.

[0036] The step (5) uses PCA to transform the node vector of the graph into a graph feature vector, and finally obtains the quality assessment score through the activation function. The specific steps are as follows:

[0037] The first step is to obtain the feature vector of each node. The vector comes from the output of the graph convolutional network (GCN) and the graph attention network (GAT). Combine the vectors of all nodes into a matrix , where each row represents the feature vector of a node. Assume there are N nodes, and the feature vector of each node is dimension, we get the node feature matrix The dimension is ;

[0038] ;

[0039] Make sure each feature has a mean of 0 and a variance of 1 and standardize it using the following formula:

[0040] ,in is the mean of the feature, is the standard deviation;

[0041] Step 2: Calculate the standardized feature matrix The covariance matrix of : ;

[0042] Solving for eigenvalues ​​and eigenvectors: Covariance matrix Perform eigenvalue decomposition to obtain eigenvalues ​​and corresponding eigenvectors;

[0043] ,in, is the eigenvalue, is the eigenvector;

[0044] Step 3: Select the principal component: According to the size of the eigenvalue, select the first The eigenvectors corresponding to the eigenvalues ​​are the principal components, which are used to represent the main features of the data. Project it into the selected principal component space to obtain the eigenvector of the graph; ,in is the eigenvector matrix formed by the selected principal components;

[0045] Through the above steps, the source code and the cross-platform ported code are converted into an adjacency matrix and The node vector of is converted into a graph feature vector, which retains most of the information and can be used for subsequent quality assessment tasks in a low-dimensional space. The benefit of using PCA is that it can significantly reduce the computational complexity while improving the visualization effect.

[0046] The PCA algorithm is used to compress the data space and map the features of high-dimensional data to low-dimensional space, thereby reducing the dimensionality and noise of the feature vector and removing redundant information and noise interference. The feature vector of the graph is used to measure the semantic and grammatical features of the source code and the code after cross-platform porting, and finally a quality assessment score is obtained through an activation function.

[0047] A cross-platform code transplant quality assessment system, the system comprising a basic data input module, a code attribute graph generation module, an adjacency matrix conversion module, a node vector acquisition module and a quality assessment module;

[0048] The basic data input module is used to obtain source code and cross-platform transplanted code data sets;

[0049] The generated code attribute graph module executes step (2) of the method;

[0050] The adjacency matrix conversion module executes step (3) of the method;

[0051] The node vector obtaining module executes step (4) of the method;

[0052] The quality assessment module executes step (5) of the method.

[0053] The present invention also provides a cross-platform code transplant quality assessment device, which is equipped with the system.

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

[0055] (1) In the code transplant quality assessment, the present invention combines the structural characteristics of the code attribute graph for the first time, including features such as code syntax and semantic structure, to solve the problem that the existing code translation assessment methods focus too much on surface similarity and are too strict in accuracy, while ignoring the logical relationship and structural characteristics of the code in the transplant.

[0056] (2) The present invention proposes for the first time a community discovery algorithm based on edge betweenness to simplify the code attribute graph and cut off unimportant or isolated edges, which can effectively simplify the connection relationship between different modules of the CPG, enhance the connectivity within the modules in the code, and make the code structure clear. Each deletion will reduce the possible connections in the network, making the subsequent division process more efficient, guiding the code attribute graph structure to converge to a more stable state, reducing the computational complexity, and improving the efficiency of the algorithm. The loss of valuable information is avoided to a large extent, and the problem of redundant structure affecting the quality assessment of code translation is solved. Using a community discovery algorithm to identify dense substructures in the graph may help delete unimportant or isolated nodes and edges, and improve accuracy and reliability by removing unnecessary edges. In this process, removing useless edges (i.e., redundant or low-strength connections) can reduce the complexity of the network, thereby speeding up the operation of the community discovery algorithm. Useless edges may cause noise, which in turn affects the recognition of code structure.

[0057] (3) The present invention proposes a new cross-platform code transplantation evaluation method for the first time based on the joint neural network of GCN and GAT. The weights of the two neural networks of GCN and GAT in the joint neural network are non-shared, which can better process differential data, that is, it can better process source code and cross-language translation code, and is more conducive to improving the accuracy of code quality detection and performance evaluation in cross-platform and cross-language code transplantation. The joint neural network based on GCN and GAT can learn richer and more representative features from different input data, which helps to improve the discrimination ability of the model, reduce the complexity of the model, and thus reduce the risk of overfitting.

[0058] (4) The present invention has broad application prospects in quality verification and delivery performance evaluation in cross-platform and cross-language code transplantation, and has important prospects in code quality visualization.

[0059] (5) In summary, the cross-platform code transplant quality assessment method and system based on the joint neural network of GCN and GAT has more advantages than the traditional assessment method. In essence, both GCN and GAT aggregate the features of neighboring vertices to the central vertex (an aggregate operation) and use the local stationary on the graph to learn new vertex feature expressions. The difference is that GCN uses the Laplacian matrix, while GAT uses the attention coefficient. GCN and GAT are good at different types of graph structures and features. By combining them, they can capture the diversity of graph information more comprehensively. GCN is good at capturing global structural information, while GAT can refine the relationship between nodes. By comparing the two feature vectors, richer graph similarity information can be obtained. GCN captures the global structural information of the source code, and GAT refines the relationship between the nodes after transplantation and translation, better reflecting the structural similarity between the transplanted code and the source code, and accurately evaluating the quality of the transplantation results. Different models have different sensitivities to noise and abnormal data. Combining them can improve the robustness of feature extraction and reduce the bias caused by a single model. BRIEF DESCRIPTION OF THE DRAWINGS

[0060] Figure 1 It is a logical flow diagram of the present invention;

[0061] Figure 2 It is the GAT deep neural learning network module in the joint neural network of GCN and GAT;

[0062] Figure 3 It is the GCN deep neural learning network module in the joint neural network of GCN and GAT;

[0063] Figure 4 This is a comparison chart of the experimental results. DETAILED DESCRIPTION

[0064] The present invention will be further described below in conjunction with specific embodiments.

[0065] like Figure 1As shown in the figure, a cross-platform code transplantation quality assessment method and system provided in this case obtains an open source dataset and uses the Joern static analysis tool to generate a code property graph (CPG) of the source code and the cross-platform transplanted code, then uses NetworkX to construct an undirected graph and simplifies it into an adjacency matrix, and finally inputs it into a joint neural network composed of a graph convolutional network (GCN) and a graph attention network (GAT) to obtain node vectors. The feature vector of the graph is obtained through the PCA algorithm, thereby effectively processing the differences between the source code and its transplanted version. Finally, a quality assessment score is generated through an activation function. The method and system include the following steps: (1) obtaining an open source dataset, including a source code dataset and a cross-platform transplanted code dataset; wherein the source code dataset refers to the source code of the original project; the cross-platform transplanted code dataset is the code after the source code is translated and transplanted to the cross-platform, and after modification and debugging, it can run on the new target platform; the source code dataset and the cross-platform transplanted code dataset are in a one-to-one correspondence.

[0066] In this step, C++ code and Java code are taken as examples, and a Microsoft open source data set is used, in which the C++ code is the source code, and the Java code is the code after cross-platform porting. The Java code is ported after C++ code translation.

[0067] (2) Use the open source static analysis tool Joern to analyze C++ code and Java code and generate a code property graph. Joern is an open source source code analysis tool that generates a code property graph (CPG) without compiling the source code.

[0068] CPG is a graphical structure that combines syntax trees, control flow graphs, and data flow graphs. It can generate and query code property graphs and fully represent the behavioral and structural characteristics of the code. The open source static analysis tool Joern is an open source tool focused on code analysis that can generate and query code property graphs. It can directly analyze the source code and generate CPG. It supports user-defined queries to mine potential security vulnerabilities, complexity indicators, and improvement points in the code. Visualizing CPG can help understand the structure of the code.

[0069] (3) Use the networkx library to read the CPG and construct a code undirected graph. Use the betweenness-based community discovery algorithm to simplify the undirected graph and convert it into an adjacency matrix after removing redundant information. The NetworkX library is a Python library used to create, manipulate, and study complex network structures in the field of network analysis and graph theory. Use the networkx library to process the CPG and construct a code undirected graph.

[0070] Use the networkx library to read CPG to build an undirected graph of C++ and Java codes. Use a community discovery algorithm based on betweenness to simplify the undirected graph and convert it into an adjacency matrix after removing redundant information. The steps to use the community discovery algorithm based on betweenness to trim redundant edges from the undirected graph are as follows:

[0071] The first step is to read the code property graph (CPG) and use the networkx library to process the CPG to obtain the code undirected graph .in, represents the code property graph, represents the set of vertices in the graph, Represents the set of edges in a graph;

[0072] Step 2: For each edge in the code undirected graph, calculate its betweenness centrality, which represents the number of shortest paths through the edge; find the code undirected graph Delete the edge with the highest betweenness centrality from the code undirected graph to obtain a new code undirected graph ;

[0073] The definition of betweenness centrality is as follows, where Indicates passing through node of The number of shortest paths, express The number of shortest paths. The betweenness centrality reflects the importance of node v as a "bridge". ;

[0074] The third step is to use the depth traversal algorithm to determine whether the graph is connected. If it is connected, repeat steps (2) and (3). If it is not connected, The output is an adjacency matrix .

[0075] The community discovery algorithm is a community discovery algorithm based on betweenness. The basic idea is to continuously remove edges from the network in order from large to small according to the edge betweenness centrality until the entire network is decomposed into various communities. The code undirected graph is simplified based on the edge betweenness. By deleting unimportant or isolated nodes and edges, the connection relationship between different modules of the CPG is effectively simplified, which can enhance the connectivity within the modules in the code and make the code structure clear. This method guides the code attribute graph structure to converge to a more stable state by gradually deleting edges with high edge betweenness. Each deletion will reduce the possible connections in the network, making the subsequent division process more efficient, reducing the computational complexity, and improving the efficiency of the algorithm.

[0076] (4) After removing redundant edges from the C++ and JAVA codes in steps (2) and (3), the connection matrix is ​​output. The adjacency matrix of the C++ code is , the adjacency matrix of the Java code after cross-platform porting is . The adjacency matrix Input into the joint neural network of GCN and GAT to obtain the node vector of the graph.

[0077] The joint neural network of GCN and GAT is a neural network that combines graph convolutional networks (GCN) and graph attention networks (GAT) as basic components, and uses the advantages of both to perform feature learning and code quality evaluation after source code and cross-platform porting. The two neural networks do not share weights, and GCN (uses convolution operations to process graph structure data, which can effectively aggregate information of neighboring nodes. In each layer, information is propagated through the adjacency matrix. GAT introduces an attention mechanism, which allows the network to weightedly aggregate neighbor information according to the different importance of node neighbors. During the information propagation process, GAT calculates the weight of the edge through the self-attention mechanism, thereby achieving more flexible feature learning. By combining the advantages of GCN and GAT, the features extracted from these two modules are processed, and the joint neural network of GCN and GAT can achieve stronger expressiveness and flexibility in code quality evaluation tasks.

[0078] (4.1) The specific steps for extracting node vectors of C++ source code graphs through GAT are as follows:

[0079] Step 1: The adjacency matrix of the C++ source code is , the feature matrix H is represented by the feature vector of each node, N is the number of nodes, and F is the feature dimension. The feature matrix H is represented as: ;

[0080] Node connection adjacency matrix It is expressed as: ;

[0081] The feature matrix Through a learnable weight matrix Perform a linear transformation:

[0082] ,in is the feature dimension after transformation.

[0083] Step 2: For any pair of adjacent nodes and , calculate the attention coefficient between them :

[0084] : ;

[0085] Calculate the attention score using a learnable vector a:

[0086] ( ),in ;

[0087] The attention coefficients are normalized using the softmax function to obtain the weights αij:

[0088] ,in Is a node The set of neighbor nodes of

[0089] Use the calculated attention weights to perform weighted summation on the features of neighboring nodes and calculate the new representation hi′′ of node i: , where σ is the activation function ReLU.

[0090] The above-mentioned Graph Attention Networks (GAT) is an effective Graph Neural Network (GNN) architecture that can extract node vectors of the adjacency matrix through the self-attention mechanism. The adjacency matrix A describes the connection relationship between nodes in the graph. For each node, its feature vector is also required, and each row corresponds to the feature of a node. The details of the deep learning neural network described in this step are as follows Figure 2 shown.

[0091] GAT introduces a self-attention mechanism to calculate the influence between nodes. After multiple layers of GAT processing, the output of the last layer is the feature vector of each node, which can effectively capture the structure and attribute information of the node in the graph. Through the above steps, GAT can extract the feature vector of each node from the adjacency matrix. The self-attention mechanism of GAT provides flexible modeling of the relationship between nodes, significantly improving the ability of graph data processing.

[0092] (4.2) The specific steps for extracting node vectors of the transplanted Java code graph through GCN are as follows:

[0093] Step 1: The adjacency matrix of the transplanted Java code is B. Self-loops are added to the adjacency matrix, that is, the diagonal elements are set to 1. This ensures that each node can consider its own characteristics; , the updated adjacency matrix is ,in is the identity matrix;

[0094] Normalize the adjacency matrix: , where D is the degree matrix and D[i][j] represents the degree of node i.

[0095] In the second step, each node has a feature vector, and the features are combined into a feature matrix X, where the rows of X represent nodes and the columns represent features. In GCN, the feature vectors of nodes are convolved through the adjacency matrix. The calculation of a graph convolution layer can be expressed as: ,in It is The node feature matrix of the layer, is the learnable weight matrix and σ is the activation function ReLU.

[0096] The third step is to extract features through two layers of GCN convolution operations. In the first layer of the network, the adjacency matrix of the transplanted Java code is input, and the feature map extracted by the full convolution layer is used as the node feature. The first layer of the convolution module transfers information. After mapping the nodes to the latent space, the second layer of the GCN module is used to achieve more efficient graph convolution on the aggregated nodes. The final model is achieved by fusing the output node vectors of the two layers of the network.

[0097] Step 4: Finally, we get the embedding representation of each node (i.e., node vector), which can capture the characteristics of the structure and attributes of the node in the graph. By training the GCN model and using the above steps and the adjacency matrix, we get the embedding vector of each node. The details of the deep learning neural network described in this step are as follows: Figure 3 shown.

[0098] (5) Use PCA to transform the node vector of the graph into a graph feature vector, and finally obtain the quality assessment score through the activation function. The specific steps are as follows:

[0099] After obtaining the feature vector of each node, the vector comes from the output of the graph convolutional network (GCN) and the graph attention network (GAT). Combine the vectors of all nodes into a matrix , where each row represents the feature vector of a node. Assume there are N nodes, and the feature vector of each node is dimension, we get the node feature matrix The dimension is ;

[0100] ;

[0101] Make sure each feature has a mean of 0 and a variance of 1 and standardize it using the following formula: ,in is the mean of the feature, is the standard deviation;

[0102] Calculate the standardized feature matrix The covariance matrix of : ;

[0103] Solving for eigenvalues ​​and eigenvectors: Covariance matrix Perform eigenvalue decomposition to obtain the eigenvalues ​​and corresponding eigenvectors: ,in, is the eigenvalue, is the eigenvector;

[0104] Select the principal component: According to the size of the eigenvalue, select the first The eigenvectors corresponding to the eigenvalues ​​are the principal components, which are used to represent the main features of the data. Projecting into the selected principal component space, we obtain the eigenvector of the graph: ,in is the eigenvector matrix formed by the selected principal components.

[0105] Through the above steps, the source code and the cross-platform ported code are converted into an adjacency matrix and The node vector of is converted into a graph feature vector, which retains most of the information and can be used for subsequent quality assessment tasks in a low-dimensional space. The benefit of using PCA is that it can significantly reduce the computational complexity while improving the visualization effect.

[0106] The PCA algorithm is used to compress the data space, map the features of high-dimensional data to low-dimensional space, achieve dimensionality reduction and noise reduction of feature vectors, and remove redundant information and noise interference. The feature vectors of the graph are used to measure the semantic and grammatical features of the source code and the code after cross-platform porting. Finally, an activation function Softmax is used to obtain the quality assessment score of the C++ code ported to Java code. The closer the quality assessment score is to 1, the higher the quality of the code porting. In this example, the quality assessment score of the C++ code ported to Java code is 0.74, which is consistent with the actual Microsoft The quality of open source datasets is similar, and the evaluation method in this case can be used to evaluate Microsoft The quality of code porting of open source datasets is evaluated.

[0107] In summary, by adopting a cross-platform code transplantation quality assessment method and system of the present invention, the joint neural network of GCN and GAT is used as an effective method for quality assessment of source code and cross-platform transplanted code, which well realizes the fact that traditional assessment methods cannot pay good attention to the grammatical and semantic structure of deep code and other features, improves the accuracy and efficiency of code assessment, and has practical application value.

[0108] Example 2

[0109] like Figure 1 , Figure 2 and Figure 3As shown, this example provides a cross-platform code transplant quality assessment method, system and device, which uses a betweenness-based community discovery algorithm to simplify the code undirected graph and a joint neural network.

[0110] In this example, we use C# code to Java code, C++ code to C# code, and Java code to C++ code as examples. We use the GitHub open source dataset and manually write a code dataset with higher accuracy after the migration as a comparison. The quality assessment score is calculated through the following steps:

[0111] (1) Obtain basic data, using open source data sets, to obtain source code and cross-platform ported code data sets, where the source code data set and the cross-platform ported code data set are in one-to-one correspondence;

[0112] (2) Use the open source static analysis tool Joern to analyze the source code and the cross-platform ported code to generate a code property graph;

[0113] (3) Use the networkx library to read the CPG code to build an undirected graph, use the betweenness-based community discovery algorithm to simplify the undirected graph, and convert it into an adjacency matrix after removing redundant information;

[0114] (4) Input the adjacency matrix of the source code and the cross-platform ported code after conversion in steps (2) and (3) into the joint neural network of the graph convolution network and the graph attention network to obtain the node vector of the graph. The graph convolution network is abbreviated as GCN, and the graph attention network is abbreviated as GAT.

[0115] (5) The node vector of the graph is converted into a graph feature vector using principal component analysis, and finally a quality assessment score is obtained through an activation function; the joint neural network of GCN and GAT has two inputs, two neural networks, and the weights of the two neural networks are not shared.

[0116] The details of the deep learning neural network described in this step are as follows Figure 2 , Figure 3 shown.

[0117] In this embodiment, the method is tested using open source datasets and manually compiled datasets, such as Figure 4 As shown in the figure, the evaluation results of the open source dataset and the manually written high-quality dataset are similar. The comparison results show that the evaluation method can accurately evaluate the quality of the source code and the transplanted code.

[0118] In summary, the cross-platform code transplant quality assessment method, system and device of the present invention provide a new method for code transplant assessment, and use a joint neural network in code assessment, which greatly improves the code transplant quality assessment capability.

Claims

1. A cross-platform code transplantation quality assessment method, characterized in that: The method comprises the following steps: (1) Obtain basic data, using open source data sets, to obtain source code and cross-platform ported code data sets, where the source code data set and the cross-platform ported code data set are in one-to-one correspondence; (2) Use the open source static analysis tool Joern to analyze the source code and the cross-platform ported code to generate a code property graph; (3) Use the networkx library to read the code property graph and construct a code undirected graph, which is abbreviated as CPG. Use the betweenness-based community discovery algorithm to simplify the undirected graph and convert it into an adjacency matrix after removing redundant information. (4) Input the adjacency matrix of the source code and the cross-platform ported code after conversion in steps (2) and (3) into the joint neural network of the graph convolution network and the graph attention network to obtain the node vector of the graph. The graph convolution network is abbreviated as GCN, and the graph attention network is abbreviated as GAT. (5) The node vector of the graph is converted into a graph feature vector using principal component analysis, and finally a quality assessment score is obtained through an activation function; the joint neural network of GCN and GAT has two inputs, two neural networks, and the weights of the two neural networks are not shared; The steps of using the betweenness-based community discovery algorithm to prune redundant edges from the code undirected graph in step (3) are as follows: The first step is to read the code attribute graphs of the source code and the cross-platform ported code respectively, and use the networkx library to process the CPG to obtain the code undirected graph G(V,E); where G represents the code attribute graph, V represents the vertex set in the graph, and E represents the edge set in the graph; Step 2: For each edge in the code undirected graph, calculate its betweenness centrality, which represents the number of shortest paths through the edge; find the code undirected graph G and delete the edge with the highest betweenness centrality from the code undirected graph to obtain a new code undirected graph G'; The third step is to use a deep traversal algorithm to determine whether the code undirected graphs of the source code and the cross-platform ported code are connected. If they are connected, repeat steps (2) and (3). If they are not connected, output the code undirected graph G as an adjacency matrix A.

2. A cross-platform code transplantation quality assessment method according to claim 1, characterized in that: The step (4) extracts the node vector of the source code graph through GAT, and the specific steps are as follows: Step 1. The adjacency matrix of the source code is A. The feature matrix H is represented by the feature vector of each node. N is the number of nodes and F is the feature dimension. The feature matrix H is represented as: The node connection adjacency matrix A is expressed as: A∈R N×N ; The feature matrix H is linearly transformed through a learnable weight matrix W: H′=HW,where W∈R F×F′ , F′ is the feature dimension after transformation; Step 2: For any pair of adjacent nodes i and j, calculate the attention coefficient e between them ij : First, concatenate the features of node i and node j: Calculate the attention score using a learnable vector a: where a∈R 2F′ ; The attention coefficients are normalized using the softmax function to obtain the weights αij: Where θ(i) is the set of neighbor nodes of node i; Step 3: Use the calculated attention weights to perform weighted summation on the features of neighboring nodes and calculate the new representation h of node i i ″:h i ″=σ(∑ j∈θ(i) α ij h j ′), where σ is the activation function ReLU.

3. A cross-platform code transplantation quality assessment method according to claim 1, characterized in that: The node vectors of the source code graph are extracted through GCN. The specific steps are as follows: Step 1: The adjacency matrix of the code after cross-platform porting is B. Self-loops are added to the adjacency matrix, that is, the diagonal elements are set to 1; B'=B+I, and the updated adjacency matrix is ​​B', where I is the identity matrix; Normalize the adjacency matrix: Where D is the degree matrix, D[i][j] represents the degree of node i; In the second step, each node has a feature vector, and the features are combined into a feature matrix X, where the rows of X represent nodes and the columns represent features. In GCN, the feature vectors of the nodes are convolved through the adjacency matrix. The calculation of a graph convolution layer can be expressed as: H (l+1) =σ(B”H (l) W (l) ), Among them, H (l) is the node feature matrix of the lth layer, W (l) is a learnable weight matrix, σ is the activation function ReLU; The third step is to extract features through two layers of GCN layer convolution operations. In the first layer of the network, the adjacency matrix of the code after cross-platform porting is input, and the feature map extracted by the full convolution layer is used as the node feature. The first layer of the convolution module transfers information; after mapping the nodes to the latent space, the second layer of the GCN module is used to implement graph convolution on the aggregated nodes. The final model is achieved by fusing the output node vectors of the two layers of the network; Finally, the embedded representation of each node, that is, the node vector, is obtained by training the GCN model and using the embedded vector of each node.

4. A cross-platform code transplantation quality assessment method according to claim 1, characterized in that: The step (5) uses PCA to convert the node vector of the graph into a graph feature vector, and finally obtains the quality assessment score through the activation function. The specific steps are as follows: Step 1: After obtaining the feature vector of each node, the vector comes from the output of GCN and GAT; combine the vectors of all nodes into a matrix X, where each row represents the feature vector of a node; there are N nodes, and the feature vector of each node has D dimensions, so the dimension of the node feature matrix X is N×D; Make sure each feature has a mean of 0 and a variance of 1 and standardize it using the following formula: Where μ is the mean of the feature and σ is the standard deviation; The second step is to calculate the covariance matrix C of the standardized feature matrix X′: Solve the eigenvalue and eigenvector: perform eigenvalue decomposition on the covariance matrix C to obtain the eigenvalue and the corresponding eigenvector; Cv = λv, where λ is the eigenvalue and v is the eigenvector; Step 3: Select the principal components: According to the size of the eigenvalues, select the eigenvectors corresponding to the first k eigenvalues. These eigenvectors are the principal components, which are used to represent the main features of the data. Project the original node feature matrix X′ into the selected principal component space to obtain the eigenvector of the graph. G = X'W, where W is the eigenvector matrix formed by the selected principal components; Through the above steps, the source code and the cross-platform ported code are converted into the node vectors of the adjacency matrix A and B into graph feature vectors, and the graph feature vectors are used to measure the semantic and grammatical features of the source code and the cross-platform ported code, and finally a quality assessment score is obtained through an activation function.

5. A cross-platform code transplantation quality assessment system, characterized in that: The system runs the method described in any one of claims 1 to 4, wherein the system comprises a basic data input module, a code attribute graph generation module, an adjacency matrix conversion module, a node vector acquisition module, and a quality assessment module; The basic data input module is used to obtain source code and cross-platform transplanted code data sets; The generated code attribute graph module executes step (2) of the method described; The adjacency matrix conversion module executes step (3) of the method described; The node vector obtaining module executes step (4) of the method described; The quality assessment module executes step (5) of the method described.

6. A cross-platform code transplant quality assessment device, characterized in that: The device is equipped with the system according to claim 5.

Citation Information

Patent Citations

  • Code feature extraction method and system based on hierarchical comparative learning

    CN113238797A

  • Multi-mode-based function level vulnerability detection method

    CN116611071A