Code generation method and device, equipment, storage medium and program product

By using a multi-objective genetic algorithm and large-scale model optimization to design the initial class diagram of the model, the problem of low design quality in traditional model-driven engineering is solved, and the efficiency and quality of code generation are improved.

CN121143777BActive Publication Date: 2026-04-07CHINA ELECTRONICS RELIABILITY AND ENVIRONMENTAL TESTING INSTITUTE ((THE FIFTH INSTITUTE OF ELECTRONICS MINISTRY OF INDUSTRY AND INFORMATION TECHNOLOGY) (CHINA SAIBAO LABORATORY)
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-11-17
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

Traditional model-driven engineering suffers from low design quality, resulting in inefficient code generation and the need for repeated revisions.

Method used

A multi-objective genetic algorithm and a large model are used to optimize the initial class diagram of the design model, generating monolithic system code that matches the requirements model.

Benefits of technology

It improves code generation efficiency, makes the generated monolithic system code more closely match the requirement model, and improves code quality.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121143777B_ABST
    Figure CN121143777B_ABST
Patent Text Reader

Abstract

This application relates to a code generation method, apparatus, device, storage medium, and program product. The method includes: obtaining a requirement model; obtaining an initial class diagram of a design model based on the requirement model; optimizing the initial class diagram of the design model using a multi-objective genetic algorithm and a large model to obtain an optimized design model; and generating monolithic system code matching the requirement model based on the optimized design model. This application can improve code generation efficiency and code generation quality.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of software development, and particularly relates to a monomer system code generation method and device, equipment, a storage medium and a program product. BACKGROUND

[0002] Model-driven engineering can apply software model technology to development empowerment in the whole life cycle of software development, support model-driven capabilities in various stages such as requirement, design, development, testing and operation of basic software, and thus improve software development efficiency and software quality.

[0003] However, in the traditional model-driven engineering scheme, the design model generated based on the requirement model still has the problem of low design quality, which leads to the need of the developer to repeatedly correct the model, thereby affecting the efficiency of code generation. SUMMARY

[0004] Therefore, it is necessary to provide a code generation method, device, equipment, storage medium and program product capable of improving the efficiency of code generation.

[0005] In a first aspect, the present application provides a code generation method, which comprises:

[0006] obtaining a requirement model;

[0007] obtaining an initial class diagram of a design model based on the requirement model;

[0008] optimizing the initial class diagram of the design model by using a multi-objective genetic algorithm and a large model to obtain an optimized design model;

[0009] generating a monomer system code matching the requirement model based on the optimized design model.

[0010] In one of the embodiments, the initial class diagram of the design model is optimized by using the multi-objective genetic algorithm and the large model to obtain the optimized design model, which comprises:

[0011] optimizing the responsibility distribution of the initial class diagram by using the multi-objective genetic algorithm according to a design quality index to obtain the initial class diagram after optimization of the responsibility distribution;

[0012] optimizing the initial class diagram after optimization of the responsibility distribution by using the large model to obtain the optimized design model based on the anemia model architecture design.

[0013] In one of the embodiments, the responsibility distribution of the initial class diagram is optimized by using the multi-objective genetic algorithm according to the design quality index to obtain the initial class diagram after optimization of the responsibility distribution, which comprises:

[0014] The optimized design model is taken as an initial population of the multi-objective genetic algorithm, and a fitness function of the multi-objective genetic algorithm is determined according to a design quality index; the design quality index includes a coupling degree, a cohesion degree, a class size, and a method quantity;

[0015] Based on the initial population and the fitness function of the multi-objective genetic algorithm, a responsibility distribution of the initial class diagram is optimized by the multi-objective genetic algorithm to obtain an initial class diagram after optimization of the responsibility distribution.

[0016] In one of the embodiments, the initial class diagram after optimization of the responsibility distribution is designed and optimized by the large model to obtain an optimized design model based on the anemia model architecture design, including:

[0017] The initial class diagram after optimization of the responsibility distribution is converted into a text form and input into the large model to obtain a model input result; the model input result includes a class diagram rationality evaluation result, a class diagram understandability evaluation result, and a class interaction timing diagram.

[0018] Based on the initial class diagram after optimization of the responsibility distribution in the text form and the model input result, the large model is instructed to apply a design pattern to optimize class diagram design of the initial class diagram to obtain the optimized design model.

[0019] In one of the embodiments, based on the requirement model, an initial class diagram of the design model is obtained, including:

[0020] Based on a concept class diagram of the requirement model, entity classes and class relationships of the initial class diagram are determined;

[0021] According to a system sequence diagram and a use case diagram of the requirement model, service classes of the initial class diagram are generated;

[0022] Based on the entity classes of the initial class diagram, entity management classes of the initial class diagram are generated; the entity management classes have an association relationship with all the entity classes.

[0023] In one of the embodiments, based on the optimized design model, monolithic system code matching the requirement model is generated, including:

[0024] Based on the optimized design model, architecture design code of the monolithic system code is generated according to the Jakarta EE specification;

[0025] According to a business use case of the optimized design model, business logic code of the monolithic system code is generated;

[0026] Based on the optimized design model, data access code of the monolithic system code is generated according to an ORM framework.

[0027] In one of the embodiments, the method further includes:

[0028] The system operates a contract based on the requirement model to obtain a sequence diagram of the design model.

[0029] The sequence diagram is used to describe dynamic interaction behavior inside the monomer system.

[0030] In a second aspect, the present application further provides a code generation apparatus, the apparatus comprising:

[0031] a model obtaining module configured to obtain a requirement model;

[0032] an initial class diagram obtaining module configured to obtain an initial class diagram of a design model based on the requirement model;

[0033] an optimization module configured to optimize the initial class diagram of the design model by using a multi-objective genetic algorithm and a large model to obtain an optimized design model;

[0034] a code generation module configured to generate a monomer system code matching the requirement model based on the optimized design model.

[0035] In a third aspect, the present application further provides a computer device comprising a memory and a processor, the memory storing a computer program, and the processor implementing the steps of the method of any one of the embodiments of the first aspect when executing the computer program.

[0036] In a fourth aspect, the present application further provides a computer readable storage medium having a computer program stored thereon, and the computer program, when executed by a processor, implements the steps of the method of any one of the embodiments of the first aspect.

[0037] The code generation method, apparatus, device, storage medium and program product described above obtain a requirement model, and based on the requirement model, obtain an initial class diagram corresponding to a design model, and then optimize the initial class diagram by using a multi-objective genetic algorithm and a large model to obtain an optimized design model. Based on the optimized design model, a monomer system code matching the requirement model can be generated. In this way, the code generation efficiency can be improved, and the monomer system code generated is more in line with the requirement model. BRIEF DESCRIPTION OF DRAWINGS

[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related art, the drawings needed to be used in the description of the embodiments of the present application or the related art will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other related drawings can also be obtained without creative labor on the basis of these drawings.

[0039] Figure 1 An application environment diagram of the code generation method in an embodiment;

[0040] Figure 2 a flowchart of a code generation method in one embodiment;

[0041] Figure 3 a flowchart of a method for obtaining an optimized design model in one embodiment;

[0042] Figure 4 a flowchart of a method for obtaining an initial class diagram after optimization of responsibility distribution in one embodiment;

[0043] Figure 5 a flowchart of a method for obtaining an optimized design model in another embodiment;

[0044] Figure 6 a flowchart of a method for generating an initial class diagram in one embodiment;

[0045] Figure 7 a flowchart of a method for generating a monolithic system code in one embodiment;

[0046] Figure 8 a structural block diagram of a code generation apparatus in one embodiment;

[0047] Figure 9 an internal structure diagram of a computer device in one embodiment. DETAILED DESCRIPTION

[0048] In order to make the purpose, technical scheme and advantages of the present application clearer, the present application will be further described in detail below with reference to the drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and should not be used to limit the present application.

[0049] The terms "comprise" and "have" and any variations thereof used in the present application are intended to cover non-exclusive inclusion. The term "a plurality of" used in the present application refers to two or more. The term "and / or" used in the present application refers to one of the options or any combination of a plurality of options.

[0050] The code generation method provided by the embodiments of the present application can be applied to, for example, Figure 1The application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data required by the server 104 to process. The data storage system can be integrated on the server 104, or placed on the cloud or other network servers. Among them, the terminal 102 can be, but is not limited to, various personal computers, notebook computers, smart phones, tablet computers, unmanned aerial vehicles, low-altitude flying vehicles, Internet of Things devices, and portable wearable devices. The Internet of Things device can be a smart speaker, a smart TV, a smart air conditioner, a smart vehicle device, a projection device, etc. The portable wearable device can be a smart watch, a smart bracelet, a head-mounted device, etc. The head-mounted device can be a virtual reality (VR) device, an augmented reality (AR) device, smart glasses, etc. The server 104 can be a standalone physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.

[0051] In one embodiment, as Figure 2 shown, a code generation method is provided. The embodiment takes the method applied to the terminal 102 as an example. It can be understood that the method can also be applied to the server 104, and can also be applied to a system including the terminal 102 and the server 104, and implemented through the interaction of the terminal 102 and the server 104. In the embodiment, the method includes the following steps S202 to S208. Among them:

[0052] Step S202, obtaining a requirement model.

[0053] Exemplarily, the requirement model can include a use case diagram, a system sequence diagram, a system operation contract, and a conceptual class diagram. In some examples, the requirement model can be a UML / SysML model constructed according to development requirements.

[0054] Among them, the use case diagram of the requirement model can describe which interactions between the user and the system can be performed through the form of use case, so as to describe which valuable services the system can provide for the user, and which work the user can complete with the help of the system. It should be noted that the use case cannot actually be equivalent to the system operation, and in general, a use case is completed by multiple system operations to achieve the user's use target. Another important concept in the use case diagram is the actor, which can be used to describe the type of user in the system, or to describe the external system interacting with the system.

[0055] System sequence diagram can be used to describe the process of use case implementation, including which participants need to interact with the system, which system interaction events the participants need to initiate, and the order in which these events occur. It should be noted that unlike the sequence diagram in the design model, the system sequence diagram actually regards the system as a black box, only describes the input of the user to the system and the output of the system to the user, and does not describe the lifeline of the internal objects of the system and the interaction between the objects (which belongs to the category of describing the requirements of the system).

[0056] System operation contract can be used to describe a single system operation. The system operation contract can include the state of the system before the system operation is executed, i.e., the precondition; the system operation contract can also include the state of the system after the system operation is executed, i.e., the postcondition. In actual application, before the system operation is executed, the system state described in the precondition of the system operation contract should be checked, and after the condition is met, the corresponding system operation can be executed. In actual application, after the system operation is executed, the system state should be adjusted according to the postcondition of the system operation contract, and the adjustment mainly includes three types of adjustments: ① creating new objects (including initialization of object attributes and relationships), deleting existing objects; ② creating or removing the association relationship between objects; ③ modifying the attributes of existing objects.

[0057] Conceptual class diagram can be used to describe concepts in the problem domain and the relationship between concepts. It should be noted that in the conceptual class diagram, classes are used to represent concepts, the association relationship between classes is used to represent the relationship between concepts, and the attributes of classes are used to represent the properties of concepts. It can be understood that compared with the class diagram in the design model, the classes in the conceptual class diagram do not contain operations and do not describe how the system encapsulates operations.

[0058] In some examples, the system operation contract implemented by the present application can be written in object constraint language (OCL). Object constraint language OCL is a lightweight formal specification language that can be used to add constraints to UML (Unified Modeling Language) to solve the problem that constraints described in natural language are easy to be ambiguous. It can be understood that on the one hand, traditional formal languages often require developers to have a good mathematical background, and by using OCL which is easy to read and use, general business or system modeling personnel can also learn and implement the technical solutions of the present application; on the other hand, based on the requirement model, by using OCL and other formal languages conforming to first-order predicate logic to formalize the constraints on model elements, the high reliability requirement of safety-critical software can also be met.

[0059] Specifically, the terminal can obtain a requirements model that includes use case diagrams, system sequence diagrams, system operation contracts, and conceptual class diagrams.

[0060] Step S204: Based on the requirement model, obtain the initial class diagram of the design model.

[0061] The design model can include class diagrams and sequence diagrams. For example... Figure 5 As shown, class diagrams can be used to describe the static characteristics of a system, which can include classes, relationships between classes, and class attributes and operations. Sequence diagrams, on the other hand, can be used to describe the dynamic interactive behavior of a system, showing the order of events in the interaction between objects and the message passing.

[0062] Specifically, the terminal can determine the initial class diagram of the design model based on the requirement model.

[0063] For example, the terminal can determine the entity classes and class relationships of the design model to determine the initial class diagram based on the conceptual class diagram, system sequence diagram and use case diagram of the requirement model; it can obtain the sequence diagram of the design model based on the system operation contract of the requirement model; based on the initial class diagram and sequence diagram, the initial design model for subsequent optimization can be determined.

[0064] In some examples, for monolithic system code generation, the design model selects the monolithic architecture pattern. Considering the widespread application of the anemic domain model in practical software systems, especially in web system development, embodiments of this application can generate a design model that conforms to the anemic domain model architecture design based on the requirement model. Furthermore, the terminal can use the Spring Boot framework to quickly implement the anemic domain model architecture design, thereby efficiently obtaining usable code. It can be understood that, under the application of the model-driven development method, the design model of the anemic domain model architecture design can automatically generate usable Spring Boot code.

[0065] It's important to note that in actual monolithic system development using object-oriented methods, when employing the Spring Boot framework, the system architecture conforms to the anemic domain model. This involves creating a Controller layer to handle requests from external sources or the presentation layer. The Controller can then call the corresponding service class in the Service layer based on the request. The service classes in the Service layer encapsulate the business logic and process POJO (Plain Ordinary Java Object) objects as needed. A POJO is a Java object that does not inherit from any class, implement any interface, or have been intruded upon by other frameworks; it only contains its own properties and their getter and setter methods. Therefore, a POJO corresponds to the Domain layer in the anemic domain model, which is the most significant difference between the anemic and rich domain models. Furthermore, the Spring Boot framework also creates a Mapper layer to handle CRUD operations and persistence, which corresponds to the Repositories layer in the anemic domain model.

[0066] Step S206: Optimize the initial class diagram of the design model using a multi-objective genetic algorithm and a large model to obtain the optimized design model.

[0067] For example, the initial design model of the generated anemia model architecture can be used as the initial population, and then the fitness function can be defined with the class design quality index. The initial class graph of the design model can be optimized using a multi-objective genetic algorithm (MOGA).

[0068] In some examples, the class diagram of the design model can be converted into text form (e.g., using PlantUML) and input into the large model to evaluate whether the current design can achieve collaboration between classes, and to further optimize the class diagram using the optimization suggestions provided by the large model.

[0069] Specifically, the terminal can use multi-objective genetic algorithms and large models to optimize the initial class diagram of the design model, thereby obtaining an optimized design model.

[0070] Step S208: Based on the optimized design model, generate monolithic system code that matches the requirement model.

[0071] It should be noted that the optimized design model mainly includes the following aspects: system architecture design information, used to describe the overall system framework and the interaction between various modules; object collaboration information, used to describe the relationships and collaboration processes between various objects in the system; and DTO (Data Transfer Object), used to describe the definition and use of data transfer objects. By modeling various aspects of the system, such as architecture, objects, and data, the design model describes the design structure and operating mechanism of a system.

[0072] For example, the required monolithic system code is generated based on the system architecture design information, object collaboration information, and DTOs of the optimized design model.

[0073] Specifically, based on the optimized design model, the terminal can generate monolithic system code that matches the requirement model.

[0074] The code generation method described above can obtain a requirement model, determine an initial design model based on this model, and then optimize the initial class diagram of the design model using a multi-objective genetic algorithm and a large model to obtain an optimized design model. Based on the optimized design model, monolithic system code that matches the requirement model can be generated. Through this approach, this application can improve code generation efficiency and make the generated monolithic system code more closely aligned with the requirement model.

[0075] In one embodiment, such as Figure 3 As shown, the initial class graph of the design model is optimized using a multi-objective genetic algorithm and a large model to obtain the optimized design model, including the following steps S302 to S304. Wherein:

[0076] Step S302: Based on the design quality indicators, optimize the responsibility allocation of the initial class diagram using a multi-objective genetic algorithm to obtain the optimized initial class diagram.

[0077] It is understandable that, in the context of Object-Oriented Analysis and Design (OOAD), the allocation of responsibilities among classes is a crucial issue. The responsibility allocation described in the embodiments of this application can be understood as determining which attributes and functions each class possesses.

[0078] It should be noted that in traditional design schemes, the allocation of responsibilities among classes relies on human judgment and decision-making. In practical applications, design principles such as the General Responsibility Assignment Software Principles (GRASP) can provide design guidance for technical personnel. For example, in the aforementioned anemic model architecture design, the Single Responsibility Principle and the Information Expert Principle are implicitly used to guide the design.

[0079] For example, design quality metrics may include class design quality metrics such as coupling degree, cohesion degree, class size, and number of methods.

[0080] Specifically, the terminal can use the aforementioned generated anemia model architecture design as the initial population, define the fitness function with class design quality indicators (such as coupling degree, cohesion, class size, number of methods, etc.), and use a multi-objective genetic algorithm to optimize the responsibility allocation of the initial class graph to obtain the initial class graph after optimized responsibility allocation.

[0081] Step S304: The initial class diagram after the optimized responsibility allocation is designed and optimized using the large model to obtain the optimized design model based on the anemic model architecture design.

[0082] For example, the large model has two levels of application in optimizing class diagram design: first, it evaluates whether the initial class diagram design can achieve collaboration between classes and generate the corresponding sequence diagram; second, it provides suggestions on applying design patterns to optimize class diagram design.

[0083] Specifically, the terminal can use the large model to evaluate whether the current design can achieve collaboration between classes, and use the optimization suggestions provided by the large model to further optimize the class diagram.

[0084] In one embodiment, such as Figure 4 As shown, based on the design quality indicators, a multi-objective genetic algorithm is used to optimize the responsibility allocation of the initial class graph, resulting in an optimized initial class graph. This includes the following steps S402 to S404. Wherein:

[0085] Step S402: Use the optimized design model as the initial population of the multi-objective genetic algorithm, and determine the fitness function of the multi-objective genetic algorithm according to the design quality index.

[0086] Design quality metrics may include coupling, cohesion, class size (i.e., class size), and number of methods.

[0087] For example, the fitness function of a multi-objective genetic algorithm can be defined using design quality metrics such as class coupling and cohesion measures.

[0088] It's important to note that a Genetic Algorithm (GA) is a search algorithm that mimics the process of biological evolution. Its core idea is to solve optimization problems by simulating the principles of natural selection and genetics. For ease of understanding, the following provides some basic components and the working process of a genetic algorithm:

[0089] ① Population: At the start of the algorithm, a set of random solutions is generated as the initial population. Each solution is called an "individual" and represents a point in the problem space.

[0090] ② Fitness Function: The fitness of each individual determines its probability of survival in the population. Fitness generally reflects the quality of the solution to the problem; individuals with higher fitness are more likely to be selected to generate the next generation.

[0091] ③ Selection: Select individuals from the current population to produce the next generation based on their fitness.

[0092] ④ Crossover: Selected individuals are used to generate new individuals through a crossover operation. Crossover simulates the process of chromosome exchange in biological heredity and can produce new individuals with parental characteristics.

[0093] ⑤ Mutation: Randomly altering some genes in newly generated individuals to increase population diversity. Mutation can prevent the algorithm from prematurely converging to a local optimum.

[0094] ⑥ Iteration: A new generation of individuals forms a new population, which is used for the next round of selection, crossover, and mutation. This process is repeated until a termination condition is met (such as the number of iterations or the quality of the solution).

[0095] Specifically, the terminal can use the optimized design model as the initial population of the multi-objective genetic algorithm and determine the fitness function of the multi-objective genetic algorithm based on the design quality index.

[0096] Step S404: Based on the initial population and fitness function of the multi-objective genetic algorithm, optimize the responsibility allocation of the initial class graph through the multi-objective genetic algorithm to obtain the initial class graph after optimized responsibility allocation.

[0097] It is understandable that there are many quality evaluation criteria in the design of responsibility assignment in class diagrams. However, traditional alternative heuristics make it difficult to combine these evaluation criteria into an optimization goal, resulting in low efficiency in the optimization process of responsibility assignment in class diagrams, which affects code generation efficiency and code generation quality.

[0098] Specifically, when the terminal uses a multi-objective genetic algorithm to solve the class responsibility allocation problem, the optimization objective can be designed as follows: to maximize the design quality of the class. In this case, design quality indicators can include class coupling, cohesion, class size, number of methods, and other class design quality metrics. Further, the terminal can use the aforementioned anemic model architecture design as the initial population and define the fitness function as the class design quality indicator. Then, based on the current fitness, it selects excellent design schemes for reproduction (i.e., "selection"). Next, it randomly selects two design schemes and swaps some of their features according to preset rules to generate new design schemes (i.e., "crossover"). Then, it randomly changes certain parts of a design scheme to increase diversity (i.e., "mutation") to iteratively optimize until the optimization process reaches the maximum number of iterations or the current fitness no longer significantly improves.

[0099] It is understood that the embodiments of this application, by employing a multi-objective genetic algorithm, can provide automated decision support for the reallocation of methods and attributes (i.e., responsibility allocation) of classes in a class diagram, thereby effectively improving the efficiency of class diagram optimization, and thus improving the efficiency and quality of code generation.

[0100] In one embodiment, such as Figure 5 As shown, the initial class diagram after optimizing the allocation of responsibilities is designed and optimized using a large model to obtain an optimized design model based on the anemic model architecture design, including the following steps S502 to S504. Wherein:

[0101] Step S502: Convert the initial class diagram after optimizing the responsibility allocation into text form and input it into the large model to obtain the model input result.

[0102] The model input results can include class diagram rationality evaluation results, class diagram comprehensibility evaluation results, and class interaction sequence diagrams. Rationality evaluation results can be used to assess the rationality of the actual meaning of the class diagram; class diagram comprehensibility evaluation results can be used to assess the comprehensibility of the class diagram; and class interaction sequence diagrams can be used to represent the interaction process between classes.

[0103] It is understandable that in the aforementioned steps of using a multi-objective genetic algorithm to optimize the initial class diagram, the optimal solution for approximating the index is considered. In fact, the rationality of the actual meaning of the class diagram and the understandability of the class diagram can be further considered to further optimize the class diagram of the design model, thereby effectively improving the quality of code generation.

[0104] For example, class diagrams can be converted into text form and input into a large model to quickly determine whether classes in the current class diagram can collaborate and generate sequence diagrams of interactions between classes. Optionally, PlantUML can be used to convert class diagrams and / or sequence diagrams into text form.

[0105] Specifically, the terminal can convert the initial class diagram after optimizing the allocation of responsibilities into text form and input it into the large model to obtain the rationality evaluation results for the current class, the class diagram understandability evaluation results, and the class interaction sequence diagram.

[0106] Step S504: Based on the initial class diagram and model input results after optimizing the responsibility allocation in text form, instruct the large model to apply design patterns to optimize the class diagram design of the initial class diagram, and obtain the optimized design model.

[0107] For example, if the design model is input into the large model in text form, a design Prompt can be used to enable the large model to provide suggestions for optimizing the design by applying design patterns.

[0108] It should be noted that design patterns are mature solutions for solving common design problems in object-oriented software development. In practical applications, through preliminary research, when the current design of the class diagram and user requirements are input into the larger model, the larger model can provide suggestions for optimizing the design using application design patterns, thereby improving the initial class diagram design.

[0109] Specifically, the terminal can use the initial class diagram and model input results after optimizing the allocation of responsibilities in text form to instruct the large model to apply design patterns to optimize the class diagram design of the initial class diagram, thereby obtaining the optimized design model.

[0110] In one embodiment, such as Figure 6 As shown, based on the requirement model, the initial class diagram of the design model is obtained, including the following steps S602 to S606. Wherein:

[0111] Step S602: Based on the conceptual class diagram of the requirement model, determine the entity classes and class relationships of the initial class diagram.

[0112] For example, when automatically generating the class diagram of the design model from the requirements model, the entity classes and relationships between classes in the conceptual class diagram of the requirements model are retained, and GET / SET methods corresponding to class attributes are added to the design model. It can be understood that the relationships between classes in the conceptual class diagram of the requirements model have a natural consistency with the actual requirements; this part should also remain unchanged in the design model to avoid errors or missing relationships in the class relationships.

[0113] Specifically, the terminal can determine the entity classes and class relationships of the initial class diagram based on the conceptual class diagram of the demand model.

[0114] Step S604: Generate the service classes of the initial class diagram based on the system sequence diagram and use case diagram of the requirement model.

[0115] For example, when the class diagram of the design model is automatically generated from the requirement model, the system operation related to a use case can be encapsulated into the newly created service class (i.e., Service class) of the corresponding design model based on the use case diagram (containing use cases) and the system sequence diagram (containing system operations related to use cases).

[0116] For example, for a use case, a new service class can be extracted based on the relevant information in the use case diagram and the system sequence diagram. This class contains information such as class name, attributes, and operations. The class name can be extracted from the use case diagram, while the attributes and operations can be extracted from the system sequence diagram and the system operation contract.

[0117] Specifically, the terminal can generate service classes for the initial class diagram based on the system sequence diagram and use case diagram of the requirement model.

[0118] Step S606: Based on the entity classes in the initial class diagram, generate the entity management class of the initial class diagram.

[0119] The entity management class has a relationship with all entity classes.

[0120] For example, when the class diagram of the design model is automatically generated from the requirement model, a new entity management class (i.e., the EntityManager class) is created. This class is associated with all other entity classes to implement the functions of adding, deleting, modifying and querying objects.

[0121] It is understandable that entity management classes should have atomic operations related to adding, deleting, modifying, and querying objects. For example, atomic operations may include finding objects and collections of objects based on conditions, creating objects, adding objects to a collection of objects of the same class, deleting objects, and finding objects or collections of objects associated with an object based on conditions.

[0122] Specifically, in order to automatically generate class diagrams from the requirement model, the terminal can execute the following three rules: The first rule is to preserve the classes and relationships between classes in the conceptual class diagram and add GET / SET methods corresponding to class attributes; the second rule is to encapsulate all system operations related to a use case into the corresponding newly created Service class based on the use case diagram and system sequence diagram; the third rule is to create a new EntityManager class, which is associated with all other classes to complete the creation, deletion, modification, and query of objects.

[0123] In one embodiment, such asFigure 7 As shown, based on the optimized design model, monolithic system code matching the requirement model is generated, including the following steps S702 to S706. Wherein:

[0124] Step S702: Based on the optimized design model, generate the architecture design code for the monolithic system code according to the Jakarta EE specification.

[0125] For example, based on the optimized design model, a three-tier architecture (presentation layer, service layer, and data access layer) can be adopted according to the Jakarta EE specification to divide the system functional modules corresponding to the design model into the corresponding layers, so as to achieve decoupling between layers.

[0126] In some examples, using the Java language and Spring Boot framework, based on an optimized design model and in accordance with the Jakarta EE specification, a Controller layer is created to handle external requests, a Service layer is created to encapsulate business logic, and a Mapper layer is created to handle CRUD operations and persistence of objects, in order to generate monolithic system code that conforms to the anemic domain model.

[0127] Specifically, the terminal can generate the architecture design code for the monolithic system code based on the optimized design model and in accordance with the Jakarta EE specification.

[0128] Step S704: Based on the business use cases of the optimized design model, generate the business logic code for the monolithic system.

[0129] For example, an object interaction diagram can be extracted from the main business use cases of the design model, and a UML state machine diagram can be used to describe the state transitions of each business process in the object interaction diagram, thereby achieving visualization and management of the process. Furthermore, in the Service layer, the code for each functional point and business rule of the system is implemented based on the business use cases and the object interaction diagram.

[0130] Specifically, the terminal can generate business logic code for the monolithic system based on the business use cases of the optimized design model.

[0131] Step S706: Based on the optimized design model and according to the ORM framework, generate the data access code for the monolithic system code.

[0132] For example, generating data access code for a monolithic system based on the optimized design model and according to the ORM framework may include the following steps:

[0133] ① Use JPA (Java Persistence API) standard operation entity classes to encapsulate relational databases and hide the underlying database implementation details. For example, use JPA's CRUD interface to perform data persistence operations.

[0134] ② Use JPA annotations to map the properties in the entity class to the structure of the database table, automatically generate the table structure and manage object relationships;

[0135] ③ Choose a suitable ORM (Object-Relational Mapping) framework (such as Hibernate) to implement the ORM mapping between objects and relational databases, and automatically generate SQL (Structured Query Language) statements to realize data CRUD operations.

[0136] Specifically, the terminal can generate data access code for the monolithic system based on the optimized design model and the ORM framework.

[0137] In some possible implementations, system deployment code can also be generated based on the optimized design model. For example, the project can be packaged into a Docker image using a Dockerfile, and then container orchestration tools such as Kubernetes can be used to achieve automated deployment and elastic scaling of the system. Optionally, the system interface documentation can be defined using OpenAPI standards to facilitate understanding and use of system functions.

[0138] In some possible implementations, based on the optimized design model, other related code can be generated, such as: using the JPA standard caching mechanism to cache query results and improve the efficiency of subsequent identical queries, thereby decoupling the database and the application; encapsulating some common processes using the template method pattern to improve code reusability; using the JWT standard to pass a token in the request to achieve request authentication and ensure security; adopting a transaction management mechanism to maintain data consistency and combining multiple operations into an indivisible unit of work; encapsulating request parameters and response results into DTO objects to achieve data transmission of requests and responses; encapsulating call relationships through request forwarding to isolate dependencies between systems; using message packing and unpacking mechanisms to serialize request and response content to hide data transmission details; and encapsulating various functional modules of the system into services to divide interfaces and responsibilities.

[0139] In one embodiment, the method further includes the following steps:

[0140] Based on the system operation contract of the demand model, the sequence diagram of the design model is obtained;

[0141] Sequence diagrams can be used to describe dynamic interactions within a monolithic system. For example, a sequence diagram of a design model can be generated based on the definition, preconditions, and postconditions of the main body of a system operation contract.

[0142] In some examples, the main body of the system operation contract can be divided into the following three parts:

[0143] The first part is the definition section, which is used to define instances of classes or collections of instances of classes. In practical applications, variables defined in this definition section will be used in preconditions and postconditions.

[0144] The second part is the precondition, which checks the system state before the system operation is executed. This check usually verifies whether an object exists and whether its value meets specific conditions. It should be noted that the checks in the precondition usually do not involve interactions between objects.

[0145] The third part is the postcondition, which describes the state the system should be in after the system operation contract is executed. This part involves changes in the system state and the dynamic behavior inside the system, and is the main basis for generating the system sequence diagram.

[0146] It should be noted that the process of generating a sequence diagram is actually the process of extracting messages and objects from the OCL statements of the system operation contract. Specifically, it may include: the identification of message tags, the identification of message senders and receivers.

[0147] Specifically, the terminal summarizes the OCL statements commonly used in system operation contracts, formulates conversion rules based on statement types, and thus realizes the conversion of OCL statements into messages in the sequence diagram.

[0148] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps. It is understood that the steps in different embodiments can be freely combined as needed, and all non-contradictory solutions formed by such combinations are within the scope of protection of this application.

[0149] Based on the same inventive concept, this application also provides a code generation apparatus for implementing the code generation method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more code generation apparatus embodiments provided below can be found in the limitations of the code generation method described above, and will not be repeated here.

[0150] In one exemplary embodiment, such as Figure 8 As shown, a code generation apparatus 800 is provided, comprising:

[0151] Model acquisition module 802 is used to acquire the requirement model;

[0152] The initial class diagram is obtained from model 804, which is used to obtain the initial class diagram of the design model based on the requirement model.

[0153] The optimization module 806 is used to optimize the initial class diagram of the design model using a multi-objective genetic algorithm and a large model, so as to obtain the optimized design model.

[0154] Code generation module 808 is used to generate monolithic system code that matches the requirement model based on the optimized design model.

[0155] In one embodiment, the optimization module 806 is further configured to optimize the responsibility allocation of the initial class diagram using a multi-objective genetic algorithm based on the design quality index, thereby obtaining an initial class diagram with optimized responsibility allocation; and to optimize the design of the initial class diagram with optimized responsibility allocation using a large model, thereby obtaining an optimized design model based on the anemia model architecture design.

[0156] In one embodiment, the optimization module 806 is further configured to use the optimized design model as the initial population of the multi-objective genetic algorithm, and determine the fitness function of the multi-objective genetic algorithm according to the design quality indicators; the design quality indicators include coupling degree, cohesion, class size and number of methods; based on the initial population and fitness function of the multi-objective genetic algorithm, the responsibility allocation of the initial class graph is optimized by the multi-objective genetic algorithm to obtain the initial class graph after optimized responsibility allocation.

[0157] In one embodiment, the optimization module 806 is further configured to convert the initial class diagram after optimizing the responsibility allocation into text form and input it into the large model to obtain the model input result; the model input result includes the class diagram rationality evaluation result, the class diagram understandability evaluation result, and the class interaction sequence diagram; based on the initial class diagram after optimizing the responsibility allocation in text form and the model input result, the large model is instructed to apply design patterns to optimize the class diagram design of the initial class diagram to obtain the optimized design model.

[0158] In one embodiment, the initial class diagram acquisition model 804 is further used to determine the entity classes and class relationships of the initial class diagram based on the conceptual class diagram of the requirement model; generate the service classes of the initial class diagram according to the system sequence diagram and use case diagram of the requirement model; generate the entity management class of the initial class diagram based on the entity classes of the initial class diagram; the entity management class has an association relationship with all entity classes.

[0159] In one embodiment, the code generation module 808 is further configured to generate the architecture design code of the monolithic system code based on the optimized design model and in accordance with the Jakarta EE specification; generate the business logic code of the monolithic system code based on the business use cases of the optimized design model; and generate the data access code of the monolithic system code based on the optimized design model and in accordance with the ORM framework.

[0160] In one embodiment, the device 800 further includes:

[0161] The sequence diagram generation module is used to obtain the sequence diagram of the design model based on the system operation contract of the requirement model; the sequence diagram is used to describe the dynamic interaction behavior within the monolithic system.

[0162] Each module in the aforementioned code generation device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.

[0163] In one exemplary embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 9As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a code generation method. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.

[0164] Those skilled in the art will understand that Figure 9 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0165] In one embodiment, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.

[0166] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above method embodiments.

[0167] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.

[0168] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.

[0169] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.

[0170] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.

Claims

1. A code generation method, characterized in that, The method includes: Obtain the requirement model; the requirement model is a UML / SysML model. Based on the aforementioned requirement model, the initial class diagram of the design model is obtained; The initial class graph of the design model is optimized using a multi-objective genetic algorithm and a large model to obtain the optimized design model; Based on the optimized design model, generate monolithic system code that matches the requirement model; The optimization of the initial class diagram of the design model using a multi-objective genetic algorithm and a large model to obtain an optimized design model includes: optimizing the responsibility allocation of the initial class diagram using the multi-objective genetic algorithm according to design quality indicators to obtain an initial class diagram with optimized responsibility allocation; the design quality indicators include coupling degree, cohesion, class size, and number of methods; and optimizing the initial class diagram with optimized responsibility allocation using the large model to obtain the optimized design model based on an anemic model architecture. The step of optimizing the initial class diagram after the optimized responsibility allocation using the large model to obtain the optimized design model based on the anemic model architecture includes: converting the initial class diagram after the optimized responsibility allocation into text form and inputting it into the large model to obtain model input results; the model input results include class diagram rationality evaluation results, class diagram understandability evaluation results, and class interaction sequence diagrams; based on the text form of the initial class diagram after the optimized responsibility allocation and the model input results, instructing the large model to apply design patterns to optimize the class diagram design of the initial class diagram to obtain the optimized design model; The step of converting the initial class diagram after the optimized responsibility allocation into text form includes: using PlantUML to convert the initial class diagram into text form; The step of optimizing the responsibility allocation of the initial class graph using the multi-objective genetic algorithm based on design quality indicators to obtain an initial class graph with optimized responsibility allocation includes: using the optimized design model as the initial population of the multi-objective genetic algorithm, and determining the fitness function of the multi-objective genetic algorithm according to the design quality indicators; the design quality indicators include coupling degree, cohesion, class size, and number of methods; and optimizing the responsibility allocation of the initial class graph using the multi-objective genetic algorithm based on the initial population and fitness function to obtain the initial class graph with optimized responsibility allocation.

2. The method according to claim 1, characterized in that, The initial class diagram of the design model obtained based on the requirement model includes: Based on the conceptual class diagram of the requirement model, the entity classes and class relationships of the initial class diagram are determined; Based on the system sequence diagram and use case diagram of the requirement model, the service classes of the initial class diagram are generated; Based on the entity classes in the initial class diagram, an entity management class is generated from the initial class diagram; the entity management class has an association relationship with all the entity classes.

3. The method according to claim 1, characterized in that, The process of generating monolithic system code that matches the requirement model based on the optimized design model includes: Based on the optimized design model, and in accordance with the Jakarta EE specification, the architecture design code for the monolithic system is generated. Based on the business use cases of the optimized design model, generate the business logic code of the monolithic system code; Based on the optimized design model, and according to the ORM framework, the data access code of the monolithic system code is generated.

4. The method according to any one of claims 1 to 3, characterized in that, The method further includes: Based on the system operation contract of the demand model, the sequence diagram of the design model is obtained; The sequence diagram is used to describe the dynamic interaction behavior within a single system.

5. A code generation device, characterized in that, The device includes: The model acquisition module is used to acquire the requirement model; the requirement model is a UML / SysML model. The initial class diagram acquisition model is used to obtain the initial class diagram of the design model based on the requirement model; The optimization module is used to optimize the initial class diagram of the design model using a multi-objective genetic algorithm and a large model to obtain the optimized design model. The code generation module is used to generate monolithic system code that matches the requirement model based on the optimized design model. The optimization module is further configured to optimize the responsibility allocation of the initial class graph using the multi-objective genetic algorithm based on the design quality indicators, to obtain the initial class graph with optimized responsibility allocation; the design quality indicators include coupling degree, cohesion degree, class size and number of methods; the initial class graph with optimized responsibility allocation is optimized using the large model to obtain the optimized design model based on the anemic model architecture design; The optimization module is further configured to convert the initial class diagram after the optimized responsibility allocation into text form and input it into the large model to obtain model input results; the model input results include class diagram rationality evaluation results, class diagram understandability evaluation results, and class interaction sequence diagrams; based on the text form of the initial class diagram after the optimized responsibility allocation and the model input results, the large model is instructed to apply design patterns to optimize the class diagram design of the initial class diagram to obtain the optimized design model; The optimization module is also used to convert the initial class diagram into text form using PlantUML; The optimization module is further configured to use the optimized design model as the initial population of the multi-objective genetic algorithm, and determine the fitness function of the multi-objective genetic algorithm according to the design quality index; the design quality index includes coupling degree, cohesion degree, class size and number of methods; based on the initial population and fitness function of the multi-objective genetic algorithm, the responsibility allocation of the initial class graph is optimized by the multi-objective genetic algorithm to obtain the initial class graph after the optimized responsibility allocation.

6. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 4.

7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 4.