A method and apparatus for automatic code generation
Through custom model description language and parsing engine, front-end and back-end codes are generated, which solves the problems of high R&D participation and fixed page organization in the existing technology, and achieves flexible code automatic generation and strong scalability.
Patent Information
- Application Number
- CN202210047291.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-17
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2042-01-17
AI Technical Summary
Existing code generation technology requires high R&D personnel participation and development experience, the page organization structure is fixed, personalized customization cannot be achieved, and the expansion capabilities are poor.
Using a custom model description language, code templates are generated through the parsing engine, and using Bacos paradigm rules and custom extended rules to parse the model description language, generate front-end and back-end code, support rich template and template parsing classes, and realize flexible code automatic generation.
It simplifies design and modeling steps, improves the universality and scalability of code generation, reduces the requirements for development experience, and allows non-technical backgrounds to participate in application development, supporting complex page design and business logic.
Smart Images

Figure CN114489597B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software engineering, in particular to a method and device for automatic code generation. Background Art
[0002] Code generation technology is a technology that uses programs to generate code. It programs with programs and aims to liberate coding productivity. The principle of code generation is to read metadata in the engineering field and generate source code that conforms to programming specifications according to certain design patterns and rules. The commonly used code generation methods mainly include the following: (1) generating code based on templates (currently commonly used tools include FreeMarker, Velocity, JET, etc.); (2) generating code based on models; (3) generating code based on DOM; (4) generating code based on code comment parsing; (5) generating code based on object relationships, etc.
[0003] Existing code generation technologies can all improve software development efficiency and the software code styles are highly consistent, but there are also the following defect problems:
[0004] (1) The participation of R & D personnel is relatively high and certain development experience is required to master. For example, generating code based on templates requires writing template files; generating code based on models has complex modeling steps (it is necessary to construct a four-layer meta-model system of MDA); generating code based on DOM requires using XML to construct a DOM syntax tree; generating code based on code comment parsing requires outputting pseudo-code logic; generating code based on object relationships requires a certain foundation in object orientation and relational databases.
[0005] (2) The page organizational structure is fixed and single, component elements cannot be fully covered, personalized page customization is difficult, and the idea of "design is programming" cannot be fully reflected.
[0006] Most of the front-end pages generated by existing code generation technologies are list and form pages corresponding to databases and entity class metadata, and the page presentation forms are fixed and single (the styles of each component are fixed), and the HTML presentation forms outside the database and entity class fields (such as fonts, paragraphs, dividers...) cannot be deeply integrated with the page. Summary of the Invention
[0007] The purpose of the present invention is to overcome the deficiencies in the prior art and provide a method and device for automatic code generation, which are simple to operate, have strong scalability, can omit complex design and modeling steps, and improve the universality of the method.
[0008] To achieve the above purpose, the present invention is implemented by adopting the following technical solutions:
[0009] In the first aspect, the present invention provides a method for automatic code generation, including the following steps:
[0010] Receive a code automatic generation request of the model description language sent by the user. Among them, the code automatic generation request carries at least the following information of the model description language: metadata set, target data;
[0011] Call the parsing engine according to the model description language code automatic generation request to obtain a target code template. The target code template matches the information carried in the code automatic generation request, and the target code template contains placeholders;
[0012] Replace the placeholders in the target code template with the target data in the model description language code automatic generation request to obtain a front-end code file;
[0013] Call the database schema definition language according to the database table-related information and metadata set in the model description language code automatic generation request to obtain database tables;
[0014] Generate control layer, business logic layer, and data access layer codes according to the code generation tool for the database table structure to obtain a back-end code file.
[0015] Further, calling the parsing engine according to the model description language code automatic generation request to obtain a target code template includes:
[0016] According to the splitting rules, the splitting rules include Backus-Naur form rules and custom extension rules, and the splitting rules include Backus-Naur form rules and custom extension rules. Split the model description language request to obtain multiple model description language split sub-statements;
[0017] According to the first sentence of the model description language split sub-statement, identify the front-end UI framework name by judgment;
[0018] According to the UI framework name, obtain the preset JS file information, front-end HTML header file information, and other basic import file strings that need to be imported in the front-end framework from the database;
[0019] Traverse each model description language split sub-statement, and parse to obtain the control name according to the rules;
[0020] Match the template parsing class with the same name as the control name according to the control name to obtain a target code HTML template and a JS script template.
[0021] Further, the method of replacing the placeholders in the target code template with the target data in the model description language code automatic generation request to obtain a front-end code file includes:
[0022] Step A: Parse the target data in the model language according to the rules to obtain a style attribute set and a business attribute set; the target data includes style attributes and business attributes;
[0023] Step B: Replace the placeholders in the target code HTML template and JS script template according to the style attribute set and business attribute set to obtain an automatically generated front-end sub-target code string;
[0024] Step C: Repeat steps A - B until all the model language description segmentation statements are parsed, and obtain the front-end sub-target code strings automatically generated by each model description language segmentation sub-statement;
[0025] Step D: Concatenate the page header file and the front-end sub-target code strings automatically generated by each model description language segmentation sub-statement to obtain the full front-end target code string;
[0026] Step E: Write the full front-end target code string into a file according to the default naming convention and project path to obtain the front-end target code file.
[0027] Further, the method for obtaining a database table by automatically generating metadata sets in requests according to the model description language code and calling the database schema definition language includes:
[0028] According to the second sentence of the model description language segmentation sub-statement, identify relevant database table information by judgment;
[0029] Parse the metadata attributes in the model language according to the rules to obtain a set of metadata fields;
[0030] Call the database schema definition language according to the relevant database table information and the set of metadata fields to obtain a database table; the relevant database table information includes table name and table comment.
[0031] Further, the method for obtaining a back-end code file by generating control layer, business logic layer, and data access layer code according to a code generation tool for database table structures includes:
[0032] Generate control layer, business logic layer, and data access layer code according to the code generation tool for the database table to obtain all back-end target code;
[0033] Write the all back-end target code into a file according to the agreed naming convention and project path to obtain the back-end target code file.
[0034] Further, the method further includes: performing one-click deployment according to the front-end code file, back-end code file, and database table to complete the release of functional applications.
[0035] In a second aspect, the present invention provides a code automatic generation device, and the device includes:
[0036] A receiving module, configured to receive a code automatic generation request of a model description language sent by a user, where the code automatic generation request carries at least the following information of the model description language: a metadata set and target data;
[0037] A parsing module, configured to obtain a target code template by calling a parsing engine according to the model description language code automatic generation request, where the target code template matches the information carried in the code automatic generation request, and the target code template contains placeholders;
[0038] A front-end output module, configured to replace the placeholders in the target code template with the target data in the model description language code automatic generation request to obtain a front-end code file;
[0039] A data table output module, configured to call a database schema definition language according to database table-related information and a metadata set in the model description language code automatic generation request to obtain a database table;
[0040] A back-end output module, configured to generate control layer, business logic layer, and data access layer codes according to a code generation tool for the database table structure to obtain a back-end code file.
[0041] Further, the method by which the parsing module obtains a target code template by calling a parsing engine according to the model description language code automatic generation request includes:
[0042] According to a splitting rule, the splitting rule includes a Backus-Naur Form rule and a custom extension rule, and the model description language request is split to obtain multiple model description language split sub-statements;
[0043] According to the first sentence of the model description language split sub-statement, the front-end UI framework name is identified by judgment;
[0044] According to the UI framework name, information on JS files that need to be introduced, front-end HTML header file information, and other basic introduction file strings preset in the front-end framework are obtained from the database;
[0045] According to the second sentence of the model description language split sub-statement, database table-related information is identified by judgment;
[0046] Traverse each model description language split sub-statement, and parse to obtain a control name according to the rule;
[0047] Match a template parsing class with the same name as the control name according to the control name to obtain a target code HTML template and a JS script template.
[0048] Further, the method for the front-end output module to automatically generate the target data in the request to replace the placeholder in the target code template to obtain the front-end code file includes:
[0049] Step A: Parse the target data in the model language according to the rules to obtain a set of style attributes and a set of business attributes; the target data includes style attributes and business attributes.
[0050] Step B: Replace the placeholders in the target code HTML template and JS script template according to the set of style attributes and the set of business attributes to obtain an automatically generated front-end sub-target code string.
[0051] Step C: Repeat steps A-B until all the parsing of the model language description segmentation statements is completed, and obtain the front-end sub-target code strings automatically generated by each model description language segmentation sub-statement.
[0052] Step D: Concatenate the page header file and the front-end sub-target code strings automatically generated by each model description language segmentation sub-statement to obtain the entire front-end target code string.
[0053] Step E: Write the entire front-end target code string into a file according to the default naming convention and the project path to obtain the front-end target code file.
[0054] In a third aspect, the present invention provides a code automatic generation device, including a processor and a storage medium;
[0055] The storage medium is used to store instructions;
[0056] The processor is used to operate according to the instructions to execute the steps of the method described in the first aspect.
[0057] Compared with the prior art, the beneficial effects achieved by the present invention are:
[0058] 1. In the code automatic generation method of the present invention, the target code template can be determined by parsing according to the model description language written by the user, and then the placeholders in the target code template can be replaced with business attributes and style attributes, so that the automatically generated code and database tables can be obtained, which is more intelligent and flexible. In addition, when other types of codes need to be generated, only the code template library needs to be extended, which alleviates the technical problems of complex configuration and poor code generation expansion ability in the existing code automatic generation methods.
[0059] 2. The custom page description language of the present invention can achieve the "popularization" of technology and omit complex design and modeling steps; in addition, the present invention has strong scalability, and when other types of code templates need to be generated, only the code template library needs to be extended.
[0060] 3. In the code automatic generation device of the present invention, corresponding type of code can be obtained according to the model description language written by the user, which is more intelligent and flexible. In addition, when other types of code templates need to be generated, only the code template library needs to be extended, with strong scalability, alleviating the technical problems of complicated configuration, fixed code generation type, and poor code generation extension ability in the existing code automatic generation devices. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] Figure 1 It is the overall working flowchart of the code automatic generation of the present invention.
[0062] Figure 2 It is the detailed working flowchart of the parsing engine for constructing front-end code, back-end code, and database tables in the present invention.
[0063] Figure 3 It is the structural schematic diagram of the code automatic generation device provided by the embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0064] The present invention will be further described below with reference to the drawings. The following embodiments are only used to more clearly illustrate the technical solution of the present invention, and cannot be used to limit the protection scope of the present invention.
[0065] Embodiment 1:
[0066] The present embodiment provides a code automatic generation method, including the following steps:
[0067] Step 1: Receive a code automatic generation request of the model description language sent by the user. Among them, the code automatic generation request carries at least the following information of the model description language: metadata set, target data;
[0068] Step 2: Call a parsing engine according to the code automatic generation request of the model description language to obtain a target code template, where the target code template matches the information carried in the code automatic generation request, and the target code template contains placeholders;
[0069] Step 3: Replace the placeholders in the target code template with the target data in the code automatic generation request of the model description language to obtain a front-end code file;
[0070] Step 4: Call a database schema definition language according to the database table related information and metadata set in the code automatic generation request of the model description language to obtain a database table;
[0071] Step 5: Generate control layer, business logic layer, and data access layer code according to the code generation tool of the database table structure to obtain a back-end code file.
[0072] Specifically, the steps to automatically generate a request call to the parsing engine based on the model description language code to obtain a target code template are as follows:
[0073] Step 2.1: According to the splitting rules, which include Backus-Naur Form (BNF) rules and custom extension rules, split the model description language request to obtain multiple model description language split sub-statements.
[0074] Step 2.2: Based on the first sentence of the model description language split sub-statements, identify the front-end UI framework name through judgment.
[0075] Step 2.3: According to the UI framework name, obtain the pre-set JS file information, front-end HTML header file information, and other basic import file strings that need to be imported in the front-end framework from the database.
[0076] Step 2.4: Based on the second sentence of the model description language split sub-statements, identify the database table-related information through judgment.
[0077] Step 2.5: Traverse each model description language split sub-statement and parse to obtain the control name according to the rules.
[0078] Step 2.6: Match the template parsing class with the same name as the control name to obtain the target code HTML template and JS script template.
[0079] Specifically, the method of automatically generating a front-end code file by replacing the placeholders in the target code template with the target data in the model description language code includes:
[0080] Step A: Parse the target data in the model language according to the rules to obtain a style attribute set and a business attribute set; the target data includes style attributes and business attributes.
[0081] Step B: Replace the placeholders in the target code HTML template and JS script template according to the style attribute set and business attribute set to obtain an automatically generated front-end sub-target code string.
[0082] Step C: Repeat steps A - B until the parsing of all model language description split statements is completed, and obtain the front-end sub-target code strings automatically generated for each model description language split sub-statement.
[0083] Step D: Concatenate the page header file and the front-end sub-target code strings automatically generated for each model description language split sub-statement to obtain the front-end all-target code string.
[0084] Step E: Write all the target code strings of the front end into a file according to the default naming convention and the project path to obtain the front-end target code file.
[0085] Specifically, the method for automatically generating the relevant information of the database table in the request and the metadata set calling the database schema definition language according to the model description language code includes:
[0086] Split the second sub-statement of the model description language, and obtain the relevant information of the database table by judgment and recognition;
[0087] Parse the metadata attributes in the model language according to the rules to obtain the metadata field set;
[0088] Call the database schema definition language according to the relevant information of the database table and the metadata field set to obtain the database table; the relevant information of the database table includes the table name and table comment.
[0089] Specifically, the method for generating the code of the control layer, business logic layer, and data access layer according to the code generation tool of the database table structure to obtain the back-end code file includes:
[0090] Generate the code of the control layer, business logic layer, and data access layer according to the code generation tool of the database table to obtain all the back-end target codes;
[0091] Write all the back-end target codes into a file according to the naming convention and the project path to obtain the back-end target code file.
[0092] Specifically, the method further includes:
[0093] Step 6: Perform one-click deployment according to the front-end code file, back-end code file, and database table to complete the release of the functional application.
[0094] The method of this embodiment is based on a custom description language that can describe page structure, page migration, and business logic. It is similar to the configuration language of PLC or simulation modeling, between programming languages and natural languages, and can be mastered without development experience. The custom code automatic generation method is parsed by a built-in parsing engine (PARSER) (where the parsing engine is written according to the JAVA language by the present invention, and contains various template parsing classes and rule parsing logics), and a code set that conforms to the JAVAWEB specification is generated. Its specific work process includes the following steps:
[0095] S11: The user writes the model description language according to the page prototype to obtain a model description language code automatic generation request;
[0096] S12: Call the parsing engine according to the model description language code automatic generation request to obtain the target code template;
[0097] S13: The parsing engine automatically generates the target data in the request to replace the placeholders in the target code template according to the model description language code, and obtains the front-end code file;
[0098] S14: The parsing engine automatically generates the database table-related information and metadata in the request according to the model description language code, and invokes DDL (Database Schema Definition Language) to obtain the database table;
[0099] S15: The parsing engine generates the control layer, business logic layer, and data access layer code according to the code generation tool for the database table structure, and obtains the back-end code file;
[0100] S16: Perform one-click deployment according to the automatically generated code file to complete the release of the functional application.
[0101] Specifically, the specific implementation steps of the parsing engine in S12, S13, S14, and S15 are as follows. Refer to Figure 2 :
[0102] S101: Split the model description language request according to the rules (BNF (Backus-Naur Form) rules and custom extension rules) to obtain multiple model description language split sub-statements;
[0103] S102: According to the first sentence of the model description language split sub-statement, the parsing engine identifies the front-end UI framework name by judgment;
[0104] S103: According to the UI framework name obtained in S102, the parsing engine obtains the information of the JS files that need to be introduced, the header file information of the front-end HTML, and other basic introduction file strings that have been preset in the front-end framework from the database;
[0105] S104: According to the second sentence of the model description language split sub-statement, identify the database table name and table comment-related information by judgment;
[0106] S105: Traverse each model description language split sub-statement and parse the control name according to the rules;
[0107] S106: Match the template parsing class with the same name as the control name according to the control name to obtain the target code HTML template and JS script template;
[0108] S107: Parse the target data (style attributes, business attributes) in the model language according to the rules to obtain the style attribute set and business attribute set;
[0109] S108: Parse the metadata attributes in the model language according to the rules to obtain the metadata field set;
[0110] S109: Replace the placeholders in the HTML template and JS script template obtained in S106 with the style attribute set and business attribute set obtained in S107 to obtain an automatically generated front-end sub-goal code string;
[0111] S110: Repeat steps S106 - S109 until all model description language segmentation statements are parsed, and obtain front-end sub-goal code strings automatically generated for each model description language segmentation sub-statement;
[0112] S111: Concatenate the page header file obtained in S103 and the front-end sub-goal code strings obtained in S110 to obtain a front-end all-goal code string;
[0113] S112: Write the front-end all-goal code string obtained in S111 into a file according to the default naming convention and project path to obtain a front-end goal code file;
[0114] S113: Call DDL (Database Schema Definition Language) according to the database table name, table comment related information obtained in S104 and the metadata field set obtained in S108 to obtain a database table;
[0115] S114: Generate control layer, business logic layer, and data access layer code according to the code generation tool of the database table to obtain all back-end goal code;
[0116] S115: Write all the back-end goal code obtained in S114 into a file according to the naming convention and project path to obtain a back-end goal code file;
[0117] S116: Perform one-click deployment according to the automatically generated code file to complete the release of the functional application.
[0118] The following is a method for automatically generating some example codes of a certain construction machinery Internet of Things platform. According to the above technical solution description, the specific implementation steps are illustrated as follows:
[0119]
[0120] S101: Split the model description language request according to ";", and obtain multiple model description language segmentation sub-statements;
[0121] S102: According to the first sentence of the model description language segmentation sub-statement, the parsing engine identifies the front-end UI framework name through judgment: EasyUI;
[0122] S103: According to the UI framework name obtained in S102, the parsing engine obtains the information of the JS files that need to be introduced, the front-end HTML header file information, and other basic introduction file information preset in the front-end framework from the database.
[0123] S104: Based on the second sentence of the model language description sentence, the database table name: productModel and the table annotation related information: product model are obtained through judgment and recognition.
[0124] Loop start (LOOP START)
[0125] (1) The third sentence of the model description language segmentation sub-sentence:
[0126] form-start(id=productModel_form);
[0127] S105: For the model description language segmentation sub-sentence, the control name: Form is obtained by parsing according to the rules.
[0128] S106: Match the template parsing class Form with the same name as the control name to obtain the target code HTML template.
[0129] <form id="{0}"> < / form>
[0130] S107: Parse the target data in the model language according to the rules to obtain the style attribute set and business attribute set: productModel_form.
[0131] S108: Parse the metadata attributes in the model language according to the rules to obtain the metadata field set: none.
[0132] S109: Replace the placeholders in the HTML template and JS script template obtained in S106 with the style attribute set and business attribute set obtained in S107 to obtain the automatically generated front-end sub-target code string.
[0133]
[0134] (2) The fourth sentence of the model description language segmentation sub-sentence:
[0135] Font(name=Product Model,size=15,ElementDivMarginTop=20);
[0136] S105: For the model description language segmentation sub-sentence, the control name: Font is obtained by parsing according to the rules.
[0137] S106: Match the template parsing class Font with the same name as the control name to obtain the target code HTML template.
[0138] <div style="{2}"> <font size="{1}">{0}< / font>
[0139] S107: Parse the target data in the model language according to the rules to obtain a set of style attributes and a set of business attributes: name = product model, size = 15, ElementDivMarginTop = 20.
[0140] S108: Parse the metadata attributes in the model language according to the rules to obtain a set of metadata fields: none.
[0141] S109: Replace the placeholders in the HTML template obtained in S106 with the set of style attributes and the set of business attributes obtained in S107 to obtain an automatically generated front-end sub-goal code string.
[0142] <div style="margin-top:20px"> <font size="15">Product Model< / font>
[0143] (3) The fifth sub-statement of the model description language splitting:
[0144]
[0145] S105: Split the sub-statements of the model description language and parse to obtain the control name according to the rules: ComboBox;
[0146] S106: Match the template parsing class ComboBox with the same name as the control name to obtain the target code HTML template:
[0147]
[0148] JS template:
[0149]
[0150] S107: Parse the target data in the model language according to the rules to obtain a set of style attributes and a set of business attributes: name = tonnage name, ElementDivFloat = left, id = vehicleTonnage, datasource = tonnage name, defaultValue = please select, required = true, readonlyVariables = tonnage type - combox, cancelReadonlyCondition = please select, cancelReadonlyVariables = tonnage type - combox.
[0151] S108: Parse the metadata attributes in the model language according to the rules to obtain a set of metadata fields: variable = vehicleTonnage.
[0152] S109: Replace the placeholders in the HTML template and JS script template obtained in S106 with the style attribute set and business attribute set obtained in S107 to obtain an automatically generated front-end sub-goal code string.
[0153]
[0154]
[0155] (4) The sixth sub-statement of the model description language segmentation, and the parsing process can refer to the fifth sub-statement of the model description language segmentation.
[0156]
[0157] (5) The seventh sub-statement of the model description language segmentation:
[0158] Input(name=Material Number,ElementDivFloat=left,id=materialNum,required=true,variable=materialNum);
[0159] S105: The model description language segmentation sub-statement, and the control name is parsed according to the rule: Input;
[0160] S106: Match the template parsing class Input with the same name as the control name to obtain the target code HTML template:
[0161]
[0162] JS template:
[0163]
[0164] S107: Parse the target data in the model language according to the rule to obtain the style attribute set and business attribute set: name = material number, ElementDivFloat = left, id = materialNum, required = true.
[0165] S108: Parse the metadata attributes in the model language according to the rule to obtain the metadata field set: variable = materialNum.
[0166] S109: Replace the placeholders in the HTML template and JS script template obtained in S106 with the style attribute set and business attribute set obtained in S107 to obtain an automatically generated front-end sub-goal code string.
[0167]
[0168]
[0169] (6) The eighth sub - statement of the model description language segmentation, for the parsing process, refer to the seventh sub - statement of the model description language segmentation.
[0170] Input(name=Model Name,ElementDivFloat=left,id=modelName,required=true,variable=modelName);
[0171] (7) The ninth sub - statement of the model description language segmentation:
[0172] form-end;
[0173] S105: The sub - statement of the model description language segmentation, parse to obtain the control name according to the rule: Form;
[0174] S106: Match the template parsing class Form with the same name as the control name according to the control name, and obtain the target code HTML template;
[0175]
[0176] S107: Parse the target data in the model language according to the rule, and obtain the style attribute set and business attribute set: none;
[0177] S108: Parse the metadata attributes in the model language according to the rule, and obtain the metadata field set: none;
[0178] S109: Replace the placeholders in the HTML template obtained in S106 with the style attribute set and business attribute set obtained in S107, and obtain the automatically generated front - end sub - target code string.
[0179]
[0180] (8) The tenth sub - statement of the model description language segmentation:
[0181] SUBMIT(name=Submit,width=80,id=submitButton);
[0182] S105: The sub - statement of the model description language segmentation, parse to obtain the control name according to the rule: SUBMIT;
[0183] S106: Match the template parsing class SUBMIT with the same name as the control name according to the control name, and obtain the target code HTML template;
[0184] <a id="{2}"class="easyui-linkbutton"href="#"style="{1}"onclick="submit()">{0}
[0185] JS template:
[0186]
[0187] S107: Parse the target data in the model language according to the rules to obtain a set of style attributes and a set of business attributes: name = submit, width = 80, id = submitButton;
[0188] S108: Parse the metadata attributes in the model language according to the rules to obtain a set of metadata fields: none;
[0189] S109: Replace the HTML template and JS template obtained in S106 with the set of style attributes and the set of business attributes obtained in S107 to obtain an automatically generated front-end sub-goal code string.
[0190] <a id="submitbutton"class="easyui-linkbutton"href="#"style="width:80px"onclick="submit()">Submit
[0191]
[0192] End of loop (LOOP END)
[0193] S111: Concatenate the page header file obtained in S103 and the front-end sub-goal code strings obtained in S110 to obtain a full front-end target code string, and the concatenation order is: header file + each JS sub-goal code + each HTML sub-goal code.
[0194] S112: Write the full front-end target code string obtained in S111 into a file according to the default naming convention and project path to obtain a front-end target code file.
[0195] S113: Call DDL (Database Schema Definition Language) according to the database table name, table comment related information obtained in S104 and the set of metadata fields obtained in S108 to obtain a database table.
[0196]
[0197] S114: Generate control layer, business logic layer, and data access layer code according to the code generation tool of the database table to obtain all back-end target code.
[0198] S115: Write all the back-end target code obtained in S114 into a file according to the agreed naming convention and project path to obtain a back-end target code file.
[0199] (2) Key points of the present invention
[0200] (1) Custom page description language
[0201] This code automatic generation method can omit the cumbersome design and modeling steps in other code automatic generation technologies, and can easily and efficiently achieve low-code development. The custom page description language requires no development experience. One only needs to "describe" according to the page prototype based on the provided model description language help document. With a little training, system users can master it, enabling more people to participate in application development, not only professional programmers with programming skills, but also business personnel without a technical background can build applications.
[0202] (2) Rich templates and template parsing classes
[0203] The parsing engine supports rich templates and template parsing classes, supports complex page design and business logic, and overcomes the problems such as the single type of controls and fixed control styles in other code automatic generation technologies. It can use a custom language to implement the extension of components and page presentation forms, deeply reflecting the idea of "what you see is what you program".
[0204] The method of this embodiment is conducive to the "popularization" of the technology that the custom page description language can achieve, omitting complex design and modeling steps. The control template library of this embodiment is rich and supports complex page design and business logic. In addition, this method has strong scalability. When other types of code templates need to be generated, only the code template library needs to be extended.
[0205] Embodiment Three:
[0206] This embodiment provides a code automatic generation device. Referring to Figure 3 , the device includes:
[0207] A receiving module, configured to receive a model description language code automatic generation request sent by a user. Among them, the code automatic generation request carries at least the following information: a metadata set, a business attribute set, and a style attribute set;
[0208] A parsing module, configured to call a target code template in the parsing engine code template library according to the code automatic generation request, where the target code template matches the information carried in the code automatic generation request, and the target code template contains placeholders;
[0209] A front-end output module, configured to obtain front-end generated code and files by replacing the placeholders in the target code template with the business attribute set and style attribute set in the parsed model description language;
[0210] A data table output module, configured to generate DDL and database tables by parsing the database table-related information and metadata set in the model description language. The database table-related information mainly includes the package name, table comment information, and table fields are obtained through the metadata set;
[0211] The back - end output module is used to call a code generation tool based on the database table structure through the database table to generate codes for the control layer, business logic layer, and data access layer, so as to obtain the back - end business code file.
[0212] In the code automatic generation device of the present invention, the user only needs to write the model description language to obtain a code automatic generation request. Then, the parsing engine calls the target code template according to the code automatic generation request. Next, the parsing engine replaces the placeholders in the target code template with the business attribute set and style attribute set in the model description language to obtain the front - end automatically generated code. Then, the parsing engine creates a DDL (Database Schema Definition Language) with the database table - related information and metadata set in the model description language to obtain the database table. Finally, the parsing engine calls a code generation tool based on the database table structure to generate the back - end generated code. In the code automatic generation device in this embodiment, the corresponding type of code can be obtained according to the model description language written by the user, which is more intelligent and flexible. In addition, when other types of code templates need to be generated, only the code template library needs to be extended, and the scalability is strong, alleviating the technical problems of complex configuration, fixed code generation types, and poor code generation extension ability in the existing code automatic generation devices.
[0213] Each module of the device in this embodiment can be used to implement the code generation method described in Embodiment 1.
[0214] Embodiment 4:
[0215] The embodiment of the present invention also provides a code automatic generation device, including a processor and a storage medium;
[0216] The storage medium is used to store instructions;
[0217] The processor is used to operate according to the instructions to execute the steps of the method described in Embodiment 1:
[0218] S11: The user writes the model description language according to the page prototype to obtain a code automatic generation request for the model description language;
[0219] S12: Call the parsing engine according to the code automatic generation request for the model description language to obtain the target code template;
[0220] S13: The parsing engine replaces the placeholders in the target code template with the target data in the code automatic generation request for the model description language to obtain the front - end code file;
[0221] S14: The parsing engine calls the DDL (Database Schema Definition Language) with the database table - related information and metadata in the code automatic generation request for the model description language to obtain the database table;
[0222] S15: The parsing engine generates the codes for the control layer, business logic layer, and data access layer according to the code generation tool for the database table structure, and obtains the backend code files.
[0223] S16: Perform one-click deployment based on the automatically generated code files to complete the release of the functional application.
[0224] Those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0225] The present application is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram can be implemented by computer program instructions, and the combination of the processes and / or blocks in the flowchart and / or block diagram can also be implemented. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate means for implementing the functions specified in one Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0226] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer-readable memory generate a manufactured product including instruction means, and the instruction means implements the functions specified in one Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0227] These computer program instructions can also be loaded onto a computer or other programmable data processing device, so that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0228] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and modifications can be made, and these improvements and modifications should also be regarded as the protection scope of the present invention.
Claims
1. A method for automatic code generation, characterized in that, it includes the following steps: Receiving a code automatic generation request of a model description language sent by a user, wherein at least the following information of the model description language is carried in the code automatic generation request: a metadata set, target data; Invoking a parsing engine according to the model description language code automatic generation request to obtain a target code template, the target code template matching the information carried in the code automatic generation request, and the target code template containing placeholders; Replacing the placeholders in the target code template with the target data in the model description language code automatic generation request to obtain a front-end code file; Invoking a database schema definition language according to the database table related information and metadata set in the model description language code automatic generation request to obtain database tables; Generating control layer, business logic layer, and data access layer codes according to a code generation tool for the database table structure to obtain a back-end code file; Invoking a parsing engine according to the model description language code automatic generation request to obtain a target code template, including: According to splitting rules, the splitting rules including Backus-Naur Form rules and custom extension rules, splitting the model description language request to obtain multiple model description language split sub-statements; Based on the first sentence of the model description language split sub-statement, identifying the front-end UI framework name by judgment; According to the UI framework name, obtaining the pre-set JS file information to be introduced, front-end HTML header file information, and other basic introduction file strings in the front-end framework from the database; Traversing each model description language split sub-statement, and parsing to obtain a control name according to the rules; Matching a template parsing class with the same name as the control name according to the control name to obtain a target code HTML template and a JS script template.
2. The code automatic generation method according to claim 1, characterized in that, The method for replacing the placeholders in the target code template with the target data in the model description language code automatic generation request to obtain a front-end code file includes: Step A: Parsing the target data in the model language according to the rules to obtain a style attribute set and a business attribute set; the target data includes style attributes and business attributes; Step B: Replacing the placeholders in the target code HTML template and JS script template with the style attribute set and business attribute set to obtain an automatically generated front-end sub-target code string; Step C: Repeating steps A-B until the parsing of all model language description split statements ends, obtaining front-end sub-target code strings automatically generated for each model description language split sub-statement; Step D: Concatenating the page header file and the front-end sub-target code strings automatically generated for each model description language split sub-statement to obtain a front-end all-target code string; Step E: Writing the front-end all-target code string into a file according to the default convention naming method and project path to obtain a front-end target code file.
3. The code automatic generation method according to claim 2, characterized in that, A method for automatically generating a set of metadata in a request according to model description language code and invoking a database schema definition language to obtain a database table includes: Splitting the second sub - statement of the model description language, and obtaining database table - related information through judgment and recognition; Parsing metadata attributes in the model language according to rules to obtain a set of metadata fields; Invoking a database schema definition language according to the database table - related information and the set of metadata fields to obtain a database table; the database table - related information includes a table name and a table comment.
4. According to the code automatic generation method described in claim 3, it is characterized in that A method for generating control layer, business logic layer, and data access layer code according to a code generation tool for database table structure to obtain a back - end code file includes: Generating control layer, business logic layer, and data access layer code according to a code generation tool for database tables to obtain all back - end target code; Writing the all back - end target code into a file according to a convention naming method and a project path to obtain a back - end target code file.
5. According to the code automatic generation method described in claim 1, it is characterized in that The method further includes: performing one - key deployment according to a front - end code file, a back - end code file, and a database table to complete the release of a functional application.
6. A code automatic generation device for executing the method described in claim 1, it is characterized in that The device includes: A receiving module, configured to receive a code automatic generation request of a model description language sent by a user, where at least the following information of the model description language is carried in the code automatic generation request: a set of metadata, target data; An analysis module, configured to obtain a target code template by invoking an analysis engine according to the code automatic generation request of the model description language, the target code template matching the information carried in the code automatic generation request, and placeholders being included in the target code template; A front - end output module, configured to replace placeholders in the target code template according to the target data in the code automatic generation request of the model description language to obtain a front - end code file; A data table output module, configured to invoke a database schema definition language according to the database table - related information and the set of metadata in the code automatic generation request of the model description language to obtain a database table; A back - end output module, configured to generate control layer, business logic layer, and data access layer code according to a code generation tool for database table structure to obtain a back - end code file.
7. According to the code automatic generation device described in claim 6, it is characterized in that The method by which the analysis module obtains a target code template by invoking an analysis engine according to the code automatic generation request of the model description language includes: According to splitting rules, the splitting rules include Backus - Naur form rules and custom extension rules, splitting the model description language request to obtain multiple model description language splitting sub - statements; According to the first sub - statement of the model description language splitting sub - statements, identifying the front - end UI framework name through judgment; Obtain the information of the pre-set JS files to be introduced, the header file information of the front-end HTML, and the strings of other basic introduction files in the front-end framework from the database according to the name of the UI framework; Traverse each sub-statement of the model description language segmentation, and parse to obtain the control name according to the rules; Match the template parsing class with the same name as the control name according to the control name, and obtain the target code HTML template and the JS script template.
8. The code automatic generation device according to claim 7, characterized in that, The method for the front-end output module to automatically generate the target data in the request to replace the placeholders in the target code template according to the model description language code to obtain the front-end code file includes: Step A: Parse the target data in the model language according to the rules to obtain a set of style attributes and a set of business attributes; the target data includes style attributes and business attributes; Step B: Replace the placeholders in the target code HTML template and the JS script template according to the set of style attributes and the set of business attributes to obtain an automatically generated front-end sub-target code string; Step C: Repeat steps A-B until the parsing of all the model language description segmentation statements is completed, and obtain the front-end sub-target code strings automatically generated by each model language description segmentation sub-statement; Step D: Concatenate the page header file and the front-end sub-target code strings automatically generated by each model language description segmentation sub-statement to obtain the entire front-end target code string; Step E: Write the entire front-end target code string into a file according to the default naming convention and the project path to obtain the front-end target code file.
9. A code automatic generation device, characterized in that, comprises a processor and a storage medium; The storage medium is used to store instructions; The processor is used to operate according to the instructions to execute the steps of the method according to any one of claims 1-5.
Citation Information
Patent Citations
Code generation method based on ASN type object
CN101154227A
Code automatic generation method, code automatic generation device and electronic equipment
CN107729003A