Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lightweight Java State Machine

License

NotificationsYou must be signed in to change notification settings

stateless4j/stateless4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

    <dependency>        <groupId>com.github.stateless4j</groupId>        <artifactId>stateless4j</artifactId>        <version>2.6.0</version>    </dependency>

Introduction

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

Features

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 callingonExit/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.

Hierarchical States

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.

Entry/Exit Events

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.

Action on transition

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.

License

Apache 2.0 License

Created by@oxo42

Maintained by Chris Narkiewicz@ezaquarii


[8]ページ先頭

©2009-2025 Movatter.jp