A code generation method based on one-way mapping synchronization and incremental update
By using a method based on one-way mapping synchronization and incremental updates, the problems of synchronization inconsistency and low generation efficiency in model-driven development are solved, achieving efficient and accurate code generation and verification, and improving the synchronization efficiency and consistency between the model and the code.
Patent Information
- Application Number
- CN202511920537.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-18
- Publication Date
- 2026-11-27
- Estimated Expiration
- 2045-12-18
AI Technical Summary
Existing model-driven development technologies suffer from problems such as synchronization inconsistency, insufficient logic verification, and low code generation efficiency. In particular, when dealing with complex layered modular models, the synchronization and updating of models and code are not timely and the generation efficiency is low.
A code generation method based on one-way mapping synchronization and incremental update is adopted. By generating mapping objects, the mapping relationship is verified before construction. The AST generation and one-way mapping synchronization relationship are established, and incremental AST synchronization is performed when the model changes. Finally, code generation and dual verification are performed, and multi-threading technology is used for parallel processing.
It improves the synchronization efficiency and accuracy of the model and AST, enhances the logic verification capability, significantly improves code generation efficiency, and ensures the quality and consistency of the generated code.
Smart Images

Figure CN121680910B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of model-driven code generation technology, and in particular to a code generation method based on unidirectional mapping synchronization and incremental updates. Background Technology
[0002] In the field of software development, model-driven development (MDT) uses graphical modeling tools to build system models and then automatically converts these models into code, which can improve development efficiency and code quality while reducing manual coding errors. With the continuous development of complex systems, higher demands are being placed on the hierarchical and modular design of models and the accuracy and efficiency of code generation.
[0003] In existing technologies, model-to-code generation is often too tightly coupled, which presents the following problems when dealing with complex hierarchical modular models: Inconsistency caused by synchronization issues: When the model changes, the code cannot be updated in a timely and accurate manner, which can easily lead to inconsistency between the model and the code.
[0004] Insufficient logical validation: The consistency validation of the model's logical topology and metadata is not strict enough, which may lead to problems such as type mismatch and circular dependencies during code generation.
[0005] Low code generation efficiency: Traditional code generation methods typically use single-threaded processing, which results in low generation efficiency for large-scale modular models and cannot meet the development needs of complex systems. Summary of the Invention
[0006] In view of the above problems, the present invention is proposed to provide a code generation method based on one-way mapping synchronization and incremental update that overcomes or at least partially solves the above problems.
[0007] According to one aspect of the present invention, a code generation method based on one-way mapping synchronization and incremental update is provided, the code generation method comprising: Generate a mapping object; Before constructing the mapping relationship, the generated model object and metadata are validated; Establish a synchronization relationship between AST generation and one-way mapping; Model changes are synchronized with incremental AST; After the one-way mapping synchronization relationship is established, the code generation stage begins; The generated code and mapping relationship are double-verified.
[0008] Optionally, generating the mapping object specifically includes: Users can build simulation models and construct layered modular model objects by using graphical modeling tools to visually drag and drop and connect lines. The hierarchical modular model object is organized in a tree structure, with each module as a node, containing input and output ports and internal logical function attributes. The modules form a logical network through port connections.
[0009] Optionally, the step of performing verification on the generated model object and metadata before constructing the mapping relationship specifically includes: Logical topology verification: A depth-first search algorithm is used to traverse the hierarchical structure of the model objects to check for circular dependencies, broken circuits, disallowed modules, and logical errors in unconnected modules, ensuring that the data flow between modules is correct; Metadata consistency check: Compare the metadata declared for each module port with the actual connection port to verify its correctness.
[0010] Optionally, establishing the synchronization relationship between AST generation and one-way mapping specifically includes: The AST generator of the core processing layer is based on the hierarchical structure of the model objects and uses a depth-first traversal strategy to generate the AST. Starting from the root module, each module is processed sequentially according to the hierarchical order, and a corresponding AST subtree is created for the current module based on the logic of the current module. Add module structure information to AST node attribute fields, add ASTNode reference to each module node, and add module reference to the corresponding module object to each AST node; Next, check if the current module has submodules. If it does, process each submodule in the same way in sequence. The AST returned by each submodule is merged into the current parent AST structure as a child node. This process continues until all submodules of the model object have been traversed and the AST structure is generated.
[0011] Optionally, the module structure information specifically includes: module ID, port definition, and hierarchical path.
[0012] Optionally, the synchronization of model changes with incremental AST specifically includes: When any module in the model is modified, the corresponding AST node is found based on the ASTNode reference, and the update method is called synchronously to update it.
[0013] Optionally, after the one-way mapping synchronization relationship is established, entering the code generation stage specifically includes: Topology optimization and conflict detection: Based on the improved Kahn algorithm, the module connection DAG graph is topologically sorted. During the node traversal, the correctness of the data type and dimension of the input and output ports is verified in real time. Modules with mismatched types are recorded and the problematic modules and connecting edges are highlighted in the graphical interface. Parallelized AST processing: The identification module connects the independent module groups in the DAG graph, and uses multi-threading technology to generate the corresponding AST subtrees in parallel; Code generation: The code generation layer uses a depth-first traversal algorithm to traverse the entire AST node based on the module interface metadata and AST structure. Starting from the root node, it generates the corresponding C language code for each node based on the saved module information and code template engine.
[0014] Optionally, the dual verification of the generated code and mapping relationship specifically includes: MISRA-C Check: The generated C code is checked against the MISRA-C standard using a syntax checking tool; Mapping consistency verification: By traversing the AST nodes and model object nodes, the state information of the two is compared to check whether the code generation process has generated errors. If inconsistencies are found, the difference modules and nodes are marked and a report is generated.
[0015] This invention provides a code generation method based on one-way mapping synchronization and incremental updates. The code generation method includes: generating mapping objects; verifying the generated model objects and metadata before constructing the mapping relationship; establishing an AST generation and one-way mapping synchronization relationship; synchronizing model changes with incremental AST; entering the code generation stage after the one-way mapping synchronization relationship is established; and performing dual verification on the generated code and mapping relationship. The adoption of a hierarchical modular model object and AST one-way mapping synchronization mechanism improves the efficiency and accuracy of model and AST synchronization.
[0016] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description
[0017] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 A flowchart illustrating a code generation method based on one-way mapping synchronization and incremental update provided in an embodiment of the present invention; Figure 2 A flowchart illustrating the construction process of a hierarchical modular model object provided in an embodiment of the present invention; Figure 3The initial AST generation process provided in this embodiment of the invention; Figure 4 This is an incremental synchronous update process provided in the embodiments of the present invention; Figure 5 The code generation process provided for embodiments of the present invention. Detailed Implementation
[0019] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
[0020] The terms "comprising" and "having," and any variations thereof, in the specification, embodiments, claims, and drawings of this invention are intended to cover non-exclusive inclusion, such as including a series of steps or units.
[0021] The technical solution of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments.
[0022] like Figure 1 As shown, the present invention provides a code generation method based on one-way mapping synchronization and incremental update, including: generating a mapping object; performing verification on the generated model object and metadata before constructing the mapping relationship; establishing an AST generation and one-way mapping synchronization relationship; synchronizing model changes with incremental AST; entering the code generation stage after the one-way mapping synchronization relationship is established; and performing dual verification on the generated code and mapping relationship.
[0023] like Figures 2-5 As shown, this invention revolves around the one-way mapping synchronization mechanism between the logic of hierarchical modular model objects and the Abstract Syntax Tree (AST). The specific steps are as follows: 1. Mapping object generation Users can build simulation models using graphical modeling tools through visual drag-and-drop and connection methods, creating hierarchical modular model objects. These model objects are organized in a tree structure, with each module as a node, containing attributes such as input / output ports and internal logic functions. The modules form a complete logical network through port connections.
[0024] 2. Verification before mapping Before constructing the mapping relationship, the generated model object and metadata are validated.
[0025] Logical topology verification: A depth-first search algorithm is used to traverse the hierarchical structure of the model objects to check for logical errors in the module connection relationships, such as circular dependencies, broken circuits, modules that are not allowed to be connected, and unconnected modules, to ensure that the data flow between modules is correct.
[0026] Metadata consistency check: Compare the metadata declared for each module's ports with the actual connection ports to ensure correctness. For example, check whether a floating-point output port is incorrectly connected to an integer input port to prevent subsequent mapping and code generation anomalies caused by metadata conflicts.
[0027] 3. Establishment of synchronization relationship between AST generation and one-way mapping The core processing layer's AST generator is based on the hierarchical structure of model objects and uses a depth-first traversal strategy for generation. Starting from the root module, it processes each module sequentially according to the hierarchical order, creating a corresponding AST subtree for the current module based on its logic. Module structure information (module ID, port definition, hierarchical path, etc.) is added to the AST node attribute fields. An ASTNode reference (corresponding AST node) is added to each module node, and a module reference (corresponding module node) is added to each AST node. Next, it checks if the current module has submodules. If it does, each submodule is processed sequentially using the same method, and the AST returned by each submodule is merged into the current parent AST structure as a child node. This process continues until all submodules of the model object have been traversed, resulting in a complete AST structure.
[0028] 4. Model changes are synchronized with incremental AST. When any module in the model is modified, the corresponding AST node is found based on the ASTNode reference, and the update method is called synchronously to update it. For example, when the output type of a constant module changes from int to double, the module update will find the corresponding AST node through the ASTNode reference and pass the information that the output port type has changed from int to double. The AST node will then process the information accordingly and convert the type of the created variable from int to double.
[0029] 5. Code generation After the one-way mapping synchronization relationship is established, the code generation stage begins.
[0030] Topology optimization and conflict detection: Based on the improved Kahn algorithm, the module connection DAG graph is topologically sorted. During the node traversal, the correctness of the data type and dimension of the input and output ports is verified in real time. Modules with mismatched types are recorded and the problematic modules and connecting edges are highlighted in the graphical interface.
[0031] Parallelized AST processing: The identification module connects the independent module groups in the DAG graph, and uses multi-threading technology to generate the corresponding AST subtrees in parallel.
[0032] Code generation: The code generation layer uses a depth-first traversal algorithm to traverse the entire AST node based on the module interface metadata and AST structure. Starting from the root node, it generates the corresponding C language code for each node based on the saved module information and code template engine.
[0033] 6. Code generation result inspection The generated code and mapping relationship are double-verified.
[0034] MISRA-C Check: The generated C code is checked against the MISRA-C standard using a syntax checking tool.
[0035] Mapping consistency verification: By traversing the AST nodes and model object nodes, the state information of the two is compared to check whether the code generation process has generated errors. If inconsistencies are found, the difference modules and nodes are marked and a report is generated.
[0036] One-way mapping synchronization mechanism: The one-way mapping synchronization mechanism between the hierarchical modular model objects and the AST is adopted, which improves the efficiency and accuracy of model and AST synchronization.
[0037] Incremental update: When a module in the model changes, incremental updates are implemented through ASTNode references, which only process the affected AST nodes, reducing unnecessary computation and resource consumption.
[0038] Parallel processing: During the code generation phase, multi-threading technology is used to perform parallel AST processing on the group of independent modules, which significantly improves code generation efficiency.
[0039] Dual verification mechanism: The generated code is checked against the MISRA-C specification and verified for mapping consistency to ensure code quality and consistency between the model and the code.
[0040] Beneficial effects: Improving mapping synchronization efficiency: The one-way mapping synchronization mechanism enables the rapid location and updating of the corresponding AST node when the model changes. For example, when the output type of a constant module changes, the corresponding AST node can be directly found and the variable type updated through ASTNode references, reducing unnecessary dependency checks and data synchronization operations and improving synchronization efficiency.
[0041] Enhanced logical verification capabilities: The pre-mapping verification stage uses a depth-first search algorithm to perform logical topology verification and metadata consistency verification, which can detect problems such as circular dependencies, broken circuits, and type mismatches in module connections in advance.
[0042] Improve code generation efficiency: Parallel AST processing utilizes multi-threading technology to generate dependency-free module groups in parallel, fully leveraging the performance of multi-core processors.
[0043] In the mapping object generation stage, in addition to graphical drag-and-drop and connection methods, text input is also supported to define modules and connection relationships to meet the modeling habits of different users.
[0044] In parallelized AST processing, in addition to using multi-threading technology, distributed computing technology is also considered to distribute the independent module groups to different computing nodes for processing, thereby improving processing efficiency.
[0045] The above specific embodiments further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A code generation method based on unidirectional mapping synchronization and incremental update, characterized in that, The code generation method includes: Generate a mapping object, including: Users can build simulation models and construct layered modular model objects by using graphical modeling tools to visually drag and drop and connect lines. The hierarchical modular model object is organized in a tree structure, with each module as a node, containing input and output ports and internal logical function attributes. The modules form a logical network through port connections. Before constructing the mapping relationship, the generated model objects and metadata are validated, including: Logical topology verification: A depth-first search algorithm is used to traverse the hierarchical structure of the model objects to check for circular dependencies, broken circuits, disallowed modules, and logical errors in unconnected modules, ensuring that the data flow between modules is correct; Metadata consistency verification: Compare the metadata declared for each module's ports with the actual connection ports to ensure correctness; Establish a synchronization relationship between AST generation and one-way mapping, including: The core processing layer's AST generator is based on the hierarchical structure of model objects and uses a depth-first traversal strategy for generation. Starting from the root module, it processes each module sequentially according to the hierarchical order, creating a corresponding AST subtree for the current module based on its logic. Module structure information is added to the AST node attribute fields, an ASTNode reference is added to each module node, and a module reference for the corresponding module object is added to each AST node. Next, it checks whether the current module has submodules. If it does, each submodule is processed in the same way in sequence, and the AST returned by each submodule is merged into the current parent AST structure as a child node. This process continues until all submodules of the model object have been traversed, and the AST structure is generated by combining them. Model changes are synchronized with incremental AST, including: when any module in the model is changed, the corresponding AST node is found based on the ASTNode reference, and the update method is called synchronously to update it; After the one-way mapping synchronization relationship is established, the code generation stage begins; The specific methods used in the code generation phase include: The code generation layer uses a depth-first traversal algorithm to traverse the entire AST node based on the module interface metadata and AST structure. Starting from the root node, it generates corresponding C language code for each node based on the saved module information and code template engine. The generated code and mapping relationship are double-verified.
2. The code generation method based on unidirectional mapping synchronization and incremental update according to claim 1, characterized in that, The code generation phase employs topology optimization and conflict detection, specifically including: The module-connected DAG graph is topologically sorted based on the improved Kahn algorithm. During the node traversal, the correctness of the data type and dimension of the input and output ports is verified in real time. Modules with mismatched types are recorded and the problematic modules and their connecting edges are highlighted in the graphical interface.
3. The code generation method based on unidirectional mapping synchronization and incremental update according to claim 1, characterized in that, The code generation phase employs parallelized AST processing, specifically including: The identification module connects to the independent module group in the DAG graph and uses multi-threading technology to generate the corresponding AST subtree in parallel.
4. The code generation method based on unidirectional mapping synchronization and incremental update according to claim 1, characterized in that, The dual verification of the generated code and mapping relationship specifically includes: MISRA-C Check: The generated C code is checked for compliance with the MISRA-C standard using a syntax checking tool. Mapping consistency verification: By traversing the AST nodes and model object nodes, the state information of the two is compared to check whether the code generation process has generated errors. If inconsistencies are found, the difference modules and nodes are marked and a report is generated.
5. The code generation method based on unidirectional mapping synchronization and incremental update according to claim 1, characterized in that, The module structure information specifically includes: module ID, port definition, and hierarchical path.
