A semi-automatic software system refactoring method based on class-level networks

Through the semi-automatic reconstruction method based on class-level networks, the problems of code smell and insufficient developer participation at the software system architecture level are solved, and the flexibility and sustainability of the software system are improved and the controllability of the reconstruction process is achieved.

CN114371871BActive Publication Date: 2024-08-30ZHEJIANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210046329.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-01-10
Publication Date
2024-08-30
Estimated Expiration
2042-01-10

AI Technical Summary

Technical Problem

Existing technologies cannot effectively solve the code smell problem at the software system architecture level, and the automated reconstruction method lacks developer participation, resulting in reconstruction uncertainty and the inability to meet the flexibility and sustainability needs of large software systems.

Method used

A semi-automatic reconstruction method based on class-level networks is adopted. By establishing a class-level network structure, calculating network parameter values, using the NSGA-II multi-objective genetic algorithm to generate reconstruction operation suggestions, and sorting through local search algorithms, developer feedback is introduced. Allow developers to choose and adjust refactoring actions until code smells are eliminated.

Benefits of technology

It improves the flexibility and sustainability of the software system at the architectural level, enhances developers' control and autonomy over the reconstruction process, reduces reconstruction uncertainty, and improves the practicality of the reconstruction method.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114371871B
    Figure CN114371871B
Patent Text Reader

Abstract

The present invention discloses a semi-automatic reconstruction method of a software system based on a class-level network. The method introduces the feedback of developers into the reconstruction implementation process. The generation of reconstruction operations is fully automated, and the developer decides whether to execute the reconstruction operation. Therefore, the developer only needs to pay attention to the rationality of the reconstruction operation, which can shorten the reconstruction implementation time and increase the developer's trust in the reconstruction process. In addition, the present invention focuses on the class-level network formed by the entire software system. The present invention can improve the flexibility and sustainability of the original software system from the class-level level.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of software engineering, and in particular relates to a semi-automatic reconstruction method of a software system based on a class-level network. Background Art

[0002] Refactoring refers to improving the quality and performance of software by adjusting program code without changing the external behavior of the code, so as to make its design pattern and architecture more reasonable. High-quality software systems are very important for enterprises in today's era, and refactoring is an important means to improve the quality of software systems. For developers, determining the refactoring location and refactoring method has always been a difficult problem. In order to solve the above problems, researchers have proposed many automated refactoring methods, which can automatically detect the parts of the software system that need to be refactored and generate corresponding refactoring methods. In the long-term development and maintenance process, the architecture of the software system will inevitably change continuously due to new requirements. These changes may cause incomprehensible, complex, and problematic structures in the software system, which are called code smells. An important direction of current refactoring research is to detect the parts of the software system with code smells through specific algorithms, and then use corresponding means to refactor this part of the code to eliminate code smells, so as to achieve the purpose of improving the flexibility and sustainability of the software system.

[0003] The paper [A fuzzy genetic automatic refactoring approach to improve software maintainability and flexibility [J]. Raana Saheb Nasagh, Mahnoosh Shahidi, Mehrdad Ashtiani. Soft Comput. 25(6): 4295-4325 (2021)] proposes a method that uses a fuzzy genetic algorithm to detect code smells and automatically implement refactoring. It can detect possible code smells at the software system architecture level and automatically perform corresponding refactoring operations; however, this method is fully automated, and developers cannot participate in the refactoring implementation process, which increases the uncertainty of software system refactoring, making this fully automatic refactoring method unable to become the preferred method for developers to implement refactoring. In addition, the document [Recommendation System for Software Refactoring Using Innovation and Interactive Dynamic Optimization [C]. Mohamed Wiem Mkaouer, Marouane Kessentini, Slim Bechikh, Kalyanmoy Deb, Meló Cinnéide. ASE 2014: 331-336] proposed a software refactoring recommendation system based on innovation and interactive dynamic optimization. The system determines the refactoring operation by detecting the possible code smells at the software system code level, and introduces the feedback of developers, which can better solve the problem of uncertainty for developers in automated refactoring. However, the system focuses on the code level in terms of code smell detection, and does not pay attention to the detection of code smells at the system architecture level. For smaller software systems, this method is effective and feasible; but for large software systems that focus on the synergy and stability between modules, this method cannot meet the needs of software development itself.The document [JDeodorant: Identification and Application of Extract Class Refactorings [C]. Marios Fokaefs, Nikolaos Tsantalis, Eleni Stroulia, Alexander Chatzigeorgiou. ICSE 2011: 1037-1039] proposed a plug-in tool called JDeodorant, which provides an automated refactoring solution for four different code smells. It locates code smells by calculating the distance metric between classes and generates a variety of corresponding refactoring operations for developers to choose from by searching the refactoring operation space. However, the tool only focuses on improving the cohesion of the software system and reducing the coupling of the software system, and fails to take into account more attributes in the software system.

