001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Dec 1, 2002
005     * Time: 1:16:41 AM
006     */
007    
008    package EVolve.visualization.XYViz.ValValViz;
009    
010    import java.awt.*;
011    import java.awt.event.KeyEvent;
012    import javax.swing.*;
013    import EVolve.Scene;
014    import EVolve.data.Element;
015    import EVolve.util.painters.MissRatePainter;
016    import EVolve.util.painters.shapes.Line;
017    import EVolve.util.xmlutils.datastructures.SerializedVisualization;
018    import EVolve.visualization.AutoShapeImage;
019    import EVolve.visualization.Dimension;
020    import EVolve.visualization.Predictor;
021    import EVolve.visualization.PredictorFactory;
022    import EVolve.visualization.ReferenceDimension;
023    import EVolve.visualization.ValueDimension;
024    
025    public class MissPredictionMetricViz extends ValueValueVisualization {
026        private PredictorFactory[] factory; // predictor factories
027        private Predictor[] predictor; // predictors
028        private JComboBox comboPredictor; // combobox for selecting predictor factory
029        private PredictorFactory selectedFactory; // predictor factory in use
030        private JCheckBox chkSelectTimeFrame;
031        protected static JCheckBox selectionOptions[] = null;
032        private static int SELECT_OPTION;
033        protected JTextField textInterval;
034    
035        public MissPredictionMetricViz(PredictorFactory[] factory) {
036            super();
037            interval = 1000;
038            this.factory = factory;
039            SELECT_OPTION = 0x0001;
040        }
041    
042        public Dimension[] createDimension() {
043            Dimension[] returnVal = new Dimension[3];
044            xAxis = new ValueDimension();
045            entityIdFilter = new ReferenceDimension();
046            filter2 = new ReferenceDimension();
047    
048            returnVal[0] = xAxis;
049            returnVal[1] = entityIdFilter;
050            returnVal[2] = filter2;
051    
052            return returnVal;
053        }
054    
055        protected void updateConfiguration() {
056            try {
057                if (autoInterval != -1) {
058                    interval = autoInterval;
059                    autoInterval = -1;
060                } else
061                    interval = Integer.parseInt(textInterval.getText());
062    
063                super.updateConfiguration();
064                canvas.setName("Bytecode", "Miss rate");
065            } catch (NumberFormatException e) {
066                Scene.showErrorMessage("Interval must be an integer");
067                configure();
068            }
069        }
070    
071        protected JPanel createConfigurationPanel() {
072            JPanel configurationPanel = new JPanel(new GridLayout(2, 1, 5, 5));
073    
074            selectedFactory = factory[0];
075    
076            JPanel panelTop = new JPanel(new FlowLayout());
077            configurationPanel.add(panelTop);
078    
079            panelTop.add(new JLabel("Predictor: "));
080    
081            comboPredictor = new JComboBox();
082            for (int i = 0; i < factory.length; i++) {
083                comboPredictor.addItem(factory[i].getName());
084            }
085            panelTop.add(comboPredictor);
086    
087    
088            JPanel panelBottom = new JPanel(new FlowLayout());
089            panelBottom.add(new JLabel("Interval: "));
090    
091            textInterval = new JTextField(String.valueOf(interval), 10);
092            panelBottom.add(textInterval);
093    
094            configurationPanel.add(panelBottom);
095    
096            return configurationPanel;
097        }
098    
099        public void preVisualize() {
100            predictor = new Predictor[entityIdFilter.getMaxEntityNumber()];
101            for (int i = 0; i < predictor.length; i++) {
102                predictor[i] = selectedFactory.createPredictor();
103            }
104    
105            image = new AutoShapeImage(new Line(0,0,1));
106            xMax = 0;
107            installPainter();
108            super.preVisualize();
109        }
110    
111        public void receiveElement(Element element) {
112            if (element.isOptional()) return;
113    
114            long temp = xAxis.getField(element);
115            long x = temp / interval;
116            long y = entityIdFilter.getField(element);
117            long z = filter2.getField(element);
118    
119            countEvents(temp);
120    
121            if (xOffset == -1) xOffset = x;
122            painter.paint(image,x-xOffset,y,filter2.getEntityFromInt((int)z).getId());
123    
124            if (temp > xMax) {
125                xMax = temp;
126            }
127        }
128    
129        public void visualize() {
130            canvas.setName(xAxis.getName() + " (" + xMax + ")","Miss Prediction rate (" +
131                          ((MissRatePainter)painter).getMaxMiss()*100 + "%)");
132            sort();
133        }
134    
135        protected String mouseMove(int x, int y) {
136            int X = canvas.getImageX(x);
137            if ((X >= 0) && (X < image.getW())) {
138                Scene.setStatus(((MissRatePainter)painter).getMissrate(X)+"%");
139            } else {
140                Scene.setStatus(" ");
141            }
142            return null;
143        }
144    
145        protected void installPainter() {
146            painter = new MissRatePainter(predictor,filter2.getDataFilter().getTargetType());
147        }
148    
149        public void makeSelection() {
150            preMakeSelection();
151            if (selectionName==null) return;
152    
153            int x1 = canvas.getStartX();
154            int x2 = canvas.getEndX();
155    
156            if (dataSourceId != Scene.getDataSourceManager().getCurrentDataSourceId()) {
157                Scene.showErrorMessage("The active data source used currently is different from \n" +
158                                       "this visualization, please choose \"" +
159                                       Scene.getDataSourceManager().getUsedDataSourceName(dataSourceId)+"\".");
160                return;
161            }
162    
163            if (((x1<0)&&(x2<0)) || ((x1>=timeMap.size()))&&(x2>=timeMap.size()))
164               return;
165    
166            if (x1 < 0) {
167                x1 = 0;
168            }
169    
170            if (x2 > (timeMap.size() - 1)) {
171                x2 = timeMap.size() - 1;
172            }
173    
174            int[] selection = new int[entityIdFilter.getEntityNumber()];
175            for (int i = 0; i < selection.length; i++) {
176                selection[i] = i;
177            }
178    
179    
180            entityIdFilter.makeSelection(selectionName,subjectDefinition.getType(),selection,((long[])timeMap.get(x1))[1],
181                                         ((long[])timeMap.get(x2))[1],timeMap);
182    
183        }
184    
185        public void restoreConfiguration(SerializedVisualization config) {
186    
187            for (int i=0; i<factory.length; i++) {
188                if (factory[i].getName().equals(config.PredictorName)) {
189                    comboPredictor.setSelectedIndex(i);
190                    selectedFactory = factory[i];
191                    break;
192                }
193            }
194    
195            super.restoreConfiguration(config);
196        }
197    
198        public SerializedVisualization getCurrentConfiguration() {
199            SerializedVisualization data = super.getCurrentConfiguration();
200            data.xAxis = xAxis.getName();
201            data.yAxis = entityIdFilter.getName();
202            data.zAxis = filter2.getName();
203            data.PredictorName = selectedFactory.getName();
204    
205            return data;
206        }
207    
208        public JCheckBox[] createSelectionOptions() {
209            if (selectionOptions!=null) return selectionOptions;
210    
211            chkSelectTimeFrame = new JCheckBox("Time Frame");
212            chkSelectTimeFrame.setMnemonic(KeyEvent.VK_T);
213            chkSelectTimeFrame.setSelected(true);
214            chkSelectTimeFrame.setEnabled(false);
215    
216    
217            selectionOptions = new JCheckBox[1];
218            selectionOptions[0] = chkSelectTimeFrame;
219    
220            return selectionOptions;
221        }
222    
223        public Object clone() {
224            MissPredictionMetricViz o = (MissPredictionMetricViz) super.clone();
225            o.dimension[0] = o.xAxis;
226            o.dimension[1] = o.entityIdFilter;
227            o.dimension[2] = o.filter2;
228            o.factory = new PredictorFactory[factory.length];
229            for (int i=0; i<factory.length; i++) {
230                o.factory[i] = (PredictorFactory)factory[i].clone();
231            }
232            o.selectedFactory = (PredictorFactory)selectedFactory.clone();
233            if (predictor != null) {
234                o.predictor = new Predictor[predictor.length];
235                for (int i=0; i<predictor.length; i++)
236                    o.predictor[i] = (Predictor)predictor[i].clone();
237            }
238            o.createDialog();
239            return o;
240        }
241    
242    }