File-based code generation method and device, equipment and storage medium

By parsing the object file and looking for multiplexed code in the code base to generate object code, the code generation problem in the existing technology that requires developers to participate is solved, and the code generation is automatically generated and development efficiency is improved.

CN119806513AActive Publication Date: 2025-04-11CHINA TELECOM ARTIFICIAL INTELLIGENCE TECHNOLOGY (BEIJING) CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411805052.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-09
Publication Date
2025-04-11
Estimated Expiration
2044-12-09

AI Technical Summary

Technical Problem

The existing technology requires the participation of developers when generating code, and it is impossible to automatically generate code.

Method used

By parsing the target file, determining the target function it needs to be achieved, and looking for reused codes in the corresponding code base of the target file, creating new codes for implementing the functions to be implemented, and finally generating target codes for implementing the target function.

Benefits of technology

It effectively reduces the repeated writing of code, improves development efficiency, and realizes automatic code generation, reducing the participation of developers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119806513A_ABST
    Figure CN119806513A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a file-based code generation method and device, equipment and a storage medium. A target file is analyzed, and a target function needing to be achieved by the target file is obtained; the target file has a corresponding code library; according to the target function, determining a reuse code which can be reused in a code library corresponding to the target file; determining a target function which cannot be realized by the reuse code as a to-be-realized function; creating a newly added code for realizing the to-be-realized function; and according to the reuse code and the newly added code, generating a target code for realizing the target function. According to the embodiment of the invention, the target file is analyzed, the target function needing to be realized is determined, and then the reuse code which can be reused is searched in the code library corresponding to the target file, so that repeated compiling of the code can be effectively reduced, and the development efficiency is improved. And moreover, the participation of developers is not needed in the process of generating the target code, so that automatic code generation is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of artificial intelligence technology, and in particular to a file-based code generation method and device, an electronic device, and a storage medium. Background Art

[0002] With the rapid development of artificial intelligence technology, especially the maturity of deep learning and natural language processing technology, code generation based on large language models has gradually become an important tool in the field of software development. Large language models (such as GPT, Codex, etc.) can generate code snippets that meet user needs by learning a large number of code bases and programming language patterns. This technology significantly improves the efficiency of software development, especially the automation and intelligent completion of code writing and prompts.

[0003] In the related art, code logic is generated based on the developer's comments and descriptions when the developer is writing. However, the developer's participation is required in writing the code, and the ambiguity of the large language model's understanding must be constantly corrected. Summary of the invention

[0004] The embodiment of the present application provides a file-based code generation method to solve the problem that the developer participation is required in the process of code generation and automatic code generation cannot be achieved.

[0005] Correspondingly, an embodiment of the present application also provides a file-based code generation device, an electronic device and a storage medium to ensure the implementation and application of the above method.

[0006] In order to solve the above problems, the embodiment of the present application discloses a file-based code generation method, the method comprising:

[0007] Parsing the target file to obtain the target function that the target file needs to implement; the target file has a corresponding code library;

[0008] According to the target function, determining in a code library corresponding to the target file a reused code that can be reused;

[0009] Determine the target function that cannot be realized by the reused code as the function to be realized;

[0010] Creating new code for implementing the function to be implemented;

[0011] According to the reused code and the newly added code, a target code for realizing the target function is generated.

[0012] Optionally, parsing the target file to obtain a target function that the target file needs to implement includes:

[0013] Parsing the target file to obtain semantic information of the target file;

[0014] The target function is determined according to the semantic information of the target file.

[0015] Optionally, the target function has a corresponding code logic, and determining, according to the target function, in a code library corresponding to the target file, a reusable code that can be reused 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 reused code is determined according to the code logic corresponding to the code and the code logic corresponding to the target function.

[0018] Optionally, determining the reused code according to 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] The code having the same logic as the code corresponding to the target function is determined as the reused code.

[0021] Optionally, generating a target code for implementing the target function according to the reused code and the newly added code includes:

[0022] Determining a target code structure for implementing 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 according to the reused code and the newly added code, the method further includes:

