-
Notifications
You must be signed in to change notification settings - Fork 1
Utilizing Automata4j
pavl_g edited this page Aug 15, 2023
·
19 revisions
This page describes the use of Automata4j to implement both forms of Finite-State-Automata, the Non-deterministic, and Deterministic patterns.
This tutorial discusses the following:
- Create a Gradle project and add the dependencies.
- The non-deterministic pattern (NDFSA).
- The deterministic pattern (DFSA).
- Injecting an NDFSA path into the DFSA pattern.
- Create a Gradle project and add the dependencies:
# initialize a gradle project and build
$ gradle init # select Java and JUnit
$ ./gradlew build
// add the Gradle dependency in build.gradle file
plugins {
id 'application'
}
application {
mainClass = 'com.example.Main'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
implementation "io.github.software-hardware-codesign:automata4j:1.0.0-alpha"
}
# download the binaries and re-build the project
$ ./gradlew build
- The non-deterministic pattern (NDFSA):
- Recall, a SpaceShip traveling from point A to point B with speed [x].
- Recall, point A is an Engine Start state, and point B is a Docking state.
- Recall, the path marked from point A to point B is the transition path with an input value [x] representing the speed of the SpaceShip.
- An NDFSA pattern can have multiple start states and doesn't assert the uniqueness of a transition path, which means a transition path from point A to point B with an input value [x] may repeat without any runtime consequences.
- In this case, if a transition path is defined from point B back to point A with speed [x], the SpaceShip can return back from point A to point B using the previous transition path without any runtime consequences.
To implement this in a Game Loop using an NDFSA pattern, the following implementation routines are available:
- Use a
Transition
object with a Next State assigned as the start state (Point A), and aTransitionalListener
to assign the concurrent state (Point B) when the transition to the start state has been completed.
/* define an NDFSA manager object */
final TransitionalManager transitionalManager = new TransitionalManager();
/* creates a state defining the transition between the 2 points (A, B) */
/* the input value is of type Speed and the tracer object is of type Vector3f
representing the location of the space ship after traveling the required distance */
/* travels from point A to point B */
final AutoState<Speed, Vector3f> stateAB = new TravelPath<>();
/* travels from point B back to point A*/
final AutoState<Speed, Vector3f> stateBA = new TravelPath<>();
/* Assigns the start state */
transitionalManager.assignNextState(stateAB);
/* Use the NDFSA transitional manager to traverse through the states with pre-defined input values */
transitionalManager.transit(new TransitionalListener<Speed, Vector3f>() {
@Override
public void onTransition(AutoState<Speed, Vector3f> presentState) {
/* assigns the next state and starts the transition */
transitionalManager.assignNextState(stateBA);
/* transits to stateBA without dispatching an event on finishing transition */
transitionalManager.transit(null);
}
});
- Use a
TransitionPath
object with 2 AutoStates, a present state, and a next state, theTransitionalManager
assigns and transits to the present state and then assigns the next state, but it will never transit to the next state unless an explicit dispatch to the transit function is invoked in a registeredTransitionalListener
object.
-
The deterministic pattern (DFSA):
-
Injecting an NDFSA path into the DFSA pattern:
-
For advanced usages, see the examples: