Methods, systems, and storage media for converting node graphs to target language code.
By using depth-first traversal and reverse dependency backtracking, target language code independent of the runtime engine is generated, solving the code dependency and readability problems in visual programming systems and achieving efficient code conversion and maintainability.
Patent Information
- Application Number
- CN202611593155.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-10-15
- Publication Date
- 2026-11-17
AI Technical Summary
Existing visual programming systems generate code that relies on runtime engines, lacks the independence and engineering compatibility of native code, has weak semantic mapping capabilities, poor readability, and is difficult to meet the needs of manual debugging and secondary development.
By performing a depth-first traversal along the execution pins starting from the function entry node, the main execution order of the code is determined. Then, based on the dependencies of the data pins, a reverse dependency backtracking is performed to determine the declaration order. The conversion of callback functions is delayed, variables declared in the main flow are reused, and standard source code for the target language is generated.
It realizes the conversion of node graphs into independently executable target language code, avoiding undefined variables and state pollution. The generated code can be integrated into standard engineering toolchains and has good readability and maintainability.
Smart Images

Figure CN123114482A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of visual programming technology, and in particular to a method for converting node graphs to target language code, a system for converting node graphs to target language code, and a computer-readable storage medium. Background Technology
[0002] Visual programming expresses program logic graphically. Users build programs by dragging and dropping nodes and connecting pins, and the system then converts the graphical program into executable code text. Existing visual programming systems (such as Node-RED, Blockly, etc.) have the following defects in the process of converting node graphs into deliverable code: (1) The generated products depend on the runtime engine and lack the independence and engineering compatibility of native code. Most existing systems adopt the interpreted execution mode, and their output products are usually intermediate configuration files such as JSON and XML, rather than native code that can be run directly. The generated programs cannot run independently without a specific runtime environment, and are also difficult to be directly processed and optimized by modern front-end engineering toolchains such as Babel, Webpack, and Rollup, which increases the deployment complexity and cuts off the integration path between visual programming and standard software development processes. (2) The semantic mapping capability is weak and cannot support complex business logic and high-level execution context. Existing code generation technologies are mostly limited to simple linear logic or single function graph structures, lacking the ability to deeply map complex program semantics. When faced with complex business scenarios such as multi-function graph organization, inter-function calls, deep nested closures, and asynchronous callbacks, they cannot build rigorous lexical scope and execution context in the generated code, which can easily lead to variable naming conflicts, state pollution, or logical execution errors, making it difficult to achieve business expression capabilities equivalent to traditional text programming. (3) The readability and maintainability of the generated code are poor, making it difficult to meet the needs of manual debugging and secondary development. Existing conversion algorithms usually only focus on whether the code can be executed by the machine. The generated code lacks standardized variable naming, clear hierarchical structure, and consistent formatting style, making it difficult for developers to directly read and locate defects, let alone perform secondary editing and expansion on this basis.
[0003] Therefore, how to convert the node graph in visual programming into JavaScript code that can be executed independently of the runtime environment and has a rigorous execution context and good readability has become a technical problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0004] This invention provides a method, system, and computer-readable storage medium for converting node graphs to target language code, thereby solving the problems of the generated products relying on the node graph running engine, weak semantic mapping capabilities, and poor readability of the generated code in related technologies.
[0005] As a first aspect of the present invention, a method for converting node graphs to target language code is provided, comprising: A receiving node diagram, the node diagram including functional nodes and execution pins and data pins connecting the functional nodes; Starting from the function entry node in the node graph, perform a depth-first traversal of the functional nodes along the execution pin to obtain the main execution order of the code. Based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it. When traversing to the target node that references the callback function, the conversion of the callback function will be delayed until after the main flow conversion is completed, and the variables declared in the main flow will be reused when the callback function is converted. The main flow code is generated according to the main branch order and the declaration order, and the main flow code and the converted callback function code are assembled into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph execution engine.
[0006] Furthermore, based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, including: Push the currently traversed functional node onto the stack, and push the preceding dependent nodes connected to the currently traversed functional node via the data pins onto the stack. The preceding dependent node is used as the new currently traversed functional node, and the above stack pushing steps are repeated until the functional node pushed onto the stack is a source node without preceding dependent nodes. Functional nodes are popped from the stack in a last-in-first-out (LIFO) order, and then generated in the code sequence according to the popped order to determine the declaration order.
[0007] Furthermore, based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, which also includes: Before pushing the preceding dependent node onto the stack, determine whether the preceding dependent node already exists in the stack; In response to the fact that the preceding dependent node already exists in the stack, it is determined that the node graph has a circular dependency.
[0008] Furthermore, the conversion of the callback function is delayed until after the main process conversion is completed, including: When traversing to the target node that references the callback function, write a callback placeholder in the main flow statement corresponding to the target node, and temporarily store the definition of the callback function in the callback repository; After the main process transformation is completed, the callback repository is traversed, the temporarily stored callback functions are transformed, and the callback placeholders are replaced with the transformed callback function code.
[0009] Furthermore, when transforming the callback function, variables declared in the main flow are reused, including: During the main process transformation, declaration statements are generated for the variables in the main process; When the callback function is transformed, in response to the callback function referencing a variable already declared in the main flow, the identifier of the declared variable is referenced in the callback function code, and the declaration statement of the declared variable is not regenerated.
[0010] Furthermore, the main process code and the converted callback function code are assembled into code text in the target language, including: Based on the functions called by each functional node, generate a module import declaration including a module import identifier; According to the main branch order and the declaration order, the main flow code is concatenated with the converted callback function code to obtain the concatenated code; The concatenated code is combined with the module import declaration, and the entry function of the node graph is attached to the global variable object of the target language to obtain the code text of the target language.
[0011] Furthermore, the method for converting node graphs to target language code also includes the following steps performed before the depth-first traversal step: Starting from the function entry node, mark the reachable functional nodes along the execution pin and the data pin; During the transformation process, transformations are not performed on functional nodes that are not marked.
[0012] Furthermore, the node graph includes multiple function graphs, each function graph including a function entry node; the method for converting the node graph to target language code further includes: Taking the current function graph among the multiple function graphs as the object, perform the depth-first traversal and the reverse dependency backtracking to obtain the function code corresponding to the current function graph; In response to a function node in the current function graph referencing another function graph in the plurality of function graphs, the referenced other function graph is identified as the referenced function graph; The referenced function graph is used as a preceding dependency node for backtracking, and the above steps are recursively executed on the referenced function graph to obtain the function code corresponding to the referenced function graph.
[0013] As another aspect of the present invention, a node graph to target language code conversion system is provided for implementing the node graph to target language code conversion method described above, wherein the system includes: A node graph receiving module is used to receive a node graph, which includes functional nodes and execution pins and data pins connecting the functional nodes; The main execution order determination module is used to perform a depth-first traversal of the functional nodes along the execution pin, starting from the function entry node in the node graph, to obtain the main execution order of the code. The declaration order determination module is used to perform reverse dependency backtracking on the traversed functional nodes according to the dependency relationship represented by the data pin to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it. The callback transformation module is used to delay the transformation of the callback function until the main process transformation is completed when traversing to the target node that references the callback function, and to reuse the variables declared in the main process when transforming the callback function; The code assembly module is used to generate main flow code according to the trunk order and the declaration order, and assemble the main flow code and the converted callback function code into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph execution engine.
[0014] As another aspect of the present invention, a computer-readable storage medium is provided having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the node graph to target language code conversion method described above.
[0015] The node graph to target language code conversion method provided by this invention effectively determines the code execution order by performing a depth-first traversal along the execution pins starting from the function entry node to obtain the main branch order; it determines the declaration order by performing reverse dependency backtracking based on the dependency relationships represented by the data pins, ensuring that the dependent functional nodes are declared before the functional nodes that depend on them, effectively avoiding the problem of undefined variables; it achieves rigorous execution context construction by delaying the conversion of callback functions until after the main flow conversion is completed and reusing variables already declared in the main flow, avoiding duplicate declarations and state pollution of variables; and it assembles the main flow code and callback function code into standard source code of the target language, so that the execution of the standard source code does not depend on the node graph execution engine, thereby realizing the conversion of node graphs into target language code that can be executed independently and integrated into standard engineering toolchains. Attached Figure Description
[0016] The accompanying drawings are provided to further illustrate the invention and form part of the specification. They are used together with the following detailed description to explain the invention, but do not constitute a limitation thereof.
[0017] Figure 1 This is a flowchart of the node graph to target language code conversion method provided by the present invention.
[0018] Figure 2 This is a flowchart for determining the declaration order of each functional node, provided by the present invention.
[0019] Figure 3 This is a schematic diagram of the graph structure for converting strings to numbers and printing them, as provided by the present invention.
[0020] Figure 4 The flowchart provided by this invention delays the conversion of the callback function until after the main process conversion is completed.
[0021] Figure 5 This invention provides a flowchart for reusing variables declared in the main flow when converting callback functions.
[0022] Figure 6 This invention provides a flowchart for assembling the main process code and the converted callback function code into code text in the target language.
[0023] Figure 7 This is a schematic diagram of the node graph structure for factorial calculation provided by the present invention.
[0024] Figure 8 This is a schematic diagram of the node graph structure for asynchronous requests provided by the present invention.
[0025] Figure 9This is a structural block diagram of the node graph to target language code conversion device provided by the present invention.
[0026] Figure 10 This is a structural block diagram of the electronic device provided by the present invention. Detailed Implementation
[0027] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.
[0028] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0029] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate for the embodiments of the invention described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0030] In this embodiment of the invention, the node graph refers to a data structure that graphically represents program logic, including several functional nodes and pins connecting each functional node. A functional node is a node capable of performing a specific function and providing data to and / or receiving data from other functional nodes. An execution pin is a pin used to connect two functional nodes and characterize the execution order (i.e., control flow). Two functional nodes connected by execution pins form a sequential execution relationship. A data pin is a pin used to connect two functional nodes and characterize the data transfer relationship (i.e., data flow). A functional node obtains the output of another functional node as input through a data pin, thus forming a dependency on the functional node it connects to. The execution order formed by the connection of execution pins is called control flow, and the dependency relationship formed by the connection of data pins is called data flow. This invention implements code conversion of the node graph based on a data-control dual-flow model.
[0031] In this embodiment of the invention, the node graph execution engine refers to an engine used to parse and / or run the node graph, which interprets and executes node by node according to the data structure of the node graph, such as the Node-RED runtime, Blockly code executor, etc. The standard source code refers to target language source code that can be directly executed by the target language runtime environment without being parsed by the node graph execution engine, and is distinct from machine code. The target language can be the source code of any programmable language such as JavaScript, TypeScript, Python, etc.
[0032] In this embodiment of the invention, as an example, the node graph includes a function graph, which includes a start node, a loop node, and a return node. The start node and the loop node, and the loop node and the return node, are connected via execution pins, respectively. The loop node references a loop body callback function. The child node graph corresponding to the loop body callback function includes an addition node and a multiplication node. The multiplication node is connected to the addition node via a data pin to obtain the output of the addition node as its own input. Taking JavaScript as the target language as an example, the conversion process of this node graph is as follows.
[0033] This embodiment provides a method for converting node graphs to target language code. Figure 1 This is a flowchart of a node graph to target language code conversion method provided by an embodiment of the present invention, such as... Figure 1 As shown, it includes: S100, Receive node diagram, the node diagram includes functional nodes and execution pins and data pins connecting the functional nodes.
[0034] Specifically, the node diagram includes functional nodes, namely a start node, a loop node, a return node, an addition node, and a multiplication node. The node diagram also includes execution pins and data pins connecting the functional nodes, wherein the start node, loop node, and return node are connected sequentially by execution pins, and the multiplication node and addition node are connected by data pins. The node diagram also includes a function entry node; in this embodiment, the start node is the function entry node of the function diagram.
[0035] S200. Starting from the function entry node in the node graph, perform a depth-first traversal of the function nodes along the execution pin to obtain the main execution order of the code.
[0036] Specifically, starting from the function entry node (start node), a depth-first traversal is performed along the execution pins to obtain the main execution order of the code. Specifically, traversing sequentially along the execution pins from the start node yields the following main execution order: start node → loop node → return node. This main execution order represents the execution sequence defined by the execution pins, i.e., the start node is executed first, then the loop node, and finally the return node.
[0037] S300. Based on the dependency relationship represented by the data pin, perform reverse dependency backtracking on the traversed functional nodes to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it.
[0038] Specifically, for each multiplication node encountered, its data pin depends on the output of the addition node. Therefore, after performing reverse dependency backtracking, it is determined that the code corresponding to the addition node should be declared (generated) before the code corresponding to the multiplication node. This declaration order ensures that the functional node producing the output has been declared before its use, thus avoiding the "undefined variable" problem caused by inconsistencies between the node's position in the node graph and its declaration order.
[0039] S400. When traversing to the target node that references the callback function, the conversion of the callback function is delayed until after the main flow conversion is completed, and the variables declared in the main flow are reused when the callback function is converted.
[0040] Specifically, when traversing to the loop node, the loop node is determined to be the target node that references the callback function (loop body callback function). At this time, the loop body callback function is not immediately converted, but the conversion of the loop body callback function is delayed until the main flow (i.e., the main flow of the function graph) is completed. When converting the loop body callback function, the variables declared in the main flow are reused, thereby ensuring that the callback function can correctly reference the variables in its scope while avoiding the redeclaration of variables.
[0041] S500. Generate main flow code according to the main branch order and the declaration order, and assemble the main flow code and the converted callback function code into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph running engine.
[0042] Specifically, the main flow code is generated according to the main branch order obtained in S200 and the declaration order obtained in S300. In the main flow code, the loop call statement generated at the loop node contains a callback function. The loop body callback function code obtained by the delayed conversion in S400 is assembled with the main flow code to obtain the standard source code as the target language (JavaScript) code text. The standard source code is standard source code that can be directly executed by the JavaScript runtime environment without being parsed by the node graph runtime engine, and its execution does not depend on the node graph runtime engine.
[0043] The node graph to target language code conversion method provided by this invention obtains a clear code execution backbone order by performing a depth-first traversal along the execution pins starting from the function entry node, ensuring that the execution order of the generated code is consistent with the control flow expressed by the node graph. It determines the declaration order by performing reverse dependency backtracking based on the dependencies represented by the data pins, ensuring that the dependent functional nodes are declared before the functional nodes that depend on them, avoiding undefined variable problems caused by inconsistencies between node arrangement and declaration order. When traversing to the target node referencing the callback function, the conversion of the callback function is delayed until after the main flow conversion is completed, and variables already declared in the main flow are reused during callback function conversion, avoiding duplicate variable declarations and state pollution while ensuring that the callback function can correctly construct its execution context. By assembling the main flow code and the callback function code into standard source code of the target language, the execution of the generated standard source code does not depend on the node graph runtime engine, thus enabling independent operation and integration into standard engineering toolchains, solving the problem in related technologies where the generated artifacts depend on the node graph runtime engine.
[0044] In this embodiment of the invention, the determination of the declaration order through reverse dependency backtracking can be implemented using a stack structure. Specifically, based on the dependency relationships represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, such as... Figure 2 As shown, it includes: S310. Push the currently traversed functional node onto the stack, and push the preceding dependent node connected to the currently traversed functional node through the data pin onto the stack. S320. Take the aforementioned predecessor dependency node as the new currently traversed functional node, and repeat the above stack push step until the functional node pushed onto the stack is a source node without a predecessor dependency node. S330. Pop the functional nodes in the stack in the last-in-first-out order, and generate them in the code sequence in the order they were popped out, so as to determine the declaration order.
[0045] See Figure 3As shown, by way of example, taking "converting a string to a number and printing" as an example: the node graph includes a start node, a console print node, a string-to-number node, and a return node, wherein the start node, the console print node, and the return node are sequentially connected through execution pins, and the string-to-number node is connected to the console print node through a data pin to provide an input to the console print node (the input of the string-to-number node is the input of the start node). The conversion process includes: 1) Execution flow traversal: Traverse sequentially from the start node along the execution pins, and obtain the main sequence as start node → console print node → return node; 2) Node-by-node processing and dependency chain parsing: For the traversed console print node, push its pre-dependent node (the string-to-number node) connected via the data pin into the stack; since the string-to-number node has no pre-dependent node, it is regarded as a source node, and the pushing process is stopped; then pop the functional nodes in the stack sequentially in a last-in-first-out order, and generate the popped functional nodes into the code sequence sequentially in the popping order. Using the last-in-first-out feature of the stack, the console print node pushed into the stack earlier is popped later, and the string-to-number node pushed into the stack later is popped first, so that the popped functional nodes are sequentially in the popping order: string-to-number node → console print node → (function return). Accordingly, the function call sequence funcList = [string-to-number function, console print function, function return] is constructed in reverse order, that is, the code corresponding to the string-to-number node is generated before the code corresponding to the console print node, thereby determining a declaration order that causes the depended functional node to be declared before the functional node that depends on it; 3) Module dependency collection: During the node-by-node processing, collect the modules where the functions called by each functional node are located, for example, the string-to-number node corresponds to typeUtils.js, and the console print node corresponds to windowUtils.js, so as to obtain module import information; 4) Code assembly: Splice and generate a code text according to the main sequence and the declaration order, and the generated JavaScript code example is as follows: import { gv_str2num} from "resources / common / Type Judgment and Conversion / 1.0.0 / typeUtils.js"; import { gv_consolePrint} from "resources / common / Global / 1.0.0 / windowUtils.js"; if (!window.DevBlueprint) { window.DevBlueprint = {};} window.DevBlueprint.main = function(inVal) { const gv_str2numReturn = gv_str2num(inVal); const gv_consolePrintReturn = gv_consolePrint("log", gv_str2numReturn); return gv_str2numReturn; } The return values of each functional node are assigned to corresponding variables (e.g., gv_str2numReturn, gv_consolePrintReturn), and the return value variables of the dependent nodes (string to number nodes) are used before the nodes that depend on them (console print nodes), reflecting the declaration order.
[0046] In this embodiment of the invention, based on the dependency relationships represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, which further includes: Before pushing the preceding dependent node onto the stack, determine whether the preceding dependent node already exists in the stack; In response to the fact that the preceding dependent node already exists in the stack, it is determined that the node graph has a circular dependency.
[0047] It should be noted that "already exists in the stack" here means that the preceding dependent node exists in the stack corresponding to the backtracking path formed by the current reverse dependency backtracking. However, in the case where multiple functional nodes share the same preceding dependent node (i.e., a normal directed acyclic structure), since the shared preceding dependent node is no longer in the stack after being popped from the stack, it will not be judged as already existing in the stack when it is pushed onto the stack again as a preceding dependent node by other functional nodes, and therefore will not be misjudged as a circular dependency. Thus, the embodiments of the present invention can correctly support the scenario where multiple functional nodes share the same preceding dependent node while realizing circular dependency detection.
[0048] In this embodiment of the invention, the conversion of the callback function is delayed until after the main process conversion is completed, such as... Figure 4 As shown, it includes: S410. When traversing to the target node that references the callback function, write a callback placeholder in the main flow statement corresponding to the target node, and temporarily store the definition of the callback function in the callback repository. S420. After the main process conversion is completed, the callback repository is traversed, the temporarily stored callback functions are converted, and the callback placeholders are replaced with the converted callback function code.
[0049] This invention employs a strategy of "main flow priority extraction + placeholders + delayed conversion + closure reuse": When compiling the main flow, if a target node referencing a callback function is encountered, a unique callback placeholder is written at the corresponding position in the main flow statement, and the definition of the callback function is temporarily stored in the callback repository without immediately expanding the callback function; after the main flow conversion is completed, the callback repository is traversed, each callback function is converted one by one, and the converted callback function code replaces the corresponding callback placeholder. This approach ensures that the conversion of callback functions occurs after the variable declarations in the main flow are completed, thereby guaranteeing that the callback functions can correctly reuse the variables declared in the main flow.
[0050] In this embodiment of the invention, variables declared in the main flow are reused when the callback function is transformed, such as... Figure 5 As shown, it includes: S430. During the main process transformation, declaration statements are generated for the variables in the main process; S440. When converting the callback function, in response to the callback function referencing a variable already declared in the main flow, the identifier of the declared variable is referenced in the callback function code, and the declaration statement of the declared variable is not regenerated.
[0051] Conversely, if the callback function references an independent variable not declared in the main process, then the independent variable is locally declared within the callback function. Thus, the callback function reuses variables already declared in the main process through closures, avoiding duplicate execution or state pollution caused by the same variable being declared repeatedly in both the main process and the callback function.
[0052] In this embodiment of the invention, the main process code and the converted callback function code are assembled into code text in the target language, such as... Figure 6 As shown, it includes: S510. Generate a module import declaration, including a module import identifier, based on the functions called by each functional node. S520. According to the main branch order and the declaration order, the main flow code is concatenated with the converted callback function code to obtain the concatenated code; S530. Combine the concatenated code with the module import declaration, and attach the entry function of the node graph to the global variable object of the target language to obtain the code text of the target language.
[0053] Specifically, as an example, the module imports an import statement declared as the target language (JavaScript), the global variable object of the target language is the browser's global variable object `window`, and the entry function of the node graph is mounted to `window.DevBlueprint`, for example: if (!window.DevBlueprint) { window.DevBlueprint = {};} window.DevBlueprint.main = function(inVal) { ...}; Based on this, the generated code text becomes independent code that can be called through the global variable object and has complete module dependency information.
[0054] In this embodiment of the invention, the method for converting node graphs to target language code further includes steps performed before the depth-first traversal step: 1) Starting from the function entry node, mark the reachable functional nodes along the execution pin and the data pin; 2) During the conversion process, the conversion is not performed on functional nodes that are not marked.
[0055] This invention adopts the principle of "full conversion and on-demand output": first, a function call relationship graph is established, and then reachability analysis is performed starting from the entry function. Only the logic of the reached (accessible) nodes is converted, while no code is generated for unreachable isolated nodes or invalid logic, thereby avoiding the generation of redundant code and improving the simplicity and readability of the generated code.
[0056] In this embodiment of the invention, the node graph includes multiple function graphs, each function graph including a function entry node; the method for converting the node graph to target language code further includes: 1) Taking the current function graph among the multiple function graphs as the object, perform the depth-first traversal and the reverse dependency backtracking to obtain the function code corresponding to the current function graph; 2) In response to a function node in the current function graph referencing another function graph in the plurality of function graphs, the referenced other function graph is identified as the referenced function graph; 3) Backtrack the referenced function graph as a preceding dependency node and recursively execute the above steps on the referenced function graph to obtain the function code corresponding to the referenced function graph.
[0057] As an example, such as Figure 7As shown, taking "factorial calculation" as an example: the node graph includes a main function graph and a my_factorial function graph. The main function graph includes a start node, a factorial operation node, and a return node. The my_factorial function graph includes a start node, a loop (for) node, and a return node. The child node graph corresponding to the loop body callback function of the loop node includes an addition node, a multiplication node, and a variable assignment node. The node graph also includes the global variable factorial_result. The conversion process includes: Phase 1: Compiling the main function graph: Starting from the function entry node of the main function graph, perform a depth-first traversal and reverse dependency backtracking to obtain the function code corresponding to the main function graph; where the factorial operation node references the my_factorial function graph, then the my_factorial function graph is identified as the referenced function graph, it is used as a predecessor dependency node for backtracking, and the my_factorial function graph is recursively transformed to obtain the function code corresponding to the my_factorial function graph.
[0058] Phase 2: Compiling the my_factorial function graph: Depth-first traversal and reverse dependency backtracking are performed again. The loop node triggers the loop body callback function. The callback function is converted with a delay. The callback body is parsed to obtain the function call sequence (addition function, variable reading, multiplication function, variable assignment). When converting the callback function, the variables declared in the main flow and global variables are reused.
[0059] Phase 3, Code Assembly: Assemble the function code with module import declarations and global variable declarations. An example of the generated JavaScript code is shown below: import { gv_add, gv_multiply} from "resources / common / arithmetic / 1.0.0 / promotableOperatorUtils.js"; import { gv_forLoop} from "resources / common / logiccontrol / 1.0.0 / logicControllerUtils.js"; if (!window.DevBlueprint) { window.DevBlueprint = {};} const globalVariables = {"factorial_result":1} function my_factorial(n) { / / Loop (for) const gv_forLoopReturn = gv_forLoop(0, n, undefined, undefined,function (index) { / / Addition operation const gv_addReturn = gv_add(index, 1); / / Multiplication operation const gv_multiplyReturn = gv_multiply(gv_addReturn,globalVariables.factorial_result); globalVariables.factorial_result = gv_multiplyReturn; return globalVariables.factorial_result }); return gv_forLoopReturn } window.DevBlueprint.simple_factorial = function simple_factorial (n){ / / Factorial operation const my_factorialReturn = my_factorial(n); return my_factorialReturn }; In the generated code above, the loop callback function reuses the variables globalVariables and the parameters of the function in the main flow through closure, and is not declared repeatedly, which reflects the delayed conversion and variable reuse mechanism of the callback function; the function code corresponding to the my_factorial function graph is generated before the function code corresponding to the main function graph, which reflects that the declaration order of the referenced function graph across function graphs is determined by backtracking as a preceding dependent node.
[0060] As an example, such as Figure 8As shown, taking "asynchronous request" as an example, the conversion of asynchronous callback is explained as follows: The node graph includes a start node, a GET request node, a console print node, a current time node, and a return node. The start node, GET request node, and console print node are connected sequentially via execution pins. The GET request node, in addition to the first output execution pin (subsequently), also includes two output execution pins: "Request successful execution" and "Request failed execution." The "Request successful execution" pin is connected to a request success callback function graph (which includes the start node and the console print node). The current time node is a pure function (its pure function flag is true) and has no execution pin. The conversion process includes: Phase 1: Compile the main function graph: Traverse sequentially along the execution pins from the start node to obtain the main branch order as start node → GET request node → console print node; among them, "get current time" is a pure function with no execution pin and does not participate in the execution flow traversal. Its return value serves as the data dependency of the console print node and is triggered during the dependency chain resolution phase. When resolving the dependency chain of a GET request node, its parameters are collected: [" / api / system / info", request parameters (not connected), request headers (not connected), callback for successful request execution, callback for failed request execution (not connected)]. The node is identified as having two output execution pins: "successful request execution" and "failed request execution," which are determined to be callback pins. The "successful request execution" pin is already connected, and a callback function name (e.g., gv_httpGetCallback, where uid is the unique identifier of the node instance) is generated accordingly. The callback definition is recorded (the callback branch start node is the console print node connected to this pin, and the parameter name set is the data output pin name "request data" associated with this pin). This callback definition is pushed into the callback compilation queue (i.e., the callback repository). The function body is not expanded yet, and a callback placeholder value `callback1` is written to the parameter list. The "failed request execution" pin is not connected, and `undefined` is written to the corresponding parameter position. Based on this, the function call sequence `funcList = [GET request function, get current time function, console print function]` is obtained, corresponding to the following statement: const gv_httpGetReturn = gv_httpGet(" / api / system / info", undefined,undefined, $callback1$, undefined); const gv_dateNowReturn = gv_dateNow(); const gv_consolePrintReturn = gv_consolePrint("log", gv_dateNowReturn); Stage 2, compiling the asynchronous callback function graph: taking the node that calls the callback function (the GET request node) as the start node, all nodes of the start node and the callback function form a function graph, and the parsing process is executed on this function graph again; performing dependency chain parsing on the console print node in the callback function body, where "request data" is the name of the data output pin associated with the callback pin, which is injected as the formal parameter of the callback function by the GET request node when the request succeeds, and is not pushed into the stack as a pre-dependency; accordingly, the function call sequence funcList = [console print function] is obtained, the statement "const gv_consolePrintReturn = gv_consolePrint("log", request data);" is generated, and the return statement return gv_consolePrintReturn is appended at the end of the callback function body. Then the placeholder callback1 in the main process statement is replaced with the parsing result.
[0061] Stage 3, code assembly: generating module import declarations, and replacing the placeholders in the main process statements with callback function code, the generated JavaScript code example is as follows: import { gv_httpGet} from "resources / common / Interface Request / HTTP / HTTPS Interface / 1.0.0 / httpUtils.js"; import { gv_dateNow} from "resources / common / Date / 1.0.0 / dateUtils.js"; import { gv_consolePrint} from "resources / common / Global / 1.0.0 / windowUtils.js"; if (!window.DevBlueprint) { window.DevBlueprint = {}; } window.DevBlueprint.main = function() { / / GET request const gv_httpGetReturn = gv_httpGet(" / api / system / info", undefined, undefined, function (request data) { / / Console print const gv_consolePrintReturn = gv_consolePrint("log", Request data); return gv_consolePrintReturn }, undefined); / / Get current time const gv_dateNowReturn = gv_dateNow(); / / Console print const gv_consolePrintReturn = gv_consolePrint("log", gv_dateNowReturn); } Therefore, asynchronous callback functions are transformed into a form where placeholders are replaced by callback function bodies. The asynchronous callback function bodies are treated as independent scopes and reuse main process variables. While maintaining the closure characteristics, the duplicate declaration of variables is avoided, and the execution business logic of asynchronous callbacks can be correctly expressed.
[0062] In this embodiment of the invention, for the return values of each functional node, an incremental naming strategy is adopted to generate standardized variable identifiers: variable identifiers are generated for each functional node based on its identifier, for example, variable identifiers are generated in the format of "[function name]Return" (such as gv_str2numReturn, gv_multiplyReturn, my_factorialReturn); when the generated variable identifier is the same as the previously generated variable identifier, a numeric suffix is automatically appended to the generated variable identifier until the generated variable identifier is unique (such as fooReturn, fooReturn_1, fooReturn_1_1), thereby avoiding variable naming conflicts and improving the readability of the generated code.
[0063] In this embodiment of the invention, the method for converting node graphs to target language code further includes automatically generating business comments: during code generation, the node meta-information of each functional node is read, including function names, function descriptions, input and output parameters, etc.; based on the node meta-information, business comments are automatically generated before the core code segments corresponding to each functional node, covering the core code segments corresponding to all logical nodes, without the need for manual addition of additional business description comments. For example, in the code generated in the above embodiments, comments such as " / / loop (for)", " / / addition operation", " / / multiplication operation", " / / factorial operation", " / / GET request", and " / / get current time" are automatically generated from the node meta-information. Thus, the generated code possesses standardized variable naming, a clear hierarchical structure, and a consistent formatting style, becoming an engineering asset that can be reviewed, debugged, and edited.
[0064] In this embodiment of the invention, the receiving node graph can be implemented in the following way: obtaining serialized blueprint data (e.g., blueprint data in JSON format) describing the node graph, wherein the blueprint data includes the type, parameters, and connection relationships of each functional node and each execution pin and data pin; parsing the blueprint data to construct a memory graph structure including each functional node and its connection relationships; the subsequent depth-first traversal and the reverse dependency backtracking are both performed based on the memory graph structure.
[0065] In this embodiment of the invention, to support the above conversion process, the following core data structure is defined: A Blueprint is a project container that contains multiple EditorGraphs (function graphs) and a global variable collection. EditorGraph (Function Graph) is used to describe the logical topology of a function, including a list of nodes and connections. ExecuteReference identifies the pointer to a function call, supporting pointers to functions within a Blueprint, global variables, or external library functions. ExecuteReference is a union type, consisting of three concrete implementations, corresponding to the three types of referenced objects pointed to by the node output: (1) Function object, used for functions inside the blueprint and functions in the node library card. Its fields include function name, file path, category, display name, parameter array, real-time execution flag, pure function flag, asynchronous flag, inline source code, and the function identifier corresponding to the function in the blueprint; wherein, the existence of the function identifier indicates that the function is created in the blueprint function rather than the node card, and is also the basis for locating the target function graph and constructing the function call graph in the future; (2) Function selection value, used for external library functions, whose fields include function name, file path (in the form of "current directory / function name_version number.js"), and real-time execution flag; (3) Variable object, used for global variables, whose fields include variable identifier, variable name, type, default value, and whether it is a write operation flag.
[0066] In summary, the node graph to target language code conversion method provided in this invention uses a "data-control" dual-flow model to losslessly compile the node graph into standard source code of the target language. On one hand, it determines the main branch order through depth-first traversal driven by execution pins and determines the declaration order through stack-based reverse dependency backtracking of data pin dependencies, ensuring the correctness of the generated code in both control and data flow dimensions. On the other hand, it constructs a rigorous execution context through delayed conversion of callback functions and cross-scope variable reuse, enabling the generated code to support complex business logic such as multiple function calls, nested callbacks, and asynchronous callbacks. Simultaneously, it improves the readability and maintainability of the generated code through automatic generation of module import declarations, standardization of variable identifiers, automatic generation and on-demand output of business comments. The generated standard source code does not depend on the node graph runtime engine, can run independently without it, and can be integrated with standard engineering toolchains such as Babel, Webpack, and Rollup, achieving the integration of visual programming and standard software development processes. Compared with manually writing equivalent front-end logic code, under the same functionality, the method of this invention reduces the amount of code developed manually by more than 60% and shortens the function debugging and iteration cycle by more than 50%.
[0067] As another embodiment of the present invention, a node graph to target language code conversion system 100 is provided to implement the node graph to target language code conversion method described above, wherein, as Figure 9 As shown, it includes: The node diagram receiving module 110 is used to receive a node diagram, which includes functional nodes and execution pins and data pins connecting the functional nodes. The main execution order determination module 120 is used to perform a depth-first traversal of the functional nodes along the execution pin, starting from the function entry node in the node graph, to obtain the main execution order of the code. The declaration order determination module 130 is used to perform reverse dependency backtracking on the traversed functional nodes according to the dependency relationship represented by the data pin to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it. The callback conversion module 140 is used to delay the conversion of the callback function until the main flow conversion is completed when traversing to the target node that references the callback function, and to reuse the variables declared in the main flow when converting the callback function. The code assembly module 150 is used to generate main flow code according to the main branch order and the declaration order, and assemble the main flow code and the converted callback function code into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph execution engine.
[0068] The node graph to target language code conversion device provided by this invention obtains a clear code execution backbone order by performing a depth-first traversal along the execution pins starting from the function entry node, ensuring that the execution order of the generated code is consistent with the control flow expressed by the node graph. It determines the declaration order by performing reverse dependency backtracking based on the dependencies represented by the data pins, ensuring that the dependent functional nodes are declared before the functional nodes that depend on them, avoiding undefined variable problems caused by inconsistencies between node arrangement and declaration order. When traversing to the target node referencing the callback function, the conversion of the callback function is delayed until after the main flow conversion is completed, and variables already declared in the main flow are reused during callback function conversion. This avoids duplicate variable declarations and state pollution while ensuring that the callback function can correctly construct its execution context. By assembling the main flow code and the callback function code into standard source code of the target language, the execution of the generated standard source code does not depend on the node graph execution engine, thus enabling independent operation and integration into standard engineering toolchains, solving the problem in related technologies where the generated artifacts depend on the node graph execution engine.
[0069] The specific working principle of the node graph to target language code conversion device of the present invention can be referred to the description of the node graph to target language code conversion method above, and will not be repeated here.
[0070] As another embodiment of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, wherein the computer program, when executed by a processor, implements the node graph to target language code conversion method described above.
[0071] In this embodiment of the invention, a non-transitory computer-readable storage medium is provided. The computer-readable storage medium stores computer-executable instructions that can execute the node graph-to-target language code conversion method in any of the above method embodiments. The storage medium may be a magnetic disk, optical disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk drive (HDD), or solid-state drive (SSD), etc.; the storage medium may also include combinations of the above types of memory.
[0072] As another embodiment of the present invention, an electronic device is provided, comprising: a memory for storing a computer program; and a processor for calling and executing the computer program stored in the memory to implement the node graph to target language code conversion method described above.
[0073] like Figure 10 As shown, the electronic device 10 may include: at least one processor 11, such as a CPU (Central Processing Unit), at least one communication interface 13, a memory 14, and at least one communication bus 12. The communication bus 12 is used to enable communication between these components. The communication interface 13 may include a display screen or a keyboard; optionally, the communication interface 13 may also include a standard wired interface or a wireless interface. The memory 14 may be high-speed RAM (Random Access Memory) or non-volatile memory, such as at least one disk drive. Optionally, the memory 14 may also be at least one storage device located remotely from the aforementioned processor 11. The memory 14 stores application programs, and the processor 11 calls the program code stored in the memory 14 to execute any of the aforementioned method steps.
[0074] The communication bus 12 can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The communication bus 12 can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 10The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0075] The memory 14 may include volatile memory, such as random-access memory (RAM); the memory may also include non-volatile memory, such as flash memory, hard disk drive (HDD) or solid-state drive (SSD); the memory 14 may also include a combination of the above types of memory.
[0076] The processor 11 can be a central processing unit (CPU), a network processor (NP), or a combination of CPU and NP.
[0077] The processor 11 may further include a hardware chip. This hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0078] Optionally, memory 14 is also used to store program instructions. Processor 11 can invoke program instructions to implement the present invention. Figure 1 The node graph to target language code conversion method shown in the embodiment.
[0079] It is understood that the above embodiments are merely exemplary implementations used to illustrate the principles of the present invention, and the present invention is not limited thereto. For those skilled in the art, various modifications and improvements can be made without departing from the spirit and essence of the present invention, and these modifications and improvements are also considered to be within the scope of protection of the present invention.
Claims
1. A method for converting node graphs to target language code, characterized in that, include: A receiving node diagram, the node diagram including functional nodes and execution pins and data pins connecting the functional nodes; Starting from the function entry node in the node graph, perform a depth-first traversal of the functional nodes along the execution pin to obtain the main execution order of the code. Based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it. When traversing to the target node that references the callback function, the conversion of the callback function will be delayed until after the main flow conversion is completed, and the variables declared in the main flow will be reused when the callback function is converted. The main flow code is generated according to the main branch order and the declaration order, and the main flow code and the converted callback function code are assembled into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph execution engine.
2. The method for converting node graphs to target language code according to claim 1, characterized in that, Based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, including: Push the currently traversed functional node onto the stack, and push the preceding dependent nodes connected to the currently traversed functional node via the data pins onto the stack. The preceding dependent node is used as the new currently traversed functional node, and the above stack pushing steps are repeated until the functional node pushed onto the stack is a source node without preceding dependent nodes. Functional nodes are popped from the stack in a last-in-first-out (LIFO) order, and then generated in the code sequence according to the popped order to determine the declaration order.
3. The method for converting node graphs to target language code according to claim 2, characterized in that, Based on the dependencies represented by the data pins, reverse dependency backtracking is performed on the traversed functional nodes to determine the declaration order of each functional node, which also includes: Before pushing the preceding dependent node onto the stack, determine whether the preceding dependent node already exists in the stack; In response to the fact that the preceding dependent node already exists in the stack, it is determined that the node graph has a circular dependency.
4. The method for converting node graphs to target language code according to any one of claims 1 to 3, characterized in that, The conversion of the callback function will be delayed until after the main process conversion is completed, including: When traversing to the target node that references the callback function, write a callback placeholder in the main flow statement corresponding to the target node, and temporarily store the definition of the callback function in the callback repository; After the main process transformation is completed, the callback repository is traversed, the temporarily stored callback functions are transformed, and the callback placeholders are replaced with the transformed callback function code.
5. The method for converting node graphs to target language code according to claim 4, characterized in that, When transforming the callback function, variables declared in the main flow are reused, including: During the main process transformation, declaration statements are generated for the variables in the main process; When the callback function is transformed, in response to the callback function referencing a variable already declared in the main flow, the identifier of the declared variable is referenced in the callback function code, and the declaration statement of the declared variable is not regenerated.
6. The method for converting node graphs to target language code according to any one of claims 1 to 3, characterized in that, The main process code and the converted callback function code are assembled into code text in the target language, including: Based on the functions called by each functional node, generate a module import declaration including a module import identifier; According to the main branch order and the declaration order, the main flow code is concatenated with the converted callback function code to obtain the concatenated code; The concatenated code is combined with the module import declaration, and the entry function of the node graph is attached to the global variable object of the target language to obtain the code text of the target language.
7. The method for converting node graphs to target language code according to any one of claims 1 to 3, characterized in that, The method for converting node graphs to target language code also includes steps performed before the depth-first traversal step: Starting from the function entry node, mark the reachable functional nodes along the execution pin and the data pin; During the transformation process, transformations are not performed on functional nodes that are not marked.
8. The method for converting node graphs to target language code according to any one of claims 1 to 3, characterized in that, The node graph includes multiple function graphs, and each function graph includes a function entry node; The method for converting node graphs to target language code also includes: Taking the current function graph among the multiple function graphs as the object, perform the depth-first traversal and the reverse dependency backtracking to obtain the function code corresponding to the current function graph; In response to a function node in the current function graph referencing another function graph in the plurality of function graphs, the referenced other function graph is identified as the referenced function graph; The referenced function graph is used as a preceding dependency node for backtracking, and the above steps are recursively executed on the referenced function graph to obtain the function code corresponding to the referenced function graph.
9. A system for converting node graphs to target language code, used to implement the method for converting node graphs to target language code as described in any one of claims 1 to 8, characterized in that, include: A node graph receiving module is used to receive a node graph, which includes functional nodes and execution pins and data pins connecting the functional nodes; The main execution order determination module is used to perform a depth-first traversal of the functional nodes along the execution pin, starting from the function entry node in the node graph, to obtain the main execution order of the code. The declaration order determination module is used to perform reverse dependency backtracking on the traversed functional nodes according to the dependency relationship represented by the data pin to determine the declaration order of each functional node, wherein the declaration order is such that the dependent functional node is declared before the functional node that depends on it. The callback transformation module is used to delay the transformation of the callback function until the main process transformation is completed when traversing to the target node that references the callback function, and to reuse the variables declared in the main process when transforming the callback function; The code assembly module is used to generate main flow code according to the trunk order and the declaration order, and assemble the main flow code and the converted callback function code into code text in the target language, wherein the code text in the target language is the standard source code of the target language, and the execution of the standard source code does not depend on the node graph execution engine.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the method for converting node graphs to target language code as described in any one of claims 1 to 8.