[0004] Therefore, the existing technology requires a refactoring method that is oriented to the software architecture level, focuses on multiple attributes of the software system, and allows developers to independently choose refactoring operations to meet the software system's requirements for flexibility and sustainability at the architecture level, as well as the developers' requirements for controllable refactoring process. Summary of the invention

[0005] In view of the above, the present invention provides a semi-automatic reconstruction method of a software system based on a class-level network, which can improve the flexibility and sustainability of the software system at the architectural level, and give developers more autonomy during the reconstruction process to enhance the practicality of the reconstruction method.

[0006] A semi-automatic refactoring method for a software system based on a class-level network comprises the following steps:

[0007] (1) For the software system to be reconstructed, establish its corresponding class-level network structure;

[0008] (2) calculating the network parameter value of each node in the class-level network structure;

[0009] (3) Determine and locate nodes with code smell in the network structure based on network parameter values ​​and add them to the set of nodes to be reconstructed;

[0010] (4) For nodes with code smell, the NSGA-II multi-objective genetic algorithm is used to generate multiple sets of refactoring operation suggestion sequences;

[0011] (5) All refactoring suggestions are sorted using a local search algorithm and provided to developers. Developers provide feedback by choosing to accept, reject, or adjust these refactoring suggestions. The software system is then adjusted and refactored based on the developer's feedback information. The feedback information is also introduced into the refactoring suggestion sorting process for the next iteration.

[0012] (6) Repeat steps (1) to (5) until there are no nodes with code smell in the software system.

[0013] Furthermore, the specific implementation method of step (1) is: converting the entire software system into a class-level network structure similar to a directed graph, wherein each class in the software system corresponds to each node in the network structure, and the relationship between classes corresponds to the edge between two nodes. The direction of the edge indicates the direction of the relationship between the classes (for example, if class A calls a method in class B, the direction of the edge is from the node formed by class A to the node formed by class B), and the weight of the edge reflects the closeness of the relationship between the two nodes connected by the edge. The relationship between classes includes inheritance, realization, combination, aggregation, association, and dependency.

[0014] Furthermore, the weight calculation expression of the edge in the class-level network structure is as follows:

[0015]

[0016] Where: e represents any edge in the class-level network structure, x and y represent the two nodes connected by edge e, W(e) is the weight of edge e, R i (x, y) is the weight value of the i-th relationship between nodes x and y, and N is the number of relationships between nodes x and y; when the i-th relationship is an inheritance or realization relationship, R i (x, y) takes the value of 1; when the i-th relationship belongs to a combination relationship, R i (x, y) takes the value of 0.8; when the i-th relationship belongs to an aggregation or association relationship, R i (x, y) takes the value of 0.5; when the i-th relationship is a dependent relationship, R i The value of (x,y) is 0.2.

[0017] Furthermore, the network parameter values ​​of the nodes include medium centrality, closeness centrality, in-degree and out-degree.

