source: trunk/src/org/expeditee/agents/DefaultAgent.java@ 108

Last change on this file since 108 was 108, checked in by ra33, 16 years ago

Heaps of changes!!!!
Added circles...
Better drawing of lines etc etc

File size: 4.1 KB
Line 
1package org.expeditee.agents;
2
3import org.expeditee.gui.Frame;
4import org.expeditee.gui.FrameGraphics;
5import org.expeditee.gui.TimeKeeper;
6import org.expeditee.stats.SessionStats;
7
8/**
9 * The framework for loading of agents accounts for two possible constructors.
10 * The first takes no parametres and is called when the agent is run without
11 * parametres. The second takes a single string parametre which is the
12 * constructor called when an agent is run with a parametre.
13 *
14 * @author johnathon, mike
15 *
16 */
17public abstract class DefaultAgent implements Agent {
18 public static final String CLIPBOARD = "Clipboard";
19
20 protected long _timeRemaining = 0;
21
22 // The shortest delay between frames
23 public static final long TIMER_RESOLUTION = 10;
24
25 protected Frame _start = null;
26
27 protected Frame _end = null;
28
29 protected boolean _running = true;
30
31 protected boolean _stop = false;
32
33 protected int _frameCount = 0;
34
35 protected int _itemCount = 0;
36
37 protected TimeKeeper _timer;
38
39 // delay between frames, in ms
40 protected long _delay = 0;
41
42 public DefaultAgent(String delay) {
43 super();
44 try {
45 _delay = (int) (Double.parseDouble(delay) * 1000);
46 } catch (Exception e) {
47 }
48 }
49
50 public DefaultAgent() {
51 super();
52 }
53
54 /**
55 * Performs any post-processing actions, such as displaying a completion
56 * message to the user
57 *
58 * @param frame
59 * The starting Frame this Agent was executed on
60 */
61 protected void finalise(Frame start) {
62 if (_frameCount == 0)
63 _frameCount++;
64
65 int framesPerSecond = (int) Math.round(_frameCount
66 / (_timer.getElapsedMillis() / 1000.0));
67 String stats = (_itemCount > 0 ? ("Items: " + _itemCount + ", ") : "")
68 + (_frameCount > 1 ? ("Frames: " + _frameCount + ", ") : "")
69 + "Time: " + _timer.getElapsedStringSeconds()
70 + (framesPerSecond > 1 ? (", FPS: " + framesPerSecond) : "");
71 String msg = this.getClass().getSimpleName() + " stats- ";
72
73 message(msg + stats);
74 }
75
76 /**
77 * Performs any pre-processing of the starting frame, which may include
78 * searching the Frame for tags that determine the Agent behaviour
79 *
80 * @param start
81 */
82 public boolean initialise(Frame init) {
83 message("Starting " + this.getClass().getSimpleName() + "...");
84 _timer = new TimeKeeper();
85 return true;
86 }
87
88 public void setStartFrame(Frame start) {
89 _start = start;
90 }
91
92 public void run() {
93 // init is now called by Actions.java
94 /*
95 * if(!initialise(_start)){ FrameGraphics.ErrorMessage("Error
96 * initialising agent."); _running = false; return; }
97 */
98
99 SessionStats.setEnabled(false);
100 _start.change();
101 _end = process(_start);
102
103 finalise(_start);
104
105 _running = false;
106
107 SessionStats.setEnabled(true);
108 }
109
110 public boolean hasResultFrame() {
111 return _end != null;
112 }
113
114 public Frame getResultFrame() {
115 return _end;
116 }
117
118 /**
119 * Processes the given Frame, behaviour depends on individual
120 * implementation. If this JAG displays any kind of completion Frame to the
121 * user it should be returned from this method, otherwise null can be
122 * returned.
123 *
124 * @param frame
125 * the Frame to process
126 * @return The completion Frame to show to the user, or null
127 */
128 protected abstract Frame process(Frame frame);
129
130 /**
131 * Displays a message to the user
132 *
133 * @param message
134 * The message to display to the user
135 */
136 protected void message(String message) {
137 FrameGraphics.DisplayMessageAlways(message);
138 }
139
140 protected void overwriteMessage(String message) {
141 FrameGraphics.OverwriteMessage(message);
142 }
143
144 public boolean isRunning() {
145 return _running;
146 }
147
148 public void stop() {
149 _timeRemaining = 0;
150 _stop = true;
151 }
152
153 public void interrupt() {
154 if (_timeRemaining > 0)
155 _timeRemaining = 0;
156 else
157 stop();
158 }
159
160 /**
161 * Pauses the execution of this Agent for the given time period (in ms.)
162 * This is used for the delay between displaying frames.
163 *
164 * @param time
165 */
166 protected void pause(long time) {
167 try {
168 if (time < 0)
169 _timeRemaining = Long.MAX_VALUE;
170 else
171 _timeRemaining = time;
172
173 while (_timeRemaining > 0) {
174 Thread.yield();
175 Thread.sleep(TIMER_RESOLUTION);
176 _timeRemaining -= TIMER_RESOLUTION;
177 }
178 } catch (InterruptedException e) {
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.