Unlocking the Power of DSLs: Stateless State Machines

DSL

What is a DSL?

/\d{3}-\d{3}-\d{4}/

Categories of DSLs:

builder.externalTransition()
.from(States.STATE1)
.to(States.STATE2)
.on(Events.EVENT1)
.when(checkCondition())
.perform(doAction());

How to Choose between DSLs

Fluent Interfaces

when(mockedList.get(anyInt())).thenReturn("element")
when(mockedList.get(anyInt())).thenReturn("element")
String element = mockedList.get(anyInt());
boolean isExpected = "element".equals(element);
OkHttpClient.Builder builder=new OkHttpClient.Builder();
OkHttpClient okHttpClient=builder
.readTimeout(5*1000, TimeUnit.SECONDS)
.writeTimeout(5*1000, TimeUnit.SECONDS)
.connectTimeout(5*1000, TimeUnit.SECONDS)

State Machines

State Machine Selection

Open-Source State Machines are Too Complex

Open-Source State Machines have Poor Performance

Implement the State Machine

State Machine Domain Model

//StateMachine
public class StateMachineImpl<S,E,C> implements StateMachine<S, E, C> {
private String machineId;
private final Map<S, State<S,E,C>> stateMap;
...
}
//State
public class StateImpl<S,E,C> implements State<S,E,C> {
protected final S stateId;
private Map<E, Transition<S, E,C>> transitions = new HashMap<>();
...
}
//Transition
public class TransitionImpl<S,E,C> implements Transition<S,E,C> {
private State<S, E, C> source;
private State<S, E, C> target;
private E event;
private Condition<C> condition;
private Action<S,E,C> action;
...
}

Fluent API for Creating the State Machine

class  TransitionBuilderImpl<S,E,C> implements ExternalTransitionBuilder<S,E,C>, InternalTransitionBuilder<S,E,C>, From<S,E,C>, On<S,E,C>, To<S,E,C> {    
...
@Override
public From<S, E, C> from(S stateId) {
source = StateHelper.getState(stateMap,stateId);
return this;
}
@Override
public To<S, E, C> to(S stateId) {
target = StateHelper.getState(stateMap, stateId);
return this;
}
...
}

Stateless Design of the State Machine

Use the State Machine

StateMachineBuilder<States, Events, Context> builder = StateMachineBuilderFactory.create();
//external transition
builder.externalTransition()
.from(States.STATE1)
.to(States.STATE2)
.on(Events.EVENT1)
.when(checkCondition())
.perform(doAction());
//internal transition
builder.internalTransition()
.within(States.STATE2)
.on(Events.INTERNAL_EVENT)
.when(checkCondition())
.perform(doAction());
//external transitions
builder.externalTransitions()
.fromAmong(States.STATE1, States.STATE2, States.STATE3)
.to(States.STATE4)
.on(Events.EVENT4)
.when(checkCondition())
.perform(doAction());

builder.build(machineId);

Original Source:

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Make a Game

Intelligently Generate Frontend Code from Design Files: imgcook

How to use Firebase with Flutter

RayML using Kubernetes operator

SALESFORCE LIGHTNING KNOWLEDGE OF NEW FEATURES

ProtonDate — Write up (HeroCTF)

A Complete Review of Philo Streaming TV Services

How I’ve Adapted my VSCode Workspace

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Improving Apache HTTP Server’s performance using ListenCoresBucketsRatio directive

Adding/Replacing Cassandra Nodes, You might wanna cleanup

Practical Progressive Delivery with Argo Rollouts — Canary

Zero Touch Provisioning IOT or Other Devices on cloud