Code generation method and device, equipment and storage medium

By analyzing the target language text written in a specific language in the field, generating code in multiple programming languages, solving the problem of insufficient universality of code generation methods in the prior art, and achieving efficient and flexible code generation.

CN119311277APending Publication Date: 2025-01-14TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202310873579.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-07-14
Publication Date
2025-01-14

Smart Images

  • Figure CN119311277A_ABST
    Figure CN119311277A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code generation method and device, equipment and a storage medium, and relates to the technical field of computers.The method comprises the steps that a target language text corresponding to a target service is obtained, the target language text is compiled through a field specific language DSL, the target language text is analyzed, metadata corresponding to the target language text is obtained, and the metadata are stored in a database; codes of M different programming languages are generated according to the metadata and preset code template files of M different programming languages, M is a positive integer, and the code template file of one programming language comprises a calling structure of the programming language, a grammar structure of the programming language and template variables used for generating code snippets of the programming language. Therefore, the codes of various different programming languages can be generated based on the target language text corresponding to the target business, and the universality and efficiency of automatically generating the codes are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a code generation method, apparatus, device and storage medium. Background Art

[0002] At present, code generation is mainly used in low-code technology or auxiliary tool capabilities, which is suitable for improving the efficiency and cost of software development and improving the ability to quickly deliver software. Among them, low-code refers to a development method that can quickly generate applications with a small amount of code or no coding.

[0003] In the existing code generation methods, generally, conversion call generation for specific components is performed, such as entity and database call generation for a database, which is only code generation for a specific programming language for a specific application.

[0004] However, the above method only supports the generation of framework code for a specific application scenario. Moreover, in actual production projects, different systems or modules may be written in different programming languages. Therefore, the existing code generation method has certain limitations and is not very universal. Summary of the invention

[0005] The embodiments of the present application provide a code generation method, apparatus, device and storage medium, which can generate codes in multiple different programming languages, thereby improving the universality and efficiency of automatic code generation.

[0006] In a first aspect, an embodiment of the present application provides a code generation method, comprising:

[0007] Obtaining a target language text corresponding to a target business, where the target language text is written in a domain specific language (DSL);

[0008] Parsing the target language text to obtain metadata corresponding to the target language text;

[0009] According to the metadata and preset code template files of M different programming languages, codes of the M different programming languages ​​are generated, where M is a positive integer, and a code template file of a programming language includes a calling structure of the programming language, a grammatical structure of the programming language, and template variables for generating code snippets of the programming language.

[0010] In a second aspect, an embodiment of the present application provides a code generation device, including:

[0011] An acquisition module, used to acquire a target language text corresponding to a target business, wherein the target language text is written in a domain specific language DSL;

[0012] A parsing module, used for parsing the target language text to obtain metadata corresponding to the target language text;

[0013] A code generation module is used to generate codes for M different programming languages ​​based on the metadata and preset code template files for M different programming languages, where M is a positive integer and a code template file for a programming language includes a calling structure of the programming language, a grammatical structure of the programming language and template variables for generating code snippets for the programming language.

[0014] In a third aspect, an embodiment of the present application provides a code generation device, comprising: a processor and a memory, the memory being used to store a computer program, the processor being used to call and run the computer program stored in the memory to execute the method of the first aspect.

[0015] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium for storing a computer program, wherein the computer program enables a computer to execute the method of the first aspect.

[0016] In a fifth aspect, an embodiment of the present application provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute the method described in the first aspect.

[0017] In summary, in the embodiment of the present application, by first obtaining the target language text corresponding to the target business written using DSL, and then parsing the target language text to obtain the metadata corresponding to the target language text, the code in M ​​different programming languages ​​is generated according to the metadata and the preset code template files of M different programming languages. By writing the target language text corresponding to the target business using DSL, and parsing independent metadata that is not related to the programming language from the target language text written in DSL, the DSL description removes the restrictions of the project language, uses the preset code template files of different programming languages ​​to fix the basic syntax of various programming languages, and then generates codes in different programming languages ​​according to the metadata combined with the code template files of different programming languages. Thus, codes in multiple different programming languages ​​can be generated based on the target language text corresponding to the target business, thereby improving the universality and efficiency of automatic code generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0019] Figure 1A schematic diagram of an implementation scenario of a code generation method provided in an embodiment of the present application;

[0020] Figure 2 A flowchart of a code generation method provided in an embodiment of the present application;

[0021] Figure 3 A schematic diagram after orchestration corresponding to the business logic of creating a model;

[0022] Figure 4 A flowchart of a code generation method provided in an embodiment of the present application;

[0023] Figure 5 A logic arrangement flow chart when a target business is a lottery provided in an embodiment of the present application;

[0024] Figure 6 A schematic diagram of a main process template interpretation and execution process provided in an embodiment of the present application;

[0025] Figure 7 A schematic diagram of the structure of a code generation device provided in an embodiment of the present application;

[0026] Figure 8 It is a schematic block diagram of a code generation device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0027] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0028] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product, or server that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products, or devices.

[0029] Before introducing the technical solution of the embodiment of the present application, the following is an introduction to the relevant knowledge of the embodiment of the present application:

[0030] 1. Natural language processing (NLP) is an important direction in the fields of computer science and artificial intelligence. It studies various theories and methods that can achieve effective communication between people and computers using natural language. Natural language processing is a science that integrates linguistics, computer science, and mathematics. Therefore, research in this field will involve natural language, that is, the language people use in daily life, so it is closely related to the study of linguistics. Natural language processing technology usually includes text processing, semantic understanding, machine translation, robot question answering, knowledge graph and other technologies.

[0031] 2. Domain Specific Language (DSL) is a computer language that focuses on a certain application field, also translated as domain-specific language.

[0032] 3. Metadata is data used to describe data. The data content in metadata can be used to generate code.

[0033] 4. Golang (Go), a statically strongly typed, compiled, concurrent programming language with garbage collection capabilities.

[0034] 5. Operator: a computing unit. Any operation performed on any function can be regarded as an operator.

[0035] The existing code generation method has certain limitations and is not universal. To solve this technical problem, an embodiment of the present application provides a code generation method, which first obtains the target language text corresponding to the target business written using DSL, and then parses the target language text to obtain the metadata corresponding to the target language text, and generates M codes in different programming languages ​​according to the metadata and the preset code template files of M different programming languages. In this application, by writing the target language text corresponding to the target business using DSL, parsing independent metadata that is not related to the programming language from the target language text written in DSL, the DSL description removes the restrictions of the project language, and uses the preset code template files of different programming languages ​​to fix the basic syntax of various programming languages, and then generates codes in different programming languages ​​according to the metadata combined with the code template files of different programming languages. Thus, codes in multiple different programming languages ​​can be generated based on the target language text corresponding to the target business, thereby improving the universality and efficiency of automatic code generation.

[0036] The following is a brief introduction to the application scenarios to which the technical solutions of the embodiments of the present application can be applied. It should be noted that the application scenarios introduced below are only used to illustrate the embodiments of the present application and are not limited. In specific implementation, the technical solutions provided by the embodiments of the present application can be flexibly applied according to actual needs.

[0037] The embodiments of the present application can be applied to low-code / zero-code platforms. Low-code is a method of developing applications through visualization, which allows developers to create services / applications by dragging and dropping multiple code elements to arrange them into new logic through graphical operations. The code generation method provided in the embodiments of the present application can generate executable complete logic codes in different programming languages ​​based on code template files in multiple programming languages, which can be used to quickly generate business codes to implement business functions. Optionally, the code generation method provided in the embodiments of the present application can be used to generate a code engine on a low-code platform to improve the ability to quickly build businesses.

[0038] For example, in actual applications, the business capabilities of a platform (such as lotteries, coupon issuance, limited times, etc.) can be logically arranged to generate a DSL for description, and new business logic capability codes can be generated through a code generation engine equipped with the code generation method provided in the embodiment of the present application, thereby quickly realizing the development and deployment of new business capabilities.

[0039] For example, Figure 1 A schematic diagram of an implementation scenario of a code generation method provided in an embodiment of the present application, such as Figure 1 As shown, the implementation scenario of the embodiment of the present application involves a server 1 and a terminal device 2, and the terminal device 2 can communicate data with the server 1 through a communication network.

[0040] Among them, in some possible implementations, the terminal device 2 refers to a type of device that has rich human-computer interaction methods, has the ability to access the Internet, is usually equipped with various operating systems, and has strong processing capabilities. The terminal device 2 includes but is not limited to mobile phones, computers, intelligent voice interaction devices, vehicle-mounted terminals, etc.

[0041] Figure 1 The server 1 in the example may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services. This embodiment of the application does not limit this.

[0042] The code generation method provided in the embodiment of the present application may be executed on a terminal device, may be executed on a server, or may be executed jointly by the terminal device and the server. The above examples should not be construed as limiting the embodiments of the present application.

[0043] In one embodiment, the terminal device 2 obtains the target language text corresponding to the target business, and the target language text is written in a domain-specific language DSL. Optionally, there are two ways to obtain the target language text in this embodiment. One way is that the terminal device 2 responds to the operation of the target object dragging the orchestration node in the visual orchestration interface, and performs a graphical logical orchestration on the orchestration node to generate the target language text corresponding to the target business. Another way is: the terminal device 2 receives the target language text corresponding to the target business input by the target object through the target interface. Then, the terminal device 2 executes the code generation method provided in the embodiment of the present application to generate codes in multiple different programming languages. In this embodiment, the terminal device executes the code generation method provided in the embodiment of the present application.

[0044] In one embodiment, if Figure 1 As shown, the terminal device 2 obtains the target language text corresponding to the target business, and sends the target language text to the server 1, and the server 1 executes the code generation method provided in the embodiment of the present application to generate codes in multiple different programming languages. Similarly, the terminal device 2 also has the above two ways to obtain the target language text.

[0045] The technical solution of the embodiment of the present application will be described in detail below:

[0046] Figure 2 A flowchart of a code generation method provided in an embodiment of the present application. The execution subject of the method may be a code generation device. The execution subject of this embodiment may be a terminal device or a server. Figure 2 As shown, the method may include the following steps:

[0047] S101. Obtain a target language text corresponding to a target business, where the target language text is written using DSL.

[0048] Specifically, DSL refers to a domain-specific language, which is a computer language that focuses on a certain application domain. It is also translated as a domain-specific language. The target business is a business that implements a business capability, such as lottery, coupon issuance, and time limits. Through the idea of ​​domain-specific language, a domain-specific language can be built for the business to be implemented. This language can describe business logic and other information by defining configurations. For example, Figure 3 This is a schematic diagram of the business logic of creating a model after arrangement, such as Figure 3 As shown in the figure, the business logic of creating the model includes creating model parameter check, obtaining model ID, issuing limited configuration, creating large shipment configuration, issuing lottery personalized configuration and issuing basic model information, a total of 6 orchestration nodes. Figure 3 Part of the DSL generated by the graphical arrangement can be:

