通过实例学习SpringStateMachine之Washer

2023-10-10 07:20

本文主要是介绍通过实例学习SpringStateMachine之Washer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景介绍

本系列通过学习SpringStateMachine中附带的10余个Sample来学习SpringStateMachine中的各个概念和用法。项目是使用的分支为2.2.0.RELEASE。项目参考文档也是2.2.0.RELEASE。

Washer简介

Washer是对洗衣机的描述。通过建立洗衣机的的状态机来演示如何使用history来实现状态的恢复。 例如甩干的过程中突然按下了暂停按钮。如果继续点击恢复,那么会恢复到原来的状态。

下图是washer状态机的描述。这里利用到了HISTORY这个伪状态来实现历史状态的恢复。当启动机器后,RUNNING下有3个子状态。如果当前在子状态DRYING下,且产生了CUTPOWER事件产,那么RUNNING会转换到POWEROFF。如果在次产生RESTOREPOWER事件,那么会恢复到RUNNING状态下的子状态。从而实现了状态恢复的演示。
在这里插入图片描述

Washer 依赖

项目在实现上述功能时,需要依赖springshell,官方给出的demo[1]使用了spring-shell1.2,本文将其改为spring-shell 2.0.0.RELEASE。

<dependency><groupId>org.springframework.statemachine</groupId><artifactId>spring-statemachine-core</artifactId><version>2.2.0.RELEASE</version></dependency><dependency><groupId>org.springframework.shell</groupId><artifactId>spring-shell-starter</artifactId><version>2.0.0.RELEASE</version></dependency>

Washer 实现

Washer依然是一个分层状态机。整个状态机的状态如下:

public enum States {RUNNING, HISTORY, END,WASHING, RINSING, DRYING,POWEROFF
}

RUNNING状态下包含一个子层,内有3个状态。而状态恢复就是当状态转换到某个子层后,能再恢复到变换前的子状态。

状态机的事件如下:

public enum Events {RINSE, DRY, STOP,RESTOREPOWER, CUTPOWER
}

首先我们需要配置状态。为了配置HISTORY这个伪状态,我们用到了history方法,同时指定了状态类型为History.SHALLOW。WASHING、RINSING、RINSING、HISTORY均是RUNNING的子状态。

@Override
public void configure(StateMachineStateConfigurer<States, Events> states)throws Exception {states.withStates().initial(States.RUNNING).state(States.POWEROFF).end(States.END).and().withStates().parent(States.RUNNING).initial(States.WASHING).state(States.RINSING).state(States.DRYING).history(States.HISTORY, History.SHALLOW);
}

接着我们添加转换。核心点就是子状态机无论内部子状态是什么,在CUTPOWER事件产生后都能够转换到POWEROFF状态。

其次是POWEROFF到HISTORY状态转换,由于HISTORY是一个RUNNING子状态机中的伪状态。转换到HISOTRY后,RUNNING内部子状态会恢复到从RUNNING转换到POWEROFF前的状态。

@Override
public void configure(StateMachineTransitionConfigurer<States, Events> transitions)throws Exception {transitions.withExternal().source(States.WASHING).target(States.RINSING).event(Events.RINSE).and().withExternal().source(States.RINSING).target(States.DRYING).event(Events.DRY).and().withExternal().source(States.RUNNING).target(States.POWEROFF).event(Events.CUTPOWER).and().withExternal().source(States.POWEROFF).target(States.HISTORY).event(Events.RESTOREPOWER).and().withExternal().source(States.RUNNING).target(States.END).event(Events.STOP);
}

为了观察效果,我们增加一下Action来打印输出,进行观察。完整的配置如下:

