I am working on a Spring boot application, where I need to implement a state transition table in the following format, where the combination of action and condition determines the transition of the entity from one state to another state:
From State| To State| User Action | Condition
----------------------------------------------------------------
S0 | S1 | Create a record |
-----------------------------------------------------------------
S1 | S1 | Update a record |
-----------------------------------------------------------------
S1 | S1 | Run validation |
-----------------------------------------------------------------
S1 | S2 | Mark state as S2 | Record is valid
In this application, there are already APIs for Create, Update, and Running Validation.
As per the requirement, the state machine should be configurable, so that any new state can be added/existing state can be deleted as well as action and condition can be changed for a given transition through the dashboard.
To accomodate this requirement, I have thought of considering State, Action, Condition and StateTransition as separate entities as follows:
Action entity:
@Entity
@Table(name = "action")
public class Action implements Serializable {
@Id
private String id;
@Column(name = "action_type")
@Enumerated(EnumType.STRING)
private ActionType actionType;
@Column(name = "action_description")
private String actionDescription;
}
Action type enum:
Enum ActionType {
CREATE_RECORD, UPDATE_RECORD, RUN_VALIDATION;
}
State entity:
@Entity
@Table(name = "state")
public class State implements Serializable {
@Id
private String id;
@Column(name = "name")
private String name;
}
Condition entity (Cannot understand how to do this)
@Entity
@Table(name = "condition")
public class Condition implements Serializable {
@Id
private String id;
@Column(name = "description")
private String description;
... CANNOT UNDERSTAND WHAT ELSE SHOULD I PUT HERE
}
State Transition entity as follows:
@Entity
@Table(name = "state_transition")
public class State implements Serializable {
@Id
private String id;
@Column(name = "from_state")
private String fromStateId;
@Column(name = "to_state")
private String toStateId;
@Column(name = "action_id")
private String actionId;
@Column(name = "condition_id")
private String conditionId;
}
Basically, with the combination of current state, action, and condition, I should be able to figure out the next state from the state transition table.
But I am stuck in the following things:
How should I implement the User Action? As I mentioned before, some of these actions as I specified above have been there in the application as APIs. Basically, I cannot understand how can I tie the user action entity that I would like to implement with the existing flow.
How should I implement the Condition as an entity ? Condition is basically an expression that either evaluates to True/False. I am not getting any clue as, how can I capture this as an entity.
Could anyone please give any pointer regarding this? Thanks.