[0049]

[0050]

[0051] Part of the DSL content above includes name ("Name"), version ("Version"), content ("Comment"), input ("Input"), output ("output"), and orchestration node ("states"), but only the detailed contents of two orchestration nodes (create model parameter check and get model ID) are shown, and the remaining four orchestration nodes are not shown.

[0052] Specifically, the DSL can be in various formats. For example, the DSL shown above uses the Json format to describe the DSL. Optionally, the Yaml format and Xml format can also be used to express the semantics defined in the above Json format, that is, the Yaml format and Xml format can also be used to describe the DSL.

[0053] Optionally, in an implementable manner, obtaining the target language text corresponding to the target business in S101 may specifically be:

[0054] S1011 . In response to the target object dragging an orchestration node in the visual orchestration interface, graphically arrange the orchestration nodes and generate a target language text corresponding to the target business.

[0055] In this mode, the execution subject of S1011 may be a terminal device, which displays a visual orchestration interface including multiple orchestration nodes. The target object may be a user, who may drag the orchestration node. The terminal device responds to the operation by performing graphical logic orchestration on the orchestration node to generate a target language text corresponding to the target business. In this embodiment, DSL may be defined to complete the expression of new functional logic by describing the logical relationships between various orchestration nodes (not limited to service interfaces / functions), such as serial, parallel, branch, loop, error handling, variable mapping, and function call.

[0056] In another practicable manner, obtaining the target language text corresponding to the target business in S101 may specifically be:

[0057] S1011', receiving a target language text corresponding to a target business input by a target object through a target interface. In this method, the target object can manually write a DSL corresponding to the target business, that is, a developer can manually write a DSL corresponding to the target business through a target interface.

[0058] If the code generation method of this embodiment is executed by a server, then obtaining the target language text corresponding to the target service in S101 may specifically be: receiving the target language text corresponding to the target service sent by a terminal device.

[0059] S102: Parse the target language text to obtain metadata corresponding to the target language text.

[0060] Specifically, the target language text obtains the expression of the functional logic of the target business by describing the logical relationship between each orchestration node. Optionally, the target language text is parsed in S102 to obtain metadata corresponding to the target language text, which may be:

[0061] S1021. Parse the target language text according to the DSL orchestration structure to obtain a plurality of process data, entity data and context data included in each process data, wherein the plurality of process data includes data of N orchestration nodes, where N is a positive integer.

[0062] Specifically, the functional logic and data involved in the DSL expression can be parsed to obtain basic metadata. The metadata can mainly include three parts: process data, entity data included in each process data, and context data. The target language text can include multiple process data. For example, if the target business involves N orchestration nodes, the multiple process data can include data of N orchestration nodes. The orchestration structure of the DSL can be the grammatical structure of the DSL. Among them, the data of each orchestration node can include the type and specific data of the orchestration node. The specific data, for example, includes data such as the name of the orchestration node. The type of the orchestration node can be, for example, serial, parallel, branch, loop, error handling, and function call. The entity data included in the process data can also be called entity data embedded in the process data. The scope of the entity data can be the type of different fields, embedded structure, field name, package name involved in the call, function name, input parameter type and formal parameter name. The context data can be data such as field information and reference resource information other than the process data and the entity data of the process data.

[0063] S1022. Combining multiple process data, entity data and context data included in each process data into metadata corresponding to the target language text.

[0064] S103. Generate codes of M different programming languages ​​according to metadata and preset code template files of M different programming languages, where M is a positive integer, and a code template file of a programming language includes a calling structure of the programming language, a grammatical structure of the programming language, and template variables for generating code snippets of the programming language.

[0065] Optionally, the programming language in this embodiment may include, for example, Go language, Java language, Nodejs language, etc., and may also include other programming languages, which is not limited in this embodiment.

[0066] Specifically, a code template file of a programming language includes a main process template and code templates corresponding to different types of orchestration nodes. In an implementable manner, in S103, codes of M different programming languages ​​are generated according to metadata and preset code template files of M different programming languages, which may be:

[0067] S1031. For each of the code template files of M different programming languages, map metadata to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes, and generate a code of the programming language.

[0068] Specifically, code template files of M different programming languages ​​are pre-set (or pre-stored). For each of the M code template files of different programming languages, a code template file of the programming language can be selected from the M code template files in turn, and metadata is mapped to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes to generate a code of the programming language.

[0069] Since the types of orchestration nodes include serial, parallel, branch, loop, error handling and function call, etc., optionally, the code templates corresponding to the types of different orchestration nodes may include at least one of a branch template, a loop template, a parallel template, a serial template, an error handling template, a function call template, a field declaration template and a field assignment template.

[0070] Optionally, in an implementable manner, in S1031, according to the main process template and the code templates corresponding to the types of different orchestration nodes, the metadata is mapped to the code template file of the programming language to generate a code of the programming language, which may be specifically:

[0071] S10311. Traverse the N orchestration nodes in the metadata, determine the type of the currently traversed orchestration node, and determine a code template corresponding to the type of the currently traversed orchestration node from a code template file of a programming language.

