(spring全家桶十)Spring Statemachine有限状态机与地址分析

简介: 一、有限状态机有限状态机是一个特殊的有向图,包含节点和连接这些节点的弧。每个有限状态机都有开始、结束和若干个中间状态,每个弧上带有从一个状态进入下一个状态的条件。

一、有限状态机

有限状态机是一个特殊的有向图,包含节点和连接这些节点的弧。每个有限状态机都有开始、结束和若干个中间状态,每个弧上带有从一个状态进入下一个状态的条件。

以一个简化的购物流程为例,开始和结束之间有待下单、待支付、待发货、待收货四个状态,从一个状态转向另外一个状态中间需要发送事件。

这里写图片描述

有限状态机可以用于中文地址分析,识别地址的有限状态机如下。

这里写图片描述

给出一个地址,如果当前状态是“省”,后面一个词组是二级市,就进入状态“市”;如果下一个词组是某某区或者某某县,就进入“区县”状态。
如果一个地址能从状态机的开始状态经过若干中间状态到达终止状态,地址有效,否则地址无效。

例如:北京市海淀区清华东路17号,有效。河北省大连市友好路,无效。

二、Spring Statemachine

Spring Statemachine是spring的有限状态机开源框架,诞生不久。

Spring Statemachine框架的目标:
1.简单易用
2.采用层次化状态机结构简化复杂状态配置
3.State machine regions提供复杂的状态机配置
4.使用triggers, transitions, guards and actions.
5.类型安全的适配器配置
6.用于Spring应用程序上下文之外的简单实例化的生成器模式
7.基于ZooKeeper实现的分布式状态机
8.有事件监听器
9.在持久层存储状态配置
10.Spring IOC集成,bean可以和状态机交互

三、实例

3.1 创建spring boot工程

访问:http://start.spring.io/ ,新建一个spring boot工程:

这里写图片描述

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.statemachine</groupId>
    <artifactId>statemachinedemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>statemachinedemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.statemachine</groupId>
            <artifactId>spring-statemachine-core</artifactId>
            <version>1.2.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

创建状态:

public enum States {
    START,     //开始
    PROVINCE,  //省
    CITY,      //市
    DISTRIC,    //区县
    STREET,     //街道
    STREET_NUM,  //街道号
}

定义事件:

public enum Events {
    HAS_PROVINCE,    //地址中有一级省份
    HAS_CITY,        //地址中有二级市
    HAS_DISTRIC,     //地址中有三级县或者区
    HAS_STREET,      //地址中有四级的街道
    HAS_STREET_NUM   //地址中有五级的街道号
}

设置状态之间的转换关系:

import com.sun.org.apache.regexp.internal.RE;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.transition.Transition;

import java.util.EnumSet;


@Configuration
@EnableStateMachine
public class Config extends EnumStateMachineConfigurerAdapter<States, Events> {
    public static final Logger logger = Logger.getLogger(Config.class);

    @Override
    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
        states
                .withStates()
                .initial(States.START)
                .states(EnumSet.allOf(States.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
        transitions
                .withExternal().source(States.START).target(States.PROVINCE).event(Events.HAS_PROVINCE)
                .and()
                .withExternal().source(States.START).target(States.CITY).event(Events.HAS_CITY)
                .and()
                .withExternal().source(States.PROVINCE).target(States.CITY).event(Events.HAS_CITY)
                .and()
                .withExternal().source(States.PROVINCE).target(States.DISTRIC).event(Events.HAS_DISTRIC)
                .and()
                .withExternal().source(States.CITY).target(States.STREET).event(Events.HAS_STREET)
                .and()
                .withExternal().source(States.STREET).target(States.STREET_NUM).event(Events.HAS_STREET_NUM);
    }

    public StateMachineListener<States, Events> listener() {
        return new StateMachineListenerAdapter<States, Events>() {
            @Override
            public void transition(Transition<States, Events> transition) {
                if (transition.getTarget().getId() == States.START) {
                    logger.info("开始:");
                    return;
                }

                if (transition.getSource().getId() == States.START &&
                        transition.getTarget().getId() == States.PROVINCE) {
                    logger.info("省份:");
                    return;
                }


                if (transition.getSource().getId() == States.START &&
                        transition.getTarget().getId() == States.CITY) {
                    logger.info("市:");
                    return;
                }

                if (transition.getSource().getId() == States.PROVINCE &&
                        transition.getTarget().getId() == States.CITY) {
                    logger.info("市:");
                    return;
                }

                if (transition.getSource().getId() == States.PROVINCE &&
                        transition.getTarget().getId() == States.DISTRIC) {
                    logger.info("县:");
                    return;
                }

                if (transition.getSource().getId() == States.CITY &&
                        transition.getTarget().getId() == States.STREET) {
                    logger.info("街道:");
                    return;
                }

                if (transition.getSource().getId() == States.DISTRIC &&
                        transition.getTarget().getId() == States.STREET) {
                    logger.info("街道:");
                    return;
                }

                if (transition.getSource().getId() == States.STREET &&
                        transition.getTarget().getId() == States.STREET_NUM) {
                    logger.info("街道号:");
                    return;
                }
            }
        };
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
        config.withConfiguration().listener(listener());

    }
}

运行:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.statemachine.StateMachine;
@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Autowired
    private StateMachine<States, Events> stateMachine;

