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