001    package EVolve;
002    
003    /* EVolve - an Extensible Software Visualization Framework
004     * Copyright (C) 2001-2002 Qin Wang
005     *
006     * This library is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Library General Public
008     * License as published by the Free Software Foundation; either
009     * version 2 of the License, or (at your option) any later version.
010     *
011     * This library is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Library General Public License for more details.
015     *
016     * You should have received a copy of the GNU Library General Public
017     * License along with this library; if not, write to the
018     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
019     * Boston, MA 02111-1307, USA.
020     */
021    
022    /*
023     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
024     */
025    
026    import EVolve.*;
027    import EVolve.util.NumericStringComparator;
028    //import EVolve.util.InstructionAnalyzeRunner;
029    import EVolve.data.*;
030    import java.io.*;
031    import java.util.*;
032    import javax.swing.*;
033    
034    public class DemoSource implements DataSource {
035        private RandomAccessFile file;
036    
037        private EntityBuilder classBuilder;
038    
039        private EntityBuilder threadBuilder;
040    
041        private EntityBuilder methodBuilder;
042        private FieldDefinition methodDefiningClass;
043    
044        private EntityBuilder locationBuilder;
045    
046        private EntityBuilder sizeBuilder;
047    
048        private EventBuilder allocationBuilder;
049        private FieldDefinition allocationObjectCount;
050        private FieldDefinition allocationObjectSize;
051        private FieldDefinition allocationBytecode;
052        private FieldDefinition allocationObjectType;
053        private FieldDefinition allocationThread;
054        private FieldDefinition allocationMethod;
055        private FieldDefinition allocationLocation;
056        private FieldDefinition allocationSize;
057        private FieldDefinition allocationFieldSum;
058        private FieldDefinition allocationFieldCounter;
059    
060        private EventBuilder invocationBuilder;
061        private FieldDefinition invocationNumber;
062        private FieldDefinition invocationBytecode;
063        private FieldDefinition invocationThread;
064        private FieldDefinition invocationMethod;
065        private FieldDefinition invocationLocation;
066        private FieldDefinition invocationStart;
067        private FieldDefinition invocationEnd;
068        private FieldDefinition invocationFieldCounter;
069    
070    
071        private ElementDefinition[] definition;
072        private int definitionCounter;
073    
074        private TreeMap classMap;
075        private TreeMap threadMap;
076        private TreeMap methodMap;
077        private TreeMap locationMap;
078        private TreeMap sizeMap;
079    
080    //    private InstructionAnalyzeRunner runner;
081    
082        public void init() throws DataProcessingException {
083            String fn = Scene.getDataFileName();
084            //runner = Scene.getToolsManager().getInstructionAnalyzeRunner();
085            if (fn == null) {
086                JFileChooser fc = new JFileChooser(Scene.getUIManager().getLastDataDir());
087                if (fc.showOpenDialog(Scene.getFrame()) == JFileChooser.APPROVE_OPTION) {
088                    try {
089                        file = new RandomAccessFile(fc.getSelectedFile(), "r");
090                        Scene.setDataFilename(fc.getSelectedFile().getPath());
091                        Scene.getUIManager().setLastDataDir(fc.getSelectedFile().getPath());
092                        Scene.setDataFilename(null);
093                    } catch (IOException e) {
094                        throw new DataProcessingException("File loading failed.");
095                    }
096                } else {
097                    throw new DataProcessingException("File loading canceled.");
098                }
099            } else {
100                try {
101                    file = new RandomAccessFile(fn, "r");
102                } catch (IOException e) {
103                    throw new DataProcessingException("File loading failed.");
104                }
105            }
106    
107    
108        }
109    
110        public void startBuildDefinition() throws DataProcessingException {
111            String[] propertySum = {"time","sum"};
112            String[] propertyCount = {"time","count","Dotplot time"};
113            String[] propertyAmount = {"amount"};
114            String[] propertyCoordinate = {"coordinate","time"};
115            String[] propertyIndicator = {"indicator"};
116    
117            definition = new ElementDefinition[7];
118    
119            classBuilder = new EntityBuilder("Class", "Class");
120            definition[0] = classBuilder.buildDefinition();
121    
122            threadBuilder = new EntityBuilder("Thread", "Thread");
123            definition[1] = threadBuilder.buildDefinition();
124    
125            methodBuilder = new EntityBuilder("Method", "Method");
126            methodDefiningClass = methodBuilder.buildReferenceDefinition("Defining Class", classBuilder, null, "Defining class of the method");
127            definition[2] = methodBuilder.buildDefinition();
128    
129            locationBuilder = new EntityBuilder("Location", "Location in Method");
130            definition[3] = locationBuilder.buildDefinition();
131    
132            sizeBuilder = new EntityBuilder("Size", "Object Size");
133            sizeBuilder.addComparator(new NumericStringComparator());
134            definition[4] = sizeBuilder.buildDefinition();
135    
136            allocationBuilder = new EventBuilder("Object Allocation", "Object allocation event");
137            allocationFieldSum = allocationBuilder.buildValueDefinition("Memory Allocated", propertySum, "Size of memory allocated");
138            allocationFieldCounter = allocationBuilder.buildValueDefinition("Allocations", propertyCount, "Number of objects allocated");
139            allocationObjectCount = allocationBuilder.buildValueDefinition("Object Count", propertyAmount, "Number of objects allocated");
140            allocationObjectSize = allocationBuilder.buildValueDefinition("Memory Allocated", propertyAmount, "Size of memory allocated");
141            allocationBytecode = allocationBuilder.buildValueDefinition("Bytecode", propertyCoordinate, "Bytecode sequence");
142            allocationObjectType = allocationBuilder.buildReferenceDefinition("Object Type", classBuilder, null, "Type of the object");
143            allocationThread = allocationBuilder.buildReferenceDefinition("Thread", threadBuilder, null, "Thread in which the object is allocated");
144            allocationMethod = allocationBuilder.buildReferenceDefinition("Allocating Methods", methodBuilder, null, "Method that creates the object");
145            allocationLocation = allocationBuilder.buildReferenceDefinition("Allocating Locations", locationBuilder, null, "Location where the object is created");
146            allocationSize = allocationBuilder.buildReferenceDefinition("Allocation Size", sizeBuilder, null, "Allocation Size");
147            definition[5] = allocationBuilder.buildDefinition();
148    
149            invocationBuilder = new EventBuilder("Method Invocation", "Method invocation event");
150            invocationFieldCounter = invocationBuilder.buildValueDefinition("Invocations", propertyCount, "Total number of invocations");
151            invocationNumber = invocationBuilder.buildValueDefinition("Number of Invocations", propertyAmount, "Total number of invocations");
152            invocationBytecode = invocationBuilder.buildValueDefinition("Bytecode", propertyCoordinate, "Bytecode sequence");
153            invocationThread = invocationBuilder.buildReferenceDefinition("Thread", threadBuilder, null, "Thread in which the method is invoked");
154            invocationMethod = invocationBuilder.buildReferenceDefinition("Method", methodBuilder, null, "Method that is invoked");
155            invocationLocation = invocationBuilder.buildReferenceDefinition("Invoking Locations", locationBuilder, null, "Location where the method is invoked");
156            invocationStart = invocationBuilder.buildValueDefinition("Invoking start",propertyIndicator,"Start of invocation");
157            invocationEnd = invocationBuilder.buildValueDefinition("Invoking end",propertyIndicator,"End of invocation");
158            definition[6] = invocationBuilder.buildDefinition();
159    
160            definitionCounter = -1;
161        }
162    
163        public ElementDefinition getNextDefinition() throws DataProcessingException {
164            definitionCounter++;
165            if (definitionCounter < definition.length) {
166                return definition[definitionCounter];
167            } else {
168                return null;
169            }
170        }
171    
172        private String getSub(String line, int part) {
173            int start = line.indexOf(' ') + 1;
174            int i = 1;
175            while (i < part) {
176                start = line.indexOf(' ', start) + 1;
177                i++;
178            }
179            int end = line.indexOf(' ', start);
180            if (end == -1) {
181                return line.substring(start);
182            } else {
183                return line.substring(start, end);
184            }
185        }
186    
187        public void startBuildEntity() throws DataProcessingException {
188            try {
189                file.seek(0);
190    
191                classMap = new TreeMap();
192                threadMap = new TreeMap();
193                methodMap = new TreeMap();
194                locationMap = new TreeMap();
195                sizeMap = new TreeMap();
196            } catch (IOException e) {
197                throw new DataProcessingException("File processing failed.");
198            }
199        }
200    
201        public Entity getNextEntity() throws DataProcessingException {
202            try {
203                Entity returnVal = null;
204                String line = file.readLine();
205    
206                while ((returnVal == null) && (line != null) &&(line.length()>0)) {
207                    line = line.trim();
208                    char ch = line.charAt(0);
209    
210                    if (ch == 'C') {
211                        classBuilder.newEntity(getSub(line, 1));
212                        returnVal = classBuilder.buildEntity();
213                        classMap.put(getSub(line, 2), returnVal);
214                        //runner.addClass(Integer.parseInt(getSub(line,2)),getSub(line,1));
215                    } else if (ch == 'T') {
216                        threadBuilder.newEntity(getSub(line, 1));
217                        returnVal = threadBuilder.buildEntity();
218                        threadMap.put(getSub(line, 2), returnVal);
219                    } else if (ch == 'M') {
220                        methodBuilder.newEntity(getSub(line, 1));
221                        methodBuilder.addReferenceField(methodDefiningClass, (Entity)(classMap.get(getSub(line, 3))));
222                        returnVal = methodBuilder.buildEntity();
223                        methodMap.put(getSub(line, 2), returnVal);
224                        //runner.addMethod(Integer.parseInt(getSub(line,3)),Integer.parseInt(getSub(line,2)),getSub(line,1));
225                    } else if (ch == 'L') {
226                        locationBuilder.newEntity(getSub(line, 1));
227                        returnVal = locationBuilder.buildEntity();
228                        locationMap.put(getSub(line, 2), returnVal);
229                    } else if (ch == 'O') {
230                        String size = getSub(line, 6);
231                        if (!sizeMap.containsKey(size)) {
232                            sizeBuilder.newEntity(size);
233                            returnVal = sizeBuilder.buildEntity();
234                            sizeMap.put(size, returnVal);
235                        }
236                    }
237    
238                    if (returnVal == null) {
239                        line = file.readLine();
240                    }
241                }
242    
243                return returnVal;
244            } catch (IOException e) {
245                throw new DataProcessingException("File processing failed.");
246            }
247        }
248    
249        public void startBuildEvent() throws DataProcessingException {
250            try {
251                file.seek(0);
252            } catch (IOException e) {
253                throw new DataProcessingException("File processing failed.");
254            }
255        }
256    
257        public Event getNextEvent() throws DataProcessingException {
258            try {
259                Event returnVal = null;
260                String line = file.readLine();
261    
262                while ((returnVal == null) && (line != null)) {
263                    line = line.trim();
264                    char ch = line.charAt(0);
265    
266                    if (ch == 'O') {
267                        allocationBuilder.newEvent();
268                        String size = getSub(line, 6);
269                        allocationBuilder.addValueField(allocationObjectCount, 1);
270                        allocationBuilder.addValueField(allocationObjectSize, Integer.parseInt(getSub(line, 6)));
271                        allocationBuilder.addValueField(allocationFieldCounter, 1);
272                        allocationBuilder.addValueField(allocationFieldSum, Integer.parseInt(getSub(line, 6)));
273                        allocationBuilder.addValueField(allocationBytecode, Integer.parseInt(getSub(line, 1)));
274                        allocationBuilder.addReferenceField(allocationObjectType, (Entity)(classMap.get(getSub(line, 5))));
275                        allocationBuilder.addReferenceField(allocationThread, (Entity)(threadMap.get(getSub(line, 2))));
276                        allocationBuilder.addReferenceField(allocationMethod, (Entity)(methodMap.get(getSub(line, 3))));
277                        allocationBuilder.addReferenceField(allocationLocation, (Entity)(locationMap.get(getSub(line, 4))));
278                        allocationBuilder.addReferenceField(allocationSize, (Entity)(sizeMap.get(size)));
279                        returnVal = allocationBuilder.buildEvent();
280                    } else if (ch == '+') {
281                        invocationBuilder.newEvent();
282                        invocationBuilder.addValueField(invocationNumber, 1);
283                        invocationBuilder.addValueField(invocationFieldCounter, 1);
284                        invocationBuilder.addValueField(invocationBytecode, Integer.parseInt(getSub(line, 1)));
285                        invocationBuilder.addReferenceField(invocationThread, (Entity)(threadMap.get(getSub(line, 2))));
286                        invocationBuilder.addReferenceField(invocationMethod, (Entity)(methodMap.get(getSub(line, 3))));
287                        invocationBuilder.addReferenceField(invocationLocation, (Entity)(locationMap.get(getSub(line, 4))));
288                        invocationBuilder.addValueField(invocationStart,Integer.MIN_VALUE);
289                        returnVal = invocationBuilder.buildEvent();
290                    } else if (ch == '-') {
291                        invocationBuilder.newEvent();
292                        invocationBuilder.addValueField(invocationNumber, 1);
293                        invocationBuilder.addValueField(invocationBytecode, Integer.parseInt(getSub(line, 1)));
294                        invocationBuilder.addReferenceField(invocationThread, (Entity)(threadMap.get(getSub(line, 2))));
295                        invocationBuilder.addReferenceField(invocationMethod, (Entity)(methodMap.get(getSub(line, 3))));
296                        invocationBuilder.addReferenceField(invocationLocation, (Entity)(locationMap.get(getSub(line, 4))));
297                        invocationBuilder.addValueField(invocationEnd,Integer.MAX_VALUE);
298                        returnVal = invocationBuilder.buildEvent();
299                    }
300    
301                    if (returnVal == null) {
302                        line = file.readLine();
303                    }
304                }
305    
306                return returnVal;
307            } catch (IOException e) {
308                throw new DataProcessingException("File processing failed.");
309            }
310        }
311    
312    }