Nothing Special   »   [go: up one dir, main page]

CN117632132A - Code generation method, device, electronic equipment and computer readable storage medium - Google Patents

Code generation method, device, electronic equipment and computer readable storage medium Download PDF

Info

Publication number
CN117632132A
CN117632132A CN202311557940.6A CN202311557940A CN117632132A CN 117632132 A CN117632132 A CN 117632132A CN 202311557940 A CN202311557940 A CN 202311557940A CN 117632132 A CN117632132 A CN 117632132A
Authority
CN
China
Prior art keywords
code
grammar tree
model
initial
tree
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311557940.6A
Other languages
Chinese (zh)
Inventor
陈曦
张美庆
潘海清
张平生
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Merchants Bank Co Ltd
Original Assignee
China Merchants Bank Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Merchants Bank Co Ltd filed Critical China Merchants Bank Co Ltd
Priority to CN202311557940.6A priority Critical patent/CN117632132A/en
Publication of CN117632132A publication Critical patent/CN117632132A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

The application discloses a code generation method, a code generation device, electronic equipment and a computer readable storage medium, and relates to the technical field of software codes, wherein the code generation method comprises the following steps: obtaining design model data, and converting the design model data into model metadata; obtaining structure configuration parameters, and analyzing the structure configuration parameters to obtain file output positions; acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template; and generating an object code file according to the object grammar tree, and storing the object code file to the file output position. The method and the device can generate the corresponding target code file according to the design model data, ensure the timely update of the code file, define the position of the target code file according to the structural configuration parameters, and facilitate flexible adaptation to development specifications of different teams.

Description

