Code generation method, device, equipment and computer program product

By semantic analysis and iteratively generating the system architecture of the original code snippet, the problem of lack of contextual correlation of code generation code is solved, and system code with semantic consistency and logical correlation is generated.

CN120447910APending Publication Date: 2025-08-08KE COM (BEIJING) TECHNOLOGY CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510591918.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-08
Publication Date
2025-08-08

AI Technical Summary

Technical Problem

In the prior art, the code generated by the code model lacks contextual relevance and logical consistency, and it is difficult to adapt to complex and real application scenarios.

Method used

By semantic analysis of the original code snippet, predict the target system, generate development requirements documents and system architecture, iterate the module code of each functional module until the preset iteration completion conditions are met, and the final system code is generated.

Benefits of technology

Ensure that the functional modules in the generated system code are related to each other, have semantic consistency and logical correlation, and improve the structural clarity, logic and diversity of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120447910A_ABST
    Figure CN120447910A_ABST
Patent Text Reader

Abstract

The invention provides a code generation method and device, equipment and a computer program product, and the method comprises the steps: carrying out the semantic analysis of an original code snippet, and predicting a target system to which the original code snippet belongs; generating a development demand document of the target system, wherein development demand information of the target system is recorded in the development demand document; generating a system architecture of the target system based on the development demand information of the target system; based on the system architecture of the target system, iteratively generating a module code of each functional module, and obtaining a system code of the current round of the target system; and iteratively executing the code generation process to realize code generation of the next round until a preset iteration completion condition is met, thereby obtaining a final system code of the target system. According to the technical scheme, the code of the target system can be iteratively generated on the basis of the original code snippets based on the working mode of the waterfall model, and the code with high complexity and complex dependency relationship can be generated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of artificial intelligence technology, and in particular to a code generation method, apparatus, device, and computer program product. Background Art

[0002] With the continuous development of software development, the technology of automatically generating program code has gradually become a research hotspot in the application field, and the technology of automatically generating code using large code models has emerged.

[0003] Large code models typically require extensive code datasets for training in order to learn the syntax, structure, coding style, and logic of a programming language. Related technologies, which rely on code snippet mutation, can generate large amounts of code. However, these generated codes lack contextual relevance and logical consistency, making it difficult to simulate the code structure and flow of real-world development. This makes it difficult for trained models to adapt to complex, real-world application scenarios.

[0004] Therefore, how to generate high-quality code with contextual relevance is a technical problem that needs to be solved urgently. Summary of the Invention

[0005] In order to solve the above technical problems, embodiments of the present disclosure provide a code generation method, apparatus, device, and computer program product.

[0006] According to a first aspect of an embodiment of the present disclosure, there is provided a code generation method, comprising:

[0007] Performing semantic analysis on the original code snippet to predict the target system to which the original code snippet belongs;

[0008] Generating a development requirement document for the target system, wherein the development requirement document records development requirement information for the target system;

[0009] Based on the development requirement information of the target system, a system architecture of the target system is generated, wherein the system architecture includes at least one of the following: multiple functional modules included in the target system, dependencies between the functional modules, and module requirement information and interface information of the functional modules;

[0010] Iteratively generate module codes for each functional module based on the system architecture of the target system to obtain the current round of system codes for the target system;

[0011] The above code generation process is iteratively executed to achieve the next round of code generation until the preset iteration completion condition is met, thereby obtaining the final system code of the target system.

[0012] According to a second aspect of an embodiment of the present disclosure, there is provided a code generation device, comprising:

[0013] A prediction module, configured to perform semantic analysis on the original code snippet and predict the target system to which the original code snippet belongs;

[0014] A requirement document generating module, configured to generate a development requirement document for the target system, wherein the development requirement document records the development requirement information of the target system;

[0015] a system architecture generation module, configured to generate a system architecture of the target system based on the development requirement information of the target system, wherein the system architecture includes at least one of the following: multiple functional modules included in the target system, dependencies between the functional modules, and module requirement information and interface information of the functional modules;

[0016] A code generation module, configured to iteratively generate module codes for each functional module based on the system architecture of the target system, to obtain the system code for the current round of the target system;

[0017] The iteration module is used to iteratively execute the above code generation process to achieve the next round of code generation until the preset iteration completion conditions are met to obtain the final system code of the target system.

[0018] According to a third aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided, wherein the storage medium stores computer program instructions, and when the computer program instructions are executed, the above-mentioned code generation method is implemented.

[0019] According to a fourth aspect of the embodiments of the present disclosure, there is provided an electronic device, comprising:

[0020] a memory for storing a computer program product;

[0021] The processor is configured to execute the computer program product stored in the memory, and when the computer program product is executed, the above-mentioned code generation method is implemented.

[0022] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, comprising computer program instructions, which implement the above-mentioned code generation method when executed by a processor.

[0023] Based on the above embodiments of the present disclosure, when code generation is required, the original code snippet can be semantically parsed to predict the target system to which the original code snippet belongs; a development requirement document for the target system is generated, wherein the development requirement document records the development requirement information of the target system; based on the development requirement information of the target system, a system architecture of the target system is generated, wherein the system architecture includes at least one of the following: multiple functional modules included in the target system, dependencies between the functional modules, and module requirement information and interface information of the functional modules; based on the system architecture of the target system, module code for each functional module is iteratively generated to obtain the current round of system code for the target system; the above code generation process is iteratively executed to implement the next round of code generation until the preset iteration completion condition is met to obtain the final system code for the target system. Therefore, the technical solution disclosed in the present invention can determine the target system to be generated based on the original code fragment, and generate a complete and coherent system code from the systematic method of deriving the requirements of the target system to the gradual implementation in accordance with the working mode of the waterfall model, and can ensure that the functional modules in the generated system code are interconnected, with semantic consistency and logical relevance; in addition, by iteratively executing the code generation process, it helps that each iteration round is progressively optimized based on the code of the previous round, ensuring the gradual improvement of the code complexity and logic, and improving the structural clarity, logic and diversity of the generated system code.

