Biblioteca Java - Blame information for rev 17
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
17 | mihai | 1 | package rtuml.capsuleimpl; |
2 | |||
3 | import rtuml.capsule.*; | ||
4 | import java.io.*; | ||
5 | |||
6 | |||
7 | public class Test { | ||
8 | public static void main(String[] args) throws Exception { | ||
9 | |||
10 | StateMachine m1 = new StateMachine("1"); | ||
11 | |||
12 | OnState on = new OnState(); | ||
13 | OffState off = new OffState(); | ||
14 | |||
15 | TransitionToOff toOff = new TransitionToOff(off); | ||
16 | TransitionToOn toOn = new TransitionToOn(on); | ||
17 | |||
18 | on.addTransition(toOff); | ||
19 | off.addTransition(toOn); | ||
20 | |||
21 | m1.addState(on); | ||
22 | |||
23 | |||
24 | m1.addState(off); | ||
25 | |||
26 | m1.setStartState(off); | ||
27 | |||
28 | ////////////////////////////////// | ||
29 | |||
30 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
31 | System.out.print("Enter command (on/off) and press Enter:"); | ||
32 | while(true){ | ||
33 | String s = br.readLine(); | ||
34 | if(s.equalsIgnoreCase("on")){ | ||
35 | m1.dispatchEvent(new SwitchTurnedOn()); | ||
36 | } | ||
37 | else if(s.equalsIgnoreCase("off")){ | ||
38 | m1.dispatchEvent(new SwitchTurnedOff()); | ||
39 | } | ||
40 | |||
41 | } | ||
42 | |||
43 | } | ||
44 | } | ||
45 | |||
46 | class SwitchTurnedOn extends Event{} | ||
47 | |||
48 | class SwitchTurnedOff extends Event{} | ||
49 | |||
50 | class OnState extends MState{ | ||
51 | |||
52 | @Override | ||
53 | public void exitAction(Event e) { | ||
54 | |||
55 | } | ||
56 | |||
57 | @Override | ||
58 | public void entryAction(Event e) { | ||
59 | System.out.println("Swith light ON"); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public void doAction(Event e) { | ||
64 | System.out.println("Light stays ON!"); | ||
65 | |||
66 | } | ||
67 | } | ||
68 | |||
69 | class OffState extends MState{ | ||
70 | |||
71 | @Override | ||
72 | public void exitAction(Event e) { | ||
73 | |||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public void entryAction(Event e) { | ||
78 | System.out.println("Swith light OFF"); | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public void doAction(Event e) { | ||
83 | System.out.println("Light stays OFF!"); | ||
84 | |||
85 | } | ||
86 | } | ||
87 | |||
88 | class TransitionToOff extends Transition{ | ||
89 | |||
90 | public TransitionToOff(MState nextState) { | ||
91 | super(nextState); | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public boolean gaurdCondition(Event e) { | ||
96 | return e instanceof SwitchTurnedOff; | ||
97 | } | ||
98 | |||
99 | } | ||
100 | |||
101 | class TransitionToOn extends Transition{ | ||
102 | |||
103 | public TransitionToOn(MState nextState) { | ||
104 | super(nextState); | ||
105 | } | ||
106 | |||
107 | @Override | ||
108 | public boolean gaurdCondition(Event e) { | ||
109 | return e instanceof SwitchTurnedOn; | ||
110 | } | ||
111 | |||
112 | } | ||
113 | |||
114 | |||
115 |