Method for loading and executing process by script engine and script engine
By designing a virtual machine script engine, supporting multi-process, multi-threading and mainstream protocols, the problems of high costs, functional redundancy and insufficient flexibility in automated logistics process control of PLC equipment are solved, and the effect of reducing project implementation costs and improving on-site process implementation flexibility is achieved.
Patent Information
- Application Number
- CN202411761515.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-03
- Publication Date
- 2025-03-25
AI Technical Summary
In the automated logistics process control, existing PLC equipment has problems such as high cost, functional redundancy, difficulty in implementing peripheral system protocols, and difficulty in single thread meeting multi-task concurrent processing and insufficient system flexibility.
Design a virtual machine script engine, which supports mainstream protocols, multi-process and multi-threading, and has system resource recycling functions. Through the rule engine, process files are parsed and compiled, virtual machines load symbol tables and program tables, initialize variables and process management, and execute process entities of the execution engine.
It reduces project implementation costs, improves the flexibility and scalability of on-site process implementation, supports complex application scenarios, and lowers the threshold for use through IEC61131-3 standards.
Smart Images

Figure CN119690587A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of script engines, and in particular to a method for loading and executing a process of a script engine and a script engine. Background Art
[0002] In the field of automated logistics process control, some equipment scheduling systems or simple stand-alone equipment usually use PLC or code programming to control equipment, process control and interact with peripheral systems. However, there are shortcomings in control and project implementation, as follows:
[0003] 1) The cost of PLC is too high. Some simple devices do not need to be controlled by industrial controllers, resulting in a lot of redundant functions and cost waste.
[0004] 2) The PLC’s ability to connect to peripheral systems is limited, and protocols such as webapi cannot be implemented or are extremely difficult to implement.
[0005] 3) Traditional PLCs are usually single-threaded and cannot meet the needs of multi-task concurrent processing, resulting in slow system response and low resource utilization.
[0006] 4) Some systems use code programming to implement the process and single-machine control of project implementation. This leads to insufficient system flexibility and the inability to form standardized products. In addition, the implementation of a project requires the development of some additional functions, making project version management difficult and the utilization rate of developer resources low. Summary of the invention
[0007] In the field of automated logistics process control, the existing equipment scheduling system or simple stand-alone equipment has deficiencies and defects in process control. The present invention proposes a method and a script engine involving loading and executing a process with a script engine. The engine is a virtual machine script engine that supports mainstream protocols, supports multi-process and multi-threading, and has a complete system resource recovery function. Through this engine, the flexibility of on-site process implementation is improved, making it more adaptable to complex application scenarios. The use threshold can be lowered based on the IEC61131-3 standard, aiming to reduce project implementation costs and improve the flexibility and scalability of on-site process implementation.
[0008] The present invention discloses a method for a script engine to load and execute a process, which comprises:
[0009] After the rule engine parses and compiles all the process files in the working directory to generate the grammar rule verification tree, symbol table and program table, the virtual machine loads the symbol table and program table into the system and initializes the variables in the global variable table. In the process of initializing the variables, the global symbols in the symbol table are initialized separately and added to the global variable table, and an index is created for the global variable table. After the variable initialization is completed, the virtual machine starts the process management, which is responsible for the management of the program call frame stack and the process local variable container.
[0010] Furthermore, the rule engine parses and compiles all the process files in the working directory to generate a grammar rule verification tree, a symbol table and a program table, including:
[0011] The rule engine analyzes the process file through the lexical analyzer, and translates the instructions in the process file into an instruction set executable by the virtual machine. The syntax analyzer receives the instruction set transmitted by the lexical analyzer, and executes the instruction set according to the virtual machine instruction rules to form an abstract syntax tree. The abstract syntax tree is traversed using the tree analyzer to form a syntax rule verification tree.
[0012] The rule engine parses the process file, extracts and summarizes the symbols and programs in all process files to form a symbol table and a program table.
[0013] Furthermore, when a process is triggered by an external interface, the virtual machine grabs the grammar rule verification tree corresponding to the process from the program table and creates an index for it, so that the subsequent execution engine can find the instruction set; then the virtual machine starts to initialize the process and specifies its namespace and scope; process initialization is used to create a program call frame stack and a process local variable container for the process entity.
[0014] Further, the process entity includes a program call frame, which defines control information of the program and uniquely identifies an instruction in the program table through a tree identifier, an instruction type and a source statement identifier and a program counter;
[0015] In the process entity, the program call frame is stored in the form of a stack; the process local variable container stores the value of the symbol in the process, that is, the context of program execution;
[0016] When the process entity is initialized, the process is added to the execution queue of the process management. When the process management verification is completed, the process is set to the running state.
[0017] Furthermore, when the process entity is created and set to the running state, the execution engine takes out the process entity to be executed from the process management, and the process takes out the program call frame at the top of the program call frame stack and starts execution according to the definition of the program call frame.
[0018] Furthermore, the execution engine takes out the process entity to be executed from the process management, takes out the top element in the program call frame stack, and searches for instructions in the execution environment according to the control information in the program call frame; if the instruction is an instruction to call a new program, the original program counter is incremented by one to point to the next instruction, and the original program call frame is pushed into the stack, a new program call frame is created and pushed into the stack, and the character container is switched; otherwise, it is determined whether the program counter in the original program call frame is greater than the number of instruction lines. If it is greater, it means that the program has been executed, and its return value is saved, and it is popped out of the stack, and switched to the character container of the previous calling program; if it is less than, it means that the instruction still needs to be executed downward, and the result is stored in the character container after execution, the program counter is incremented by one, and then the program call frame is pushed into the stack;
[0019] The execution engine continues to take out the process entities to be executed from the process management until the program call frame stack is empty.
[0020] The present invention also discloses a script engine, which implements the method of loading and executing a process by a script engine as described in any of the above items, and comprises a rule engine, a virtual machine and a driving unit; the rule engine is connected to the driving unit through the virtual machine; the rule engine comprises a lexical analyzer, a syntax analyzer and a tree analyzer; the virtual machine consists of a symbol table and a program table, a global variable table and a process management when the process is executed; the driving unit comprises an execution engine; the execution engine is used to read program instructions, execute instructions and process data.
[0021] Furthermore, the rule engine is used to form a grammar rule verification tree by pre-loading defined grammar rules; the rule engine is used to summarize the symbol table and program namespace in the process file to provide a static symbol table and program table for the initialization of the virtual machine.
[0022] Furthermore, the virtual machine defines all programs and characters as templates for instruction execution and data processing; it stores the context of process execution and global variables.
[0023] Furthermore, the driving unit also includes an execution engine, a coprocessor, a thread pool, a middle stage instruction set and a background method library; the execution engine is connected to the coprocessor, the thread pool, and the middle stage instruction set respectively; the middle stage instruction set is connected to the background method library;
[0024] The middle stage instruction set is the basic method library of the execution engine, which is mainly used to implement numerical operations, logical operations, coprocessor calls, thread creation and process jump instructions; the background method library is a supplement to the middle stage instruction set, which is mainly used to implement operations on complex structures; the thread pool is a management module for process creation threads, which is mainly used to manage the life cycle and execution of threads created by the process; the coprocessor is a third-party functional instruction set that separates instructions into coprocessors;
[0025] The execution engine outsources related functions to the coprocessor by asynchronously calling the coprocessor; the related functions include graphics processing, file processing, network services and external interfaces.
[0026] Due to the adoption of the above technical solution, the present invention has the following advantages:
[0027] 1. The script engine proposed in the present invention is a virtual machine script engine that supports mainstream protocols, supports multi-process and multi-threading, and has a complete system resource recovery function. The use threshold can be lowered based on the IEC61131-3 standard.
[0028] 2. The present invention reduces the project implementation cost and improves the flexibility and scalability of on-site process implementation. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the embodiments of the present invention. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.
[0030] Figure 1 A block diagram of a script engine according to an embodiment of the present invention;
[0031] Figure 2 A schematic diagram of a partial grammar rule verification tree of an embodiment of the present invention;
[0032] Figure 3 A schematic diagram of a process entity structure of an embodiment of the present invention;
[0033] Figure 4 The figure is a schematic diagram of the execution logic of the execution engine according to the embodiment of the present invention. DETAILED DESCRIPTION
[0034] The present invention is further described in conjunction with the accompanying drawings and embodiments, and the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by ordinary technicians in this field should fall within the scope of protection of the embodiments of the present invention.
[0035] In order to reduce project implementation costs and improve the flexibility and scalability of on-site process implementation, see Figure 1 The present invention provides an embodiment of a method for a script engine to load and execute a process, which includes:
[0036] After the rule engine parses all the process files in the working directory and compiles them to generate the syntax rule verification tree, symbol table and program table, the virtual machine starts the startup process, that is, the virtual machine loads the symbol table and program table into the system and initializes the variables in the global variable table; in the process of initializing the variables, the global symbols in the symbol table are initialized separately and added to the global variable table, and an index is established for the global variable table; after the variable initialization is completed, the virtual machine starts the process management, which is responsible for the management of the program call frame stack and the process local variable container.
[0037] In one embodiment of the present application, the virtual machine is the workspace of the script engine, in which all programs and characters are defined as templates for instruction execution and data processing; the context of process execution and global variables are stored.
[0038] In one embodiment of the present application, the rule engine parses and compiles all process files in the working directory to generate a grammar rule verification tree, a symbol table, and a program table, including:
[0039] The rule engine analyzes the process file through the lexical analyzer, translates the instructions in the process file into an instruction set executable by the virtual machine, and the syntax analyzer receives the instruction set transmitted by the lexical analyzer, and executes the instruction set according to the virtual machine instruction rules to form an abstract syntax tree; the abstract syntax tree is traversed by the tree analyzer to form a syntax rule verification tree; some syntax rule verification trees formed by the process file are as follows Figure 2 As shown;
[0040] The rule engine parses the process file, extracts and summarizes the symbols and programs in all process files to form a symbol table and a program table.
[0041] In one embodiment of the present application, when a process is triggered by an external interface, the virtual machine captures the grammar rule verification tree corresponding to the process from the program table and creates an index for it to facilitate the subsequent execution engine to find the instruction set; then the virtual machine starts to initialize the process and specifies its namespace and scope; process initialization is used to create a program call frame stack and a process local variable container for the process entity.
[0042] In one embodiment of the present application, see Figure 3 ,The process entity includes a program call frame that defines the ,control information of the program and uniquely identifies an instruction in the ,program table through the tree identifier, the instruction type and the source ,statement identifier and the program counter;
[0043] In the process entity, the program call frame is stored in the form of a stack; the process local variable container stores the value of the symbol in the process, that is, the context of program execution;
[0044] When the process entity is initialized, the process is added to the execution queue of the process management. When the process management verification is completed, the process is set to the running state.
[0045] In one embodiment of the present application, after the process entity is created and set to the running state, the execution engine will take out the process entity to be executed from the process management, the process will take out the program call frame at the top of the program call frame stack, and start execution according to the definition of the program call frame.
[0046] In one embodiment of the present application, Figure 4 Briefly shows the execution logic of the execution engine, including normal execution logic, online context switching when the program is called, etc. Figure 4 , the execution engine takes out the process entity to be executed from the process management, takes out the top element in the program call frame stack, and searches for instructions in the execution environment according to the control information in the program call frame; if the instruction is an instruction to call a new program, the original program counter is incremented by one to point to the next instruction, and the original program call frame is pushed into the stack, a new program call frame is created and pushed into the stack, and the character container is switched; otherwise, it is determined whether the program counter in the original program call frame is greater than the number of instruction lines. If it is greater, it means that the program has been executed, and its return value is saved, popped out of the stack, and switched to the character container of the previous calling program; if it is less than, it means that the instruction still needs to be executed downward, and the result is stored in the character container after execution, the program counter is incremented by one, and then the program call frame is pushed into the stack;
[0047] The execution engine continues to take out the process entities to be executed from the process management until the program call frame stack is empty.
[0048] The present invention also provides an embodiment of a script engine, which implements the method of the script engine loading and executing a process described in the above embodiment, and includes a rule engine, a virtual machine and a driver unit; the rule engine is connected to the driver unit through the virtual machine; the rule engine includes a lexical analyzer, a syntax analyzer and a tree analyzer; the virtual machine is composed of a symbol table and a program table, a global variable table, and process management during process execution; the driver unit includes an execution engine; the execution engine is used to read program instructions, execute instructions and process data.
[0049] Optionally, it can be a script engine based on IEC61131-3. Based on the IEC61131-3 standard, it can lower the usage threshold, support mainstream protocols, support multi-process and multi-threading, and have a virtual machine-type script engine with complete system resource recovery.
[0050] Optionally, it also includes a front-end IDE (Integrated Development Environment). The front-end is developed using the Vue framework, and its main functions include: 1) running log viewing, 2) process project management, 3) process operation management, 4) virtual machine reloading, 5) lexical analysis, 6) syntax analysis, 7) statement error prompts, automatic code completion, etc. The back-end engine is mainly divided into three parts: rule engine, virtual machine and execution engine.
[0051] Optionally, the main functions of the rule engine are process file loading, lexical analysis, syntax rule verification tree generation, static syntax checking, etc. The main functions of the virtual machine are symbol table maintenance, runtime resource management, program table management, variable table maintenance, process life cycle management, thread declaration cycle management, call stack management, etc. The main functions of the execution engine are coprocessor calling, instruction set management, background method library management, etc.
[0052] Furthermore, the rule engine is used to form a grammar rule verification tree by pre-loading defined grammar rules; the rule engine is used to summarize the symbol table and program namespace in the process file to provide a static symbol table and program table for the initialization of the virtual machine. The rule engine can form a grammar rule verification tree by pre-loading defined IEC61131-3 grammar rules.
[0053] Furthermore, the virtual machine is the workspace during the execution of the business process, which is composed of a symbol table and a program table, a global variable table, and an execution entity (process) during process execution.
[0054] Furthermore, the virtual machine defines all programs and characters as templates for instruction execution and data processing; it stores the context of process execution and global variables.
[0055] Furthermore, the execution engine is a component that reads program instructions, executes instructions, and processes data, and is the execution and control core of the business processing model. The driver unit mainly includes the execution engine, coprocessor, thread pool, middle-end instruction set, and background method library.
[0056] Furthermore, the driving unit also includes an execution engine, a coprocessor, a thread pool, a middle stage instruction set and a background method library; the execution engine is connected to the coprocessor, the thread pool, and the middle stage instruction set respectively; the middle stage instruction set is connected to the background method library;
[0057] The middle stage instruction set is the basic method library of the execution engine, which is mainly used to implement numerical operations, logical operations, coprocessor calls, thread creation and process jump instructions; the background method library is a supplement to the middle stage instruction set, which is mainly used to implement operations on complex structures; the thread pool is a management module for process creation threads, which is mainly used to manage the life cycle and execution of threads created by the process; the coprocessor is a third-party functional instruction set that separates instructions into coprocessors;
[0058] The execution engine outsources related functions to the coprocessor by asynchronously calling the coprocessor; the related functions include graphics processing, file processing, network services and external interfaces.
[0059] The execution engine is the core of the business process model driving unit. During its execution, it mainly works together with the middle-end instruction set, background method library, thread pool and coprocessor to complete the work.
[0060] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the above embodiments, ordinary technicians in the relevant field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for a script engine to load and execute a process, characterized in that: include: After the rule engine parses and compiles all the process files in the working directory to generate the grammar rule verification tree, symbol table and program table, the virtual machine loads the symbol table and program table into the system and initializes the variables in the global variable table; During the process of initializing variables, the global symbols in the symbol table are initialized separately and added to the global variable table, and an index is created for the global variable table; after the variable initialization is completed, the virtual machine starts process management, which is responsible for the management of the program call frame stack and the process local variable container.
2. The method for loading and executing a process by a script engine according to claim 1, characterized in that: The rule engine parses and compiles all process files in the working directory to generate a grammar rule verification tree, a symbol table, and a program table, including: The rule engine analyzes the process file through the lexical analyzer, and translates the instructions in the process file into an instruction set executable by the virtual machine. The syntax analyzer receives the instruction set transmitted by the lexical analyzer, and executes the instruction set according to the virtual machine instruction rules to form an abstract syntax tree. The abstract syntax tree is traversed using the tree analyzer to form a syntax rule verification tree. The rule engine parses the process file, extracts and summarizes the symbols and programs in all process files to form a symbol table and a program table.
3. The method for loading and executing a process by a script engine according to claim 1, characterized in that: When a process is triggered by an external interface, the virtual machine grabs the syntax rule verification tree corresponding to the process from the program table and creates an index for it, so that the subsequent execution engine can find the instruction set; then the virtual machine starts to initialize the process and specifies its namespace and scope; process initialization is used to create a program call frame stack and process local variable container for the process entity.
4. The method for loading and executing a process by a script engine according to claim 1, characterized in that: The process entity includes a program call frame, which defines the control information of the program and uniquely identifies an instruction in the program table through a tree identifier, an instruction type and a source statement identifier and a program counter; In the process entity, the program call frame is stored in the form of a stack; the process local variable container stores the value of the symbol in the process, that is, the context of program execution; When the process entity is initialized, the process is added to the execution queue of the process management. When the process management verification is completed, the process is set to the running state.
5. The method for loading and executing a process by a script engine according to claim 4, characterized in that: When the process entity is created and set to the running state, the execution engine will take out the process entity to be executed from the process management, the process will take out the program call frame at the top of the program call frame stack, and start execution according to the definition of the program call frame.
6. The method for loading and executing a process by a script engine according to claim 1, characterized in that: The execution engine takes out the process entity to be executed from the process management, takes out the top element in the program call frame stack, and searches for instructions in the execution environment according to the control information in the program call frame; if the instruction is an instruction to call a new program, the original program counter is incremented by one to point to the next instruction, and the original program call frame is pushed into the stack, a new program call frame is created and pushed into the stack, and the character container is switched; otherwise, it is determined whether the program counter in the original program call frame is greater than the number of instruction lines. If it is greater, it means that the program has been executed, and its return value is saved, popped out of the stack, and switched to the character container of the previous calling program; if it is less than, it means that the instruction still needs to be executed downward, and the result is stored in the character container after execution, the program counter is incremented by one, and then the program call frame is pushed into the stack; The execution engine continues to take out the process entities to be executed from the process management until the program call frame stack is empty.
7. A script engine, implementing the method of loading and executing a process in a script engine according to any one of claims 1 to 6, characterized in that: It includes a rule engine, a virtual machine and a driver unit; the rule engine is connected to the driver unit through the virtual machine; the rule engine includes a lexical analyzer, a syntax analyzer and a tree analyzer; The virtual machine consists of a symbol table and a program table, a global variable table, and process management during process execution; the driver unit includes an execution engine; the execution engine is used to read program instructions, execute instructions, and process data.
8. The script engine according to claim 7, characterized in that: The rule engine is used to form a grammar rule verification tree by pre-loading defined grammar rules; the rule engine is used to summarize the symbol table and program namespace in the process file to provide a static symbol table and program table for the initialization of the virtual machine.
9. The script engine according to claim 7, characterized in that: The virtual machine defines all programs and characters as templates for instruction execution and data processing; it stores the context of process execution and global variables.
10. The script engine according to claim 7, characterized in that: The driver unit also includes an execution engine, a coprocessor, a thread pool, a middle-stage instruction set, and a background method library; the execution engine is connected to the coprocessor, the thread pool, and the middle-stage instruction set respectively; the middle-stage instruction set is connected to the background method library; The middle stage instruction set is the basic method library of the execution engine, which is mainly used to implement numerical operations, logical operations, coprocessor calls, thread creation and process jump instructions; the background method library is a supplement to the middle stage instruction set, which is mainly used to implement operations on complex structures; the thread pool is a management module for process creation threads, which is mainly used to manage the life cycle and execution of threads created by the process; the coprocessor is a third-party functional instruction set that separates instructions into coprocessors; The execution engine outsources related functions to the coprocessor by asynchronously calling the coprocessor; the related functions include graphics processing, file processing, network services and external interfaces.