    @Override
    public void run(String... args) throws Exception {
        stateMachine.start();
        stateMachine.sendEvent(Events.HAS_PROVINCE);
        stateMachine.sendEvent(Events.HAS_CITY);
        stateMachine.sendEvent(Events.HAS_STREET);
        stateMachine.sendEvent(Events.HAS_STREET_NUM);
    }
}

结果:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.6.RELEASE)

2017-09-23 17:55:27.787  INFO 9344 --- [           main] xtipc.statemachine.addr.Application      : Starting Application on elk-PC with PID 9344 (E:\CODE\statemachinedemo\target\classes started by elk in E:\CODE\statemachinedemo)
2017-09-23 17:55:27.790  INFO 9344 --- [           main] xtipc.statemachine.addr.Application      : No active profile set, falling back to default profiles: default
2017-09-23 17:55:27.864  INFO 9344 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5fdba6f9: startup date [Sat Sep 23 17:55:27 CST 2017]; root of context hierarchy
2017-09-23 17:55:28.565  INFO 9344 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.statemachine.config.configuration.StateMachineAnnotationPostProcessorConfiguration' of type [org.springframework.statemachine.config.configuration.StateMachineAnnotationPostProcessorConfiguration$$EnhancerBySpringCGLIB$$3d777845] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-09-23 17:55:29.075  INFO 9344 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-09-23 17:55:29.079  INFO 9344 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2017-09-23 17:55:29.089  INFO 9344 --- [           main] xtipc.statemachine.addr.Config           : 开始:
2017-09-23 17:55:29.092  INFO 9344 --- [           main] o.s.s.support.LifecycleObjectSupport     : started org.springframework.statemachine.support.DefaultStateMachineExecutor@77102b91
2017-09-23 17:55:29.092  INFO 9344 --- [           main] o.s.s.support.LifecycleObjectSupport     : started CITY PROVINCE END DISTRIC STREET_NUM STREET START  / START / uuid=868923f4-239a-4730-ae7b-f71753876f3a / id=null
2017-09-23 17:55:29.097  INFO 9344 --- [           main] xtipc.statemachine.addr.Config           : 省份:
2017-09-23 17:55:29.098  INFO 9344 --- [           main] xtipc.statemachine.addr.Config           : 市:
2017-09-23 17:55:29.098  INFO 9344 --- [           main] xtipc.statemachine.addr.Config           : 街道:
2017-09-23 17:55:29.098  INFO 9344 --- [           main] xtipc.statemachine.addr.Config           : 街道号:
2017-09-23 17:55:29.099  INFO 9344 --- [           main] xtipc.statemachine.addr.Application      : Started Application in 1.655 seconds (JVM running for 2.133)
2017-09-23 17:55:29.100  INFO 9344 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5fdba6f9: startup date [Sat Sep 23 17:55:27 CST 2017]; root of context hierarchy
2017-09-23 17:55:29.101  INFO 9344 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2017-09-23 17:55:29.102  INFO 9344 --- [       Thread-2] o.s.s.support.LifecycleObjectSupport     : stopped org.springframework.statemachine.support.DefaultStateMachineExecutor@77102b91
2017-09-23 17:55:29.102  INFO 9344 --- [       Thread-2] o.s.s.support.LifecycleObjectSupport     : stopped CITY PROVINCE END DISTRIC STREET_NUM STREET START  /  / uuid=868923f4-239a-4730-ae7b-f71753876f3a / id=null
2017-09-23 17:55:29.103  INFO 9344 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-09-23 17:55:29.112  INFO 9344 --- [       Thread-2] o.s.s.support.LifecycleObjectSupport     : destroy called
目录
相关文章
|
6月前
|
XML 存储 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
109 0
|
25天前
|
Java BI API
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
这篇文章介绍了如何在Spring Boot项目中整合iTextPDF库来导出PDF文件,包括写入大文本和HTML代码,并分析了几种常用的Java PDF导出工具。
258 0
spring boot 整合 itextpdf 导出 PDF,写入大文本,写入HTML代码,分析当下导出PDF的几个工具
|
1月前
|
XML Java 应用服务中间件
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
136 2
|
2月前
|
设计模式 Java Spring
spring源码设计模式分析(五)-策略模式
spring源码设计模式分析(五)-策略模式
|
2月前
|
负载均衡 Java 网络架构
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
实现微服务网关:Zuul与Spring Cloud Gateway的比较分析
79 5
|
2月前
|
消息中间件 设计模式 缓存
spring源码设计模式分析(四)-观察者模式
spring源码设计模式分析(四)-观察者模式
|
2月前
|
设计模式 Java Spring
spring源码设计模式分析(六)-模板方法模式
spring源码设计模式分析(六)-模板方法模式
|
2月前
|
设计模式 Java Spring
spring源码设计模式分析(七)-委派模式
spring源码设计模式分析(七)-委派模式
|
2月前
|
设计模式 Java 数据库
spring源码设计模式分析(八)-访问者模式
spring源码设计模式分析(八)-访问者模式
|
2月前
|
设计模式 搜索推荐 Java
spring源码设计模式分析(三)
spring源码设计模式分析(三)