com.github.kabal163:state-machine-spring-boot-samples

Implementation of the "State Machine" pattern. It makes easy to develop services where entities have finite number of states and set of transition rules from one state to another.

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.github.kabal163
ArtifactId

ArtifactId

state-machine-spring-boot-samples
Last Version

Last Version

0.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

Implementation of the "State Machine" pattern. It makes easy to develop services where entities have finite number of states and set of transition rules from one state to another.

Download state-machine-spring-boot-samples

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.kabal163/state-machine-spring-boot-samples/ -->
<dependency>
    <groupId>com.github.kabal163</groupId>
    <artifactId>state-machine-spring-boot-samples</artifactId>
    <version>0.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.kabal163/state-machine-spring-boot-samples/
implementation 'com.github.kabal163:state-machine-spring-boot-samples:0.3.0'
// https://jarcasting.com/artifacts/com.github.kabal163/state-machine-spring-boot-samples/
implementation ("com.github.kabal163:state-machine-spring-boot-samples:0.3.0")
'com.github.kabal163:state-machine-spring-boot-samples:jar:0.3.0'
<dependency org="com.github.kabal163" name="state-machine-spring-boot-samples" rev="0.3.0">
  <artifact name="state-machine-spring-boot-samples" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.kabal163', module='state-machine-spring-boot-samples', version='0.3.0')
)
libraryDependencies += "com.github.kabal163" % "state-machine-spring-boot-samples" % "0.3.0"
[com.github.kabal163/state-machine-spring-boot-samples "0.3.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.github.kabal163 : state-machine-spring-boot-starter jar 0.3.0

test (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar

Project Modules

There are no modules declared in this project.

Akuna State Machine

The state machine is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. This library helps you to implement the model.

Terminology

Stateful object - any entity which implements StatefulObject interface and has finite numbers of states.

Source state - the state of an entity in the moment of starting transition process.

Target state - the desired state of an entity after finish of transition.

Event - any signal notifying about some event.

Transition - changing the entity's state from source to target according the incoming event.

State context - a context which contains all necessary information for the transition.

Action - the step of transition which performs some logic. If any action fails then the whole transition will be treated as failed.

Condition - a rule which determines possibility of transition at this moment. If any condition returns false then transition will not be performed.

Add dependency in your project

<dependency>
    <groupId>com.github.kabal163</groupId>
    <artifactId>state-machine</artifactId>
    <version>0.4.1</version>
</dependency>

You need to implement LifecycleConfiguration and configure your lifecycle.

The main class you interact with is LifecycleManager. It responsible for applying appropriate transition to your entity according entities current state and incoming event.

You need to create it using its default implementation LifecycleManagerImpl.

@Bean
public LifecycleManager<State, Event> myLifecycleManager(LifecycleConfiguration<State, Event> configuration) {
    Map<String, Lifecycle<State, Event>> lifecyclesByName = new JavaConfigLifecyclesInitializer().initialize(singleton(configuration));
    TransitionProvider<State, Event> transitionProvider = new TransitionProviderImpl<>(lifecyclesByName);
    return new LifecycleManagerImpl<>(transitionProvider);
}

So, in order to create the LifecycleManager you need to:

  1. Implement LifecycleConfiguration and describe your lifecycle
  2. Use JavaConfigLifecyclesInitializer in order to create Lifecycle from the LifecycleConfiguration
  3. Create TransitionProvider and pass the created lifecycle to it.
  4. Create the LifecycleManager and pass the TransitionProvider to it.

That's all.

In order to perform a transition you need to use the execute method of LifecycleManager. It receives the stateful object, event and optionally a map of any variables which are needed during a transition. LifecycleManager will put those variables to the state context. You can use the state context in your conditions and actions. Also, you can share some data between conditions and actions via the state context.

Example:

lifecycleManager.execute(myStatefulObject, MY_EVENT)

This method returns TransitionResult which contains information about performed transition.

Versions

Version
0.3.0
0.2.1
0.2
0.1