Biblioteca Java - Rev 17
Subversion Repositories:
package rtuml.capsule;
import java.util.*;
/**
* A thread whcih execute a state machine.
* @author evo2
*
*/
public class StateMachine extends Thread{
private String id;
private boolean active = true;
private ArrayList<MState> states = new ArrayList<MState>();
private MState currentState = null;
public StateMachine(String id) {
super();
this.id = id;
}
public void addState(MState state){
states.add(state);
}
public void setStartState(MState state){
if(states.contains(state)){
currentState = state;
}
else
throw new StateMachineException("Start state is not a state of "+this+" StateMachine.");
}
public void dispatchEvent(Event event){
//while(active){
if(currentState==null)
throw new StateMachineException("Current state not set for "+this+" StateMachine.");
//check if we can advance to next state
transition(event);
//evaluate event and do action
//execute state action
currentState.doAction(event);
//}//.while
}
private void transition(Event event){
Transition t = currentState.checkExitConditions(event);
if(t!=null){
//we can advance to the next state
currentState.exitAction(event);
//next state becomes current state
currentState = t.getNextState();
currentState.entryAction(event);
}
}
public String toString(){
return "[StateMachine Id="+id+"]";
}
}
import java.util.*;
/**
* A thread whcih execute a state machine.
* @author evo2
*
*/
public class StateMachine extends Thread{
private String id;
private boolean active = true;
private ArrayList<MState> states = new ArrayList<MState>();
private MState currentState = null;
public StateMachine(String id) {
super();
this.id = id;
}
public void addState(MState state){
states.add(state);
}
public void setStartState(MState state){
if(states.contains(state)){
currentState = state;
}
else
throw new StateMachineException("Start state is not a state of "+this+" StateMachine.");
}
public void dispatchEvent(Event event){
//while(active){
if(currentState==null)
throw new StateMachineException("Current state not set for "+this+" StateMachine.");
//check if we can advance to next state
transition(event);
//evaluate event and do action
//execute state action
currentState.doAction(event);
//}//.while
}
private void transition(Event event){
Transition t = currentState.checkExitConditions(event);
if(t!=null){
//we can advance to the next state
currentState.exitAction(event);
//next state becomes current state
currentState = t.getNextState();
currentState.entryAction(event);
}
}
public String toString(){
return "[StateMachine Id="+id+"]";
}
}