A code library UML diagram automatic generation method and system based on abstract syntax tree and large language model cooperation
By using AST and LLM in a collaborative process, the problems of multilingual support, performance bottlenecks and semantic understanding in UML diagram generation in existing technologies are solved, and efficient and high-quality automatic generation of UML diagrams is achieved, which is suitable for complex software architectures.
Patent Information
- Application Number
- CN202610863790.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-15
- Publication Date
- 2026-08-25
AI Technical Summary
Existing technologies have problems in generating UML diagrams, such as insufficient multi-language support, performance bottlenecks when processing large-scale code, lack of semantic understanding and information overload, and inability to identify design intent. As a result, the generated UML diagrams are of low quality and difficult to adapt to complex software architectures.
We employ a collaborative approach of Abstract Syntax Tree (AST) and Large Language Model (LLM). By parsing the AST, we extract the structured skeleton information of the codebase, compress it into high-density data, input it into the LLM for semantic analysis, generate UML diagram text that conforms to PlantUML or Mermaid description languages, and finally output a visual chart by the rendering engine. We process large-scale code in blocks and ensure quality through closed-loop iteration.
It enables unified UML diagram generation for multi-language projects, improves the efficiency of large-scale code processing, and generates UML diagrams with architectural semantic insights, supports version control and automated updates, and outputs high-quality diagrams.
Smart Images