[0025] Splitting the target code into multiple functions and determining the calling relationship between the multiple functions; the functions have corresponding functions and metadata;

[0026] Constructing a directed graph for managing the functions according to the calling relationships between the multiple functions;

[0027] The directed graph, the function corresponding to the function, and the metadata corresponding to the function are stored in a database.

[0028] Optionally, after storing the directed graph, the function corresponding to the function, and the metadata corresponding to the function in a database, the method further includes:

[0029] Determining the number of times the function is called through the database;

[0030] When the number of times the function is called exceeds a preset number, the function is reconstructed to optimize the function.

[0031] The embodiment of the present application also discloses a file-based code generation device, the device comprising:

[0032] A target function module is used to parse a target file to obtain a target function that the target file needs to implement; the target file has a corresponding code library;

[0033] A reuse code module, used to determine, according to the target function, in a code library corresponding to the target file, reused code that can be reused;

[0034] A function module to be implemented, used to determine the target function that cannot be implemented by the reused code as the function to be implemented;

[0035] A new code module is used to create new codes for implementing the functions to be implemented;

[0036] The target code module is used to generate a target code for realizing the target function according to the reused code and the newly added code.

[0037] An embodiment of the present application also discloses an electronic device, including: a processor; and a memory, on which executable code is stored. When the executable code is executed, the processor executes the file-based code generation method as described in any one of the embodiments of the present application.

[0038] The embodiments of the present application also disclose one or more machine-readable media on which executable codes are stored. When the executable codes are executed, the processor executes the file-based code generation method as described in any one of the embodiments of the present application.

[0039] Compared with the prior art, the embodiments of the present application have the following advantages:

[0040] In an embodiment of the present application, the target file is parsed to obtain the target function that the target file needs to implement; the target file has a corresponding code library; according to the target function, in the code library corresponding to the target file, a reused code that can be reused is determined; the target function that cannot be implemented by the reused code is determined as a function to be implemented; a new code for implementing the function to be implemented is created; according to the reused code and the new code, a target code for implementing the target function is generated. The embodiment of the present application parses the target file and determines the target function that needs to be implemented, and then searches for the reused code that can be reused in the code library corresponding to the target file, which can effectively reduce the repeated writing of code and improve development efficiency. In addition, there is no need for developers to participate in the process of generating the target code, thereby realizing automatic code generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 is a flowchart of a step of an embodiment of a file-based code generation method of the present application;

[0042] Figure 2 It is a system architecture diagram for automated code generation and management of this application;

[0043] Figure 3 This is a flowchart of implementing code generation in the model layer of the present application;

[0044] Figure 4 It is a structural block diagram of an embodiment of a file-based code generation device of the present application;

[0045] Figure 5 It is a schematic diagram of the structure of a device provided in one embodiment of the present application. DETAILED DESCRIPTION

[0046] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0047] In the embodiment of the present application, the automatic generation of code is achieved based on the SpringCloud 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) that aims to simplify the development of common patterns in distributed systems. Spring Cloud provides a series of tools and libraries to help developers quickly build and deploy microservice applications. The core functions of Spring Cloud include service discovery, configuration management, load balancing, circuit breakers, distributed messaging, etc.

[0049] Reference Figure 1 , is a flowchart of a file-based code generation method embodiment of the present application, comprising the following steps:

[0050] Step 101, parsing a target file to obtain a target function that the target file needs to implement; the target file has a corresponding code library;

[0051] In the embodiment of the present application, the target file is a description document of the project requirements, which defines the functions and goals that the project needs to achieve. The target file is the starting point and basis for the generation of the project, and the project is a specific implementation generated according to the target file, including code, etc. The goal of the project is to achieve the functions described in the target file. Specifically, the target file is the input of 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, the target file is parsed to understand the functional requirements described in the file, that is, the target functions that the target file needs to achieve. In addition, the target file is also associated with a code library, which contains existing code resources. Specifically, the code library corresponding to the target file is pre-defined by the developer based on the target file and contains existing code resources.

