[919] | 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 |
|
---|
[4] | 19 | package org.expeditee.agents;
|
---|
| 20 |
|
---|
[1102] | 21 | import org.expeditee.gui.DisplayController;
|
---|
[4] | 22 | import org.expeditee.gui.Frame;
|
---|
[121] | 23 | import org.expeditee.gui.MessageBay;
|
---|
[45] | 24 | import org.expeditee.gui.TimeKeeper;
|
---|
[133] | 25 | import org.expeditee.items.Item;
|
---|
[4] | 26 | import 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 | *
|
---|
[45] | 34 | * @author johnathon, mike
|
---|
[4] | 35 | *
|
---|
| 36 | */
|
---|
| 37 | public abstract class DefaultAgent implements Agent {
|
---|
[390] | 38 | public static int AGENTS_RUNNING = 0;
|
---|
| 39 |
|
---|
[80] | 40 | public static final String CLIPBOARD = "Clipboard";
|
---|
[133] | 41 |
|
---|
[70] | 42 | protected long _timeRemaining = 0;
|
---|
| 43 |
|
---|
| 44 | // The shortest delay between frames
|
---|
[86] | 45 | public static final long TIMER_RESOLUTION = 10;
|
---|
[70] | 46 |
|
---|
[133] | 47 | protected Item _clicked = null;
|
---|
| 48 |
|
---|
[4] | 49 | protected Frame _start = null;
|
---|
| 50 |
|
---|
| 51 | protected Frame _end = null;
|
---|
| 52 |
|
---|
| 53 | protected boolean _running = true;
|
---|
| 54 |
|
---|
| 55 | protected boolean _stop = false;
|
---|
| 56 |
|
---|
[45] | 57 | protected int _frameCount = 0;
|
---|
[70] | 58 |
|
---|
[45] | 59 | protected int _itemCount = 0;
|
---|
[70] | 60 |
|
---|
[45] | 61 | protected TimeKeeper _timer;
|
---|
| 62 |
|
---|
[70] | 63 | // delay between frames, in ms
|
---|
| 64 | protected long _delay = 0;
|
---|
| 65 |
|
---|
| 66 | public DefaultAgent(String delay) {
|
---|
[390] | 67 | this();
|
---|
[70] | 68 | try {
|
---|
[72] | 69 | _delay = (int) (Double.parseDouble(delay) * 1000);
|
---|
[70] | 70 | } catch (Exception e) {
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public DefaultAgent() {
|
---|
| 75 | super();
|
---|
[390] | 76 | AGENTS_RUNNING++;
|
---|
[70] | 77 | }
|
---|
[390] | 78 |
|
---|
| 79 | public static boolean isAgentRunning() {
|
---|
| 80 | return AGENTS_RUNNING > 0;
|
---|
| 81 | }
|
---|
[70] | 82 |
|
---|
[4] | 83 | /**
|
---|
[45] | 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) {
|
---|
[70] | 91 | if (_frameCount == 0)
|
---|
[45] | 92 | _frameCount++;
|
---|
| 93 |
|
---|
[70] | 94 | int framesPerSecond = (int) Math.round(_frameCount
|
---|
| 95 | / (_timer.getElapsedMillis() / 1000.0));
|
---|
| 96 | String stats = (_itemCount > 0 ? ("Items: " + _itemCount + ", ") : "")
|
---|
[72] | 97 | + (_frameCount > 1 ? ("Frames: " + _frameCount + ", ") : "")
|
---|
| 98 | + "Time: " + _timer.getElapsedStringSeconds()
|
---|
[70] | 99 | + (framesPerSecond > 1 ? (", FPS: " + framesPerSecond) : "");
|
---|
[45] | 100 | String msg = this.getClass().getSimpleName() + " stats- ";
|
---|
| 101 |
|
---|
| 102 | message(msg + stats);
|
---|
| 103 | }
|
---|
[70] | 104 |
|
---|
[45] | 105 | /**
|
---|
[4] | 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 | */
|
---|
[133] | 111 | public boolean initialise(Frame init, Item launcher) {
|
---|
| 112 | _start = init;
|
---|
| 113 | _clicked = launcher;
|
---|
[45] | 114 | message("Starting " + this.getClass().getSimpleName() + "...");
|
---|
| 115 | _timer = new TimeKeeper();
|
---|
[4] | 116 | return true;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public void run() {
|
---|
| 120 | SessionStats.setEnabled(false);
|
---|
[133] | 121 | if (_start != null)
|
---|
| 122 | _start.change();
|
---|
[4] | 123 | _end = process(_start);
|
---|
| 124 |
|
---|
| 125 | finalise(_start);
|
---|
| 126 |
|
---|
| 127 | _running = false;
|
---|
[390] | 128 | AGENTS_RUNNING--;
|
---|
[4] | 129 |
|
---|
[1102] | 130 | DisplayController.requestRefresh(true);
|
---|
[4] | 131 | SessionStats.setEnabled(true);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public boolean hasResultFrame() {
|
---|
[313] | 135 | return getResultFrame() != null;
|
---|
[4] | 136 | }
|
---|
[179] | 137 |
|
---|
| 138 | public boolean hasResultString() {
|
---|
| 139 | return false;
|
---|
| 140 | }
|
---|
[4] | 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) {
|
---|
[121] | 165 | MessageBay.displayMessageAlways(message);
|
---|
[4] | 166 | }
|
---|
| 167 |
|
---|
| 168 | protected void overwriteMessage(String message) {
|
---|
[121] | 169 | MessageBay.overwriteMessage(message);
|
---|
[4] | 170 | }
|
---|
| 171 |
|
---|
| 172 | public boolean isRunning() {
|
---|
| 173 | return _running;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | public void stop() {
|
---|
[70] | 177 | _timeRemaining = 0;
|
---|
[4] | 178 | _stop = true;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | public void interrupt() {
|
---|
[70] | 182 | if (_timeRemaining > 0)
|
---|
| 183 | _timeRemaining = 0;
|
---|
| 184 | else
|
---|
| 185 | stop();
|
---|
[4] | 186 | }
|
---|
[70] | 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 | }
|
---|
[4] | 209 | }
|
---|