All Projects → UnquietCode → Jstate

UnquietCode / Jstate

Licence: mit
Advanced state machines in Java.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Jstate

simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-70.24%)
Mutual labels:  fsm, state-machine, finite-state-machine
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (+132.14%)
Mutual labels:  state-machine, finite-state-machine, fsm
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (+34.52%)
Mutual labels:  state-machine, finite-state-machine, fsm
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+189.29%)
Mutual labels:  fsm, state-machine, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+430.95%)
Mutual labels:  state-machine, finite-state-machine, fsm
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (+4.76%)
Mutual labels:  state-machine, finite-state-machine, fsm
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (+79.76%)
Mutual labels:  state-machine, finite-state-machine, fsm
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+2159.52%)
Mutual labels:  state-machine, finite-state-machine, fsm
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+25240.48%)
Mutual labels:  fsm, state-machine, finite-state-machine
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (-46.43%)
Mutual labels:  fsm, state-machine, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-44.05%)
Mutual labels:  fsm, state-machine, finite-state-machine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-76.19%)
Mutual labels:  fsm, state-machine, finite-state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-70.24%)
Mutual labels:  fsm, state-machine, finite-state-machine
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-66.67%)
Mutual labels:  fsm, state-machine, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+215.48%)
Mutual labels:  state-machine, finite-state-machine, fsm
ember-fsm
[Maintenance Mode] A promise-aware finite state machine implementation for Ember
Stars: ✭ 37 (-55.95%)
Mutual labels:  fsm, state-machine
qm
QM model-based design tool and code generator based on UML state machines
Stars: ✭ 54 (-35.71%)
Mutual labels:  fsm, state-machine
xstate-cpp-generator
C++ State Machine generator for Xstate
Stars: ✭ 33 (-60.71%)
Mutual labels:  fsm, state-machine
Hal
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down
Stars: ✭ 63 (-25%)
Mutual labels:  state-machine, finite-state-machine
fea state machines
A Buffet Of C++17 State Machines
Stars: ✭ 19 (-77.38%)
Mutual labels:  fsm, state-machine

JState (v3.1) Build Status

A core Java tool which provides state machine semantics using enums, strings, or anything else you want to represent the various states. States have transitions which can move them to other states. Callbacks are provided for transitions, and for each state when entering or exiting. It is also possible to route a transition request based on your own logic. You can even provide a callback which will fire when a sequence of states is matched.

All of the methods which modify, transition, or inquire about the state are synchronized, allowing multiple threads access to the same state machine. However, to avoid unpredictable behavior, it is generally better to construct your state machine up front and not modify it thereafter. The EnumStateMachine and StringStateMachine in particular can be serialized to and from their string representations.

As of version 3.0, the minimum version of Java required is JDK 8.

Installation

The project is built using Maven, and the artifacts are available from Maven Central. (If you are a current user of the tool, note that the group name has been recently changed to accommodate Sonatype's repository hosting requirements.)

<dependency>
    <groupId>com.unquietcode.tools.jstate</groupId>
    <artifactId>jstate</artifactId>
    <version>3.1</version>
</dependency>

Documentation

You can view the provided Javadocs or the unit tests for more information about how to use the library.

Usage

A typical use case might be a state machine for controlling a process, which can move between the states [Ready, Running, Paused, Stopping, Stopped, Finished].

State Diagram

After declaring a state enum we can set up a new state machine as follows:

enum State {
    Ready, Running, Paused, Stopping, Stopped, Finished
}

...

EnumStateMachine<State> esm = new EnumStateMachine<>(State.Ready);
esm.addTransitions(State.Ready, State.Running, State.Finished);
esm.addTransitions(State.Running, State.Paused, State.Stopping);
esm.addTransitions(State.Paused, State.Running, State.Stopping);
esm.addTransitions(State.Stopping, State.Stopped);
esm.addTransitions(State.Stopped, State.Finished);
esm.addTransitions(State.Finished, State.Ready, null);

esm.transition(State.Running);

The initial state is set either in the constructor or the setInitialState(...) method. The addTransition(...) method supports mapping from 1..n states. In the example above, we see that some states can move to more than one other states. The null state is also a possibility, depending on your preference.

Callbacks can be added as transitions are defined, and fire during transition between states:

TransitionHandler<State> cb = new TransitionHandler<>() {
    public void onTransition(State from, State to) {
        // ....
    }
};

esm.addTransitions(cb, State.Ready, State.Running);

Callbacks can also be added on entering or exiting a state.

esm.onEntering(State.Running, new StateHandler<State>() {
	public void onState(State state) {
		entered.incrementAndGet();
	}
});

esm.onExiting(State.Running, new StateHandler<State>() {
	public void onState(State state) {
		exited.incrementAndGet();
	}
});

StateRouters allow you to 'deflect' or 'redirect' a transition based on your own custom logic. There are several pre-defined routers available which provide round-robin and randomized routing.

esm.routeBeforeEntering(TestStates.Three, new StateRouter<TestStates>() {
	public TestStates route(TestStates current, TestStates next) {
		return TestStates.Two;
	}
});

SequenceHandlers are callbacks which are triggered whenever the specified sequence of states occurs in the state machine.

final List<Color> _pattern = Arrays.asList(Color.Blue, Color.Green, Color.Orange);

sm.onSequence(_pattern, new SequenceHandler<Color>() {
	public void onMatch(List<Color> pattern) {
		// pattern equals [Blue, Green, Orange]
	}
});

There is also support for wildcard matching in sequences, available through the use of the PatternBuilder class.

final Pattern<Color> _pattern = PatternBuilder.<Color>create()
	.add(Color.Red, Color.Blue)
	.addWildcard()
	.add(Color.Green)
.build();

sm.onSequence(_pattern, new SequenceHandler<Color>() {
	public void onMatch(List<Color> pattern) {
		// pattern equals [Red, Blue, Purple, Green]
	}
});

A special form of StringStateMachine (which uses strings as states) is available as the ReflectiveStateMachine. This flavor allows you to declare your callbacks as methods of the state machine class. The arguments are flexible, matching the standalone callback method's signature and allowing you to skip parameters you don't care about.

ReflectiveStateMachine sm = new ReflectiveStateMachine() {

	// (optional method to declare transitions inline)
	protected void declareTransitions() {
		addTransition(null, "blue");
		addTransition("blue", "green");
		addTransition("green", null);
	}

	public void onEnteringBlue(String state) {
		enteringBlue.incrementAndGet();
	}

	public void onExitingBlue() {
		exitingBlue.incrementAndGet();
	}

	public void onGreen() {
		enteringGreen.incrementAndGet();
	}

	public void onEntering() {
		enteringAny.incrementAndGet();
	}

	public void onExiting() {
		exitingAny.incrementAndGet();
	}

	public void onTransition() {
		transitionAny.incrementAndGet();
	}
};

sm.transition("blue");
sm.transition("green");
sm.transition(null);

See the tests for more usage examples.

License

JState is licensed under the MIT license. Go wild.

Questions / Comments / Feedback

Send an email to [email protected]

Peace, love, and code.

Thanks!

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].