[0024] The technical solution of the present disclosure is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] The above and other purposes, features, and advantages of the present disclosure will become more apparent through a more detailed description of the embodiments of the present disclosure in conjunction with the accompanying drawings. The accompanying drawings are intended to provide a further understanding of the embodiments of the present disclosure and constitute a part of the specification. Together with the embodiments of the present disclosure, they are used to explain the present disclosure and do not constitute a limitation of the present disclosure. In the drawings, the same reference numerals generally represent the same components or steps.

[0026] Figure 1 A flowchart of an embodiment of the code generation method disclosed herein;

[0027] Figure 2 A flowchart of another embodiment of the code generation method disclosed herein;

[0028] Figure 3 This is a flowchart of an embodiment of code verification in the code generation method disclosed herein;

[0029] Figure 4 This is a schematic structural diagram of an embodiment of a code generation device disclosed herein;

[0030] Figure 5A schematic structural diagram of another embodiment of the code generation device disclosed herein;

[0031] Figure 6 It is a structural diagram of an electronic device provided by an exemplary embodiment of the present application. DETAILED DESCRIPTION

[0032] The exemplary embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all the embodiments of the present disclosure, and it should be understood that the present disclosure is not limited to the exemplary embodiments described herein.

[0033] It should be noted that the relative arrangement of components and steps, the numerical expressions and numerical values set forth in these embodiments do not limit the scope of the present disclosure unless specifically stated otherwise.

[0034] Those skilled in the art will understand that the terms "first" and "second" in the embodiments of the present disclosure are only used to distinguish different steps, devices or modules, and do not represent any specific technical meanings, nor do they indicate a necessary logical order between them.

[0035] It should also be understood that in the embodiments of the present disclosure, “plurality” may refer to two or more than two, and “at least one” may refer to one, two, or more than two.

[0036] It should also be understood that any component, data or structure mentioned in the embodiments of the present disclosure can generally be understood as one or more, unless explicitly limited or otherwise indicated in the context.

[0037] In addition, the term "and / or" in this disclosure merely describes an association between related objects, indicating that three possible relationships exist. For example, "A and / or B" can represent: A exists alone, A and B exist simultaneously, and B exists alone. Furthermore, the character " / " in this disclosure generally indicates that the related objects are in an "or" relationship.

[0038] It should also be understood that the description of the various embodiments in this disclosure focuses on the differences between the various embodiments, and the same or similar aspects thereof can be referenced with each other. For the sake of brevity, they will not be described one by one.

[0039] At the same time, it should be understood that for the convenience of description, the sizes of the various parts shown in the drawings are not drawn according to the actual proportional relationship.

[0040] The following description of at least one exemplary embodiment is merely illustrative in nature and is in no way intended to limit the present disclosure, its application, or uses.

[0041] Technologies, methods, and equipment known to ordinary technicians in the relevant art may not be discussed in detail, but where appropriate, the technologies, methods, and equipment should be considered part of the specification.

[0042] It should be noted that like reference numerals and letters refer to like items in the following figures, and therefore, once an item is defined in one figure, it need not be further discussed in subsequent figures.

[0043] Embodiments of the present disclosure may be applied to electronic devices such as terminal devices, computer systems, and servers, and may operate in conjunction with numerous other general-purpose or specialized computing system environments or configurations. Examples of well-known terminal devices, computing systems, environments, and / or configurations suitable for use with electronic devices such as terminal devices, computer systems, or servers include, but are not limited to, personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network personal computers, minicomputer systems, mainframe computer systems, and distributed cloud computing technology environments including any of the above systems.

[0044] Electronic devices such as terminal devices, computer systems, and servers can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Generally, program modules can include routines, programs, object programs, components, logic, data structures, etc., which perform specific tasks or implement specific abstract data types. Computer systems / servers can be implemented in a distributed cloud computing environment. In a distributed cloud computing environment, tasks can be performed by remote processing devices linked through a communication network. In a distributed cloud computing environment, program modules can be located on local or remote computing system storage media, including storage devices.

[0045] Overview of the Disclosure

[0046] In the process of implementing the present disclosure, the inventors found that when generating code samples for training large code models, it is difficult to obtain high-quality code data that matches the actual development specifications, and the richness and complexity of the code samples cannot be guaranteed. As a result, the generalization ability of the trained large code model is insufficient, and it is impossible to generate high-quality code for specific fields or specific tasks.

[0047] In the code generation method provided by the present disclosure, the original code fragment can be semantically parsed to predict the target system to which the original code fragment belongs; a development requirement document of the target system is generated, and the development requirement document records the development requirement information of the target system; based on the development requirement information of the target system, a system architecture of the target system is generated, and the system architecture includes at least one of the following: multiple functional modules contained in the target system, the dependency relationship between each functional module, and the module requirement information and interface information of each functional module; based on the system architecture of the target system, the module code of each functional module is iteratively generated to obtain the system code of the current round of the target system; the above code generation process is iteratively executed to realize the code generation of the next round until the preset iteration completion condition is met, and the final system code of the target system is obtained. Therefore, the technical solution of the present disclosure can determine the target system to be generated based on the original code fragment, and generate a complete and coherent system code from the systematic method of deriving the requirements of the target system to the step-by-step implementation according to the working mode of the waterfall model, which can ensure that the functional modules in the generated system code are interrelated and have semantic consistency and logical correlation.

[0048] In the embodiment of the present disclosure, the process of generating a system requirement document based on the original code fragment, then generating a system architecture based on the system requirement document, and iteratively generating system code based on the system architecture is a code generation process that utilizes the phased nature of the waterfall model in software development. The entire code generation process can be implemented by an agent. Among them, an agent refers to an agent that can perceive the environment and take actions to achieve specific goals. The agent designed by the technical solution of the present disclosure can implement the above-mentioned code generation method, expanding the original code fragment of a single function into a complete and coherent system code.

[0049] Exemplary Methods

