Biblioteca Java - Blame information for rev 17
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
17 | mihai | 1 | package rtuml.capsule; |
2 | import java.util.*; | ||
3 | |||
4 | /** | ||
5 | * A thread whcih execute a state machine. | ||
6 | * @author evo2 | ||
7 | * | ||
8 | */ | ||
9 | public class StateMachine extends Thread{ | ||
10 | private String id; | ||
11 | private boolean active = true; | ||
12 | private ArrayList<MState> states = new ArrayList<MState>(); | ||
13 | private MState currentState = null; | ||
14 | |||
15 | public StateMachine(String id) { | ||
16 | super(); | ||
17 | this.id = id; | ||
18 | } | ||
19 | |||
20 | public void addState(MState state){ | ||
21 | states.add(state); | ||
22 | } | ||
23 | |||
24 | public void setStartState(MState state){ | ||
25 | if(states.contains(state)){ | ||
26 | currentState = state; | ||
27 | } | ||
28 | else | ||
29 | throw new StateMachineException("Start state is not a state of "+this+" StateMachine."); | ||
30 | } | ||
31 | |||
32 | public void dispatchEvent(Event event){ | ||
33 | //while(active){ | ||
34 | if(currentState==null) | ||
35 | throw new StateMachineException("Current state not set for "+this+" StateMachine."); | ||
36 | |||
37 | //check if we can advance to next state | ||
38 | transition(event); | ||
39 | |||
40 | //evaluate event and do action | ||
41 | //execute state action | ||
42 | currentState.doAction(event); | ||
43 | |||
44 | //}//.while | ||
45 | |||
46 | } | ||
47 | |||
48 | private void transition(Event event){ | ||
49 | Transition t = currentState.checkExitConditions(event); | ||
50 | if(t!=null){ | ||
51 | //we can advance to the next state | ||
52 | currentState.exitAction(event); | ||
53 | |||
54 | //next state becomes current state | ||
55 | currentState = t.getNextState(); | ||
56 | currentState.entryAction(event); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | public String toString(){ | ||
61 | return "[StateMachine Id="+id+"]"; | ||
62 | } | ||
63 | |||
64 | } |