A file-based code generation method, device, equipment and storage medium
By parsing the target file and searching for reusable code in the codebase to generate the target code, the problem of code generation that requires developer involvement in existing technologies is solved, and automated and efficient code generation is achieved.
Patent Information
- Application Number
- CN202411805052.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-09
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2044-12-09
AI Technical Summary
Current technologies require developer involvement to generate code, making it impossible to automate code generation.
By parsing the target file, the target function that needs to be implemented is determined, and reusable code that can be reused is found in the code library corresponding to the target file to generate the target code.
It enables automated code generation without the need for developer intervention, reducing repetitive code writing and improving development efficiency and code quality.
Smart Images

Figure CN119806513B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and in particular to a file-based code generation method and apparatus, an electronic device, and a storage medium. Background Technology
[0002] With the rapid development of artificial intelligence technology, especially the maturity of deep learning and natural language processing, code generation based on large language models has gradually become an important tool in the software development field. Large language models (such as GPT and Codex) can generate code snippets that meet user needs by learning from large codebases and programming language patterns. This technology significantly improves the efficiency of software development, especially by automating and intelligently completing code writing and suggestion.
[0003] In related technologies, code logic is generated based on the developer's comments and descriptions during the coding process. However, the coding process requires the participation of the developer and continuous correction of ambiguities in the understanding of the large language model. Summary of the Invention
[0004] This application provides a file-based code generation method to solve the problem that the code generation process requires developer participation and cannot achieve automated code generation.
[0005] Accordingly, embodiments of this application also provide a file-based code generation device, an electronic device, and a storage medium to ensure the implementation and application of the above methods.
[0006] To address the aforementioned problems, this application discloses a file-based code generation method, the method comprising:
[0007] The target file is parsed to obtain the target functionality that the target file needs to implement; the target file has a corresponding code library.
[0008] Based on the target function, reusable code that can be reused is determined in the code library corresponding to the target file;
[0009] The target functions that cannot be achieved by the reused code are identified as functions to be implemented.
[0010] Create new code to implement the functionality to be implemented;
[0011] Based on the reused code and the newly added code, target code for implementing the target function is generated.
[0012] Optionally, parsing the target file to obtain the target function that the target file needs to implement includes:
[0013] The target file is parsed to obtain its semantic information;
[0014] The target function is determined based on the semantic information of the target file.
[0015] Optionally, the target function has corresponding code logic, and the step of determining reusable code that can be reused in the code library corresponding to the target file based on the target function includes:
[0016] Analyze the code in the code library corresponding to the target file to determine the code logic corresponding to the code;
[0017] The reusable code is determined based on the code logic corresponding to the code and the code logic corresponding to the target function.
[0018] Optionally, determining the reusable code based on the code logic corresponding to the code and the code logic corresponding to the target function includes:
[0019] In the code library corresponding to the target file, traverse the code logic corresponding to the target function;
[0020] Code with the same logic as the target function is identified as the reusable code.
[0021] Optionally, generating target code for implementing the target function based on the reused code and the newly added code includes:
[0022] Determine the target code structure used to implement the target function;
[0023] The reused code and the newly added code are combined according to the target code structure to obtain the target code.
[0024] Optionally, after generating the target code corresponding to the target file based on the reused code and the newly added code, the method further includes:
[0025] The target code is broken down into multiple functions, and the calling relationships between these functions are determined; each function has corresponding functionality and metadata.
[0026] Construct a directed graph to manage the functions based on the calling relationships between the multiple functions;
[0027] The directed graph, the functions corresponding to the functions, and the metadata corresponding to the functions are stored in the database.
[0028] Optionally, after storing the directed graph, the functionality corresponding to the function, and the metadata corresponding to the function in the database, the method further includes:
[0029] The number of times the function is called is determined using the database;
[0030] When the number of times the function is called exceeds a preset number, the function is refactored to optimize it.
[0031] This application also discloses a file-based code generation apparatus, the apparatus comprising:
[0032] The target function module is used to parse the target file to obtain the target function that the target file needs to implement; the target file has a corresponding code library.
[0033] A code reuse module is used to determine reusable code that can be reused in the code library corresponding to the target file based on the target function.
[0034] The functional modules to be implemented are used to identify target functions that cannot be achieved by the reused code as functional modules to be implemented.
[0035] A new code module is added to create new code to implement the functionality to be implemented.
[0036] The target code module is used to generate target code for implementing the target function based on the reused code and the newly added code.
[0037] This application also discloses an electronic device, including: a processor; and a memory storing executable code thereon, wherein when the executable code is executed, the processor performs any of the file-based code generation methods described in the embodiments of this application.
[0038] This application also discloses one or more machine-readable media storing executable code, which, when executed, causes a processor to perform any of the file-based code generation methods described in this application.
[0039] Compared with the prior art, the embodiments of this application have the following advantages:
[0040] In this embodiment, a target file is parsed to obtain the target function that the target file needs to implement; the target file has a corresponding code library; based on the target function, reusable code that can be reused is determined in the code library corresponding to the target file; target functions that cannot be implemented by the reusable code are determined as functions to be implemented; new code is created to implement the functions to be implemented; and target code for implementing the target function is generated based on the reusable code and the new code. This embodiment effectively reduces repetitive code writing and improves development efficiency by parsing the target file and determining its target function, and then searching for reusable code in the code library corresponding to the target file. Furthermore, no developer intervention is required during the generation of target code, thus achieving automated code generation. Attached Figure Description
[0041] Figure 1 This is a flowchart illustrating the steps of an embodiment of a file-based code generation method according to this application;
[0042] Figure 2 This is a system architecture diagram for automated code generation and management according to this application;
[0043] Figure 3 This is a schematic diagram illustrating a process for implementing code generation in the model layer according to this application;
[0044] Figure 4 This is a structural block diagram of an embodiment of a file-based code generation apparatus according to this application;
[0045] Figure 5 This is a schematic diagram of the structure of a device provided in an embodiment of this application. Detailed Implementation
[0046] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0047] In this embodiment of the application, the automatic generation of code is achieved based on the Spring Cloud framework (an ordered collection of a series of frameworks).
[0048] Spring Cloud is a microservice framework based on Spring Boot (an open-source application framework) designed to simplify the development of common patterns in distributed systems. Spring Cloud provides a suite of tools and libraries to help developers quickly build and deploy microservice applications. Core functionalities of Spring Cloud include service discovery, configuration management, load balancing, circuit breakers, and distributed messaging.
[0049] Reference Figure 1 This is a flowchart illustrating the steps of an embodiment of a file-based code generation method according to this application, including the following steps:
[0050] Step 101: Parse the target file to obtain the target function that the target file needs to implement; the target file has a corresponding code library;
[0051] In this embodiment, the target file is a description document of project requirements, defining the functions and goals that the project needs to achieve. The target file is the starting point and basis for project generation; the project is the specific implementation generated based on the target file, including code, etc. The goal of the project is to implement the functions described in the target file. Specifically, the target file is the input to the project, the project is the output of the target file, the target file guides the generation of the project, and the project is the specific implementation of the target file.
[0052] Therefore, by parsing the target file, we can understand the functional requirements it describes, i.e., the target functionality the target file needs to implement. Furthermore, the target file is associated with a code repository containing existing code resources. Specifically, the code repository corresponding to the target file is predefined by the developers based on the target file and includes existing code resources.
[0053] In addition to determining the target functions that the target file needs to implement, it is also necessary to parse out the function interfaces, which are the key components for implementing the target functions.
[0054] By parsing target files, project requirements can be accurately understood, providing a clear direction for subsequent code generation. This automates the process from requirements to implementation, improving development efficiency and accuracy. Furthermore, linking to a code repository helps utilize existing code resources, further enhancing development efficiency.
[0055] Step 102: Based on the target function, determine the reusable code that can be reused in the code library corresponding to the target file;
[0056] During project generation, based on the functional requirements (target functions) described in the target file, reusable code snippets are searched in the existing codebase and these snippets are called reusable code. Reusable code can be directly used to implement some or all of the target functions, thereby reducing development time and workload.
[0057] This application's embodiments reduce repetitive work and improve development efficiency by reusing existing code, while also helping to maintain code consistency and quality. By searching for reusable code in existing codebases, existing code resources can be utilized more effectively, avoiding the generation of redundant code.
[0058] Step 103: Identify the target functions that cannot be achieved by the reused code as functions to be implemented;
[0059] In this embodiment of the application, after determining the reusable code, the functions that the reusable code can achieve are analyzed, and those target functions that cannot be achieved by the reusable code are marked as "functions to be implemented".
[0060] Based on this, we can identify which features need to be newly developed to avoid omissions or duplication of work, and focus code generation on the features that require new code implementation, thereby improving development efficiency.
[0061] Step 104: Create new code to implement the function to be implemented;
[0062] In this embodiment of the application, after determining the functions to be implemented, new code is generated to implement these functions. The new code is created specifically to satisfy the functions to be implemented.
[0063] By creating new code, the parts of the target functionality that cannot be accomplished by reusing existing code can be achieved.
[0064] Step 105: Generate target code for implementing the target function based on the reused code and the newly added code.
[0065] Finally, the reused code and the newly added code will be integrated together to generate a complete code module (target code), which can implement all the functions described in the target file.
[0066] The generated target code is a complete solution that meets all the requirements of the target file, while making use of existing code resources, thus improving development efficiency and code quality.
[0067] This application's embodiments parse the target file and determine its required functions, then search for reusable code in the corresponding code library. This effectively reduces repetitive code writing and improves development efficiency. Furthermore, no developer intervention is required during the target code generation process, thus achieving automated code generation.
[0068] In one embodiment of this application, step 101, parsing the target file to obtain the target function that the target file needs to implement, includes:
[0069] The target file is parsed to obtain its semantic information;
[0070] The target function is determined based on the semantic information of the target file.
[0071] In this embodiment of the application, to determine the target function, the target file is subjected to in-depth analysis, which goes beyond simple text parsing. Instead, natural language processing (NLP) or other advanced parsing techniques are used to extract semantic information from the target file. Semantic information includes, but is not limited to, key concepts, functional descriptions, and requirement details in the file.
[0072] By parsing the target file and obtaining its semantic information, we can more accurately understand the file's content and avoid misunderstandings or omissions of key information.
[0073] After obtaining the semantic information of the target file, the specific functions that the target file needs to implement are determined based on this information. These functions are determined based on the descriptions and requirements in the file, ensuring the accuracy and completeness of the functions.
[0074] By determining the target function based on semantic information, it is possible to ensure that the determined function is completely consistent with the description in the target file, avoiding function omissions or errors. This helps to improve the accuracy and reliability of the subsequent target code, thereby improving the accuracy and reliability of the project.
[0075] Furthermore, automated semantic parsing and target function determination avoid developer involvement, improving the efficiency and consistency of target code development.
[0076] This application embodiment, by parsing the semantic information of the target file and determining the target function based on it, can more accurately and completely understand the project requirements, ensuring that the project's functional implementation is completely consistent with the description in the target file. This not only improves the accuracy and reliability of the project, but also avoids the involvement of developers and improves development efficiency.
[0077] In one embodiment of this application, the target function has corresponding code logic. Step 102, determining reusable code that can be reused in the code library corresponding to the target file based on the target function, includes:
[0078] Analyze the code in the code library corresponding to the target file to determine the code logic corresponding to the code;
[0079] The reusable code is determined based on the code logic corresponding to the code and the code logic corresponding to the target function.
[0080] In this embodiment of the application, the code in the code library corresponding to the target file will be analyzed to extract the logic of each code segment. This includes identifying the functions, services, modules in the code and their calling relationships, thereby identifying which code logic is independent and which is interdependent.
[0081] By analyzing the code logic in the codebase, we can gain a comprehensive understanding of the functionality and structure of the existing code, providing detailed information for determining reusable code in the future.
[0082] After identifying the code logic in the codebase, this logic is compared with the code logic corresponding to the target functionality. This comparison identifies which code logic matches the requirements of the target functionality, thus determining which code can be reused.
[0083] Specifically, in the Spring Cloud framework, dependencies such as service invocation, load balancing, and service discovery are core components for building a microservice architecture. By combining these dependencies, it's possible to more specifically infer which code logic in the codebase corresponds to reusable code.
[0084] Service calls: In Spring Cloud, service calls are typically implemented using tools such as RestTemplate, Feign, or WebClient, which allow one service to call another.
[0085] For example, suppose there is an order service and an inventory service. The order service needs to call the inventory service to check the inventory status. If there is already code in the codebase corresponding to a common service call logic, then that code can be reused.
[0086] Load balancing: Spring Cloud implements load balancing through Ribbon or Spring Cloud LoadBalancer. Load balancing logic typically involves selecting a service instance to handle requests.
[0087] For example, suppose there is a user service that needs to call multiple instances of a logging service to record user actions. If a general load balancing logic already exists in the codebase, then that logic can be reused.
[0088] Service discovery: Spring Cloud implements service discovery through Eureka or Consul. Service discovery logic typically involves registering and discovering service instances.
[0089] For example, suppose there is a configuration service that needs to discover and invoke monitoring services for multiple instances to obtain system status. If a generic service discovery logic already exists in the codebase, that logic can be reused.
[0090] This application's embodiments, by combining dependencies in Spring Cloud (such as service invocation, load balancing, service discovery, etc.), can more specifically infer which code logic in the codebase corresponds to reusable code. Reusing the code corresponding to these common logics not only reduces repetitive work and improves development efficiency, but also maintains code consistency and quality, improving project reliability and maintainability.
[0091] In one embodiment of this application, determining the reusable code based on the code logic corresponding to the code and the code logic corresponding to the target function includes:
[0092] In the code library corresponding to the target file, traverse the code logic corresponding to the target function;
[0093] Code with the same logic as the target function is identified as the reusable code.
[0094] To compare the code logic corresponding to the target function with the code logic corresponding to the target function, the logic of each code segment is first examined one by one in the code library corresponding to the target file. This process is similar to performing a comprehensive scan of the code library to ensure that no code segments that may be identical to the target function's logic are missed.
[0095] By traversing the code logic in the codebase, one can gain a comprehensive understanding of the functionality and structure of the existing code, ensuring that no potentially reusable code is overlooked.
[0096] During the traversal, the code logic in the codebase is compared one by one with the code logic corresponding to the target function. If a code snippet is found to be completely identical to the target function's logic, it will be automatically identified as reusable code.
[0097] By comparing the code logic with the target functional logic, we can accurately determine which code can be reused, avoiding unnecessary code rewriting and improving development efficiency and code quality.
[0098] In one embodiment of this application, step 105, generating target code for implementing the target function based on the reused code and the newly added code, includes:
[0099] Determine the target code structure used to implement the target function;
[0100] The reused code and the newly added code are combined according to the target code structure to obtain the target code.
[0101] Before generating the target code, its overall structure is first determined. This includes defining the code's module division, function call relationships, class structure, and so on. The target code structure is determined based on the requirements of the target functionality and design specifications.
[0102] By defining the target code structure, we can ensure that the generated code has a good organization and structure, which facilitates subsequent maintenance and expansion.
[0103] Once the target code structure is determined, reusable and new code will be combined according to the predetermined structure. This process is similar to assembling different building blocks according to a design blueprint to ultimately form a complete code module.
[0104] By combining code according to the target code structure, it is possible to ensure that the generated code modules are consistent and complete, meeting all the requirements of the target function.
[0105] In one embodiment of this application, after step 105, generating the target code corresponding to the target file based on the reused code and the newly added code, the method further includes:
[0106] The target code is broken down into multiple functions, and the calling relationships between these functions are determined; each function has corresponding functionality and metadata.
[0107] Construct a directed graph to manage the functions based on the calling relationships between the multiple functions;
[0108] The directed graph, the functions corresponding to the functions, and the metadata corresponding to the functions are stored in the database.
[0109] After generating the target code, it is broken down into multiple independent functions. Each function has a clear functional description (e.g., one function processes orders, another interacts with the database) and metadata (e.g., function name, parameters, return value, etc.). The call relationships between these functions are also analyzed to determine which functions call other functions.
[0110] By breaking down the target code into multiple functions and determining the calling relationships, the structure and logic of the code can be better understood, facilitating subsequent management and optimization.
[0111] Then, a directed graph is generated based on the call relationships between functions. In this graph, each node represents a function, and each edge represents the call relationship between functions. In this way, the call relationships of all functions in the project can be managed in the form of a graph.
[0112] The generation of directed graphs facilitates the tracking and optimization of function call relationships, helping developers better understand the execution flow and dependencies of the code.
[0113] After obtaining the directed graph, the generated directed graph, function descriptions, and metadata are stored in the database, which helps with long-term management and querying, and improves the maintainability and traceability of the code.
[0114] In one embodiment of this application, after the target code is split into multiple functions, function documentation is automatically generated to record the function's purpose, inputs and outputs, exception handling, and other relevant information. This function documentation helps developers understand the target code and facilitates the integration of front-end and back-end services.
[0115] After generating the target code, this application's embodiments achieve better code management and optimization by splitting the target code into multiple functions, determining the call relationships between functions, constructing a directed graph, and storing relevant information. This process not only improves code readability and maintainability but also facilitates tracking and optimizing function call relationships, supports subsequent code analysis and optimization, and improves development efficiency.
[0116] In one embodiment of this application, after storing the directed graph, the functionality corresponding to the function, and the metadata corresponding to the function in the database, the method further includes:
[0117] The number of times the function is called is determined using the database;
[0118] When the number of times the function is called exceeds a preset number, the function is refactored to optimize it.
[0119] Since the database stores function-related information, the function call information stored in the database is continuously monitored to count the number of times each function is called. This process can be achieved through database queries and log analysis.
[0120] For example, database queries can be used to count the number of times each function is called; log analysis tools (such as ELK Stack, Splunk, etc.) can also be used to parse log files and count the number of times each function is called.
[0121] By automating the recording and counting of function calls, we can not only understand the actual usage of functions in real time, providing data support for subsequent function optimization and refactoring, but also reduce manual intervention and improve development efficiency and consistency.
[0122] When a function is called more than a preset threshold, the refactoring mechanism is automatically triggered. The refactoring process may include optimizing function performance, simplifying code logic, and reducing unnecessary dependencies. The goal of refactoring is to improve function efficiency and maintainability.
[0123] For example, suppose there is a function `processOrder` that processes orders and updates the database. This function is called frequently during peak periods, causing excessive database load. Therefore, batch updating the database can reduce the number of database connections and queries, thereby improving the performance of the `processOrder` function.
[0124] Another example: Suppose there's a function `calculateTotalPrice` that calculates the total price of an order. This function has complex logic, containing multiple nested conditional statements. Therefore, the complex logic can be broken down into several smaller functions to simplify the code structure, thereby improving the readability and maintainability of the `calculateTotalPrice` function.
[0125] As another example, suppose there is a function `sendEmail` responsible for sending emails. This function depends on an external email service, but in some cases, sending emails is not necessary. Therefore, the strategy pattern can be used to reduce the dependence on the external email service, thereby improving the flexibility and scalability of the `sendEmail` function.
[0126] Automatically refactoring frequently called functions can significantly improve code quality and system performance. Furthermore, refactoring functions not only reduces resource consumption and maintenance costs but also enhances code flexibility and scalability, further supporting continuous optimization and improvement of the project.
[0127] In one embodiment of this application, when a function is refactored, the function metadata and directed graph in the database are updated. This helps the project maintain high efficiency and optimization, especially during project expansion or changes in requirements.
[0128] This application's embodiments parse the target file and determine its required functions, then search for reusable code in the corresponding code library. This effectively reduces repetitive code writing and improves development efficiency. Furthermore, no developer intervention is required during the target code generation process, thus achieving automated code generation.
[0129] In this embodiment of the application, a system for automated code generation and management of a large language model and the Spring Cloud framework will be constructed.
[0130] Reference Figure 2 This is a system architecture diagram for automated code generation and management, which includes four parts: front-end service, back-end service, model layer and data layer.
[0131] 1) Front-end service
[0132] Functionality: Provides user interface and interactive features.
[0133] Components: Vue (a JavaScript framework for building user interfaces) web pages and apps (Applications).
[0134] Use the Vue framework to build web pages and apps, providing users with a user-friendly interface and interactive experience.
[0135] 2) Backend services
[0136] Function: Handles business logic and data operations.
[0137] Components: Function call monitoring, database, model interaction, and front-end functionality support.
[0138] It is responsible for handling front-end requests, performing business logic processing and data operations, and exchanging data with the model layer through model interaction.
[0139] 3) Model layer
[0140] Functionality: Provides various models for automated code generation and management.
[0141] Components: File parsing model, code reuse model, target code model, code splitting model, function processing model.
[0142] ① File parsing model
[0143] Input: Target file.
[0144] Function: All functions in the target file are broken down into different interfaces to implement the functions, and the target functions that the target file needs to implement are determined, which facilitates the subsequent project generation.
[0145] Output: Output the target function, all interfaces and the inputs and outputs of each interface, and detailed functions.
[0146] ② Reuse code model
[0147] Input: The code library corresponding to the target function and the target file.
[0148] Functionality: The code reuse model analyzes existing code in the codebase to identify which functions, services, or modules can be reused and which need to be rebuilt. This model infers which code logic can be reused based on the existing project structure and dependencies in Spring Cloud (such as service calls, load balancing, service discovery, etc.).
[0149] Output: Reusable code.
[0150] The code reuse model identifies existing code in the codebase to implement the target functionality and provides directly reusable code to the code generation module. Existing code in the codebase refers to project code containing multiple functionalities.
[0151] ③ Target code model
[0152] Input: Target functionality and reusable code.
[0153] Functionality: The code generation model generates user-specified functional modules and breaks them down into different functions. When generating target code, it references the output of the code reuse model, reusing functions from the codebase. The generated target code maintains project consistency and modularity, ensuring the independence and scalability of each service and module in the system architecture.
[0154] Output: Target code.
[0155] When the existing codebase does not contain code to implement the function, the target code module generates the corresponding code for that function, and generates a complete target code to implement the target function based on the reusable code provided in the reusable code model.
[0156] ④ Code splitting model
[0157] Input: Target code.
[0158] Functionality: The code splitting model breaks down the target code into multiple functions and automatically generates detailed functional descriptions and purposes for each function. Simultaneously, it generates a directed graph that records the call relationships between functions.
[0159] Output: Metadata for the function, functional description of the function, and directed graph.
[0160] 4) Data Layer
[0161] Function: Store and manage data.
[0162] Components: MySQL (an open-source relational database management system) and Neo4j (an open-source graph database management system).
[0163] We use two databases, MySQL and Neo4j, to store structured data and graph data, respectively.
[0164] In addition, the model layer in the system architecture also includes a function call monitoring and refactoring model, a function logging model, and a function documentation model.
[0165] Function call monitoring and refactoring model: It continuously monitors the number of times each function is called, and when the number of times a function is called exceeds a certain threshold, the function will be automatically refactored.
[0166] Function record model: When a function is refactored, the function metadata and directed graph in the database are updated.
[0167] Function documentation model: Automatically generates function documentation, recording the function's purpose, inputs and outputs, exception handling, and other relevant information. This helps developers understand the functions and facilitates the integration of front-end and back-end services.
[0168] Reference Figure 3 This is a flowchart illustrating a process for implementing code generation in the model layer according to this application. Specifically, the file parsing model outputs the target function to the reusable code model, the reusable code model outputs the reusable code to the target code model, the target code model outputs the target code to the code splitting model, the code splitting model splits the target code into multiple functions, and automatically generates a detailed functional description and function for each function. At the same time, it generates a directed graph to record the calling relationship between functions.
[0169] This application's embodiments parse the target file and determine its required functions, then search for reusable code in the corresponding code library. This effectively reduces repetitive code writing and improves development efficiency. Furthermore, no developer intervention is required during the target code generation process, thus achieving automated code generation.
[0170] It should be noted that, for the sake of simplicity, the method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments of this application are not limited to the described order of actions, because according to the embodiments of this application, some steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also understand that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of this application.
[0171] Based on the above embodiments, this embodiment also provides a file-based code generation device, which can be applied to electronic devices such as terminal devices and servers.
[0172] Reference Figure 4 The diagram illustrates a structural block diagram of an embodiment of a file-based code generation apparatus according to this application, which may specifically include the following modules:
[0173] The target function module 401 is used to parse the target file to obtain the target function that the target file needs to implement; the target file has a corresponding code library.
[0174] The code reuse module 402 is used to determine reusable code in the code library corresponding to the target file based on the target function.
[0175] The function module 403 is used to identify the target function that cannot be achieved by the reused code as the function to be implemented;
[0176] A new code module 404 is added to create new code for implementing the aforementioned function.
[0177] The target code module 405 is used to generate target code for implementing the target function based on the reused code and the newly added code.
[0178] This application also provides a non-volatile readable storage medium storing one or more modules (programs). When these modules are applied to a device, they enable the device to execute the instructions for the method steps in this application.
[0179] This application provides one or more machine-readable media storing instructions that, when executed by one or more processors, cause an electronic device to perform one or more of the methods described in the above embodiments. In this application, the electronic device includes various types of devices such as terminal devices and servers (clusters).
[0180] The embodiments of this disclosure can be implemented as an apparatus configured as desired using any suitable hardware, firmware, software, or any combination thereof, including electronic devices such as terminal devices, servers (clusters), etc. Figure 5 An exemplary apparatus 500 is schematically shown that can be used to implement the various embodiments described in this application.
[0181] In one embodiment, Figure 5 An exemplary device 500 is shown, which includes one or more processors 502, a control module (chipset) 504 coupled to at least one of the processors 502, a memory 506 coupled to the control module 504, a non-volatile memory (NVM) / storage device 508 coupled to the control module 504, one or more input / output devices 510 coupled to the control module 504, and a network interface 512 coupled to the control module 504.
[0182] Processor 502 may include one or more single-core or multi-core processors, and processor 502 may include any combination of general-purpose processors or special-purpose processors (e.g., graphics processors, application processors, baseband processors, etc.). In some embodiments, device 500 can serve as a terminal device, server (cluster), or other device as described in the embodiments of this application.
[0183] In some embodiments, the apparatus 500 may include one or more computer-readable media (e.g., memory 506 or NVM / storage device 508) having instructions 514 and one or more processors 502 that are combined with the one or more computer-readable media and configured to execute the instructions 514 to implement the module and thus perform the actions described in this disclosure.
[0184] In one embodiment, the control module 504 may include any suitable interface controller to provide any suitable interface to at least one of the processors 502 and / or any suitable device or component communicating with the control module 504.
[0185] The control module 504 may include a memory controller module to provide an interface to the memory 506. The memory controller module may be a hardware module, a software module, and / or a firmware module.
[0186] Memory 506 may be used, for example, to load and store data and / or instructions 514 for device 500. In one embodiment, memory 506 may include any suitable volatile memory, such as suitable DRAM. In some embodiments, memory 506 may include double data rate type quad synchronous dynamic random access memory (DDR4 SDRAM).
[0187] In one embodiment, the control module 504 may include one or more input / output controllers to provide an interface to the NVM / storage device 508 and (one or more) input / output devices 510.
[0188] For example, NVM / storage device 508 may be used to store data and / or instructions 514. NVM / storage device 508 may include any suitable non-volatile memory (e.g., flash memory) and / or may include any suitable (one or more) non-volatile storage devices (e.g., one or more hard disk drives (HDDs), one or more optical disc drives (CDs), and / or one or more digital universal optical disc (DVD) drives).
[0189] NVM / storage device 508 may include storage resources that are physically part of a device on which device 500 is mounted, or that can be accessed by the device without needing to be part of the device. For example, NVM / storage device 508 may be accessed via a network through one or more input / output devices 510.
[0190] One or more input / output devices 510 may provide an interface for device 500 to communicate with any other suitable device. Input / output devices 510 may include communication components, audio components, sensor components, etc. A network interface 512 may provide an interface for device 500 to communicate via one or more networks. Device 500 may wirelessly communicate with one or more components of a wireless network according to any of one or more wireless network standards and / or protocols, such as accessing wireless networks based on communication standards, such as WiFi, 2G, 3G, 4G, 5G, etc., or combinations thereof.
[0191] In one embodiment, at least one of the processors 502 may be logically packaged with one or more controllers (e.g., memory controller modules) of the control module 504. In one embodiment, at least one of the processors 502 may be logically packaged with one or more controllers of the control module 504 to form a system-in-package (SiP). In one embodiment, at least one of the processors 502 may be integrated with the logic of one or more controllers of the control module 504 on the same die. In one embodiment, at least one of the processors 502 may be integrated with the logic of one or more controllers of the control module 504 on the same die to form a system-on-a-chip (SoC).
[0192] In various embodiments, device 500 may be, but is not limited to, a terminal device such as a server, desktop computing device, or mobile computing device (e.g., laptop computing device, handheld computing device, tablet computer, netbook, etc.). In various embodiments, device 500 may have more or fewer components and / or different architectures. For example, in some embodiments, device 500 includes one or more cameras, a keyboard, a liquid crystal display (LCD) screen (including a touchscreen display), a non-volatile memory port, multiple antennas, a graphics chip, an application-specific integrated circuit (ASIC), and a speaker.
[0193] The detection device can use a main control chip as a processor or control module, and sensor data, position information, etc. can be stored in a memory or NVM / storage device. The sensor group can be used as an input / output device, and the communication interface can include a network interface.
[0194] As the device embodiment is basically similar to the method embodiment, the description is relatively simple, and relevant parts can be found in the description of the method embodiment.
[0195] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.
[0196] This application describes embodiments with reference to flowchart illustrations and / or block diagrams of methods, terminal devices (systems), and computer program products according to embodiments of this application. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable file-based code generation terminal device to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable file-based code generation terminal device, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0197] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable file-based code generation terminal device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0198] These computer program instructions can also be loaded onto a computer or other programmable file-based code generation terminal device, causing a series of operational steps to be performed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable terminal device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0199] Although preferred embodiments of the present application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments of the present application.
[0200] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or terminal device that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or terminal device. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or terminal device that includes said element.
[0201] The foregoing has provided a detailed description of a file-based code generation method and apparatus, an electronic device, and a storage medium provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A file-based code generation method, characterized in that, The method includes: The target file is parsed to obtain the target functions that the target file needs to implement; the target file has a corresponding code library; the target file is a description document of project requirements, used to define the functions and goals that the project needs to achieve. Analyze the code in the code library corresponding to the target file to determine the code logic corresponding to the code. Determining the code logic includes: identifying functions, services, modules in the code and their calling relationships, and identifying which code logic is independent and which code logic is interdependent. Based on the code logic corresponding to the code and the code logic corresponding to the target function, determine the reusable code; The target functions that cannot be achieved by the reused code are identified as functions to be implemented. Create new code to implement the functionality to be implemented; Based on the reused code and the newly added code, target code for implementing the target function is generated.
2. The method according to claim 1, characterized in that, The process of parsing the target file to obtain the target functions that the target file needs to implement includes: The target file is parsed to obtain its semantic information; The target function is determined based on the semantic information of the target file.
3. The method according to claim 1, characterized in that, The step of determining the reusable code based on the code logic corresponding to the code and the code logic corresponding to the target function includes: In the code library corresponding to the target file, traverse the code logic corresponding to the target function; Code with the same logic as the target function is identified as the reusable code.
4. The method according to claim 1, characterized in that, The step of generating target code for implementing the target function based on the reused code and the newly added code includes: Determine the target code structure used to implement the target function; The reused code and the newly added code are combined according to the target code structure to obtain the target code.
5. The method according to claim 1, characterized in that, After generating the target code corresponding to the target file based on the reused code and the newly added code, the method further includes: The target code is broken down into multiple functions, and the calling relationships between these functions are determined; each function has corresponding functionality and metadata. Construct a directed graph to manage the functions based on the calling relationships between the multiple functions; The directed graph, the functions corresponding to the functions, and the metadata corresponding to the functions are stored in the database.
6. The method according to claim 5, characterized in that, After storing the directed graph, the functionality corresponding to the function, and the metadata corresponding to the function in the database, the method further includes: The number of times the function is called is determined using the database; When the number of times the function is called exceeds a preset number, the function is refactored to optimize it.
7. A file-based code generation device, characterized in that, The device includes: The target function module is used to parse the target file to obtain the target functions that the target file needs to implement; the target file has a corresponding code library; the target file is a description document of project requirements, used to define the functions and goals that the project needs to achieve. The code reuse module is used to analyze the code in the code library corresponding to the target file, determine the code logic corresponding to the code, and determine the code logic corresponding to the code includes: identifying functions, services, modules in the code and their calling relationships, identifying which code logics are independent and which code logics are interdependent; and determining reusable code based on the code logic corresponding to the code and the code logic corresponding to the target function. The functional modules to be implemented are used to identify target functions that cannot be achieved by the reused code as functional modules to be implemented. A new code module is added to create new code to implement the functionality to be implemented. The target code module is used to generate target code for implementing the target function based on the reused code and the newly added code.
8. An electronic device, characterized in that, include: processor; and A memory having executable code stored thereon, which, when executed, causes the processor to perform the file-based code generation method as described in any one of claims 1-6.
9. One or more machine-readable media having executable code stored thereon, which, when executed, causes a processor to perform the file-based code generation method as described in any one of claims 1-6.
Citation Information
Patent Citations
Software development method and device based on artificial intelligence and electronic device
CN110795077A
Sample construction method, code generation method, electronic equipment and storage medium
CN117556263A