State machine linkage method and device
By receiving and sending state transfer messages in the state machine instance, the linkage between the state machine instances is realized, which solves the problem that the state machine instances cannot be linked, reduces system resource consumption, and improves system efficiency.
Patent Information
- Application Number
- CN202110089376.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-22
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2041-01-22
AI Technical Summary
The existing state machine implementation method cannot meet the state transfer linkage between different state machine instances. Especially when the amount of business object data is large, frequent creation of state machines leads to excessive consumption of system resources.
By receiving state transfer messages, executing preset actions and sending state transfer messages to associated state machine instances, state transfer linkage between different state machine instances is achieved, and frequent creation of state machine instances is reduced through persistent storage and object pools.
It realizes the state transfer linkage between different state machine instances, reduces system resource consumption, and improves system stability and efficiency.
Smart Images

Figure CN113760403B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a state machine linkage method and device. Background Art
[0002] A finite state machine (FSM), or state machine for short, is a mathematical model that represents finite states and the transitions, actions, and other behaviors between them. Generally, a finite state machine controls state transitions through the relationships between states, events, and actions. Using a state machine model to handle state transitions in a business environment allows for a clearer visualization of the state flow through state topology diagrams, avoiding excessive and cumbersome if / else logic control statements in the code and making it easier to maintain.
[0003] The existing state machine implementation method generally follows the following process: first, all states involved in the business process are defined as the states of the state machine; then, the current state of the state machine is determined, and the next state and corresponding actions are determined based on the current state of the state machine and the received events. Based on this information, the state machine is controlled to jump from the current state to the next state and execute the corresponding actions.
[0004] During the implementation of the present invention, the inventors discovered that the existing technology suffers from at least the following issues: Existing state machine implementations are generally designed based on a set state transition topology, which fails to address scenarios where state transitions between different state machines are linked. For example, a state transition in one project can also cause state transitions in associated projects. Furthermore, for scenarios with large amounts of business object data, the existing technology requires frequent state machine creation, which consumes excessive system resources. Summary of the Invention
[0005] In view of this, an embodiment of the present invention provides a state machine linkage method and apparatus, which can realize state transfer linkage between different state machine instances.
[0006] To achieve the above object, according to one aspect of the present invention, a state machine linkage method is provided.
[0007] The state machine linkage method of an embodiment of the present invention includes: receiving a first state transition message pointing to a first state machine instance; wherein the first state transition message carries an identifier of a business object corresponding to the first state machine instance, and a first event for instructing the first state machine instance to perform a state transition; performing a state transition on the first state machine instance based on the first event, executing a preset action corresponding to the state transition, and changing the state of the business object based on the action and the business object identifier carried in the first state transition message; based on the action, 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, so that the second state machine instance performs a state transition; wherein the second state transition message carries an identifier of the 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.
[0008] Optionally, the action includes: a first public action to exit the previous state, a second public action to enter the next state, and a specific action corresponding to the state transition; and, executing the preset action corresponding to the state transition includes: after receiving the first state transition message, executing the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs a state transition, executing the second public action, the specific action and the exit method defined by the message interceptor.
[0009] Optionally, the specific action includes: a first method for changing the state of a business object; and the changing the state of a business object based on the action and the business object identifier carried in the first state transition message includes: determining the corresponding business object using the business object identifier carried in the first state transition message; and executing the first method to change the state of the determined business object.
[0010] Optionally, the specific action includes: a second method for sending a state transition message; and, based on the action, causing the first state machine instance to send a second state transition message to the second state machine instance associated with the first state machine instance, includes: executing the second method to cause the first state machine instance to send a second state transition message to the second state machine instance.
[0011] Optionally, any state machine instance in the first state machine instance and the second state machine instance is obtained according to the following steps: determine whether there is state machine data corresponding to the business object in the preset persistent storage unit: if so, deserialize the state machine data into the memory to form a state machine instance.
[0012] Optionally, any state machine instance among the first state machine instance and the second state machine instance is obtained according to the following steps: determine whether there is a state machine instance corresponding to the business object in the object pool set in the memory: if so, call the state machine instance from the object pool, and adjust the current state of the state machine instance to a state consistent with the current state of the business object; and the state machine linkage method further includes: after the use of the state machine instance is completed, return the state machine instance to the object pool.
[0013] To achieve the above object, according to another aspect of the present invention, a state machine linkage device is provided.
[0014] The state machine linkage device of an embodiment of the present invention may include: a receiving unit, used to receive a first state transition message pointing to a first state machine instance; wherein the first state transition message carries an identifier of a business object corresponding to the first state machine instance, and a first event for instructing the first state machine instance to perform a state transition; a business object change unit, used to: perform a state transition on the first state machine instance according to the first event, execute a preset action corresponding to the state transition, and change the state of the business object based on the action and the business object identifier carried in the first state transition message; a linkage unit, used to: based on the action, enable 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, so that the second state machine instance performs a state transition; wherein the second state transition message carries an identifier of the 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.
[0015] Optionally, the action includes: a first public action to exit the previous state, a second public action to enter the next state, and a specific action corresponding to the state transition; and the business object change unit is further used to: after receiving the first state transition message, execute the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs a state transition, execute the second public action, the specific action and the exit method defined by the message interceptor.
[0016] To achieve the above objective, according to another aspect of the present invention, an electronic device is provided.
[0017] An electronic device of the present invention includes: one or more processors; a storage device 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 implement the state machine linkage method provided by the present invention.
[0018] To achieve the above objective, according to another aspect of the present invention, a computer-readable storage medium is provided.
[0019] A computer-readable storage medium of the present invention stores a computer program, which, when executed by a processor, implements the state machine linkage method provided by the present invention.
[0020] According to the technical solution of the present invention, the embodiments of the above invention have the following advantages or beneficial effects: a message encapsulation and sending function is pre-set in the state machine instance. When a state machine instance receives a state transition message and then performs a state transition, in addition to executing the preset action corresponding to the state transition to change the state of the corresponding business object, the preset action is executed to cause 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 performs a state transition, thereby realizing the state transition linkage of any multiple state machine instances and meeting the business scenario of state transition linkage between business objects. In addition, when obtaining a state machine instance, in addition to the direct creation method, the embodiments of the present invention can also use the state machine data pre-stored in the persistent storage unit that corresponds to the business object one by one, deserialize the state machine data to form a 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 to be consistent with the current state of the business object to form the required state machine instance. Using these two methods can reduce the frequent creation of state machine instances and eliminate the memory, CPU (Central Processing Unit) and network overhead generated by creating and recycling objects.
[0021] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] The accompanying drawings are provided for a better understanding of the present invention and are not intended to limit the present invention.
[0023] Figure 1 This is a schematic diagram of the main steps of the state machine linkage method according to an embodiment of the present invention;
[0024] Figure 2 This is a schematic diagram of the overall state transition process of a state machine instance in an embodiment of the present invention;
[0025] Figure 3 is a UML (Unified Modeling Language) diagram of a state machine instance created in an embodiment of the present invention;
[0026] Figure 4 This is a schematic diagram of the steps of receiving an event by a state machine instance in an embodiment of the present invention;
[0027] Figure 51 is a schematic diagram of a state transition process of a state machine instance in an embodiment of the present invention;
[0028] Figure 6 is a schematic diagram of the linkage of a state machine instance in an embodiment of the present invention;
[0029] Figure 7 It is a schematic diagram of the components of the state machine linkage device in an embodiment of the present invention;
[0030] Figure 8 is an exemplary system architecture diagram in which embodiments of the present invention may be applied;
[0031] Figure 9 Schematic diagram of the structure of an electronic device used to implement the state machine linkage method in an embodiment of the present invention. DETAILED DESCRIPTION
[0032] The following description of exemplary embodiments of the present invention is made in conjunction with the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding. These details should be considered as merely exemplary. Therefore, it should be appreciated by those skilled in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0033] It should be pointed out that, in the absence of conflict, the embodiments of the present invention and the technical features therein may be combined with each other.
[0034] Figure 1 1 is a schematic diagram of the main steps of the state machine linkage method according to an embodiment of the present invention.
[0035] like Figure 1 As shown, the state machine linkage method of the embodiment of the present invention can be specifically performed according to the following steps:
[0036] Step S101: Receive a first state transfer message directed to a first state machine instance.
[0037] In this step, the server storing multiple state machine instances receives a first state transfer message, and the first state transfer message points to the first state machine instance, that is, the first state transfer message is sent to the first state machine instance by the message sender. The first state transfer message can carry the identifier of the business object corresponding to the first state machine instance, and the first event for instructing the first state machine instance to perform a state transfer. Among them, the first state machine instance can be any state machine instance stored in the server; the business object identifier carried in the first state transfer message can be used to determine the business object corresponding to the first state machine; the first event can realize a certain state transfer of the first state machine instance according to the pre-set transfer logic. The following first explains the relevant concepts of finite state machines.
[0038] In an embodiment of the present invention, a finite state machine can control the transition of states through the relationship between states, events, and actions, wherein a state refers to a condition of an object in its life cycle, and an object in a particular state must satisfy certain conditions, perform certain actions, or wait for certain events. Events occupy a certain position in time and space, and have a specific meaning for a finite state machine (abbreviated as a state machine). Events usually cause state transitions, prompting the state machine to transfer from one state to another. Actions refer to atomic operations that can be executed in a state machine (i.e., operations that will not be interrupted by the thread scheduling mechanism).
[0039] In practical applications, different business objects (such as orders and projects) can often be abstracted into corresponding state machine instances. For example, in project management, a main project is a long-term project that includes multiple subprojects. The main project and subprojects can be considered interrelated business objects. The main project can be abstracted into state machine instance 1 with states a, b, and c, while the subprojects can be abstracted into state machine instance 2 with states a, b, and d. States a, b, c, and d correspond to actual states such as waiting for bidding, bidding in progress, and bidding completed. These two state machine instances have different state transition topologies (a state transition topology includes all states of the state machine instance, all state transition modes, and the events and actions corresponding to the state transition modes, which can be described using diagrams or tables). The following table shows the state transition topology of state machine instance 1 corresponding to the main project, where the first column represents the previous state and the first row represents the next state.
[0040]
[0041] According to the table above, when event 1 occurs, state machine instance 1 remains in state a and executes action 1. When event 2 occurs, state machine instance 1 is triggered to transition from state a to state b and execute action 2. When event 3 occurs, state machine instance 1 is triggered to transition from state a to state c and execute action 3. In specific applications, a single event can be bound to multiple state transitions. When an event is received, the state machine instance can determine the corresponding state transition from the state transitions bound to the event based on its current state.
[0042] In actual scenarios, the state transitions between the main project and the sub-project often have linkage characteristics. For example, when the main project transitions from state a to state b, the sub-project also needs to transition from state a to state b. Therefore, the corresponding state machine instance 1 and state machine instance 2 also need to simulate this feature. In the existing technology, the state transition logic can only be implemented based on a single state machine instance.
[0043] In order to solve the above problem, the embodiment of the present invention provides a method for supporting state transfer linkage between different state machine instances.
[0044] In step S101, the server receives a first state transfer message sent by the client, wherein the client refers to the message sender, and the client may be in the same system or the same host as the server, or in a different system or host from the server.
[0045] Step S102: performing a 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 business object based on the action and the business object identifier carried in the first state transition message.
[0046] In this step, after receiving the first state transition message directed to the first state machine instance, the server performs a corresponding state transition on the first state machine instance according to the 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 above-mentioned preset action includes: a first public action to exit the previous state, a second public action to enter the next state, and a specific action corresponding to the state transition. Among them, the first public action and the second public action are actions that each state transition mode has. For example, in the above example, each state transition mode of the state machine instance 1 (including transitioning from state a to state b, from state b to state c, from state c to state b, etc.) has the same first public action and second public action. In contrast to the public action, the specific action refers to an action that only corresponds to the current state transition mode. The specific action corresponding to each state transition mode is generally different.
[0047] In this way, the specific method of executing the preset action in step S102 can be as follows: after receiving the first state transition message, the server executes the first public action; after the first state machine instance performs the state transition corresponding to the first time, the server executes the second public action and the specific action. Continuing with the above example, after receiving the first event of "arrival of the bidding deadline" (the first event corresponds to the state transition method of state machine instance 1 from state a to state b), the server executes the first public action of "calculating project completion" in state machine instance 1; after state machine instance 1 performs the state transition, the server executes the second public action of "obtaining current project data" and the specific action of "requesting the algorithm service to perform relevant calculations corresponding to state b".
[0048] As a preferred solution, the embodiment of the present invention can also pre-configure a message interceptor corresponding to each event so that after receiving the message and event, the pre-defined method of the message interceptor is executed to implement the customized calculation logic. After the message interceptor is configured, the specific method of executing the preset action in step S102 can be as follows: after receiving the first state transition message, the server executes the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs the state transition, the second public action, the above-mentioned specific action and the exit method defined by the message interceptor are executed in sequence. Continuing with the above example, the entry method defined by the message interceptor can be "recording system logs and the current status of the project", and the exit method defined by the message interceptor can be "obtaining request algorithm parameters".
[0049] In step S102, after the first state machine instance performs state transfer, the server can also change the state of the business object based on the above-mentioned preset action and the business object identifier carried in the first state transfer message. Specifically, the above-mentioned specific action may include a first method for changing the state of the business object. The server can first use the business object identifier carried in the first state transfer message to determine the corresponding business object, and then perform a corresponding change to the state of the determined business object by executing the first method. In actual applications, the specific data of the business object can be stored in a database, so the operation of changing the business object can be performed in the database. It can be understood that this change method is consistent with the state transfer method of the first state machine instance. Through the above settings, the embodiment of the present invention can realize automatic change of the business object state in response to external events based on the finite state machine.
[0050] Step S103: Based on the action, the first state machine instance sends a second state transfer message to the second state machine instance associated with the first state machine instance, so that the second state machine instance performs state transfer.
[0051] In an embodiment of the present invention, client functionality, namely, message encapsulation and sending functionality, can be pre-added to each state machine instance. This allows the server to, when executing the aforementioned preset action, send a second state transition message to a second state machine instance associated with the first state machine instance, thereby causing the second state machine instance to transition state, thereby achieving state transition linkage between the first and second state machine instances. The data in the second state transition message is similar to that in the first state transition message, and may include an identifier of the business object corresponding to the second state machine instance and a second event for instructing the second state machine instance to transition state.
[0052] Specifically, the aforementioned specific action may include a second method for sending a state transition message. The state transition linkage may be performed by: the server executing the second method in the specific action causes the first state machine instance to send a second state transition message to the second state machine instance. Upon receiving the second state transition message, the second state machine instance performs a corresponding state transition based on the second event carried therein, determines the corresponding business object based on the business object identifier in the second state transition message, and executes a preset action in the database to change the state of the business object. It is understood that the second state machine instance may also, in a manner similar to the first state machine instance, send a state transition message to a third state machine instance associated with the second state machine instance, causing the third state machine instance to perform a state transition. In addition to performing a state transition, the third state machine instance may also send a state transition message to a fourth state machine instance associated therewith, causing the fourth state machine instance to perform a state transition. In this way, state transition linkage can be achieved for any number of state machine instances, thereby overcoming the drawback of the prior art that programming can only be performed for a single state machine instance.
[0053] It can be understood that step S103 is executed after "transferring the state of the first state machine instance based on the first event" in step S102, and can also be executed before, after or at the same time as "changing the state of the business object based on the action and the business object identifier carried in the first state transfer message" in step S102.
[0054] In an embodiment of the present invention, a new state machine instance acquisition method is provided to address the problems existing in existing state machine instance acquisition methods. In the prior art, when a state machine instance corresponding to a business object needs to be acquired, the state machine instance is typically directly created and then destroyed after use, resulting in significant memory, CPU, and network overhead. The present invention provides the following two state machine instance acquisition methods.
[0055] First, after the use of the state machine instance is completed, the state machine data corresponding to the business object (that is, the data formed by the serialization of the state machine instance, which is also consistent with the current state of the business object) is stored in a preset persistent storage unit (such as a cache or hard disk); when it is necessary to obtain the state machine instance corresponding to the business object, it is determined whether the state machine data corresponding to the business object exists in the persistent storage unit (it can be understood that each state machine instance will be bound to the corresponding business object by configuring the business object identifier when it is created. The above judgment can be achieved by detecting the business object identifier in the state machine data). If the state machine data corresponding to the business object exists in the persistent storage unit, the state machine data can be deserialized into the memory to form a state machine instance. 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 restoring a byte sequence into a data object.
[0056] Secondly, after the state machine instance is created in the historical period, the state machine instance can be stored in the object pool in the memory. Generally, the state machine instance stored in the object pool is in the initial state (that is, the state of the state machine instance after the state machine instance is created), and can provide reuse for each business object. When it is necessary to obtain the state machine instance corresponding to the business object, you can first determine whether there is a state machine instance corresponding to the business object in the object pool: if so, call the state machine instance from the object pool, and adjust the current state of the state machine instance to a state consistent with the current state of the business object, and you can get a usable state machine instance. In the above method, after the use of the state machine instance is completed, the state machine instance can be returned to the object pool.
[0057] The above two state machine acquisition methods can eliminate the huge memory, CPU and network overhead caused by frequent creation and destruction of state machine instances, thereby reducing system pressure.
[0058] The technical solution of the present invention will be further described below based on a specific embodiment.
[0059] This embodiment is applied in project management scenarios, controlling the state transitions of each project by establishing a state machine. Specifically, projects have a master-subproject relationship, with the master project and subprojects each having their own states, events, state transition topologies, and actions performed before and after the state transition. Furthermore, the state transition topologies of the master project and subprojects are linked, meaning that a state transition in the master project will cause a state transition in the subproject, and vice versa. Current state machine implementations cannot meet the requirements of the aforementioned business scenarios where multiple sets of state transition topologies are linked. Figure 2 The overall state transfer process of the state machine instance in this embodiment is shown.
[0060] Before using the state machine instance to execute the state transfer of the business object, the following configuration is required in advance.
[0061] Configuration 1: Define the state and event enumeration of the state machine instance according to the business scenario, and bind the state machine instance to the corresponding business object identifier.
[0062] Configuration 2: Build a state machine creation module. The state machine creation module adopts the design pattern of factory mode plus strategy mode. The structure is as follows Figure 3 Each StateMachineBuilder maintains a separate set of state transition topologies, which may include: various states of the state machine, state transition methods and corresponding specific actions after the state machine receives an event, common actions for the state machine to enter or exit any state, and judgment conditions for the state machine to transition from one state to another after receiving an event (based on these judgment conditions, it can be determined whether the state machine instance enters a certain state or remains in the original state. This judgment condition can also be left unconfigured).
[0063] exist Figure 3 In the Spring container, different StateMachineBuilder classes have the same interface method, build(). Executing this method configures and instantiates the data contained in the state transfer topology as properties. When the Spring container is started, each StateMachineBuilder is instantiated and injected into the Spring container. The state transfer topology type is then passed in through the custom factory, StateMachineFactory (the state transfer topology type can be obtained using the getBuilder(type) method; different state transfer topologies belong to different state transfer topology types), and the corresponding StateMachineBuilder is obtained from the Spring container. StateMachineFactory exposes a create() interface to the client, which can be used to obtain a StateMachineBuilder object from the Spring container based on the state transfer topology type, and then call its build() method to complete the configuration and instantiation of the state machine.
[0064] Configuration 3: Configure the message interceptor corresponding to the event and define the corresponding entry method and exit method.
[0065] Configuration 4: Build a state machine persistence module for storing and retrieving state machine instances. In this embodiment, each business object can correspond to a state machine instance. When the business object enters any state, the state machine instance can be stored in a persistent storage unit. At this point, the state of the business object and the state machine instance are consistent. When the business object is called again, the state machine instance can be directly restored from the persistent storage unit, instead of recreating the state machine, thus quickly obtaining a usable state machine instance.
[0066] The following combination Figure 2 Describe the overall state transition process of a state machine instance.
[0067] First, enter business data such as the business object and business type, and determine whether the state machine instance needs to be persisted in subsequent processes. If so, use the state machine persistence module to obtain the state machine instance. That is, determine whether the state machine data corresponding to the business object exists in the persistent storage unit. If so, deserialize the state machine data into a state machine instance for direct use. If not, use the state machine creation module to create the state machine instance.
[0068] If it is determined that the state machine instance does not need to be persisted in subsequent processes, a further determination is made as to whether an object pool is maintained. If an object pool exists, the state machine instance corresponding to the business object is retrieved from the object pool and state matching is performed. If an object pool does not exist, the state machine instance is created using the state machine creation module. State matching refers to adjusting the current state of the state machine instance, whether created using the state machine creation module or retrieved from the object pool, to a state consistent with the current state of the business object. After state matching, a usable state machine instance is obtained.
[0069] Afterwards, the state machine instance can transition states based on event signals sent by the client. After the state machine instance is used, the state machine persistence module can be used to store it in a persistent storage unit for subsequent use. If the state machine instance was previously obtained from an object pool, it must be returned to the object pool after use.
[0070] In the above process, the use of persistent storage units and object pools to maintain and obtain state machine instances can effectively avoid the repeated creation and destruction of state machine instances. The persistence method can quickly restore available state machine instances with the state at the time of storage. The state machine instances in the object pool provide reuse for various business objects. Users can obtain state machine instances with the initial state from the object pool and return them to the object pool after the work is completed instead of directly destroying them. Based on these two methods, the state transition topology, corresponding actions, judgment conditions, etc. maintained within the state machine instance can be quickly restored. When there are a large number of business objects that need to be processed concurrently in the business scenario, these two methods can be used to avoid creating too many objects and causing a surge in memory, effectively ensuring system stability.
[0071] In this embodiment, after obtaining an available state machine instance, the client can send an event to the state machine instance. The state machine instance switches to the next state according to the event bound in the state transition topology and the state transition mode, and performs related actions. Figure 4 FIG. 1 is a schematic diagram of the steps of receiving an event by a state machine instance in an embodiment of the present invention. Figure 4 As shown in the figure, the event and business object data (including the business object identifier) are encapsulated as a message body structure (i.e., state transfer message) and passed to the state machine instance. After receiving the state transfer message, the state machine instance puts the event carried in it into the message queue. Then, the state machine instance can consume the messages in the message queue in a synchronous thread or asynchronous thread manner according to the complexity and execution time of the state transfer method (it can be submitted to the event processing thread for processing). The entire consumption process is as follows Figure 5 shown.
[0072] See also Figure 5First, based on the message interceptor in Configuration 3, the entry method preTransition() defined by the message interceptor is executed. Next, the state machine instance executes the pre-set transition condition judgment method guard() to evaluate the state transition. If this method returns true, the evaluation passes and the state transition process continues. If the evaluation result is false, the state transition process exits. Thereafter, based on the data in Configuration 2, the state machine instance retrieves the first public action afterStateAction() for exiting the previous state, the second public action beforeStateAction() for entering the next state, and the specific action action() corresponding to the current state transition. It executes afterStateAction() before the state transition and, after the state transition, executes beforeStateAction() and action() in sequence. action() may include the first and second methods. Executing the first method changes the state of the business object in the database to align with the current state of the state machine instance. Executing the second method causes the first state machine instance to send a second state transition message to the second state machine instance, thereby implementing state transition linkage between different state machine instances. Finally, the exit method postTransition() defined by the message interceptor is executed.
[0073] Figure 6 The linkage of the state machine instance in this embodiment is shown. Figure 6 As shown, each state machine instance internally holds a corresponding business object and is in a certain state. When state machine instance A receives event E1 from the client, it executes the corresponding state transition (from state S1 to state S2) and changes the state of the business object. It then uses the embedded message encapsulation and sending functions to send event E2 to state machine B. Upon receiving E2, state machine instance B executes the corresponding state transition (from state S3 to state S4) and changes the state of the business object. It then sends event E3 to state machine C, and so on. This achieves state transition linkage among multiple associated state machine instances.
[0074] In the technical solution of the embodiment of the present invention, a message encapsulation and sending function is pre-set in the state machine instance. When a state machine instance receives a state transition message and then performs a state transition, in addition to executing the preset action corresponding to the state transition to change the state of the corresponding business object, the preset action is executed to cause 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 performs a state transition. This realizes the state transition linkage of any multiple state machine instances and meets the business scenario of state transition linkage between business objects. In addition, when obtaining a state machine instance, in addition to the direct creation method, the embodiment of the present invention can also use the state machine data pre-stored in the persistent storage unit that corresponds to the business object one by one, deserialize the state machine data to form a state machine instance, or call the state machine instance pre-stored in the memory object pool, adjust the current state of the state machine instance to be consistent with the current state of the business object, and thus form the required state machine instance. Using these two methods can reduce the frequent creation of state machine instances and eliminate the memory, CPU and network overhead generated by creating and recycling objects.
[0075] It is understandable that the above examples are only for illustrating the technical solutions of the present invention and do not necessarily strictly correspond to real scenarios.
[0076] It should be noted that for the aforementioned method embodiments, for ease of description, they are expressed as a series of action combinations. However, those skilled in the art should be aware that the present invention is not limited to the order of the actions described, and certain steps can actually be performed in other orders or simultaneously. In addition, those skilled in the art should also be aware that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily required to implement the present invention.
[0077] In order to better implement the above solutions of the embodiments of the present invention, relevant devices for implementing the above solutions are also provided below.
[0078] See also Figure 7 As shown, the state machine linkage device 700 provided by the embodiment of the present invention may include: a receiving unit 701 , a business object changing unit 702 and a linkage unit 703 .
[0079] Among them, the receiving unit 701 can be used to receive a first state transition message pointing to the first state machine instance; wherein the first state transition message carries the identifier of the business object corresponding to the first state machine instance, and the first event for instructing the first state machine instance to perform a state transition; the business object change unit 702 can be used to: perform a state transition on the first state machine instance according to the first event, execute a preset action corresponding to the state transition, and change the state of the business object based on the action and the business object identifier carried in the first state transition message; the linkage unit 703 can be used to: based on the action, enable the first state machine instance to send a second state transition message to the second state machine instance associated with the first state machine instance, so that the second state machine instance performs a state transition; wherein the second state transition message carries the identifier of the business object corresponding to the second state machine instance, and the second event for instructing the second state machine instance to perform a state transition.
[0080] In an embodiment of the present invention, the actions may include: a first public action to exit a previous state, a second public action to enter a subsequent state, and a specific action corresponding to the state transition; and the business object change unit 702 may be further used to: after receiving the first state transition message, execute the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs a state transition, execute the second public action, the specific action and the exit method defined by the message interceptor.
[0081] As a preferred solution, the specific action may include: a first method for changing the state of a business object; and the business object change unit 702 may be further used to: determine the corresponding business object using the business object identifier carried in the first state transfer message; and execute the first method to change the state of the determined business object.
[0082] Preferably, the specific action may include: a second method for sending a state transfer message; and the linkage unit 703 may be further configured to: execute the second method to enable the first state machine instance to send a second state transfer message to the second state machine instance.
[0083] In actual applications, the device 700 may further include a first state machine instance recovery unit, which can be used to: determine whether there is state machine data corresponding to the business object in the preset persistent storage unit; if so, deserialize the state machine data into the memory to form a state machine instance.
[0084] In addition, in an embodiment of the present invention, the device 700 may further include a second state machine instance recovery unit, which can be used to: determine whether there is a state machine instance corresponding to the business object in the object pool set in the memory; if so, call the state machine instance from the object pool and adjust the current state of the state machine instance to a state consistent with the current state of the business object; and the device 700 may further include a state machine instance return unit, which can be used to: return the state machine instance to the object pool after the use of the state machine instance is completed.
[0085] In the technical solution of the embodiment of the present invention, a message encapsulation and sending function is pre-set in the state machine instance. When a state machine instance receives a state transition message and then performs a state transition, in addition to executing the preset action corresponding to the state transition to change the state of the corresponding business object, the preset action is executed to cause 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 performs a state transition. This realizes the state transition linkage of any multiple state machine instances and meets the business scenario of state transition linkage between business objects. In addition, when obtaining a state machine instance, in addition to the direct creation method, the embodiment of the present invention can also use the state machine data pre-stored in the persistent storage unit that corresponds to the business object one by one, deserialize the state machine data to form a state machine instance, or call the state machine instance pre-stored in the memory object pool, adjust the current state of the state machine instance to be consistent with the current state of the business object, and thus form the required state machine instance. Using these two methods can reduce the frequent creation of state machine instances and eliminate the memory, CPU and network overhead generated by creating and recycling objects.
[0086] Figure 8 An exemplary system architecture 800 is shown to which the state machine linkage method or state machine linkage apparatus according to an embodiment of the present invention can be applied.
[0087] like Figure 8 As shown, system architecture 800 may include terminal devices 801, 802, and 803, a network 804, and a server 805 (this architecture is merely an example, and the components included in the specific architecture may be adjusted based on the specific application). Network 804 is used to provide a medium for communication links between terminal devices 801, 802, and 803 and server 805. Network 804 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0088] Users can use terminal devices 801, 802, 803 to interact with server 805 via network 804 to receive or send messages, etc. Various client applications can be installed on terminal devices 801, 802, 803, such as a message sending application (only as an example).
[0089] The terminal devices 801 , 802 , and 803 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0090] Server 805 may be a server that provides various services, such as a computing server that supports a messaging application operated by a user using terminal devices 801, 802, and 803 (for example only). The computing server may receive state transition messages sent by terminal devices 801, 802, and 803, and perform state transitions on a state machine instance provided on the computing server based on the state transition messages.
[0091] It should be noted that the state machine linkage method provided in the embodiment of the present invention is generally executed by the server 805 , and accordingly, the state machine linkage device is generally set in the server 805 .
[0092] It should be understood that Figure 8 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.
[0093] The present invention also provides an electronic device. The electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the state machine linkage method provided by the present invention.
[0094] Reference below Figure 9 , which shows a schematic structural diagram of a computer system 900 of an electronic device suitable for implementing an embodiment of the present invention. Figure 9 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present invention.
[0095] like Figure 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage unit 908 into a random access memory (RAM) 903. Various programs and data required for the operation of the computer system 900 are also stored in the RAM 903. The CPU 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.
[0096] The following components are connected to the I / O interface 905: an input section 906 including a keyboard, a mouse, and the like; an output section 907 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 908 including a hard disk and the like; and a communication section 909 including a network interface card such as a LAN card or a modem. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to the I / O interface 905 as needed. A removable medium 911, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 910 as needed, so that computer programs read therefrom can be installed in the storage section 908 as needed.
[0097] In particular, according to embodiments disclosed herein, the processes described in the main step diagrams above can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for executing the methods shown in the main step diagrams. In the above embodiments, the computer program can be downloaded and installed from a network via the communication section 909 and / or installed from removable media 911. When the computer program is executed by the central processing unit 901, the above-described functions defined in the system of the present invention are performed.
[0098] It should be noted that the computer-readable medium described in the present invention may be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device, or component. In the present invention, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. Program code embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wireline, optical cable, RF, or any suitable combination thereof.
[0099] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the above-mentioned module, program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0100] The units described in the embodiments of the present invention may be implemented in software or hardware. The units described may also be provided in a processor. For example, they may be described as comprising a receiving unit, a business object change unit, and a linkage unit. The names of these units do not, in some cases, limit the units themselves. For example, the receiving unit may also be described as a "unit that provides a state transition message to the business object change unit."
[0101] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently and not be assembled into the device. The computer-readable medium carries one or more programs, and when the one or more programs are executed by the device, the device performs the following steps: receiving a first state transition message directed to a first state machine instance; wherein the first state transition message carries an identifier of a business object corresponding to the first state machine instance and a first event for instructing the first state machine instance to perform a state transition; performing a state transition on the first state machine instance based on the first event, executing a preset action corresponding to the state transition, and changing the state of the business object based on the action and the business object identifier carried in the first state transition message; and, based on the action, 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, so as to cause the second state machine instance to perform a state transition; wherein the second state transition message carries an identifier of the 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.
[0102] In the technical solution of the embodiment of the present invention, a message encapsulation and sending function is pre-set in the state machine instance. When a state machine instance receives a state transition message and then performs a state transition, in addition to executing the preset action corresponding to the state transition to change the state of the corresponding business object, the preset action is executed to cause 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 performs a state transition. This realizes the state transition linkage of any multiple state machine instances and meets the business scenario of state transition linkage between business objects. In addition, when obtaining a state machine instance, in addition to the direct creation method, the embodiment of the present invention can also use the state machine data pre-stored in the persistent storage unit that corresponds to the business object one by one, deserialize the state machine data to form a state machine instance, or call the state machine instance pre-stored in the memory object pool, adjust the current state of the state machine instance to be consistent with the current state of the business object, and thus form the required state machine instance. Using these two methods can reduce the frequent creation of state machine instances and eliminate the memory, CPU and network overhead generated by creating and recycling objects.
[0103] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.
Claims
1. A state machine linkage method, characterized in that: include: Receive a first state transfer message directed to the first state machine instance; wherein the first state transfer message carries an identifier of a business object corresponding to the first state machine instance and a first event for instructing the first state machine instance to perform a state transfer; Performing a 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 business object based on the action and the business object identifier carried in the first state transition message; Based on the action, the first state machine instance sends a second state transition message to a second state machine instance associated with the first state machine instance, so that the second state machine instance performs a state transition; wherein the second state transition message carries 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; The actions include: a first public action for exiting a previous state, a second public action for entering a next state, and a specific action corresponding to state transition; and The execution of the preset action corresponding to the state transition includes: after receiving the first state transition message, executing the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs a state transition, executing the second public action, the specific action and the exit method defined by the message interceptor.
2. The state machine linkage method according to claim 1, characterized in that: The specific action includes: a first method for changing the state of a business object; and the changing the state of the business object based on the action and the business object identifier carried in the first state transition message includes: Determine the corresponding business object using the business object identifier carried in the first state transfer message; The first method is executed to change the state of the determined business object.
3. The state machine linkage method according to claim 1, characterized in that: The specific action includes: a second method for sending a state transfer message; and the step of causing the first state machine instance to send a second state transfer message to the second state machine instance associated with the first state machine instance based on the action includes: Executing the second method causes the first state machine instance to send a second state transition message to the second state machine instance.
4. The state machine linkage method according to any one of claims 1 to 3, characterized in that: Any state machine instance of the first state machine instance and the second state machine instance is obtained according to the following steps: Determine whether there is state machine data corresponding to the business object in the preset persistent storage unit: If so, deserialize the state machine data into the memory to form a state machine instance.
5. The state machine linkage method according to any one of claims 1 to 3, characterized in that: Any state machine instance of the first state machine instance and the second state machine instance is obtained according to the following steps: determining whether a state machine instance corresponding to the business object exists in an object pool set in memory: if so, calling the state machine instance from the object pool, and adjusting the current state of the state machine instance to a state consistent with the current state of the business object; And, the state machine linkage method further includes: After the use of the state machine instance is completed, the state machine instance is returned to the object pool.
6. A state machine linkage device, characterized in that: include: A receiving unit, configured to receive a first state transfer message directed to a first state machine instance; wherein the first state transfer message carries an identifier of a business object corresponding to the first state machine instance and a first event for instructing the first state machine instance to perform a state transfer; A business object changing unit, configured to: perform a state transition on the first state machine instance according to the first event, execute a preset action corresponding to the state transition, and change the state of the business object based on the action and the business object identifier carried in the first state transition message; A linkage unit is configured to: based on the action, cause 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, so as to cause the second state machine instance to perform a state transition; wherein the second state transition message carries 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; The actions include: a first public action for exiting a previous state, a second public action for entering a next state, and a specific action corresponding to state transition; and The business object change unit is further used to: after receiving the first state transition message, execute the entry method and the first public action defined by the message interceptor corresponding to the first event; after the first state machine instance performs state transition, execute the second public action, the specific action and the exit method defined by the message interceptor.
7. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the state machine linkage method according to any one of claims 1 to 5.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the state machine linkage method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Cooperative control method for state machine, device and state machine system
CN102486743A