[0050] Figure 1 FIG. 1 is a flow chart of an embodiment of the code generation method disclosed herein. Figure 1 The method shown includes steps 101 to 105. Each step is described below.

[0051] In step 101, semantic parsing is performed on the original code snippet to predict the target system to which the original code snippet belongs.

[0052] The original code snippet can be a code block in a system code, such as a code block corresponding to a user interface form component or a code block corresponding to a page frame. The target system can be a possible system predicted based on the original code snippet, such as a library management system or a user management system.

[0053] In the embodiments of the present disclosure, the original code snippets can be actual code written by developers, and can be obtained from original code obtained through systematic data collection on an open source platform. An open source platform refers to an online platform that provides functions such as open source software, code hosting, and community communication. The technical solution of the present disclosure can obtain multiple original code snippets from the open source platform based on the protocol provisions of the open source platform.

[0054] In the embodiment of the present disclosure, performing semantic analysis on the original code snippet may include performing semantic parsing on code data in the original code snippet, and may also include performing semantic parsing on comment data in the original code snippet.

[0055] In a specific implementation, an artificial intelligence large model (AI large model) can be used to predict the corresponding target system based on the original code snippet. The AI large model can be trained based on a large amount of code data samples and can generate the corresponding target system based on the original code snippet.

[0056] In step 102, a development requirement document of the target system is generated, in which development requirement information of the target system is recorded.

[0057] Among them, the development requirements document of the target system is a crucial document in the system development process. It records the development requirements information of the target system, including functional requirements information, non-functional requirements information, interface design requirements information, etc., which can provide guidance for subsequent system architecture design and code generation. Functional requirements information is used to indicate the functions to be implemented by the target system. For example, the functions to be implemented by the customer management system include the entry, query, update and deletion of new customer information. Non-functional requirements information is used to indicate some performance requirements that the target system needs to have, such as the response time to user request operations is less than 2 seconds and the ability to support multiple systems. Interface design requirements information is used to indicate the design requirements for the system interface, such as the design requirements for interface layout, font size, etc.

[0058] In the disclosed embodiment, a development requirement document for the target system can be automatically generated based on the system type of the target system. The development requirement document can be an initial requirement document, which only includes the basic functions that the target system needs to implement. For example, the library management system needs to have book query functions and new book upload functions.

[0059] This step can be implemented using a requirements document generation model. This model is a pre-trained AI model, a software module used to design and develop requirements documents based on system names or partial system functions. The model generates and outputs development requirements documents based on the target system's system type or name.

[0060] Specifically, a model prompt word may be generated based on the system type or system name of the target system, and the model prompt word is used to guide the requirement document generation model to complete the generation of the development requirement document.

[0061] The model prompt words include but are not limited to at least one of the following: task instructions, system name, system type, original code snippet (or the functional scope corresponding to the original code snippet). The task instruction is used to indicate the text or instructions that guide the requirement document generation model to generate the development requirement document. The system name and system type can represent the basic functions of the system. For example, the basic function of the user management system is to manage user information. The functional scope corresponding to the original code snippet includes the functions that the original code snippet has implemented. For example, the function corresponding to the original code snippet is the display function of book information.

[0062] For example, the model prompt words are as follows:

[0063] "Task instructions: You are an intelligent developer, responsible for determining the development requirements document for the target system based on the target system. The system is a library management system, the system type is information management, and the original code snippet is *****."

[0064] Optionally, a large amount of system development requirement information can be collected and counted. For example, user requirements and business requirements of multiple systems can be collected through interviews, questionnaires, observations, etc., and system development requirement documents can be pre-generated and stored based on the statistical results. Therefore, after generating code and determining the target system, the corresponding development requirement document can be obtained from the stored development requirement documents of multiple systems according to the name of the target system; or, the development requirement document of the target system can be generated by adjusting the stored development requirement documents of the same type as the target system.

[0065] In some implementations, the target system's development requirements document may not be unique. During the iterative generation of the target system's system code, the target system's development requirements document may be adjusted based on actual needs to produce a new version of the development requirements document. For example, after implementing the target system's basic user login functionality, a two-factor authentication mechanism may be added to this user login functionality.

[0066] In step 103 , a system architecture of the target system is generated based on the development requirement information of the target system.

[0067] The system architecture includes at least one of the following: multiple functional modules included in the target system, dependency relationships between the functional modules, and module requirement information and interface information of the functional modules.

[0068] In the embodiment of the present disclosure, when generating the system architecture of the target system based on the development requirement information of the target system, the system can be modularly designed according to the development requirement information, with each functional module implementing some specific functions separately or through the interaction of multiple modules, and the module requirement information of each functional module can be determined.

[0069] It can be understood that when determining the module requirement information of each functional module, for the functional module corresponding to the original code fragment, the module requirement information of the functional module corresponding to the original code fragment can be directly inferred based on the original module fragment as context information to ensure that the generated functional module can cover the functional scope of the original code fragment.

[0070] Furthermore, after determining the functional modules that should be possessed to implement the target system, the dependency relationships between the functional modules can be determined through the interaction logic between the different functional modules, and a detailed architecture diagram and description documents for each functional module can be generated.

[0071] For example, if the target system is a user information management system, and it is determined that the system development requirement information corresponding to the user information management system includes a login function, a permission management function, and a data storage function, then a system architecture including a login module, a permission management module and a user data storage module can be generated, and the interaction logic between the login module and the permission management module can be established to provide corresponding operation permissions for users who have successfully logged in.

[0072] In the disclosed embodiment, when generating the description document of each functional module, the interface information of each functional module may also be declared, and the interface information includes an interface definition and an interface specification. The interface definition is an interface used to call or declare the functional module. The interface specification may include protocol specifications for data interaction between different modules, such as the Transmission Control Protocol (TCP), the Hypertext Transfer Protocol (HTTP), etc., and may also include interface path specifications, interface naming specifications, etc. By declaring the interface definition and interface specification of each functional module, it helps to ensure the maintainability and scalability between the functional modules in the system.