[0053] In addition, in addition to determining the target functions that the target file needs to implement, it is also necessary to parse and obtain the function interfaces, which are the key components for implementing the target functions.

[0054] By parsing the target file, we can accurately understand the project requirements, provide a clear direction for subsequent code generation, and automate the process from requirements to implementation, improving development efficiency and accuracy. At the same time, linking to the code library helps to utilize existing code resources and improve development efficiency.

[0055] Step 102: Determine, according to the target function, in the code library corresponding to the target file, a reused code that can be reused;

[0056] In the process of generating a project, we will search for reusable code snippets in the existing code base according to the functional requirements (target functions) described in the target file, and these code snippets are called reused code. Reusable code can be directly used to implement part or all of the target functions, thereby reducing development time and workload.

[0057] The embodiments of the present application can reduce duplication of work and improve development efficiency by reusing existing code, and also help maintain the consistency and quality of code. By searching for reused code in an existing code base, existing code resources can be more effectively utilized to avoid the generation of redundant code.

[0058] Step 103: determining the target function that cannot be realized by the reused code as the function to be realized;

[0059] In an embodiment of the present application, after the reused code is determined, the functions that can be implemented by the reused code are analyzed, and those target functions that cannot be implemented by the reused code are marked as "functions to be implemented".

[0060] Based on this, it is possible to determine which functions require new development to avoid omissions or duplication of work, and to focus code generation on functions that require new code implementation, thereby improving development efficiency.

[0061] Step 104: Create new code for implementing the function to be implemented;

[0062] In the embodiment of the present application, after the functions to be implemented are determined, new codes will be generated to implement these functions, and the newly added codes are specially created to meet the functions to be implemented.

[0063] Create additional code to implement the target functionality that cannot be accomplished with reused code.

[0064] Step 105: Generate a target code for implementing the target function according to 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 requirements in the target file, while utilizing existing code resources to improve development efficiency and code quality.

[0067] The embodiment of the present application analyzes the target file and determines the target function to be implemented, and then searches for reusable code in the code library corresponding to the target file, which can effectively reduce the duplication of code writing and improve development efficiency. In addition, in the process of generating the target code, there is no need for developers to participate, thereby realizing automatic code generation.

[0068] In one embodiment of the present application, the step 101 of parsing the target file to obtain the target function that the target file needs to implement includes:

[0069] Parsing the target file to obtain semantic information of the target file;

[0070] The target function is determined according to the semantic information of the target file.

[0071] In the embodiment of the present application, in order to determine the target function, the target file will be deeply parsed, not just simple text parsing, but through natural language processing (NLP) or other advanced parsing technology, the semantic information in the target file is extracted. The semantic information includes but is not limited to the key concepts, function descriptions, requirement details, etc. in the file.

[0072] By parsing the target file and obtaining its semantic information, we can more accurately understand the content of the file and avoid misunderstanding or missing key information.

[0073] After obtaining the semantic information of the target file, the specific functions that the target file needs to implement will be determined based on this information. These functions are determined based on the description 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 of the target file, avoiding function omissions or errors, which helps the accuracy and reliability of subsequent target codes, thereby improving the accuracy and reliability of the project.

[0075] In addition, automated semantic parsing and determination of target functionality avoids developer involvement and improves target code development efficiency and consistency.

[0076] The embodiment of the present application parses the semantic information of the target file and determines the target function based on it, so as to understand the project requirements more accurately and completely and ensure that the functional implementation of the project is completely consistent with the description of the target file. This not only improves the accuracy and reliability of the project, but also avoids the participation of developers and improves development efficiency.

[0077] In one embodiment of the present application, the target function has corresponding code logic, and the step 102, according to the target function, determines the reused code that can be reused in the code library corresponding to the target file, including:

[0078] Analyze the code in the code library corresponding to the target file to determine the code logic corresponding to the code;

[0079] The reused code is determined according to the code logic corresponding to the code and the code logic corresponding to the target function.