[0072] Specifically, the data of the orchestration node includes the type of the orchestration node. By traversing to the current orchestration node, the type of the currently traversed orchestration node can be directly known. Since the code template file of the programming language includes the main process template and code templates corresponding to the types of different orchestration nodes, the code template corresponding to the type of the currently traversed orchestration node can be directly found from the code template file of the programming language.

[0073] S10312. Calculate code template variables according to the context data in the metadata, interpret and execute the code template corresponding to the type of the currently traversed orchestration node, and generate a code snippet of the orchestration node. The code snippet of the orchestration node complies with the syntax of the programming language.

[0074] Specifically, the code template variables are calculated according to the context data in the metadata, and if new variables are calculated, the new variables are used to replace the corresponding variables in the code template.

[0075] Specifically, in an implementable manner, interpreting and executing a code template corresponding to the type of the currently traversed orchestration node may specifically be:

[0076] If it is determined that the code template corresponding to the type of the currently traversed orchestration node does not include a sub-process template, the code template corresponding to the type of the currently traversed orchestration node is interpreted and executed in a preset manner, that is, the code template corresponding to the type of the currently traversed orchestration node is interpreted and executed in a normal process.

[0077] If it is determined that the code template corresponding to the type of the currently traversed orchestration node includes a sub-process template, in the process of interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in a preset manner, the sub-process template is interpreted and executed in a recursive generation manner.

[0078] Specifically, for each orchestration node, such as an orchestration node of type serial, parallel, branch, loop, error handling, and function call, it can be mapped to a sequential code block through the main process template, a parallel execution code block, if else judgment, various error judgment returns or conversions, etc. If the code template corresponding to an orchestration node includes a sub-process template, the sub-process template is interpreted and executed in a recursive generation manner.

[0079] S10323. The N orchestration nodes in the metadata are then traversed until all the N orchestration nodes are traversed to obtain code snippets of the N orchestration nodes.

[0080] S10324. Generate a code in a programming language according to the code snippets of the N orchestration nodes.

[0081] Optionally, in an implementable manner, a code of a programming language is generated according to the code snippets of N arrangement nodes, which may be specifically: the code snippets of N arrangement nodes are combined according to the arrangement structure of the target language text to obtain a code of a programming language.

[0082] Specifically, the code of a programming language finally generated is a code program. The program is expressed as a function. The code generated by the entire process is expressed by a function. For example, the code example generated when the programming language is Go is as follows:

[0083]

[0084]

[0085] Among them, ctx belongs to the context parameter of the go language, and cvar is the structure of the complete set of parameter structures automatically generated by the input and output parameters appearing in the process, which is used for the flow of the entire process parameters. The function contains the code generated by the main process template. The parsing logic of the template itself is decoupled from the language. The content in "{{}}" is the template, and the non-"{{}}" part is replaced by the syntax of the corresponding language. The generated code needs to conform to the syntax of the corresponding programming language, so that it is possible to parse out independent metadata that is independent of the language from a unified DSL, and then map the metadata to a template with a small amount of language syntax to finally generate code.

[0086] For example, when the programming language is Java, the generated code example is as follows:

[0087] / F Execute function

[0088] public static void F(cvar CVar)throws Exception{

[0089] {{-template "flow".Flow}}

[0090] }

[0091] In this embodiment, for Figure 3 The DSL generated by the graphical arrangement, the generated golang language code is as follows:

[0092]

[0093]

[0094] The above code only shows part of the upper-level code structure.

[0095] Optionally, after S103, the method of this embodiment may further include:

[0096] S104. Perform preset processing on the codes in M ​​different programming languages, where the preset processing includes at least one of formatting, pre-compiling and checking.

[0097] Specifically, after generating codes in different programming languages, the generated codes in different programming languages ​​can be processed by preset processing, such as formatting, precompiling and verifying the codes, and the processed codes can be submitted to the code repository after processing. For example, for the code in the golang language, tools such as goimport, gofmt and go build can be used for processing.

[0098] The code generation method provided in this embodiment first obtains the target language text corresponding to the target business written using DSL, then parses the target language text to obtain metadata corresponding to the target language text, and generates codes in M ​​different programming languages ​​based on the metadata and the preset code template files of M different programming languages. By writing the target language text corresponding to the target business using DSL, and parsing independent metadata unrelated to the programming language from the target language text written in DSL, the DSL description removes the restrictions of the project language, uses the preset code template files of different programming languages ​​to fix the basic syntax of various programming languages, and then generates codes in different programming languages ​​based on the metadata combined with the code template files of different programming languages. Thus, codes in multiple different programming languages ​​can be generated based on the target language text corresponding to the target business, thereby improving the universality and efficiency of automatic code generation.

[0099] The following is a detailed description of the code generation method provided in the embodiment of the present application in conjunction with a specific embodiment.

[0100] Figure 4 A flowchart of a code generation method provided in an embodiment of the present application is shown in FIG. Figure 4 As shown, the method may include the following steps:

[0101] S201 . In response to the target object dragging an orchestration node in a visual orchestration interface, graphically arrange the orchestration nodes and generate a target language text corresponding to the target business.

[0102] In this mode, the execution subject may be a terminal device, which displays a visual orchestration interface including multiple orchestration nodes. The target object may be a user, who may drag the orchestration node. The terminal device responds to the operation by performing graphical logic orchestration on the orchestration node to generate a target language text corresponding to the target business. In this embodiment, DSL may be defined to express new functional logic by describing the logical relationships between various orchestration nodes (not limited to service interfaces / functions), such as serial, parallel, branch, loop, error handling, variable mapping, and function call.

