simple state machine


License

License

GroupId

GroupId

com.loyayz
ArtifactId

ArtifactId

simple-statemachine
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

simple state machine
simple state machine
Project URL

Project URL

https://github.com/loyayz/simple-statemachine
Source Code Management

Source Code Management

https://github.com/loyayz/simple-statemachine

Download simple-statemachine

How to add to project

<!-- https://jarcasting.com/artifacts/com.loyayz/simple-statemachine/ -->
<dependency>
    <groupId>com.loyayz</groupId>
    <artifactId>simple-statemachine</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.loyayz/simple-statemachine/
implementation 'com.loyayz:simple-statemachine:1.0.1'
// https://jarcasting.com/artifacts/com.loyayz/simple-statemachine/
implementation ("com.loyayz:simple-statemachine:1.0.1")
'com.loyayz:simple-statemachine:jar:1.0.1'
<dependency org="com.loyayz" name="simple-statemachine" rev="1.0.1">
  <artifact name="simple-statemachine" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.loyayz', module='simple-statemachine', version='1.0.1')
)
libraryDependencies += "com.loyayz" % "simple-statemachine" % "1.0.1"
[com.loyayz/simple-statemachine "1.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.18.18

test (1)

Group / Artifact Type Version
junit : junit jar 4.13.2

Project Modules

There are no modules declared in this project.

simple-statemachine

Maven central

简单通用的状态机,解决业务中的状态流转问题。

1 实现原理

实现原理

2 安装

<dependencies>
  <dependency>
    <groupId>com.loyayz</groupId>
    <artifactId>simple-statemachine</artifactId>
    <version>1.0.1</version>
  </dependency>
</dependencies>

3 快速开始

3.1 定义状态机

  // 状态
  enum States {
    STATE1, STATE2, STATE3,STATE4
  }
  // 事件
  enum Events {
      EVENT1, EVENT2, EVENT3
  }
  // 状态机
  public void define() {
    StatemachineBuilder.<States, Events, String>create()
          // STATE1 -> STATE2 on EVENT1
          .newTransition()
          .from(States.STATE1)
          .to(States.STATE2)
          .on(Events.EVENT1)
          .when((ctx) -> true)
          .then((from, to, event, ctx) -> System.out.println("ctx [" + ctx + "], " + from + " -> " + to + " on " + event))
          .build()
          .register("my_first_statemachine");
  }

3.2 使用

  // 获取状态机
  Statemachine<States, Events, String> statemachine = StatemachineFactory.get("my_first_statemachine");
  // 执行状态机
  statemachine.execute(States.STATE1, Events.EVENT1, "test");

使用示例

Versions

Version
1.0.1
1.0.0