001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.data;
025    
026    import EVolve.*;
027    import EVolve.visualization.*;
028    import java.awt.*;
029    import java.util.*;
030    
031    public class DataManager {
032        private DataSource dataSource;
033        private ElementDefinition[] definition;
034        private ReferenceLink[] link;
035        private Entity[][] entity;
036        private ElementFilter[][] elementFilter;
037        private Color color;
038        private int colorRGB;
039        private int eventCounter;
040        private int[] eventInterval;
041        private int currentInterval;
042        private boolean dataProcessed;
043    
044        public DataManager(DataSource dataSource) {
045            this.dataSource = dataSource;
046            this.eventCounter = 0;
047        }
048    
049        public void init() throws DataProcessingException {
050            ElementBuilder.init();
051    
052            dataSource.init();
053    
054            dataSource.startBuildDefinition();
055    
056            ArrayList definitionList = new ArrayList();
057            ElementDefinition nextDefinition = dataSource.getNextDefinition();
058            while (nextDefinition != null) {
059                definitionList.add(nextDefinition);
060                nextDefinition = dataSource.getNextDefinition();
061            }
062    
063            definition = new ElementDefinition[definitionList.size()];
064            for (int i = 0; i < definition.length; i++) {
065                definition[((ElementDefinition)(definitionList.get(i))).getType()] = (ElementDefinition)(definitionList.get(i));
066            }
067    
068            ArrayList[] entityList = new ArrayList[definition.length];
069            for (int i = 0; i < entityList.length; i++) {
070                entityList[i] = new ArrayList();
071            }
072    
073            dataSource.startBuildEntity();
074    
075            Entity nextEntity = dataSource.getNextEntity();
076            while (nextEntity != null) {
077                entityList[nextEntity.getType()].add(nextEntity);
078                nextEntity = dataSource.getNextEntity();
079            }
080    
081            entity = new Entity[entityList.length][];
082    
083            for (int i = 0; i < entity.length; i++) {
084                entity[i] = new Entity[entityList[i].size()];
085                for (int j = 0; j < entity[i].length; j++) {
086                    entity[i][((Entity)(entityList[i].get(j))).getId()] = (Entity)(entityList[i].get(j));
087                }
088            }
089    
090            // creates links
091            ArrayList list = new ArrayList();
092            for (int i = 0; i < definition.length; i++) {
093                for (int j = 0; j < definition[i].getFieldDefinition().length; j++) {
094                    if ((definition[i].getFieldDefinition()[j].getReference() != -1) && (definition[i].getFieldDefinition()[j].getReference() != i)) {
095                        list.add(new ReferenceLink(definition[i].getFieldDefinition()[j].getName(), i, j, definition[i].getFieldDefinition()[j].getReference(), definition[i].getFieldDefinition()[j].getDescription()));
096                    }
097                }
098            }
099    
100            int head = 0;
101            int tail = list.size();
102            int initSize = list.size();
103    
104            while (head < tail) {
105                for (int i = head; i < tail; i++) {
106                    for (int j = 0; j < initSize; j++) {
107                        ReferenceLink from = (ReferenceLink)(list.get(j));
108                        ReferenceLink to = (ReferenceLink)(list.get(i));
109                        if ((from.getTargetType() == to.getSourceType()) && (from.getSourceType() != to.getTargetType())) {
110                            list.add(new ReferenceLink(from, to));
111                        }
112                    }
113                }
114                head = tail;
115                tail = list.size();
116            }
117    
118            link = new ReferenceLink[list.size()];
119            for (int i = 0; i < link.length; i++) {
120                link[i] = (ReferenceLink)(list.get(i));
121            }
122        }
123    
124        public void sendEvents() throws DataProcessingException {
125            Selection[] selection = Scene.getFilter().getSelection();
126            Selection activeSelection = Scene.getFilter().getActiveSelection();
127            dataProcessed = false;
128            if (activeSelection != null) {
129                eventInterval = new int[4];
130                eventInterval[0] = 0;
131                eventInterval[1] = activeSelection.getStart();
132                eventInterval[2] = activeSelection.getEnd();
133                eventInterval[3] = Integer.MAX_VALUE;
134            } else {
135                eventInterval = new int[2];
136                eventInterval[0] = 0;
137                eventInterval[1] = Integer.MAX_VALUE;
138            }
139    
140            elementFilter = new ElementFilter[eventInterval.length - 1][definition.length];
141            for (int i = 0; i < elementFilter.length; i++) {
142                boolean isIncluded = activeSelection == null ? true : false;
143                for (int j = 0; j < selection.length; j++) {
144                    if ((selection[j] == activeSelection)&&(selection[j].getStart() >= eventInterval[i])&&(selection[j].getEnd() <= eventInterval[i+1])) {
145                        isIncluded = true;
146                        break;
147                    }
148                }
149    
150                if (isIncluded)
151                {
152                    ArrayList list = new ArrayList();
153                    if (activeSelection != null) list.add(activeSelection);
154                    for (int j = 0; j < selection.length; j++) {
155                        if ((selection[j].getColor()!=null) && (selection[j] != activeSelection)) {
156                            list.add(selection[j].specialClone());
157                        }
158                    }
159                    Selection[] tempSelection = new Selection[list.size()];
160                    for (int j = 0; j < tempSelection.length; j++) {
161                        tempSelection[j] = (Selection)(list.get(j));
162                    }
163    
164                    for (int j = 0; j < definition.length; j++) {
165                        if (Scene.getVisualizationManager().getVisualizationType().indexOf("" + j + "") != -1) {
166                            elementFilter[i][j] = new ElementFilter(definition[j], tempSelection);
167                        } else {
168                            elementFilter[i][j] = null;
169                        }
170                    }
171                } else {
172                    elementFilter[i] = null;
173                }
174    
175            }
176    
177            currentInterval = 0;
178    
179            dataSource.startBuildEvent();
180            eventCounter = 0;
181    
182            Event nextEvent = dataSource.getNextEvent();
183            while (nextEvent != null) {
184                if ((elementFilter[currentInterval] != null) && (elementFilter[currentInterval][nextEvent.getType()] != null)) {
185                    color = elementFilter[currentInterval][nextEvent.getType()].getColor(nextEvent);
186                    if (color != null)
187                    {
188                        colorRGB = color.getRGB();
189                        Scene.getVisualizationManager().receiveEvent(nextEvent);
190                    }
191                }
192    
193                if (eventCounter == eventInterval[currentInterval + 1]) {
194                    currentInterval++;
195                }
196    
197                eventCounter++;
198                nextEvent = dataSource.getNextEvent();
199            }
200            dataProcessed = true;
201        }
202    
203        public Entity[][] getEntity() {
204            return entity;
205        }
206    
207        public ElementDefinition[] getElementDefinition() {
208            return definition;
209        }
210    
211        public ElementDefinition[] getElementDefinition(VisualizationDefinition visualizationDefinition) {
212            ArrayList list = new ArrayList();
213            for (int i = 0; i < definition.length; i++) {
214                boolean add = true;
215                for (int j = 0; j < visualizationDefinition.getDimensionDefinition().length; j++) {
216                    DataFilter[] temp = getDataFilter(definition[i], visualizationDefinition.getDimensionDefinition()[j].getProperty());
217                    if (temp.length == 0) {
218                        add = false;
219                        break;
220                    }
221                }
222                if (add) {
223                    list.add(definition[i]);
224                }
225            }
226    
227            ElementDefinition[] returnVal = new ElementDefinition[list.size()];
228            for (int i = 0; i < returnVal.length; i++) {
229                returnVal[i] = (ElementDefinition)(list.get(i));
230            }
231    
232            return returnVal;
233        }
234    
235        public DataFilter[] getDataFilter(ElementDefinition subjectDefinition, String property) {
236            ArrayList list = new ArrayList();
237            FieldDefinition fd;
238            if (property.equals("reference")) {
239                for (int i = 0; i < link.length; i++) {
240                    if (link[i].getSourceType() == subjectDefinition.getType()) {
241                        list.add(new DataFilter(link[i].getName(), link[i].getSourceIndex(), link[i], link[i].getDescription(),null));
242                    }
243                }
244            } else {
245                for (int i = 0; i < subjectDefinition.getFieldDefinition().length; i++) {
246                    fd = subjectDefinition.getFieldDefinition()[i];
247                    if (subjectDefinition.getFieldDefinition()[i].hasProperty(property)) {
248                        list.add(new DataFilter(subjectDefinition.getFieldDefinition()[i].getName(), i,
249                                                null, subjectDefinition.getFieldDefinition()[i].getDescription(),fd.getProperties()));
250                    }
251                }
252            }
253    
254            DataFilter[] returnVal = new DataFilter[list.size()];
255            for (int i = 0; i < returnVal.length; i++) {
256                returnVal[i] = (DataFilter)(list.get(i));
257            }
258    
259            return returnVal;
260        }
261    
262        public Color getColor() {
263            return color;
264        }
265    
266        public int getColorRGB() {
267            return colorRGB;
268        }
269    
270        public int getEventCounter() {
271            return eventCounter;
272        }
273    
274        public ReferenceLink[] getReferenceLink() {
275            return link;
276        }
277    
278        public boolean isCompleted() {
279            return dataProcessed;
280        }
281    
282        private void getTraceSize() {
283        }
284    
285    }