All Projects → stateless4j → Stateless4j

stateless4j / Stateless4j

Licence: apache-2.0
Lightweight Java State Machine

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Stateless4j

FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (-93.16%)
Mutual labels:  fsm, transition, finite-state-machine
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+3134.95%)
Mutual labels:  fsm, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (-32.22%)
Mutual labels:  finite-state-machine, fsm
Libfsm
DFA regular expression library & friends
Stars: ✭ 512 (-22.19%)
Mutual labels:  finite-state-machine, fsm
visual-automata
Visual Automata is a Python 3 library built as a wrapper for the Automata library to add more visualization features.
Stars: ✭ 55 (-91.64%)
Mutual labels:  fsm, finite-state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-96.2%)
Mutual labels:  fsm, finite-state-machine
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-95.74%)
Mutual labels:  fsm, finite-state-machine
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+188.45%)
Mutual labels:  finite-state-machine, fsm
fsm
Finite State Machine for Go inspired by Akka FSM
Stars: ✭ 59 (-91.03%)
Mutual labels:  fsm, finite-state-machine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-96.2%)
Mutual labels:  fsm, finite-state-machine
django-logic
Django Logic - easy way to implement state-based business logic with pure functions
Stars: ✭ 44 (-93.31%)
Mutual labels:  fsm, transition
raider
OWASP Raider: a novel framework for manipulating the HTTP processes of persistent sessions
Stars: ✭ 88 (-86.63%)
Mutual labels:  fsm, finite-state-machine
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (-70.36%)
Mutual labels:  finite-state-machine, fsm
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-92.86%)
Mutual labels:  fsm, finite-state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (-77.05%)
Mutual labels:  finite-state-machine, fsm
as fsm
A finite state machine implementation for elixir
Stars: ✭ 14 (-97.87%)
Mutual labels:  fsm, finite-state-machine
Automata
A Python library for simulating finite automata, pushdown automata, and Turing machines
Stars: ✭ 121 (-81.61%)
Mutual labels:  finite-state-machine, fsm
Statelin
A finite state machine for Kotlin and Android
Stars: ✭ 134 (-79.64%)
Mutual labels:  finite-state-machine, fsm
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-96.96%)
Mutual labels:  fsm, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (-63.07%)
Mutual labels:  fsm, finite-state-machine

Maven

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

Build Status

Introduction

Create state machines and lightweight state machine-based workflows directly in java code.

StateMachineConfig<State, Trigger> phoneCallConfig = new StateMachineConfig<>();

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 /examples
phoneCallConfig.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 =
        new StateMachine<>(State.OffHook, phoneCallConfig);

phoneCall.fire(Trigger.CallDialed);
assertEquals(State.Ringing, phoneCall.getState());

stateless4j is a port of stateless 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 calling onExit/onEntry)
  • Introspection

Some useful extensions are also provided:

  • Parameterised triggers
  • Reentrant states

Hierarchical States

In the example below, the OnHold state is a substate of the Connected state. This means that an OnHold call is still 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 the StateMachine.getState() property, which will report the precise current state, an isInState(State) method is provided. isInState(State) will take substates into account, so that if the example above was in the OnHold state, isInState(State.Connected) would also evaluate to true.

Entry/Exit Events

In the example, the startCallTimer() method will be executed when a call is connected. The stopCallTimer() will be executed when call completes (by either hanging up or hurling the phone against the wall.)

The call can move between the Connected and OnHold states without the startCallTimer() and stopCallTimer() methods being called repeatedly because the OnHold state is a substate of the Connected state.

Entry/Exit event handlers can be supplied with a parameter of type Transition 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 called without any parameters. For 'dynamic' transitions (those who compute the target state based on trigger-given parameters) the parameters of the trigger will be given to the action.

This action is only executed if the transition is actually taken; so if the transition is guarded and the guard forbids a transition, then the action is not executed.

If the transition is taken, the action will be executed between the onExit handler of the current state and the onEntry handler of the target state (which might be the same state in case of a re-entrant transition.

License

Apache 2.0 License

Created by @oxo42

Maintained by Chris Narkiewicz @ezaquarii

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].