[0018] Furthermore, the specific implementation of step (3) is as follows: first, the average value of each network parameter value is calculated. For any node in the network structure, if the deviation ε between any network parameter value of the node and its average value is greater than 10% of the average value, the node is determined to have code smell, and the node is added to the set of nodes to be reconstructed, and then the degree of deviation of the network parameter value of the node is further determined:

[0019] When the deviation ε is greater than 10% and less than 12% of the mean, the degree of deviation is very low;

[0020] When the deviation ε is greater than or equal to 12% and less than 15% of the mean, the degree of deviation is low;

[0021] When the deviation ε is greater than or equal to 15% and less than 18% of the mean, the degree of deviation is medium;

[0022] When the deviation ε is greater than or equal to 18% and less than 20% of the mean, the degree of deviation is high;

[0023] When the deviation ε is greater than or equal to 20% of the mean value, the degree of deviation is very high.

[0024] Furthermore, the NSGA-II multi-objective genetic algorithm used in the step (4) takes the class-level network structure, the set of nodes to be reconstructed and all network parameter values ​​of each node as input, takes reducing the deviation of the network parameter values ​​of the nodes to be reconstructed as the objective function, and outputs multiple groups of reconstruction operation suggestion sequences that meet the Pareto front solution, each group of sequences contains multiple reconstruction operation suggestions.

[0025] Furthermore, the NSGA-II multi-objective genetic algorithm initializes a population of a certain size, and the individuals in the population are a rule in the form of "IF condition 1 AND condition 2 THEN code smell type and degree" and its corresponding reconstruction operation suggestion, and condition 1 and condition 2 are the deviation degrees of the node network parameter values; the crossover operation in the genetic algorithm is: randomly combining the individuals in the population in pairs, and for the two combined individuals, exchanging the code smell type and degree after THEN in the rules of the two individuals; the mutation operation in the genetic algorithm is: changing AND in the individual rule to OR, or changing OR to AND.

[0026] Furthermore, the local search algorithm calculates the score of each reconstruction operation suggestion by the following formula, and sorts all reconstruction operation suggestions according to the scores;

[0027]

[0028] Where: R i,j represents the i-th reconstruction operation suggestion in the j-th reconstruction operation suggestion sequence, Score(Ri,j ) Suggest R for the refactoring operation i,j The score of number_occurence is the refactoring operation suggestion R i,j The number of occurrences in the previous iteration, max_number_occurence is the maximum number of occurrences of the refactoring operation suggestion in the previous iteration, and recommended_ref is the number of occurrences of the refactoring operation suggestion R i,j The recommended factor.

[0029] Furthermore, the determination criteria of the recommended coefficient recommended_ref are: if the reconstruction operation recommends R i,j If the refactoring operation is accepted by the developer in the previous iteration, recommended_ref is set to 1; if the refactoring operation is recommended by R i,j If there is no corresponding feedback information or the developer chooses to reject it during the previous iteration, recommended_ref is set to 0; if the refactoring operation recommends R i,j In the previous iteration, the developer chooses to make adjustments. The software system will adjust and refactor based on the semantic similarity between the previously accepted refactoring operations and code elements and feedback to the developer. Similarly, the developer can choose to accept, reject or continue to adjust. At this time, set recommended_ref to 0.5 k , k is the developer's suggestion R for the refactoring operation in the previous iteration i,j The number of adjustments.

[0030] The present invention introduces the developer's feedback into the reconstruction implementation process. The generation of the reconstruction operation is fully automated, and the developer decides whether to execute the reconstruction operation. Therefore, the developer only needs to pay attention to the rationality of the reconstruction operation, which can not only shorten the reconstruction implementation time, but also increase the developer's trust in the reconstruction process. In addition, the present invention focuses on the class-level network formed by the entire software system. The present invention can improve the flexibility and sustainability of the original software system from the class-level level. BRIEF DESCRIPTION OF THE DRAWINGS

[0031] Figure 1 It is a flowchart of the semi-automatic reconstruction method of the software system of the present invention.

[0032] Figure 2 A schematic diagram of converting a software system into a class-level network structure.

