Code template extraction method and related device
By automatically extracting code templates from a large number of software projects and using graph structure and maximum common subgraph algorithms to generate rich code templates, the problem of simple content in existing code templates and human summary bias is solved, and efficient and accurate code template generation is achieved.
Patent Information
- Application Number
- CN202110639042.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-08
- Publication Date
- 2026-01-16
- Estimated Expiration
- 2041-06-08
AI Technical Summary
The existing code templates are simple and limited in number, which cannot meet the needs of developers. Furthermore, the code templates summarized by humans have biases and omissions.
By automatically extracting code templates from a large number of software projects, using graph structures to display method call relationships, and employing the maximum common subgraph algorithm and clustering techniques, rich and usable code templates are generated.
It improves the usability of code templates, generates feature-rich code templates that meet development needs, and avoids the biases and omissions of human summarization.
Smart Images

Figure CN115454392B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of program development, and particularly relates to a code template extraction method and related equipment. BACKGROUND
[0002] In the software development process, code templates are helpful to improve the development efficiency of developers. Code templates can reduce the amount of code writing, simplify the development process, shorten the development cycle, and save development costs. However, there are two problems with the code templates on the market at present: on the one hand, the existing code templates have simple content and limited quantity, and cannot meet the needs of developers; on the other hand, the current code templates are mainly obtained by human summary, and are greatly influenced by subjectivity, which may cause a large deviation between the obtained code templates and the actual needs of developers. SUMMARY
[0003] Embodiments of the present application provide a code template extraction method and related equipment, which can automatically extract code templates from software projects and improve the usability of code templates.
[0004] The first aspect of the present application discloses a code template extraction method, which comprises: obtaining a plurality of software projects; for each software project, generating a graph structure, which shows the syntax structure and method calling relationship of the code files contained in the software project; finding the maximum common subgraph between each two graph structures; calculating the similarity between each two maximum common subgraphs; clustering all the maximum common subgraphs according to the similarity between each two maximum common subgraphs to obtain at least one similar subgraph group; and for each target similar subgraph group in the at least one similar subgraph group, generating a code template according to the code corresponding to each node and the method calling relationship corresponding to each edge of the target subgraph in the target similar subgraph group.
[0005] The code template extraction method provided by the embodiments of the present application can automatically extract code templates from a large number (for example, one hundred thousand or one million) of software projects, and the extracted code templates are rich in functions and can meet the needs of developers. The method shows the method calling relationship of the software project through a graph structure, so that the extracted code templates have complete calling chains and the usability of the code templates is improved. The method automatically extracts code templates, avoiding the deviation and omission of human summary.
[0006] In some optional embodiments, the generating a graph structure for each software project comprises: performing syntax analysis on each code file of the software project to obtain an abstract syntax tree of each code file, each class defined in the code file corresponding to an abstract syntax tree; performing code dependency analysis on each code file of the software project to obtain a method call relationship of each code file; and combining the abstract syntax tree and the method call relationship of each code file of the software project to obtain the graph structure corresponding to the software project.
[0007] The code template generation method provided by the embodiments of the present application performs syntax analysis on each class in a code file, converts each class in the code file into an abstract syntax tree, and thus converts the code file into a structured representation.
[0008] In some optional embodiments, the obtaining the maximum common subgraph between each two graph structures comprises: using a maximum common subgraph algorithm based on a maximum clique to obtain the maximum common subgraph between each two graph structures.
[0009] In some optional embodiments, the obtaining the maximum common subgraph between each two graph structures comprises: performing fuzzy processing on classes, methods and variables contained in all nodes of the two graph structures; generating an association graph of the two graph structures, each node of the association graph being a node combination of the two graph structures, and each edge of the association graph corresponding to an edge that exists simultaneously or does not exist simultaneously in the two graph structures; and solving a maximum clique of the association graph to obtain the maximum common subgraph.
[0010] In some optional embodiments, the solving the maximum clique of the association graph comprises: solving the maximum clique of the association graph by using a tabu search algorithm.
[0011] The maximum common subgraph algorithm based on a maximum clique can solve a nondeterministic polynomial difficult problem and quickly and accurately obtain the maximum common subgraph between each two graph structures.
[0012] In some optional embodiments, the calculating the similarity between each two maximum common subgraphs comprises: performing random traversal on the two maximum common subgraphs; counting frequencies of each node being visited in the two maximum common subgraphs; and calculating a matching degree of the frequencies of each node being visited in the two maximum common subgraphs, the matching degree being taken as the similarity of the two maximum common subgraphs.
[0013] In some optional embodiments, after the calculating the similarity between each two maximum common subgraphs, the method further comprises: normalizing the similarity between each two maximum common subgraphs; and forming a similarity matrix by using the normalized similarity between each two maximum common subgraphs.
[0014] In some optional embodiments, the clustering of the maximal common subgraphs according to the similarity between each two maximal common subgraphs comprises hierarchical clustering of the maximal common subgraphs according to the similarity between each two maximal common subgraphs.
[0015] In some optional embodiments, the number of maximal common subgraphs in the target similar subgraph group is greater than or equal to a first preset number. In some optional embodiments, the number of target similar subgraph groups is less than or equal to a second preset number.
[0016] The number of maximal common subgraphs in the similar subgraph group being greater than or equal to a first preset number indicates that the corresponding code of the similar subgraph group has a high frequency of occurrence in the code file (i.e., is more general). The number of similar subgraph groups being greater than or equal to a second preset number indicates that the grouping of maximal common subgraphs is excessive. By screening the similar subgraph groups, a code template with more generalization can be obtained.
[0017] In some optional embodiments, before the code template is generated according to the code corresponding to each node and the method call relationship corresponding to each edge of the target subgraph in the target similar subgraph group, the method further comprises: calculating the sum of squared distances of each maximal common subgraph in the target similar subgraph group from other maximal common subgraphs, and taking the maximal common subgraph with the smallest sum of squared distances as the target subgraph; or calculating the average distance of each maximal common subgraph in the target similar subgraph group from other maximal common subgraphs, and taking the maximal common subgraph with the smallest average distance as the target subgraph.
[0018] In some optional embodiments, after the code template is generated according to the code corresponding to each node and the method call relationship corresponding to each edge of the target subgraph in the target similar subgraph group, the method further comprises: supplementing the missing call chain of the code template; and / or cleaning up the useless logic code of the code template.
[0019] In some optional embodiments, the supplementing of the missing call chain of the code template and / or the cleaning up of the useless logic code of the code template comprises: supplementing, by a general control flow graph, the missing call chain of the code template; and / or cleaning up, by a general control flow graph, the useless logic code of the code template.
[0020] The second aspect of the present application discloses a computer-readable storage medium comprising computer instructions, which, when executed on an electronic device, cause the electronic device to perform the code template extraction method of the first aspect.
[0021] The third aspect of the present application discloses an electronic device, the electronic device includes a processor and a memory, the memory is used for storing instructions, and the processor is used for calling the instructions in the memory, so that the electronic device executes the code template extraction method as described in the first aspect.
[0022] The fourth aspect of the present application discloses a chip system applied to an electronic device; the chip system includes an interface circuit and a processor; the interface circuit and the processor are interconnected through a circuit; the interface circuit is used for receiving a signal from a memory of the electronic device and sending a signal to the processor, and the signal includes computer instructions stored in the memory; when the processor executes the computer instructions, the chip system executes the code template extraction method as described in the first aspect.
[0023] It should be understood that the computer readable storage medium of the second aspect, the electronic device of the third aspect and the chip system of the fourth aspect provided above all correspond to the method of the first aspect, and therefore the beneficial effects achieved thereby can refer to the beneficial effects of the corresponding method provided above, which will not be described here again. BRIEF DESCRIPTION OF DRAWINGS
[0024] Figure 1 is an application scenario diagram of the code template extraction method provided by the embodiments of the present application.
[0025] Figure 2 is a flowchart of the code template extraction method provided by the embodiments of the present application.
[0026] Figure 3 is a diagram of performing syntax analysis on each code file of a software project to obtain an abstract syntax tree of each code file.
[0027] Figure 4 is a diagram of obtaining a graph structure corresponding to a software project by combining an abstract syntax tree and a method call relationship of each code file of the software project.
[0028] Figure 5 is a diagram of a maximum common subgraph between two graph structures.
[0029] Figure 6 is a diagram of performing fuzzification processing on classes, methods and variables contained in nodes of a graph structure.
[0030] Figure 7 is another diagram of a maximum common subgraph between two graph structures.
[0031] Figure 8 is a structural diagram of the electronic device provided by the embodiments of the present application. DETAILED DESCRIPTION
[0032] For ease of understanding, exemplary descriptions of some concepts related to the embodiments of the present application are given for reference.
[0033] It should be noted that "at least one" in the present application means one or more, and "multiple" means two or more than two. The "and / or" describes the association between the associated objects, which means that there can be three relationships, for example, A and / or B can represent: A exists alone, A and B exist together, and B exists alone, where A and B can be singular or plural. The terms "first", "second", "third", "fourth" and the like (if any) in the description and claims of the present application and the drawings are used to distinguish similar objects, and are not used to describe a specific order or sequence.
[0034] In order to better understand the code template extraction method and related equipment provided by the embodiments of the present application, the application scenario of the code template extraction method of the present application will be described first.
[0035] Figure 1 is the application scenario diagram of the code template extraction method provided by the embodiments of the present application.
[0036] As Figure 1 shown, the code template extraction method provided by the embodiments of the present application is applied to an electronic device 10 (shown as a notebook in the figure). According to the code template extraction method provided by the embodiments of the present application, the electronic device 10 can automatically extract code templates from a large number of software projects (for example, one hundred thousand or one million). The extracted code templates have rich functions and can meet the needs of developers. The method displays the method call relationship of the software project through the graph structure, so that the extracted code templates have complete call chains, improving the usability of the code templates. The method automatically extracts code templates, avoiding the deviation and omission of human summary.
[0037] The electronic device 10 can be a tablet computer, a desktop computer, a notebook computer, a palm computer, a cloud server, etc.
[0038] Figure 2 is the flowchart of the code template extraction method provided by the embodiments of the present application. The code template extraction method is applied to an electronic device.
[0039] As Figure 2 shown, the code template extraction method provided by the embodiments of the present application specifically includes:
[0040] 201, obtaining a plurality of software projects.
[0041] Each software project is used to implement a software development. Each software project includes at least one code file. Each code file includes multiple lines of code.
[0042] The electronic device can include an internal memory from which the electronic device obtains the plurality of software items. Alternatively, the electronic device can connect an external device (e.g., an item server) from which the electronic device obtains the plurality of software items.
[0043] The electronic device can obtain the plurality of software items by accessing a preset local path. Alternatively, the electronic device can obtain the plurality of software items by accessing a designated network path.
[0044] 202, for each software item, a graph structure is generated.
[0045] The graph structure is used to show the syntax structure and method call relationship of the code files included in the software item.
[0046] In an embodiment of the present application, for each software item, syntax analysis is performed on each code file of the software item to obtain an abstract syntax tree of each code file, each class defined in the code file corresponding to an abstract syntax tree; code dependency analysis is performed on each code file of the software item to obtain a method call relationship of each code file; and the abstract syntax tree and the method call relationship of each code file of the software item are combined to obtain a graph structure corresponding to the software item.
[0047] An abstract syntax tree (AST) is an abstract representation of the syntax structure of source code. The abstract syntax tree represents the syntax structure of the source code in a tree form, each node of the abstract syntax tree representing a structure of the source code, such as a class, a method, etc.
[0048] Syntax analysis of each code file of the software item is to decompose the syntax structure of the code file.
[0049] For example, the code file includes the following code:
[0050]
[0051] The main body of this piece of code is the HelloWorld class, the class has the main method, and the main method calls the System.out.println method to output the "Hello World" string.
[0052] Figure 3 is a schematic diagram of syntax analysis of each code file of the software item to obtain an abstract syntax tree of each code file. As Figure 3 shown, syntax analysis can be performed on the above code to obtain Figure 3The abstract syntax tree shown includes four nodes of JCCompilationUnit, JCClassDecl, JCMethodDecl and JCMethodInvocation, where JCCompilationUnit represents a compilation unit, JCClassDecl represents a class declaration, JCMethodDecl represents a method declaration, and JCMethodInvocation represents a method invocation. Figure 3 The abstract syntax tree shown covers the HelloWorld class through JCClassDecl, covers the main method through JCMethodDecl, and covers the System.out.println method invocation through JCMethodInvocation.
[0053] The code template generation method provided in the embodiments of the present application performs syntax analysis on each class in a code file, converts each class in the code file into an abstract syntax tree, and thus converts the code file into a structured representation.
[0054] With the abstract syntax tree, a class-level graph structure can be obtained. However, the graph structure is composed of multiple disconnected trees (i.e., abstract syntax trees), and the relationship between the trees (i.e., classes) is lost. In order to associate the abstract syntax trees, code dependency analysis can be performed on each code file of the software project to obtain the method invocation relationship of each code file. The method invocation relationship of each code file can include the method invocation relationship between different classes of the same code file, or the method invocation relationship between different classes of different code files. The code dependency analysis on each code file of the software project can be performed by a syntax analyzer (for example, ANTLR), and the method invocation relationship of each code file can be extracted. The method invocation relationship of each code file can be recorded in a chart.
[0055] After obtaining the abstract syntax tree of each code file of the software project and the method invocation relationship of each code file, the abstract syntax tree and the method invocation relationship of each code file of the software project can be combined to obtain the graph structure corresponding to the software project. The combination of the abstract syntax tree and the method invocation relationship of each code file of the software project to obtain the graph structure corresponding to the software project is to associate the abstract syntax trees of the code files of the software project, and thus integrate the entire software project into a graph.
[0056] Figure 4 is a schematic diagram of combining the abstract syntax tree and the method invocation relationship of each code file of the software project to obtain the graph structure corresponding to the software project. As shown in FIG. 4, the abstract syntax tree of each code file of the software project is associated, and the entire software project is integrated into a graph. Figure 4As shown, the software item includes three abstract syntax trees AST A, AST B and AST C, there is a method call relationship between AST A and AST B, and there is a method call relationship between AST B and AST C, therefore, AST A and AST B are associated (i.e. a connection is added between AST A and AST B), and AST B and AST C are associated (i.e. a connection is added between AST B and AST C), to obtain a graph structure corresponding to the software item.
[0057] In an embodiment of the present application, the generated graph structure can include the direction of the method call. For example, if a node a in AST A calls a node b in AST B, then in the generated graph structure, node a points to node b. For another example, if a node b in AST B calls a node c in AST C, then in the generated graph structure, node b points to node c.
[0058] In this step, one graph structure is generated for each software item, and the software item and the graph structure are in one-to-one correspondence. For example, if there are N software items, then N graph structures are generated.
[0059] 203, find the maximum common subgraph between each two graph structures.
[0060] The definition of the maximum common subgraph is as follows: given two graph structures G and G', if there is another graph structure g, which satisfies and there is no graph structure such that |g'| ≥ |g|, then g is called the maximum common subgraph of graph G and G'. That is, the maximum common subgraph between two graph structures is the largest identical part of the two graph structures.
[0061] In this step, for each two graph structures, the maximum common subgraph between the two graph structures is found. For example, if there are N software items, corresponding to N graph structures, there are N(N-1) / 2 combinations, then N(N-1) / 2 maximum common subgraphs need to be found.
[0062] The code template is the common framework part in the code file, and for the graph structure, the code template is the common subgraph of the graph structure. Therefore, the extraction problem of the code template can be converted into finding the maximum common subgraph between two graphs.
[0063] Figure 5 is a schematic diagram of the maximum common subgraph between two graph structures. In the diagram, 51 and 52 are two graph structures, and the maximum common subgraph between the two graph structures includes six nodes (i.e. the gray nodes).
[0064] The solution of the maximum common subgraph belongs to a non-deterministic polynomial (NP) difficult problem, and the solution of the NP difficult problem is mainly divided into two kinds of incomplete algorithm and complete algorithm. The incomplete algorithm only searches part of the solution space, and reduces the solution time on the premise of sacrificing the accuracy. The complete algorithm will traverse all the solution space to find the exact solution. In the embodiments of the present application, the maximum common subgraph of each two graph structures can be solved by the complete algorithm, and the maximum common subgraph algorithm based on the maximum group can be used for solving.
[0065] A group (clique) is a complete subgraph of an undirected graph, and there is an edge between each pair of nodes (vertices). If a group is not contained in any other group, that is, the group is not a proper subset of any other group, then the group is called a maximal group of the graph G. The maximum group is the maximal group with the largest number of nodes. That is, the maximum group is the largest group globally, and the maximal group is the largest group locally.
[0066] The maximum common subgraph algorithm based on the maximum group converts the maximum common subgraph problem into the maximum group problem, and finds the optimal solution by the corresponding solving method. The maximum common subgraph algorithm based on the maximum group needs to judge the matching of the nodes.
[0067] The code template is the public framework part in the code file. In order to identify the public framework part in the code file and avoid the influence of different class names, method names and variable names (or variables), when the maximum common subgraph between each two graph structures is solved, the classes, methods and variables contained in all the nodes of the two graph structures can be fuzzed (mainly the class names, method names and variable names are fuzzed, and the variables are also fuzzed). The class names, method names and variable names (or variables) contained in all the nodes of the two graph structures can be replaced by a unified character.
[0068] Figure 6 is a schematic diagram of the fuzzing of the classes, methods and variables contained in the nodes of the graph structure. It is assumed that the graph structure includes Figure 3 The abstract syntax tree shown in the drawing, after the fuzzing of the classes, methods and variables contained in all the nodes of the graph structure, the abstract syntax tree becomes Figure 6 , wherein the class name "HelloWorld" in the JCClassDecl node is fuzzed to "ClassA", and the variable "Hello World" in the JCMethodInvocation node is fuzzed to "StringA".
[0069] Figure 7is another illustration of the maximum common subgraph between two graph structures. In the figure, 71 and 72 are the graph structures after fuzzification, and the maximum common subgraph between 71 and 72 includes three nodes (i.e., the gray nodes).
[0070] The maximum common subgraph between two graph structures can be obtained by generating an associated graph of the two graph structures. Each node of the associated graph corresponds to a node combination of the two graph structures, and each edge of the associated graph corresponds to an edge that exists simultaneously or does not exist simultaneously in the two graph structures.
[0071] The maximum clique of the associated graph of the two graph structures can be solved by a Tabu Search (TS) algorithm.
[0072] The Tabu Search algorithm is a global neighborhood search algorithm that simulates the optimization characteristics of human memory functions. It avoids detours by using a local neighborhood search mechanism and corresponding Tabu criteria, and releases some excellent states that are prohibited by the Tabu level, thereby ensuring diversified and effective exploration to ultimately achieve global optimization.
[0073] In an embodiment of the present application, a maximum common subgraph algorithm based on the maximum clique is used to obtain the maximum common subgraph between each two graph structures. The specific method is as follows:
[0074] Two graph structures are denoted as G1 and G2, the nodes of G1 are g 1i , i = 1, 2, …, n, n is the number of nodes of G1, the nodes of G2 are g 2j , j = 1, 2, …, m, m is the number of nodes of G2, and the maximum common subgraph between G1 and G2 is obtained by:
[0075] Fuzzification is performed on the classes, methods, and variables contained in all nodes of G1 and G2;
[0076] An associated graph G3 corresponding to G1 and G2 is generated, G3 includes nodes (g 1i , g 2j ), i = 1, 2, …, n, j = 1, 2, …, m, which are node combinations of G1 and G2, for any two nodes (g 1i , g 2j ) and (g 1i , g 2j′ ) of G3, i' = 1, 2, …, n, i ≠ i', j' = 1, 2, …, m, j ≠ j', if g 1i , g 1i′ exist in G1, and g 2j , g 2j′ exist in G2, or if g 1i , g 1i′There is no edge in G1, while g 2j , g 2j′ There is no edge in G2, while (g 1i , g 2j ) and (g 1i , g 2j′ ) have edges in G3.
[0077] Solve the maximum clique of G3, and obtain the maximum common subgraph of G1 and G2.
[0078] It should be noted that if the graph structure generated in 202 includes the direction of method call, when calculating the maximum common subgraph between each two graph structures, the direction of the edge representing the method call relationship in the two graph structures needs to be considered. In the two graph structures, if the direction of the edge representing the method call relationship is inconsistent, the edge does not belong to the maximum common subgraph.
[0079] 204, calculate the similarity between each two maximum common subgraphs.
[0080] In an embodiment of the present application, the similarity between each two maximum common subgraphs is calculated in the following way:
[0081] Randomly traverse the two maximum common subgraphs;
[0082] Statistical frequency of each node in the two maximum common subgraphs being visited;
[0083] Calculate the matching degree of the frequency of each node in the two maximum common subgraphs being visited, and take the matching degree as the similarity of the two maximum common subgraphs.
[0084] Random traversal is to randomly select a node from the maximum common subgraph for visiting, and then randomly select the next node from the node for visiting, until the leaf node of the maximum common subgraph is visited. The two maximum common subgraphs can be randomly traversed for a preset number of times (for example, each maximum common subgraph is randomly traversed 200 times), and after the two common subgraphs are traversed for the preset number of times, the frequency of each node in the two maximum common subgraphs being visited is counted.
[0085] The matching degree of the frequency of each node in the two maximum common subgraphs being visited can be calculated, and the matching degree of the frequency of each node in the two maximum common subgraphs being visited is calculated according to the matching degree of the frequency of each node being visited.
[0086] In an embodiment of the present application, after calculating the similarity between each two maximum common subgraphs, the similarity between each two maximum common subgraphs can be normalized; the similarity between each two maximum common subgraphs after normalization forms a similarity matrix.
[0087] 205, clustering the maximal common subgraphs according to the similarity between each two maximal common subgraphs to obtain a similar subgraph group.
[0088] In an embodiment of the present application, the maximal common subgraphs can be hierarchically clustered according to the similarity between each two maximal common subgraphs to obtain a similar subgraph group. The number of similar subgraph groups can be one or multiple (usually multiple).
[0089] All maximal common subgraphs can be clustered. Alternatively, part of the maximal common subgraphs can be clustered.
[0090] Hierarchical clustering is a kind of clustering algorithm that creates a hierarchical nested clustering tree by calculating the similarity between different categories of data points. In the clustering tree, different categories of original data points are the lowest layer of the tree, and the root node of the tree is the root node of a cluster. The clustering tree can be created by a bottom-up merging method. The merging algorithm of hierarchical clustering combines the two most similar data points in all data points by calculating the similarity between them, and iterates this process to generate a clustering tree.
[0091] In an embodiment of the present application, the maximal common subgraphs can be clustered based on the similarity matrix of the maximal common subgraphs. As described above, the similarity matrix is generated according to the similarity between each two maximal common subgraphs.
[0092] 206, obtaining a target similar subgraph group from the similar subgraph groups, and obtaining a target subgraph from each target similar subgraph group.
[0093] Each similar subgraph group can be a target similar subgraph group. Alternatively, part of the similar subgraph groups can be target similar subgraph groups.
[0094] A preset number of similar subgraph groups can be selected as target similar subgraph groups. Alternatively, similar subgraph groups that meet the conditions can be selected as target similar subgraph groups.
[0095] One target subgraph can be obtained from one target similar subgraph group. Alternatively, multiple target subgraphs can be obtained from one target similar subgraph group.
[0096] In an embodiment of the present application, for each similar subgraph group, it is determined whether the number of maximal common subgraphs of the similar subgraph group is greater than or equal to a first preset number (for example, 100). If the number of maximal common subgraphs of the similar subgraph group is greater than or equal to the first preset number, the similar subgraph group is a target similar subgraph group. That is, the number of maximal common subgraphs of the target similar subgraph group can be greater than or equal to the first preset number.
[0097] The number of the maximum common subgraphs of the similar subgraph group is greater than or equal to the first preset number, which indicates that the similar subgraph group corresponds to a code that appears frequently in the code file (i.e., the code is more common), and thus the similar subgraph group is taken as a target similar subgraph group to obtain a code template.
[0098] In another embodiment of the present application, it can be determined whether the number of the similar subgraph groups is greater than or equal to a second preset number (e.g., 200). If the number of the similar subgraph groups is greater than or equal to the second preset number, a third preset number of the similar subgraph groups are selected from the similar subgraph groups as the target similar subgraph groups. The third preset number is less than or equal to the second preset number. That is, the number of the target similar subgraph groups can be less than or equal to the second preset number.
[0099] The number of the similar subgraph groups being greater than or equal to the second preset number indicates that the grouping of the maximum common subgraphs is excessive, and thus a preset number (i.e., the third preset number) of the similar subgraph groups can be selected as the target similar subgraph groups to obtain the code template. For example, the first 200 similar subgraph groups with the largest number of the maximum common subgraphs can be selected as the target similar subgraph groups.
[0100] For each target similar subgraph group, a maximum common subgraph in the target similar subgraph group can be selected as a target subgraph at random, or a maximum common subgraph satisfying a preset condition can be selected as the target subgraph. The number of the target subgraphs can be one or multiple.
[0101] In an embodiment of the present application, for each target similar subgraph group, a distance sum of squares of each maximum common subgraph in the target similar subgraph group and other maximum common subgraphs can be calculated, and a maximum common subgraph with the smallest distance sum of squares is taken as a target subgraph of the target similar subgraph group. Alternatively, an average distance of each maximum common subgraph in the target similar subgraph group and other maximum common subgraphs can be calculated, and a maximum common subgraph with the smallest average distance is taken as the target subgraph of the target similar subgraph group.
[0102] 207, code restoration is performed on the target subgraph according to the code file to obtain a code template.
[0103] The code restoration on the target subgraph according to the code file is to determine a code corresponding to each node and a method call relationship corresponding to each edge of the target subgraph according to the code file, and to generate a code template according to the code corresponding to each node and the method call relationship corresponding to each edge of the target subgraph.
[0104] For example, the code restoration is performed on the target subgraph shown in FIG. 7 to obtain the following code template. Figure 7
[0105]
[0106] In an embodiment of the present application, after the code template is obtained by code restoration of the target subgraph according to the code file, the missing call chain of the code template can be supplemented. The missing call chain of the code template is supplemented by making the incomplete method call statement in the code template complete.
[0107] Alternatively, after the code template is obtained by code restoration of the target subgraph according to the code file, the useless logic code of the code template can be cleaned up. The useless logic code of the code template is cleaned up by deleting the code without actual meaning in the code template.
[0108] The missing call chain of the code template and the useless logic code of the code template can be supplemented and cleaned up by using the control flow graph.
[0109] Figure 8 is a structural schematic diagram of an electronic device disclosed by an embodiment of the present application. As shown in Figure 8 , the electronic device 80 can include: a radio frequency (RF) circuit 801, a memory 802, an input unit 803, a display unit 804, a sensor 805, an audio circuit 806, a wireless fidelity (Wi-Fi) module 807, a processor 808, and a power supply 809, and the like. Those skilled in the art can understand that the structure shown in Figure 8 does not constitute a limitation on the electronic device, and can include more or fewer components than the diagram, or combine certain components, or different component arrangements.
[0110] The RF circuit 801 can be used to transceive information or receive and send signals during a call. In particular, after receiving the downlink information of the base station, it is transferred to the processor 808 for processing. In addition, data related to the uplink is sent to the base station. Generally, the RF circuit 801 includes, but is not limited to: an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (LNA), a duplexer, and the like.
[0111] The memory 802 can be used to store software programs and modules, and the processor 808 can execute various functions of the electronic device and data processing by running the software programs and modules stored in the memory 802. The memory 802 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, at least one application program required by a function (such as a sound playing function, an image playing function, etc.), and the like; the data storage area can store data created according to the use of the electronic device (such as audio data, a phone book, etc.), and the like. In addition, the memory 802 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other volatile solid-state storage device.
[0112] The input unit 803 can be used to receive inputted digital or character information, and to generate key signal input related to user settings of the electronic device and function control. Specifically, the input unit 803 can include a touch panel 8031 and other input devices 8032. The touch panel 8031, also called a touch screen, can collect a user's touch operation (such as a user's operation on or near the touch panel 8031 using a finger, a stylus, or any suitable object or accessory) and drive a corresponding connection device according to a pre-set program. Alternatively, the touch panel 8031 can include two parts, a touch detection device and a touch controller. The touch detection device detects the user's touch position and detects a signal caused by the touch operation, and transmits the signal to the touch controller; the touch controller receives the touch information from the touch detection device, converts it into touch coordinates, and sends it to the processor 808, and receives the commands from the processor 808 and executes them. In addition, the touch panel 8031 can be implemented in various types such as a resistive type, a capacitive type, an infrared type, and a surface acoustic wave type. In addition to the touch panel 8031, the input unit 803 can also include other input devices 8032. Specifically, the other input devices 8032 can include one or more of a physical keyboard, function keys (such as volume control keys, on / off keys, etc.), a trackball, a mouse, a joystick, etc.
[0113] The display unit 804 can be used to display information input by a user or information provided to a user, as well as various menus of the electronic device. The display unit 804 can include a display panel 8041, which can be configured in the form of a Liquid Crystal Display (LCD), an Organic Light-Emitting Diode (OLED), etc. Further, the touch panel 8031 can cover the display panel 8041, and when the touch panel 8031 detects a touch operation on or near it, it transmits to the processor 808 to determine the type of touch event, and then the processor 808 provides a corresponding visual output on the display panel 8041 according to the type of touch event. Although in the above embodiment, the touch panel 8031 and the display panel 8041 are implemented as two independent components to realize the input and output functions of the electronic device, in some embodiments, the touch panel 8031 and the display panel 8041 can be integrated to realize the input and output functions of the electronic device. Figure 8
[0114] The electronic device can further include at least one sensor 805, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor can include an ambient light sensor that can adjust the brightness of the display panel 8041 according to the brightness of ambient light, and a proximity sensor that can turn off the display panel 8041 and / or the backlight when the electronic device is moved to the ear. As one of the motion sensors, the accelerometer sensor can detect the magnitude of acceleration in each direction (generally three axes), and when at rest, can detect the magnitude and direction of gravity, and can be used for applications that identify the posture of the electronic device (such as landscape / portrait switching, related games, magnetometer posture calibration), vibration recognition related functions (such as pedometer, tapping), and the like. In addition, the electronic device can also be configured with a gyroscope, a barometer, a hygrometer, a thermometer, an infrared sensor, and other sensors, which will not be described here.
[0115] The audio circuit 806, the speaker 8061, and the microphone 8062 can provide an audio interface between the user and the electronic device. The audio circuit 806 can convert the received audio data into an electrical signal, transmit the electrical signal to the speaker 8061, and convert the electrical signal into a sound signal output by the speaker 8061. On the other hand, the microphone 8062 converts the collected sound signal into an electrical signal, which is received by the audio circuit 806 and converted into audio data. The audio data is output to the processor 808 for processing, and then transmitted to another electronic device via the RF circuit 801, or output to the memory 802 for further processing.
[0116] Wi-Fi belongs to a short-range wireless transmission technology. The electronic device can help the user to send and receive emails, browse web pages, and access streaming media through the Wi-Fi module 807, which provides the user with wireless broadband Internet access. Although Figure 8 The Wi-Fi module 807 is shown, but it can be understood that it does not belong to the essential components of the electronic device, and can be omitted as needed without changing the essence of the application.
[0117] The processor 808 is the control center of the electronic device, which connects all parts of the electronic device through various interfaces and lines, executes various functions of the electronic device and processes data by running or executing software programs and / or modules stored in the memory 802 and calling data stored in the memory 802, thereby monitoring the entire electronic device. Optionally, the processor 808 can include one or more processing units; preferably, the processor 808 can integrate an application processor and a modem, wherein the application processor mainly processes the operating system, user interface, and application program, and the modem mainly processes wireless communication. It can be understood that the above-mentioned modem processor can also be integrated into the processor 808.
[0118] The electronic device further includes a power supply 809 (such as a battery) that supplies power to each component. The power supply can be logically connected to the processor 808 through a power management system, so that the power management system can manage charging, discharging, and power consumption management, and the like.
[0119] Although not shown, the electronic device can also include a camera, a Bluetooth module, and the like, which will not be described here.
[0120] Figure 8 The electronic device described in the above can be used to implement the code template extraction method of the present application Figure 2 Some or all of the processes in the method embodiments introduced above can refer to the foregoing Figure 2 The related descriptions in the embodiments are not repeated here.
[0121] The embodiment also provides a computer storage medium, which stores computer instructions, when the computer instructions are executed on the electronic device, the electronic device executes the related method steps to implement the code template extraction method in the above embodiments.
[0122] The embodiment also provides a computer program product, when the computer program product is executed on the electronic device, the electronic device executes the related steps to implement the code template extraction method in the above embodiments.
[0123] In addition, the embodiment of the present application also provides a device, which can be a chip, a component or a module. The device can include a processor and a memory connected to each other. The memory is used to store computer execution instructions. When the device is running, the processor can execute the computer execution instructions stored in the memory to make the chip execute the code template extraction method in the above method embodiments.
[0124] The electronic device, computer storage medium, computer program product or chip provided by the embodiment are used to execute the corresponding method provided above, so the beneficial effects that can be achieved are referred to the beneficial effects of the corresponding method provided above, which will not be repeated here.
[0125] Through the description of the above embodiments, those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above functional modules is taken as an example for illustration. In actual application, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above.
[0126] In several embodiments provided in the present application, it should be understood that the disclosed apparatus and method can be implemented in other manners. For example, the described apparatus embodiments are merely schematic. The division of the modules or units is merely logical function division. There can be another division manner in actual implementation. For example, a plurality of units or components can be combined or integrated into another apparatus, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections can be indirect couplings or communication connections through some interfaces, apparatuses or units, and can be in electrical, mechanical or other forms.
[0127] The units described as separate components can or can not be physically separate, and the components shown as units can be one physical unit or multiple physical units, i.e., can be located in one place or distributed in multiple different places. Some or all of the units can be selected according to actual needs to achieve the purpose of the embodiments.
[0128] In addition, each functional unit in the embodiments of the present application can be integrated in one processing unit, or each unit can exist physically, or two or more units can be integrated in one unit. The integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
[0129] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of the present application essentially or in other words, the part that makes contributions to the prior art or the whole or part of the technical solutions can be embodied in the form of a software product. The software product is stored in a storage medium, and includes a number of instructions for causing an apparatus (which can be a single-chip microcomputer, a chip, etc.) or a processor to perform all or part of the steps of the methods described in the embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes.
[0130] The above description is merely a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or replacements within the technical scope disclosed in the present application should be covered in the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. A code template extraction method characterized by, The method comprises: acquiring a plurality of software projects; generating a graph structure for each software project, the graph structure showing the syntax structure and method call relationship of code files contained in the software project; calculating the maximum common subgraph between each two graph structures; randomly traversing each two maximum common subgraphs; counting the frequency of each node being accessed in each two maximum common subgraphs; calculating the matching degree of the frequency of each node being accessed in each two maximum common subgraphs, taking the matching degree as the similarity between each two maximum common subgraphs; clustering the maximum common subgraphs according to the similarity between each two maximum common subgraphs to obtain at least one similar subgraph group; for each target similar subgraph group in the at least one similar subgraph group, generating a code template according to the code corresponding to each node and the method call relationship corresponding to each edge of the target subgraph in the target similar subgraph group.
2. The code template extraction method of claim 1, wherein, The generating a graph structure for each software project comprises: performing syntax analysis on each code file of the software project to obtain an abstract syntax tree of each code file, each class defined in the code file corresponding to an abstract syntax tree; performing code dependency analysis on each code file of the software project to obtain the method call relationship of each code file; combining the abstract syntax tree and the method call relationship of each code file of the software project to obtain the graph structure corresponding to the software project.
3. The code template extraction method of claim 1, wherein, The calculating the maximum common subgraph between each two graph structures comprises: using a maximum clique-based maximum common subgraph algorithm to calculate the maximum common subgraph between each two graph structures.
4. The code template extraction method of claim 3, wherein, The calculating the maximum common subgraph between each two graph structures comprises: fuzzing the classes, methods and variables contained in all nodes of the two graph structures; generating an association graph of the two graph structures, each node of the association graph being a node combination of the two graph structures, and each edge of the association graph corresponding to an edge that exists simultaneously or does not exist simultaneously in the two graph structures; solving the maximum clique of the association graph to obtain the maximum common subgraph.
5. The code template extraction method of claim 4, wherein, The solving the maximum clique of the association graph comprises: solving the maximum clique of the association graph by a tabu search algorithm.
6. The code template extraction method of claim 1, wherein, The method further comprises: normalizing the similarity between each two maximum common subgraphs; composing the similarity between each two maximum common subgraphs after normalization into a similarity matrix.
7. The code template extraction method of claim 1, wherein, The clustering the maximum common subgraphs according to the similarity between each two maximum common subgraphs comprises: performing hierarchical clustering on the maximum common subgraphs according to the similarity between each two maximum common subgraphs.
8. The code template extraction method of claim 1, wherein, The number of maximum common subgraphs of the target similar subgraph group is greater than or equal to a first preset number.
9. The code template extraction method of claim 1, wherein, The number of target similar subgraph groups is less than or equal to a second preset number.
10. The code template extraction method according to any one of claims 1 to 9, wherein, Before the generating a code template according to the code corresponding to each node and the method call relationship corresponding to each edge of the target subgraph in the target similar subgraph group, the method further comprises: Calculate the sum of squares of distances of each maximal common subgraph in the target similar subgraph group from other maximal common subgraphs, and take the maximal common subgraph with the minimum sum of squares of distances as the target subgraph; or Calculate the average distance of each maximal common subgraph in the target similar subgraph group from other maximal common subgraphs, and take the maximal common subgraph with the minimum average distance as the target subgraph.
11. The code template extraction method according to any one of claims 1 to 9, wherein, After generating the code template according to the method call relationship between each node corresponding to the target subgraph in the target similar subgraph group and each edge, the method further comprises: Supplement the missing call chain of the code template; and / or Clean up the useless logic code of the code template.
12. The code template extraction method of claim 11, wherein, Supplement the missing call chain of the code template; and / or Clean up the useless logic code of the code template includes: The general control flow graph supplements the missing call chain of the code template; and / or The general control flow graph cleans up the useless logic code of the code template. The computer program product comprises computer instructions, which, when executed on an electronic device, cause the electronic device to perform the code template extraction method according to any one of claims 1 to 12.
13. A computer-readable storage medium, characterized in that, The electronic device comprises a processor and a memory, wherein the memory is configured to store instructions, and the processor is configured to invoke the instructions in the memory, so that the electronic device performs the code template extraction method according to any one of claims 1 to 12.
14. An electronic device, comprising: The chip system is applied to an electronic device; the chip system comprises an interface circuit and a processor; the interface circuit and the processor are interconnected through a circuit; the interface circuit is configured to receive a signal from a memory of the electronic device and send a signal to the processor, and the signal comprises computer instructions stored in the memory; when the processor executes the computer instructions, the chip system performs the code template extraction method according to any one of claims 1 to 12.
15. A chip system, characterized by