- Notifications
You must be signed in to change notification settings - Fork191
Lightweight Java State Machine
License
stateless4j/stateless4j
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
<dependency> <groupId>com.github.stateless4j</groupId> <artifactId>stateless4j</artifactId> <version>2.6.0</version> </dependency>
Createstate machines and lightweight state machine-based workflowsdirectly in java code.
StateMachineConfig<State,Trigger>phoneCallConfig =newStateMachineConfig<>();phoneCallConfig.configure(State.OffHook) .permit(Trigger.CallDialed,State.Ringing);phoneCallConfig.configure(State.Ringing) .permit(Trigger.HungUp,State.OffHook) .permit(Trigger.CallConnected,State.Connected);// this example uses Java 8 method references// a Java 7 example is provided in /examplesphoneCallConfig.configure(State.Connected) .onEntry(this::startCallTimer) .onExit(this::stopCallTimer) .permit(Trigger.LeftMessage,State.OffHook) .permit(Trigger.HungUp,State.OffHook) .permit(Trigger.PlacedOnHold,State.OnHold);// ...StateMachine<State,Trigger>phoneCall =newStateMachine<>(State.OffHook,phoneCallConfig);phoneCall.fire(Trigger.CallDialed);assertEquals(State.Ringing,phoneCall.getState());
stateless4j is a port ofstateless for java
Most standard state machine constructs are supported:
- Generic support for states and triggers of any java type (numbers, strings, enums, etc.)
- Hierarchical states
- Entry/exit events for states
- Guard clauses to support conditional transitions
- User-defined actions can be executed when transitioning
- Internal transitions (not calling
onExit/onEntry) - Introspection
Some useful extensions are also provided:
- Parameterised triggers
- Reentrant states
Parallel states are not supported, but if you are looking for it, there is a fork that supports it:ParallelStateless4j.
In the example below, theOnHold state is a substate of theConnected state. This means that anOnHold call isstill connected.
phoneCall.configure(State.OnHold) .substateOf(State.Connected) .permit(Trigger.TakenOffHold,State.Connected) .permit(Trigger.HungUp,State.OffHook) .permit(Trigger.PhoneHurledAgainstWall,State.PhoneDestroyed);
In addition to theStateMachine.getState() property, which will report the precise current state, anisInState(State)method is provided.isInState(State) will take substates into account, so that if the example above was in theOnHold state,isInState(State.Connected) would also evaluate totrue.
In the example, thestartCallTimer() method will be executed when a call is connected. ThestopCallTimer() will beexecuted when call completes (by either hanging up or hurling the phone against the wall.)
The call can move between theConnected andOnHold states without thestartCallTimer() andstopCallTimer()methods being called repeatedly because theOnHold state is a substate of theConnected state.
Entry/Exit event handlers can be supplied with a parameter of typeTransition that describes the trigger,source and destination states.
It is possible to execute a user-defined action when doing a transition.For a 'normal' or 're-entrant' transition this action will be calledwithout any parameters. For 'dynamic' transitions (those who compute thetarget state based on trigger-given parameters) the parameters of thetrigger will be given to the action.
This action is only executed if the transition is actually taken; so ifthe transition is guarded and the guard forbids a transition, then theaction is not executed.
If the transition is taken, the action will be executed between theonExit handler of the current state and theonEntry handler of thetarget state (which might be the same state in case of a re-entranttransition.
Apache 2.0 License
Created by@oxo42
Maintained by Chris Narkiewicz@ezaquarii
About
Lightweight Java State Machine
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.