CN110083351B - Method and device for generating code - Google Patents

Method and device for generating code Download PDF

Info

Publication number
CN110083351B
CN110083351B CN201910325412.5A CN201910325412A CN110083351B CN 110083351 B CN110083351 B CN 110083351B CN 201910325412 A CN201910325412 A CN 201910325412A CN 110083351 B CN110083351 B CN 110083351B
Authority
CN
China
Prior art keywords
code
vertex
target
flow chart
template
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.)
Active
Application number
CN201910325412.5A
Other languages
Chinese (zh)
Other versions
CN110083351A (en
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.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology 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 Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN201910325412.5A priority Critical patent/CN110083351B/en
Publication of CN110083351A publication Critical patent/CN110083351A/en
Application granted granted Critical
Publication of CN110083351B publication Critical patent/CN110083351B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/35Creation or generation of source code model driven

Landscapes

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

Abstract

The embodiment of the application discloses a method and a device for generating codes. One embodiment of the method comprises the following steps: in response to receiving a request to generate a code from a target flow chart, determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flow chart matches an input parameter sequence of an end point of the line; in response to the determination, performing topological sorting on the target flow chart to obtain a vertex sequence after the topological sorting of the target flow chart; creating an empty code segment, generating code content corresponding to each vertex in the vertex sequence according to the sequence of the vertex from front to back in the vertex sequence, and adding the generated code content to the end of the code segment; the code segment is determined to be the code corresponding to the request to generate the code according to the target flowchart. This embodiment enables the generation of code according to a flow chart.

Description

Method and device for generating code
Technical Field
The embodiment of the application relates to the technical field of computers, in particular to a method and a device for generating codes.
Background
At present, when codes of various applications are compiled, debugging and testing are performed after codes are compiled manually, and finally, the applications are deployed after the testing is passed. The method is complex in operation, long in development period and low in development efficiency, various errors are easy to occur in manual writing, and the accuracy of the generated codes is affected.
Disclosure of Invention
The embodiment of the application provides a method and a device for generating codes.
In a first aspect, embodiments of the present application provide a method for generating a code, the method comprising: in response to receiving a request to generate a code according to a target flow chart, determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flow chart matches an input parameter sequence of an end point of the line, wherein a vertex of the target flow chart comprises a vertex graph and vertex information, and the vertex information comprises a vertex identifier, a code template identifier, at least one attribute parameter, an input parameter sequence and an output parameter sequence; in response to the determination, performing topological sorting on the target flow chart to obtain a vertex sequence after the topological sorting of the target flow chart; creating an empty code segment, generating code content corresponding to each vertex in the vertex sequence according to the sequence of the vertex from front to back in the vertex sequence, and adding the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by the following steps: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertexes positioned upstream of the vertex in a vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex; the code segment is determined to be the code corresponding to the request to generate the code according to the target flowchart.
In some embodiments, before creating an empty code segment and for each vertex in the sequence of vertices, generating code content corresponding to the vertex in the sequence of vertices in the order of front to back of the vertex, and adding the generated code content to the end of the code segment, the method further comprises: generating an object code template identification set by using different code template identifications in vertex information of each vertex in the vertex sequence; and loading the code template indicated by each code template identifier in the target code template identifier set into a cache.
In some embodiments, before determining whether the target flow diagram is a directed acyclic graph and whether the output parameter sequence of the start of each line in the target flow diagram matches the input parameter sequence of the end of the line in response to receiving a request to generate a code from the target flow diagram, the method further comprises: in response to detecting a selection operation of a user on a vertex icon in a vertex icon set presented in a current interface, determining an upstream vertex icon and a downstream vertex icon of a target vertex icon in a flow chart presented in the current interface, wherein the target vertex icon is the vertex icon aimed at by the selection operation, and the vertex icon is associated with a code template identifier; in response to determining that the input parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template identification associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template identification associated with the downstream vertex icon, adding the target vertex icon as a new vertex to the flow chart presented at the current interface; and determining the flow chart presented by the current interface as a target flow chart.
In some embodiments, prior to determining the code segment as code corresponding to the request to generate code according to the target flowchart, the method further comprises: and deleting repeated code sentences used for referencing the same library file in the code section, and reserving one code sentence used for referencing the same library file.
In some embodiments, prior to determining the code segment as code corresponding to the request to generate code according to the target flowchart, the method further comprises: and deleting repeated code sentences for declaring the same function in the code segment, and reserving one code sentence for declaring the same function.
In some embodiments, prior to determining the code segment as code corresponding to the request to generate code according to the target flowchart, the method further comprises: searching a code sub-section matched with a preset execution time identifier in the code section; for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving the code sub-segment which is matched with the code sub-segment in the determined execution times, and deleting other code sub-segments which are matched with the code sub-segment in the code segment.
In some embodiments, prior to determining the code segment as code corresponding to the request to generate code according to the target flowchart, the method further comprises: and replacing the preset resource application identifier in the code segment with a corresponding resource application code statement.
In some embodiments, prior to determining the code segment as code corresponding to the request to generate code according to the target flowchart, the method further comprises: and replacing the preset resource release identifier in the code segment with a corresponding resource release code statement.
In some embodiments, generating code content corresponding to the vertex further comprises: before a first log generation code statement is added to the generated code content as a first sentence code statement of the code content corresponding to the vertex, and after a second log generation code statement is added to the generated code content as a last sentence code statement of the code content corresponding to the vertex, wherein the first log generation code statement is used for indicating that a first log is generated, the first log is used for indicating the time when the code indicated by the code template identification of the vertex starts executing, the first log generation code statement is used for indicating that a second log is generated, and the second log is used for indicating the time when the code indicated by the code template identification of the vertex ends executing.
In some embodiments, the method further comprises: code files corresponding to the requests to generate code according to the target flow chart are generated and stored to the disk.
In some embodiments, the method further comprises: searching a library file dependency statement in a code corresponding to a request for generating the code according to the target flowchart; and for each searched library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file into a folder in which the code file is stored.
In some embodiments, generating and storing code files to disk corresponding to requests to generate code according to a target flow chart includes: generating at least one code file from codes corresponding to the requests for generating codes according to the target flow chart, wherein the number of lines of code sentences of each generated code file is within a preset line number range; the generated at least one code file is stored to a disk.
In a second aspect, embodiments of the present application provide an apparatus for generating a code, the apparatus comprising: a first determining unit configured to determine, in response to receiving a request to generate a code according to a target flowchart, whether the target flowchart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flowchart matches an input parameter sequence of an end point of the line, wherein a vertex of the target flowchart includes a vertex graph and vertex information, the vertex information includes a vertex identification, a code template identification, at least one attribute parameter, the input parameter sequence, and the output parameter sequence; the sequencing unit is configured to respond to the determination that the target flow chart is topologically sequenced, and a vertex sequence of the target flow chart after the topological sequencing is obtained; a code generating unit configured to create an empty code segment, and for each vertex in the vertex sequence, generating code content corresponding to the vertex in the order of the vertex from front to back in the vertex sequence, and adding the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertexes positioned upstream of the vertex in a vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex; and a second determination unit configured to determine the code segment as a code corresponding to a request to generate the code according to the target flowchart.
In some embodiments, the apparatus further comprises a code template loading unit configured to: generating code content corresponding to each vertex in the vertex sequence according to the sequence from front to back of the vertex in the vertex sequence, and generating an object code template identification set by using different code template identifications in vertex information of each vertex in the vertex sequence before adding the generated code content to the end of the code segment; and loading the code templates indicated by each code template identifier in the set of target code template identifiers into a cache.
In some embodiments, the apparatus further comprises a target flowchart determining unit configured to: before determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a starting point of each line in the target flow chart is matched with an input parameter sequence of an ending point of the line in response to receiving a request for generating codes according to the target flow chart, and determining an upstream vertex icon and a downstream vertex icon of the target vertex icon in the flow chart presented by a current interface in response to detecting a user selection operation of the vertex icons in the vertex icon set presented by the current interface, wherein the target vertex icons are vertex icons aimed by the selection operation, and the vertex icons are associated with code template identifications; and in response to determining that the input parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template identification associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template identification associated with the downstream vertex icon, adding the target vertex icon as a new vertex to the flow chart presented at the current interface; and determining the flow chart presented by the current interface as a target flow chart.
In some embodiments, the apparatus further comprises a first repetition code statement deletion unit configured to: the repeated code statement in the code segment for referencing the same library file is deleted and a code statement for referencing the same library file is retained prior to determining the code segment as a code corresponding to the request to generate code according to the target flowchart.
In some embodiments, the apparatus further comprises a second repetition code statement deletion unit configured to: the repeated code statement for declaring the same function in the code segment is deleted and a code statement for declaring the same function is retained before the code segment is determined to be the code corresponding to the request for generating the code according to the target flowchart.
In some embodiments, the apparatus further comprises a code sub-segment deletion unit configured to: searching a code sub-section matched with a preset execution time identifier in the code section before determining the code section as the code corresponding to the request for generating the code according to the target flow chart; and for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving the code sub-segments, matched with the code sub-segment, of the determined execution times in the code segment, and deleting other code sub-segments, matched with the code sub-segment, in the code segment.
In some embodiments, the apparatus further comprises a resource application unit configured to: before determining the code segment as a code corresponding to a request to generate a code according to the target flowchart, replacing a preset resource application identifier in the code segment with a corresponding resource application code statement.
In some embodiments, the apparatus further comprises a resource release unit configured to: before determining the code segment as a code corresponding to a request to generate code according to the target flowchart, replacing a preset resource release identifier in the code segment with a corresponding resource release code statement.
In some embodiments, generating code content corresponding to the vertex further comprises: before a first log generating code statement is added to the generated code content as a first sentence of the code content corresponding to the vertex, and after a second log generating code statement is added to the generated code content as a last sentence of the code content corresponding to the vertex, wherein the first log generating code statement is used for indicating that a first log is generated, the first log is used for indicating that a code template of the vertex indicates that the indicated code starts executing, the first log generating code statement is used for indicating that a second log is generated, and the second log is used for indicating that the code template of the vertex indicates that the indicated code execution ends.
In some embodiments, the apparatus further comprises a code file generation unit configured to: code files corresponding to the requests to generate code according to the target flow chart are generated and stored to the disk.
In some embodiments, the apparatus further comprises a library file copy storage unit configured to: searching a library file dependency statement in a code corresponding to a request for generating the code according to the target flowchart; and for each searched library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file into a folder in which the code file is stored.
In some embodiments, the code file generating unit is further configured to: generating at least one code file from codes corresponding to the requests for generating codes according to the target flow chart, wherein the number of lines of code sentences of each generated code file is within a preset line number range; and storing the generated at least one code file to a disk.
In a third aspect, an embodiment of the present application provides an electronic device, including: one or more processors; and a storage device having one or more programs stored thereon, which when executed by the one or more processors, cause the one or more processors to implement the method as described in any of the implementations of the first aspect.
In a fourth aspect, embodiments of the present application provide a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by one or more processors, implements a method as described in any of the implementations of the first aspect.
Currently, when generating codes, manual code writing or automatic code generation is realized by replacing preset variables in a simple template. In the method and the device for generating the code provided by the embodiments of the present application, whether the target flowchart is a directed acyclic graph and whether the output parameter sequence of the start point of each line in the target flowchart matches the input parameter sequence of the end point of the line are determined by receiving a request for generating the code according to the target flowchart, where the vertex of the target flowchart includes a vertex identifier, a code template identifier, at least one attribute parameter, an input parameter sequence, and an output parameter sequence. And if so, performing topological sorting on the target flow chart to obtain a vertex sequence after the topological sorting of the target flow chart. Reconstructing an empty code segment, generating code content corresponding to each vertex in the vertex sequence from front to back in the vertex sequence for each vertex, and adding the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by: and replacing placeholders corresponding to the input parameters in the code template content indicated by the code template identification of the vertex with output parameters in the corresponding code content of the adjacent vertex positioned at the upstream of the vertex in the vertex sequence, and replacing the placeholders corresponding to the attribute parameters with the corresponding attribute parameters of the vertex to obtain the code content corresponding to the vertex. Finally, the code segment is determined as the generated code. Therefore, the code template prepared in advance can be utilized to automatically generate the code according to the flow chart, the development cost is reduced, a developer can generate the corresponding code only by giving the flow chart, and more complex logic can be realized. For example, training processes and online use processes of various deep learning models in deep learning, etc. can be implemented.
Drawings
Other features, objects and advantages of the present application will become more apparent upon reading of the detailed description of non-limiting embodiments, made with reference to the following drawings, in which:
FIG. 1 is an exemplary system architecture diagram in which an embodiment of the present application may be applied;
FIG. 2 is a flow chart of one embodiment of a method for generating code according to the present application;
FIG. 3A is a schematic diagram of one application scenario of a method for generating code according to the present application;
FIG. 3B is a schematic diagram of the directed acyclic graph according to the embodiment of FIG. 3A of the present application;
FIG. 3C is a schematic diagram of a code segment generation process in accordance with the embodiment of FIG. 3A of the present application;
FIG. 4 is a flow chart of yet another embodiment of a method for generating code according to the present application;
FIG. 5 is a schematic structural diagram of one embodiment of an apparatus for generating code according to the present application;
fig. 6 is a schematic diagram of a computer system suitable for use in implementing embodiments of the present application.
Detailed Description
The present application is described in further detail below with reference to the drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the invention and are not limiting of the invention. It should be noted that, for convenience of description, only the portions related to the present invention are shown in the drawings.
It should be noted that, in the case of no conflict, the embodiments and features in the embodiments may be combined with each other. The present application will be described in detail below with reference to the accompanying drawings in conjunction with embodiments.
FIG. 1 illustrates an exemplary system architecture 100 to which embodiments of the methods for generating code or apparatus for generating code of the present application may be applied.
As shown in fig. 1, a system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 is used as a medium to provide communication links between the terminal devices 101, 102, 103 and the server 105. The network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, among others.
The user may interact with the server 105 via the network 104 using the terminal devices 101, 102, 103 to receive or send messages or the like. Various communication client applications, such as a code generation class application, a web browser application, a shopping class application, a search class application, an instant messaging tool, a mailbox client, social platform software, etc., may be installed on the terminal devices 101, 102, 103.
The terminal devices 101, 102, 103 may be hardware or software. When the terminal devices 101, 102, 103 are hardware, they may be various electronic devices with display screens, including but not limited to smartphones, tablet computers, electronic book readers, MP3 players (Moving Picture Experts Group Audio Layer III, dynamic video expert compression standard audio plane 3), MP4 (Moving Picture Experts Group Audio Layer IV, dynamic video expert compression standard audio plane 4) players, laptop and desktop computers, and the like. When the terminal devices 101, 102, 103 are software, they can be installed in the above-listed electronic devices. Which may be implemented as a plurality of software or software modules, or as a single software or software module. The present invention is not particularly limited herein.
The server 105 may be a server providing various services, such as a background code generation server providing support for flowcharts displayed on the terminal devices 101, 102, 103. The background code generation server may perform analysis or the like on the received data such as the request for generating the code according to the flowchart, and feed back the processing result (e.g., the generated code) to the terminal device.
It should be noted that, the method for generating a code provided in the embodiments of the present application is generally performed by the server 105, and accordingly, the device for generating a code is generally disposed in the server 105.
The server 105 may be hardware or software. When the server 105 is hardware, it may be implemented as a distributed server cluster formed by a plurality of servers, or as a single server. When the server 105 is software, it may be implemented as a plurality of software or software modules (for example, to provide a code generation service), or may be implemented as a single software or software module, which is not particularly limited herein.
It should be noted that the server 105 may also receive locally a request to generate code according to the target flow chart and ultimately generate code, in which case the exemplary system architecture 100 may not include the terminal devices 101, 102, 103 and the network 104.
It should also be noted that the request for generating the code according to the target flow chart may also be received locally in the terminal device 101, 102, 103, in which case the method for generating the code may also be performed by the terminal device 101, 102, 103, and correspondingly the means for generating the code may also be provided in the terminal device 101, 102, 103. At this point, the exemplary system architecture 100 may not include the server 105 and the network 104.
It should be understood that the number of terminal devices, networks and servers in fig. 1 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
With continued reference to FIG. 2, a flow 200 of one embodiment of a method for generating code according to the present application is shown. The method for generating codes comprises the following steps:
in response to receiving a request to generate a code from a target flow diagram, a determination is made as to whether the target flow diagram is a directed acyclic diagram and whether the output parameter sequence at the beginning of each line in the target flow diagram matches the input parameter sequence at the end of the line 201.
In this embodiment, an execution subject (e.g., a server shown in fig. 1) of the method for generating a code may first receive, locally or remotely, a request for generating a code according to a target flowchart from another electronic device connected to the execution subject network, and then determine, if the target flowchart is a directed acyclic graph and if an output parameter sequence of a start point of each line in the target flowchart matches an input parameter sequence of an end point of the line, if the request is received.
Here, the target flow graph may be composed of at least one vertex and at least one connection between two vertices. Wherein each vertex may include a vertex graph and vertex information, which may include a vertex identification, a code template identification, at least one attribute parameter, an input parameter sequence, and an output parameter sequence. Here, the vertex pattern of the vertex may be various patterns, for example, may be rectangular, circular, elliptical, and other various shapes, which are not particularly limited in this application.
Vertex identifications in the vertex information are used to uniquely indicate the respective vertices in the target flow graph.
The code templates in the vertex information identify which code template the vertex corresponds to. In practice, at least one code template may be preset according to actual service requirements, each code template is specified with an input parameter sequence, an output parameter sequence and an attribute parameter sequence required by the code template, and the code template includes codes required by executing data processing operations. The input parameter sequence needs to specify the data type of each input parameter, the output parameter sequence needs to specify the data type of each output parameter, and the attribute parameter sequence needs to specify the data type of each attribute parameter. It should be noted that, here, the input parameter may be used to indicate data to be processed by the data processing logic corresponding to the code template, the attribute parameter may be used to indicate an operation parameter of the data processing logic corresponding to the code template to perform an operation, and the output parameter may be used to indicate result data obtained after the data processing logic corresponding to the code template is performed.
Here, the execution body described above may employ various implementations to determine whether the target flow diagram is a directed acyclic graph (Directed Acyclic Graph, DAG). It should be noted that how to determine whether the graph is a directed acyclic graph is a prior art widely studied and applied at present, and will not be described herein.
Here, determining whether the output parameter sequence of the start point of each line in the target flowchart matches the input parameter sequence of the end point of the line may specifically include: for each line in the target flowchart, it is determined whether the data type of each output parameter in the output parameter sequence in the vertex information of the start point of the line is the same as the data type of the corresponding position input parameter in the input parameter sequence in the vertex information of the end point of the line. If the determinations are all the same, it may be determined that the output parameter sequence of the start of the line matches the input parameter sequence of the end of the line.
Step 202, in response to determining that the target flow chart is topologically ordered, obtaining a vertex sequence after the target flow chart is topologically ordered.
In this embodiment, the execution body may perform topology sequencing on the target flowchart to obtain the vertex sequence after topology sequencing of the target flowchart when it is determined in step 201 that the target flowchart is a directed acyclic graph and the output parameter sequence of the start point of each line in the target flowchart is matched with the input parameter sequence of the end point of the line.
It should be noted that, how to topologically order the directed acyclic graph is a well-known technology widely studied and applied at present, and will not be described herein.
Step 203, creating an empty code segment, and for each vertex in the vertex sequence, generating code content corresponding to the vertex in the front-to-back order of the vertex in the vertex sequence, and adding the generated code content to the end of the code segment.
In this embodiment, after obtaining the vertex sequence of the topological ordering of the target flowchart, the execution body may first create an empty code segment. Then, for each vertex in the above-described vertex sequence, code content corresponding to the vertex is generated in the order of the vertex from front to back in the vertex sequence, and the generated code content is added to the end of the code segment.
Here, the code content corresponding to the vertex may be obtained by:
first, code template content indicated by the code template identification in vertex information of the vertex is acquired.
And secondly, replacing the placeholder corresponding to the input parameter in the obtained code template content with the output parameter in the corresponding code content of the adjacent vertex before the vertex in the vertex sequence.
And then, replacing placeholders corresponding to the attribute parameters in the obtained code template content with the corresponding attribute parameters in the vertex information of the vertex.
And finally, determining the obtained code template content after the replacing operation as the code content corresponding to the vertex.
In some optional implementations of the present embodiment, after generating the code content corresponding to the vertex, a first log generation code statement may be further added to the generated code content before the first sentence code statement of the code content corresponding to the vertex is added to the generated code content, and a second log generation code statement may be added to the generated code content after the last sentence code statement of the code content corresponding to the vertex, where the first log generation code statement is used to indicate generation of a first log indicating a time when the code indicated by the code template identifier of the vertex starts executing, the first log generation code statement is used to indicate generation of a second log indicating a time when the code indicated by the code template identifier of the vertex ends executing. Thus, the time when the code content corresponding to the vertex in the target flow chart starts to execute and ends to execute can be known through the first log and the second log.
In some optional implementations of this embodiment, the foregoing execution body may execute the following steps 205 and 206 before step 203:
step 205, generating a set of target code template identifiers by using different code template identifiers in vertex information of each vertex in the vertex sequence.
Step 206, loading the code template indicated by each code template identifier in the target code template identifier set into the cache.
Through step 205 and step 206, the code templates corresponding to the vertices in the target flowchart are loaded into the cache, and then in the process of executing step 203, the code template content indicated by the code template identifier in the vertex information of each vertex in the target flowchart can be obtained from the cache, so as to increase the execution speed.
In step 204, the code segment is determined to be the code corresponding to the request to generate the code according to the target flowchart.
In this embodiment, the execution body may determine a code segment obtained by adding the code content corresponding to each vertex in the vertex sequence to the empty code segment in step 203 as a code corresponding to the request for generating a code according to the target flowchart received in step 201.
With continued reference to fig. 3A, fig. 3A is a schematic diagram of an application scenario of the method for generating a code according to the present embodiment. In the application scenario of fig. 3A, the server 301 may first receive a request 303 to generate a code according to the target flowchart 302, and then determine that the target flowchart 302 is a directed acyclic graph and that the output parameter sequence of the start of each line in the target flowchart 302 matches the input parameter sequence of the end of the line. Referring specifically to fig. 3B, fig. 3B is a schematic diagram of the directed acyclic graph 302 according to an embodiment of fig. 3A of the present application. As shown in fig. 3B, the directed acyclic graph 302 includes a vertex P1 to a vertex P4, and a connection a from the vertex P1 to the vertex P3, a connection B from the vertex P2 to the vertex P3, and a connection C from the vertex P3 to the vertex P4. The server 301 may then topologically order the target flow graph 302 to obtain a sequence of vertices 304 topologically ordered by the target flow graph 302. The resulting vertex sequence 304 for the directed acyclic graph shown in FIG. 3B is { P1, P2, P3, P4}. Thereafter, the server 301 may create an empty code segment 305 and, for each vertex in the vertex sequence 304, generate code content 305 corresponding to that vertex in the order of the vertex from front to back in the vertex sequence 304, and add the generated code content to the end of the code segment 305. Referring specifically to fig. 3C, fig. 3C is a schematic diagram illustrating a code segment 305 generation process according to the embodiment shown in fig. 3A of the present application. As can be seen from fig. 3C, the code segment 305 is composed of code content corresponding to each vertex in the vertex sequence 304{ P1, P2, P3, P4}, and since P1 and P2 have no upstream nodes, the code content corresponding to P1 is the real corresponding attribute parameter "real-para1" of replacing the placeholder "para1" corresponding to the attribute parameter in the code template content indicated by the code template identification of P1 with the real corresponding attribute parameter "real-para1" of P1, and the code content corresponding to P2 is the real corresponding attribute parameter "real-para2" of replacing the placeholder "para2" corresponding to the attribute parameter in the code template content indicated by the code template identification of P2 with the code template identification of P2. And the upstream node of P3 includes P1 and P2, so that the code content corresponding to P3 is to replace the placeholder "input3" corresponding to the input parameter in the code template content indicated by the code template identifier of P3 with the output parameters "output1" and "output2" of the upstream neighboring nodes P1 and P2 of P3, and replace the placeholder "para3" corresponding to the attribute parameter with the real corresponding attribute parameter "real_para3" of P3. The upstream node of P4 is P3, so that the code content corresponding to P4 is to replace the placeholder "input4" corresponding to the input parameter in the code template content indicated by the code template identifier of P4 with the output parameter "output3" of the upstream neighboring node P3 of P4, and replace the placeholder "para4" corresponding to the attribute parameter with the real corresponding attribute parameter "real_para4" of P4. Finally, the server 301 may determine the code segment 305 as a code corresponding to the request 303 to generate the code according to the target flow chart. The method provided by the embodiment of the application automatically generates the code by utilizing the flow chart under the condition that the input parameter sequence of the starting point of each line in the flow chart in the form of the directed acyclic graph is matched with the output parameter sequence of the line, thereby reducing the development cost, improving the code generation speed and improving the code accuracy.
With further reference to fig. 4, a flow 400 of yet another embodiment of a method for generating code is shown. The flow 400 of the method for generating code comprises the steps of:
in step 401, in response to detecting a user selection operation of a vertex icon in a vertex icon set presented in the current interface, an upstream vertex icon and a downstream vertex icon of a target vertex icon in a flowchart presented by the current interface are determined.
In this embodiment, an execution body (e.g., a server shown in fig. 1) of the method for generating a code may be presented to the vertex icon set in the current interface, so that the execution body may determine, when detecting a user's selection operation of a vertex icon in the vertex icon set presented in the current interface, an upstream vertex icon and a downstream vertex icon in a flowchart presented by the current interface for a target vertex icon. The target vertex icons are vertex icons aimed at by the selection operation, and each vertex icon is associated with a code template identifier. The code template identification is used to uniquely indicate the code template.
It should be noted that, in the vertex icons in the flowchart, if there is a directional connection between the two vertex icons, for example, a connection from the vertex icon P1 to the vertex icon P2, the vertex icon P1 is an upstream vertex icon of the vertex icon P2, and the vertex icon P2 is a downstream vertex icon of the vertex icon P1.
In other words, the user may select vertex icons in the presented current interface (e.g., through a drag operation) and draw directed connecting lines between the vertex icons to form a flowchart. If the user selects a vertex icon, it is necessary to determine the upstream and downstream vertex icons of the user-selected vertex icon in the flow chart presented by the current interface.
In response to determining that the input parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template identification associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template identification associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template identification associated with the downstream vertex icon, the target vertex icon is added as a new vertex to the flow chart presented at the current interface, step 402.
In this embodiment, after the user selects the target vertex icon and determines the upstream vertex icon and the downstream vertex icon of the target vertex icon, the execution subject may determine whether the input parameter sequence in the code template indicated by the code template identifier associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template identifier associated with the upstream vertex icon, and determine whether the output parameter sequence in the code template indicated by the code template identifier associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template identifier associated with the downstream vertex icon, if both the above determinations are yes, it may indicate that the position of the target vertex icon selected by the user in the flow chart displayed by the current interface is appropriate, and then the target vertex icon may be added as a new vertex to the flow chart presented by the current interface.
Step 403, determining the flow chart presented by the current interface as the target flow chart.
In response to receiving the request to generate code from the target flow diagram, a determination is made as to whether the target flow diagram is a directed acyclic graph and whether the output parameter sequence at the beginning of each line in the target flow diagram matches the input parameter sequence at the end of the line 404.
In step 405, in response to determining that the determination is yes, performing topology ordering on the target flowchart, and obtaining a vertex sequence after topology ordering of the target flowchart.
Step 406, creating an empty code segment, and for each vertex in the vertex sequence, generating code content corresponding to the vertex in the front-to-back order of the vertex in the vertex sequence, and adding the generated code content to the end of the code segment.
In this embodiment, the specific operations of step 404, step 405 and step 406 are substantially the same as the operations of step 201, step 202 and step 203 in the embodiment shown in fig. 2, and will not be described herein.
Step 407, deleting the repeated code statement for referencing the same library file in the code segment, and reserving a code statement for referencing the same library file.
In practice, to reduce duplication of code writing, library files are often referenced in the code, and then various functions in the library files may be used. For example, the library file may be a header file (file with extension. H), a static library file (file with extension. Lib), and a dynamic library file (file with extension. Dll). Since the code segments generated by the steps splice the code content corresponding to each vertex in the flow chart together, the code statement repeatedly referencing the same library file is unavoidable.
Thus, in this embodiment, the execution body may delete the repeated code statement in the code segment for referencing the same library file, and reserve a code statement for referencing the same library file.
For example, a preset dependency library statement identifier may be introduced in a code template for indicating a preset location of a code statement referencing a dependency library file (e.g., a previous line of the code statement). In this way, the execution body may first find the code statement in the code section at the reverse preset position of the preset dependency library statement identifier (for example, the next line of the preset dependency library statement identifier). Then, the execution body may delete the repeated code statement for referencing the same library file from the found code statement, and only one code statement for referencing the same library file is reserved.
In step 408, duplicate code statements in the code segment for declaring the same function are deleted, and a code statement for declaring the same function is reserved.
In practice, to reduce duplication of code writing, functions are often declared in the code, which may then be called in the code. Code segments generated by the steps splice the code content corresponding to each vertex in the flow chart together, and code sentences of the same function with repeated life cannot be avoided.
Thus, in this embodiment, the execution body may delete the repeated code statement for declaring the same function in the code segment, and reserve a code statement for declaring the same function.
For example, a preset function declaration identifier may be introduced in a code template for indicating a preset location of a code statement that declares a function (e.g., a previous line of the code statement). In this way, the execution body may first find the code statement of the reverse preset position of the preset function declaration identifier (for example, the next line of the preset function declaration identifier) in the code section. Then, the execution body may delete the repeated code statement for declaring the same function from the found code statements, and reserve a code statement for declaring the same function.
Step 409, searching the code sub-segments matching with the preset execution time identifier.
In practice, there are limits to the number of times that some code segments can be executed. For example, an initialization operation can generally only be performed once after a flow has begun, and cannot be performed subsequently. For another example, performing some operations consumes relatively much memory and limits the number of times such operations can be performed.
Therefore, in this embodiment, the execution body may search the code segment for a code sub-segment that matches the preset execution number identifier. In practice, when a code template is pre-specified, for a code sub-segment with a limit on the number of executions, a preset execution number identifier is used to specify the maximum number of executions of the code sub-segment.
Step 410, for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving the determined execution times in the code segment, and deleting other code sub-segments matched with the code sub-segment in the code segment.
In this embodiment, the execution body may first determine, for each code sub-segment found in step 409, the execution times corresponding to the execution times statement in the code sub-segment, then reserve the determined execution times in the code segment for the code sub-segment that matches the code sub-segment, and delete other code sub-segments in the code segment that match the code sub-segment.
Thus, each code sub-segment in the code segment meets the execution times requirement of the code sub-segment.
In step 411, the preset resource application identifier in the code segment is replaced with a corresponding resource application code statement.
In order to reduce the manual workload of writing the code template and to reduce code errors, corresponding resource application code sentences are not written in the code template, and only preset resource application identifiers are written. Therefore, in this embodiment, the execution body may replace the preset resource application identifier in the code segment with the corresponding resource application code statement.
Step 412, replacing the preset resource release identifier in the code segment with the corresponding resource release code statement.
Similarly, in order to reduce the manual workload of writing the code template and to reduce code errors, no corresponding resource release code statement is written in the code template, and only the preset resource release identifier is written. Therefore, in this embodiment, the execution body may replace the preset resource release identifier in the code segment with the corresponding resource release code statement.
It should be noted that, each of the steps 407 to 412 is a specific step of optimizing the code, and the execution body may execute the steps 407 to 412 in various combinations after executing the step 406. For example, the steps 407 to 412 may be sequentially performed, only one of them may be performed, or at least two of them may be arbitrarily combined and performed in an arbitrary order, which is not particularly limited in this application.
In step 413, the code segment is determined to be the code corresponding to the request to generate the code according to the target flowchart.
In this embodiment, the specific operation of step 413 is substantially the same as that of step 204 in the embodiment shown in fig. 2, and will not be described herein.
Step 414, code files corresponding to the requests for code generation according to the target flow chart are generated and stored in the disk.
In this embodiment, the execution body may generate different types of code files according to a programming language according to which a code corresponding to a request for generating a code according to a target flowchart is based, and store the generated code files to a disk.
In some alternative implementations of the present embodiment, step 414 may proceed as follows:
first, at least one code file is generated from a code corresponding to a request for generating a code according to a target flowchart, wherein the number of lines of code statements of each generated code file is within a preset number of lines.
Here, the preset line number range may be a line number range suitable for a technician to read a code. In addition, the code content corresponding to each vertex in the target flow chart should be in the same code file as much as possible, so that the code content of each code file is better suitable for a technician to understand.
The generated at least one code file may then be stored to disk.
In step 415, library file dependency statements in the code corresponding to the request to generate code according to the target flow chart are looked up.
In this embodiment, the execution body may use a library file dependent statement keyword in a programming language according to which a code corresponding to a request for generating the code according to the target flowchart is searched for, so as to search for a library file dependent statement in the code corresponding to the request for generating the code according to the target flowchart.
Step 416, for each found library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file in the folder in which the code file is stored.
In this embodiment, for each library file dependent statement found in step 415, the execution subject copies the library file indicated by the library file dependent statement and stores the copied library file in the folder in which the code file is stored.
In this way, the code file and the library files relied upon in the code file are stored to the same folder to facilitate referencing the library files.
As can be seen from fig. 4, compared with the embodiment corresponding to fig. 2, the flow 400 of the method for generating codes in this embodiment is further provided with a step of detecting whether the output parameter sequence of the upstream vertex icon matches the input parameter sequence of the downstream vertex icon during the process of drawing the flow chart by the user, a step of performing various optimizations on the generated codes, and a step of generating code files and storing the code files to a disk. Therefore, the scheme described in the embodiment can more comprehensively realize the process from drawing the flow chart to finally generating the code file, and the code content in the generated code file is optimized.
With further reference to fig. 5, as an implementation of the method shown in the foregoing figures, the present application provides an embodiment of an apparatus for generating a code, where the embodiment of the apparatus corresponds to the embodiment of the method shown in fig. 2, and the apparatus may be specifically applied in various electronic devices.
As shown in fig. 5, the apparatus 500 for generating a code of the present embodiment includes: a first determination unit 501, a sorting unit 502, a code generation unit 503, and a second determination unit 504. Wherein, the first determining unit 501 is configured to determine, in response to receiving a request for generating a code according to a target flowchart, whether the target flowchart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flowchart matches an input parameter sequence of an end point of the line, wherein a vertex of the target flowchart includes a vertex graph and vertex information, and the vertex information includes a vertex identifier, a code template identifier, at least one attribute parameter, an input parameter sequence, and an output parameter sequence; a sorting unit 502 configured to perform topology sorting on the target flowchart to obtain a vertex sequence after topology sorting of the target flowchart in response to determining that the target flowchart is; a code generating unit 503 configured to create a new empty code segment, and for each vertex in the vertex sequence, generate code content corresponding to the vertex in the order of the vertex from front to back in the vertex sequence, and add the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertices located upstream of the vertex in the vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex; and a second determining unit 504 configured to determine the code segment as a code corresponding to the request for generating a code according to the target flowchart.
In this embodiment, the specific processes and the technical effects of the first determining unit 501, the sorting unit 502, the code generating unit 503 and the second determining unit 504 of the apparatus 500 for generating codes may refer to the relevant descriptions of the steps 201, 202, 203 and 204 in the corresponding embodiment of fig. 2, and are not repeated here.
In some optional implementations of this embodiment, the apparatus 500 may further include a code template loading unit (not shown in fig. 5) configured to: generating code content corresponding to each vertex in the vertex sequence from front to back in the vertex sequence for each vertex in the vertex sequence, and generating an object code template identification set by using different code template identifications in vertex information of each vertex in the vertex sequence before adding the generated code content to the end of the code segment; and loading the code template indicated by each code template identifier in the target code template identifier set into a cache.
In some optional implementations of this embodiment, the apparatus 500 may further include a target flowchart determining unit (not shown in fig. 5) configured to: before determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a starting point of each line in the target flow chart is matched with an input parameter sequence of an ending point of the line or not in response to receiving a request for generating codes according to the target flow chart, determining an upstream vertex icon and a downstream vertex icon of the target vertex icon in the flow chart presented by the current interface in response to detecting a selection operation of a user on the vertex icons in the vertex icon set presented by the current interface, wherein the target vertex icons are vertex icons aimed by the selection operation, and the vertex icons are associated with code template identifiers; and in response to determining that the input parameter sequence in the code template indicated by the code template identifier associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template identifier associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template identifier associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template identifier associated with the downstream vertex icon, adding the target vertex icon as a new vertex to the flow chart presented at the current interface; and determining the flow chart presented by the current interface as the target flow chart.
In some optional implementations of this embodiment, the apparatus 500 may further include a first repetition code statement deletion unit (not shown in fig. 5) configured to: and deleting repeated code sentences used for referencing the same library file in the code segments before the code segments are determined to be codes corresponding to the requests for generating codes according to the target flow chart, and reserving a code sentence used for referencing the same library file.
In some optional implementations of this embodiment, the apparatus 500 may further include a second repetition code statement deletion unit (not shown in fig. 5) configured to: and deleting repeated code sentences used for declaring the same function in the code segments before the code segments are determined to be codes corresponding to the requests for generating codes according to the target flow chart, and reserving one code sentence used for declaring the same function.
In some optional implementations of this embodiment, the apparatus 500 may further include a code sub-segment deletion unit (not shown in fig. 5) configured to: searching a code sub-section matched with a preset execution time identifier in the code section before the code section is determined to be the code corresponding to the request for generating the code according to the target flow chart; and for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving the determined execution times in the code segment to be matched with the code sub-segment, and deleting other code sub-segments matched with the code sub-segment in the code segment.
In some optional implementations of this embodiment, the apparatus 500 may further include a resource application unit (not shown in fig. 5) configured to: before the code segment is determined to be the code corresponding to the request for generating the code according to the target flow chart, the preset resource application identifier in the code segment is replaced by a corresponding resource application code statement.
In some optional implementations of this embodiment, the apparatus 500 may further include a resource release unit (not shown in fig. 5) configured to: before the code segment is determined to be the code corresponding to the request for generating the code according to the target flow chart, the preset resource release identifier in the code segment is replaced by a corresponding resource release code statement.
In some optional implementations of this embodiment, the generating code content corresponding to the vertex may further include: before a first log generation code sentence is added to the generated code content as a first sentence code sentence of the code content corresponding to the vertex, and after a second log generation code sentence is added to the generated code content as a last sentence code sentence of the code content corresponding to the vertex, wherein the first log generation code sentence is used for indicating the generation of a first log indicating the time when the code indicated by the code template identification of the vertex starts executing, the first log generation code sentence is used for indicating the generation of a second log indicating the time when the code indicated by the code template identification of the vertex ends executing.
In some optional implementations of this embodiment, the apparatus may further include a code file generating unit (not shown in fig. 5) configured to: code files corresponding to the requests to generate code according to the target flow chart are generated and stored to the disk.
In some optional implementations of this embodiment, the apparatus 500 may further include a library file copy storage unit (not shown in fig. 5) configured to: searching a library file dependency statement in a code corresponding to a request for generating the code according to the target flowchart; and for each searched library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file into a folder in which the code file is stored.
In some optional implementations of this embodiment, the code file generating unit may be further configured to: generating at least one code file from codes corresponding to the requests for generating codes according to the target flow chart, wherein the number of lines of code sentences of each generated code file is within a preset line number range; and storing the generated at least one code file to a disk.
It should be noted that, the implementation details and technical effects of each unit in the apparatus for generating a code provided in the embodiments of the present application may refer to the descriptions of other embodiments in the present application, which are not described herein again.
Referring now to FIG. 6, a schematic diagram of a computer system 600 suitable for use in implementing an electronic device of an embodiment of the present application is shown. The electronic device shown in fig. 6 is only an example and should not impose any limitation on the functionality and scope of use of the embodiments of the present application.
As shown in fig. 6, the computer system 600 includes a central processing unit (CPU, central Processing Unit) 601, which can perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 602 or a program loaded from a storage section 608 into a random access Memory (RAM, random Access Memory) 603. In the RAM 603, various programs and data required for the operation of the system 600 are also stored. The CPU 601, ROM 602, and RAM 603 are connected to each other through a bus 604. An Input/Output (I/O) interface 605 is also connected to bus 604.
The following components are connected to the I/O interface 605: an input portion 606 including a keyboard, mouse, etc.; an output portion 607 including a Cathode Ray Tube (CRT), a liquid crystal display (LCD, liquid Crystal Display), and the like, a speaker, and the like; a storage section 608 including a hard disk and the like; and a communication section 609 including a network interface card such as a LAN (local area network ) card, a modem, or the like. The communication section 609 performs communication processing via a network such as the internet. The drive 610 is also connected to the I/O interface 605 as needed. Removable media 611 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is installed as needed on drive 610 so that a computer program read therefrom is installed as needed into storage section 608.
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 through the communication portion 609, and/or installed from the removable medium 611. The above-described functions defined in the method of the present application are performed when the computer program is executed by a Central Processing Unit (CPU) 601. It should be noted that the computer readable medium described in the present application may be a computer readable signal medium or a computer readable storage medium, or any combination of the two. The computer readable storage medium can be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, 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 connection 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 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 the context of this document, 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, apparatus, or device. In the present application, however, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, with computer-readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
Computer program code for carrying out operations of the present application 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 connected to the user's computer through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computer (for example, 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 units involved in the embodiments of the present application may be implemented by software, or may be implemented by hardware. The described units may also be provided in a processor, for example, described as: a processor includes a first determination unit, an ordering unit, a code generation unit, and a second determination unit. The names of these units do not limit the units themselves in some cases, for example, the sorting unit may also be described as "a unit that topologically sorts the target flowchart to obtain a vertex sequence after the topological sorting of the target flowchart".
As another aspect, the present application also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be present alone without being fitted into the device. The computer readable medium carries one or more programs which, when executed by the apparatus, cause the apparatus to: in response to receiving a request to generate a code according to a target flow chart, determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flow chart matches an input parameter sequence of an end point of the line, wherein a vertex of the target flow chart comprises a vertex graph and vertex information, and the vertex information comprises a vertex identifier, a code template identifier, at least one attribute parameter, an input parameter sequence and an output parameter sequence; in response to the determination, performing topological sorting on the target flow chart to obtain a vertex sequence after the topological sorting of the target flow chart; creating an empty code segment, generating code content corresponding to each vertex in the vertex sequence according to the sequence of the vertex from front to back in the vertex sequence, and adding the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by the following steps: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertexes positioned upstream of the vertex in a vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex; the code segment is determined to be the code corresponding to the request to generate the code according to the target flowchart.
The foregoing description is only of the preferred embodiments of the present application and is presented as a description of the principles of the technology being utilized. It will be appreciated by persons skilled in the art that the scope of the invention referred to in this application is not limited to the specific combinations of features described above, but it is intended to cover other embodiments in which any combination of features described above or equivalents thereof is possible without departing from the spirit of the invention. Such as the above-described features and technical features having similar functions (but not limited to) disclosed in the present application are replaced with each other.

Claims (26)

1. A method for generating code, comprising:
in response to receiving a request to generate a code according to a target flow chart, determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flow chart is matched with an input parameter sequence of an end point of the line, wherein a vertex of the target flow chart comprises a vertex graph and vertex information, the vertex information comprises a vertex identifier, a code template identifier, at least one attribute parameter, the input parameter sequence and the output parameter sequence, and the code template identifier is used for indicating a code template corresponding to the vertex;
In response to determining that the target flow chart is topologically ordered, obtaining a vertex sequence of the target flow chart after the topological ordering;
creating an empty code segment, generating code content corresponding to each vertex in the vertex sequence according to the sequence of the vertex from front to back, and adding the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by the following steps: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertices located upstream of the vertex in the vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex;
determining the code segment as a code corresponding to the request to generate code according to the target flowchart;
wherein determining whether the output parameter sequence of the start point of each line in the target flowchart matches the input parameter sequence of the end point of the line comprises:
determining whether the data type of each output parameter in the output parameter sequence in the vertex information of the starting point of each line is the same as the data type of the input parameter in the corresponding position in the input parameter sequence in the vertex information of the ending point of the line;
In response to determining that the output parameter sequence of the start of the line matches the input parameter sequence of the end of the line.
2. The method of claim 1, wherein, prior to creating an empty code segment and for each vertex in the sequence of vertices, generating code content corresponding to the vertex in the sequence of vertices from front to back, and adding the generated code content to the end of the code segment, the method further comprises:
generating a target code template identification set by using different code template identifications in vertex information of each vertex in the vertex sequence;
and loading the code template indicated by each code template identifier in the target code template identifier set into a cache.
3. The method of claim 2, wherein prior to determining whether the target flow diagram is a directed acyclic graph and whether the output parameter sequence of the start of each line in the target flow diagram matches the input parameter sequence of the end of the line in response to receiving a request to generate a code from the target flow diagram, the method further comprises:
in response to detecting a selection operation of a user on a vertex icon in a vertex icon set presented in a current interface, determining an upstream vertex icon and a downstream vertex icon of a target vertex icon in a flow chart presented by the current interface, wherein the target vertex icon is the vertex icon aimed at by the selection operation, and the vertex icon is associated with a code template identifier;
In response to determining that the input parameter sequence in the code template indicated by the code template associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template associated with the downstream vertex icon, adding the target vertex icon as a new vertex to the flow chart presented at the current interface;
and determining the flow chart presented by the current interface as the target flow chart.
4. A method according to any of claims 1-3, wherein, prior to determining the code segment as a code corresponding to the request to generate code from a target flow chart, the method further comprises:
and deleting repeated code sentences used for referencing the same library file in the code section, and reserving a code sentence used for referencing the same library file.
5. A method according to any of claims 1-3, wherein, prior to determining the code segment as a code corresponding to the request to generate code from a target flow chart, the method further comprises:
And deleting repeated code sentences used for declaring the same function in the code segment, and reserving one code sentence used for declaring the same function.
6. A method according to any of claims 1-3, wherein, prior to determining the code segment as a code corresponding to the request to generate code from a target flow chart, the method further comprises:
searching a code sub-section matched with a preset execution time identifier in the code section;
for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving the determined execution times in the code segment to be matched with the code sub-segment, and deleting other code sub-segments matched with the code sub-segment in the code segment.
7. A method according to any of claims 1-3, wherein, prior to determining the code segment as a code corresponding to the request to generate code from a target flow chart, the method further comprises:
and replacing the preset resource application identifier in the code segment with a corresponding resource application code statement.
8. A method according to any of claims 1-3, wherein, prior to determining the code segment as a code corresponding to the request to generate code from a target flow chart, the method further comprises:
And replacing the preset resource release identifier in the code segment with a corresponding resource release code statement.
9. A method according to any one of claims 1-3, wherein the generating code content corresponding to the vertex further comprises:
before a first log generation code statement is added to the generated code content as a first sentence code statement of the code content corresponding to the vertex, and after a second log generation code statement is added to the generated code content as a last sentence code statement of the code content corresponding to the vertex, wherein the first log generation code statement is used for indicating that a first log is generated, the first log is used for indicating that a code template of the vertex indicates that a code indicated by the code template starts executing, the first log generation code statement is used for indicating that a second log is generated, and the second log is used for indicating that the code template of the vertex indicates that the code indicated by the code template of the vertex ends executing.
10. A method according to any one of claims 1-3, wherein the method further comprises:
code files corresponding to the requests to generate code according to the target flow chart are generated and stored to the disk.
11. The method of claim 10, wherein the method further comprises:
searching a library file dependency statement in a code corresponding to a request for generating the code according to the target flowchart;
and for each searched library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file into a folder in which the code file is stored.
12. The method of claim 10, wherein the generating and storing the code file corresponding to the request to generate the code according to the target flowchart to the disk comprises:
generating at least one code file from codes corresponding to the requests for generating codes according to the target flow chart, wherein the number of lines of code sentences of each generated code file is within a preset line number range;
the generated at least one code file is stored to a disk.
13. An apparatus for generating a code, comprising:
a first determining unit configured to determine, in response to receiving a request to generate a code according to a target flowchart, whether the target flowchart is a directed acyclic graph and whether an output parameter sequence of a start point of each line in the target flowchart matches an input parameter sequence of an end point of the line, wherein a vertex of the target flowchart includes a vertex graph and vertex information, the vertex information includes a vertex identifier, a code template identifier, at least one attribute parameter, an input parameter sequence, and an output parameter sequence, wherein the code template identifier is used to indicate a code template corresponding to the vertex;
The sequencing unit is configured to respond to the determination that the target flow chart is topologically sequenced, and a vertex sequence after the target flow chart is topologically sequenced is obtained;
a code generating unit configured to create an empty code segment, and for each vertex in the vertex sequence, generate code content corresponding to the vertex in the order of the vertex from front to back in the vertex sequence, and add the generated code content to the end of the code segment, wherein the code content corresponding to the vertex is determined by: replacing placeholders corresponding to input parameters in code template content indicated by the code template identification of the vertex with output parameters in corresponding code content of adjacent vertices located upstream of the vertex in the vertex sequence, and replacing placeholders corresponding to all attribute parameters with corresponding attribute parameters of the vertex to obtain code content corresponding to the vertex;
a second determination unit configured to determine the code segment as a code corresponding to the request to generate a code according to a target flowchart;
wherein determining whether the output parameter sequence of the start point of each line in the target flowchart matches the input parameter sequence of the end point of the line comprises:
Determining whether the data type of each output parameter in the output parameter sequence in the vertex information of the starting point of each line is the same as the data type of the input parameter in the corresponding position in the input parameter sequence in the vertex information of the ending point of the line;
in response to determining that the output parameter sequence of the start of the line matches the input parameter sequence of the end of the line.
14. The apparatus of claim 13, wherein the apparatus further comprises a code template loading unit configured to:
generating code content corresponding to each vertex in the vertex sequence according to the sequence from front to back of the vertex in the vertex sequence, and generating an object code template identification set by using different code template identifications in vertex information of each vertex in the vertex sequence before adding the generated code content to the end of the code segment; and
and loading the code template indicated by each code template identifier in the target code template identifier set into a cache.
15. The apparatus of claim 14, wherein the apparatus further comprises a target flowchart determination unit configured to:
Before determining whether the target flow chart is a directed acyclic graph and whether an output parameter sequence of a starting point of each line in the target flow chart is matched with an input parameter sequence of an ending point of the line or not in response to receiving a request for generating codes according to the target flow chart, and determining an upstream vertex icon and a downstream vertex icon of the target vertex icon in the flow chart presented by a current interface in response to detecting a user selection operation of the vertex icons in the vertex icon set presented by the current interface, wherein the target vertex icons are vertex icons aimed by the selection operation, and the vertex icons are associated with code template identifiers; and
in response to determining that the input parameter sequence in the code template indicated by the code template associated with the target vertex icon matches the output parameter sequence in the code template indicated by the code template associated with the upstream vertex icon, and determining that the output parameter sequence in the code template indicated by the code template associated with the target vertex icon matches the input parameter sequence in the code template indicated by the code template associated with the downstream vertex icon, adding the target vertex icon as a new vertex to the flow chart presented at the current interface; and
And determining the flow chart presented by the current interface as the target flow chart.
16. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a first repetition code statement deletion unit configured to:
and deleting repeated code sentences used for referencing the same library file in the code segments before the code segments are determined to be codes corresponding to the requests for generating codes according to the target flow chart, and reserving one code sentence used for referencing the same library file.
17. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a second repetition code statement deletion unit configured to:
and deleting repeated code sentences used for declaring the same function in the code segments before the code segments are determined to be codes corresponding to the requests for generating codes according to the target flow chart, and reserving one code sentence used for declaring the same function.
18. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a code sub-segment deletion unit configured to:
searching a code sub-section matched with a preset execution time identifier in the code section before the code section is determined to be the code corresponding to the request for generating the code according to the target flow chart; and
And for each searched code sub-segment, determining the execution times corresponding to the execution times statement in the code sub-segment, reserving a plurality of code sub-segments matched with the code sub-segment for the determined execution times in the code segment, and deleting other code sub-segments matched with the code sub-segment in the code segment.
19. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a resource application unit configured to:
and before the code segment is determined to be the code corresponding to the request for generating the code according to the target flow chart, replacing the preset resource application identifier in the code segment with a corresponding resource application code statement.
20. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a resource release unit configured to:
and before the code segment is determined to be the code corresponding to the request for generating the code according to the target flow chart, replacing the preset resource release identifier in the code segment with a corresponding resource release code statement.
21. The apparatus of any of claims 13-15, wherein the generating code content corresponding to the vertex further comprises:
Before a first log generation code statement is added to the generated code content as a first sentence code statement of the code content corresponding to the vertex, and after a second log generation code statement is added to the generated code content as a last sentence code statement of the code content corresponding to the vertex, wherein the first log generation code statement is used for indicating that a first log is generated, the first log is used for indicating that a code template of the vertex indicates that a code indicated by the code template starts executing, the first log generation code statement is used for indicating that a second log is generated, and the second log is used for indicating that the code template of the vertex indicates that the code indicated by the code template of the vertex ends executing.
22. The apparatus according to any of claims 13-15, wherein the apparatus further comprises a code file generating unit configured to:
code files corresponding to the requests to generate code according to the target flow chart are generated and stored to the disk.
23. The apparatus of claim 22, wherein the apparatus further comprises a library file copy storage unit configured to:
searching a library file dependency statement in a code corresponding to a request for generating the code according to the target flowchart; and
And for each searched library file dependent statement, copying the library file indicated by the library file dependent statement and storing the copied library file into a folder in which the code file is stored.
24. The apparatus of claim 22, wherein the code file generation unit is further configured to:
generating at least one code file from codes corresponding to the requests for generating codes according to the target flow chart, wherein the number of lines of code sentences of each generated code file is within a preset line number range; and
the generated at least one code file is stored to a disk.
25. An electronic device, comprising:
one or more processors;
a storage device having one or more programs stored thereon,
the one or more programs, when executed by the one or more processors, cause the one or more processors to implement the method of any of claims 1-12.
26. A computer readable storage medium having stored thereon a computer program, wherein the computer program when executed by one or more processors implements the method of any of claims 1-12.
CN201910325412.5A 2019-04-22 2019-04-22 Method and device for generating code Active CN110083351B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910325412.5A CN110083351B (en) 2019-04-22 2019-04-22 Method and device for generating code

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910325412.5A CN110083351B (en) 2019-04-22 2019-04-22 Method and device for generating code

Publications (2)

Publication Number Publication Date
CN110083351A CN110083351A (en) 2019-08-02
CN110083351B true CN110083351B (en) 2023-06-27

Family

ID=67416076

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910325412.5A Active CN110083351B (en) 2019-04-22 2019-04-22 Method and device for generating code

Country Status (1)

Country Link
CN (1) CN110083351B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110795089A (en) * 2019-10-29 2020-02-14 中国建设银行股份有限公司 Code generation method, device, equipment and storage medium
CN113535151B (en) * 2020-04-14 2024-05-17 北京京东振世信息技术有限公司 Code generation method and device

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH04205423A (en) * 1990-11-30 1992-07-27 Seiko Epson Corp Program forming device
JP2005202612A (en) * 2004-01-15 2005-07-28 Hitachi Eng Co Ltd Data base generation program preparing apparatus
US10114624B1 (en) * 2017-10-12 2018-10-30 Devfactory Fz-Llc Blackbox matching engine
CN109344230A (en) * 2018-10-31 2019-02-15 任志颖 Code library file generation, code search, connection, optimization and transplantation method
CN109522005A (en) * 2018-11-15 2019-03-26 苏州友教习亦教育科技有限公司 Cross-platform GRAPHICAL PROGRAMMING method

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102033748B (en) * 2010-12-03 2013-11-06 中国科学院软件研究所 Method for generating data processing flow codes
CN102591654B (en) * 2011-12-28 2015-09-23 方正国际软件有限公司 A kind of code generator based on SSH framework and method
KR101418489B1 (en) * 2013-03-27 2014-07-14 한국항공우주산업 주식회사 Verifying Device for Auto Generated Code of Fly-By-Wire Helicopter Flight Control Law and Verifying Method for the same
CN104965760B (en) * 2015-06-29 2018-12-21 广州酷狗计算机科技有限公司 A kind of method and apparatus of management software functional module life cycle
CN107729003A (en) * 2017-11-20 2018-02-23 江苏神州信源系统工程有限公司 Automatic generation method, device and the electronic equipment of code
CN108762764A (en) * 2018-06-05 2018-11-06 中国平安人寿保险股份有限公司 Code automatic generation method, device, computer equipment and storage medium
CN109101231B (en) * 2018-08-07 2022-09-23 百度在线网络技术(北京)有限公司 Script code generation method and device, computer equipment and storage medium

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH04205423A (en) * 1990-11-30 1992-07-27 Seiko Epson Corp Program forming device
JP2005202612A (en) * 2004-01-15 2005-07-28 Hitachi Eng Co Ltd Data base generation program preparing apparatus
US10114624B1 (en) * 2017-10-12 2018-10-30 Devfactory Fz-Llc Blackbox matching engine
CN109344230A (en) * 2018-10-31 2019-02-15 任志颖 Code library file generation, code search, connection, optimization and transplantation method
CN109522005A (en) * 2018-11-15 2019-03-26 苏州友教习亦教育科技有限公司 Cross-platform GRAPHICAL PROGRAMMING method

Non-Patent Citations (5)

* Cited by examiner, † Cited by third party
Title
AngularJS前端框架与后端代码自动生成系统的设计与实现;郑杰;《中国优秀硕士学位论文全文数据库 (信息科技辑)》(第01期);I138-62 *
On Identifying Patterns in Code Repositories to Assist the Generation of Hardware Templates;Adriano Sanches等;《2010 International Conference on Field Programmable Logic and Applications》;267-270 *
基于图形的代码自动生成系统优化设计与实现;刘运求;《中国优秀硕士学位论文全文数据库 (信息科技辑)》(第06期);I138-111 *
基于模板技术的UML状态图代码框架生成;郭冬阳;《中国优秀硕士学位论文全文数据库 (信息科技辑)》(第S2期);I138-487 *
程序流程图到代码的自动生成算法;王黎明等;《西安电子科技大学学报》;第39卷(第6期);70-77 *

Also Published As

Publication number Publication date
CN110083351A (en) 2019-08-02

Similar Documents

Publication Publication Date Title
US20190196672A1 (en) Visual effects system for "big data" analysis workflow editors, distribution platforms, execution engines, and management systems comprising same
CN107943877B (en) Method and device for generating multimedia content to be played
US20160313874A1 (en) Visual effects system for "big data" analysis workflow editors, distribution platforms, execution engines, and management systems comprising same
CN109829164B (en) Method and device for generating text
CN107526639B (en) Resource arrangement method, medium, device and computing equipment
CN110083351B (en) Method and device for generating code
WO2019029451A1 (en) Method for publishing mobile applications and electronic apparatus
CN111506300A (en) Applet generation method, device, equipment and storage medium
CN110780874A (en) Method and apparatus for generating information
US20130185696A1 (en) Manipulating source code patches
CN111107133A (en) Generation method of difference packet, data updating method, device and storage medium
US9454361B2 (en) System and method of merging of objects from different replicas
CN109254778B (en) Method and apparatus for deploying an information flow system
CN112947912A (en) Method and device for generating code, electronic equipment and storage medium
CN109582347B (en) Method and device for acquiring front-end codes
CN112181408A (en) Method and device for displaying view list on front page of application program
CN113297081B (en) Execution method and device of continuous integrated pipeline
CN115167822A (en) Branch code merging method, device, equipment and storage medium
CN112395194B (en) Method and device for accessing test platform
CN113806327A (en) Database design method and device and related equipment
CN109857838B (en) Method and apparatus for generating information
CN110795102B (en) Module compiling method, device, electronic equipment and computer readable medium
CN112363700A (en) Cooperative creation method and device of intelligent contract, computer equipment and storage medium
CN110377326B (en) Installation package generation method, installation package generation device, development device and computer readable medium
CN112579428A (en) Interface testing method and device, electronic 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
GR01 Patent grant
GR01 Patent grant