Code generation method and device, equipment and storage medium
By filtering and verifying the reusable code metamodel in the code model library, the problems of low-code platforms in model-to-code conversion and universality are solved, and multi-domain program code generation and efficient code generation universality are achieved.
Patent Information
- Application Number
- CN202411686657.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-22
- Publication Date
- 2025-02-28
AI Technical Summary
The existing low-code platform is difficult to implement in terms of model-to-specific code conversion, and its scope of use is limited, so it cannot achieve universality and is only suitable for vertical applications.
The target reusable code metamodel corresponding to the code generation requirement information is filtered out through the pre-built code model library, and the pre-built rule library is used to checksum combination to generate multi-domain program code.
On the basis of ensuring the accuracy of code generation, we realize the generation of multi-domain program code and improve the universality of code generation in the application field.
Smart Images

Figure CN119536720A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code generation method, device, equipment and storage medium. Background Art
[0002] Low-code technology refers to technology that can complete application construction by writing a small amount of code or no code. Low-code technology can improve development efficiency, shorten application delivery cycle, save development costs and lower technical barriers.
[0003] In related technologies, low-code platforms use template-based code generation technology or model-driven code generation technology. Template-based code generation technology has poor flexibility; model-driven code generation technology is powerful, but it is difficult to implement the conversion from model to specific code, and its scope of use is limited. In actual applications, it can only be applied in a few scenarios, and in most cases, it can only generate relatively simple code. For the above reasons, most low-code platforms are currently only applicable to vertical application fields and cannot achieve universality. Summary of the invention
[0004] In view of this, the purpose of the present invention is to provide a code generation method, device, equipment and storage medium, which can realize the generation of multi-field program codes on the basis of ensuring the accuracy of code generation and improve the universality of code generation in application fields. The specific scheme is as follows:
[0005] In a first aspect, the present application discloses a code generation method, comprising:
[0006] According to the code generation requirement information, a target reusable code metamodel corresponding to the code generation requirement information is screened out from a pre-built code model library; the code model library includes reusable code metamodels corresponding to various types of services, and the reusable code metamodel includes one or more of an application template class model, a function module class model, and a code snippet class model;
[0007] Verifying the target reusable code metamodel using a pre-built rule base to obtain a verified target reusable code metamodel;
[0008] All the verified target reusable code metamodels are combined to obtain program code corresponding to the code generation requirement information.
[0009] Optionally, before filtering out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information, the method further includes:
[0010] Acquire the code generation requirement information; the code generation requirement information includes first requirement information for a parent metamodel and second requirement information for a child metamodel; the application template class model is a parent metamodel, and the function module class model and the code snippet class model are child metamodels;
[0011] The step of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information includes:
[0012] Filtering an application template class model corresponding to the first requirement information from a pre-built code model library to obtain a target application template model;
[0013] Determine all sub-level meta-models corresponding to the target application template model to obtain a target sub-level meta-model;
[0014] Filtering out the function module class model and the code snippet class model corresponding to the second requirement information from all the target sub-level meta-models to obtain a target function module model and a target code snippet model;
[0015] Based on the target application template model, the target function module model and the target code snippet model, a target reusable code metamodel corresponding to the code generation requirement information is obtained.
[0016] Optionally, before determining all sub-level meta-models corresponding to the target application template model, the method further includes:
[0017] Determine the number of the screened target application template models;
[0018] If two or more target application template models are selected, the code generation process ends;
[0019] If only one target application template model is screened out, the operation of determining all sub-level meta-models corresponding to the target application template model is performed.
[0020] Optionally, the using of a pre-built rule base to verify the target reusable code metamodel to obtain a verified target reusable code metamodel includes:
[0021] Utilize a pre-built rule library to filter out target reusable code metamodels that do not conform to the rules; the rule library includes metamodel custom rules, global custom rules, and engine built-in rules;
[0022] The verified target reusable code metamodel is obtained based on the remaining metamodels after screening.
[0023] Optionally, the construction of the code model library includes:
[0024] Extract the rules and operations involved in the application to obtain a functional module model and a code snippet model corresponding to the application;
[0025] Extracting the project framework of the application to obtain an application template model of the application, and adding an identifier at a target location in the application template model; the identifier is used to indicate that the target location is used to add the function module class model or the code snippet class model;
[0026] The code model library is obtained based on the application template models, function module models and code snippet models corresponding to various application programs.
[0027] Optionally, combining all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information includes:
[0028] According to the attribute information of the identifier of the application template model in the target reusable code metamodel, the function module model and the code fragment model in the target reusable code metamodel are added to the corresponding target position to obtain the program code corresponding to the code generation requirement information.
[0029] Optionally, the code generation requirement information includes a requirement description text and / or a metamodel restriction condition for describing the code generation requirement;
[0030] The step of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information includes:
[0031] If the code generation requirement information includes a requirement description text for describing the code generation requirement, then comparing the requirement description text with the function description text corresponding to each of the reusable code meta-models in the code model library;
[0032] According to the text similarity between the requirement description text and the function description text, a reusable code meta-model matching the requirement description text is screened out from the code model library to obtain a first meta-model set;
[0033] If the code generation requirement information includes a metamodel restriction condition, the model restriction condition is compared with the model attribute information of each of the reusable code metamodels in the code model library;
[0034] According to the comparison result between the model restriction condition and the model attribute information, reusable code meta-models meeting the model restriction condition are screened out from the code model library to obtain a second meta-model set;
[0035] The intersection of the first metamodel set and the second metamodel set is screened out to obtain the target reusable code metamodel.
[0036] In a second aspect, the present application discloses a code generation device, comprising:
[0037] A metamodel screening module is used to screen out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a function module class model, and a code snippet class model;
[0038] A metamodel verification module, used to verify the target reusable code metamodel using a pre-built rule base to obtain a target reusable code metamodel that passes the verification;
[0039] The metamodel combination module is used to combine all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information.
[0040] In a third aspect, the present application discloses an electronic device, comprising:
[0041] Memory, used to store computer programs;
[0042] The processor is used to execute the computer program to implement the aforementioned code generation method.
[0043] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein the computer program implements the aforementioned code generation method when executed by a processor.
[0044] In the present application, according to the code generation requirement information, a target reusable code metamodel corresponding to the code generation requirement information is screened out from a pre-constructed code model library; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a functional module class model and a code fragment class model; the target reusable code metamodel is verified using a pre-constructed rule library to obtain a target reusable code metamodel that passes the verification; all the target reusable code metamodels that pass the verification are combined to obtain a program code corresponding to the code generation requirement information. It can be seen that by constructing corresponding reusable code metamodels for various types of businesses in advance, and dividing the metacode models into application template class models, functional module class models and code snippet class models, when generating code, the matching target reusable code metamodel is screened out from the code model library according to the code generation requirement information issued by the user, and the target reusable code metamodel is verified according to the preset rule library. Finally, the program code corresponding to the code generation requirement information is obtained by combining the verified code metamodels. On the basis of ensuring the accuracy of code generation, the generation of multi-domain program code is realized, thereby improving the universality of code generation in application fields. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0046] FIG1 is a flow chart of a code generation method provided by the present application;
[0047] FIG2 is a flowchart of a specific code generation method provided by the present application;
[0048] Figure 3 A schematic diagram of the structure of a code generation device provided by this application;
[0049] FIG4 is a structural diagram of an electronic device provided in this application. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0051] In the related technology, the low-code platform adopts template-based code generation technology or model-driven code generation technology. Template-based code generation technology has poor flexibility; model-driven code generation technology is powerful, but it is difficult to implement the conversion from model to specific code, and its scope of use is limited. In actual applications, it can only be applied in a few scenarios, and in most cases, it can only generate relatively simple code. For the above reasons, most of the current low-code platforms are only applicable to vertical application fields and cannot achieve universal applicability. In order to overcome the above technical problems, the present application proposes a code generation method that can realize the generation of program codes in multiple fields on the basis of ensuring the accuracy of code generation, thereby improving the universality of code generation in the application field.
[0052] The present application embodiment discloses a code generation method. Referring to FIG1 , the method may include the following steps:
[0053] Step S11: According to the code generation requirement information, filter out the target reusable code metamodel corresponding to the code generation requirement information from the pre-built code model library; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of application template class models, function module class models and code snippet class models.
[0054] In this embodiment, a code model library is generated in advance based on the application template model, function module model and code snippet model of various services. Specifically, the construction of the above code model library may include the following steps: extracting the rules and operations involved in the application to obtain the function module model and code snippet model corresponding to the application; extracting the project framework of the application to obtain the application template model of the application, and adding an identifier to the target position in the application template model; the identifier is used to represent that the target position is used to add the function module class model or the code snippet class model; based on the application template model, function module model and code snippet model corresponding to various applications, the code model library is obtained. The above code model library can be called by the engine, that is, the reusable code metamodel is registered to the engine. The above added identifier can specifically be a placeholder slot, that is, the application template model is a framework, and the required function module model and code snippet model can be inserted into the application template model based on the application template model, and the specific insertion position is pre-identified by the placeholder slot.
[0055] That is, by collecting application templates, functional modules and code snippets, a code model library is built. Through the rules and operations involved in the program, the functional module model and code snippet model corresponding to the application can be generated; the application template model can be generated according to the project framework of the program. For example, the overall project framework of the crawler program can be extracted into a reusable general application template; at the same time, network requests and processing operations, various crawling strategies and various data storage operations can be extracted into reusable functional templates or code snippets, including configuration initialization, initiating network requests, parsing text and extracting data, and data storage models. That is, for relatively independent specific business function modules such as crawling strategies and data extraction and processing, since the module code implementations corresponding to different crawling targets and different resource types are different, it is necessary to register multiple commonly used function module metamodels for each type of function module. For example, the code implementations of common crawling strategies need to be registered in the engine so that the engine can flexibly select the corresponding crawling strategy function module according to the specific crawling business scenario; in order to enable the engine to have flexible and fine-grained code generation capabilities, it is also necessary to register all commonly used code snippets in the field of web crawler development in the engine, including non-specific business-related code snippets such as various data processing and data storage-related code snippets, as well as specific business-related code snippets such as crawler protocol extraction and parsing, proxy configuration, etc. For example, for storage-related code metamodels, it is necessary to register code snippet metamodels for basic operations of add, delete, modify and query of various commonly used databases and various disk file types, so that the engine can flexibly generate corresponding data storage-related code segments according to business scenarios and developer needs.
[0056] In some embodiments, before filtering out the target reusable code metamodel corresponding to the code generation requirement information from the pre-built code model library according to the code generation requirement information, it may also include: obtaining the code generation requirement information; the code generation requirement information includes first requirement information for the parent metamodel and second requirement information for the child metamodel; the application template class model is the parent metamodel, and the function module class model and the code snippet class model are the child metamodels. That is, since the application template model is the basis as a framework, by issuing requirement information according to the hierarchy and adapting the metamodel according to the hierarchy, the speed and accuracy of metamodel retrieval can be improved.
[0057] Accordingly, the above-mentioned step of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information may include the following steps:
[0058] S21: Filter out the application template class model corresponding to the first requirement information from the pre-built code model library to obtain the target application template model, that is, first determine the parent meta-model, that is, the target application template model.
[0059] S22: Determine all the sub-level meta-models corresponding to the target application template model to obtain the target sub-level meta-model; filter out the function module class model and the code snippet class model corresponding to the second requirement information from all the target sub-level meta-models to obtain the target function module model and the target code snippet model. That is, the model library contains the correspondence between each parent meta-model and each sub-level meta-model, and the correspondence can characterize whether the sub-level meta-model is applicable to the parent meta-model. Therefore, after determining the target application template model, all the corresponding sub-level meta-models can be determined; and then, secondary screening is performed from the filtered sub-level meta-models according to the second requirement information to obtain the target function module model and the target code snippet model.
[0060] S23: Based on the target application template model, the target function module model and the target code snippet model, obtain a target reusable code meta-model corresponding to the code generation requirement information.
[0061] Finally, the selected target application template model, target functional module model and target code snippet model are used as the target reusable code meta-model corresponding to the code generation requirement information.
[0062] It is understandable that a complete code generation requirement can be composed of multiple description requirements, where the first requirement description is used to filter the parent metamodel, and the remaining requirement descriptions are used to filter the child metamodel. Each description requirement can be separated by a line break. When you need to use the engine to quickly create a specific type of project, the first requirement content needs to enter the relevant requirement description about the application template, and the subsequent requirement content needs to enter the requirement description related to the functional module or code snippet; similarly, if you need to insert a specific functional module at a specified location, the first requirement content needs to enter a description of the functional module, and the subsequent content needs to enter the relevant description of each code snippet to be inserted into the functional module code segment; of course, if you need to quickly add a specific code snippet, you can only enter one description requirement.
[0063] In some embodiments, before determining all the child meta-models corresponding to the target application template model, the process may also include: determining the number of the selected target application template models; if two or more target application template models are selected, the code generation process is terminated; if only one target application template model is selected, the operation of determining all the child meta-models corresponding to the target application template model is performed. That is, there is usually only one application template model as a framework, but there can be multiple functional modules and code snippets, so if the result contains two or more parent meta-model objects, the code generation process can be terminated.
[0064] In some embodiments, after filtering out two or more target application template models, the method may further include: generating a prompt box according to the target application template model, and obtaining a user selection instruction for a target application template model in the prompt box, so as to execute the code generation process according to the target application template model selected by the user; or, regenerating a prompt box for inputting requirement information, so that the user can re-enter the first requirement information for the parent metamodel. It can be understood that there is usually only one application template model as a framework. If an application template model is filtered out, the user can perform further selection, or let the user re-enter; by supporting user selection or re-entering, the flexibility and efficiency of code generation can be improved. After filtering out the application template class model corresponding to the first requirement information from the pre-built code model library, the method may further include: if the application template class model corresponding to the first requirement information is not filtered out, the code generation process is terminated; or, regenerating a prompt box for inputting requirement information, so that the user can re-enter the first requirement information for the parent metamodel. That is, if the application template class model is not filtered out, the code generation process is suspended, and the user is prompted to be abnormal, or the user can be prompted to re-issue the requirement information, so as to re-query the application template class model.
[0065] In some embodiments, the code generation requirement information includes a requirement description text and / or a metamodel restriction condition for describing the code generation requirement; that is, the functional description text and the direct metamodel restriction condition can be used as the code generation requirement information. Accordingly, the method of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information includes the following steps:
[0066] S31: If the code generation requirement information includes a requirement description text for describing the code generation requirement, the requirement description text is compared with the function description text corresponding to each of the reusable code metamodels in the code model library. That is, when the code generation requirement information includes a requirement description text for describing the code generation requirement, a metamodel screening is performed based on the requirement description text. Specifically, the text similarity between the requirement description text and the function description text of each metamodel in the code model library can be compared, wherein the function description text of the metamodel can be added according to the function description of the metamodel when the code model library is constructed.
[0067] S32: According to the text similarity between the requirement description text and the function description text, reusable code metamodels matching the requirement description text are screened out from the code model library to obtain a first metamodel set. For example, if the text similarity is greater than a preset text similarity threshold, it can be considered as a required metamodel, and all the screened metamodels are obtained to obtain a metamodel set.
[0068] S33: If the code generation requirement information includes metamodel qualification conditions, the model qualification conditions are compared with the model attribute information of each reusable code metamodel in the code model library. That is, if the metamodel qualification conditions are included, the metamodel is screened once according to the metamodel qualification conditions. The following are some specific metamodel qualification conditions. For example, model qualification condition 1 includes: the name of the framework, library or module that needs to be dependent on; that is, it represents the need to screen the metamodel that uses specific dependencies. Model qualification condition 2 includes: the name of the application scenario; that is, it represents the metamodel suitable for a specific application scenario. Model qualification condition 3 includes: the programming mode; that is, it represents the screening of metamodels based on the established programming mode. Model qualification condition 4 includes: the name of the function type; that is, it represents the screening of metamodels that meet specific functional requirements; it should be noted that this condition can be used together with the application scenario screening condition, otherwise the available metamodels in the general application scenario are screened by default.
[0069] S34: According to the comparison result between the model restriction condition and the model attribute information, select the reusable code meta-model that meets the model restriction condition from the code model library to obtain a second meta-model set. If the model restriction condition matches the model attribute information, it is used as a model in the second meta-model set.
[0070] S35: Filter out the intersection of the first metamodel set and the second metamodel set to obtain the target reusable code metamodel.
[0071] Finally, the intersection of the first metamodel set and the second metamodel set is taken as the final target reusable code metamodel. By using description text and metamodel restriction conditions as metamodel matching methods, not only the flexibility of metamodel screening is improved, but also the accuracy of metamodel screening is improved.
[0072] In addition, in another embodiment, the code generation requirement information includes first requirement information for the parent metamodel and second requirement information for the child metamodel, and both the first requirement information and the second requirement information include requirement description text and / or metamodel restriction conditions that describe the code generation requirements.
[0073] It should be noted that the acquired code generation requirement information may be a semi-structured natural language description of the code generation requirement, which needs to be parsed first to extract the description content, restriction conditions and other parameters, and convert it into a structured code generation requirement representation. The parsing method can be based on the format of the code generation requirement information. Specifically, the complete text format requirement description can be divided into an array format according to the line break, and the array element is the text representation of a single description; first parse the requirement description related to the parent metamodel, extract the requirement description content by regular matching, and then use the space as a separator for the remaining content to obtain all the restriction descriptions, and extract the restriction from each restriction description; combine the extracted contents into a requirement description object in the target format; after completing the parsing of the requirement description related to the parent metamodel, parse the remaining code generation requirement descriptions in turn, and finally convert the complete requirement description into the target array format, and the elements in the array are each structured requirement description object in turn.
[0074] Step S12: Verify the target reusable code metamodel using the pre-built rule base to obtain a verified target reusable code metamodel.
[0075] For example, as shown in FIG2 , after the target reusable code metamodel is screened out, it is verified using the pre-selected rule base, and screened again to obtain the target reusable code metamodel that passes the verification.
[0076] In some embodiments, the use of a pre-built rule library to verify the target reusable code metamodel to obtain a target reusable code metamodel that passes the verification may include: using a pre-built rule library to filter out target reusable code metamodels that do not comply with the rules; the rule library includes metamodel custom rules, global custom rules, and engine built-in rules; and obtaining the target reusable code metamodel that passes the verification based on the remaining metamodels after screening.
[0077] That is, pre-defined built-in generation rules, global custom rules, and code model custom generation rules. The contents of the engine built-in rules, global custom rules, and metamodel custom rules are traversed and matched with the target reusable code metamodel respectively. Through verification, metamodels that do not meet the rule restrictions can be filtered out, and rules related to the metamodel combination method can be screened out for the final metamodel combination stage. In this process, if there is a conflict between the metamodel custom rules and the global custom rules, the metamodel custom rules can be given a high priority. Among them, model custom rules refer to the customized validation and constraints of the model according to specific needs during the software development process. These rules are usually defined on the attributes or fields of the model to ensure the integrity and consistency of the data. For example, define the validation rules of the form, which are automatically validated before the form is submitted to ensure the security of the form and the validity of the information. Through these methods, the custom rules of the model can be effectively implemented to ensure the flexibility and efficiency of software development.
[0078] Step S13: Combine all the verified target reusable code metamodels to obtain program code corresponding to the code generation requirement information.
[0079] Finally, based on the verified target reusable code metamodel, the code is combined to obtain the program code corresponding to the code generation requirement information. That is, all verified target reusable code metamodels are combined according to the code generation rules, and the final generated code is returned to the engine client.
[0080] In some embodiments, combining all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information may include: adding the function module model and the code fragment model in the target reusable code metamodel to the corresponding target position according to the attribute information of the identifier of the application template model in the target reusable code metamodel to obtain the program code corresponding to the code generation requirement information.
[0081] The identifier is the placeholder slot pre-added by the application template model. The attribute information of the placeholder slot represents the type of metamodel allowed to be inserted in the position. Therefore, according to the attribute information of the identifier and the code generation rules of the metamodel to be added, the appropriate addition position is determined and added for the target reusable code metamodel that passes the verification. That is, for each child metamodel, the slot with the highest matching degree is selected for insertion according to the attribute restrictions of each placeholder slot in the parent metamodel and the custom generation rules acting on the child metamodel. In addition, after insertion, it is further checked whether the synthesized code meets the programming specification requirements restricted by the built-in rules of the engine. If not, it is necessary to reselect the insertion position. After completing the embedding of all child metamodels, the format specification of the combined code can be further adjusted, specifically including calling the formatting tool of the corresponding programming language to format the generated code as a whole.
[0082] This application enables the engine to have low-code development capabilities in the application scenario by adding reusable code metamodels and rule libraries related to the application field to the engine platform. The code automatically generated by the engine will eventually be dynamically combined by code metamodels at each level in the code model library according to specific generation rules, solving the problems of universality and scalability of code generated by low-code platforms and supporting multiple programming languages.
[0083] As can be seen from the above, in this embodiment, according to the code generation requirement information, a target reusable code metamodel corresponding to the code generation requirement information is screened out from a pre-constructed code model library; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a functional module class model and a code fragment class model; the target reusable code metamodel is verified using a pre-constructed rule library to obtain a target reusable code metamodel that passes the verification; all the target reusable code metamodels that pass the verification are combined to obtain the program code corresponding to the code generation requirement information. It can be seen that by constructing corresponding reusable code metamodels for various types of businesses in advance, and dividing the metacode models into application template class models, functional module class models and code snippet class models, when generating code, the matching target reusable code metamodel is screened out from the code model library according to the code generation requirement information issued by the user, and the target reusable code metamodel is verified according to the preset rule library. Finally, the program code corresponding to the code generation requirement information is obtained by combining the verified code metamodels. On the basis of ensuring the accuracy of code generation, the generation of multi-domain program code is realized, thereby improving the universality of code generation in application fields.
[0084] Correspondingly, the present application also discloses a code generation device, see Figure 3 As shown, the device comprises:
[0085] The metamodel screening module 11 is used to screen out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a function module class model, and a code snippet class model;
[0086] The metamodel verification module 12 is used to verify the target reusable code metamodel using a pre-built rule base to obtain a target reusable code metamodel that passes the verification;
[0087] The metamodel combination module 13 is used to combine all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information.
[0088] As can be seen from the above, in this embodiment, according to the code generation requirement information, a target reusable code metamodel corresponding to the code generation requirement information is screened out from a pre-constructed code model library; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a functional module class model and a code fragment class model; the target reusable code metamodel is verified using a pre-constructed rule library to obtain a target reusable code metamodel that passes the verification; all the target reusable code metamodels that pass the verification are combined to obtain the program code corresponding to the code generation requirement information. It can be seen that by constructing corresponding reusable code metamodels for various types of businesses in advance, and dividing the metacode models into application template class models, functional module class models and code snippet class models, when generating code, the matching target reusable code metamodel is screened out from the code model library according to the code generation requirement information issued by the user, and the target reusable code metamodel is verified according to the preset rule library. Finally, the program code corresponding to the code generation requirement information is obtained by combining the verified code metamodels. On the basis of ensuring the accuracy of code generation, the generation of multi-domain program code is realized, thereby improving the universality of code generation in application fields.
[0089] In some specific embodiments, the code generating device may specifically include:
[0090] A code generation requirement information acquisition unit is used to acquire the code generation requirement information before filtering out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information; the code generation requirement information includes first requirement information for a parent metamodel and second requirement information for a child metamodel; the application template class model is the parent metamodel, and the function module class model and the code snippet class model are child metamodels;
[0091] Accordingly, the meta-model screening module 11 may specifically include:
[0092] An application template class model screening unit, used to screen out an application template class model corresponding to the first requirement information from a pre-built code model library to obtain a target application template model;
[0093] A sub-level meta-model determining unit, used to determine all sub-level meta-models corresponding to the target application template model to obtain a target sub-level meta-model;
[0094] Function module class model and code snippet class model unit screening, used to screen out function module class models and code snippet class models corresponding to the second requirement information from all the target sub-level metamodels, so as to obtain target function module models and target code snippet models;
[0095] The target reusable code metamodel generating unit is used to obtain the target reusable code metamodel corresponding to the code generation requirement information based on the target application template model, the target function module model and the target code fragment model.
[0096] In some specific embodiments, the meta-model screening module 11 may specifically include:
[0097] A target application template model quantity determination unit, used to determine the quantity of the screened target application template models before determining all sub-level meta-models corresponding to the target application template model;
[0098] An ending unit, used for ending the code generation process if two or more target application template models are screened out;
[0099] An execution unit is used to execute the operation of determining all sub-level meta-models corresponding to the target application template model if only one target application template model is screened out.
[0100] In some specific embodiments, the meta-model checking module 12 may specifically include:
[0101] A screening unit, used to screen out target reusable code metamodels that do not conform to the rules using a pre-built rule library; the rule library includes metamodel custom rules, global custom rules, and engine built-in rules;
[0102] The verification unit is used to obtain the verified target reusable code metamodel based on the remaining metamodels after screening.
[0103] In some specific embodiments, the code generating device may specifically include:
[0104] A first model building unit is used to extract the rules and operations involved in the application program to obtain a functional module model and a code snippet model corresponding to the application program;
[0105] The second model construction unit is used to extract the project framework of the application program, obtain the application template model of the application program, and add an identifier at a target location in the application template model; the identifier is used to indicate that the target location is used to add the function module class model or the code snippet class model;
[0106] The code model library generating unit is used to obtain the code model library based on the application template models, function module models and code snippet models corresponding to various types of application programs.
[0107] In some specific embodiments, the meta-model combination module 13 may specifically include:
[0108] A model adding unit is used to add the function module model and the code fragment model in the target reusable code metamodel to the corresponding target position according to the attribute information of the identification of the application template model in the target reusable code metamodel, so as to obtain the program code corresponding to the code generation requirement information.
[0109] In some specific embodiments, the code generation requirement information may specifically include a requirement description text and / or a metamodel restriction condition for describing the code generation requirement;
[0110] Accordingly, the meta-model screening module 11 may specifically include:
[0111] A first comparison unit is used for comparing the requirement description text with the function description text corresponding to each of the reusable code metamodels in the code model library if the code generation requirement information includes a requirement description text for describing the code generation requirement;
[0112] A first metamodel set determining unit, configured to filter out reusable code metamodels matching the requirement description text from the code model library according to the text similarity between the requirement description text and the function description text, to obtain a first metamodel set;
[0113] a second comparing unit, configured to compare the model limiting condition with the model attribute information of each of the reusable code metamodels in the code model library if the code generation requirement information includes a metamodel limiting condition;
[0114] A second metamodel set determining unit, configured to screen out reusable code metamodels that meet the model restriction conditions from the code model library according to a comparison result between the model restriction conditions and the model attribute information, to obtain a second metamodel set;
[0115] The model intersection determination unit is used to filter out the intersection of the first metamodel set and the second metamodel set to obtain the target reusable code metamodel.
[0116] Furthermore, an embodiment of the present application also discloses an electronic device, as shown in FIG4 . The content in the figure cannot be regarded as any limitation on the scope of use of the present application.
[0117] FIG4 is a schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the code generation method disclosed in any of the aforementioned embodiments.
[0118] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0119] In addition, the memory 22, as a carrier for storing resources, can be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon include an operating system 221, a computer program 222, and data 223 including code generation requirement information, etc. The storage method can be temporary storage or permanent storage.
[0120] The operating system 221 is used to manage and control the hardware devices and computer programs 222 on the electronic device 20, so as to realize the operation and processing of the massive data 223 in the memory 22 by the processor 21, and it can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the code generation method executed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.
[0121] Furthermore, an embodiment of the present application also discloses a computer storage medium, in which computer executable instructions are stored. When the computer executable instructions are loaded and executed by a processor, the steps of the code generation method disclosed in any of the aforementioned embodiments are implemented.
[0122] Furthermore, an embodiment of the present application also discloses a computer program product, in which a computer program is stored. When the computer program is loaded and executed by a processor, the steps of the code generation method disclosed in any of the aforementioned embodiments are implemented.
[0123] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0124] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0125] 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 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 device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0126] The above is a detailed introduction to a code generation method, device, equipment and storage medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A code generation method, characterized in that: include: According to the code generation requirement information, a target reusable code metamodel corresponding to the code generation requirement information is screened out from a pre-built code model library; The code model library includes reusable code meta-models corresponding to various types of services, and the reusable code meta-models include one or more of application template class models, function module class models and code snippet class models; Verifying the target reusable code metamodel using a pre-built rule base to obtain a verified target reusable code metamodel; All the verified target reusable code metamodels are combined to obtain program code corresponding to the code generation requirement information.
2. The code generation method according to claim 1, characterized in that: Before filtering out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information, the method further includes: Acquire the code generation requirement information; the code generation requirement information includes first requirement information for a parent metamodel and second requirement information for a child metamodel; the application template class model is a parent metamodel, and the function module class model and the code snippet class model are child metamodels; The step of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information includes: Filtering an application template class model corresponding to the first requirement information from a pre-built code model library to obtain a target application template model; Determine all sub-level meta-models corresponding to the target application template model to obtain a target sub-level meta-model; Filtering out the function module class model and the code snippet class model corresponding to the second requirement information from all the target sub-level meta-models to obtain a target function module model and a target code snippet model; Based on the target application template model, the target function module model and the target code snippet model, a target reusable code metamodel corresponding to the code generation requirement information is obtained.
3. The code generation method according to claim 2, characterized in that: Before determining all sub-level meta-models corresponding to the target application template model, the method further includes: Determine the number of the screened target application template models; If two or more target application template models are selected, the code generation process ends; If only one target application template model is screened out, the operation of determining all sub-level meta-models corresponding to the target application template model is performed.
4. The code generation method according to claim 1, characterized in that: The method of verifying the target reusable code metamodel by using a pre-built rule base to obtain a verified target reusable code metamodel includes: Utilize a pre-built rule library to filter out target reusable code metamodels that do not conform to the rules; the rule library includes metamodel custom rules, global custom rules, and engine built-in rules; The verified target reusable code metamodel is obtained based on the remaining metamodels after screening.
5. The code generation method according to claim 1, characterized in that: The construction of the code model library includes: Extract the rules and operations involved in the application to obtain a functional module model and a code snippet model corresponding to the application; Extracting the project framework of the application to obtain an application template model of the application, and adding an identifier at a target location in the application template model; the identifier is used to indicate that the target location is used to add the function module class model or the code snippet class model; The code model library is obtained based on the application template models, function module models and code snippet models corresponding to various application programs.
6. The code generation method according to claim 5, characterized in that: The step of combining all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information includes: According to the attribute information of the identifier of the application template model in the target reusable code metamodel, the function module model and the code fragment model in the target reusable code metamodel are added to the corresponding target position to obtain the program code corresponding to the code generation requirement information.
7. The code generation method according to any one of claims 1 to 6, characterized in that: The code generation requirement information includes a requirement description text and / or a metamodel restriction condition for describing the code generation requirement; The step of selecting a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information includes: If the code generation requirement information includes a requirement description text for describing the code generation requirement, then comparing the requirement description text with the function description text corresponding to each of the reusable code meta-models in the code model library; According to the text similarity between the requirement description text and the function description text, a reusable code meta-model matching the requirement description text is screened out from the code model library to obtain a first meta-model set; If the code generation requirement information includes a metamodel restriction condition, the model restriction condition is compared with the model attribute information of each of the reusable code metamodels in the code model library; According to the comparison result between the model restriction condition and the model attribute information, reusable code meta-models meeting the model restriction condition are screened out from the code model library to obtain a second meta-model set; The intersection of the first metamodel set and the second metamodel set is screened out to obtain the target reusable code metamodel.
8. A code generating device, characterized in that: include: A metamodel screening module is used to screen out a target reusable code metamodel corresponding to the code generation requirement information from a pre-built code model library according to the code generation requirement information; the code model library includes reusable code metamodels corresponding to various types of businesses, and the reusable code metamodel includes one or more of an application template class model, a function module class model, and a code snippet class model; A metamodel verification module, used to verify the target reusable code metamodel using a pre-built rule base to obtain a target reusable code metamodel that passes the verification; The metamodel combination module is used to combine all the verified target reusable code metamodels to obtain the program code corresponding to the code generation requirement information.
9. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor, configured to execute the computer program to implement the code generation method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: Used to store a computer program; wherein when the computer program is executed by a processor, the code generation method according to any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Code generation method and device, equipment, medium and product
CN119917108A
Model-based application building method and device, equipment, medium and program product
CN120848870A
Industrial equipment access method and device, electronic equipment and storage medium
CN121967411A