This project provides a custom state machine implementation in Java, designed to manage complex workflows with states, events, guards, and actions. The state machine is built from scratch without relying on external libraries and features a fluent API for defining transitions.
The state machine allows you to:
- Define states and events.
- Create transitions between states triggered by events.
- Add guards to validate conditions before allowing transitions.
- Define actions to perform operations during transitions.
- Use a fluent API for easy configuration.
- Fluent API: Chain methods like
.from(),.to(),.on(),.when(), and.perform(). - Guards: Validate conditions before allowing a transition.
- Actions: Perform operations during transitions.
- Custom Context: Pass dynamic data with events using a context object.
- Lightweight: No external dependencies.
- Java Development Kit (JDK) 8 or later.
- Apache Maven installed.
Clone the repository:
git clone https://github.com/Anas2001/bstate-machine.gitNavigate to the project directory:
cd state-machineBuild the project using Maven:
mvn clean installDefine your states and events for an order processing system using enums:
public enum OrderState {
ORDER_PLACED,
PROCESSING,
SHIPPED,
DELIVERED,
CANCELED
}
public enum OrderEvent {
PROCESS_ORDER,
SHIP_ORDER,
DELIVER_ORDER,
CANCEL_ORDER
}Use the StateMachineBuilder to define transitions:
StateMachineBuilder<OrderState, OrderEvent, OrderContext> builder = new StateMachineBuilder<>(OrderState.ORDER_PLACED, new OrderContext());
builder.externalTransition()
.from(OrderState.ORDER_PLACED)
.to(OrderState.PROCESSING)
.on(OrderEvent.PROCESS_ORDER)
.perform(context -> System.out.println("Order is now being processed"))
.buildTransition();
builder.externalTransition()
.from(OrderState.PROCESSING)
.to(OrderState.SHIPPED)
.on(OrderEvent.SHIP_ORDER)
.perform(context -> System.out.println("Order has been shipped"))
.buildTransition();
builder.externalTransition()
.from(OrderState.SHIPPED)
.to(OrderState.DELIVERED)
.on(OrderEvent.DELIVER_ORDER)
.perform(context -> System.out.println("Order has been delivered"))
.buildTransition();
builder.externalTransition()
.from(OrderState.ORDER_PLACED)
.to(OrderState.CANCELED)
.on(OrderEvent.CANCEL_ORDER)
.perform(context -> System.out.println("Order has been canceled"))
.buildTransition();
StateMachine<OrderState, OrderEvent, OrderContext> stateMachine = builder.build();Use the StateMachine to fire events and trigger transitions:
if (stateMachine.fire(OrderEvent.PROCESS_ORDER)) {
System.out.println("Order moved to PROCESSING state.");
}
if (stateMachine.fire(OrderEvent.SHIP_ORDER)) {
System.out.println("Order moved to SHIPPED state.");
}
if (stateMachine.fire(OrderEvent.DELIVER_ORDER)) {
System.out.println("Order moved to DELIVERED state.");
}- Define States and Events:
- Create enums for states (
OrderState) and events (OrderEvent).
- Create enums for states (
- Define the Context:
- Create a class (
OrderContext) to manage order-specific data.
- Create a class (
- Build the State Machine:
- Use the
StateMachineBuilderto define transitions with guards and actions.
- Use the
- Fire Events:
- Use the
StateMachineto trigger transitions by firing events.
- Use the
- Test and Debug:
- Verify transitions through logs and test cases.
- Initial State:
ORDER_PLACED - Event:
PROCESS_ORDER- Action: Transition to
PROCESSINGand log the status.
- Action: Transition to
- Event:
SHIP_ORDER- Action: Transition to
SHIPPEDand notify the customer.
- Action: Transition to
- Event:
DELIVER_ORDER- Action: Transition to
DELIVEREDand mark order as complete.
- Action: Transition to
- Event:
CANCEL_ORDER(only valid fromORDER_PLACED)- Action: Transition to
CANCELEDand issue a refund if necessary.
- Action: Transition to
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request with a detailed description of your changes.
This project is licensed under the MIT License. See the LICENSE file for details.