[0033] Figure 3 Schematic diagram of the crossover operation of the NSGA-II algorithm.

[0034] Figure 4 Schematic diagram of the mutation operation of the NSGA-II algorithm. DETAILED DESCRIPTION

[0035] In order to describe the present invention more specifically, the technical solution of the present invention is described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0036] like Figure 1 As shown, the semi-automatic reconstruction method of the software system based on the class-level network of the present invention comprises the following steps:

[0037] Step 1: Generate the class-level network structure of the reconstructed system.

[0038] Use relevant tools (such as Visual Paradigm) to transform the entire refactored system into a Figure 2 In the directed graph shown in the figure, each class in the system is a node in the network, and the relationship between classes (including inheritance, implementation, combination, aggregation, association, and dependency) is an edge between two nodes. The direction of the edge indicates the direction of the relationship between the classes. For example, if class A calls a method in class B, the direction of the edge is from the node formed by class A to the node formed by class B. The weight of the edge reflects the closeness of the connection between the two nodes connected by the edge, and the weight of the edge is calculated according to the following formula:

[0039]

[0040] Where: N is the number of relationships between x and y, R i (x,y) is the weight of the i-th relationship between x and y; when the i-th relationship is inheritance and implementation, R i (x, y) takes the value 1 first. When the i-th relationship is a combination, R i (x, y) takes a value of 0.8 first. When the i-th relationship is an aggregation and association, R i (x, y) takes the value of 0.5 first. When the i-th relationship is dependent, R i The preferred value of (x,y) is 0.2.

[0041] Step 2: Calculate the network parameter values ​​of each node in the network.

[0042] The medium centrality (Betweennesscentrality) of all nodes in the network structure is calculated according to the following formula:

[0043]

[0044] Where: C b(z) represents the medium centrality of node z, V represents the set of all nodes in the network structure G, σ(x,y) represents the total number of shortest paths connecting nodes x and y, and σ(x,y,z) represents the total number of shortest paths connecting nodes x and y and passing through node z.

[0045] The closeness centrality of all nodes in the network structure is calculated according to the following formula:

[0046]

[0047] Where: d(v,u) represents the shortest distance from node v to node u, and n represents the number of summary points in the network structure G.

[0048] Finally, we calculate the in-degree and out-degree of each node respectively. The value of in-degree centrality is the total number of edges whose end point is the node, and the value of out-degree centrality is the total number of edges whose start point is the node.

[0049] Step 3: Compare the calculated network parameters of each node to determine which node and parameter value has an abnormality and locate the code smell.

[0050] First, calculate the average value of the four network parameters in the entire network structure, then traverse each node in the network structure in turn, calculate the deviation between the network parameter value of the node and the average value, and judge whether there is code smell and the degree of deviation based on the degree of deviation:

[0051] When the deviation is less than 10%, there is no code smell;

[0052] When the deviation is greater than or equal to 10% and less than 12%, the degree of deviation is very low;

[0053] When the deviation is greater than or equal to 12% and less than 15%, the degree of deviation is low;

[0054] When the deviation is greater than or equal to 15% and less than 18%, the degree of deviation is moderate;

[0055] When the deviation is greater than or equal to 18% and less than 20%, the degree of deviation is high;

[0056] When the deviation is 20% or more, the degree of deviation is very high.

[0057] And all nodes with more than 10% network parameter deviation are added to the set of nodes to be reconstructed.

[0058] Step 4: Use the NSGA-II multi-objective genetic algorithm to generate a series of reconstruction operation suggestions, and use the local search algorithm to rank these suggestions.

[0059] The network structure generated in step 1 and the set of nodes to be reconstructed and their network parameter values ​​are used as the input of the algorithm. Reducing the deviation of the network parameter values ​​of the nodes to be reconstructed is used as the objective function of the algorithm. The rule set of the form "IF closenesscentrality is High AND in-degree is Very High THEN god class existencepotential is High" and its corresponding reconstruction operation are used as the gene of the NSGA-II algorithm. The initial population size of the NSGA-II algorithm is set to 100, the number of iterations is 100, the crossover probability is 0.9, and the mutation probability is 0.1.

[0060] The crossover operation of the NSGA-II algorithm is: randomly select two parent rules and exchange the statements after "THEN" in the parent rule statements, such as Figure 3 The mutation operation is: randomly select a rule and change the "AND" in the parent rule to "OR", or change "OR" to "AND", such as Figure 4 shown.

[0061] The NSGA-II algorithm outputs multiple sets of rules that satisfy the Pareto front solution and the corresponding reconstruction operations, as shown in Table 1. The following formula is then used to calculate the scores of these reconstruction operations and sort them from high to low:

[0062]

[0063] Among them: Score (R i,j ) is the score of the i-th reconstruction operation in the j-th reconstruction operation sequence, and the first term is the score of the reconstruction operation R in all Pareto front solutions. i,j The second item is to count the number of occurrences of the refactoring operation and normalize it to 0 to 1; the second item is to count the developer's feedback in the previous cycle (i.e., choosing to accept, reject or adjust the refactoring operation R i,j ), recommended_ref is about R i,j The recommended factor.

[0064] Table 1

[0065]

[0066]

[0067]

[0068] Step 5: The developer chooses to accept, reject or adjust these refactoring suggestions. The refactoring system adjusts the refactored system based on the developer's choice and introduces the developer's feedback into the next round of sorting.

[0069] When the developer chooses to accept, the system will automatically execute the refactoring operation and adjust the recommended_ref of the refactoring operation to 1; when the developer chooses to reject, the system will skip the refactoring operation and adjust the recommended_ref of the refactoring operation to 0; when the developer chooses to adjust, the system will modify the refactoring based on the semantic similarity between the refactoring operations accepted in the past and the code elements and provide it to the developer. Similarly, the developer can choose to accept, reject or continue to modify. At this time, the recommended_ref of the refactoring operation is adjusted to 0.5 to the power of i, where i is the number of modifications.

[0070] Step 6: Repeat steps 1 to 5 until the refactored system has no code smell or the developer actively terminates the refactoring.

[0071] The above description of the embodiments is to facilitate the understanding and application of the present invention by those skilled in the art. It is obvious that those skilled in the art can easily make various modifications to the above embodiments and apply the general principles described herein to other embodiments without creative work. Therefore, the present invention is not limited to the above embodiments. Improvements and modifications made by those skilled in the art to the present invention based on the disclosure of the present invention should be within the protection scope of the present invention.

Claims

