1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import org.expeditee.gui.DisplayIO;
|
---|
4 | import org.expeditee.gui.Frame;
|
---|
5 | import org.expeditee.gui.FrameGraphics;
|
---|
6 | import org.expeditee.gui.TimeKeeper;
|
---|
7 | import org.expeditee.stats.SessionStats;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * The framework for loading of agents accounts for two possible constructors.
|
---|
11 | * The first takes no parametres and is called when the agent is run without
|
---|
12 | * parametres. The second takes a single string parametre which is the
|
---|
13 | * constructor called when an agent is run with a parametre.
|
---|
14 | *
|
---|
15 | * @author johnathon, mike
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public abstract class DefaultAgent implements Agent {
|
---|
19 |
|
---|
20 | protected Frame _start = null;
|
---|
21 |
|
---|
22 | protected Frame _end = null;
|
---|
23 |
|
---|
24 | protected boolean _running = true;
|
---|
25 |
|
---|
26 | protected boolean _stop = false;
|
---|
27 |
|
---|
28 | protected int _frameCount = 0;
|
---|
29 | protected int _itemCount = 0;
|
---|
30 |
|
---|
31 | protected TimeKeeper _timer;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Performs any post-processing actions, such as displaying a completion
|
---|
35 | * message to the user
|
---|
36 | *
|
---|
37 | * @param frame
|
---|
38 | * The starting Frame this Agent was executed on
|
---|
39 | */
|
---|
40 | protected void finalise(Frame start) {
|
---|
41 | if (_frameCount == 0)
|
---|
42 | _frameCount++;
|
---|
43 |
|
---|
44 | int framesPerSecond = (int) (_frameCount / (_timer.getElapsedMillis() / 1000.0));
|
---|
45 | String stats = (_itemCount > 0 ? ("Items: " + _itemCount + ", ") : "") +
|
---|
46 | "Frames: " + _frameCount + ", Time: "
|
---|
47 | + _timer.getElapsedStringSeconds() + ", FPS: " + framesPerSecond;
|
---|
48 | String msg = this.getClass().getSimpleName() + " stats- ";
|
---|
49 |
|
---|
50 | message(msg + stats);
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Performs any pre-processing of the starting frame, which may include
|
---|
55 | * searching the Frame for tags that determine the Agent behaviour
|
---|
56 | *
|
---|
57 | * @param start
|
---|
58 | */
|
---|
59 | public boolean initialise(Frame init) {
|
---|
60 | message("Starting " + this.getClass().getSimpleName() + "...");
|
---|
61 | _timer = new TimeKeeper();
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void setStartFrame(Frame start) {
|
---|
66 | _start = start;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public void run() {
|
---|
70 | // init is now called by Actions.java
|
---|
71 | /*
|
---|
72 | * if(!initialise(_start)){ FrameGraphics.ErrorMessage("Error
|
---|
73 | * initialising agent."); _running = false; return; }
|
---|
74 | */
|
---|
75 |
|
---|
76 | SessionStats.setEnabled(false);
|
---|
77 |
|
---|
78 | _end = process(_start);
|
---|
79 |
|
---|
80 | finalise(_start);
|
---|
81 |
|
---|
82 | _running = false;
|
---|
83 |
|
---|
84 | SessionStats.setEnabled(true);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public boolean hasResultFrame() {
|
---|
88 | return _end != null;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Frame getResultFrame() {
|
---|
92 | return _end;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Processes the given Frame, behaviour depends on individual
|
---|
97 | * implementation. If this JAG displays any kind of completion Frame to the
|
---|
98 | * user it should be returned from this method, otherwise null can be
|
---|
99 | * returned.
|
---|
100 | *
|
---|
101 | * @param frame
|
---|
102 | * the Frame to process
|
---|
103 | * @return The completion Frame to show to the user, or null
|
---|
104 | */
|
---|
105 | protected abstract Frame process(Frame frame);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Displays a message to the user
|
---|
109 | *
|
---|
110 | * @param message
|
---|
111 | * The message to display to the user
|
---|
112 | */
|
---|
113 | protected void message(String message) {
|
---|
114 | FrameGraphics.DisplayMessageAlways(message);
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void overwriteMessage(String message) {
|
---|
118 | FrameGraphics.OverwriteMessage(message);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public boolean isRunning() {
|
---|
122 | return _running;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void stop() {
|
---|
126 | _stop = true;
|
---|
127 | }
|
---|
128 |
|
---|
129 | public void interrupt() {
|
---|
130 | stop();
|
---|
131 | }
|
---|
132 | }
|
---|