[0080] In an embodiment of the present application, the code in the code library corresponding to the target file is analyzed to extract the logic of each code fragment, which includes identifying the functions, services, modules in the code and their calling relationships with each other, and then being able to identify which code logics are independent and which are interdependent.

[0081] By analyzing the code logic in the code base, we can fully understand the function and structure of the existing code, and provide detailed information for the subsequent reuse code determination.

[0082] After the code logic in the code base is determined, it will be compared with the code logic corresponding to the target function. Through comparison, it is possible to identify which code logic matches the requirements of the target function, thereby determining which code can be reused.

[0083] Specifically, in the Spring Cloud framework, dependencies such as service calls, load balancing, and service discovery are the core components for building a microservice architecture. By combining these dependencies, you can more specifically infer which code logic in the code base can be reused.

[0084] Service calls: In Spring Cloud, service calls are usually implemented through tools such as RestTemplate, Feign, or WebClient, which allow one service to call another service.

[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 a common service call logic corresponding to the code in the code base, then the code corresponding to this logic can be reused.

[0086] Load balancing: Spring Cloud implements load balancing through Ribbon or Spring Cloud LoadBalancer. The load balancing logic usually involves selecting a service instance to handle the request.

[0087] For example, suppose there is a user service that needs to call multiple instances of the log service to record user operations. If there is already a common load balancing logic corresponding code in the code base, then the logic corresponding code can be reused.

[0088] Service discovery: Spring Cloud implements service discovery through Eureka or Consul. The service discovery logic usually involves registering and discovering service instances.

[0089] For example, suppose there is a configuration service that needs to discover and call multiple instances of monitoring services to obtain system status. If there is already a common service discovery logic corresponding to the code in the code base, then the code corresponding to this logic can be reused.

[0090] By combining the dependencies in Spring Cloud (such as service calls, load balancing, service discovery, etc.), the embodiments of this application can more specifically infer which code logic in the code base can be reused. The reuse of the code corresponding to these common logics not only reduces duplication of work and improves development efficiency, but also maintains the consistency and quality of the code, and improves the reliability and maintainability of the project.

[0091] In one embodiment of the present application, determining the reused code according to 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] The code having the same logic as the code corresponding to the target function is determined as the reused code.

[0094] In order to compare the code logic corresponding to the code with the code logic corresponding to the target function, first, the logic of each code snippet is checked one by one in the code base corresponding to the target file. This process is similar to a comprehensive scan of the code base to ensure that no code snippets that may be the same as the target function logic are missed.

[0095] By traversing the code logic in the code base, you can fully understand the functionality and structure of the existing code, ensuring that no potential reused code is missed.

[0096] During the traversal process, the code logic in the code base will be compared with the code logic corresponding to the target function one by one. If the logic of a code snippet is found to be exactly the same as the logic of the target function, the code snippet will be automatically determined as reused code.

[0097] By comparing code logic and target function logic, we can accurately determine which codes can be reused, avoiding unnecessary code rewriting and improving development efficiency and code quality.

[0098] In one embodiment of the present application, the step 105, generating a target code for implementing the target function according to the reused code and the newly added code, includes:

[0099] Determining a target code structure for implementing 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, the overall structure of the target code is determined first. This includes determining the module division of the code, function call relationships, class structure, etc. The target code structure is determined based on the requirements and design specifications of the target function.

[0102] By determining the target code structure, we can ensure that the generated code has a good organization and structure, which is convenient for subsequent maintenance and expansion.

[0103] After the target code structure is determined, the reused code and the new code will be combined according to the predetermined structure. This process is similar to assembling different building blocks according to the design drawings to form a complete code module.

[0104] By combining according to the target code structure, it can be ensured that the generated code modules are consistent and complete and meet all requirements of the target functions.

[0105] In one embodiment of the present application, after generating the target code corresponding to the target file according to the reused code and the newly added code in step 105, the method further includes:

[0106] Splitting the target code into multiple functions and determining the calling relationship between the multiple functions; the functions have corresponding functions and metadata;

[0107] Constructing a directed graph for managing the functions according to the calling relationships between the multiple functions;

