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

标签: 有限状态机statemachinespring
808人阅读 评论(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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    《从Lucene到Elasticsearch:全文检索实战》
    Lucene、ES、ELK开发交流群:370734940
    Lucene、ES、ELK开发交流
    个人资料
    • 访问:811082次
    • 积分:8389
    • 等级:
    • 排名:第2473名
    • 原创:196篇
    • 转载:2篇
    • 译文:6篇
    • 评论:446条
    StackOverFlow
    http://stackoverflow.com/users/6526424
    统计
    博客专栏
    文章分类
    最新评论