[0073] In step 104 , based on the system architecture of the target system, module codes of each functional module are iteratively generated to obtain the current round of system codes of the target system.

[0074] Among them, the target system includes multiple functional modules, and each functional module can be iteratively implemented according to the system architecture to complete the system code that meets the development requirement information.

[0075] Exemplarily, the target system includes three functional modules, namely the page frame module, the navigation bar module, and the form module. The page frame code can be first implemented according to the module requirement information of the page frame module, and then the module code of the navigation bar module can be iteratively generated based on the page frame code. Finally, the module code of the form module can be generated based on the module code including the navigation bar module. In this way, one round of iteration is realized to obtain the system code of the current round of the target system.

[0076] In the embodiment of the present disclosure, after the module code of each functional module is completed, code logic verification, unit testing, etc. can be performed on the module code, and corresponding comment information can be added to the module code to ensure the functionality and readability of the module.

[0077] In a specific implementation, a large code model can be used to automatically generate the module code of the functional module to be generated based on the module requirements of each functional module and the generated module code. The large code model can be a pre-trained large model, such as a Generative Pre-Trained Transformer (GPT), which generates the module code of the functional module to be generated based on the module requirements of each functional module and the generated module code.

[0078] In some optional implementations, a large number of training samples may be collected in advance, each of which may include various code samples and module requirement samples of functional modules. The initial model is trained using the large number of training samples. That is, the initial model is fed with the large number of training samples so that the initial model learns these training samples. This ensures that, after model training is complete, the module code of the functional module to be generated can be generated based on the module requirements of each functional module and the generated module code.

[0079] In step 105, the above code generation process is iteratively executed to achieve the next round of code generation until the preset iteration completion condition is met, thereby obtaining the final system code of the target system.

[0080] The preset iteration completion condition is used to indicate the condition for determining the end of the process of iteratively generating the system code of the target system.

[0081] In some embodiments, the preset iteration completion condition may include the total number of iterations exceeding a preset threshold, that is, after the number of code generation for the target system exceeds a certain number, such as more than 3 times, the code generation process can be determined to be complete.

[0082] In some embodiments, after each code generation process is completed, new system requirements can be generated and a new development requirements document can be created based on the new system requirements. If the system architecture corresponding to the new requirements has changed, a new system architecture needs to be generated based on the new development requirements document, and then the system code for the current iteration round can be generated based on the new system architecture.

[0083] In specific implementation, based on the generated system code, further semantic analysis can be performed on the system code, and possible context information can be obtained to obtain new functional requirements and implement the expansion of system functions.

[0084] For example, the target system is a user information management system. In the first iteration round, the initial system code including the login function, permission management function, and data storage function is generated. By expanding the function of the initial system code, a new data encryption storage function module can be added based on the data storage function module.

[0085] Through the above steps 101 to 105, when code needs to be generated, the original code fragment can be semantically parsed to predict the target system to which the original code fragment belongs; a development requirement document for the target system is generated, and the development requirement document records the development requirement information of the target system; based on the development requirement information of the target system, a system architecture of the target system is generated, and the system architecture includes at least one of the following: multiple functional modules included in the target system, the dependency relationship between each functional module, and the module requirement information and interface information of each functional module; based on the system architecture of the target system, the module code of each functional module is iteratively generated to obtain the system code of the current round of the target system; the above code generation process is iteratively executed to realize the next round of code generation until the preset iteration completion condition is met to obtain the final system code of the target system. Therefore, the technical solution disclosed in the present invention can determine the target system to be generated based on the original code fragment, and generate a complete and coherent system code from the systematic method of deriving the requirements of the target system to the gradual implementation in accordance with the working mode of the waterfall model, which can ensure that the functional modules in the generated system code are interconnected and have semantic consistency and logical relevance; in addition, by iteratively executing the code generation process, it helps that each iteration round is progressively optimized based on the code of the previous round, ensuring the gradual improvement of the code complexity and logic, and improving the structural clarity, logic and diversity of the generated code.

[0086] In some optional implementations, each time a code is generated, such as generating a module code or generating a system code based on a development requirements document, the generated code can be stored in a code repository, and the code storage path, code version identifier and generation time of the module code or system code generated in the current iteration round can be stored in a code update record table.

[0087] Among them, by recording the code version identification, code storage path and generation time generated by the target system in each round in the code update record table, it is helpful to clarify the evolution path of the system code of the target system from zero code to the final version, and then help to clarify the incremental relationship between each version of module code or system code. Moreover, the traceability of each version of module code or system code is stronger. When used as a code sample to train the code model, it can provide a clear reference basis and support for the understanding and optimization of the code model.

[0088] Figure 2 This is a flow chart of another embodiment of the code generation method disclosed in the present invention. This embodiment of the present invention takes how to implement a round of system code generation as an example for illustrative description. Figure 2 The method shown includes steps 201 to 206. Each step is described below.

[0089] In step 201 , multiple functional modules are sorted according to the dependency relationship between the functional modules in the system architecture. The order of the functional modules is used to indicate the order of generating module codes of the functional modules.

[0090] Dependencies between functional modules refer to the need for one module to reference or call functions or data from other modules. The order in which functional modules are implemented is closely related to the dependencies between them. These dependencies determine the order in which they are initialized. The module code for the dependent functional module is generated first, as the functions or data of other modules depend on it. For example, in a system architecture, hardware-layer modules are typically initialized before system-layer modules, and framework-layer modules are initialized before application-layer modules.

[0091] In other implementations, for functional modules that do not have a direct dependency relationship, their implementation order can be determined based on the implementation order of the functional modules that each functional module depends on. For example, functional module 1 depends on functional module 2, and functional module 3 depends on functional module 4. There is no necessary, direct dependency relationship between functional modules 1 and 2, but functional module 2 is implemented before functional module 4. In this case, functional module 1 can be implemented before functional module 3.

[0092] Optionally, for functional modules that do not have direct dependencies, the implementation order is not fixed and can also be determined randomly.