[0103] Figure 5 A logic arrangement flow chart when a target business is a lottery provided in an embodiment of the present application is as follows: Figure 5 As shown, in the visual orchestration interface, there are 9 orchestration nodes under the component directory tree. In the flowchart after the graphical logic orchestration, there are 5 serial orchestration nodes, a branch orchestration node, and 4 orchestration nodes that determine whether to execute return (Return) or serial execution based on conditions. Among them, the first 5 serial orchestration nodes are: obtaining user login status information, obtaining application activity information, general obtaining regional information, the remaining number of draws, and taking the first draw ticket. The 4 serial orchestration nodes under the branch orchestration node are: general draw-shipping agreement, draw parameter combination, draw interface, and draw result processing.

[0104] In this embodiment, by Figure 5 The DSL generated by graphically arranging the visual arrangement interface can be:

[0105]

[0106]

[0107] Among them, "states" is an arrangement node. The above target language text shows in detail the content of the first arrangement node and the content of the sixth arrangement node. +Object{…} is the abbreviated content of the arrangement node. Click "+" to expand the detailed content.

[0108] S202: Parse the target language text to obtain metadata corresponding to the target language text.

[0109] Specifically, the target language text obtains the expression of the functional logic of the target business by describing the logical relationship between each orchestration node. Optionally, the target language text is parsed in S202 to obtain metadata corresponding to the target language text, which may be:

[0110] S2021. Parse the target language text according to the orchestration structure of the DSL to obtain a plurality of process data, entity data and context data included in each process data, wherein the plurality of process data includes data of N orchestration nodes, where N is a positive integer.

[0111] Specifically, the functional logic and data involved in the DSL expression can be parsed to obtain basic metadata. The metadata can mainly include three parts: process data, entity data included in each process data, and context data. The target language text can include multiple process data. For example, if the target business involves N orchestration nodes, the multiple process data can include data of N orchestration nodes. The orchestration structure of the DSL can be the grammatical structure of the DSL. Among them, the data of each orchestration node can include the type and specific data of the orchestration node. The specific data, for example, includes data such as the name of the orchestration node. The type of the orchestration node can be, for example, serial, parallel, branch, loop, error handling, and function call. The entity data included in the process data can also be called entity data embedded in the process data. The scope of the entity data can be the type of different fields, embedded structure, field name, package name involved in the call, function name, input parameter type and formal parameter name. The context data can be data such as field information and reference resource information other than the process data and the entity data of the process data.

[0112] S2022. Combining the plurality of process data, the entity data and the context data included in each process data into metadata corresponding to the target language text.

[0113] Specifically, for example, Figure 5 The DSL generated by the visual orchestration interface is graphically arranged. The metadata obtained by parsing the above DSL can be:

[0114]

[0115] Among them, States is the data of multiple orchestration nodes, Cvar, NotCreate bool and *flowintermal

[0116] The context data may be data other than process data and entity data of process data, such as field information and reference resource information.

[0117] S203: Generate codes in M ​​different programming languages ​​according to the metadata and preset code template files in M ​​different programming languages.

[0118] Wherein, M is a positive integer, and a code template file of a programming language includes a calling structure of the programming language, a grammatical structure of the programming language, and template variables for generating code snippets of the programming language.

[0119] Specifically, a code template file of a programming language includes a main process template and code templates corresponding to different types of orchestration nodes. The following uses a code template file of the go language to illustrate a general template structure.

[0120] For example, the main process template has the following structure:

[0121]

[0122]

[0123] For example, the general structure of the code template corresponding to the type of the orchestration node is as follows:

[0124]

[0125]

[0126] For example, the general structure of the code template corresponding to the type of the orchestration node is as follows:

[0127]

[0128]

[0129] For example, if the type of the orchestration node is parallel, the general structure of the code template is as follows:

[0130]

[0131] For example, the code template corresponding to the type of the orchestration node is error handling and has the following general structure:

[0132]

[0133] The language fragment syntax of the above template is mainly Golang. Similarly, to generate other languages, you only need to make a few grammatical adjustments to the template. In addition to the common language logic templates mentioned above, you can define more detailed code generation templates based on the same ideas in the actual situation, such as field declaration, field assignment, function call, etc. templates to complete more detailed code generation in the F function through arrangement.

[0134] In an practicable manner, in S203, codes in M ​​different programming languages ​​are generated according to the metadata and the preset code template files in M ​​different programming languages, which may be specifically:

[0135] S2031. For each of the code template files of M different programming languages, map metadata to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes, and generate a code of the programming language.

[0136] Specifically, code template files of M different programming languages ​​are pre-set (or pre-stored). For each of the M code template files of different programming languages, a code template file of the programming language can be selected from the M code template files in turn, and metadata is mapped to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes to generate a code of the programming language.

[0137] Since the types of orchestration nodes include serial, parallel, branch, loop, error handling and function call, etc., optionally, the code templates corresponding to the types of different orchestration nodes may include at least one of a branch template, a loop template, a parallel template, a serial template, an error handling template, a function call template, a field declaration template and a field assignment template.

[0138] Optionally, in an implementable manner, in S2031, according to the main process template and the code templates corresponding to the types of different orchestration nodes, the metadata is mapped to the code template file of the programming language to generate a code of the programming language, which may be specifically:

[0139] S20311. Traverse the N orchestration nodes in the metadata, determine the type of the currently traversed orchestration node, and determine a code template corresponding to the type of the currently traversed orchestration node from a code template file of a programming language.

