设为首页 加入收藏

TOP

状态机的介绍和使用(二)
2023-08-06 07:49:48 】 浏览:89
Tags:
,并进行闭包遍历,少写了许多POJO对象,效率更高。

2.3.2 外部DSL

以plantUML为例,外部DSL不受限于宿主语言的语法,对用户很友好,尤其是对于不懂宿主语言语法的用户。但外部DSL的自定义语法需要有配套的语法分析器。常见的语法分析器有:YACC、ANTLR等。

https://github.com/plantuml/plantuml

https://plantuml.com/zh/

2.3.3 DSL & DDD(领域驱动)

DDD和DSL的融合有三点:面向领域、模型的组装方式、分层架构演进。DSL 可以看作是在领域模型之上的一层外壳,可以显著增强领域模型的能力。

它的价值主要有两个,一是提升了开发人员的生产力,二是增进了开发人员与领域专家的沟通。外部 DSL 就是对领域模型的一种组装方式。

3 状态机实现的调研

3.1 Spring Statemachine

官网:https://spring.io/projects/spring-statemachine#learn

源码:https://github.com/spring-projects/spring-statemachine

API:https://docs.spring.io/spring-statemachine/docs/3.2.0/api/

Spring Statemachine is a framework for application developers to use state machine concepts with Spring applications. Spring Statemachine 是应用程序开发人员在Spring应用程序中使用状态机概念的框架。

Spring Statemachine 提供如下特色:

?Easy to use flat one level state machine for simple use cases.(易于使用的扁平单级状态机,用于简单的使用案例。)

?Hierarchical state machine structure to ease complex state configuration.(分层状态机结构,以简化复杂的状态配置。)

?State machine regions to provide even more complex state configurations.(状态机区域提供更复杂的状态配置。)

?Usage of triggers, transitions, guards and actions.(使用触发器、transitions、guards和actions。)

?Type safe configuration adapter.(应用安全的配置适配器。)

?Builder pattern for easy instantiation for use outside of Spring Application context(用于在Spring Application上下文之外使用的简单实例化的生成器模式)

?Recipes for usual use cases(通常用例的手册)

?Distributed state machine based on a Zookeeper State machine event listeners.(基于Zookeeper的分布式状态机状态机事件监听器。)

?UML Eclipse Papyrus modeling.(UML Eclipse Papyrus 建模)

?Store machine config in a persistent storage.(存储状态机配置到持久层)

?Spring IOC integration to associate beans with a state machine.(Spring IOC集成将bean与状态机关联起来)

Spring StateMachine提供了papyrus的Eclipse Plugin,用来辅助构建状态机。

更多Eclipse建模插件可参见文档:https://docs.spring.io/spring-statemachine/docs/3.2.0/reference/#sm-papyrus

Spring状态机的配置、定义、事件、状态扩展、上下文集成、安全性、错误处理等,可以参看如下文档:

https://docs.spring.io/spring-statemachine/docs/3.2.0/reference/#statemachine

3.2 COLA状态机DSL实现

COLA 是 Clean Object-Oriented and Layered Architecture的缩写,代表“整洁面向对象分层架构”。 目前COLA已经发展到COLA v4。COLA提供了一个DDD落地的解决方案,其中包含了一个开源、简单、轻量、性能极高的状态机DSL实现,解决业务中的状态流转问题。

COLA状态机组件实现一个仅支持简单状态流转的状态机,该状态机的核心概念如下图所示,主要包括:

1.State:状态

2.Event:事件,状态由事件触发,引起变化

3.Transition:流转,表示从一个状态到另一个状态

4.External Transition:外部流转,两个不同状态之间的流转

5.Internal Transition:内部流转,同一个状态之间的流转

6.Condition:条件,表示是否允许到达某个状态

7.Action:动作,到达某个状态之后,可以做什么

8.StateMachine:状态机

整个状态机的核心语义模型(Semantic Model):

4 状态机DEMO

4.1 Spring状态机示例

代码地址:http://xingyun.jd.com/codingRoot/ilt/spring-statemachine-demo/

例如,起始节点为SI、结束节点为SF,起始节点后续有S1、S2、S3三个节点的简单状态机。

Spring Boot项目需引入Spring状态机组件。

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





4.1.1 构造状态机

@Configuration
@EnableStateMachine
@Slf4j
public class SimpleStateMachineConfiguration extends StateMachineConfigurerAdapter<String, String> {
    /**
     * 定义初始节点、结束节点和状态节点
     * @param states the {@link StateMachineStateConfigurer}
     * @throws Exception
     */
    @Override
    public void configure(StateMachineStateConfigurer<String, String> states) thr
首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇注意!JAVA中的值传递 下一篇Sprint Boot学习路线2

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目