CN113760403A - State machine linkage method and device - Google Patents
State machine linkage method and device Download PDFInfo
- Publication number
- CN113760403A CN113760403A CN202110089376.4A CN202110089376A CN113760403A CN 113760403 A CN113760403 A CN 113760403A CN 202110089376 A CN202110089376 A CN 202110089376A CN 113760403 A CN113760403 A CN 113760403A
- Authority
- CN
- China
- Prior art keywords
- state
- state machine
- machine instance
- state transition
- action
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/44—Arrangements for executing specific programs
- G06F9/448—Execution paradigms, e.g. implementations of programming paradigms
- G06F9/4498—Finite state machines
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
- G06F9/46—Multiprogramming arrangements
- G06F9/54—Interprogram communication
- G06F9/542—Event management; Broadcasting; Multicasting; Notifications
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Multimedia (AREA)
- Stored Programmes (AREA)
Abstract
The invention discloses a state machine linkage method and device, and relates to the technical field of computers. One embodiment of the method comprises: receiving a first state transition message directed to a first state machine instance; the first state transition message carries a service object identifier corresponding to the first state machine instance and a first event; carrying out state transition on a first state machine instance according to a first event, executing a preset action corresponding to the state transition, and changing the state of a business object based on the action and a business object identifier; causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action; the second state transition message carries a service object identifier corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition. This embodiment enables state transition linkage between different state machine instances.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a state machine linkage method and device.
Background
A Finite State Machine (FSM), referred to as a State Machine, is a mathematical model representing Finite states and the behavior of transitions, actions, etc. between these states. Typically, finite state machines control state transitions through relationships between states, events, and actions. The state transition in the service is processed by utilizing the state machine model, the state flow process can be clearly seen in a state topological graph mode, and excessive and complicated if/else logic control statements in the code can be avoided, so that the code is easy to maintain.
The existing state machine implementation method generally comprises the following procedures: firstly, defining all states related in a business process as states of a state machine; and then, determining the current state of the state machine, determining the next state and the corresponding action according to the current state of the state machine and the received event, controlling the state machine to jump from the current state to the next state according to the information, and executing the corresponding action.
In the process of implementing the invention, the inventor finds that the prior art has at least the following problems: the existing state machine implementation process generally carries out program design based on a set of state transition topology, and can not meet the condition of state transition linkage of different state machines. For example, a state transition of an item may cause the state of the associated item to transition as well. In addition, for a scene with a large amount of business object data, the prior art needs to frequently create a state machine, thereby excessively consuming system resources.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method and an apparatus for linking state machines, which can implement state transition linkage between different state machine instances.
To achieve the above object, according to one aspect of the present invention, a state machine linkage method is provided.
The state machine linkage method of the embodiment of the invention comprises the following steps: receiving a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition; carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message; causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
Optionally, the actions include: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to a state transition; and, the executing of the preset action corresponding to the state transition includes: after receiving the first state transition message, executing an entering method and a first public action defined by a message interceptor corresponding to the first event; after the first state machine instance makes a state transition, a second common action, the particular action, and the message interceptor defined exit method are performed.
Optionally, the specific action includes: a first method for changing a state of a business object; and changing the state of the service object based on the action and the service object identifier carried in the first state transition message, including: determining a corresponding service object by using the service object identifier carried in the first state transition message; a first method is performed to alter the state of the determined business object.
Optionally, the specific action includes: a second method for sending a state transition message; and causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action, comprising: the second method is executed to cause the first state machine instance to send a second state transition message to the second state machine instance.
Optionally, any one of the first state machine instance and the second state machine instance is obtained according to the following steps: judging whether state machine data corresponding to the business object exists in a preset persistent storage unit or not: and if so, deserializing the state machine data into the memory to form a state machine instance.
Optionally, any one of the first state machine instance and the second state machine instance is obtained according to the following steps: judging whether a state machine instance corresponding to the business object exists in an object pool arranged in the memory: if yes, calling the state machine instance from the object pool, and adjusting the current state of the state machine instance to be a state consistent with the current state of the business object; and the state machine linkage method further comprises the following steps: after the use of the state machine instance is completed, the state machine instance is returned to the object pool.
To achieve the above objects, according to another aspect of the present invention, there is provided a state machine linkage.
The state machine linkage of an embodiment of the present invention may include: a receiving unit configured to receive a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition; a business object change unit for: carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message; a linkage unit for: causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
Optionally, the actions include: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to a state transition; and the business object changing unit is further used for: after receiving the first state transition message, executing an entering method and a first public action defined by a message interceptor corresponding to the first event; after the first state machine instance makes a state transition, a second common action, the particular action, and the message interceptor defined exit method are performed.
To achieve the above object, according to still another aspect of the present invention, there is provided an electronic apparatus.
An electronic device of the present invention includes: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors realize the state machine linkage method provided by the invention.
To achieve the above object, according to still another aspect of the present invention, there is provided a computer-readable storage medium.
A computer-readable storage medium of the present invention has stored thereon a computer program which, when executed by a processor, implements the state machine linkage method provided by the present invention.
According to the technical scheme of the invention, the embodiment of the invention has the following advantages or beneficial effects: the method comprises the steps that a message encapsulation and sending function is set in a state machine instance in advance, when a state machine instance receives a state transition message and then performs state transition, after a preset action corresponding to the state transition is executed to change the state of a corresponding service object, the preset action is executed to enable the state machine instance to send another state transition message to a state machine instance associated with the state machine instance, so that the associated state machine instance also performs the state transition, the state transition linkage of any plurality of state machine instances is realized, and the service scene of the state transition linkage between service objects is met. In addition, when obtaining the state machine instance, the embodiment of the present invention may, in addition to the direct creation mode, use the state machine data pre-stored in the persistent storage Unit and corresponding to the service object one to one, deserialize the state machine data to form the state machine instance, or call the state machine instance pre-stored in the memory object pool, and adjust the current state of the state machine instance and the current state of the service object to be consistent, so as to form the required state machine instance.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a schematic diagram of the main steps of a method for linking state machines according to an embodiment of the present invention;
FIG. 2 is a block diagram illustrating an overall state transition of an example state machine according to an embodiment of the present invention;
FIG. 3 is a diagram of a UML (Unified Modeling Language) created by a state machine instance in an embodiment of the present invention;
FIG. 4 is a diagram illustrating steps in an embodiment of a state machine to receive an event;
FIG. 5 is a diagram illustrating a state transition flow of an example state machine according to an embodiment of the present invention;
FIG. 6 is a schematic diagram of an example of a state machine in accordance with an embodiment of the present invention;
FIG. 7 is a schematic diagram of the components of the state machine linkage in an embodiment of the present invention;
FIG. 8 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
fig. 9 is a schematic structural diagram of an electronic device for implementing the state machine linkage method in the embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
It should be noted that the embodiments of the present invention and the technical features of the embodiments may be combined with each other without conflict.
FIG. 1 is a schematic diagram illustrating the main steps of a method for linking state machines according to an embodiment of the present invention.
As shown in fig. 1, the state machine linkage method according to the embodiment of the present invention may be specifically executed according to the following steps:
step S101: a first state transition message directed to a first state machine instance is received.
In this step, the server storing the plurality of state machine instances receives a first state transition message, where the first state transition message is directed to the first state machine instance, that is, the first state transition message is sent to the first state machine instance by a message sender. The first state transition message may carry an identifier of a service object corresponding to the first state machine instance, and a first event for indicating the first state machine instance to perform state transition. The first state machine instance can be any state machine instance stored by the server; the service object identifier carried in the first state transition message can be used for determining a service object corresponding to the first state machine; the first event may implement some manner of state transition of the first state machine instance in accordance with preset transition logic. The concept associated with a finite state machine is first described below.
In the embodiment of the present invention, the finite State machine may control the transition of the State through the relationship among the State (State), the Event (Event) and the Action (Action), wherein the State refers to a condition of the object in the life cycle, and the object in a certain State necessarily satisfies certain conditions, performs certain actions or waits for certain events. Events occupy certain positions in time and space and are of particular interest for finite state machines (state machines for short), which typically cause state transitions that cause the state machine to transition from one state to another. An action refers to an atomic operation that can be performed in the state machine (i.e., an operation that is not interrupted by the thread scheduling mechanism).
In practical applications, different business objects (e.g., orders, projects, etc.) may be abstracted into corresponding state machine instances, for example, in the field of project management, a main project is a project in a longer period of time, and includes a plurality of sub-projects, and the main project and the sub-projects may be used as business objects associated with each other. The main project may be abstracted to a state machine instance 1 with states a, b, c, the sub-project may be abstracted to a state machine instance 2 with states a, b, d, which may correspond to actual states waiting for, in, or completed bidding. The two state machine instances have different state transition topologies (the state transition topologies include all states of the state machine instances, all state transition manners, and events, actions, etc. corresponding to the state transition manners, which can be described by using a diagram or a table), and the following table is the state transition topology of the state machine instance 1 corresponding to the main item, where the first column represents the previous state and the first row represents the next state.
From the above table, when an event1 occurs, state machine instance 1 remains in state a, action1 is performed; when an event2 occurs, state machine instance 1 is triggered to transition from state a to state b and action2 is performed; when an event3 occurs, state machine instance 1 is triggered to transition from state a to state c and action3 is performed. In a specific application, one event can also bind a plurality of state transition modes, and when a certain event is received, a corresponding state transition mode can be determined from the state transition modes bound by the event according to the current state of the state machine instance.
In an actual scenario, the state transition of the main item and the sub item often has a linkage characteristic, for example, when the main item is transitioned from the state a to the state b, the sub item also needs to be transitioned from the state a to the state b, and therefore, the corresponding state machine instance 1 and state machine instance 2 also need to simulate the characteristic. In the prior art, the state transition logic can only be realized based on a single state machine instance, and the linkage of state transition between different state machine instances cannot be executed.
In step S101, the server receives a first state transition message sent by a client, where the client refers to a message sender, and the client may be located on the same system or the same host as the server, or may be located on a different system or host from the server.
Step S102: and carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message.
In this step, after receiving a first state transition message directed to a first state machine instance, the server performs corresponding state transition on the first state machine instance according to a first event carried in the first state transition message, and executes a preset action corresponding to the state transition before, after, or at the same time as the state transition. Preferably, in an embodiment of the present invention, the preset operation includes: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to the state transition. The first common action and the second common action are actions of each state transition mode, for example, each state transition mode (including transition from state a to state b, transition from state b to state c, transition from state c to state b, and the like) of the state machine example 1 in the above example has the same first common action and second common action. The specific action refers to an action corresponding to only the current state transition manner, as opposed to a common action, and the specific action corresponding to each state transition manner is generally different.
In this way, the specific manner of executing the preset action in step S102 may be as follows: after receiving the first state transition message, the server executes a first public action; the second common action and the particular action are performed after the first state machine instance makes a state transition corresponding to the first time. After the example is carried out, the server executes a first common action "calculate item completion" in the state machine instance 1 after receiving a first event "reach bid deadline" (the first event corresponds to a state transition mode in which the state machine instance 1 transitions from the state a to the state b); after the state transition of state machine instance 1, the second common action "get current project data" and the specific action "request algorithm service to perform the relevant calculation corresponding to state b" are performed.
As a preferred solution, the embodiment of the present invention may also pre-configure a message interceptor corresponding to each event, so as to execute a method defined by the message interceptor in advance after receiving the message and the event, so as to implement a customized computational logic. After configuring the message interceptor, the specific manner of executing the preset action in step S102 may be as follows: after receiving the first state transition message, the server executes an entry method and a first public action defined by a message interceptor corresponding to the first event; and after the first state machine instance carries out state transition, executing the second public action, the specific action and the exit method defined by the message interceptor in sequence. Taking the above example, the entry method defined by the message interceptor may be "recording the system log and the current state of the project", and the exit method defined by the message interceptor may be "obtaining the request algorithm parameters".
In step S102, after the first state machine instance performs the state transition, the server may further change the state of the service object based on the preset action and the service object identifier carried in the first state transition message. Specifically, the specific action may include a first method for changing a state of the service object, and the server may first determine the corresponding service object by using the service object identifier carried in the first state transition message, and then correspondingly change the state of the determined service object by executing the first method. In practical applications, specific data of the business object can be stored in the database, so that the operation of changing the business object can be executed in the database. It will be appreciated that this manner of alteration is consistent with the manner of state transition of the first state machine instance. Through the arrangement, the embodiment of the invention can realize the automatic change of the business object state responding to the external event based on the finite state machine.
Step S103: causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition.
In the embodiment of the present invention, a client function, that is, a message encapsulation and sending function, may be added to each state machine instance in advance, so that when the server executes the preset action, the first state machine instance can send the second state transition message to the second state machine instance associated with the first state machine instance, so as to make the second state machine instance perform state transition, thereby implementing state transition linkage between the first state machine instance and the second state machine instance. The data in the second state transition message may include, similar to the first state transition message, an identifier of a business object corresponding to the second state machine instance and a second event for instructing the second state machine instance to perform a state transition.
Specifically, the specific action may include a second method for sending a state transition message, and the manner of executing the state transition linkage may be: and the server executes a second method in the specific action, so that the first state machine instance sends a second state transition message to the second state machine instance, after the second state machine instance receives the second state transition message, the second state machine instance performs corresponding state transition based on a second event carried in the second state transition message, determines a corresponding service object according to the service object identifier in the second state transition message, and executes a preset action in the database to change the state of the service object. It can be understood that the second state machine instance may also send a state transition message to a third state machine instance associated with the second state machine instance in a manner similar to the first state machine instance, so that the third state machine instance may perform a state transition, and the third state machine instance may send a state transition message to a fourth state machine instance associated therewith after performing the state transition, so that the fourth state machine instance may perform the state transition … …, so that the state transition linkage of any plurality of state machine instances may be implemented, thereby overcoming the defect in the prior art that only a single state machine instance may be programmed.
It can be understood that step S103 is executed after "performing state transition on the first state machine instance according to the first event" in step S102, and may be executed before, after, or simultaneously with "changing the state of the service object based on the action and the service object identifier carried in the first state transition message" in step S102.
In the embodiment of the invention, a new state machine instance obtaining method is also provided to solve the problems existing in the existing state machine instance obtaining method. In the prior art, when a state machine instance corresponding to a service object needs to be acquired, a mode of directly creating the state machine instance is generally adopted, and the state machine instance is directly destroyed after being used, so that large memory, CPU and network overheads are caused. The invention provides the following two state machine instance acquisition modes.
Firstly, after the use of the state machine instance is finished, the state machine data (namely the data formed by serializing the state machine instance and keeping the state machine data consistent with the current state of the service object) corresponding to the service object one by one is stored in a preset persistent storage unit (such as a cache or a hard disk); when a state machine instance corresponding to a business object needs to be acquired, whether state machine data corresponding to the business object exists in a persistent storage unit is judged (it can be understood that each state machine instance can be bound with the corresponding business object by configuring a business object identifier when being created, the judgment can be realized by detecting the business object identifier in the state machine data), and if the state machine data corresponding to the business object exists in the persistent storage unit, the state machine data can be deserialized into a memory, so that the state machine instance is formed. In the field of computer technology, serialization refers to the process of converting a data object into a byte sequence, and deserialization refers to the process of recovering a byte sequence into a data object.
Second, after the state machine instance is created during a historical period, the state machine instance may be stored in an object pool in memory. Generally, the state machine instances stored in the object pool are in an initial state (i.e., the state machine instances are in after the state machine instances are created), and multiplexing can be provided for the business objects. When a state machine instance corresponding to a business object needs to be acquired, it may be first determined whether a state machine instance corresponding to the business object exists in the object pool: if yes, calling the state machine instance from the object pool, and adjusting the current state of the state machine instance to be the state consistent with the current state of the business object, so that the available state machine instance can be obtained. In the above manner, after the use of the state machine instance is completed, the state machine instance may be returned to the object pool.
The two state machine acquisition modes can eliminate huge expenses of a memory, a CPU and a network caused by frequently creating and destroying the state machine examples, and the system pressure is reduced.
The technical solution of the present invention will be further explained below according to a specific embodiment.
The embodiment is applied to project management scenes, and the state transition of each project is controlled by establishing a state machine. Specifically, the items have main and sub relationships, and the main item and the sub item have respective states, events, state transition topologies and actions executed before and after the state transition. Further, the state transition topology of the main item and the sub item has a linkage relationship, and when a certain state transition of the main item occurs, the state transition of the sub item can be caused, and vice versa. The current state machine implementation mode cannot meet the service scene of linkage among the plurality of sets of state transition topologies. Fig. 2 shows the overall flow of state transition of the state machine instance in the present embodiment.
The following configuration is required in advance before the state transition of the business object is performed using the state machine instance.
Configuration 1: and defining the state and event enumeration of the state machine instance according to the service scene, and binding the state machine instance with a corresponding service object identifier.
Configuration 2: and constructing a state machine creating module, wherein the state machine creating module adopts a design mode of a factory mode and a strategy mode, and the structure is shown in FIG. 3. Wherein, each statemechine builder maintains a separate set of state transition topology, and the state transition topology may include: various states of the state machine, state transition modes and corresponding specific actions after the state machine receives events, common actions of the state machine entering any state or exiting any state, and judgment conditions for the state machine to transition from one state to another state after receiving the events (according to the judgment conditions, whether the state machine instance enters a certain state or stays in an original state can be judged, and the judgment conditions can also not be configured). In fig. 3, different statemechine builder classes have the same interface method build (), and executing the method can configure and instantiate the data contained in the state transition topology as attributes. When the Spring container is started, each statemachien builder is instantiated and then injected into the Spring container, and then a state transition topology type is acquired by introducing a self-defined factory statemachien factory into the state transition topology type (a getpointer (type) method can be used for acquiring the state transition topology type, and different state transition topologies belong to different state transition topology types), and the corresponding statemachien builder is acquired from the Spring container. The StatEmachieneFactor is exposed to a create () interface of the client, the StatEmachienBuilder object can be obtained from the Spring container based on the state transition topology type by using the interface, and then the build () method of the StatEmachienBuilder object is called, so that the configuration and instantiation of the state machine are completed.
Configuration 3: a message interceptor corresponding to the event is configured and corresponding entry methods and exit methods are defined.
Configuration 4: and constructing a state machine persistence module for storing and acquiring the state machine instance. In this embodiment, each business object may correspond to one state machine instance, and when a business object enters any state, the state machine instance may be stored in the persistent storage unit, where the states of the business object and the state machine instance are consistent. When the business object is called again, the state machine instance can be restored directly from the persistent storage unit instead of the process of recreating the state machine, thereby quickly obtaining a usable state machine instance.
The overall flow of state transitions for the state machine example is described below in conjunction with fig. 2.
Firstly, business data such as business objects and business types are input, and whether the state machine instance needs to be persisted in the subsequent process is judged. If the state machine data exists, the state machine data is deserialized into the state machine instance to be directly used; if not, a state machine instance is created using a state machine creation module.
And if the state machine instance does not need to be persisted in the subsequent flow, further judging whether the object pool is maintained. If the object pool exists, acquiring a state machine instance corresponding to the service object from the object pool and executing state matching; if the object pool does not exist, a state machine instance is created using a state machine creation module. The state matching means: and for the state machine instance created by using the state machine creation module and the state machine instance acquired from the object pool, adjusting the current state of the state machine instance to be the state consistent with the current state of the business object, and obtaining the available state machine instance after state matching.
Thereafter, the state machine instance may make state transitions based on the event signals sent by the client. After the state machine instance is used, the state machine persistence module can be used to store the state machine instance in a persistent storage unit for subsequent use. If the previous state machine instance is obtained from the object pool, the state machine instance needs to be returned to the object pool after the state machine instance is used up.
In the above flow, the state machine instance is maintained and acquired by using the persistent storage unit and the object pool, so that the state machine instance can be effectively prevented from being repeatedly created and destroyed. The persistence mode can quickly recover the available state machine instances with the state during storage, the state machine instances in the object pool provide multiplexing for each business object, and a user can obtain the state machine instances with the initial state from the object pool and return the state machine instances to the object pool instead of directly destroying the state machine instances after work is completed. Based on the two modes, the state transition topology, the corresponding action, the judgment condition and the like maintained in the state machine instance can be quickly recovered, when a large number of service objects exist in a service scene and need to be processed concurrently, the two modes can be adopted to avoid the phenomenon that the memory is increased rapidly due to the fact that too many objects are created, and the system stability is effectively guaranteed.
In this embodiment, after obtaining an available state machine instance, the client may send an event to the state machine instance. The state machine instance switches to the next state according to the event and state transition mode bound in the state transition topology and executes the related action. Fig. 4 is a schematic diagram of a step in which a state machine instance receives an event in the embodiment of the present invention, and as shown in fig. 4, event and service object data (including a service object identifier) are integrally encapsulated into a message body structure (i.e., a state transition message) and are transmitted to the state machine instance, and the state machine instance receives the state transition message and then places the event carried in the state transition message into a message queue. The state machine instance may then consume (may be committed to an event processing thread for processing) the messages in the message queue in a synchronous threaded or asynchronous threaded manner, depending on the complexity and execution time of the execution state transition method. The whole consumption process is shown in fig. 5.
Referring to fig. 5, first, according to the message interceptor in configuration 3, an entry method preTransition () defined by the message interceptor is performed. Then, the state machine instance executes a preset transition condition judgment method guard () to perform state transition evaluation, if the method returns to true, the evaluation is passed, and the state transition process is continuously executed. And if the evaluation result is false, exiting the state transition process. Thereafter, the state machine instance obtains, according to the data in configuration 2, a first public action, afterstattainaction (), which exits the previous state, a second public action, beforeStateAction (), which enters the next state, and a specific action () corresponding to the current state transition, and executes the afterstattainaction () before the state transition, and executes the beforeStateAction () and the action () in sequence after the state transition, where the action () may include the aforementioned first method and second method. The state of the business object in the database can be changed by executing the first method, so that the business object state is consistent with the current state of the state machine instance, and the first state machine instance can send a second state transition message to the second state machine instance by executing the second method, so that the state transition linkage of different state machine instances is realized. Finally, the exit method postTransition () defined by the message interceptor is executed.
Fig. 6 shows the linkage of the state machine example in the present embodiment. As shown in FIG. 6, each state machine instance holds a corresponding business object internally and in a certain state. When the state machine instance A receives the event E1 sent by the client, the embedded message encapsulation and sending function is used for sending the event E2 to the state machine B after the corresponding state transition (from the state S1 to the state S2) is executed and the state of the business object is changed, and when the state machine instance B receives the event E2, the event E3 … … is sent to the state machine C after the corresponding state transition (from the state S3 to the state S4) is executed and the state of the business object is changed, so that the state transition linkage of a plurality of state machine instances with the incidence relation is realized.
In the technical scheme of the embodiment of the invention, a message encapsulation and sending function is preset in the state machine instance, when a state transition message is received by a certain state machine instance and then the state transition is carried out, the preset action corresponding to the state transition is executed to change the state of the corresponding service object, and the preset action is executed to enable the state machine instance to send another state transition message to the state machine instance associated with the state machine instance, so that the associated state machine instance also executes the state transition, thereby realizing the state transition linkage of any plurality of state machine instances and meeting the service scene of the state transition linkage between service objects. In addition, when the state machine instance is obtained, in addition to the direct creation mode, the embodiment of the present invention may also use the state machine data pre-stored in the persistent storage unit and corresponding to the service object one to one, deserialize the state machine data to form the state machine instance, or call the state machine instance pre-stored in the memory object pool, and adjust the current state of the state machine instance and the current state of the service object to be consistent, so as to form the required state machine instance.
It should be understood that the above examples are only for illustrating the technical solution of the present invention, and do not necessarily correspond to the actual scene strictly.
It should be noted that, for the convenience of description, the foregoing method embodiments are described as a series of acts, but those skilled in the art will appreciate that the present invention is not limited by the order of acts described, and that some steps may in fact be performed in other orders or concurrently. Moreover, those skilled in the art will appreciate that the embodiments described in the specification are presently preferred and that no acts or modules are necessarily required to implement the invention.
To facilitate a better implementation of the above-described aspects of embodiments of the present invention, the following also provides relevant means for implementing the above-described aspects.
Referring to fig. 5, a state machine linkage device 700 according to an embodiment of the present invention may include: receiving section 701, service object changing section 702, and interlocking section 703.
Wherein the receiving unit 701 is operable to receive a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition; the business object changing unit 702 may be configured to: carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message; the linkage unit 703 may be used to: causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
In an embodiment of the present invention, the actions may include: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to a state transition; and, the business object changing unit 702 may be further configured to: after receiving the first state transition message, executing an entering method and a first public action defined by a message interceptor corresponding to the first event; after the first state machine instance makes a state transition, a second common action, the particular action, and the message interceptor defined exit method are performed.
As a preferred scheme, the specific action may include: a first method for changing a state of a business object; and, the business object changing unit 702 may be further configured to: determining a corresponding service object by using the service object identifier carried in the first state transition message; a first method is performed to alter the state of the determined business object.
Preferably, the specific action may include: a second method for sending a state transition message; and, the linkage unit 703 may be further configured to: the second method is executed to cause the first state machine instance to send a second state transition message to the second state machine instance.
In practical applications, the apparatus 700 may further include a first state machine instance recovery unit operable to: judging whether state machine data corresponding to the business object exists in a preset persistent storage unit or not: and if so, deserializing the state machine data into the memory to form a state machine instance.
Furthermore, in an embodiment of the present invention, the apparatus 700 may further include a second state machine instance recovery unit, which may be configured to: judging whether a state machine instance corresponding to the business object exists in an object pool arranged in the memory: if yes, calling the state machine instance from the object pool, and adjusting the current state of the state machine instance to be a state consistent with the current state of the business object; and, the apparatus 700 may further comprise a state machine instance return unit operable to: after the use of the state machine instance is completed, the state machine instance is returned to the object pool.
In the technical scheme of the embodiment of the invention, a message encapsulation and sending function is preset in the state machine instance, when a state transition message is received by a certain state machine instance and then the state transition is carried out, the preset action corresponding to the state transition is executed to change the state of the corresponding service object, and the preset action is executed to enable the state machine instance to send another state transition message to the state machine instance associated with the state machine instance, so that the associated state machine instance also executes the state transition, thereby realizing the state transition linkage of any plurality of state machine instances and meeting the service scene of the state transition linkage between service objects. In addition, when the state machine instance is obtained, in addition to the direct creation mode, the embodiment of the present invention may also use the state machine data pre-stored in the persistent storage unit and corresponding to the service object one to one, deserialize the state machine data to form the state machine instance, or call the state machine instance pre-stored in the memory object pool, and adjust the current state of the state machine instance and the current state of the service object to be consistent, so as to form the required state machine instance.
Fig. 8 illustrates an exemplary system architecture 800 for a state machine linkage method or state machine linkage to which embodiments of the present invention may be applied.
As shown in fig. 8, the system architecture 800 may include terminal devices 801, 802, 803, a network 804 and a server 805 (this architecture is merely an example, and the components included in a particular architecture may be adapted according to the application specific circumstances). The network 804 serves to provide a medium for communication links between the terminal devices 801, 802, 803 and the server 805. Network 804 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
A user may use the terminal devices 801, 802, 803 to interact with a server 805 over a network 804 to receive or send messages or the like. Various client applications, such as messaging applications (for example only), may be installed on the terminal devices 801, 802, 803.
The terminal devices 801, 802, 803 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 805 may be a server providing various services, such as a calculation server (for example only) providing support for messaging applications operated by users using the terminal devices 801, 802, 803. The calculation server can receive the state transition messages sent by the terminal devices 801, 802 and 803 and carry out state transition on the state machine instances arranged in the calculation server according to the state transition messages.
It should be noted that the state machine linkage method provided by the embodiment of the present invention is generally executed by the server 805, and accordingly, the state machine linkage device is generally disposed in the server 805.
It should be understood that the number of terminal devices, networks, and servers in fig. 8 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
The invention also provides the electronic equipment. The electronic device of the embodiment of the invention comprises: one or more processors; and the storage device is used for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors realize the state machine linkage method provided by the invention.
Referring now to FIG. 9, shown is a block diagram of a computer system 900 suitable for use in implementing an electronic device of an embodiment of the present invention. The electronic device shown in fig. 9 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 9, the computer system 900 includes a Central Processing Unit (CPU)901 that can perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)902 or a program loaded from a storage section 908 into a Random Access Memory (RAM) 903. In the RAM903, various programs and data necessary for the operation of the computer system 900 are also stored. The CPU901, ROM 902, and RAM903 are connected to each other via a bus 904. An input/output (I/O) interface 905 is also connected to bus 904.
The following components are connected to the I/O interface 905: an input portion 906 including a keyboard, a mouse, and the like; an output section 907 including components such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 908 including a hard disk and the like; and a communication section 909 including a network interface card such as a LAN card, a modem, or the like. The communication section 909 performs communication processing via a network such as the internet. The drive 910 is also connected to the I/O interface 905 as necessary. A removable medium 911 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 910 as necessary, so that a computer program read out therefrom is mounted into the storage section 908 as necessary.
In particular, the processes described in the main step diagrams above may be implemented as computer software programs, according to embodiments of the present disclosure. For example, embodiments of the invention include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated in the main step diagram. In the above-described embodiment, the computer program can be downloaded and installed from the network via the communication section 909, and/or installed from the removable medium 911. The computer program, when executed by the central processing unit 901, performs the above-described functions defined in the system of the present invention.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present invention may be implemented by software or hardware. The described units may also be provided in a processor, and may be described as: a processor includes a receiving unit, a business object changing unit, and a linkage unit. Where the names of these elements do not in some cases constitute a limitation of the element itself, for example, a receiving element may also be described as an "element providing a state transition message to a business object change element".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by the apparatus, cause the apparatus to perform steps comprising: receiving a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition; carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message; causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
In the technical scheme of the embodiment of the invention, a message encapsulation and sending function is preset in the state machine instance, when a state transition message is received by a certain state machine instance and then the state transition is carried out, the preset action corresponding to the state transition is executed to change the state of the corresponding service object, and the preset action is executed to enable the state machine instance to send another state transition message to the state machine instance associated with the state machine instance, so that the associated state machine instance also executes the state transition, thereby realizing the state transition linkage of any plurality of state machine instances and meeting the service scene of the state transition linkage between service objects. In addition, when the state machine instance is obtained, in addition to the direct creation mode, the embodiment of the present invention may also use the state machine data pre-stored in the persistent storage unit and corresponding to the service object one to one, deserialize the state machine data to form the state machine instance, or call the state machine instance pre-stored in the memory object pool, and adjust the current state of the state machine instance and the current state of the service object to be consistent, so as to form the required state machine instance.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims (10)
1. A state machine linkage method, comprising:
receiving a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition;
carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message;
causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
2. The state machine linkage method of claim 1, wherein the action comprises: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to a state transition; and, the executing of the preset action corresponding to the state transition includes:
after receiving the first state transition message, executing an entering method and a first public action defined by a message interceptor corresponding to the first event;
after the first state machine instance makes a state transition, a second common action, the particular action, and the message interceptor defined exit method are performed.
3. The method of claim 2, wherein the specific action comprises: a first method for changing a state of a business object; and changing the state of the service object based on the action and the service object identifier carried in the first state transition message, including:
determining a corresponding service object by using the service object identifier carried in the first state transition message;
a first method is performed to alter the state of the determined business object.
4. The method of claim 2, wherein the specific action comprises: a second method for sending a state transition message; and causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action, comprising:
the second method is executed to cause the first state machine instance to send a second state transition message to the second state machine instance.
5. The state machine linkage method according to any of claims 1-4, wherein any of the first state machine instance and the second state machine instance is obtained according to the following steps:
judging whether state machine data corresponding to the business object exists in a preset persistent storage unit or not: and if so, deserializing the state machine data into the memory to form a state machine instance.
6. The state machine linkage method according to any of claims 1-4, wherein any of the first state machine instance and the second state machine instance is obtained according to the following steps: judging whether a state machine instance corresponding to the business object exists in an object pool arranged in the memory: if yes, calling the state machine instance from the object pool, and adjusting the current state of the state machine instance to be a state consistent with the current state of the business object; and the state machine linkage method further comprises the following steps:
after the use of the state machine instance is completed, the state machine instance is returned to the object pool.
7. A state machine linkage, comprising:
a receiving unit configured to receive a first state transition message directed to a first state machine instance; the first state transition message carries an identifier of a service object corresponding to the first state machine instance and a first event for indicating the first state machine instance to perform state transition;
a business object change unit for: carrying out state transition on the first state machine instance according to the first event, executing a preset action corresponding to the state transition, and changing the state of the service object based on the action and the service object identifier carried in the first state transition message;
a linkage unit for: causing the first state machine instance to send a second state transition message to a second state machine instance associated with the first state machine instance based on the action to cause the second state machine instance to make a state transition; the second state transition message carries an identifier of a service object corresponding to the second state machine instance and a second event for indicating the second state machine instance to perform state transition.
8. The state machine linkage according to claim 7, wherein the action comprises: a first common action to exit a previous state, a second common action to enter a subsequent state, and a specific action corresponding to a state transition; and the business object changing unit is further used for:
after receiving the first state transition message, executing an entering method and a first public action defined by a message interceptor corresponding to the first event; after the first state machine instance makes a state transition, a second common action, the particular action, and the message interceptor defined exit method are performed.
9. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the state machine linkage method of any of claims 1-6.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out a state machine linkage method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110089376.4A CN113760403A (en) | 2021-01-22 | 2021-01-22 | State machine linkage method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110089376.4A CN113760403A (en) | 2021-01-22 | 2021-01-22 | State machine linkage method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN113760403A true CN113760403A (en) | 2021-12-07 |
Family
ID=78786367
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202110089376.4A Pending CN113760403A (en) | 2021-01-22 | 2021-01-22 | State machine linkage method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN113760403A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115101169A (en) * | 2022-07-29 | 2022-09-23 | 北京欧应科技有限公司 | Method, apparatus, and medium for implementing a training action |
-
2021
- 2021-01-22 CN CN202110089376.4A patent/CN113760403A/en active Pending
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115101169A (en) * | 2022-07-29 | 2022-09-23 | 北京欧应科技有限公司 | Method, apparatus, and medium for implementing a training action |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11188380B2 (en) | Method and apparatus for processing task in smart device | |
CN110806923B (en) | Parallel processing method and device for block chain tasks, electronic equipment and medium | |
CN112925651B (en) | Application resource deployment method, device, electronic equipment and medium | |
CN111400000B (en) | Network request processing method, device, equipment and storage medium | |
CN112925652B (en) | Application resource deployment method, device, electronic equipment and medium | |
CN115292026A (en) | Management method, device and equipment of container cluster and computer readable storage medium | |
CN110781180A (en) | Data screening method and data screening device | |
CN111737275A (en) | Database update event processing method and device and computer readable storage medium | |
CN115185434A (en) | Vehicle application scene processing method, device, equipment and storage medium | |
CN113282444A (en) | Visualization method and device for business process debugging | |
CN113760403A (en) | State machine linkage method and device | |
CN113760400A (en) | Request processing method and device | |
CN112328225A (en) | Page operation method and operation system thereof | |
CN110750419B (en) | Offline task processing method and device, electronic equipment and storage medium | |
CN113535371A (en) | Method and device for multithreading asynchronous loading of resources | |
CN114697398B (en) | Data processing method, device, electronic equipment, storage medium and product | |
CN114661274A (en) | Method and device for generating intelligent contract | |
CN111399942A (en) | Network card configuration method, network card configuration device, network card configuration equipment and storage medium | |
CN115700487A (en) | Method, device and equipment for processing messages in message queue and storage medium | |
CN113098960A (en) | Service operation method, device, server and storage medium | |
CN111858234A (en) | Task execution method, device, equipment and medium | |
CN113760487A (en) | Service processing method and device | |
US20240111579A1 (en) | Termination of sidecar containers | |
CN118381825A (en) | Communication coordination method and device applied to distributed system, equipment and medium | |
CN112363817A (en) | Big data task execution method and device, storage medium and server |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |