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

Last change on this file since 919 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 5.1 KB
Line 
1/**
2 * DefaultAgent.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.agents;
20
21import org.expeditee.gui.Frame;
22import org.expeditee.gui.FrameGraphics;
23import org.expeditee.gui.MessageBay;
24import org.expeditee.gui.TimeKeeper;
25import org.expeditee.items.Item;
26import org.expeditee.stats.SessionStats;
27
28/**
29 * The framework for loading of agents accounts for two possible constructors.
30 * The first takes no parametres and is called when the agent is run without
31 * parametres. The second takes a single string parametre which is the
32 * constructor called when an agent is run with a parametre.
33 *
34 * @author johnathon, mike
35 *
36 */
37public abstract class DefaultAgent implements Agent {
38 public static int AGENTS_RUNNING = 0;
39
40 public static final String CLIPBOARD = "Clipboard";
41
42 protected long _timeRemaining = 0;
43
44 // The shortest delay between frames
45 public static final long TIMER_RESOLUTION = 10;
46
47 protected Item _clicked = null;
48
49 protected Frame _start = null;
50
51 protected Frame _end = null;
52
53 protected boolean _running = true;
54
55 protected boolean _stop = false;
56
57 protected int _frameCount = 0;
58
59 protected int _itemCount = 0;
60
61 protected TimeKeeper _timer;
62
63 // delay between frames, in ms
64 protected long _delay = 0;
65
66 public DefaultAgent(String delay) {
67 this();
68 try {
69 _delay = (int) (Double.parseDouble(delay) * 1000);
70 } catch (Exception e) {
71 }
72 }
73
74 public DefaultAgent() {
75 super();
76 AGENTS_RUNNING++;
77 }
78
79 public static boolean isAgentRunning() {
80 return AGENTS_RUNNING > 0;
81 }
82
83 /**
84 * Performs any post-processing actions, such as displaying a completion
85 * message to the user
86 *
87 * @param frame
88 * The starting Frame this Agent was executed on
89 */
90 protected void finalise(Frame start) {
91 if (_frameCount == 0)
92 _frameCount++;
93
94 int framesPerSecond = (int) Math.round(_frameCount
95 / (_timer.getElapsedMillis() / 1000.0));
96 String stats = (_itemCount > 0 ? ("Items: " + _itemCount + ", ") : "")
97 + (_frameCount > 1 ? ("Frames: " + _frameCount + ", ") : "")
98 + "Time: " + _timer.getElapsedStringSeconds()
99 + (framesPerSecond > 1 ? (", FPS: " + framesPerSecond) : "");
100 String msg = this.getClass().getSimpleName() + " stats- ";
101
102 message(msg + stats);
103 }
104
105 /**
106 * Performs any pre-processing of the starting frame, which may include
107 * searching the Frame for tags that determine the Agent behaviour
108 *
109 * @param start
110 */
111 public boolean initialise(Frame init, Item launcher) {
112 _start = init;
113 _clicked = launcher;
114 message("Starting " + this.getClass().getSimpleName() + "...");
115 _timer = new TimeKeeper();
116 return true;
117 }
118
119 public void run() {
120 SessionStats.setEnabled(false);
121 if (_start != null)
122 _start.change();
123 _end = process(_start);
124
125 finalise(_start);
126
127 _running = false;
128 AGENTS_RUNNING--;
129
130 FrameGraphics.requestRefresh(true);
131 SessionStats.setEnabled(true);
132 }
133
134 public boolean hasResultFrame() {
135 return getResultFrame() != null;
136 }
137
138 public boolean hasResultString() {
139 return false;
140 }
141
142 public Frame getResultFrame() {
143 return _end;
144 }
145
146 /**
147 * Processes the given Frame, behaviour depends on individual
148 * implementation. If this JAG displays any kind of completion Frame to the
149 * user it should be returned from this method, otherwise null can be
150 * returned.
151 *
152 * @param frame
153 * the Frame to process
154 * @return The completion Frame to show to the user, or null
155 */
156 protected abstract Frame process(Frame frame);
157
158 /**
159 * Displays a message to the user
160 *
161 * @param message
162 * The message to display to the user
163 */
164 protected void message(String message) {
165 MessageBay.displayMessageAlways(message);
166 }
167
168 protected void overwriteMessage(String message) {
169 MessageBay.overwriteMessage(message);
170 }
171
172 public boolean isRunning() {
173 return _running;
174 }
175
176 public void stop() {
177 _timeRemaining = 0;
178 _stop = true;
179 }
180
181 public void interrupt() {
182 if (_timeRemaining > 0)
183 _timeRemaining = 0;
184 else
185 stop();
186 }
187
188 /**
189 * Pauses the execution of this Agent for the given time period (in ms.)
190 * This is used for the delay between displaying frames.
191 *
192 * @param time
193 */
194 protected void pause(long time) {
195 try {
196 if (time < 0)
197 _timeRemaining = Long.MAX_VALUE;
198 else
199 _timeRemaining = time;
200
201 while (_timeRemaining > 0) {
202 Thread.yield();
203 Thread.sleep(TIMER_RESOLUTION);
204 _timeRemaining -= TIMER_RESOLUTION;
205 }
206 } catch (InterruptedException e) {
207 }
208 }
209}
Note: See TracBrowser for help on using the repository browser.