Figure CN122633240A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software engineering technology, specifically to a method and system for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models. Background Technology
[0002] Unified Modeling Language (UML) is a standardized modeling language widely used in software engineering. It is used to visually describe the structure, behavior, and interaction relationships of software systems, and is an important tool for architecture design, technical review, team communication, and documentation maintenance. With the continuous growth in the scale and complexity of software systems, the need to automatically generate UML diagrams from existing codebases (i.e., code reverse engineering) is becoming increasingly urgent. Early code reverse engineering tools (such as RationalRose, Together, and EnterpriseArchitect) adopted a static parsing approach based on syntax rules. They scanned the source code file by file using predefined rule sets and extracted structural information such as classes, interfaces, and inheritance relationships, mapping it to UML class diagrams, sequence diagrams, and other diagrams. This approach achieved certain results in single-language, small-scale projects. However, with the widespread adoption of multi-language hybrid development, microservice architecture, and various design patterns in modern software projects, the limitations of traditional rule-based static parsing methods in cross-language support, semantic understanding, and large-scale code processing have become increasingly apparent. Meanwhile, Abstract Syntax Tree (AST) parsing technology has matured, and major programming languages provide high-quality AST parsing libraries capable of accurately extracting the structured skeleton information of the source code. In recent years, Large Language Models (LLMs) have demonstrated powerful semantic analysis capabilities in code understanding and generation, providing new technical pathways for code reverse engineering. The widespread adoption of text-based UML description languages such as PlantUML and Mermaid has also provided a standardized intermediate representation layer for the automated generation and rendering of UML diagrams. However, how to effectively coordinate precise AST parsing, large language model semantic analysis, and professional UML rendering engines to achieve the automatic generation of high-quality UML diagrams for large-scale codebases remains a pressing technical challenge in this field.
[0003] Currently, there are various technical solutions for generating UML diagrams from code repositories in the industry, mainly including the following categories: (1) Reverse engineering tools based on IDE plugins. Commercial tools such as EnterpriseArchitect, VisualParadigm, and StarUML, as well as the built-in UML plugin of IntelliJ IDEA, extract structural information such as classes, interfaces, inheritance, and associations by performing static syntax parsing on the source code and mapping it to UML class diagrams. These tools usually have built-in parsing engines for specific programming languages (such as Java, C++, and C#), which scan the source code file by file through predefined syntax rules and map the identified type definitions, method signatures, field declarations, etc., to UML diagram elements. (2) Parsing tools based on compiler front-ends. Doxygen combined with Graphviz can extract call relationships and dependencies from the source code of languages such as C / C++ and Java and generate class diagrams and call diagrams; Pyreverse (Pylint component) generates class diagrams and package dependency diagrams for Python code. These tools use compiler front-ends or language-specific static analysis frameworks to extract code structure information, and the generated diagrams are output in image format. (3) Code analysis solutions based on large language models. In recent years, some research and tools have attempted to directly input source code into large language models such as GPT and Claude, and generate text descriptions of UML diagrams (such as PlantUML or Mermaid format) after the models understand the semantics of the code. Such solutions utilize the natural language understanding and code understanding capabilities of large language models, which to some extent makes up for the lack of semantic understanding in traditional rule engines. (4) Textual UML description tools such as PlantUML and Mermaid. PlantUML is an open-source tool that defines UML diagrams through a concise text description language and automatically generates visual diagrams by a rendering engine. It supports various UML diagram types such as class diagrams, sequence diagrams, activity diagrams, and component diagrams. Mermaid is a lightweight text diagram description language based on JavaScript. Its syntax is concise and intuitive. It natively supports various diagram types such as class diagrams, sequence diagrams, flowcharts, state diagrams, and ER diagrams, and is natively integrated and rendered by mainstream platforms such as GitHub, GitLab, and Markdown editors. The textual description format of such textual UML description tools is naturally suitable for version control and automated pipeline integration, and has been widely used in software documentation automation scenarios.
[0004] Although various technical solutions for generating UML diagrams from codebases have emerged in the industry, including tools based on static rule parsing, IDE integration plugins, and online reverse engineering platforms, existing technologies still have the following significant drawbacks and shortcomings: First, traditional code reverse engineering tools for generating UML diagrams (such as EnterpriseArchitect, VisualParadigm, and StarUML) employ a static parsing approach based on hard-coded rules. This requires writing and maintaining a separate set of parsing rules for each programming language. When the codebase involves multiple programming languages, the parsers for each language lack a unified semantic understanding capability, failing to automatically identify cross-language module call relationships and dependency paths. The generated UML diagrams can only reflect the local structure within a single language. Furthermore, hard-coded rules struggle to adapt to language version iterations and the introduction of new syntax features, and their parsing capabilities for advanced features in dynamically typed languages, such as duck typing, decorators, and metaprogramming, are insufficient.
[0005] Secondly, existing tools face performance bottlenecks and information overload issues when processing large-scale codebases. Traditional solutions employ a full-file parsing strategy, loading and parsing all source files in the codebase at once. When the codebase reaches hundreds of thousands of lines, the parsing time increases significantly. More critically, the UML diagrams generated by full-file parsing include all classes, interfaces, and their relationships, resulting in extremely large and complex diagrams with densely overlapping nodes and intertwined lines, severely impacting readability. Existing tools lack intelligent filtering mechanisms based on code structure hierarchy, failing to automatically select key files and modules for targeted parsing based on the analysis objectives, resulting in an extremely low signal-to-noise ratio.
[0006] Third, existing tools can only extract explicit structural information from the code (such as class inheritance, interface implementation, method signatures, etc.), and cannot understand the design intent and architectural patterns of the code. For example, traditional tools cannot automatically infer the application of design patterns such as the factory pattern and strategy pattern; for microservice architecture projects, they cannot identify cross-service interaction topologies such as API call relationships between services and message queue communication links. The generated UML diagrams remain at the level of mechanical mapping of code structure, lacking a deep understanding of the semantics of software architecture, and are difficult to provide effective support for architecture review and system refactoring.
[0007] Fourth, existing solutions fail to effectively combine precise code structure extraction with intelligent semantic analysis. Traditional rule-driven tools lack semantic understanding capabilities; while recent solutions based on large language models to directly analyze source code and generate UML diagrams suffer from limited context window length, making it impossible to process all source files of a large-scale codebase at once. Furthermore, directly using the original source code as input results in significant token waste—redundant information such as comments, blank lines, and formatting characters occupy a large amount of context window space, leading to low effective information density. Current technologies have not yet proposed a phased collaborative processing method that first extracts the skeleton structure information of the codebase (such as file trees, class hierarchies, module dependencies, etc.) using precise methods like Abstract Syntax Trees (ASTs), then inputs the structured and refined high-density information into a large language model for semantic analysis and UML diagram generation, and finally uses a professional rendering engine (such as PlantUML, Mermaid, etc.) to transform the description into a standardized visual table. Summary of the Invention
[0008] To address the shortcomings of existing codebase UML diagram generation technologies in multilingual support, large-scale code processing, semantic understanding, and structured refinement, this invention proposes an automatic codebase UML diagram generation method and system based on the collaboration of Abstract Syntax Tree (AST) and a large language model. The aim is to achieve the following: Accurately extract the skeletal information of the codebase, such as the file tree structure, class hierarchy, and module dependencies, through AST parsing; input the structured and refined high-density data into a large language model for semantic analysis; generate UML diagram description text conforming to the syntax specifications of textual UML description languages such as PlantUML and Mermaid; and finally, convert the description text into a standardized visual UML diagram using a corresponding rendering engine (such as PlantUML or Mermaid). This forms a three-stage collaborative processing flow: "accurate AST extraction → semantic analysis of the large language model → textual UML description rendering output," thus solving the problems mentioned in the background.
[0009] The key technical points and protection points of this invention are as follows: 1. A method for generating UML diagrams for codebases that facilitate collaboration between AST and large language models. A method for automatically generating UML diagrams from codebases uses the structured code skeleton data output by AST parsing as the sole information carrier between the AST parsing layer and the large language model. Specifically, the AST parser corresponding to each programming language performs syntax parsing on the codebase source files, extracting structured skeleton information such as file tree structure, class definitions, method signatures, inheritance and implementation relationships, module import / export, and function call chains, and outputs it in a language-independent unified format (such as JSON). The large language model does not directly receive the original source code, but only performs semantic analysis based on the structured skeleton data, outputting UML diagram description text that conforms to the syntax specifications of text-based UML description languages such as PlantUML and Mermaid. Finally, a matching rendering engine outputs a visual UML diagram. This collaborative mechanism enables the two technologies to form an inseparable complementary relationship—AST is responsible for accurate structure extraction and information refinement, while the large language model is responsible for semantic understanding and design pattern recognition. The two are bridged by structured skeleton data, achieving technical effects that neither can achieve alone.
[0010] 2. A phased collaborative processing architecture for accurate AST extraction and semantic analysis of large language models The UML diagram generation process is divided into three stages: "AST structure extraction → large language model semantic analysis → text-based UML description rendering output". Each stage has a clear responsibility and works in concert. The AST parsing layer is responsible for accurate structure extraction and information compression, the large language model is responsible for semantic understanding and the generation of text-based UML descriptions such as PlantUML or Mermaid, and the matching rendering engine is responsible for standardized diagram output. The three form a complementary and collaborative processing link.
[0011] 3. Chunk commit and subgraph merging strategies for large-scale codebases To address the issue of large-scale codebase skeleton data exceeding the context window limit of a large language model, the skeleton data is split into multiple sub-blocks by module or directory dimension. These sub-blocks are submitted to the large language model in batches to generate sub-graph descriptions, and then a merging module integrates these sub-graph descriptions into a complete UML description text (PlantUML or Mermaid format). This strategy enables the system to handle codebases of any size.
[0012] 4. A closed-loop iterative mechanism based on text-based UML description languages (PlantUML / Mermaid) for generation, verification, and correction. The PlantUML or Mermaid description text generated by the large language model undergoes syntax validation by the matching rendering engine. If the validation fails, the error information is fed back to the large language model for correction, forming a closed-loop iteration until a grammatically compliant and correctly rendered UML diagram is output. This mechanism ensures the quality and reliability of the output diagram.
[0013] To achieve the above objectives, the present invention provides the following technical solution: An automatic UML diagram generation system for codebases based on the collaboration of Abstract Syntax Tree (AST) and Large Language Model (MLM) includes an AST parsing and structure extraction layer, a MLM semantic analysis layer, and a textual UML description rendering output layer. The AST parsing and structure extraction layer is responsible for parsing the AST of source files in the codebase and extracting the skeleton structure information of the codebase. The MLM semantic analysis layer receives the structured code skeleton data output by the AST parsing layer and submits the structured data to the MLM for semantic analysis through carefully designed prompts. The textual UML description rendering output layer receives the UML description text generated by the MLM.
[0014] As a further aspect of the present invention: the AST parsing and structure extraction layer performs syntax parsing on the source file using the AST parser corresponding to each programming language, and extracts structured information such as file tree structure, class definition, method signature, module import-export relationship and function call chain.
[0015] As a further aspect of the present invention: the large language model semantic analysis layer includes a prompt word engineering module, a large language model, and a subgraph merging module.
[0016] As a further aspect of the present invention: the large language model, based on an understanding of software engineering knowledge, design patterns, and architectural paradigms, performs deep semantic analysis on code skeleton data, identifies design patterns, infers business logic relationships between modules, summarizes system architecture layers, and generates UML diagram description text that conforms to the syntax specifications of textual UML description languages such as PlantUML and Mermaid.
[0017] As a further aspect of the present invention: the prompt word engineering module is responsible for dynamically assembling prompt word templates according to the UML diagram type and analysis scope specified by the user, embedding structured data into the prompt word context and submitting it to the large language model.
[0018] The method for automatically generating UML diagrams based on the collaboration of abstract syntax trees and large language models in a codebase is as follows: Step 1: Users specify the target code repository path through the system interface, select the type of UML diagram to be generated, and optionally configure the analysis scope; Step 2: The system scans the code repository directory structure, automatically identifies the programming language type of each source file, and constructs a complete file tree; Step 3: The prompt word engineering module selects the corresponding template from the prompt word template library according to the UML diagram type selected by the user, embeds the structured skeleton data output in Step 2 into the prompt word context, assembles it into a complete prompt word, and submits it to the large language model; Step 4: After receiving the prompt words, the large language model performs semantic analysis on the structured skeleton data based on its understanding of software engineering knowledge, identifies design patterns, infers business relationships between modules, summarizes architectural levels, and generates UML diagram description text that conforms to the syntax specification of the textual UML description language selected by the user. Step 5: Textualized UML Description The rendering layer receives the description text and schedules the corresponding syntax validator to perform syntax verification based on the syntax type used in the description text.
[0019] As a further aspect of the present invention: In step two, for each source file, the corresponding language's AST parser is called to perform syntax parsing, extracting structural information such as class definitions, method signatures, inheritance relationships, interface implementations, module imports and exports, and function call chains. The parsing results are output in a unified structured format (JSON) to form the skeleton description data of the code library.
[0020] As a further aspect of the present invention: in step two, redundant information irrelevant to the generation of UML diagrams, such as comments, blank lines, formatting characters, and specific implementation logic, in the source code is filtered out, and only skeleton information directly related to the code structure and module relationships is retained. The information compression ratio can reach 10% to 20% of the original code volume.
[0021] As a further aspect of the present invention: In step three, for large-scale codebases, if the skeleton data exceeds the context window limit of the large language model, the system automatically executes a block-splitting strategy: the skeleton data is split into multiple sub-blocks according to the module or directory dimension, submitted to the large language model in batches to generate sub-graph descriptions respectively, and finally the sub-graph merging module integrates the sub-graph descriptions into a complete UML description text.
[0022] As a further aspect of the present invention: if the verification in step five passes, the matching rendering engine is called to generate a visual UML diagram in PNG, SVG, or other formats and return it to the user; if the verification fails, the error information is fed back to the large language model for correction, forming a closed-loop iteration of "generation → verification → correction" until a compliant UML diagram is output.
[0023] Compared with the prior art, the beneficial effects of the present invention are: This invention extracts structured skeleton information of the codebase through AST parsing instead of directly processing the original source code, compressing the information volume to 10% to 20% of the original code volume, which greatly improves the effective information density of the input large language model, enabling a larger codebase to be covered within a limited context window, and solving the problem that existing large language model solutions cannot handle large codebases due to token waste. This invention performs semantic analysis on structured skeleton data using a large language model, enabling automatic identification and expression of design patterns, architectural layers, and business relationships between modules. The generated UML diagram has architectural semantic insight, breaking through the limitation of traditional rule engines that can only perform mechanical mapping of code structure. This invention's AST parsing naturally supports multiple programming languages, and the parsing results of each language are uniformly output in a standardized structured format. Combined with the cross-language understanding capability of the large language model, it realizes the unified generation of UML diagrams for multi-language mixed projects, solving the problem of fragmented parsers for different languages in traditional tools. This invention uses text-based UML description languages such as PlantUML and Mermaid as intermediate description layers. The generated UML description text naturally supports version control and difference comparison, which facilitates integration into CI / CD pipelines to achieve automated updates of UML diagrams. The system of this invention supports the pluggable access of multiple rendering engines. Users can choose PlantUML (suitable for complex and professional UML modeling scenarios) or Mermaid (suitable for lightweight document collaboration scenarios such as GitHub / Markdown) according to the actual scenario. The corresponding rendering engines work together to ensure the standardization and high quality of the output diagrams. Attached Figure Description
[0024] Figure 1 This is the overall architecture diagram of a system for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models.
[0025] Figure 2 This is a flowchart of a method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models. Detailed Implementation
[0026] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0027] Please see Figures 1-2In this embodiment of the invention, an automatic generation system for UML diagrams of a codebase based on the collaboration of abstract syntax trees and large language models includes an AST parsing and structure extraction layer, a large language model semantic analysis layer, and a textualized UML description rendering output layer. The AST parsing and structure extraction layer is responsible for parsing the abstract syntax trees of source files in the codebase and extracting the skeleton structure information of the codebase. Specifically, it includes: parsing the source files using AST parsers corresponding to various programming languages (such as the ast module for Python, @typescript-eslint / parser for JavaScript / TypeScript, JavaParser for Java, go / ast for Go, etc.), extracting structured information such as file tree structure (directory hierarchy, file name, file type), class definition (class name, parent class, implemented interface), method signature (method name, parameter list, return type), module import and export relationships, and function call chains. The AST parsing and structure extraction layer outputs refined structured data (such as a JSON-formatted code skeleton description) rather than the original source code text, thereby significantly compressing the information volume and increasing the effective information density. The large language model semantic analysis layer includes a prompt engineering module, a large language model, and a subgraph merging module. This layer receives structured code skeleton data output from the AST parsing layer and submits it to the large language model for semantic analysis using carefully designed prompts. Based on its understanding of software engineering knowledge, design patterns, and architectural paradigms, the large language model performs deep semantic analysis on the code skeleton data, identifying design patterns (such as factory pattern, observer pattern, strategy pattern, etc.), inferring business logic relationships between modules, summarizing system architecture layers, and generating UML diagram description text conforming to the syntax specifications of text-based UML description languages such as PlantUML and Mermaid (the specific description language type is pre-selected by the user in the system or specified by the system according to a preset strategy). The prompt engineering module is responsible for dynamically assembling prompt templates according to the user-specified UML diagram type (class diagram, sequence diagram, component diagram, activity diagram, etc.) and analysis scope, embedding structured data within the prompt context, and submitting it to the large language model. The text-based UML description rendering output layer receives UML description text (PlantUML or Mermaid format) generated by the large language model. First, it selects a corresponding syntax validator based on the syntax type used in the description text to perform syntax compliance checks. Non-compliant descriptions are automatically corrected or fed back to the large language model for regeneration. Then, it calls a matching rendering engine (PlantUML rendering engine or Mermaid rendering engine) to convert the text description into standardized UML visual tables in formats such as PNG and SVG. The text-based UML description rendering output layer supports pluggable extensions of the rendering engine, facilitating the subsequent integration of other text-based UML description tools. The method for automatically generating UML diagrams based on the collaboration of abstract syntax trees and large language models in a codebase is as follows: Step 1: Users specify the target code repository path (supports local directories and Git remote repository addresses) through the system interface, select the type of UML diagram to be generated (such as class diagram, component diagram, sequence diagram, etc.), and optionally configure the analysis scope (specify directory, file matching mode, exclusion rules, etc.).
[0028] Step Two: The system scans the code repository directory structure, automatically identifies the programming language type of each source file, and constructs a complete file tree. For each source file, the corresponding language's AST parser is invoked for syntax analysis, extracting structural information such as class definitions, method signatures, inheritance relationships, interface implementations, module imports and exports, and function call chains. The parsing results are output in a unified structured format (JSON), forming the skeleton description data of the code repository. This step filters out redundant information irrelevant to UML diagram generation, such as comments, blank lines, formatting characters, and specific implementation logic, retaining only the skeleton information directly related to the code structure and module relationships. The information compression ratio can reach 10% to 20% of the original code volume.
[0029] Step 3: The prompt word engineering module selects the corresponding template from the prompt word template library based on the user-selected UML diagram type. It then embeds the structured skeleton data output in Step 2 into the prompt word context, assembling it into a complete prompt word before submitting it to the large language model. For large-scale codebases, if the skeleton data exceeds the context window limit of the large language model, the system automatically executes a chunking strategy: splitting the skeleton data into multiple sub-chunks by module or directory dimension, submitting them to the large language model in batches to generate sub-diagram descriptions, and finally, the sub-diagram merging module integrates the sub-diagram descriptions into a complete UML description text (PlantUML or Mermaid format).
[0030] Step 4: After receiving the prompt words, the large language model performs semantic analysis on the structured skeleton data based on its understanding of software engineering knowledge, identifies design patterns, infers business relationships between modules, summarizes architectural levels, and generates UML diagram description text that conforms to the syntax specifications of the user-selected textual UML description language (PlantUML or Mermaid, etc.).
[0031] Step 5: Textualized UML Description. The rendering layer receives the description text and, based on the syntax type (PlantUML or Mermaid) used in the description text, schedules the corresponding syntax validator to perform syntax validation. If the validation passes, it calls the matching rendering engine (PlantUML rendering engine or Mermaid rendering engine) to generate a visual UML diagram in PNG, SVG, or other formats and returns it to the user. If the validation fails, the error information is fed back to the large language model for correction, forming a closed-loop iteration of "generation → validation → correction" until a compliant UML diagram is output. The overall workflow of the above method steps is as follows: ① (User specifies code library and chart type) → ② (AST parsing to extract structured skeleton data) → ③ (Cue word assembly and segmentation strategy) → ④ (Semantic analysis of large language model to generate PlantUML or Mermaid description) → ⑤ (Matching rendering engine verification and output of visualization chart).
[0032] This embodiment uses a Java microservice project of an internet company as an application scenario. The company uses the SimpleCloud ezCode one-stop DevOps platform for R&D management. The core business system hosted on the platform adopts a SpringBoot + SpringCloud microservice architecture, containing over 80 microservice modules and approximately 1.2 million lines of Java code. Due to the increasing complexity of the system architecture, the architect needs to regularly generate system-level UML class diagrams and component diagrams for architecture reviews. The specific implementation steps are as follows: Step 1: Users can use the UML diagram generation function interface of the Simple Cloud ezCode platform to specify the target code repository address, select to generate "class diagram" and "component diagram", and configure the analysis scope to be the core business module directory (package paths such as service, controller, domain, etc. under src / main / java), excluding test code and third-party dependencies.
[0033] Step Two: The system scans the specified directory, identifies all Java source files, and calls the JavaParserAST parser to perform syntax parsing on each .java file, extracting structural information such as class name, package path, parent class, list of implemented interfaces, field definitions (type and name), method signature (method name, parameter type list, return type), annotation information (such as Spring annotations like @RestController, @Service, @Autowired, @FeignClient, etc.), and import statements. The parsing results are output in a unified JSON format. Taking a core module containing 800 Java classes as an example, the original source code is approximately 150,000 lines, and the structured skeleton data extracted by AST is approximately 18,000 lines of JSON, with an information compression ratio of approximately 12%.
[0034] Step 3: The prompt word engineering module loads the class diagram from the template library to generate a dedicated prompt word template based on the user-selected "class diagram" type. The template includes PlantUML class diagram syntax specifications (PlantUML is used as the description language in this embodiment; if Mermaid is selected, Mermaid classDiagram syntax specifications are loaded), design pattern recognition instructions, and Spring annotation semantic mapping rules (e.g., classes annotated with @Service are classified as service layers, classes annotated with @RestController are classified as controller layers, and interfaces annotated with @FeignClient are identified as remote call relationships). The system divides the JSON skeleton data into blocks according to microservice modules, with each block controlled within 60% of the large language model context window, and submits them to the large language model in batches.
[0035] Step 4: The large language model analyzes each batch of skeleton data, automatically identifying the application of design patterns such as the factory pattern and strategy pattern, inferring the REST call relationships between microservices through FeignClient, and organizing classes into logical groups according to business domains (such as user domain, order domain, payment domain), generating PlantUML class diagram description text. The merging module integrates the sub-diagram descriptions of each batch into a complete system-level class diagram description.
[0036] Step 5: Text-based UML Description. The rendering layer performs syntax validation on the merged description text. Once validation is successful, it calls the PlantUML rendering engine to generate an SVG-formatted UML class diagram. Architects can view the generated class diagram online through the Simple Cloud ezCode platform for use in architecture review meetings.
[0037] This example uses a full-stack project from a product team as its application context. The project's backend uses Python (FastAPI framework), and the frontend uses TypeScript (React framework), with the code hosted in a general Git repository. The team needs to generate a system component diagram to clarify the interaction between the frontend and backend. The specific implementation steps are as follows: Step 1: The user specifies the code repository, selects to generate a "component graph", and the analysis scope covers the backend / and frontend / src / directories.
[0038] Step 2: The system automatically identifies .py files under backend / and .ts / .tsx files under frontend / src / . For Python files, it uses the Pythonast module to parse them, extracting class definitions, function definitions, FastAPI route decorators (@app.get, @app.post, etc.), Pydantic model definitions, etc. For TypeScript files, it uses @typescript-eslint / parser to parse them, extracting React component definitions, interface definitions, API call functions (such as fetch, axios call paths), Reduxstore structure, etc. The parsing results for both languages are output as JSON skeleton data in the same format.
[0039] Step 3: The prompt word engineering module loads the component graph template, merges the skeleton data of the Python backend and TypeScript frontend into the prompt word context, and explicitly instructs the large language model to recognize the interaction relationship between the frontend and backend through the REST API.
[0040] Step 4: After analyzing the skeleton data using the large language model, the corresponding relationship between the front-end React components calling the back-end Fast API routes through axios is identified. The system is then divided into components such as the front-end presentation layer, API gateway layer, back-end service layer, and data access layer, generating Mermaid component flowchart description text.
[0041] Step 5: The Mermaid rendering engine performs syntax validation on the description text and generates a component diagram. Since Mermaid description text can be directly embedded into Markdown documents such as README.md and is natively rendered by mainstream code hosting platforms and Markdown editors, the team can directly archive the generated results into the project documentation. This diagram provides the team with a clear understanding of the front-end and back-end API connections and the system's layered architecture.
[0042] Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art can still modify the technical solutions described in the foregoing embodiments or make equivalent substitutions for some of the technical features. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. An automatic UML diagram generation system for codebases based on the collaboration of Abstract Syntax Tree (AST) and Large Language Model (MLM), comprising an AST parsing and structure extraction layer, a large language model semantic analysis layer, and a textualized UML description rendering output layer, characterized in that: The AST parsing and structure extraction layer is responsible for parsing the source files in the code library using abstract syntax trees and extracting the skeleton structure information of the code library. The large language model semantic analysis layer is responsible for receiving the structured code skeleton data output by the AST parsing layer and submitting the structured data to the large language model for semantic analysis through carefully designed prompts. The textualized UML description rendering output layer is responsible for receiving the UML description text generated by the large language model.
2. The automatic generation system for UML diagrams of a codebase based on the collaboration of abstract syntax trees and large language models as described in claim 1, characterized in that: The AST parsing and structure extraction layer uses the AST parser corresponding to each programming language to perform syntax parsing on the source file and extract the file tree structure, class definition, method signature, module import and export relationship and function call chain structured information.
3. The automatic generation system for UML diagrams of a codebase based on the collaboration of abstract syntax trees and large language models as described in claim 1, characterized in that: The semantic analysis layer of the large language model includes a prompt word engineering module, a large language model, and a subgraph merging module.
4. The automatic generation system for code library UML diagrams based on the collaboration of abstract syntax trees and large language models as described in claim 3, characterized in that: The large language model is based on an understanding of software engineering knowledge, design patterns, and architectural paradigms. It performs deep semantic analysis on code skeleton data, identifies design patterns, infers business logic relationships between modules, summarizes system architecture layers, and generates UML diagram description text that conforms to the syntax specifications of PlantUML and Mermaid textual UML description languages.
5. The automatic generation system for UML diagrams of a codebase based on the collaboration of abstract syntax trees and large language models as described in claim 3, characterized in that: The prompt word engineering module is responsible for dynamically assembling prompt word templates based on the UML diagram type and analysis scope specified by the user, embedding structured data into the prompt word context, and submitting it to the large language model.
6. The method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models as described in claim 1, characterized in that: The method and steps are as follows: Step 1: Users specify the target code repository path through the system interface, select the type of UML diagram to be generated, and optionally configure the analysis scope; Step 2: The system scans the code repository directory structure, automatically identifies the programming language type of each source file, and constructs a complete file tree; Step 3: The prompt word engineering module selects the corresponding template from the prompt word template library according to the UML diagram type selected by the user, embeds the structured skeleton data output in Step 2 into the prompt word context, assembles it into a complete prompt word, and submits it to the large language model; Step 4: After receiving the prompt words, the large language model performs semantic analysis on the structured skeleton data based on its understanding of software engineering knowledge, identifies design patterns, infers business relationships between modules, summarizes architectural levels, and generates UML diagram description text that conforms to the syntax specification of the textual UML description language selected by the user. Step 5: Textualized UML Description The rendering layer receives the description text and schedules the corresponding syntax validator to perform syntax verification based on the syntax type used in the description text.
7. The method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models as described in claim 6, characterized in that: In step two, for each source file, the corresponding language's AST parser is called to perform syntax parsing, extracting class definitions, method signatures, inheritance relationships, interface implementations, module imports and exports, and function call chain structure information. The parsing results are output in a unified structured format (JSON) to form the skeleton description data of the code library.
8. The method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models as described in claim 6, characterized in that: In step two, comments, blank lines, formatting characters, and redundant information in the source code that is irrelevant to the generation of the UML diagram are filtered out, and only skeleton information directly related to the code structure and module relationships is retained. The information compression ratio can reach 10% to 20% of the original code volume.
9. The method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models as described in claim 6, characterized in that: In step three, for large-scale codebases, if the skeleton data exceeds the context window limit of the large language model, the system automatically executes a chunking strategy: the skeleton data is split into multiple sub-chunks by module or directory dimension, submitted to the large language model in batches to generate sub-diagram descriptions respectively, and finally the sub-diagram merging module integrates the sub-diagram descriptions into a complete UML description text.
10. The method for automatically generating UML diagrams for codebases based on the collaboration of abstract syntax trees and large language models as described in claim 6, characterized in that: If the verification passes in step five, the matching rendering engine is called to generate a visual UML diagram in PNG or SVG format and returned to the user; if the verification fails, the error message is fed back to the large language model for correction, forming a closed-loop iteration of "generation → verification → correction" until a compliant UML diagram is output.