[0140] Specifically, the data of the orchestration node includes the type of the orchestration node. By traversing to the current orchestration node, the type of the currently traversed orchestration node can be directly known. Since the code template file of the programming language includes the main process template and code templates corresponding to the types of different orchestration nodes, the code template corresponding to the type of the currently traversed orchestration node can be directly found from the code template file of the programming language.

[0141] S20312. Calculate code template variables according to the context data in the metadata, interpret and execute the code template corresponding to the type of the currently traversed orchestration node, and generate a code snippet of the orchestration node. The code snippet of the orchestration node complies with the syntax of the programming language.

[0142] S20313. The N orchestration nodes in the metadata are then traversed until all the N orchestration nodes are traversed to obtain code snippets of the N orchestration nodes.

[0143] S10324. Generate a code in a programming language according to the code snippets of the N orchestration nodes.

[0144] Optionally, in an implementable manner, a code of a programming language is generated according to the code snippets of N arrangement nodes, which may be specifically: the code snippets of N arrangement nodes are combined according to the arrangement structure of the target language text to obtain a code of a programming language.

[0145] S204: Perform preset processing on the codes in M ​​different programming languages, where the preset processing includes at least one of formatting, pre-compiling and checking.

[0146] Figure 6 A schematic diagram of a main process template interpretation and execution process provided in an embodiment of the present application, such as Figure 6 As shown, the process may specifically include:

[0147] S301, traverse the orchestration nodes.

[0148] S302: Determine the type of the currently traversed orchestration node.

[0149] S303: Determine, from a code template file of a programming language, a code template corresponding to the type of the currently traversed orchestration node.

[0150] S304: Calculate the code template variable according to the context data in the metadata, interpret and execute the code template corresponding to the type of the currently traversed orchestration node, and generate a code snippet of the orchestration node. The code snippet of the orchestration node complies with the syntax of the programming language.

[0151] Specifically, if it is determined that the code template corresponding to the type of the currently traversed orchestration node does not include a sub-process template, the code template corresponding to the type of the currently traversed orchestration node is interpreted and executed in a preset manner. That is, the code template corresponding to the type of the currently traversed orchestration node is interpreted and executed in a normal process. If it is determined that the code template corresponding to the type of the currently traversed orchestration node includes a sub-process template, in the process of interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in a preset manner, the sub-process template is interpreted and executed in a recursive generation manner.

[0152] S305: Determine whether there is any orchestration node.

[0153] If yes, the process returns to S301 , otherwise the process ends.

[0154] Specifically, for each orchestration node, such as an orchestration node of type serial, parallel, branch, loop, error handling, and function call, it can be mapped to a sequential code block through the main process template, a parallel execution code block, if else judgment, various error judgment returns or conversions, etc. If the code template corresponding to an orchestration node includes a sub-process template, the sub-process template is interpreted and executed in a recursive generation manner.

[0155] In this embodiment, optionally, in the code templates corresponding to the main process template and different types of orchestration nodes, the functions related to the programming language used in the definition of the template can be from a built-in reference library, can be orchestrated through another DSL, or can be generated by inputting code snippets, etc. For example, the comparison operator of the branch template can be a system built-in operator library, and the operator reference code can be generated by specifying the DSL, or the operator code can be generated by describing the sub-DSL or code snippets through the DSL.

[0156] The code generation method provided in this embodiment uses DSL to write the target language text corresponding to the target business, and parses independent metadata unrelated to the programming language from the target language text written in DSL. The DSL description removes the limitation of the project language, uses preset code template files of different programming languages ​​to fix the basic syntax of various programming languages, and then generates codes of different programming languages ​​based on the metadata and the code template files of different programming languages. Therefore, codes of multiple different programming languages ​​can be generated based on the target language text corresponding to the target business, thereby improving the universality and efficiency of automatic code generation.

[0157] The code generation method provided in this embodiment realizes the definability of logical processes, high reusability of components, and high code running efficiency, improves the efficiency and universality of automatic code generation, makes template modification more flexible, and does not require re-modification of the compiler and parser. The method of generating code and then running it has higher performance than interpreted execution, and retains the advantages of the native language level itself.

[0158] Figure 7 A schematic diagram of the structure of a code generation device provided in an embodiment of the present application is shown in FIG. Figure 7 As shown, the device may include: an acquisition module 11, a parsing module 12 and a code generation module 13.

[0159] The acquisition module 11 is used to acquire the target language text corresponding to the target business, and the target language text is written in the domain specific language DSL;

[0160] The parsing module 12 is used to parse the target language text to obtain metadata corresponding to the target language text;

[0161] The code generation module 13 is used to generate codes of M different programming languages ​​based on metadata and preset code template files of M different programming languages, where M is a positive integer. A code template file of a programming language includes a calling structure of the programming language, a grammatical structure of the programming language, and template variables for generating code snippets of the programming language.

[0162] In one embodiment, the acquisition module 11 is used to:

[0163] In response to the target object dragging the orchestration node in the visual orchestration interface, the orchestration node is graphically logically arranged to generate a target language text corresponding to the target business; or

[0164] Receive the target language text corresponding to the target business input by the target object through the target interface.

[0165] In one embodiment, the target language text obtains the expression of the functional logic of the target business by describing the logical relationship between each orchestration node, and the parsing module 12 is used to:

