1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * RunPanel.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.experiment; |
---|
24 | |
---|
25 | import weka.core.SerializedObject; |
---|
26 | import weka.core.Utils; |
---|
27 | import weka.experiment.Experiment; |
---|
28 | import weka.experiment.RemoteExperiment; |
---|
29 | import weka.experiment.RemoteExperimentEvent; |
---|
30 | import weka.experiment.RemoteExperimentListener; |
---|
31 | import weka.gui.LogPanel; |
---|
32 | |
---|
33 | import java.awt.BorderLayout; |
---|
34 | import java.awt.GridBagConstraints; |
---|
35 | import java.awt.GridBagLayout; |
---|
36 | import java.awt.Insets; |
---|
37 | import java.awt.event.ActionEvent; |
---|
38 | import java.awt.event.ActionListener; |
---|
39 | import java.awt.event.WindowAdapter; |
---|
40 | import java.awt.event.WindowEvent; |
---|
41 | import java.io.BufferedInputStream; |
---|
42 | import java.io.File; |
---|
43 | import java.io.FileInputStream; |
---|
44 | import java.io.ObjectInputStream; |
---|
45 | import java.io.Serializable; |
---|
46 | |
---|
47 | import javax.swing.BorderFactory; |
---|
48 | import javax.swing.JButton; |
---|
49 | import javax.swing.JFrame; |
---|
50 | import javax.swing.JPanel; |
---|
51 | |
---|
52 | /** |
---|
53 | * This panel controls the running of an experiment. |
---|
54 | * |
---|
55 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
56 | * @version $Revision: 1.21 $ |
---|
57 | */ |
---|
58 | public class RunPanel |
---|
59 | extends JPanel |
---|
60 | implements ActionListener { |
---|
61 | |
---|
62 | /** for serialization */ |
---|
63 | private static final long serialVersionUID = 1691868018596872051L; |
---|
64 | |
---|
65 | /** The message displayed when no experiment is running */ |
---|
66 | protected static final String NOT_RUNNING = "Not running"; |
---|
67 | |
---|
68 | /** Click to start running the experiment */ |
---|
69 | protected JButton m_StartBut = new JButton("Start"); |
---|
70 | |
---|
71 | /** Click to signal the running experiment to halt */ |
---|
72 | protected JButton m_StopBut = new JButton("Stop"); |
---|
73 | |
---|
74 | protected LogPanel m_Log = new LogPanel(); |
---|
75 | |
---|
76 | /** The experiment to run */ |
---|
77 | protected Experiment m_Exp; |
---|
78 | |
---|
79 | /** The thread running the experiment */ |
---|
80 | protected Thread m_RunThread = null; |
---|
81 | |
---|
82 | /** A pointer to the results panel */ |
---|
83 | protected ResultsPanel m_ResultsPanel = null; |
---|
84 | |
---|
85 | /* |
---|
86 | * A class that handles running a copy of the experiment |
---|
87 | * in a separate thread |
---|
88 | */ |
---|
89 | class ExperimentRunner |
---|
90 | extends Thread |
---|
91 | implements Serializable { |
---|
92 | |
---|
93 | /** for serialization */ |
---|
94 | private static final long serialVersionUID = -5591889874714150118L; |
---|
95 | |
---|
96 | Experiment m_ExpCopy; |
---|
97 | |
---|
98 | public ExperimentRunner(final Experiment exp) throws Exception { |
---|
99 | |
---|
100 | // Create a full copy using serialization |
---|
101 | if (exp == null) { |
---|
102 | System.err.println("Null experiment!!!"); |
---|
103 | } else { |
---|
104 | System.err.println("Running experiment: " + exp.toString()); |
---|
105 | } |
---|
106 | System.err.println("Writing experiment copy"); |
---|
107 | SerializedObject so = new SerializedObject(exp); |
---|
108 | System.err.println("Reading experiment copy"); |
---|
109 | m_ExpCopy = (Experiment) so.getObject(); |
---|
110 | System.err.println("Made experiment copy"); |
---|
111 | } |
---|
112 | |
---|
113 | public void abortExperiment() { |
---|
114 | if (m_ExpCopy instanceof RemoteExperiment) { |
---|
115 | ((RemoteExperiment)m_ExpCopy).abortExperiment(); |
---|
116 | // m_StartBut.setEnabled(true); |
---|
117 | m_StopBut.setEnabled(false); |
---|
118 | // statusMessage(NOT_RUNNING); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Starts running the experiment. |
---|
124 | */ |
---|
125 | public void run() { |
---|
126 | |
---|
127 | m_StartBut.setEnabled(false); |
---|
128 | m_StopBut.setEnabled(true); |
---|
129 | if (m_ResultsPanel != null) { |
---|
130 | m_ResultsPanel.setExperiment(null); |
---|
131 | } |
---|
132 | try { |
---|
133 | if (m_ExpCopy instanceof RemoteExperiment) { |
---|
134 | // add a listener |
---|
135 | System.err.println("Adding a listener"); |
---|
136 | ((RemoteExperiment)m_ExpCopy). |
---|
137 | addRemoteExperimentListener(new RemoteExperimentListener() { |
---|
138 | public void remoteExperimentStatus(RemoteExperimentEvent e) { |
---|
139 | if (e.m_statusMessage) { |
---|
140 | statusMessage(e.m_messageString); |
---|
141 | } |
---|
142 | if (e.m_logMessage) { |
---|
143 | logMessage(e.m_messageString); |
---|
144 | } |
---|
145 | if (e.m_experimentFinished) { |
---|
146 | m_RunThread = null; |
---|
147 | m_StartBut.setEnabled(true); |
---|
148 | m_StopBut.setEnabled(false); |
---|
149 | statusMessage(NOT_RUNNING); |
---|
150 | } |
---|
151 | } |
---|
152 | }); |
---|
153 | } |
---|
154 | logMessage("Started"); |
---|
155 | statusMessage("Initializing..."); |
---|
156 | m_ExpCopy.initialize(); |
---|
157 | int errors = 0; |
---|
158 | if (!(m_ExpCopy instanceof RemoteExperiment)) { |
---|
159 | statusMessage("Iterating..."); |
---|
160 | while (m_RunThread != null && m_ExpCopy.hasMoreIterations()) { |
---|
161 | try { |
---|
162 | String current = "Iteration:"; |
---|
163 | if (m_ExpCopy.getUsePropertyIterator()) { |
---|
164 | int cnum = m_ExpCopy.getCurrentPropertyNumber(); |
---|
165 | String ctype = m_ExpCopy.getPropertyArray().getClass().getComponentType().getName(); |
---|
166 | int lastDot = ctype.lastIndexOf('.'); |
---|
167 | if (lastDot != -1) { |
---|
168 | ctype = ctype.substring(lastDot + 1); |
---|
169 | } |
---|
170 | String cname = " " + ctype + "=" |
---|
171 | + (cnum + 1) + ":" |
---|
172 | + m_ExpCopy.getPropertyArrayValue(cnum).getClass().getName(); |
---|
173 | current += cname; |
---|
174 | } |
---|
175 | String dname = ((File) m_ExpCopy.getDatasets() |
---|
176 | .elementAt(m_ExpCopy.getCurrentDatasetNumber())) |
---|
177 | .getName(); |
---|
178 | current += " Dataset=" + dname |
---|
179 | + " Run=" + (m_ExpCopy.getCurrentRunNumber()); |
---|
180 | statusMessage(current); |
---|
181 | m_ExpCopy.nextIteration(); |
---|
182 | } catch (Exception ex) { |
---|
183 | errors ++; |
---|
184 | logMessage(ex.getMessage()); |
---|
185 | ex.printStackTrace(); |
---|
186 | boolean continueAfterError = false; |
---|
187 | if (continueAfterError) { |
---|
188 | m_ExpCopy.advanceCounters(); // Try to keep plowing through |
---|
189 | } else { |
---|
190 | m_RunThread = null; |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | statusMessage("Postprocessing..."); |
---|
195 | m_ExpCopy.postProcess(); |
---|
196 | if (m_RunThread == null) { |
---|
197 | logMessage("Interrupted"); |
---|
198 | } else { |
---|
199 | logMessage("Finished"); |
---|
200 | } |
---|
201 | if (errors == 1) { |
---|
202 | logMessage("There was " + errors + " error"); |
---|
203 | } else { |
---|
204 | logMessage("There were " + errors + " errors"); |
---|
205 | } |
---|
206 | statusMessage(NOT_RUNNING); |
---|
207 | } else { |
---|
208 | statusMessage("Remote experiment running..."); |
---|
209 | ((RemoteExperiment)m_ExpCopy).runExperiment(); |
---|
210 | } |
---|
211 | } catch (Exception ex) { |
---|
212 | ex.printStackTrace(); |
---|
213 | System.err.println(ex.getMessage()); |
---|
214 | statusMessage(ex.getMessage()); |
---|
215 | } finally { |
---|
216 | if (m_ResultsPanel != null) { |
---|
217 | m_ResultsPanel.setExperiment(m_ExpCopy); |
---|
218 | } |
---|
219 | if (!(m_ExpCopy instanceof RemoteExperiment)) { |
---|
220 | m_RunThread = null; |
---|
221 | m_StartBut.setEnabled(true); |
---|
222 | m_StopBut.setEnabled(false); |
---|
223 | System.err.println("Done..."); |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Sets the pointer to the results panel. |
---|
231 | */ |
---|
232 | public void setResultsPanel(ResultsPanel rp) { |
---|
233 | |
---|
234 | m_ResultsPanel = rp; |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Creates the run panel with no initial experiment. |
---|
239 | */ |
---|
240 | public RunPanel() { |
---|
241 | |
---|
242 | m_StartBut.addActionListener(this); |
---|
243 | m_StopBut.addActionListener(this); |
---|
244 | m_StartBut.setEnabled(false); |
---|
245 | m_StopBut.setEnabled(false); |
---|
246 | m_StartBut.setMnemonic('S'); |
---|
247 | m_StopBut.setMnemonic('t'); |
---|
248 | m_Log.statusMessage(NOT_RUNNING); |
---|
249 | |
---|
250 | // Set the GUI layout |
---|
251 | JPanel controls = new JPanel(); |
---|
252 | GridBagLayout gb = new GridBagLayout(); |
---|
253 | GridBagConstraints constraints = new GridBagConstraints(); |
---|
254 | controls.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); |
---|
255 | // controls.setLayout(new GridLayout(1,2)); |
---|
256 | controls.setLayout(gb); |
---|
257 | constraints.gridx=0;constraints.gridy=0;constraints.weightx=5; |
---|
258 | constraints.fill = GridBagConstraints.HORIZONTAL; |
---|
259 | constraints.gridwidth=1;constraints.gridheight=1; |
---|
260 | constraints.insets = new Insets(0,2,0,2); |
---|
261 | controls.add(m_StartBut,constraints); |
---|
262 | constraints.gridx=1;constraints.gridy=0;constraints.weightx=5; |
---|
263 | constraints.gridwidth=1;constraints.gridheight=1; |
---|
264 | controls.add(m_StopBut,constraints); |
---|
265 | setLayout(new BorderLayout()); |
---|
266 | add(controls, BorderLayout.NORTH); |
---|
267 | add(m_Log, BorderLayout.CENTER); |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Creates the panel with the supplied initial experiment. |
---|
272 | * |
---|
273 | * @param exp a value of type 'Experiment' |
---|
274 | */ |
---|
275 | public RunPanel(Experiment exp) { |
---|
276 | |
---|
277 | this(); |
---|
278 | setExperiment(exp); |
---|
279 | } |
---|
280 | |
---|
281 | /** |
---|
282 | * Sets the experiment the panel operates on. |
---|
283 | * |
---|
284 | * @param exp a value of type 'Experiment' |
---|
285 | */ |
---|
286 | public void setExperiment(Experiment exp) { |
---|
287 | |
---|
288 | m_Exp = exp; |
---|
289 | m_StartBut.setEnabled(m_RunThread == null); |
---|
290 | m_StopBut.setEnabled(m_RunThread != null); |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * Controls starting and stopping the experiment. |
---|
295 | * |
---|
296 | * @param e a value of type 'ActionEvent' |
---|
297 | */ |
---|
298 | public void actionPerformed(ActionEvent e) { |
---|
299 | |
---|
300 | if (e.getSource() == m_StartBut) { |
---|
301 | if (m_RunThread == null) { |
---|
302 | try { |
---|
303 | m_RunThread = new ExperimentRunner(m_Exp); |
---|
304 | m_RunThread.setPriority(Thread.MIN_PRIORITY); // UI has most priority |
---|
305 | m_RunThread.start(); |
---|
306 | } catch (Exception ex) { |
---|
307 | ex.printStackTrace(); |
---|
308 | logMessage("Problem creating experiment copy to run: " |
---|
309 | + ex.getMessage()); |
---|
310 | } |
---|
311 | } |
---|
312 | } else if (e.getSource() == m_StopBut) { |
---|
313 | m_StopBut.setEnabled(false); |
---|
314 | logMessage("User aborting experiment. "); |
---|
315 | if (m_Exp instanceof RemoteExperiment) { |
---|
316 | logMessage("Waiting for remote tasks to " |
---|
317 | +"complete..."); |
---|
318 | } |
---|
319 | ((ExperimentRunner)m_RunThread).abortExperiment(); |
---|
320 | // m_RunThread.stop() ?? |
---|
321 | m_RunThread = null; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * Sends the supplied message to the log panel log area. |
---|
327 | * |
---|
328 | * @param message the message to log |
---|
329 | */ |
---|
330 | protected void logMessage(String message) { |
---|
331 | |
---|
332 | m_Log.logMessage(message); |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * Sends the supplied message to the log panel status line. |
---|
337 | * |
---|
338 | * @param message the status message |
---|
339 | */ |
---|
340 | protected void statusMessage(String message) { |
---|
341 | |
---|
342 | m_Log.statusMessage(message); |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * Tests out the run panel from the command line. |
---|
347 | * |
---|
348 | * @param args may contain options specifying an experiment to run. |
---|
349 | */ |
---|
350 | public static void main(String [] args) { |
---|
351 | |
---|
352 | try { |
---|
353 | boolean readExp = Utils.getFlag('l', args); |
---|
354 | final String expFile = Utils.getOption('f', args); |
---|
355 | if (readExp && (expFile.length() == 0)) { |
---|
356 | throw new Exception("A filename must be given with the -f option"); |
---|
357 | } |
---|
358 | Experiment exp = null; |
---|
359 | if (readExp) { |
---|
360 | FileInputStream fi = new FileInputStream(expFile); |
---|
361 | ObjectInputStream oi = new ObjectInputStream( |
---|
362 | new BufferedInputStream(fi)); |
---|
363 | Object to = oi.readObject(); |
---|
364 | if (to instanceof RemoteExperiment) { |
---|
365 | exp = (RemoteExperiment)to; |
---|
366 | } else { |
---|
367 | exp = (Experiment)to; |
---|
368 | } |
---|
369 | oi.close(); |
---|
370 | } else { |
---|
371 | exp = new Experiment(); |
---|
372 | } |
---|
373 | System.err.println("Initial Experiment:\n" + exp.toString()); |
---|
374 | final JFrame jf = new JFrame("Run Weka Experiment"); |
---|
375 | jf.getContentPane().setLayout(new BorderLayout()); |
---|
376 | final RunPanel sp = new RunPanel(exp); |
---|
377 | //sp.setBorder(BorderFactory.createTitledBorder("Setup")); |
---|
378 | jf.getContentPane().add(sp, BorderLayout.CENTER); |
---|
379 | jf.addWindowListener(new WindowAdapter() { |
---|
380 | public void windowClosing(WindowEvent e) { |
---|
381 | System.err.println("\nExperiment Configuration\n" |
---|
382 | + sp.m_Exp.toString()); |
---|
383 | jf.dispose(); |
---|
384 | System.exit(0); |
---|
385 | } |
---|
386 | }); |
---|
387 | jf.pack(); |
---|
388 | jf.setVisible(true); |
---|
389 | } catch (Exception ex) { |
---|
390 | ex.printStackTrace(); |
---|
391 | System.err.println(ex.getMessage()); |
---|
392 | } |
---|
393 | } |
---|
394 | } |
---|