001    /**
002     * EvaluationReport.java
003     * jCOLIBRI2 framework. 
004     * @author Juan A. Recio-García.
005     * GAIA - Group for Artificial Intelligence Applications
006     * http://gaia.fdi.ucm.es
007     * 07/05/2007
008     */
009    package jcolibri.evaluation;
010    
011    import java.util.Enumeration;
012    import java.util.Hashtable;
013    import java.util.Set;
014    import java.util.Vector;
015    
016    /**
017     * This class stores the result of an evaluation. It is configured and filled by an Evaluator.
018     * This info is also used to represent graphically the result of an evaluation. 
019     * The stored information can be:
020     * <ul>
021     * <li>Several series of data. The lengh of the series is the number of executed cycles. And several series can be stored using diferent labels.
022     * <li>Any other information. The put/getOtherData() methods allow to store any other kind of data.
023     * <li>Number of cycles.
024     * <li>Total evaluation time.
025     * <li>Time per cycle.
026     * </ul>
027     * 
028     * @author Juan A. Recio García
029     * @version 2.0
030     */
031    public class EvaluationReport
032    {
033        private long totalTime;
034        private int numberOfCycles;
035        
036        /** Stores the series info */
037        protected Hashtable<String, Vector<Double>> data;
038        
039        /** Stores other info */
040        protected Hashtable<String, String> other;
041        
042        /** Default constructor */
043        public EvaluationReport()
044        {
045            data = new Hashtable<String, Vector<Double>>();
046            other = new Hashtable<String, String>();
047        }
048    
049        /**
050         * Gets the evaluation info identified by the label
051         * @param label Identifies the evaluation serie.
052         */
053        public Vector<Double> getSeries(String label)
054        {
055            return data.get(label);
056        }
057        
058        /**
059         * Stes the evaluation info
060         * @param label Identifier of the info
061         * @param evaluation Evaluation result
062         */
063        public void setSeries(String label, Vector<Double> evaluation)
064        {
065       
066            data.put(label, evaluation);
067        }
068        
069        public void addDataToSeries(String label, Double value)
070        {
071            Vector<Double> v = data.get(label);
072            if(v == null)
073            {
074                    v = new Vector<Double>();
075                    data.put(label, v);
076            }
077            v.add(value);
078        }
079        
080        /** 
081         * Returns the name of the contained evaluation series
082         */
083        public String[] getSeriesLabels()
084        {
085            Set<String> set = data.keySet();
086            String[] res = new String[set.size()];
087            int i=0;
088            for( String e : set)
089                res[i++] = e;
090            return res;
091        }
092    
093        
094        public void putOtherData(String label, String data)
095        {
096            this.other.put(label, data);
097        }
098        
099        public String getOtherData(String label)
100        {
101            return this.other.get(label);
102        }
103        
104        public String[] getOtherLabels()
105        {
106            Set<String> set = other.keySet();
107            String[] res = new String[set.size()];
108            int i=0;
109            for( String e : set)
110                res[i++] = e;
111            return res;     
112        }
113        
114        
115        
116        public int getNumberOfCycles()
117        {
118            return numberOfCycles;
119        }
120    
121        public void setNumberOfCycles(int numberOfCycles)
122        {
123            this.numberOfCycles = numberOfCycles;
124        }
125    
126        public long getTotalTime()
127        {
128            return totalTime;
129        }
130    
131        public void setTotalTime(long totalTime)
132        {
133            this.totalTime = totalTime;
134        }
135        
136        public double getTimePerCycle()
137        {
138            return (double)this.totalTime / (double)numberOfCycles;
139        }
140        
141        /**
142         * Checks if the evaluation series are correct. This is: all them must have the same length
143         */
144        public boolean checkData()
145        {
146            boolean ok = true;
147            int l = -1;
148            for(Enumeration<Vector<Double>> iter = data.elements(); iter.hasMoreElements() && ok ;)
149            {
150                Vector<Double> v = iter.nextElement();
151                if(l == -1)
152                    l = v.size();
153                else
154                    ok = (l == v.size());      
155            }
156            return ok;      
157        }
158        
159        
160        public String toString()
161        {
162            StringBuffer s = new StringBuffer();
163            s.append("Series:\n");
164            String[] series = this.getSeriesLabels();
165            for(int i=0; i<series.length; i++)
166            {
167                    s.append("  "+series[i]+": \n    ");
168                    Vector<Double> v = this.getSeries(series[i]);
169                    for(Double d: v)
170                            s.append(d+",");
171                    s.append("\n");
172            }
173            
174            s.append("\nOther data:\n");
175            String[] other = this.getOtherLabels();
176            for(int i=0; i<other.length; i++)
177            {
178                    s.append("  "+other[i]+": "+ this.getOtherData(other[i])+"\n");
179            }
180            
181            s.append("\nNumber of Cycles: "+ this.getNumberOfCycles());
182            s.append("\nTime per Cycle:   "+ this.getTimePerCycle()+" ms");
183            s.append("\nTotal time:       "+ this.getTotalTime()+" ms");
184            
185            return s.toString();
186        }
187    }