Spring StateMachine(2) UML状态图支持

栏目: Java · 发布时间: 5年前

内容简介:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kmyhy/article/details/84787556

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kmyhy/article/details/84787556

还是刚才的以二级审批请假流程为例。

绘制流程

首先创建 Papyrus 项目,选择 StateMachine 模板,绘制流程图如下:

Spring StateMachine(2) UML状态图支持

然后创建 6 个 signal event 和与之绑定的 signal :

Spring StateMachine(2) UML状态图支持

注意,在 Papyrus 图中,除了 State (状态) 和 Transition(迁移)之外,还有 Signal 和 Signal Event 的概念,它们对应于 StateMachine 中的 Event(事件)。其中 Signal 就是事件的名字(字符串常量)。

将 Tansition 和 Signal Event 绑定(设置 trigger)。

加载 uml 状态图

将 Papyrus 生成的 uml 文件放到 idea 的 resource 目录。

因为 State、Event 和 Transition 都定义在 UML 中了,所以 States 枚举和 Events 枚举不再需要,状态机的配置也简化了,直接从 UML 文件来加载:

@Override
    public void configure(StateMachineModelConfigurer<String, String> model) throws Exception {
        model
                .withModel()
                .factory(modelFactory());
    }

    @Bean
    public StateMachineModelFactory<String, String> modelFactory() {
        // 测试二级审批请假流程
        UmlStateMachineModelFactory factory = new UmlStateMachineModelFactory(
                "classpath:model.uml");
        factory.setStateMachineComponentResolver(stateMachineComponentResolver());
        return factory;
    }

注意,这里 States 和 Events 的类型都变成了 String,毕竟在 UML 中不太好定义类型。

如果要注册 Action 或者 Guard,可以通过 Resolver 进行:

@Bean
    public StateMachineComponentResolver<String, String> stateMachineComponentResolver() {
        DefaultStateMachineComponentResolver<String, String> resolver = new DefaultStateMachineComponentResolver<>();
        resolver.registerAction("myAction", myAction());
        resolver.registerGuard("myGuard", myGuard());
        return resolver;
    }

    public Action<String, String> myAction() {
        return new Action<String, String>() {

            @Override
            public void execute(StateContext<String, String> context) {
            }
        };
    }

    public Guard<String, String> myGuard() {
        return new Guard<String, String>() {

            @Override
            public boolean evaluate(StateContext<String, String> context) {
                return false;
            }
        };
    }

发送事件

跟之前一样,发送事件使用状态机的 sendEvent。只不过 Event 的类型统统是字符串,而且必须在 UML 中定义为 Signal。

private BaseResponse sendEvent(String event,String leaveId){
        BaseResponse result = new BaseResponse();

        if(leaveId == null || leaveId.length()==0){
            result.success = false;
            result.message = "leaveId 不能为空";
            return result;
        }

        try {
            // 根据业务 id 获取状态
            persister.restore(stateMachine,leaveId);

            result.success = stateMachine.sendEvent(event);
            // 持久化状态机
            if (result.success) {
                persister.persist(stateMachine, leaveId);
            }
            JSONObject data = new JSONObject();

            result.message = result.success ? "执行成功":"执行失败";
            result.message = result.message + ",当前状态为:"+stateMachine.getState().getId();
            data.put("leaveId",leaveId);
            data.put("event",event);
            data.put("state",stateMachine.getState().getId());
            result.data = data;
        } catch (Exception e) {
            e.printStackTrace();
            result.message = e.getMessage();
        }finally {
            stateMachine.stop();
            return result;
        }
    }

可以看到,通过 UML 方式绘制状态机,减少了在代码中定义 Event、State 和 Transition 的过程,但 UML 中必须通过额外的元素(Signal和 Signal Event)来定义事件,在目前来说 Spring StateMachine 只支持 Papyrus 绘制的 UML 图。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

How to Design Programs, 2nd Edition

How to Design Programs, 2nd Edition

Matthias Felleisen、Robert Bruce Findler、Matthew Flatt、Shriram Krishnamurthi / MIT Press / 2018-5-4 / USD 57.00

A completely revised edition, offering new design recipes for interactive programs and support for images as plain values, testing, event-driven programming, and even distributed programming. This ......一起来看看 《How to Design Programs, 2nd Edition》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具