[0093] In step 202 , according to the sorted order of generating module codes for the various function modules, a target function module for which module code generation in the current iteration round is to be performed is determined in sequence from the plurality of function modules.

[0094] In the disclosed embodiment, after the functional modules are sorted, the functional modules can be implemented step by step in the order in which their module codes are generated. During each iteration of module code generation, the most highly sorted functional module among the functional modules for which module codes have not been generated can be selected as the target functional module for module code generation in the current iteration.

[0095] To implement the disclosed technical solution, during the process of iteratively generating module codes for each functional module, each time a target functional module is determined or module codes are generated for the target functional module, the target functional module can be set as an iterated module. During the process of iteratively generating module codes for the functional modules, the top-ranked functional module from the non-iterated functional modules can be selected as the target functional module, and the functional modules set as iterated modules are no longer considered.

[0096] In step 203, based on the module requirement information and interface information of the target functional module and the dependency relationship with the functional module for which the module code has been generated, the module code generated in the previous iteration round is expanded to obtain the module code generated in the current iteration round.

[0097] Among them, the module requirement information of each functional module is used to indicate the functional requirement information, non-functional requirement information, interface design requirement information, etc. to be implemented by the functional module, which can provide guidance for the generation of subsequent module code. The interface information of each functional module includes interface definition and interface specification. The interface definition is the interface used to call or declare the functional module. The interface specification may include protocol specifications for data interaction between different modules, such as TCP protocol, HTTP protocol, etc., and may also include interface path specifications, interface naming specifications, etc. By declaring the interface definition and interface specification of each functional module, it helps to ensure the maintainability and scalability between the functional modules in the system.

[0098] In the disclosed embodiment, the previous iteration round refers to the iteration that is adjacent to the current iteration round and is located before the current iteration round. The module code generated in the previous iteration round includes the codes of all generated functional modules.

[0099] Exemplarily, there are five functional modules that need to be generated for the target system. The generation order determined by step 201 is that functional module 2 precedes functional module 1, functional module 3 precedes functional module 2, functional module 4 precedes functional module 3, and functional module 5 precedes functional module 4. Therefore, the module code of functional module 5 can be generated first, and then, based on the module code of functional module 5, the module code of functional module 4 can be iteratively generated. After the module code of functional module 4 is iteratively generated, the generated code includes the codes of functional module 4 and functional module 5; then, based on the module code of functional module 4, the module code of functional module 3 can be iteratively generated. After the module code of functional module 3 is iteratively generated, the generated code includes the codes of functional module 3, functional module 4 and functional module 5; and so on. Finally, the code of functional module 1 is iteratively generated to obtain the initial system code of the target system.

[0100] In step 204, the module code generated in the current iteration round is verified, and the verification includes at least one of the following: code syntax verification, code unit testing, code integration testing, code coverage verification, and code similarity verification.

[0101] In the embodiment of the present disclosure, after each iteration of module code generation, in order to improve code quality, syntax checking, unit testing, integration testing, and code coverage checking may be performed on the iteratively generated module code.

[0102] Specifically, whether there are syntax errors in the module code can be determined by compiling the module code. If a syntax error is displayed during compilation, the code syntax check fails. If the compilation passes, it means that the syntax check is successful.

[0103] In some embodiments of the present disclosure, unit testing can be implemented through automated testing tools, for example, using the JUnit unit testing tool to automatically perform unit testing; in other embodiments of the present disclosure, in order to improve code generation efficiency, unit testing can also be implemented through code compilation to verify whether various parts of the code work as expected, such as through the compilation test framework, unit testing of C language code can be implemented.

[0104] Integration testing of the code can also be achieved through automated testing tools. Through integration testing, it can be verified whether the interactions between multiple functional modules meet expectations and whether the functional modules can work together.

[0105] Code coverage verification is mainly used to verify code coverage, function coverage, and user scenario coverage. For example, code coverage is verified through statement coverage and branch coverage, function coverage is verified through test cases, and test cases are used to verify whether the code covers all user operation scenarios. In this implementation, verifying code coverage helps ensure that the generated code covers a wider range of actual scenarios, improving code quality and scenario coverage.

[0106] In some optional implementations, the code similarity check is used to check the similarity between the module code generated in the current iteration round and the module code generated in the previous iteration round, so as to determine whether the code of this iteration is an invalid iteration. If it is determined that the code is very similar in code structure and code semantics, the similarity check result can be determined as a verification failure, and the verification fails, and the component extension code of this extension is not stored.

[0107] In step 205 , in response to the verification result indicating that the verification has passed, it is determined whether codes for all functional modules have been iteratively generated.

[0108] Furthermore, in response to the presence of a functional module for which module code has not been generated among the multiple functional modules, step 202 is iteratively executed to determine the operation of the target functional module for which module code is to be generated in the current iteration round from the multiple functional modules in turn; in response to the module codes of the multiple functional modules having been generated, step 206 is executed.

[0109] In step 206 , in response to the module codes of the plurality of functional modules being generated, the system code of the current round of the target system is obtained.

[0110] Through the above steps 201 to 206, an implementation method for iteratively generating module codes for functional modules based on the system architecture after generating the system architecture is disclosed. By iteratively generating the system code of the target system according to the system architecture, it helps to improve the semantic consistency and logical relevance of the generated system code. In addition, after each iterative code generation, the code is verified through multiple verification methods, which helps to improve the quality and scenario adaptability of the code and enhance the practical use value of the generated code.

[0111] In other optional implementations, after expanding the code generated in the previous iteration round to obtain the code generated in the current iteration round through step 203, the performance of the code generated in the current iteration round can also be optimized based on pre-optimization rules to obtain the optimized module code generated in the current iteration round.

[0112] In this implementation, the preset optimization rules for optimizing the module code may include the following rules: optimizing the algorithms and data structures in the code; and optimizing the loop structures and redundant calculations in the code.

[0113] When optimizing algorithms and data structures in your code, you can choose more appropriate algorithms and data structures to replace existing algorithms. For example, when processing large amounts of data, using a hash table (O(1) average search time) instead of a linear table (O(n) search time) can significantly increase search speed. For sorting tasks, quick sort (O(n log n) time complexity) is usually more efficient than bubble sort (O(n^2) time complexity).

[0114] When optimizing loop structures and redundant calculations in the code, you can reduce the number of loops or use more efficient loop structures. For example, when the number of iterations is known, use a for loop instead of a while loop.

[0115] In this implementation, by performing performance optimization after each iteration of code generation, it is helpful to improve the performance of the generated code and ensure that the generated code has higher quality.

[0116] Figure 3 This is a flow chart of an embodiment of code verification in the code generation method disclosed in the present invention. This embodiment of the present invention takes how to implement code similarity verification as an example for illustrative explanation. Figure 3 The method shown includes steps 301 to 303. Each step is described below.

[0117] In step 301, the similarity between the module code generated in the current iteration round and the module code generated in the previous iteration round is obtained.

[0118] In some implementations, the similarity may be determined based on the code structure and code semantics of the module code generated in the current iteration round and the module code generated in the previous iteration round.

[0119] The code structure is used to indicate the structure of the code arranged and arranged according to certain rules and organizational methods. This structure can be reflected through an abstract syntax tree. Code analysis tools, such as the Abstract Syntax Tree (AST) analysis tool, can be used to scan the module code generated in the previous iteration and the module code generated in the current iteration, respectively, to obtain the code structure corresponding to the module code generated in the previous iteration and the module code generated in the current iteration. The AST analysis tool is a tool that can convert code into a data structure that is easy to process and analyze.

[0120] Code semantics indicate the meaning of code during execution, including the purpose of variables and data types, as well as the specific operations and intentions represented by the code during execution. By performing semantic analysis on the module code generated in the previous iteration and the module code generated in the current iteration, the code semantics corresponding to the module code generated in the previous iteration and the module code generated in the current iteration can be obtained. In specific implementations, code semantics can be obtained using semantic analysis tools.

[0121] In the disclosed embodiment, determining the similarity between the module code generated in the previous iteration round and the module code generated in the current iteration round through code structure and code semantics helps to obtain the essential similarity between the module code generated in the previous iteration round and the module code generated in the current iteration round.

[0122] Furthermore, if the similarity is greater than a preset threshold, it can be determined that the module code generated in the previous iteration round and the module code generated in the current iteration round are significantly different, and step 302 can be executed; if the similarity is not greater than the preset threshold, it can be determined that the module code generated in the previous iteration round and the module code generated in the current iteration round are slightly different, and step 303 can be executed.

[0123] In step 302 , in response to the similarity being greater than a preset threshold, it is determined that the verification result of the code coverage verification is verification passed.

[0124] In step 303 , in response to the similarity not being greater than a preset threshold, it is determined that the verification result of the code coverage verification is a verification failure.

[0125] Through the above steps 301 to 303, a method for determining the similarity between the module code generated in the previous iteration round and the module code generated in the current iteration round is disclosed. By determining the similarity between the module code generated in the previous iteration round and the module code generated in the current iteration round, the generation of redundant code can be avoided, ensuring that the code data has a wide coverage and no duplication, which in turn helps to provide high-quality and highly applicable code samples for the large code model.

[0126] Exemplary devices

[0127] Figure 4 FIG. 1 is a schematic diagram of the structure of an embodiment of the code generation device disclosed in the present invention. Figure 4 As shown, the device includes:

[0128] A prediction module 41 is used to perform semantic analysis on the original code snippet and predict the target system to which the original code snippet belongs;

[0129] A requirement document generating module 42 is used to generate a development requirement document of a target system, wherein the development requirement document records the development requirement information of the target system;

[0130] The system architecture generation module 43 is used to generate the system architecture of the target system based on the development requirement information of the target system. The system architecture includes at least one of the following: multiple functional modules included in the target system, the dependency relationship between the functional modules, and the module requirement information and interface information of each functional module;

[0131] The code generation module 44 is used to iteratively generate module codes for each functional module based on the system architecture of the target system to obtain the system code of the current round of the target system;

[0132] The iteration module 45 is used to iteratively execute the above code generation process to achieve the next round of code generation until the preset iteration completion condition is met to obtain the final system code of the target system.

[0133] Figure 5 A schematic diagram of the structure of another embodiment of the code generation device disclosed in the present invention. Figure 5 As shown, in Figure 4 Based on the illustrated embodiment, in some implementations of the present disclosure, the code generation module 44 includes:

[0134] The sorting submodule 441 is used to sort multiple functional modules according to the dependency relationship between the functional modules in the system architecture. The order of the functional modules is used to indicate the order of generating module codes for the functional modules.

[0135] A first determining submodule 442 is configured to determine, from the plurality of functional modules, a target functional module for which module code generation is to be performed in a current iteration round, in accordance with the sorted order of generating module codes for the functional modules;

[0136] An extension submodule 443 is configured to extend the module code generated in the previous iteration based on the module requirement information and interface information of the target functional module and the dependency relationship between the module code generated and the functional module, thereby obtaining the module code generated in the current iteration.

[0137] A check submodule 444 is configured to check the module code generated in the current iteration round, where the check includes at least one of the following: code syntax check, code unit test, code integration test, code coverage check, and code similarity check;

[0138] An iterator submodule 445 is configured to iteratively execute an operation of sequentially determining a target functional module for generating module code in a current iteration round from among the multiple functional modules in response to the verification result indicating that the verification has passed;

[0139] The code generation submodule 446 is configured to obtain the current round of system code of the target system in response to the module codes of the plurality of functional modules being generated.

[0140] In some embodiments of the present disclosure, the syndrome module 444 includes:

[0141] An acquiring unit 4441 is configured to acquire a similarity between a module code generated in a current iteration round and a module code generated in a previous iteration round;

[0142] The verification unit 4442 is configured to determine that the verification result of the code coverage verification is a passed verification in response to the similarity being greater than a preset threshold; and to determine that the verification result of the code coverage verification is a failed verification in response to the similarity being not greater than the preset threshold.

