A code generation method, low-code platform and storage medium based on UML model
Through the UML model-based code generation method, the existing low-code platform lacks flexibility in dealing with complex business logic and data models is solved, high-quality, customizable and scalable code generation is achieved, development efficiency and code consistency are improved, and the needs of enterprise-level applications are met.
Patent Information
- Application Number
- CN202411958892.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-04
AI Technical Summary
Existing low-code platforms lack flexibility and depth in handling complex business logic and data models. The generated code is difficult to customize and expand, and is highly repetitive, unable to seamlessly integrate into existing systems or third-party tools, and cannot meet the needs of enterprise-level applications.
Using the code generation method based on the UML model, by obtaining the metadata of the physical model and converting it into DDL scripts and CRUD codes of the database table, UML charts are established and parsed into internal data structures, combining the FreeMarker template file library and predefined code files to generate path rules, generate Java code, and minimized code updates are achieved through reverse parsing and difference comparison algorithms.
Support graphical modeling of complex models, improve development efficiency, automatically generate high-quality, customizable and scalable code, reduce development costs, ensure consistency between models and code, reduce errors and rework, and the modular architecture facilitates functional expansion and maintenance.
Smart Images

Figure HDA0005216593070000011 
Figure HDA0005216593070000012 
Figure HDA0005216593070000013
Abstract
Description
Technical Field
[0001] The present application belongs to the field of low-code development technology, and specifically relates to a code generation method, storage medium and low-code platform based on a UML model. Technical Background
[0002] Currently, there are many low-code platforms on the market, most of which support the rapid construction of applications by dragging components and configuring properties. However, most of these platforms can only support the development of simple form applications, and often lack sufficient flexibility and depth when dealing with complex business logic and data models. In addition, the code they generate is often difficult to customize and expand, and the generated code is highly repetitive and difficult to seamlessly integrate with existing systems or third-party tools, which cannot meet the needs of enterprise-level applications.
[0003] Technical content
[0004] In view of the above-mentioned shortcomings and deficiencies of the prior art, the present invention proposes a code generation method and a low-code platform that can realize the design of complex models and automatic code generation.
[0005] In order to achieve the above object, the main technical solutions adopted by the present invention include:
[0006] In a first aspect, the present invention provides a code generation method based on a UML model, comprising:
[0007] Obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code;
[0008] Create UML diagrams and parse them into internal data structures based on metadata through algorithms, supporting the connection with metadata generated based on the physical model and Java general basic data;
[0009] Parsing the metadata through a code generation engine, combining the FreeMarker template file library and predefined code file generation path rules, to generate Java code;
[0010] When the UML diagram change is detected, the manually modified code can be updated in a minimized manner by identifying the differences through reverse parsing and difference comparison algorithms.
[0011] Optionally, the metadata is parsed by a code generation engine, combined with a FreeMarker template file library and a predefined code file generation path rule, to generate Java code, including:
[0012] The code generation engine parses the metadata and extracts required information, including but not limited to model name, field name, and field type;
[0013] According to the parsed data structure information, the code generation engine selects a corresponding template for application and fills the data structure information into the template according to predefined rules to replace the placeholder in the template;
[0014] The code generation engine outputs the filled template as an actual code file.
[0015] Optionally, the predefined code file generation path rule is implemented by defining a set of DSL, and the user can define DSL rules and templates through an interface, including:
[0016] Define DSL syntax;
[0017] According to the defined DSL grammar rules, a parser is created using a parser generation tool, wherein the parser parses the DSL file and generates an abstract syntax tree AST;
[0018] Design an abstract syntax tree AST structure to represent the syntax structure of the DSL, wherein the AST nodes are mapped to elements in the DSL syntax rules;
[0019] The AST is used as input to implement an interpreter or compiler to process the logic defined by the DSL; the interpreter traverses the AST and performs code generation and code parsing according to the rules defined by the DSL;
[0020] Integrate a template engine FreeMarker to fill in code templates and generate specific code according to the defined DSL rules and templates.
[0021] Optionally, the predefined code file generation path rules are implemented by defining a set of DSLs, and the syntax and semantic design of the DSL matches the characteristics of the internal data structure; the internal data structure includes but is not limited to the required class name, method name, method input / output parameters.
[0022] Optionally, when the UML diagram is detected to be changed, the manually modified code can be updated to minimize the difference by identifying the difference through reverse parsing and difference comparison algorithm, including:
[0023] Use the visitor pattern to traverse each node of the AST;
[0024] For each node in the AST, convert it back to the corresponding grammatical structure in the DSL file according to its type and attributes;
[0025] Write the converted DSL syntax structure into a file, thereby realizing reverse parsing from AST to DSL file;
[0026] Decompile the class file, obtain the file metadata, and reverse parse it into the internal data structure;
[0027] The final DSL file and the reverse parsed internal data structure are combined with the code template to realize the reverse parsing of the code.
[0028] Optionally, the step of obtaining the metadata of the physical model and converting it into a DDL script of a database table and corresponding CRUD code further includes: parsing the data definition in the physical model, and generating a database table creation statement through relationship mapping; each physical model corresponds to a database table, the physical model fields are mapped to table fields, and the data types and relationship constraints are defined according to the physical model configuration.
[0029] Optionally, the method further comprises:
[0030] Optimize the structure of database tables and codes by analyzing the inheritance, combination and dependency relationships of UML models;
[0031] For common business models, secondary development can be carried out through code download.
[0032] Optionally, the code generation engine outputs the filled template as an actual code file, and also includes: when the engine generates the code, it will perform syntax and logic checks to ensure that the generated code complies with programming specifications and design patterns.
[0033] In a second aspect, the present invention further provides a low-code platform based on a UML model, comprising:
[0034] A model mapping module is used to parse the data definition in the physical model and generate a database table creation statement through relationship mapping;
[0035] The metadata generation module is used to obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code, and output the internal data structure based on the metadata through the UML diagram established by algorithm analysis, and supports the connection with the metadata generated based on the physical model and the Java general basic data;
[0036] A code generation engine, for parsing the metadata, combining the FreeMarker template file library and predefined code file generation path rules, to generate Java code;
[0037] The bidirectional update module is used for detecting the change of the UML diagram, and identifying the difference of the manually modified code through reverse parsing and difference comparison algorithm to perform minimized update.
[0038] In a third aspect, the present invention further provides a computer-readable storage medium, comprising a computer-readable storage medium storing a computer program and a processor, wherein when the computer program is read and executed by the processor, the aforementioned UML model-based code generation method is implemented.
[0039] Compared with the prior art, the technical solution of the present invention has the following advantages: (1) supports graphical modeling of complex models and improves development efficiency; (2) automatically generates high-quality, customizable, and extensible code to reduce development costs; (3) a two-way synchronous update mechanism ensures the consistency of the model and the code, reducing errors and rework; (4) a modular architecture design facilitates functional expansion and maintenance.
[0040] Instruction Manual
[0041] Figure 1 A schematic diagram of a code generation method based on a UML model provided in an embodiment of the present invention;
[0042] Figure 2 A schematic diagram of a reverse parsing process in a code generation method based on a UML model provided in an embodiment of the present invention;
[0043] Figure 3 A workflow diagram of a code generation engine in a code generation method based on a UML model provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0044] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the specific embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. Although certain embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be interpreted as being limited to the embodiments described herein. On the contrary, these embodiments are provided to provide a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments of the present invention are only for exemplary purposes and are not intended to limit the scope of protection of the present invention.
[0045] It should be understood that the various steps described in the method embodiments of the present invention may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present invention is not limited in this respect.
[0046] The term "including" and its variations used in this document are open inclusions, that is, "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one other embodiment"; the term "some embodiments" means "at least some embodiments"; the term "optionally" means "optional embodiments". Relevant definitions of other terms will be given in the following description. It should be noted that the concepts of "first", "second", etc. mentioned in the present invention are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0047] It should be noted that the modifications of "one" and "plurality" mentioned in the present invention are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0048] refer to Figures 1 to 3 The embodiment of the present invention provides a code generation method based on a UML model, comprising:
[0049] Step S1: Obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code.
[0050] Specifically, the mapping mechanism between database tables and physical models includes: the low-code platform in this embodiment can parse the data definition in the physical model and generate database table creation statements through relationship mapping, supporting multiple databases such as sqlServer, Oracle, Mysql, etc.; each physical model corresponds to a database table, the physical model fields are mapped to table fields, and the data types and relationship constraints (such as primary keys and foreign keys) are defined according to the physical model configuration.
[0051] The metadata generation module is responsible for extracting detailed information of the physical model and converting it into the DDL (data definition language) script of the database table and the corresponding CRUD (create, read, update, delete) code, which can be run directly as a microservice.
[0052] Step S2: Create a UML diagram and parse it into an internal data structure based on metadata through an algorithm, supporting the connection with metadata generated based on the physical model and Java general basic data.
[0053] Specifically, the low-code platform in this embodiment has a built-in UML modeling tool that supports the creation of class diagrams and object diagrams. Users can define classes and relationships on the graphical interface, and the platform generates metadata through graphic recognition and parsing algorithms. Since there is a mapping relationship between UML class diagram data and metadata, the UML to metadata conversion can be completed through the mapping relationship.
[0054] Step S3: parse the metadata through the code generation engine, combine the FreeMarker template file library and the predefined code file generation path rules to generate Java code. The details are as follows:
[0055] (1) Parsing data structure: The code generation engine first parses the metadata and extracts the required information, such as model name, field name, field type, etc.
[0056] (2) Application template: Based on the parsed data structure information, the engine will select the corresponding template for application.
[0057] (3) Filling the template: The engine fills the data structure information into the template and replaces the placeholders in the template. In the process of filling the template, the engine will determine which code needs to be generated and how to generate it based on the predefined code file generation path rules. For example, if a field is marked as a foreign key, the engine may generate additional association query logic in the Service.
[0058] (4) Generate code: Finally, the engine outputs the filled template as an actual code file.
[0059] The predefined code file generation path rules in this embodiment are defined by a set of DSL (domain-specific language). Users can customize the rules and templates for code generation through the user interface of the low-code platform in this embodiment to meet specific development needs. The details are as follows:
[0060] 1. Define the DSL grammar: These rules describe the structure and valid expressions of the DSL. You can use a parser generator like ANTLR to define the grammar rules of the DSL.
[0061] 2. Construct a parser: According to the defined DSL grammar rules, use a parser generation tool to create a parser, which will be used to parse the DSL file and generate an abstract syntax tree AST. In this embodiment, JavaCC is preferably used as the parser generation tool.
[0062] 3. Design AST structure: Design an abstract syntax tree AST structure to represent the grammatical structure of the DSL. The abstract syntax tree AST nodes will correspond to the elements in the DSL grammar rules, such as classes, methods, fields, etc.
[0063] 4. Implement an interpreter or compiler: Using the abstract syntax tree AST as input, implement an interpreter or compiler to process the logic defined by the DSL. This interpreter will traverse the AST and perform code generation and code parsing according to the rules defined by the DSL.
[0064] 5. Template engine integration: Integrate a template engine FreeMarker to fill in code templates. Templates define the structure of the code, and DSL provides specific data to fill in these templates.
[0065] 6. Code generation: Generate specific code based on DSL rules and templates. This process involves traversing the AST, applying the data and rules defined by the DSL to the template, and generating a complete code file.
[0066] In particular, the internal data structure in this technical solution is specific, and it defines the metadata structure required for code generation, including the class name, method name, method input parameter, output parameter and other data required for generation. Through the structured internal data format, the availability of metadata can be increased and the complexity of the template can be reduced. These internal data lay the foundation for the formulation of DSL rules and provide users with a certain amount of modification space. In particular, the syntax and semantic design of DSL matches the characteristics of the internal data structure, so that the expressions of DSL can be directly mapped to these data structures, thereby realizing the modification of the generation rules and limiting the scope of modification to prevent code generation from being unavailable.
[0067] In addition, the metadata in step S3 includes both metadata generated from the physical model and metadata from the UML diagram conversion. The engine uses template engine technology to support code generation in Java and multiple database dialects.
[0068] In particular, when the engine generates code, it will perform syntax and logic checks to ensure that the generated code complies with programming standards and design patterns.
[0069] Step S4: When a change in the UML diagram is detected, the manually modified code can be updated to minimize the difference by identifying the difference through reverse parsing and difference comparison algorithms.
[0070] Among them, the low-code platform in this embodiment provides a mechanism for real-time monitoring of UML diagram changes. When a diagram change is detected, code generation can be manually triggered after passing the review. Any manual modification of the code can be reflected in the UML model to ensure the consistency of the model and the code. After reverse parsing uses JavaCC to generate AST, reverse parsing can be achieved by traversing AST and converting it back to the form of DSL file. The details are as follows:
[0071] (1) Traverse AST: Use the visitor pattern to traverse each node of AST.
[0072] (2) Node conversion: For each node in the AST, convert it back to the corresponding grammatical structure in the DSL file according to its type and attributes.
[0073] (3) Generate DSL file: Write the converted DSL syntax structure into a file, thereby realizing reverse parsing from AST to DSL file.
[0074] (4) Obtain file metadata by decompiling the class file and reversely parse it into internal data structures.
[0075] (5) The final DSL file and the reverse parsed internal data structure are combined with the code template to achieve reverse parsing of the code.
[0076] In summary, the technical solution of this embodiment can effectively convert the UML model into actual code, while maintaining synchronous updating of the model and the code, thereby improving development efficiency, reducing the error rate, and ensuring code quality.
[0077] Furthermore, an embodiment of the present invention further provides a low-code platform based on a UML model, including:
[0078] The model mapping module is used to parse the data definition in the physical model and generate database table creation statements through relationship mapping;
[0079] The metadata generation module is used to obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code, as well as output the internal data structure based on the metadata through the UML diagram established by algorithm analysis, and supports the connection with the metadata generated based on the physical model and the Java general basic data;
[0080] Code generation engine, used to generate Java code by parsing metadata, combining FreeMarker template file library and predefined code file generation path rules;
[0081] The bidirectional update module is used to detect changes in UML diagrams. When the manually modified code is detected, the differences can be identified through reverse parsing and difference comparison algorithms to minimize updates.
[0082] In particular, the low-code platform of this embodiment also includes an optimization strategy with model-driven and code generation. The platform optimizes the structure of database tables and codes by analyzing the inheritance, combination and dependency of UML models. In addition, for common business models, such as user rights management and transaction processing, the platform provides a basic framework, which only needs to reference the framework to implement functions, reducing repeated design work.
[0083] The technical effect that can be achieved by this embodiment is substantially the same as the technical effect that can be achieved by the aforementioned code generation method based on the UML model, and will not be described in detail here.
[0084] In addition, an embodiment of the present invention further provides a computer-readable storage medium, including a computer-readable storage medium storing a computer program and a processor. When the computer program is read and executed by the processor, a code generation method based on a UML model of the present technical solution is implemented.
[0085] The technical effect that can be achieved by this embodiment is substantially the same as the technical effect that can be achieved by the aforementioned code generation method based on the UML model, and will not be described in detail here.
[0086] Since the system / device described in the above embodiments of the present invention is a system / device used to implement the method of the above embodiments of the present invention, a person skilled in the art can understand the specific structure and deformation of the system / device based on the method described in the above embodiments of the present invention, and thus will not be described in detail here. All systems / devices used in the method of the above embodiments of the present invention belong to the scope of protection of the present invention.
[0087] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0088] The present invention is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present invention. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions.
[0089] A computer system may include clients and servers. Clients and servers are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship to each other.
[0090] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc. In the present application, the unit described as a separate component may or may not be physically separated, and the component displayed as a unit may or may not be a physical unit, that is, it may be located in one place, or it may be distributed on multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the embodiment of the present invention. In addition, each functional unit in each embodiment of the present invention can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
[0091] Although the present invention is disclosed as above, the protection scope of the present invention is not limited thereto. Those skilled in the art may make various changes and modifications without departing from the spirit and scope of the present invention, and these changes and modifications will fall within the protection scope of the present invention.
Claims
1. A code generation method based on UML model, characterized in that include: Obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code; Create UML diagrams and parse them into internal data structures based on metadata through algorithms, supporting the connection with metadata generated based on the physical model and Java general basic data; Parsing the metadata through a code generation engine, combining the FreeMarker template file library and predefined code file generation path rules, to generate Java code; When the UML diagram change is detected, the manually modified code can be updated in a minimized manner by identifying the differences through reverse parsing and difference comparison algorithms.
2. A code generation method based on UML model as claimed in claim 1, characterized in that The metadata is parsed by a code generation engine, combined with a FreeMarker template file library and a predefined code file generation path rule, to generate Java code, including: The code generation engine parses the metadata and extracts required information, including but not limited to model name, field name, and field type; According to the parsed data structure information, the code generation engine selects a corresponding template for application and fills the data structure information into the template according to predefined rules to replace the placeholder in the template; The code generation engine outputs the filled template as an actual code file.
3. A code generation method based on a UML model as claimed in claim 1 or 2, characterized in that The predefined code file generation path rules are implemented by defining a set of DSLs. The user can define DSL rules and templates through the interface, including: Define DSL syntax; According to the defined DSL grammar rules, a parser is created using a parser generation tool, wherein the parser parses the DSL file and generates an abstract syntax tree AST; Design an abstract syntax tree AST structure to represent the syntax structure of the DSL, wherein the AST nodes are mapped to elements in the DSL syntax rules; The AST is used as input to implement an interpreter or compiler to process the logic defined by the DSL; the interpreter traverses the AST and performs code generation and code parsing according to the rules defined by the DSL; Integrate a template engine FreeMarker to fill in code templates and generate specific code according to the defined DSL rules and templates.
4. A code generation method based on a UML model as described in claims 1 to 3, characterized in that The predefined code file generation path rule is implemented by defining a set of DSLs, and the syntax and semantic design of the DSL matches the characteristics of the internal data structure; the internal data structure includes but is not limited to the required class name, method name, method input / output parameters.
5. A code generation method based on UML model as claimed in claim 3, characterized in that When the UML diagram is changed, the manually modified code can be updated to a minimum by identifying the difference through reverse parsing and difference comparison algorithms, including: Use the visitor pattern to traverse each node of the AST; For each node in the AST, convert it back to the corresponding grammatical structure in the DSL file according to its type and attributes; Write the converted DSL syntax structure into a file, thereby realizing reverse parsing from AST to DSL file; Decompile the class file, obtain the file metadata, and reverse parse it into the internal data structure; The final DSL file and the reverse parsed internal data structure are combined with the code template to realize the reverse parsing of the code.
6. A code generation method based on UML model as claimed in claim 1, characterized in that The process of obtaining the metadata of the physical model and converting it into the DDL script of the database table and the corresponding CRUD code also includes: Parsing the data definition in the physical model, and generating database table creation statements through relationship mapping; Each physical model corresponds to a database table, the physical model fields are mapped to table fields, and data types and relationship constraints are defined according to the physical model configuration.
7. A code generation method based on UML model as claimed in claim 1, characterized in that The method further includes: Optimize the structure of database tables and codes by analyzing the inheritance, combination and dependency relationships of UML models; For common business models, secondary development can be carried out through code download.
8. A code generation method based on UML model as claimed in claim 2, characterized in that The code generation engine outputs the filled template as an actual code file, and also includes: When the engine generates code, it will perform syntax and logic checks to ensure that the generated code complies with programming specifications and design patterns.
9. A low-code platform based on UML model, characterized by include: A model mapping module is used to parse the data definition in the physical model and generate a database table creation statement through relationship mapping; The metadata generation module is used to obtain the metadata of the physical model and convert it into the DDL script of the database table and the corresponding CRUD code, and output the internal data structure based on the metadata through the UML diagram established by algorithm analysis, and supports the connection with the metadata generated based on the physical model and the Java general basic data; A code generation engine, for parsing the metadata, combining the FreeMarker template file library and predefined code file generation path rules, to generate Java code; The bidirectional update module is used for detecting the change of the UML diagram, and identifying the difference of the manually modified code through reverse parsing and difference comparison algorithm to perform minimized update.
10. A computer-readable storage medium, characterized in that: The invention comprises a computer-readable storage medium storing a computer program and a processor. When the computer program is read and executed by the processor, the method for generating codes based on a UML model as claimed in any one of claims 1 to 8 is implemented.
Citation Information
Cited By
Context visualization interaction and complementation method and device based on large model dialogue
CN120562571A
Code generation method based on unidirectional mapping synchronization and incremental updating
CN121680910A
Code generation method and system based on system IT model and AI agent
CN122470169A