import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.configurers.StateConfigurer;@Configuration
@EnableStateMachine
public class StateMachineConfigextends EnumStateMachineConfigurerAdapter<States, Events> {private Action<States, Events> runningAction() {return new Action<States, Events>() {@Overridepublic void execute(StateContext<States, Events> context) {System.out.println("runningAction");}};}private Action<States, Events> powerOffAction() {return new Action<States, Events>() {@Overridepublic void execute(StateContext<States, Events> context) {System.out.println("powerOffAction");}};}private Action<States, Events> rinsingAction() {return new Action<States, Events>() {@Overridepublic void execute(StateContext<States, Events> context) {System.out.println("rinsingAction");}};}private Action<States, Events> washingAction() {return new Action<States, Events>() {@Overridepublic void execute(StateContext<States, Events> context) {System.out.println("washingAction");}};}private Action<States, Events> dryingAction() {return new Action<States, Events>() {@Overridepublic void execute(StateContext<States, Events> context) {System.out.println("drying");}};}@Overridepublic void configure(StateMachineStateConfigurer<States, Events> states)throws Exception {states.withStates().initial(States.RUNNING,runningAction()).state(States.POWEROFF,powerOffAction()).end(States.END).and().withStates().parent(States.RUNNING).initial(States.WASHING,washingAction()).state(States.RINSING,rinsingAction()).state(States.DRYING,dryingAction()).history(States.HISTORY, StateConfigurer.History.DEEP);}@Overridepublic void configure(StateMachineTransitionConfigurer<States, Events> transitions)throws Exception {transitions.withExternal().source(States.WASHING).target(States.RINSING).event(Events.RINSE).and().withExternal().source(States.RINSING).target(States.DRYING).event(Events.DRY).and().withExternal().source(States.RUNNING).target(States.POWEROFF).event(Events.CUTPOWER).and().withExternal().source(States.POWEROFF).target(States.HISTORY).event(Events.RESTOREPOWER).and().withExternal().source(States.RUNNING).target(States.END).event(Events.STOP);}
}

最后我们通过命令行发送命令即可观察RUNNING内部子状态的恢复。


Entry state RUNNING
Entry state WASHING
State machine startedsm>sm event RINSE
Exit state WASHING
Entry state RINSING
Event RINSE sendsm>sm event DRY
Exit state RINSING
Entry state DRYING
Event DRY sendsm>sm event CUTPOWER
Exit state DRYING
Exit state RUNNING
Entry state POWEROFF
Event CUTPOWER sendsm>sm event RESTOREPOWER
Exit state POWEROFF
Entry state RUNNING
Entry state WASHING
Entry state DRYING
Event RESTOREPOWER send

总结

在分层状态机中,一个子状态机发生状态变化后,如果状态又转移到子状态机上,为了恢复子状态机中的内部状态,可以利用伪状态HISTORY来实现状态恢复。

参考

[1]washer doc, https://docs.spring.io/spring-statemachine/docs/2.2.0.RELEASE/reference/#statemachine-examples-washer
[2]washer source code,https://github.com/spring-projects/spring-statemachine/tree/2.2.0.RELEASE/spring-statemachine-samples/washer/src/main/java/demo/washer

这篇关于通过实例学习SpringStateMachine之Washer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/178904

相关文章

MySQL多实例管理如何在一台主机上运行多个mysql

《MySQL多实例管理如何在一台主机上运行多个mysql》文章详解了在Linux主机上通过二进制方式安装MySQL多实例的步骤,涵盖端口配置、数据目录准备、初始化与启动流程,以及排错方法,适用于构建读... 目录一、什么是mysql多实例二、二进制方式安装MySQL1.获取二进制代码包2.安装基础依赖3.清

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Apache Ignite缓存基本操作实例详解

《ApacheIgnite缓存基本操作实例详解》文章介绍了ApacheIgnite中IgniteCache的基本操作,涵盖缓存获取、动态创建、销毁、原子及条件更新、异步执行,强调线程池注意事项,避免... 目录一、获取缓存实例(Getting an Instance of a Cache)示例代码:二、动态

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

MySQL数据库的内嵌函数和联合查询实例代码

《MySQL数据库的内嵌函数和联合查询实例代码》联合查询是一种将多个查询结果组合在一起的方法,通常使用UNION、UNIONALL、INTERSECT和EXCEPT关键字,下面:本文主要介绍MyS... 目录一.数据库的内嵌函数1.1聚合函数COUNT([DISTINCT] expr)SUM([DISTIN

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解