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