[0143] In some embodiments of the present disclosure, the extension submodule 443 is configured to perform performance optimization on the module code generated in the current iteration round based on pre-optimization rules to obtain optimized module code generated in the current iteration round.

[0144] In some embodiments of the present disclosure, the device further comprises:

[0145] The storage module 46 is configured to store the module code generated in the current iteration round in the code repository; and store the code storage path, code version identifier, and generation time of the module code generated in the current iteration round in the code update record table.

[0146] The apparatus of the embodiments of the present disclosure may be used to implement the methods of the above-mentioned embodiments of the present disclosure. The two methods correspond to each other in specific implementation, and the specific implementations of the relevant parts are mutually referenced and will not be repeated here.

[0147] Exemplary electronic devices, computer program products, and computer-readable storage media

[0148] An embodiment of the present disclosure further provides an electronic device, comprising: a memory for storing a computer program; a processor for executing the computer program stored in the memory, and when the computer program is executed, implementing the code generation method of any of the above embodiments of the present disclosure.

[0149] Below, reference Figure 6 An electronic device according to an embodiment of the present disclosure is described, in which a device for implementing the method according to an embodiment of the present disclosure may be integrated. Figure 6 A structural diagram of an electronic device provided in an exemplary embodiment of the present disclosure, such as Figure 6 As shown, the electronic device includes one or more processors 61, one or more computer-readable storage media memory 62, and a computer program stored in the memory and executable on the processor. When executing the program in the memory 62, the above-mentioned code generation method can be implemented.

[0150] Specifically, in actual applications, the electronic device may further include components such as an input device 63 and an output device 64, and these components are interconnected via a bus system and / or other forms of connection mechanisms (not shown). Those skilled in the art will appreciate that Figure 6 The structure of the electronic device shown in the figure does not constitute a limitation of the electronic device, and may include more or fewer components than shown in the figure, or certain components, or different component arrangements.

[0151] The processor 61 can be a central processing unit (CPU) or other forms of processing units with code generation capabilities and / or instruction execution capabilities. It performs various functions and processes data by running or executing software programs and / or modules stored in the memory 62, and calling data stored in the memory 62, thereby monitoring the electronic device as a whole.

[0152] The memory 62 can store one or more computer program products. The memory can include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory can include, for example, random access memory (RAM) and / or cache memory. The non-volatile memory can include, for example, read-only memory (ROM), a hard disk, flash memory, etc. One or more computer program products can be stored on the computer-readable storage medium, and the processor 61 can execute the computer program products to implement the code generation method of each embodiment of the present disclosure and / or other desired functions.

[0153] The input device 63 can be used to receive input digital or character information. The input device 63 can include a keyboard, a mouse, a joystick, etc. related to user settings and function control.

[0154] The output device 64 can output various information to the outside, including the determined distance information, direction information, etc. The output device 64 can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.

[0155] The electronic device may also include a power supply for supplying power to various components, which may be logically connected to the processor 61 via a power management system, thereby enabling the power management system to manage charging, discharging, and power consumption. The power supply may also include one or more DC or AC power supplies, a recharging system, a power failure detection circuit, a power converter or inverter, a power status indicator, and other arbitrary components.

[0156] Of course, to simplify, Figure 6Only some of the components related to the present disclosure in the electronic device are shown, and components such as a bus, an input / output interface, etc. are omitted. In addition, the electronic device may further include any other appropriate components according to specific application scenarios.

[0157] In addition to the above-mentioned methods and devices, an embodiment of the present disclosure may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to execute the steps in the code generation method according to various embodiments of the present disclosure described in the above "Exemplary Method" section of this specification.

[0158] The computer program product may be written in any combination of one or more programming languages to implement the operations of the disclosed embodiments, including object-oriented programming languages such as Java, C++, and conventional procedural programming languages such as C or similar programming languages. The program code may be executed entirely on the user's computing device, partially on the user's computing device, as a stand-alone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0159] In addition, an embodiment of the present disclosure may also be a computer-readable storage medium having computer program instructions stored thereon. When the computer program instructions are executed by a processor, the processor executes the steps of the code generation method according to various embodiments of the present disclosure described in the above “Exemplary Method” section of this specification.

[0160] Computer readable storage media can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can, for example, include but is not limited to a system, device or component of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination thereof. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.

[0161] The basic principles of the present disclosure have been described above in conjunction with specific embodiments. However, it should be noted that the advantages, strengths, and effects mentioned in this disclosure are merely illustrative and not restrictive, and should not be construed as necessarily possessed by each embodiment of the present disclosure. Furthermore, the specific details disclosed above are provided for illustrative purposes and to facilitate understanding, rather than as limitations. These details do not limit the present disclosure to necessarily being implemented using these specific details.

[0162] Each embodiment in this specification is described in a progressive manner, with each embodiment focusing on its differences from the other embodiments. References to the same or similar parts between the various embodiments are sufficient. For system embodiments, since they largely correspond to method embodiments, their description is relatively simple. For relevant parts, references to the description of the method embodiments are sufficient.

[0163] Those skilled in the art will understand that all or part of the steps of implementing the above-mentioned method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above-mentioned method embodiment; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk, etc. Various media that can store program codes.

[0164] The methods and apparatus of the present disclosure may be implemented in many ways. For example, the methods and apparatus of the present disclosure may be implemented by software, hardware, firmware, or any combination of software, hardware, and firmware. The above order of steps for the method is for illustration only, and the steps of the method of the present disclosure are not limited to the order specifically described above unless otherwise specified. In addition, in some embodiments, the present disclosure may also be implemented as programs recorded in a recording medium, which include machine-readable instructions for implementing the methods according to the present disclosure. Therefore, the present disclosure also covers recording media that store programs for executing the methods according to the present disclosure.