1. A semi-automatic software system reconstruction method based on class-level network, comprising the following steps: (1) For the software system to be reconstructed, establish its corresponding class-level network structure. The specific implementation method is: convert the entire software system into a directed graph class-level network structure. Each class in the software system corresponds to each node in the network structure. The relationship between classes corresponds to the edge between two nodes. The direction of the edge indicates the direction of the relationship between classes. The weight of the edge reflects the closeness of the relationship between the two nodes connected by the edge. The relationship between classes includes inheritance, realization, combination, aggregation, association, and dependency. The edge weight calculation expression is as follows: in: e represents any edge in the class-level network structure, x and y represent the two nodes connected by edge e, W(e) is the weight of edge e, R i (x, y) is the weight value of the i-th relationship between nodes x and y, and N is the number of relationships between nodes x and y; when the i-th relationship is an inheritance or realization relationship, R i (x, y) takes the value of 1; when the i-th relationship belongs to a combination relationship, R i (x, y) takes the value of 0.8; when the i-th relationship belongs to an aggregation or association relationship, R i (x, y) takes the value of 0.5; when the i-th relationship is a dependent relationship, R i (x,y) takes the value of 0.2; (2) calculating the network parameter values ​​of each node in the class-level network structure, including medium centrality, closeness centrality, in-degree, and out-degree; (3) According to the network parameter values, nodes with code smell in the localized network structure are judged and added to the set of nodes to be reconstructed. The specific implementation method is as follows: first, the average value of each network parameter value is calculated. For any node in the network structure, if the deviation ε between any network parameter value of the node and its average value is greater than 10% of the average value, the node is judged to have code smell and added to the set of nodes to be reconstructed. Then, the deviation degree of the network parameter value of the node is further judged: When the deviation ε is greater than 10% and less than 12% of the mean, the degree of deviation is very low; When the deviation ε is greater than or equal to 12% and less than 15% of the mean, the degree of deviation is low; When the deviation ε is greater than or equal to 15% and less than 18% of the mean, the degree of deviation is medium; When the deviation ε is greater than or equal to 18% and less than 20% of the mean, the degree of deviation is high; When the deviation ε is greater than or equal to 20% of the mean, the degree of deviation is very high; (4) For nodes with code smell, the NSGA-II multi-objective genetic algorithm is used to generate multiple sets of refactoring operation suggestion sequences; (5) All refactoring suggestions are sorted using a local search algorithm and provided to developers. Developers provide feedback by choosing to accept, reject, or adjust these refactoring suggestions. The software system is then adjusted and refactored based on the developer's feedback information. The feedback information is also introduced into the refactoring suggestion sorting process for the next iteration. The local search algorithm calculates the score of each reconstruction operation suggestion by the following formula, and sorts all reconstruction operation suggestions according to the score; Where: R i,j represents the i-th reconstruction operation suggestion in the j-th reconstruction operation suggestion sequence, Score(R i,j ) Suggest R for the refactoring operation i,j The score of number_occurence is the refactoring operation suggestion R i,j The number of occurrences in the previous iteration, max_number_occurence is the maximum number of occurrences of the refactoring operation suggestion in the previous iteration, and recommended_ref is the number of occurrences of the refactoring operation suggestion R i,j The recommendation factor of The determination standard of the recommended coefficient recommended_ref is: if the reconstruction operation recommends R i,j If the refactoring operation is accepted by the developer in the previous iteration, recommended_ref is set to 1; if the refactoring operation is recommended by R i,j If there is no corresponding feedback information or the developer chooses to reject it during the previous iteration, recommended_ref is set to 0; if the refactoring operation recommends R i,j In the previous iteration, the developer chooses to make adjustments. The software system will adjust and refactor based on the semantic similarity between the previously accepted refactoring operations and code elements and feedback to the developer. Similarly, the developer can choose to accept, reject or continue to adjust. At this time, set recommended_ref to 0.5 k , k is the developer's suggestion R for the refactoring operation in the previous iteration i,j The number of adjustments; (6) Repeat steps (1) to (5) until there are no nodes with code smell in the software system.

2. The method for semi-automatic reconstruction of a software system according to claim 1, characterized in that: The NSGA-II multi-objective genetic algorithm used in the step (4) takes the class-level network structure, the set of nodes to be reconstructed, and all network parameter values ​​of each node as input, takes reducing the deviation of the network parameter values ​​of the nodes to be reconstructed as the objective function, and outputs multiple groups of reconstruction operation suggestion sequences that meet the Pareto front solution, each group of sequences contains multiple reconstruction operation suggestions.

3. The method for semi-automatic reconstruction of a software system according to claim 2, characterized in that: The NSGA-II multi-objective genetic algorithm initializes a population of a certain size, and an individual in the population is a rule in the form of "IF condition 1 AND condition 2 THEN code smell type and degree" and its corresponding reconstruction operation suggestion, and condition 1 and condition 2 are the deviation degrees of node network parameter values; the crossover operation in the genetic algorithm is: randomly combine individuals in the population in pairs, and for the two combined individuals, exchange the code smell type and degree after THEN in the rules of the two individuals; the mutation operation in the genetic algorithm is: change AND in the individual rule to OR, or change OR to AND.

Citation Information

Patent Citations

  • Defect data set construction and processing method based on software network

    CN110147322A

  • Code smell detection method based on residual network and measurement attention mechanism

    CN113656279A