关闭

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

标签: 有限状态机statemachinespring
1239人阅读 评论(0) 收藏 举报
分类:

一、有限状态机

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

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

这里写图片描述

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

这里写图片描述

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

例如:北京市海淀区清华东路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
1
0
查看评论

使用Spring StateMachine框架实现状态机

使用Spring StateMachine框架实现状态机  2016-12-18  Spring Boot 被围观 1423 次 Spring StateMachine框架可能对于大部分使用Spring的开发者来说还比较生僻,该...
  • baochanghong
  • baochanghong
  • 2017-01-09 10:00
  • 5721

使用Spring StateMachine框架实现状态机

spring statemachine刚出来不久,但是对于一些企业的大型应用的使用还是十分有借鉴意义的。 最近使用了下这个,感觉还是挺好的。 下面举个例子来说下吧:    创建一个Spring Boot的基础工程,并在pom.xml中加入spring-statemachine-core的依...
  • m0_37138008
  • m0_37138008
  • 2017-07-09 11:52
  • 1096

通过spring statemmachine 自定义构建属于自己的状态机(两种方式)

通过自定义方式构建状态机
  • m0_37138008
  • m0_37138008
  • 2017-08-13 14:05
  • 1295

基于Spring-statemachine的有限状态机(FSM)的介绍及示例

前言本文主要介绍一下状态机以及相关的一些概念。结合一个简单的订单状态流程,示例怎样在Springboot中集成Spring-statemachine。有限状态机(Finite-state machine)有限状态机(英语:finite-state machine,缩写:FSM),简称状态机,是表示有...
  • lijingyao8206
  • lijingyao8206
  • 2017-11-26 20:31
  • 18510

Spring StateMachine 介绍

Spring Statemachine (Spring 状态机)是使用 Spring框架下的状态机概念创建的一种应用程序开发框架。它使得状态机结构层次化,简化了配置状态机的过程。 这里的状态机,不是简单的有限自动状态机,是UML状态图里面的状态机概念的具体实现。不懂状态机概念可以...
  • c601097836
  • c601097836
  • 2015-10-30 22:53
  • 5112

使用Spring StateMachine框架实现状态机

http://blog.csdn.net/baochanghong/article/details/54286298
  • wuhenzhangxing
  • wuhenzhangxing
  • 2017-11-09 13:25
  • 516

StateMachine状态机

状态机在quick中是一个亮点,如果我们做一款RPG游戏,一个角色一般会拥有idle,attack,walk,run,death这些状态,如果游戏角色的状态采用分支条件判断的话,会造成非常庞大而难以维护,但一旦使用了状态机这种模式,就会显得简单方便。 对于quick中的状态机是如何实现的咱...
  • shieryueqing
  • shieryueqing
  • 2014-10-31 17:04
  • 540

spring statemachine 1.2.6 API (CHM格式)

  • 2017-08-20 14:27
  • 1.01MB
  • 下载

spring boot 全面的样例代码

  • 2017-05-10 12:47
  • 545KB
  • 下载

转载的spring cloud的全家桶,有空学习下

Spring Cloud Config:配置管理开发工具包,可以让你把配置放到远程服务器,目前支持本地存储、Git以及Subversion。  Spring Cloud Bus:事件、消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署...
  • ivan0609
  • ivan0609
  • 2017-06-22 14:55
  • 1359
    《从Lucene到Elasticsearch:全文检索实战》
    Lucene、ES、ELK开发交流群:370734940
    Lucene、ES、ELK开发交流
    个人资料
    • 访问:929801次
    • 积分:9210
    • 等级:
    • 排名:第2389名
    • 原创:209篇
    • 转载:2篇
    • 译文:6篇
    • 评论:467条
    StackOverFlow
    http://stackoverflow.com/users/6526424
    统计
    博客专栏
    文章分类
    最新评论