[0165] The description of the present disclosure is provided for purposes of illustration and description and is not intended to be exhaustive or to limit the disclosure to the disclosed form. Many modifications and variations will be apparent to those skilled in the art. The embodiments are selected and described in order to better illustrate the principles and practical applications of the present disclosure and to enable those skilled in the art to understand the present disclosure and design various embodiments with various modifications suitable for specific applications.

Claims

1. A code generation method, characterized in that: include: Performing semantic analysis on the original code snippet to predict the target system to which the original code snippet belongs; Generating a development requirement document for the target system, wherein the development requirement document records development requirement information for the target system; Based on the development requirement information of the target system, a system architecture of the target system is generated, wherein the system architecture includes at least one of the following: multiple functional modules included in the target system, dependencies between the functional modules, and module requirement information and interface information of the functional modules; Iteratively generate module codes for each functional module based on the system architecture of the target system to obtain the current round of system codes for the target system; The above code generation process is iteratively executed to achieve the next round of code generation until the preset iteration completion condition is met, thereby obtaining the final system code of the target system.

2. The method according to claim 1, characterized in that The iterative generation of module codes for each functional module based on the system architecture of the target system to obtain the current round of system codes for the target system includes: Sorting the plurality of functional modules according to the dependency relationships between the functional modules in the system architecture, wherein the order of the functional modules is used to indicate the order of generating module codes for the functional modules; According to the sorted order of generating module codes for the functional modules, determining the target functional module for generating module codes in the current iteration round from the plurality of functional modules in turn; Based on the module requirement information and interface information of the target functional module and the dependency relationship between the target functional module and the functional module for which the module code has been generated, the module code generated in the previous iteration round is expanded to obtain the module code generated in the current iteration round; Verify the module code generated in the current iteration round, wherein the verification includes at least one of the following: code syntax verification, code unit testing, code integration testing, code coverage verification, and code similarity verification; In response to the verification result indicating that the verification has passed, iteratively performing the operation of sequentially determining a target functional module for which module code is to be generated in a current iteration round from the multiple functional modules; In response to the module codes of the multiple functional modules being generated, a system code of the current round of the target system is obtained.

3. The method according to claim 2, characterized in that Performing code similarity verification on the module code generated in the current iteration round includes: Obtaining the similarity between the module code generated in the current iteration round and the module code generated in the previous iteration round; In response to the similarity being greater than a preset threshold, determining that a verification result of the code coverage verification is verification passed; In response to the similarity being not greater than a preset threshold, determining that a verification result of the code coverage verification is a verification failure.

4. The method according to claim 2, characterized in that After the module code generated in the previous iteration round is expanded to obtain the module code generated in the current iteration round, the following steps are included: Based on the pre-optimization rules, the performance of the module code generated in the current iteration round is optimized to obtain the optimized module code generated in the current iteration round.

5. The method according to claim 2, characterized in that After the module code generated in the previous iteration round is expanded to obtain the module code generated in the current iteration round, the following steps are included: The module code generated in the current iteration round is stored in the code repository; and the code storage path, code version identifier and generation time of the module code generated in the current iteration round are stored in the code update record table.

6. A code generating device, characterized in that: include: A prediction module, configured to perform semantic analysis on the original code snippet and predict the target system to which the original code snippet belongs; A requirement document generating module, configured to generate a development requirement document for the target system, wherein the development requirement document records the development requirement information of the target system; a system architecture generation module, configured to generate a system architecture of the target system based on the development requirement information of the target system, wherein the system architecture includes at least one of the following: multiple functional modules included in the target system, dependencies between the functional modules, and module requirement information and interface information of the functional modules; A code generation module, configured to iteratively generate module codes for each functional module based on the system architecture of the target system, to obtain the system code for the current round of the target system; The iteration module is used to iteratively execute the above code generation process to achieve the next round of code generation until the preset iteration completion conditions are met to obtain the final system code of the target system.

7. The device according to claim 6, characterized in that The code generation module includes: a sorting submodule, configured to sort the plurality of functional modules according to the dependency relationships between the functional modules in the system architecture, wherein the order of the functional modules is used to indicate the order in which the module codes of the functional modules are generated; A first determining submodule is configured to determine, in order of generating module codes for the function modules, a target function module for generating module codes in a current iteration round from the plurality of function modules; An extension submodule, configured to expand the module code generated in the previous iteration round based on the module requirement information and interface information of the target functional module and the dependency relationship between the module code and the functional module for which the module code has been generated, to obtain the module code generated in the current iteration round; A verification submodule, configured to verify the module code generated in the current iteration round, wherein the verification includes at least one of the following: code syntax verification, code unit testing, code integration testing, code coverage verification, and code similarity verification; an iterative submodule, configured to iteratively execute the operation of sequentially determining a target functional module for generating module code in a current iteration round from the plurality of functional modules in response to a verification result indicating that the verification has passed; The code generation submodule is configured to obtain the current round of system code of the target system in response to the module codes of the multiple functional modules being generated.

8. The device according to claim 7, characterized in that The syndrome module includes: an acquiring unit, configured to acquire a similarity between the module code generated in the current iteration round and the module code generated in the previous iteration round; A verification unit is used to determine that the verification result of the code coverage range verification is a passed verification in response to the similarity being greater than a preset threshold; and to determine that the verification result of the code coverage range verification is a failed verification in response to the similarity being not greater than the preset threshold.

9. The device according to claim 7, characterized in that The extension submodule is used to optimize the performance of the module code generated in the current iteration round based on pre-optimization rules to obtain the optimized module code generated in the current iteration round.

10. The device according to claim 7, characterized in that The device further comprises: The storage module is used to store the module code generated in the current iteration round in the code warehouse; and store the code storage path, code version identifier and generation time of the module code generated in the current iteration round in the code update record table.

11. An electronic device, comprising: a memory for storing a computer program product; A processor is configured to execute a computer program product stored in the memory, and when the computer program product is executed, implements the method described in any one of claims 1 to 5.

12. A computer program product comprising computer program instructions, characterized in that When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Cited By

  • Target embedded code generation method and device, electronic equipment and storage medium

    CN120704694A