1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package zeus.actors;
27
28 import java.util.*;
29 import zeus.util.*;
30 import zeus.concepts.*;
31
32 /***
33 * Tasks are generated in the form of 'stub' files, skeleton implementations
34 * that need to be realised by application specific program code - this code
35 * is linked to the agents through the methods of this interface. This approach
36 * enables new domain-specific functionality to be integrated with the
37 * automated created agent-specific code, without needing to modify the latter. < p>
38 *
39 * The class variables include several arrays of {@link Fact}
40 * objects, one of which passes information from agent to task, and another which
41 * stores the result of performing the task (which can then be read back by the
42 * agent when the task has completed running. <p>
43 *
44 * This is a crucially important class for developers who want to add their own
45 * application-specific functionality, and so should be studied carefully.
46 * Instructions and an example of how to write a task body are provided in
47 * Section 6 of Zeus Application Realisation Guide.
48 * TaskExternals have been available since Zeus 1.1 and allow customisation without
49 * editing the stubs - which is useful in terms of preserving your edited code
50 * in a separate file.
51 */
52
53
54 public abstract class ZeusTask extends Thread
55 {
56 /*** The information passed from the agent to the task, (a 2D array is used
57 to enable multiple fact instances) */
58 protected Fact[][] inputArgs = null;
59
60 /*** The information returned from the task to the agent */
61 protected Fact[][] outputArgs = null;
62
63 /*** The information the task expects to receive, (as specified when the task
64 was defined) this can be used to validate the inputArgs */
65 protected Fact[] expInputArgs = null;
66
67 /*** The information the task expects to return, (as specified when the task
68 was defined) this can be used to validate the outputArgs */
69 protected Fact[] expOutputArgs = null;
70
71
72 protected String[] media = null;
73 private boolean isFinished = false;
74 private boolean isRunning = false;
75 private boolean isAborted = false;
76
77 protected String desired_by;
78
79 protected AgentContext context = null;
80
81
82 protected ZeusTask() {
83 this.setPriority(Thread.NORM_PRIORITY);
84 this.setName("ZeusTask");
85 }
86
87 public void setMedia(String[] media) {
88 this.media = media;
89 }
90
91 /*** Used internally by the {@link ExecutionMonitor} to start the task,
92 users should not call this themselves */
93 public void run() {
94 isRunning = true;
95 exec();
96 isRunning = false;
97 isFinished = true;
98 }
99
100 public boolean isRunning() {
101 return isRunning;
102 }
103
104 public boolean isFinished() {
105 return isFinished;
106 }
107
108 public boolean isAborted() {
109 return isAborted;
110 }
111
112 /*** Call this method in cases where the task terminates abnormally */
113 public void abort() {
114 isAborted = true;
115
116 }
117
118 public void setContext(AgentContext context) {
119 this.context = context;
120 }
121
122 public void setInputArgs(Fact[][] t) {
123 Assert.notNull(t);
124 inputArgs = t;
125 }
126
127 public void setExpectedOutputArgs(Fact[] f) {
128 Assert.notNull(f);
129 expOutputArgs = f;
130 }
131
132 public void setExpectedInputArgs(Fact[] f) {
133 expInputArgs = f;
134 }
135
136
137 public void setOutputArgs (Fact[] f) {
138
139
140
141
142
143
144 Assert.notNull(f);
145 if (outputArgs == null)
146 outputArgs = new Fact[f.length][0];
147 outputArgs[0] = f;
148 }
149
150
151 public void setOutputArgs (Fact[][] f) {
152
153
154
155
156
157
158 Assert.notNull(f);
159 outputArgs = f;
160 }
161
162 public Fact[][] getInputArgs() { return inputArgs; }
163
164 public Fact[] getExpectedOutputArgs() { return expOutputArgs; }
165
166 public Fact[] getExpectedInputArgs() { return expInputArgs; }
167
168 public Fact[][] getOutputArgs() {
169 if ( !isFinished ) return null;
170 return outputArgs;
171 }
172
173
174 public void setDesiredBy(String desired_by) {
175 Assert.notNull(desired_by);
176 this.desired_by = desired_by;
177 }
178 public String getDesiredBy() { return desired_by; }
179
180
181 /*** This is the interface between the agent and the task; hence the body of
182 the task should implement this method */
183 protected abstract void exec();
184
185 /***
186 *This is the method called to get the string that describes this task in the service
187 *registration
188 *The concrete implementation is included to promote the smooth running of legacy
189 *agents
190 *@since 1.3
191 *@author Simon Thompson
192 */
193 public String getDescription () {
194 return ("no description");
195 }
196
197 /***
198 * Get the instance details from the task. Returns <code>null</code>
199 * if not implemented by the sub class.
200 */
201 public String getInstanceDetails() {
202 return null;
203 }
204
205 }