Typed, graph-based state machines for Java 21
Declare the transitions your domain allows as identified edges, give every state a reason for refusing the rest, and let the build validate the whole graph before the application runs.
Version 2.0.6 on Maven Central. Apache License 2.0.
- a new ticketOPENcall an operation to see what the machine answers
For a ticket that has a title and that the review board accepts:
| In state | You call | The machine answers |
|---|---|---|
| OPEN | submit(ticket) | Applied OPEN -SUBMIT-> REVIEW |
| OPEN | approve(ticket) | Rejected NO_TRANSITION approval requires review |
| OPEN | autoApprove(ticket) | Rejected NO_TRANSITION submit the ticket first |
| OPEN | retouch(ticket) | Rejected NO_TRANSITION submit the ticket first |
| OPEN | reject(ticket) | Rejected NO_TRANSITION submit the ticket first |
| REVIEW | submit(ticket) | Rejected NO_TRANSITION approve, reject or retouch the ticket |
| REVIEW | approve(ticket) | Applied REVIEW -APPROVE-> DONE |
| REVIEW | autoApprove(ticket) | Applied REVIEW -AUTO_APPROVE-> DONE |
| REVIEW | retouch(ticket) | Applied REVIEW -RETOUCH-> REVIEW |
| REVIEW | reject(ticket) | Applied REVIEW -REJECT-> OPEN |
| DONE | submit(ticket) | Rejected NO_TRANSITION terminal state DONE accepts no transitions |
| DONE | approve(ticket) | Rejected NO_TRANSITION terminal state DONE accepts no transitions |
| DONE | autoApprove(ticket) | Rejected NO_TRANSITION terminal state DONE accepts no transitions |
| DONE | retouch(ticket) | Rejected NO_TRANSITION terminal state DONE accepts no transitions |
| DONE | reject(ticket) | Rejected NO_TRANSITION terminal state DONE accepts no transitions |
| FAILED | submit(ticket) | Rejected NO_TRANSITION terminal state FAILED accepts no transitions |
| FAILED | approve(ticket) | Rejected NO_TRANSITION terminal state FAILED accepts no transitions |
| FAILED | autoApprove(ticket) | Rejected NO_TRANSITION terminal state FAILED accepts no transitions |
| FAILED | retouch(ticket) | Rejected NO_TRANSITION terminal state FAILED accepts no transitions |
| FAILED | reject(ticket) | Rejected NO_TRANSITION terminal state FAILED accepts no transitions |
Replays the outcomes GraphMaquina 2.0.6 returned for the quickstart's ticket machine.
Install
Each coordinate resolves from Maven Central. The installation page has the full settings and build files.
- Plugin
io.github.graphmaquinaversion2.0.6implementationio.github.graphmaquina:graphmaquina-core: 2.0.6 annotationProcessorio.github.graphmaquina:graphmaquina-processor: 2.0.6
- Dependency
io.github.graphmaquina:graphmaquina-core: 2.0.6 - Annotation processor path
io.github.graphmaquina:graphmaquina-processor: 2.0.6 - Plugin
io.github.graphmaquina:graphmaquina-maven-plugin: 2.0.6
What you write
@TicketsGraph.StatePolicy(state = TicketState.OPEN, otherwise = "submit the ticket first")
// REVIEW's policy is omitted here
@TicketsGraph.Reject(
from = TicketState.OPEN,
transition = TicketTransition.APPROVE,
reason = "approval requires review")
// the DECLINE route is omitted here
public interface Tickets extends Machine<Ticket, TicketState, TicketTransition> {
// SUBMIT moves OPEN to REVIEW; if Register fails, DECLINE takes the ticket to FAILED
@TicketsGraph.Transition(
id = TicketTransition.SUBMIT,
from = TicketState.OPEN,
to = TicketState.REVIEW,
action = TicketActionsRefs.Register.class,
guards = TicketActionsRefs.HasTitle.class,
onFailure = TicketTransition.DECLINE)
Outcome<Ticket, TicketState, TicketTransition> submit(Ticket ticket);
// approve, autoApprove, retouch and reject are omitted here
}- A reason for refusingOPEN answers the calls it does not accept with the reason you wrote.
- An identified edgeSUBMIT is a name you chose, not a pair of states.
- A failure routeWhen the action fails, the ticket takes DECLINE to FAILED.