[0108] The directed graph, the function corresponding to the function, and the metadata corresponding to the function are stored in a database.

[0109] After the target code is generated, it will be split into multiple independent functions, each with a clear functional description (such as a function is responsible for processing orders, another function is responsible for interacting with the database) and metadata (such as function name, parameters, return value, etc.). The calling relationship between these functions will also be analyzed to determine which functions call other functions.

[0110] By splitting the target code into multiple functions and determining the calling relationships, we can better understand the structure and logic of the code, which facilitates subsequent management and optimization.

[0111] Then, a directed graph is generated based on the calling relationships between functions. In this graph, each node represents a function, and each edge represents the calling relationship between functions. In this way, the calling 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, the functional description and metadata of the function will be stored in the database, which is helpful for long-term management and query, and improves the maintainability and traceability of the code.

[0114] In one embodiment of the present application, after the target code is split into multiple functions, a function document will be automatically generated to record the function, input and output, exception handling and other related information of each function. The function document helps developers understand the target code and facilitates the connection between front-end and back-end services.

[0115] After the target code is generated, the embodiment of the present application can better manage and optimize the code by splitting the target code into multiple functions, determining the calling relationship between functions, building a directed graph and storing relevant information. This process not only improves the readability and maintainability of the code, but also facilitates tracking and optimizing the function calling relationship, supports subsequent code analysis and optimization, and improves development efficiency.

[0116] In one embodiment of the present application, after storing the directed graph, the function corresponding to the function, and the metadata corresponding to the function in a database, the method further includes:

[0117] Determining the number of times the function is called through the database;

[0118] When the number of times the function is called exceeds a preset number, the function is reconstructed to optimize the function.

[0119] Since the database stores information related to functions, the function call information stored in the database will be continuously monitored to count the number of times each function is called. This process can be achieved through database query and log analysis.

[0120] For example, the number of times each function is called can be counted through database query; 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 automatically recording and counting the number of times a function is called, we can not only understand the actual usage of the function in real time and provide data support for subsequent function optimization and reconstruction, but also reduce manual intervention and improve development efficiency and consistency.

[0122] When the number of calls to a function exceeds the preset threshold, the refactoring mechanism will be automatically triggered. The refactoring process may include optimizing the performance of the function, simplifying the code logic, reducing unnecessary dependencies, etc. The goal of refactoring is to improve the efficiency and maintainability of the function.

[0123] For example, suppose there is a function processOrder, which is responsible for processing orders and updating the database. This function is frequently called during peak hours, resulting in excessive database load. Therefore, the database can be updated in batches to reduce the number of database connections and queries, thereby improving the performance of the function processOrder.

[0124] Another example, suppose there is a function calculateTotalPrice, which is responsible for calculating the total price of an order. This function has complex logic and contains multiple nested conditional judgments. Therefore, the complex logic can be split into multiple small 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 that is responsible for sending emails. This function relies on an external mail service, but in some cases it is not necessary to send emails. Therefore, the strategy pattern can be used to reduce the dependence on the external mail service, thereby improving the flexibility and scalability of the sendEmail function.

[0126] By automatically refactoring frequently called functions, the quality of the code and the performance of the system can be significantly improved. Moreover, refactoring functions can not only reduce resource consumption and maintenance costs, but also improve the flexibility and scalability of the code, further supporting the continuous optimization and improvement of the project.

[0127] In one embodiment of the present application, after a function is reconstructed, the function metadata and directed graph in the database are updated, which helps the project to continue to maintain high efficiency and optimization, especially during project expansion or changes in requirements.

[0128] The embodiment of the present application analyzes the target file and determines the target function to be implemented, and then searches for reusable code in the code library corresponding to the target file, which can effectively reduce the duplication of code writing and improve development efficiency. In addition, in the process of generating the target code, there is no need for developers to participate, thereby realizing automatic code generation.

[0129] In the embodiment of the present application, a system for automatic code generation and management of a large language model and Spring Cloud framework will be built.

[0130] Reference Figure 2 , is a system architecture diagram for automatic code generation and management in this application, which specifically includes four parts: front-end service, back-end service, model layer and data layer.

[0131] 1) Front-end service

[0132] Function: Provide user interface and interactive functions.

[0133] Components: web pages and apps based on Vue (a JavaScript framework for building user interfaces).

[0134] Build web pages and apps through the Vue framework to provide users with a friendly interface and interactive experience.

[0135] 2) Backend Services

[0136] Function: Process business logic and data operations.

[0137] Components: function call monitoring, database, model interaction, and front-end function support.

[0138] Responsible for processing 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] Function: Provides various models for automated code generation and management.

[0141] Components: file parsing model, reuse code model, target code model, code splitting model, function processing model.

[0142] ①File parsing model

[0143] Input: Target file.

[0144] Function: Split all functions in the target file into different interfaces for implementing the functions in the form of functional interfaces, and determine the target functions that the target file needs to implement, to facilitate subsequent project generation.

[0145] Output: Output the target function, all interfaces, input and output of each interface, and detailed functions.

[0146] ②Reuse code model

[0147] Input: code base corresponding to the target function and target files.

[0148] Function: The reusable code model analyzes the existing code in the code base to identify which functions, services or modules can be reused and which need to be regenerated. This model will infer 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 reuse code model determines whether there is code for implementing the target function in the existing code base, and provides the reuse code that can be directly reused to the code generation module. The existing code in the code base refers to a project code that contains multiple functions.

[0151] ③Target code model

[0152] Input: target function and reused code.

[0153] Functionality: The code generation model generates user-specified functional modules and divides them into different functions. When generating the target code, the output of the reuse code model is referenced to reuse the functions in the code base. The generated target code will maintain the consistency and modularity of the project to ensure that each service and module in the system architecture is independent and scalable.

[0154] Output: object code.

[0155] When there is no code for implementing the function in the existing code library, the target code module generates the code corresponding to those functions, and generates a complete target code for implementing the target function based on the reuse code provided in the reuse code model.

[0156] ④Code Splitting Model

[0157] Input: Target code.

[0158] Function: The code splitting model splits the target code into multiple functions and automatically generates a detailed function description and function for each function. At the same time, a directed graph is generated to record the calling relationship between functions.

[0159] Output: metadata corresponding to the function, functional description corresponding to 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), Neo4j (an open source graph database management system).

[0163] Use MySQL and Neo4j databases to store structured data and graph data respectively.

[0164] In addition, the model layer in the system architecture also includes function call monitoring and reconstruction model, function recording model and function documentation model.

[0165] Function call monitoring and reconstruction model: It continuously monitors the number of calls to each function. When the number of calls to a function exceeds a certain threshold, the function will be automatically reconstructed.

[0166] Function recording model: When a function is refactored, the function metadata and directed graph in the database are updated.

[0167] Function documentation model: Automatically generate function documentation to record the function, input and output, exception handling and other related information of each function, which helps developers understand the function and facilitates the connection between front-end and back-end services.

[0168] Reference Figure 3 , is a flow chart of implementing code generation in the model layer of the present application, specifically, the file parsing model outputs the target function to the reused code model, the reused code model outputs the reused 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, and at the same time, generates a directed graph to record the calling relationship between functions.

[0169] The embodiment of the present application analyzes the target file and determines the target function to be implemented, and then searches for reusable code in the code library corresponding to the target file, which can effectively reduce the duplication of code writing and improve development efficiency. In addition, in the process of generating the target code, there is no need for developers to participate, thereby realizing automatic code generation.

[0170] It should be noted that, for the method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present application are not limited by the described order of actions, because according to the embodiments of the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present application.

[0171] On the basis of the above embodiments, this embodiment further provides a file-based code generation device, which is applied to electronic devices such as terminal devices and servers.

[0172] Reference Figure 4 , shows a structural block diagram of an embodiment of a file-based code generation device of the present 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 achieve; the target file has a corresponding code library;

[0174] A reuse code module 402, configured to determine, according to the target function, in a code library corresponding to the target file, reused code that can be reused;

[0175] The function module 403 to be implemented is used to determine the target function that cannot be implemented by the reused code as the function to be implemented;

[0176] A new code module 404 is used to create new codes for implementing the functions to be implemented;

[0177] The target code module 405 is used to generate a target code for realizing the target function according to the reused code and the newly added code.

[0178] The embodiment of the present application also provides a non-volatile readable storage medium, which stores one or more modules (programs). When the one or more modules are applied to a device, the device can execute instructions (instructions) of each method step in the embodiment of the present application.

[0179] The present application embodiment provides one or more machine-readable media on which instructions are stored, and when executed by one or more processors, an electronic device executes one or more of the methods described in the above embodiments. In the present application embodiment, the electronic device includes various types of devices such as terminal devices and servers (clusters).

[0180] The embodiments of the present disclosure may be implemented as a device configured as desired using any appropriate hardware, firmware, software, or any combination thereof, and the device may include electronic devices such as terminal devices and servers (clusters). Figure 5 An exemplary apparatus 500 that can be used to implement various embodiments described in this application is schematically shown.

[0181] For one embodiment, Figure 5 An exemplary apparatus 500 is shown having one or more processors 502, a control module (chip set) 504 coupled to at least one of the processor(s) 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] The processor 502 may include one or more single-core or multi-core processors, and the processor 502 may include any combination of general-purpose processors or special-purpose processors (such as graphics processors, application processors, baseband processors, etc.). In some embodiments, the device 500 can be used as a terminal device, server (cluster), etc. described in the embodiments of the present 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 configured in conjunction with the one or more computer-readable media to execute the instructions 514 to implement a module to perform the actions described in the present disclosure.

[0184] For one embodiment, control module 504 may include any suitable interface controller to provide any suitable interface to at least one of processor(s) 502 and / or any suitable device or component in communication with 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] The memory 506 may be used, for example, to load and store data and / or instructions 514 for the device 500. For one embodiment, the memory 506 may include any suitable volatile memory, such as a suitable DRAM. In some embodiments, the memory 506 may include double data rate type four synchronous dynamic random access memory (DDR4 SDRAM).

[0187] For one embodiment, control module 504 may include one or more input / output controllers to provide an interface to NVM / storage device 508 and input / output device(s) 510 .

[0188] For example, NVM / storage 508 may be used to store data and / or instructions 514. NVM / storage 508 may include any suitable non-volatile memory (e.g., flash memory) and / or may include any suitable non-volatile storage device(s) (e.g., one or more hard disk drives (HDDs), one or more compact disk (CD) drives, and / or one or more digital versatile disk (DVD) drives).

[0189] NVM / storage device 508 may include storage resources that are physically part of the device on which apparatus 500 is installed, or it may be accessible to the device without being part of the device. For example, NVM / storage device 508 may be accessed via input / output device(s) 510 over a network.

[0190] (One or more) input / output devices 510 may provide an interface for the apparatus 500 to communicate with any other appropriate device, and the input / output device 510 may include a communication component, an audio component, a sensor component, etc. The network interface 512 may provide an interface for the apparatus 500 to communicate through one or more networks, and the apparatus 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, for example, accessing a wireless network based on a communication standard, such as WiFi, 2G, 3G, 4G, 5G, etc., or a combination thereof for wireless communication.

[0191] For one embodiment, at least one of the processor(s) 502 may be packaged together with the logic of one or more controllers (e.g., a memory controller module) of the control module 504. For one embodiment, at least one of the processor(s) 502 may be packaged together with the logic of one or more controllers of the control module 504 to form a system-in-package (SiP). For one embodiment, at least one of the processor(s) 502 may be integrated on the same die with the logic of one or more controllers of the control module 504. For one embodiment, at least one of the processor(s) 502 may be integrated on the same die with the logic of one or more controllers of the control module 504 to form a system-on-chip (SoC).

[0192] In various embodiments, the apparatus 500 may be, but is not limited to, a terminal device such as a server, a desktop computing device, or a mobile computing device (e.g., a laptop computing device, a handheld computing device, a tablet computer, a netbook, etc.). In various embodiments, the apparatus 500 may have more or fewer components and / or a different architecture. For example, in some embodiments, the apparatus 500 includes one or more cameras, a keyboard, a liquid crystal display (LCD) screen (including a touch screen display), a non-volatile memory port, multiple antennas, a graphics chip, an application-specific integrated circuit (ASIC), and a speaker.

[0193] Among them, the main control chip can be used as a processor or control module in the detection device, sensor data, location information, etc. are stored in a memory or NVM / storage device, the sensor group can be used as an input / output device, and the communication interface may include a network interface.

[0194] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0195] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0196] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable file-based code generation terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable file-based code generation terminal device generate instructions for implementing the processes in the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0197] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable file-based code generation terminal device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A 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 generating terminal device, so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, so that the instructions executed on the computer or other programmable terminal device provide for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0199] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present application.

[0200] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.

[0201] The above is a detailed introduction to a file-based code generation method and device, an electronic device and a storage medium provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for a person skilled in the art, according to the idea of ​​the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. A file-based code generation method, characterized in that: The method comprises: Parsing the target file to obtain the target function that the target file needs to implement; the target file has a corresponding code library; According to the target function, determining in a code library corresponding to the target file a reused code that can be reused; Determine the target function that cannot be realized by the reused code as the function to be realized; Creating new code for implementing the function to be implemented; According to the reused code and the newly added code, a target code for realizing the target function is generated.

2. The method according to claim 1, characterized in that The parsing of the target file to obtain the target function that the target file needs to implement includes: Parsing the target file to obtain semantic information of the target file; The target function is determined according to the semantic information of the target file.

3. The method according to claim 1, characterized in that The target function has a corresponding code logic, and determining, according to the target function, in a code library corresponding to the target file, a reused code that can be reused includes: Analyze the code in the code library corresponding to the target file to determine the code logic corresponding to the code; The reused code is determined according to the code logic corresponding to the code and the code logic corresponding to the target function.

4. The method according to claim 3, characterized in that The determining the reused code according to 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; The code having the same logic as the code corresponding to the target function is determined as the reused code.

5. The method according to claim 1, characterized in that The generating, according to the reused code and the newly added code, a target code for realizing the target function comprises: Determining a target code structure for implementing the target function; The reused code and the newly added code are combined according to the target code structure to obtain the target code.

6. The method according to claim 1, characterized in that After generating the target code corresponding to the target file according to the reused code and the newly added code, the method further includes: Splitting the target code into multiple functions and determining the calling relationship between the multiple functions; the functions have corresponding functions and metadata; Constructing a directed graph for managing the functions according to the calling relationships between the multiple functions; The directed graph, the function corresponding to the function, and the metadata corresponding to the function are stored in a database.

7. The method according to claim 6, characterized in that After storing the directed graph, the function corresponding to the function, and the metadata corresponding to the function in a database, the method further includes: Determining the number of times the function is called through the database; When the number of times the function is called exceeds a preset number, the function is reconstructed to optimize the function.

8. A file-based code generation device, characterized in that: The device comprises: A target function module is used to parse a target file to obtain a target function that the target file needs to implement; the target file has a corresponding code library; A reuse code module, used to determine, according to the target function, in a code library corresponding to the target file, reused code that can be reused; A function module to be implemented, used to determine the target function that cannot be implemented by the reused code as the function to be implemented; A new code module is used to create new codes for implementing the functions to be implemented; The target code module is used to generate a target code for realizing the target function according to the reused code and the newly added code.

9. An electronic device, characterized in that: include: processor; and A memory having executable codes stored therein, which, when executed, causes the processor to execute the file-based code generation method according to any one of claims 1 to 7.

10. One or more machine-readable media having executable codes stored thereon, which, when executed, enable a processor to execute the file-based code generation method according to any one of claims 1 to 7.

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

  • Code generation method and device, program and storage medium

    CN118672593A