[0166] Parse the target language text according to the DSL arrangement structure to obtain a plurality of process data, entity data and context data included in each process data, wherein the plurality of process data includes data of N arrangement nodes, where N is a positive integer;

[0167] The plurality of process data, the entity data and the context data included in each process data are combined into metadata corresponding to the target language text.

[0168] In one embodiment, a code template file of a programming language includes a main process template and code templates corresponding to different types of orchestration nodes, and the code generation module 13 is used to:

[0169] For each of the code template files of the M different programming languages, the metadata is mapped to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes, and a code of the programming language is generated.

[0170] In one embodiment, the code generation module 13 is specifically used for:

[0171] Traversing N orchestration nodes in the metadata, determining the type of the currently traversed orchestration node, and determining a code template corresponding to the type of the currently traversed orchestration node from a code template file of a programming language;

[0172] Calculate the code template variables according to the context data in the metadata, interpret and execute the code template corresponding to the type of the currently traversed orchestration node, and generate a code snippet of the orchestration node. The code snippet of the orchestration node complies with the syntax of the programming language.

[0173] Then, the N orchestration nodes in the metadata are traversed until all the N orchestration nodes are traversed to obtain code snippets of the N orchestration nodes;

[0174] Generate code in a programming language according to the code snippets of N orchestration nodes.

[0175] In one embodiment, the code generation module 13 is specifically used for:

[0176] If it is determined that the code template corresponding to the type of the currently traversed orchestration node does not include a sub-process template, interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in a preset manner;

[0177] If it is determined that the code template corresponding to the type of the currently traversed orchestration node includes a sub-process template, in the process of interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in a preset manner, the sub-process template is interpreted and executed in a recursive generation manner.

[0178] In one embodiment, the code generation module 13 is specifically used for:

[0179] The code fragments of N arrangement nodes are combined according to the arrangement structure of the target language text to obtain a code in a programming language.

[0180] In one embodiment, the code templates corresponding to the types of different orchestration nodes include at least one of a branch template, a loop template, a parallel template, a serial template, an error handling template, a function call template, a field declaration template, and a field assignment template.

[0181] In one embodiment, the code generation module 13 is further used to:

[0182] Preset processing is performed on codes in M ​​different programming languages, where the preset processing includes at least one of formatting, precompiling, and checking.

[0183] It should be understood that the device embodiment and the method embodiment may correspond to each other, and similar descriptions may refer to the method embodiment. To avoid repetition, they will not be described here. Specifically, Figure 7 The code generating apparatus shown can execute the method embodiment corresponding to the code generating device, and the aforementioned and other operations and / or functions of each module in the apparatus are respectively for implementing the method embodiment corresponding to the code generating device, which will not be described in detail here for the sake of brevity.

[0184] The code generation device of the embodiment of the present application is described above from the perspective of the functional module in conjunction with the accompanying drawings. It should be understood that the functional module can be implemented in hardware form, can be implemented by instructions in software form, and can also be implemented by a combination of hardware and software modules. Specifically, the steps of the method embodiment in the embodiment of the present application can be completed by the hardware integrated logic circuit and / or software form instructions in the processor, and the steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to perform, or a combination of hardware and software modules in the decoding processor to perform. Optionally, the software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, an electrically erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor reads the information in the memory, and completes the steps in the above method embodiment in conjunction with its hardware.

[0185] Figure 8 It is a schematic block diagram of a code generation device provided in an embodiment of the present application.

[0186] like Figure 8 As shown, the code generating device may include:

[0187] The memory 310 and the processor 320, the memory 310 is used to store the computer program and transmit the program code to the processor 320. In other words, the processor 320 can call and run the computer program from the memory 310 to implement the method in the embodiment of the present application.

[0188] For example, the processor 320 may be configured to execute the above method embodiments according to instructions in the computer program.

[0189] In some embodiments of the present application, the processor 320 may include but is not limited to:

[0190] General-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic device, discrete hardware components, etc.

[0191] In some embodiments of the present application, the memory 310 includes but is not limited to:

[0192] Volatile memory and / or non-volatile memory. Among them, the non-volatile memory can be read-only memory (ROM), programmable ROM (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM) or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct RAM bus random access memory (DR RAM).

[0193] In some embodiments of the present application, the computer program may be divided into one or more modules, which are stored in the memory 310 and executed by the processor 320 to complete the method provided by the present application. The one or more modules may be a series of computer program instruction segments capable of completing specific functions, and the instruction segments are used to describe the execution process of the computer program in the code generation device.

[0194] like Figure 8 As shown, the code generating device may also include:

[0195] The transceiver 330 may be connected to the processor 320 or the memory 310 .

[0196] The processor 320 may control the transceiver 330 to communicate with other devices, specifically, to send information or data to other devices, or to receive information or data sent by other devices. The transceiver 330 may include a transmitter and a receiver. The transceiver 330 may further include an antenna, and the number of antennas may be one or more.

[0197] It should be understood that the various components in the code generating device are connected via a bus system, wherein the bus system includes not only a data bus but also a power bus, a control bus and a status signal bus.

[0198] The embodiment of the present application also provides a computer storage medium on which a computer program is stored, and when the computer program is executed by a computer, the computer can perform the method of the above method embodiment. In other words, the embodiment of the present application also provides a computer program product containing instructions, and when the instructions are executed by a computer, the computer can perform the method of the above method embodiment.

[0199] When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integration. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a digital video disc (digital video disc, DVD)), or a semiconductor medium (e.g., a solid state drive (solid state disk, SSD)), etc.