Code generation method, device, electronic equipment and computer readable storage medium
Technical Field
The present disclosure relates to the field of software codes, and in particular, to a code generating method, a code generating device, an electronic device, and a computer readable storage medium.
Background
With the popularity of Domain-Driven-Design (DDD) Design ideas and the rise of micro-service architecture and technology, more and more enterprises are put into the hot tide of DDD model Design. Among them, how to efficiently convert a model designed by DDD technology into an engineering code in the landing process of the DDD model, thereby reducing the repetition coding amount, is an important requirement.
Although some code generation tools exist today that can convert a designed model into a corresponding code based on a code template. However, the code generation scheme only supports the conversion of the newly added model into the code file, can not support the continuous iterative updating of the model and the code, and because the code structure of the code template is fixed, the generated code file structure is too single, and the differentiated requirements of enterprises and teams are difficult to meet.
Disclosure of Invention
The main purpose of the present application is to provide a code generation method, a device, an electronic device and a readable storage medium, which aim to solve the technical problem that the structure of a code file generated by the existing code generation scheme is single.
To achieve the above object, the present application provides a code generation method, including:
obtaining structure configuration parameters, and analyzing the structure configuration parameters to obtain file output positions;
obtaining design model data, and converting the design model data into model metadata;
acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template;
and generating an object code file according to the object grammar tree, and storing the object code file to the file output position.
Optionally, the step of obtaining the initial syntax tree includes:
judging whether a history code file with the same name and directory as the model metadata exists or not;
if yes, constructing an initial grammar tree according to the history code file;
if not, constructing an initial grammar tree according to a preset code template.
Optionally, the step of updating the initial syntax tree based on the model metadata includes:
judging whether the attribute names of the model metadata are the same as the attribute names in the initial grammar tree;
If the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
Optionally, the step of updating the initial syntax tree based on the model metadata includes:
judging whether the method signature of the model metadata is the same as the method signature in the initial grammar tree;
if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
Optionally, the step of processing the initial syntax tree according to a preset conflict handling logic includes:
if the conflict handling logic is overlay, updating the initial syntax tree with the model metadata as a reference;
if the conflict handling logic is skipped, the initial syntax tree is not updated.
Optionally, the structural configuration parameters further comprise code structural parameters, and after the step of converting the design model data into model metadata, the method further comprises:
Configuring a code structure corresponding to the model metadata according to the code structure parameters;
the step of generating the object code file according to the object grammar tree comprises the following steps:
traversing the target grammar tree, and converting the target grammar tree into a target code file conforming to the code structure, wherein the code structure at least comprises one of a class, an interface or a method.
Optionally, the code generating method further includes:
acquiring a custom scanning path, traversing the engineering file through the custom scanning path, and acquiring an updated code file;
converting the updated code file into an abstract syntax tree;
generating corresponding model metadata according to the abstract syntax tree, wherein the model metadata at least comprises one of class names, class notes, class inheritance information, class member variables and class method information;
comparing the model metadata with the design model to be updated to obtain a comparison result;
and updating the design model to be updated based on the comparison result to obtain a target design model.
The present application also provides a code generating apparatus applied to a code generating device, the code generating apparatus including:
The structure configuration module is used for acquiring structure configuration parameters and analyzing the structure configuration parameters to obtain file output positions;
the model conversion module is used for obtaining design model data and converting the design model data into model metadata;
the grammar tree updating module is used for acquiring an initial grammar tree, updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template;
and the file generation module is used for generating an object code file according to the object grammar tree and storing the object code file to the file output position.
The application also provides an electronic device, which is an entity device, and includes: the computer-readable storage medium includes a memory, a processor, and a program of the code generation method stored on the memory and executable on the processor, the program of the code generation method realizing the steps of the code generation method as described above when executed by the processor.
The present application also provides a computer-readable storage medium having stored thereon a program that implements a code generation method, which when executed by a processor, implements the steps of the code generation method as described above.
The present application also provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of a code generation method as described above.
The application provides a code generation method, a device, electronic equipment and a computer readable storage medium, wherein the code generation method firstly obtains structure configuration parameters, analyzes the structure configuration parameters and obtains a file output position; obtaining design model data, and converting the design model data into model metadata; acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template; according to the technical scheme, the storage position of the target code structure is determined through the structure configuration parameters, so that the code structure of the target code file generated according to design model data can be configured according to the user-defined structure configuration parameters, and the requirements of different team differentiated engineering structure specifications are met.
In addition, the technical scheme of the application generates the corresponding target code file through the design model data, so that the latest modification content can be timely converted into the corresponding code file when the model is modified in the development process, the consistency of the code file and the design model is ensured, and the continuous iterative updating of the model and the code is supported.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the application and together with the description, serve to explain the principles of the application.
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings that are required to be used in the description of the embodiments or the prior art will be briefly described below, and it will be obvious to those skilled in the art that other drawings can be obtained from these drawings without inventive effort.
FIG. 1 is a flowchart of a first embodiment of a code generation method of the present application;
FIG. 2 is a schematic diagram of a code generation step in a first embodiment of a code generation method according to the present application;
FIG. 3 is a flowchart illustrating a conflict handling process in a first embodiment of a code generation method according to the present application;
FIG. 4 is a schematic diagram of functional modules of code generation and model preservation in a second embodiment of the code generation method of the present application;
FIG. 5 is a flowchart of model update in a second embodiment of the code generation method of the present application;
fig. 6 is a schematic diagram of the composition structure of a code generating device in the embodiment of the present application;
fig. 7 is a schematic device structure diagram of a hardware operating environment related to a code generating method in an embodiment of the present application.
The implementation, functional features and advantages of the present application will be further described with reference to the accompanying drawings in conjunction with the embodiments.
Detailed Description
In order to make the above objects, features and advantages of the present application more comprehensible, the following description will make the technical solutions of the embodiments of the present application clear and complete with reference to the accompanying drawings of the embodiments of the present application. It will be apparent that the described embodiments are only some, but not all, of the embodiments of the present application. All other embodiments, based on the embodiments herein, which are within the scope of the protection of the present application, will be within the purview of one of ordinary skill in the art without the exercise of inventive faculty.
Example 1
The embodiment of the application provides a code generation method, which can be applied to the field of field-Driven Design, and with the popularity of Design ideas of field-Driven Design (DDD) and the rising of micro-service architecture and technology center, more and more enterprises participate in DDD hot flashes. After the model is designed by DDD technology, the model needs to be grounded, and then the model needs to be converted into a corresponding code file for execution. Typically, after the model is initially designed, the model may be converted into a corresponding code file by various well-established code generation tools currently in the market. In the life cycle after the design model, a certain modification and updating are needed to be carried out on the design model, and the corresponding code file also needs to be correspondingly modified. If the modification is performed manually, the workload is excessive, errors are prone to occur, and the modification efficiency is low. If the modified model is converted again by the code generation tool, since most of the content is unmodified, a great deal of unnecessary time and computing resources are consumed in the code generation process, and the modification efficiency is also affected. On the other hand, since the structure of the code template of the code generation tool is fixed, enterprise-level, differentiated code structures cannot be supported.
In a first embodiment of the code generation method of the present application, referring to fig. 1, the code generation method includes:
s10, obtaining structure configuration parameters, and analyzing the structure configuration parameters to obtain file output positions;
it should be noted that, the configuration parameters of the structure are user-defined parameters for configuring the model and the code structure, and support the user to configure the engineering code structure in a personalized manner, where the configuration parameters of the structure may include the code structure and the file output position of the code, so that the process of generating the code according to the model has higher flexibility, and can quickly generate the code structure such as Java class, interface or method meeting the requirements of the user standard and specification, and can customize the code file position according to the user requirement.
Optionally, in the structural configuration parameters, the user may customize the output path, naming convention, etc. of the object code file converted and generated by each design model.
Step S20, obtaining design model data, and converting the design model data into model metadata;
in this embodiment of the present application, the design model data is data corresponding to a model designed by a DDD technology, where the design model includes an adaptation layer (adapter, etc.), an application layer (application service, transmission object DTO, etc.), a domain layer (entity, value object, domain service, etc.), and an infrastructure layer (PO (Persistant Object, persistence object) entity, a Mapper interface, etc.). In addition, model metadata refers to data representing a description model, such as a solid model, including metadata of entity codes, types, descriptions, attribute lists, method lists, and the like. The model metadata may be used to modify and update the syntax tree to make the syntax tree more fit to the design model corresponding to the design model data.
Step S30, an initial grammar tree is obtained, and the initial grammar tree is updated based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template;
in this embodiment of the present application, it should be noted that in step S30, two cases may exist, where one case is that the design model data is design model data obtained by modifying the original design model, and a corresponding historical code file has been generated according to the original design model data, and this need is to update the design model and update the historical code file synchronously, and in this case, the historical code file may be converted to generate a corresponding initial syntax tree. In another case, the design model data is data corresponding to a model of the initial design, and no history code file is generated before, in this case, the current demand is to generate an object code file according to the newly added design model, and at this time, a preset code template can be converted to generate an initial syntax tree for updating and modifying in the next step so as to obtain the object code file consistent with the design model.
In addition, in the process of updating the initial grammar tree through the model metadata, various types of information in the model metadata are sequentially compared with the content in the initial grammar tree, and when certain types of data which are included in the model metadata but not included in the initial grammar tree are encountered, the types of data are directly added into the initial grammar tree; when some data contained in the model metadata and also contained in the initial grammar tree is encountered, the model metadata and the grammar tree data are described as having data conflict, and a trade-off is needed between the model metadata and the grammar tree data, at this time, the data in the initial grammar tree can be modified or reserved according to preset conflict processing logic, wherein the data in some type can be one of attribute names or method signatures.
And step S40, generating an object code file according to the object grammar tree, and storing the object code file to the file output position.
After a target grammar tree consistent with design model data is obtained, each node of the target grammar tree can be traversed in sequence, an internal logic structure of the target grammar tree is abstracted, the relation between each node of the grammar tree and the node is converted into a corresponding code statement, and finally a target code file is generated, wherein the target code file is a code file consistent with the design model of the design model data, the preservation of codes is realized, when the model is changed, the latest target code file can be timely generated through the code generation method, the timely update of the code file is ensured, and the landing progress of a project is further accelerated.
In addition, the object code file generated in the embodiment of the present application may be stored under a folder specified by a user according to a file output position obtained by parsing the structural configuration parameter. It can be understood that the object code file includes a plurality of subcode files, and each subcode file is stored in a different folder according to a corresponding file storage location in the file output location. Therefore, the code structure of the target code file output in the embodiment of the application meets the personalized requirements of the user, and meets the differentiated development specification requirements of different enterprises or groups.
In a possible embodiment, in step S30, the step of obtaining the initial syntax tree may include:
step S31, judging whether a history code file with the same name and directory as the model metadata exists or not;
step S32, if yes, constructing an initial grammar tree according to the history code file;
and S33, if the initial grammar tree does not exist, constructing the initial grammar tree according to a preset code template.
In this embodiment of the present application, it is necessary to first retrieve the name and directory of the model metadata in the file code stored in a local machine (e.g., a computer) to determine whether there is a historical code file that is the same as the name and directory of the model metadata, because, when the code file is stored, the name may be used to characterize the name of the design model corresponding to the code file, and the directory further characterizes the specific hierarchical structure of the design model of the code file. Therefore, when the name and the catalog of the model metadata are all consistent with a certain historical code file stored by the machine, the historical code file and the model metadata can be identified to correspond to the same design model.
After the history code file is retrieved, the purpose of the code generation can be judged to be to update the existing history code file, and the history code file can be directly called to construct an initial grammar tree, wherein in the process of constructing the initial grammar tree through the history code file, the history code file can be analyzed through a Java Parser (Java language lexical analysis tool) so as to generate the initial grammar tree, and the initial grammar tree is AST (Abstract Syntax Tree, abstract grammar tree). As such, the gap between the initial syntax tree and the model metadata is relatively small, and the time and computational resources consumed in updating the initial syntax tree with the model metadata are relatively small.
As an example, referring to fig. 2, the detailed steps of the code generation method may include: firstly, obtaining design model data; then, analyzing the file generation position (namely the file output position) of the design model data by combining with the self-defined engineering structure; converting the design model into model metadata; judging whether files with the same name and the same catalog of the model metadata exist in the codes or not; if yes, constructing a grammar tree based on the stock code (i.e. the history code file); if not, constructing a grammar tree based on the initial template (namely a preset code template); the syntax tree is then updated based on the model metadata, code is generated based on the syntax tree, and generated down to the specified file (i.e., file output location).
On the other hand, when the history code file is not retrieved, the purpose of code generation at this time can be judged to be the target code file corresponding to the newly added design model, namely, a preset code template can be adopted to automatically generate a corresponding initial grammar tree, and the specific generation process can refer to the content, wherein the preset code template is a fixed template.
In some possible embodiments, in step S30, the step of updating the initial syntax tree based on the model metadata may include:
step S34, judging whether the attribute names of the model metadata are the same as the attribute names in the initial grammar tree;
step S35, if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
step S36, if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
It should be noted that, the initial syntax tree in this embodiment is a syntax tree generated by the history code file, that is, an inventory code syntax tree. In the stored code syntax tree, data possibly existing in the conflict of the model metadata, the data possibly existing in the conflict are data with the same attribute name, and the method for comparing the model metadata with the initial syntax tree through the attribute name can be called class attribute comparison. Specifically, if the data of a certain attribute name in the model metadata and the data of each attribute name in the stock code syntax tree are different, the data under the attribute name is directly added into the stock code syntax tree. If a certain attribute name in the model metadata is the same as an attribute name in the stock code syntax tree, the model metadata and the stock code syntax tree have the same attribute name data, and the data may be inconsistent. If the data of the two are consistent, the data can be updated or not updated, and the finally obtained target grammar tree is not influenced; if the data are inconsistent, determining whether the data under the attribute name in the model metadata are standard or standard in the stock code grammar tree by a preset conflict processing logic, wherein the conflict processing logic can perform self-defined setting according to the user requirement so as to meet the updating requirement of the code file under the actual condition.
In another possible embodiment, in step S30, the step of updating the initial syntax tree based on the model metadata may further include:
step S37, judging whether the method signature of the model metadata is the same as the method signature in the initial grammar tree;
step S38, if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
step S39, if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
Based on the class attribute comparison methods provided in steps S34 to S36, the embodiment of the present application further provides a class attribute comparison method. Specifically, if a certain attribute name in the model metadata and the data signed by each method in the stock code grammar tree are different, the data under the signature of the method is directly added into the stock code grammar tree. If a method signature in the model metadata is the same as a method signature in the stock code syntax tree, then the two have data signed by the same method, and the data may be inconsistent. If the data of the two are consistent, the data can be updated or not updated, and the finally obtained target grammar tree is not influenced; if the data are inconsistent, determining whether the data under the signature of the method in the model metadata are standard or the data under the signature of the method in the stock code grammar tree is standard by a preset conflict processing logic, wherein the conflict processing logic can be subjected to self-defining setting according to the user requirement so as to meet the updating requirement of the code file under the actual condition.
As an example, referring to fig. 3, upon encountering a conflict of model metadata with syntax tree data, the steps of processing logic may include: in the process of comparing the model metadata with the grammar tree, class attribute comparison or class method comparison can be selected; the comparison of the class attributes comprises judging whether attribute names are renamed or not, and if the attribute names are not renamed, updating grammar tree data by taking model metadata as a reference; if the name is the same, determining a processing method according to conflict processing logic, specifically, if the conflict processing logic is coverage, updating the grammar tree data by taking the model metadata as a standard, and if the conflict processing logic is skip, not updating the grammar tree data; class method signatures include determining whether a method signature is renamed; if the name is not the duplicate name, updating the grammar tree data by taking the model metadata as the standard; if the name is the same, the processing method is further determined according to the conflict processing logic, specifically, if the conflict processing logic is the overlay, the grammar tree data is updated based on the model metadata, and if the conflict processing logic is the skip, the grammar tree data is not updated.
Further, in step S36 or step S39, the step of processing the initial syntax tree according to the preset conflict handling logic may include:
Step A10, if the conflict processing logic is coverage, updating the initial grammar tree by taking the model metadata as a reference;
step A20, if the conflict handling logic is skipped, the initial syntax tree is not updated.
In the embodiment of the application, two conflict processing logics including "coverage" and "skip" are provided, and the processing method can be used for guiding the processing of the initial grammar when the attribute name of the model metadata is the same as the attribute name in the stock code grammar tree (i.e. the initial grammar tree) or the method signature of the model metadata is the same as the method signature in the stock code grammar tree, and the processing method comprises updating or not updating.
For example, if the conflict handling logic is an overlay, it indicates that the data in the stock code syntax tree that conflicts with the model metadata needs to be overlaid according to the model metadata, that is, the data in the model metadata is replaced by the data in the stock code syntax tree, where the conflict handling logic is generally used when the original content of the design model is modified at this time or when the weight of the design model is high. On the other hand, if the conflict handling logic is skipped, it means that the conflict data in the stock code syntax tree is not updated based on the stock code syntax tree, and this conflict handling logic is generally used when there is a new design model or when the weight of the stock code file is large.
Aiming at the situation that engineering code data and model data conflict, two effective conflict processing logics and solutions of 'model-based covering processing' and 'engineering code-based skipping processing' are provided in the embodiment of the application, and the problem of inconsistent engineering code and model data caused by model change of projects in the iterative updating process can be solved, so that the final bidirectional consistency and freshness keeping of the model and the code can be ensured under the situation that the code generation conflict is solved by combining a model freshness keeping scheme.
In another possible embodiment, the structural configuration parameters further include code structural parameters, and after the step of converting the design model data into model metadata, the method may further include:
step B10, configuring a code structure corresponding to the model metadata according to the code structure parameters;
the step of generating the object code file according to the object grammar tree comprises the following steps:
and step B20, traversing the target grammar tree, and converting the target grammar tree into a target code file conforming to the code structure, wherein the code structure at least comprises one of a class, an interface or a method.
In the embodiment of the application, a method for carrying out self-defining setting on the model metadata and the code structure of the corresponding target code file through the code structure parameters in the structure configuration parameters preset by a user is further provided, so that the code structure of the generated target code file can more accord with the personalized difference structure requirement of a team or an enterprise where the user is located, a team-level engineering structure configuration function is provided, and the different team-level differential engineering structure specification requirements are met. It can be understood that the configuration parameters include code structure parameters for setting and defining code structures corresponding to each piece of model metadata in the current design model data, so that each code structure in the object code file generated by the conversion of the design model data meets the personalized requirements of the user.
Illustratively, the code structures include one or more of the class, interface, or method of Java code structures. It can be understood that, in an object code file, a plurality of subcode files are included, each word code file has a respective code structure and is stored to a respective different file position, and the finally formed object code file is a code file with a storage position and a code structure both meeting the requirements of structural configuration parameters input by a user, so that differentiation and individuation of the code file are fully reflected, the code is convenient to attach to team/enterprise-level code specification requirements in the landing process, and the method has the characteristics of rigorousness, high efficiency, stability and the like, and can help enterprises to reduce research and development cost, improve project quality and accelerate project progress in the field of Java program development.
The embodiment of the application provides a code generation method, which comprises the steps of firstly obtaining structural configuration parameters, and analyzing the structural configuration parameters to obtain file output positions; obtaining design model data, and converting the design model data into model metadata; acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template; according to the technical scheme of the embodiment of the application, the storage position of the target code structure is determined through the structure configuration parameters, so that the structure of the target code file generated according to the design model data can be configured according to the user-defined structure configuration parameters, and the requirements of different team differentiated engineering structure specifications are met.
In addition, the technical scheme of the embodiment of the application generates the corresponding target code file through the design model data, so that the latest modified content can be timely converted into the corresponding code file when the model is modified in the development process, the consistency of the code file and the design model is ensured, and the continuous iterative updating of the model and the code is supported.
Example two
Further, in another embodiment of the present application, the same or similar content as the first embodiment may be referred to the above description, and will not be repeated. On this basis, after modifying and updating the code file, the method may further include:
step C10, acquiring a self-defined scanning path, traversing the engineering file through the self-defined scanning path, and acquiring an updated code file;
step C20, converting the updated code file into an abstract syntax tree;
step C30, generating corresponding model metadata according to the abstract syntax tree, wherein the model metadata at least comprises one of class names, class notes, class inheritance information, class member variables and class method information;
step C40, comparing the model metadata with the design model to be updated to obtain a comparison result;
and step C50, updating the design model to be updated based on the comparison result to obtain a target design model.
In the embodiment of the application, the method for updating the original design model to be updated according to the updated code file on the basis of the updated code file can be applied to a scene that the design model needs to be synchronously updated after the target code file is modified, wherein the design model to be updated is a design model corresponding to the updated code file before modification. In addition, the user-defined scan path is a path which is input by the user and is used for inquiring the updated update code file. The abstract syntax tree is an ATS tree, is an abstract representation of a syntax structure of an update code file, represents the syntax structure of a programming language in a tree form, each node on the tree represents a structure in source codes of the update code file, the comparison result comprises a place which is inconsistent with the model metadata in model metadata of a design model to be updated, and in the process of updating the design model to be updated based on the comparison result, the place which is inconsistent with the model metadata in the design model to be updated is mainly modified to be consistent with the model metadata, so that synchronous updating and bidirectional refreshing of the design model and a target code file are realized.
By combining the technical scheme of the first embodiment, the code generation method disclosed by the embodiment of the application can be applied to a model code bidirectional freshness retaining system, and is used for guaranteeing bidirectional freshness retaining of a design model and a code file and avoiding the condition of untimely updating. Referring to fig. 4, the model code bi-directional freshness protection system may include two functional modules, code generation and model freshness protection. Specifically, the code generation module provides five functions of converting a design model into model metadata, constructing a grammar tree based on an initial template, constructing a grammar tree based on stock codes, updating the grammar tree based on the model metadata, and generating codes based on an AST grammar tree (adopting JavaParser technology). The code generation process also comprises a step of processing conflict between the code and the model, and generating according to a strategy designated by a user, so as to realize the generation of the corresponding code according to the content of the model design. The model freshness keeping module provides a grammar tree construction method based on an AST grammar tree technology, generates model metadata according to the grammar tree, compares the model metadata with model metadata of a design model to be updated to obtain a comparison result, and finally can combine the comparison result (namely a change place) into the design model to realize the synchronization of the change in the code to the design model. In addition, the model code bidirectional freshness retaining system also provides a team-level code engineering structure configuration function so as to meet the engineering structure specification requirements of different team differentiation. Specifically, the output path, naming convention, etc. of each model may be defined in a configuration file.
Illustratively, referring to FIG. 5, the step of updating the design model from the updated code file includes: firstly traversing a code file, judging whether a file directory is matched with a user-defined scanning path input by a user, if so, constructing a grammar tree based on the scanned code file, generating corresponding model metadata according to the grammar tree, comparing the model metadata with model metadata of a design model to obtain a comparison result, and finally updating the comparison result to the design model.
The technical scheme of the embodiment of the application provides a synchronous updating function of the model and the code in the iterative process. By combining the technical scheme of the first embodiment, when the model is modified in the development process, the latest modified content can be timely converted into corresponding engineering codes; and when the codes are changed in development, the modeling data can be updated through the model freshness keeping function, so that the bidirectional freshness keeping of the model and the codes is realized.
Example III
The embodiment of the application also provides a code generating device, which is applied to a code generating device, and referring to fig. 6, the code generating device includes:
the structure configuration module 10 is used for obtaining structure configuration parameters and analyzing the structure configuration parameters to obtain file output positions;
A model conversion module 20 for obtaining design model data and converting the design model data into model metadata;
the grammar tree updating module 30 is configured to obtain an initial grammar tree, update the initial grammar tree based on the model metadata, and obtain a target grammar tree, where the initial grammar tree is a grammar tree corresponding to a history code file or a preset code template;
and the file generating module 40 is configured to generate an object code file according to the object syntax tree, and store the object code file to the file output position.
Optionally, the syntax tree updating module 30 is further configured to:
judging whether a history code file with the same name and directory as the model metadata exists or not;
if yes, constructing an initial grammar tree according to the history code file;
if not, constructing an initial grammar tree according to a preset code template.
Optionally, the syntax tree updating module 30 is further configured to:
judging whether the attribute names of the model metadata are the same as the attribute names in the initial grammar tree;
if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
Optionally, the syntax tree updating module 30 is further configured to:
judging whether the method signature of the model metadata is the same as the method signature in the initial grammar tree;
if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
Optionally, the syntax tree updating module 30 is further configured to:
if the conflict handling logic is overlay, updating the initial syntax tree with the model metadata as a reference;
if the conflict handling logic is skipped, the initial syntax tree is not updated.
Optionally, the code generating device further includes a structural configuration module, and the structural configuration module is further configured to:
configuring a code structure corresponding to the model metadata according to the code structure parameters;
the file generation module is further configured to:
traversing the target grammar tree, and converting the target grammar tree into a target code file conforming to the code structure, wherein the code structure at least comprises one of a class, an interface or a method.
Optionally, the code generating device further includes a model updating module, and the model updating module is further configured to:
acquiring a custom scanning path, traversing the engineering file through the custom scanning path, and acquiring an updated code file;
converting the updated code file into an abstract syntax tree;
generating corresponding model metadata according to the abstract syntax tree, wherein the model metadata at least comprises one of class names, class notes, class inheritance information, class member variables and class method information;
comparing the model metadata with the design model to be updated to obtain a comparison result;
and updating the design model to be updated based on the comparison result to obtain a target design model.
The code generation device provided by the application adopts the code generation method in the embodiment, and solves the technical problems of low accuracy and comprehensiveness of the test result of the existing multi-channel platform product. Compared with the prior art, the beneficial effects of the code generating device provided by the embodiment of the present application are the same as those of the code generating method provided by the above embodiment, and other technical features of the code generating device are the same as those disclosed in the method of the previous embodiment, which are not described in detail herein.
Example IV
The embodiment of the application provides electronic equipment, the electronic equipment includes: at least one processor; and a memory communicatively linked to the at least one processor; the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor, so that the at least one processor can execute the code generating method in the first embodiment.
Referring now to fig. 7, a schematic diagram of an electronic device suitable for use in implementing embodiments of the present disclosure is shown. The electronic devices in the embodiments of the present disclosure may include, but are not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistant, personal digital assistants), PADs (tablet computers), PMPs (Portable Media Player, portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and the like, and stationary terminals such as digital TVs, desktop computers, and the like. The electronic device shown in fig. 7 is merely an example and should not be construed to limit the functionality and scope of use of the disclosed embodiments.
As shown in fig. 7, the electronic apparatus may include a processing device 1001 (e.g., a central processing unit, a graphics processor, or the like) that can perform various appropriate actions and processes according to a program stored in a read only memory 1002 (ROM) or a program loaded from a storage device 1003 into a random access memory 1004 (RAM, random access memory). In the RAM1004, various programs and data required for the operation of the electronic device are also stored. The processing device 1004, the ROM1002, and the RAM1004 are connected to each other by a bus 1005. An input/output (I/O) interface 1006 is also linked to bus 1005.
In general, the following systems may be linked to the I/O interface 1006: input devices 1007 including, for example, a touch screen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, and the like; an output device 1008 including, for example, a liquid crystal display (LCD, liquid crystal display), a speaker, a vibrator, and the like; storage device 1003 including, for example, a magnetic tape, a hard disk, and the like; and communication means 1009. The communication means 1009 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. While electronic devices having various systems are shown in the figures, it should be understood that not all of the illustrated systems are required to be implemented or provided. More or fewer systems may alternatively be implemented or provided.
In particular, according to embodiments of the present disclosure, the processes described above with reference to flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method shown in the flowcharts. In such an embodiment, the computer program may be downloaded and installed from a network via a communication device, or installed from a storage device, or installed from ROM. The above-described functions defined in the methods of the embodiments of the present disclosure are performed when the computer program is executed by a processing device.
The electronic equipment provided by the application adopts the code generation method in the embodiment, and solves the technical problems of low accuracy and comprehensiveness of the test result of the existing multi-channel platform product. Compared with the prior art, the beneficial effects of the electronic device provided by the embodiment of the present application are the same as those of the code generation method provided by the first embodiment, and other technical features of the electronic device are the same as those disclosed by the method of the previous embodiment, which are not described in detail herein.
It should be understood that portions of the present disclosure may be implemented in hardware, software, firmware, or a combination thereof. In the description of the above embodiments, particular features, structures, materials, or characteristics may be combined in any suitable manner in any one or more embodiments or examples.
The foregoing is merely specific embodiments of the present application, but the scope of the present application is not limited thereto, and any person skilled in the art can easily think about changes or substitutions within the technical scope of the present application, and the changes and substitutions are intended to be covered by the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.
Example five
The present embodiment provides a computer-readable storage medium having computer-readable program instructions stored thereon for performing the method of code generation in the first embodiment described above.
The computer readable storage medium provided by the embodiments of the present application may be, for example, a usb disk, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, or device, or a combination of any of the foregoing. More specific examples of the computer-readable storage medium may include, but are not limited to: an electrical link having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-Only Memory (ROM), an erasable programmable read-Only Memory (EPROM, erasable Programmable Read-Only Memory, or flash Memory), an optical fiber, a portable compact disc read-Only Memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In this embodiment, a computer-readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, or device. Program code embodied on a computer readable storage medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, fiber optic cables, RF (radio frequency), and the like, or any suitable combination of the foregoing.
The above-described computer-readable storage medium may be contained in an electronic device; or may exist alone without being assembled into an electronic device.
The computer-readable storage medium carries one or more programs that, when executed by an electronic device, cause the electronic device to: obtaining structure configuration parameters, and analyzing the structure configuration parameters to obtain file output positions; obtaining design model data, and converting the design model data into model metadata; acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template; and generating an object code file according to the object grammar tree, and storing the object code file to the file output position.
Computer program code for carrying out operations of the present disclosure may be written in one or more programming languages, including an object oriented programming language such as Java, smalltalk, C ++ and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be linked to the user's computer through any kind of network, including a local area network (LAN, local area network) or a wide area network (WAN, wide Area Network), or it may be linked to an external computer (e.g., through the internet using an internet service provider).
The flowcharts and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present disclosure may be implemented in software or hardware. Wherein the name of the module does not constitute a limitation of the unit itself in some cases.
The computer readable storage medium is stored with the computer readable program instructions for executing the code generation method, and solves the technical problems of low accuracy and comprehensiveness of the test result of the existing multi-channel platform product. Compared with the prior art, the beneficial effects of the computer readable storage medium provided in the embodiment of the present application are the same as those of the code generation method provided in the above embodiment, and are not described herein.
Example six
The present application also provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of a code generation method as described above.
The computer program product provided by the application solves the technical problems of low accuracy and comprehensiveness of the test results of the existing multi-channel platform product. Compared with the prior art, the beneficial effects of the computer program product provided by the embodiment of the present application are the same as those of the code generation method provided by the foregoing embodiment, and are not described herein in detail.
The foregoing description is only of the preferred embodiments of the present application and is not intended to limit the scope of the claims, and all equivalent structures or equivalent processes using the descriptions and drawings of the present application, or direct or indirect application in other related technical fields are included in the scope of the claims.

Claims (10)

1. A code generation method, characterized in that the code generation method comprises:
obtaining structure configuration parameters, and analyzing the structure configuration parameters to obtain file output positions;
obtaining design model data, and converting the design model data into model metadata;
acquiring an initial grammar tree, and updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template;
and generating an object code file according to the object grammar tree, and storing the object code file to the file output position.
2. The code generation method of claim 1, wherein the step of obtaining an initial syntax tree comprises:
judging whether a history code file with the same name and directory as the model metadata exists or not;
if yes, constructing an initial grammar tree according to the history code file;
if not, constructing an initial grammar tree according to a preset code template.
3. The code generation method of claim 1, wherein the updating the initial syntax tree based on the model metadata comprises:
Judging whether the attribute names of the model metadata are the same as the attribute names in the initial grammar tree;
if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
4. The code generation method of claim 1, wherein the updating the initial syntax tree based on the model metadata comprises:
judging whether the method signature of the model metadata is the same as the method signature in the initial grammar tree;
if the model metadata are different, updating the initial grammar tree by taking the model metadata as a reference;
if the model metadata are the same, the model metadata are judged to have conflict with the initial grammar tree, and the initial grammar tree is processed according to preset conflict processing logic.
5. The code generation method of claim 3 or 4, wherein the step of processing the initial syntax tree according to a preset conflict handling logic comprises:
if the conflict handling logic is overlay, updating the initial syntax tree with the model metadata as a reference;
If the conflict handling logic is skipped, the initial syntax tree is not updated.
6. The code generation method of claim 1, wherein the structural configuration parameters further comprise code structural parameters, the method further comprising, after the step of converting the design model data into model metadata:
configuring a code structure corresponding to the model metadata according to the code structure parameters;
the step of generating the object code file according to the object grammar tree comprises the following steps:
traversing the target grammar tree, and converting the target grammar tree into a target code file conforming to the code structure, wherein the code structure at least comprises one of a class, an interface or a method.
7. The code generation method of claim 1, wherein the code generation method further comprises:
acquiring a custom scanning path, traversing the engineering file through the custom scanning path, and acquiring an updated code file;
converting the updated code file into an abstract syntax tree;
generating corresponding model metadata according to the abstract syntax tree, wherein the model metadata at least comprises one of class names, class notes, class inheritance information, class member variables and class method information;
Comparing the model metadata with the design model to be updated to obtain a comparison result;
and updating the design model to be updated based on the comparison result to obtain a target design model.
8. A code generation apparatus, the code generation apparatus comprising:
the structure configuration module is used for acquiring structure configuration parameters and analyzing the structure configuration parameters to obtain file output positions;
the model conversion module is used for obtaining design model data and converting the design model data into model metadata;
the grammar tree updating module is used for acquiring an initial grammar tree, updating the initial grammar tree based on the model metadata to obtain a target grammar tree, wherein the initial grammar tree is a grammar tree corresponding to a historical code file or a preset code template;
and the file generation module is used for generating an object code file according to the object grammar tree and storing the object code file to the file output position.
9. An electronic device, the electronic device comprising:
at least one processor; the method comprises the steps of,
a memory communicatively linked to the at least one processor; wherein,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the steps of the code generation method of any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that the computer-readable storage medium has stored thereon a program that implements a code generation method, the program implementing the code generation method being executed by a processor to implement the steps of the code generation method according to any one of claims 1 to 7.
CN202311557940.6A 2023-11-20 2023-11-20 Code generation method, device, electronic equipment and computer readable storage medium Pending CN117632132A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311557940.6A CN117632132A (en) 2023-11-20 2023-11-20 Code generation method, device, electronic equipment and computer readable storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311557940.6A CN117632132A (en) 2023-11-20 2023-11-20 Code generation method, device, electronic equipment and computer readable storage medium

Publications (1)

Publication Number Publication Date
CN117632132A true CN117632132A (en) 2024-03-01

Family

ID=90020883

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311557940.6A Pending CN117632132A (en) 2023-11-20 2023-11-20 Code generation method, device, electronic equipment and computer readable storage medium

Country Status (1)

Country Link
CN (1) CN117632132A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN118377472A (en) * 2024-06-26 2024-07-23 华能信息技术有限公司 Quick construction system based on code automatic generation

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN118377472A (en) * 2024-06-26 2024-07-23 华能信息技术有限公司 Quick construction system based on code automatic generation

Similar Documents

Publication Publication Date Title
WO2020119485A1 (en) Page display method and device, apparatus, and storage medium
US8789015B2 (en) Integrated application localization
US9465590B2 (en) Code generation framework for application program interface for model
US9442744B2 (en) Multilingual build integration for compiled applications
CN104932973A (en) Version compatibility test method and apparatus
EP3834080B1 (en) Static reconcilliation of application view hierarchies
CN109726217A (en) A kind of database operation method, device, equipment and storage medium
CN110727869A (en) Page construction method and device
CN113448562A (en) Automatic logic code generation method and device and electronic equipment
CN111290737A (en) Method and device for application program development and electronic equipment
CN117632132A (en) Code generation method, device, electronic equipment and computer readable storage medium
WO2023130933A1 (en) Model processing method and apparatus, code aquisition method and apparatus, and electronic device
CN111984300B (en) Code copying method and device, electronic equipment and computer readable storage medium
WO2023109648A1 (en) Model-code synchronization method
CN113296759B (en) User interface processing method, user interface processing system, device and storage medium
CN115964042A (en) Menu generation method and device, storage medium and electronic equipment
CN114692055A (en) Form processing method and device, storage medium and electronic equipment
CN110795102B (en) Module compiling method, device, electronic equipment and computer readable medium
CN116775174A (en) Processing method, device, equipment and medium based on user interface frame
CN115658276B (en) Service development method, device, electronic equipment and readable storage medium
CN114564226B (en) Code deployment method and device, electronic equipment and readable storage medium
US20240330566A1 (en) Page display method, apparatus, electronic device and storage medium
CN117573178A (en) Robot firmware updating method, apparatus and readable storage medium
CN115857937A (en) Incremental compiling method, system, electronic device and storage medium
CN116009951A (en) Code processing method, device, equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination