My ultimate goal is to have StateTransitionTablewhere the client can create an entry consisting of mainState, and claims that it can go from this ground state (stored in ArrayList). This application Scriptruns in a loop, and the transition table should be somewhat self-acting.
Please do not recommend me to use listings for this; they do not scale well
Each state must have access to Scriptand instances TransitionTable. The script is to do its duty, and the table should go to the next state using the index. Indexes depend on the order in which transitions are added to the record. The first record created is the first record used.
In the application loop, the state stored in the current record should be available and should be called from it process. Scriptcalls <T extends Script> process(T)by passing the current instance of the script:
public void loop() {
table.process(this);
}
mainState currentEntry process(T, TransitionTable), Script . script Script . , , 1 , .
public interface State<T extends Script> {
void process(T script, TransitionTable table);
}
TransitionTable:
public class TransitionTable {
private Map<State<?>, StateNode> entries = new HashMap<>();
private StateNode currentNode, startNode;
public <T extends Script> void process(T script) {
currentNode.mainState.process(script, this);
}
public StateNode createEntry(State<?> state) {
StateNode node = new StateNode(state);
map.put(state, node);
if(startNode == null)
startNode = currentNode = node;
return node;
}
public void transitionTo(int index) {
State<?> nextState = currentNode.states.get(index);
if(nextState == null)
nextNode = startNode.mainState;
currentNode = entries.get(nextNode);
}
public static final class StateNode {
private ArrayList<State<?>> states = new ArrayList<>;
private State<?> mainState;
public StateNode(State<?> state) {
mainState = state;
}
public StateNode addTransition(State<?> state) {
states.add(state);
return this;
}
}
}
process , script. , . .
, :
( # 4-of?, StateTransitionTable) (T, StateTransitionTable)
: , , State. , .
, . - , . , - , ,