[0200] Those of ordinary skill in the art will appreciate that the modules and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present application.

[0201] In the several embodiments provided in the embodiments of the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the module is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.

[0202] The modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. For example, each functional module in each embodiment of the present application may be integrated into a processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.

[0203] The above contents are only specific implementation methods of the embodiments of the present application, but the protection scope of the embodiments of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or replacements within the technical scope disclosed in the embodiments of the present application, which should be included in the protection scope of the embodiments of the present application. Therefore, the protection scope of the embodiments of the present application should be based on the protection scope of the claims.

Claims

1. A code generation method, characterized in that: include: Obtaining a target language text corresponding to a target business, where the target language text is written in a domain specific language (DSL); Parsing the target language text to obtain metadata corresponding to the target language text; According to the metadata and preset code template files of M different programming languages, codes of the M different programming languages ​​are generated, where M is a positive integer, and a code template file of a programming language includes a calling structure of the programming language, a grammatical structure of the programming language, and template variables for generating code snippets of the programming language.

2. The method according to claim 1, characterized in that The step of obtaining the target language text corresponding to the target service includes: In response to the target object dragging the arrangement node in the visual arrangement interface, the arrangement node is graphically arranged logically to generate the target language text corresponding to the target business; or A target language text corresponding to the target business input by the target object through the target interface is received.

3. The method according to claim 1, characterized in that The target language text obtains the expression of the functional logic of the target business by describing the logical relationship between each orchestration node, and the target language text is parsed to obtain metadata corresponding to the target language text, including: Parsing the target language text according to the DSL orchestration structure to obtain a plurality of process data, entity data and context data included in each process data, wherein the plurality of process data includes data of N orchestration nodes, where N is a positive integer; The plurality of process data, the entity data included in each process data and the context data are combined into metadata corresponding to the target language text.

4. The method according to claim 1, characterized in that The code template file of one programming language includes a main process template and code templates corresponding to different types of orchestration nodes, and the generating of the codes of the M different programming languages ​​according to the metadata and the preset code template files of the M different programming languages ​​includes: For each of the code template files of the M different programming languages, the metadata is mapped to the code template file of the programming language according to the main process template and the code templates corresponding to the types of different orchestration nodes, and a code of the programming language is generated.

5. The method according to claim 4, characterized in that The step of mapping the metadata to a code template file of the programming language according to the main process template and code templates corresponding to different types of orchestration nodes to generate a code of the programming language includes: Traversing the N orchestration nodes in the metadata, determining the type of the currently traversed orchestration node, and determining a code template corresponding to the type of the currently traversed orchestration node from a code template file of the programming language; Calculate code template variables according to the context data in the metadata, interpret and execute a code template corresponding to the type of the currently traversed orchestration node, and generate a code snippet of the orchestration node, wherein the code snippet of the orchestration node complies with the syntax of the programming language; Then, the N orchestration nodes in the metadata are traversed until all the N orchestration nodes are traversed to obtain code snippets of the N orchestration nodes; A code in a programming language is generated according to the code snippets of the N orchestration nodes.

6. The method according to claim 5, characterized in that The code template corresponding to the type of the currently traversed orchestration node is interpreted and executed, including: If it is determined that the code template corresponding to the type of the currently traversed orchestration node does not include a sub-process template, interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in a preset manner; If it is determined that the code template corresponding to the type of the currently traversed orchestration node includes a sub-process template, in the process of interpreting and executing the code template corresponding to the type of the currently traversed orchestration node in the preset manner, the sub-process template is interpreted and executed in a recursive generation manner.

7. The method according to claim 5, characterized in that The step of generating a code in a programming language according to the code snippets of the N orchestration nodes comprises: The code fragments of the N arrangement nodes are combined according to the arrangement structure of the target language text to obtain the code of the one programming language.

8. The method according to claim 4, characterized in that The code templates corresponding to the types of different orchestration nodes include at least one of a branch template, a loop template, a parallel template, a serial template, an error handling template, a function call template, a field declaration template, and a field assignment template.

9. The method according to any one of claims 1 to 8, characterized in that: The method further comprises: The codes in the M different programming languages ​​are subjected to preset processing, wherein the preset processing includes at least one of formatting, pre-compiling and checking.

10. A code generating device, characterized in that: include: An acquisition module, used to acquire a target language text corresponding to a target business, wherein the target language text is written in a domain specific language DSL; A parsing module, used to parse the target language text to obtain metadata corresponding to the target language text; A code generation module is used to generate codes for M different programming languages ​​based on the metadata and preset code template files for M different programming languages, where M is a positive integer and a code template file for a programming language includes a calling structure of the programming language, a grammatical structure of the programming language and template variables for generating code snippets for the programming language.

11. A code generating device, characterized in that: include: A processor and a memory, the memory being used to store a computer program, and the processor being used to call and run the computer program stored in the memory to execute the method according to any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that: The invention comprises instructions which, when executed on a computer program, cause the computer to perform the method as claimed in any one of claims 1 to 9.

13. A computer program product comprising instructions, characterized in that When the instructions are executed on a computer, the computer is caused to execute the method according to any one of claims 1 to 9.

Citation Information

Cited By

  • Method and device for rendering HTML (Hypertext Markup Language) code of Web page and computer program product

    CN119474576A

  • A method for interface generation based on domain specific language

    CN119739381A

  • Method and device for generating programming language according to clinical DVP table

    CN120929062A