001 /*
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: 2002-8-22
005 * Time: 20:04:38
006 */
007 package EVolve.util;
008
009 import java.awt.*;
010 import java.awt.event.*;
011 import java.util.*;
012 import javax.swing.*;
013 import javax.swing.filechooser.*;
014 import java.io.*;
015 import EVolve.*;
016 import EVolve.visualization.VisualizationFactory;
017 import EVolve.visualization.Visualization;
018 import EVolve.visualization.PredictorFactory;
019 import EVolve.visualization.XYViz.XYVisualization;
020
021 public class BatchRunner {
022 private JDialog dialog;
023 private java.awt.List fileList;
024 private java.awt.List procList;
025 private Frame owner;
026 private HashSet setFile;
027 private HashSet setProcessFiles;
028 private HashMap batches;
029 private HashMap mapConfig;
030 private JTextField txtConfName;
031
032 public BatchRunner() {
033 owner = Scene.getFrame();
034 setFile = new HashSet();
035 setProcessFiles = new HashSet();
036 batches = new HashMap();
037 mapConfig = new HashMap();
038 }
039
040 public void showWindow() {
041 if (dialog == null) {
042 createDialog();
043 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
044 }
045 else dialog.setVisible(true);
046 }
047
048 public void runBatch() {
049 int iBatchNum = batches.keySet().size();
050 Iterator it = batches.keySet().iterator();
051 BatchInfo batchInfo;
052 RandomAccessFile inConfigFile;
053 String dimensionDefs[] = new String[3];
054
055 if (iBatchNum <= 0) {
056 Scene.showErrorMessage("No batch available!");
057 return;
058 }
059
060 while (it.hasNext()) {
061 String outPath,dataFileName,batchName = (String) it.next();
062 VizInfo vizInfo = null;
063 Iterator it2;
064 batchInfo = (BatchInfo) batches.get(batchName);
065 // read in configure file
066 try {
067 inConfigFile = new RandomAccessFile(batchInfo.getConfigureFile(), "r");
068 String version = inConfigFile.readLine().trim();
069 if (!version.equals(Scene.VERSION)) throw (new IOException());
070 inConfigFile.readLine();
071
072 it2 = batchInfo.getTraceFileList().iterator();
073 dataFileName = (String) it2.next();
074 Scene.setDataFilename(dataFileName);
075 Scene.autoLoadDataSource();
076 vizInfo = new VizInfo();
077 mapConfig.put("Factory",vizInfo.createFactory(inConfigFile.readLine().trim()));
078 mapConfig.put("Subject",vizInfo.createSubjectDefinition(inConfigFile.readLine().trim()));
079 for (int i=0;i<dimensionDefs.length;i++) {
080 dimensionDefs[i] = inConfigFile.readLine().trim();
081 }
082 mapConfig.put("Dimension",vizInfo.createDimension(dimensionDefs));
083 mapConfig.put("Predictor",vizInfo.createPredictor(inConfigFile.readLine().trim()));
084 mapConfig.put("Interval",vizInfo.createInterval(inConfigFile.readLine().trim()));
085 outPath = inConfigFile.readLine().trim();
086 execCmd(outPath,dataFileName);
087 } catch (Exception e) {
088 System.out.println("Configure file error for batch "+batchName+".");
089 continue;
090 }
091
092 while (it2.hasNext()) {
093 // load data file first
094 dataFileName = (String)it2.next();
095 Scene.setDataFilename(dataFileName);
096 Scene.autoLoadDataSource();
097 execCmd(outPath,dataFileName);
098 }
099
100 vizInfo.reset();
101 mapConfig.clear();
102 } // end of while(it)
103 batches.clear();
104 Scene.setDataFilename(null);
105 }
106
107 private void execCmd(String path,String dataFn) {
108 Scene.getVisualizationManager().newVisualization(((VisualizationFactory)mapConfig.get("Factory")).getName());
109
110 Scene.getVisualizationManager().autoPreVisualize(mapConfig);
111 Scene.autoVisualize();
112 Scene.getVisualizationManager().autoSaveResult(path,dataFn);
113
114 }
115
116 private void createDialog(){
117 dialog = new JDialog(owner,"Create Batch",true);
118 dialog.setBounds(new Rectangle(500,400));
119
120 JPanel batchName = new JPanel(new FlowLayout());
121 dialog.getContentPane().add(batchName,BorderLayout.NORTH);
122
123 Box boxMain = new Box(BoxLayout.Y_AXIS);
124 boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
125 "Choose file(s) to be processed & Processing configuration"));
126 dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
127
128
129 Box boxLabels = new Box(BoxLayout.X_AXIS);
130 JButton buttonGetDir = new JButton("Select Directory ..");
131 buttonGetDir.addActionListener(new ActionListener(){
132 public void actionPerformed(ActionEvent e){
133 fillFileList();
134 }
135 });
136 boxLabels.add(buttonGetDir);
137 boxLabels.add(Box.createHorizontalGlue());
138 boxLabels.add(new JLabel("Processing List "));
139
140 Box boxLists = new Box(BoxLayout.X_AXIS);
141 fileList = new java.awt.List(8,true);
142
143 Box boxAddRemove = new Box(BoxLayout.Y_AXIS);
144 JButton buttonAdd = new JButton(" > ");
145 buttonAdd.addActionListener(new ActionListener(){
146 public void actionPerformed(ActionEvent e){
147 addFiles();
148 }
149 });
150 JButton buttonRemove = new JButton(" < ");
151 buttonRemove.addActionListener(new ActionListener(){
152 public void actionPerformed(ActionEvent e){
153 removeFiles();
154 }
155 });
156 boxAddRemove.add(Box.createVerticalStrut(40));
157 boxAddRemove.add(buttonAdd);
158 boxAddRemove.add(Box.createVerticalStrut(20));
159 boxAddRemove.add(buttonRemove);
160
161 procList = new java.awt.List(8,true);
162 boxLists.add(fileList);
163 boxLists.add(Box.createHorizontalStrut(40));
164 boxLists.add(boxAddRemove);
165 boxLists.add(procList);
166
167 boxMain.add(boxLabels);
168 boxMain.add(Box.createVerticalStrut(5));
169 boxMain.add(boxLists);
170
171 Box boxConfig = new Box(BoxLayout.X_AXIS);
172 boxConfig.add(new JLabel("Choose configure file:"));
173 txtConfName = new JTextField(12);
174 boxConfig.add(Box.createHorizontalStrut(20));
175 boxConfig.add(txtConfName,BorderLayout.CENTER);
176 JButton buttonConfig = new JButton("...");
177 buttonConfig.addActionListener(new ActionListener(){
178 public void actionPerformed(ActionEvent e){
179 txtConfName.setText(chooseConfig());
180 }
181 });
182 boxConfig.add(buttonConfig,BorderLayout.EAST);
183
184 Box boxOkCancel = new Box(BoxLayout.X_AXIS);
185 JButton buttonOK = new JButton("OK");
186 JButton buttonCancel = new JButton("Cancel");
187
188 buttonOK.addActionListener(new ActionListener(){
189 public void actionPerformed(ActionEvent e){
190 onOK();
191 }
192 });
193 buttonCancel.addActionListener(new ActionListener(){
194 public void actionPerformed(ActionEvent e){
195 onCancel();
196 }
197 });
198 boxOkCancel.add(Box.createHorizontalStrut(30));
199 boxOkCancel.add(buttonOK);
200 boxOkCancel.add(Box.createHorizontalStrut(20));
201 boxOkCancel.add(buttonCancel);
202
203 Box boxBottom = Box.createVerticalBox();
204 boxBottom.add(Box.createVerticalStrut(12));
205 boxBottom.add(boxConfig);
206 boxBottom.add(Box.createVerticalStrut(40));
207 boxBottom.add(boxOkCancel);
208
209
210 dialog.getContentPane().add(boxBottom,BorderLayout.SOUTH);
211 //dialog.setResizable(false);
212 }
213
214 private void clearList() {
215 fileList.removeAll();
216 procList.removeAll();
217 setFile.clear();
218 setProcessFiles.clear();
219 }
220
221 private void onOK() {
222 BatchInfo batchInfo = new BatchInfo();
223
224 if (txtConfName.getText().trim().length() == 0) {
225 Scene.showErrorMessage("No configuration file selected!");
226 return;
227 }
228
229 if (setProcessFiles.size() == 0) {
230 Scene.showErrorMessage("Select file(s) to be processed please!");
231 return;
232 }
233
234 batchInfo.setConfigureFile(txtConfName.getText().trim());
235 batchInfo.setTraceFileList(setProcessFiles);
236 batches.put("Batch "+batches.entrySet().size(),batchInfo);
237 dialog.setVisible(false);
238 clearList();
239 }
240
241 private void onCancel() {
242 dialog.setVisible(false);
243 clearList();
244 }
245
246 private String chooseConfig() {
247 JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastConfigDir());
248
249 if(fc.showOpenDialog(owner) == JFileChooser.APPROVE_OPTION) {
250 File f = fc.getSelectedFile();
251 Scene.getUIManager().setLastConfigDir(f.getPath());
252 return (f.getPath());
253 }
254 else return "";
255 }
256
257 private void addFiles() {
258 String[] selItems = fileList.getSelectedItems();
259
260 for (int i=0;i<selItems.length;i++) {
261 if (setProcessFiles.contains(selItems[i])) continue;
262
263 setProcessFiles.add(selItems[i]);
264 procList.add(selItems[i]);
265 }
266 }
267
268 private void removeFiles() {
269 String[] selItems = procList.getSelectedItems();
270
271 for (int i=0;i<selItems.length;i++) {
272 setProcessFiles.remove(selItems[i]);
273 procList.remove(selItems[i]);
274 }
275 }
276
277 private void fillFileList() {
278 JFileChooser fc = new JFileChooser();
279 String path;
280
281 fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
282
283 if(fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
284 path = fc.getSelectedFile().getAbsolutePath();
285 File dir = new File(path);
286 FileSystemView fv = fc.getFileSystemView();
287 File[] fl = fv.getFiles(dir,false);
288 for (int i=0;i<fl.length;i++) {
289 String fn = fl[i].getName();
290 if (!fl[i].isFile() || !fn.endsWith(".dat") || setFile.contains(path+File.separator+fn))
291 continue;
292 setFile.add(path+File.separator+fn);
293 fileList.add(path+File.separator+fn);
294 }
295 }
296
297 }
298
299 // how to write a Configuration file
300
301 // step 1. use factory name to get corresponding fatory which executes the visulization
302 // factory name should be as following:
303 // ****************************************************************************
304 // 1.Table 2.Amount Hot Spot 3.Coordinate Hot Spot
305 // 4.Vertical Bar Char 5.Horizontal Bar Chart 6.Correlation Graph
306 // 7.Prediction Visualization
307 // ****************************************************************************
308
309 // step 2. Choose subject, stored as elementDefinition, the name
310 // of subject should come from EVolve.DemoSource startBuildDefinition(...),
311 // there are 2 catagories:
312 // 1. Object Allocation (denoted by allocationBuilder)
313 // 2. Method Invocation (denoted by invocationBuilder)
314
315 // step 3. build up dimensions needed, these dimensions are defined in
316 // factories, depends on different factory, different dimension are defined
317 // for example, in prediction, there are 3 dimensions:
318 // x-axis, y-axis and prediction. The value of these three dimension can be
319 // found in EVolve.DemoSource, and build up in Visualization.setDefinition, stored
320 // in DataFilter
321
322 // translate subject to elementDefinition
323
324 }
325