KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hammurapi > results > simple > SimpleAggregatedResults


1 /*
2  * Hammurapi
3  * Automated Java code review system.
4  * Copyright (C) 2004 Hammurapi Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * URL: http://www.hammurapi.org
21  * e-Mail: support@hammurapi.biz
22  */

23
24 package org.hammurapi.results.simple;
25
26 import java.io.Serializable JavaDoc;
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.TreeMap JavaDoc;
38 import java.util.TreeSet JavaDoc;
39
40 import org.hammurapi.HammurapiException;
41 import org.hammurapi.Violation;
42 import org.hammurapi.Waiver;
43 import org.hammurapi.WaiverSet;
44 import org.hammurapi.results.AggregatedResults;
45 import org.hammurapi.results.Annotation;
46 import org.hammurapi.results.BasicResults;
47 import org.hammurapi.results.InspectorSummary;
48
49 import com.pavelvlasov.metrics.Metric;
50 import com.pavelvlasov.metrics.SimpleMetric;
51 import com.pavelvlasov.metrics.Metric.Measurement;
52 import com.pavelvlasov.review.SourceMarker;
53
54 /**
55  *
56  * @author Pavel Vlasov
57  * @version $Revision: 1.10 $
58  */

59 public class SimpleAggregatedResults implements AggregatedResults, Serializable JavaDoc {
60     /**
61      * Comment for <code>serialVersionUID</code>
62      */

63     private static final long serialVersionUID = -3469850853510240052L;
64     protected long codeBase=0;
65     protected long reviews=0;
66     protected double violationLevel=0;
67     private int violationsNumber=0;
68     private int waivedViolationsNumber=0;
69     private Map JavaDoc metrics=new HashMap JavaDoc();
70     private Set JavaDoc warnings=new TreeSet JavaDoc();
71     
72     public SimpleAggregatedResults(WaiverSet waiverSet) {
73         getContext().waiverSet=waiverSet;
74     }
75     
76     /**
77      * Map( severity (Integer) -> Map(string -> InspectorSummary))
78      *
79      */

80     protected Map JavaDoc severitySummary=new TreeMap JavaDoc();
81     
82     /**
83      * Add violation to severity summary
84      * @param violation
85      */

86     public Waiver addViolation(Violation violation) throws HammurapiException {
87         Context context = getContext();
88         Waiver ret = context.waiverSet==null ? null : context.waiverSet.requestWaiver(violation, false);
89         if (ret==null) {
90             Integer JavaDoc severity = violation.getDescriptor().getSeverity();
91             Integer JavaDoc s=severity;
92             Map JavaDoc rsm=(Map JavaDoc) severitySummary.get(s);
93             if (rsm==null) {
94                 rsm=new TreeMap JavaDoc();
95                 severitySummary.put(s, rsm);
96             }
97             
98             SimpleInspectorSummary rsEntry=(SimpleInspectorSummary) rsm.get(violation.getDescriptor().getName());
99             if (rsEntry==null) {
100                 rsEntry=new SimpleInspectorSummary(violation.getDescriptor());
101                 rsm.put(violation.getDescriptor().getName(), rsEntry);
102     
103             }
104             rsEntry.addLocation(violation.getSource());
105             
106             /**
107              * Violations with severity >=5 considered as hints.
108              */

109             if (severity.intValue()>0 && severity.intValue()<5) {
110                 violationLevel+=Math.pow(10, 1-severity.doubleValue());
111             }
112         } else {
113             waivedViolationsNumber++;
114         }
115         
116         return ret;
117     }
118     
119     public Map JavaDoc getSeveritySummary() {
120         return Collections.unmodifiableMap(severitySummary);
121     }
122     
123     public Number JavaDoc getMaxSeverity() {
124         Integer JavaDoc ret=null;
125         Iterator JavaDoc it=severitySummary.keySet().iterator();
126         while (it.hasNext()) {
127             Integer JavaDoc severity=(Integer JavaDoc) it.next();
128             if (ret==null || severity.intValue()<ret.intValue()) {
129                 ret=severity;
130             }
131         }
132         return ret;
133     }
134
135     public Collection JavaDoc getWarnings() {
136         return Collections.unmodifiableCollection(warnings);
137     }
138     
139     public void addWarning(Violation warning) {
140         warnings.add(warning);
141     }
142     
143     public void addMetric(SourceMarker source, String JavaDoc name, double value) {
144         class SourceAwareMetric extends SimpleMetric {
145             SourceAwareMetric(String JavaDoc name) {
146                 super(name);
147             }
148             
149             SourceAwareMetric(String JavaDoc name, boolean keepMeasurements) {
150                 super(name, keepMeasurements);
151             }
152             
153             void add(final SourceMarker source, final double value, final long time) {
154                 addMeasurement(new Measurement() {
155                     public double getValue() {
156                         return value;
157                     }
158                 
159                     public long getTime() {
160                         return time;
161                     }
162                 });
163             }
164         }
165         
166         SourceAwareMetric metric=(SourceAwareMetric) metrics.get(name);
167         if (metric==null) {
168             metric=new SourceAwareMetric(name);
169             metrics.put(name, metric);
170         }
171         metric.add(source, value, 0);
172     }
173     
174     public double getViolationLevel() {
175         return violationLevel;
176     }
177     
178     public long getReviewsNumber() {
179         return reviews;
180     }
181     
182     public long getCodeBase() {
183         return codeBase;
184     }
185     
186     public int getViolationsNumber() {
187         return violationsNumber;
188     }
189     
190 // public double getPerfectionAffinityIndex() {
191
// return 1-violationLevel/reviews;
192
// }
193
//
194
public Map JavaDoc getMetrics() {
195         return Collections.unmodifiableMap(metrics);
196     }
197     
198 // public boolean isIncomplete() {
199
// return isIncomplete;
200
// }
201

202     public void aggregate(AggregatedResults agregee) {
203         codeBase+=agregee.getCodeBase();
204         reviews+=agregee.getReviewsNumber();
205         violationsNumber+=agregee.getViolationsNumber();
206         waivedViolationsNumber+=agregee.getWaivedViolationsNumber();
207         violationLevel+=agregee.getViolationLevel();
208         warnings.addAll(agregee.getWarnings());
209         
210         Iterator JavaDoc it=agregee.getSeveritySummary().entrySet().iterator();
211         while (it.hasNext()) {
212             Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
213             Map JavaDoc ss=(Map JavaDoc) severitySummary.get(entry.getKey());
214             if (ss==null) {
215                 ss=new TreeMap JavaDoc();
216                 severitySummary.put(entry.getKey(), ss);
217             }
218             
219             Iterator JavaDoc rsit=((Map JavaDoc) entry.getValue()).values().iterator();
220             while (rsit.hasNext()) {
221                 InspectorSummary rse=(InspectorSummary) rsit.next();
222                 SimpleInspectorSummary masterRSE=(SimpleInspectorSummary) ss.get(rse.getName());
223                 if (masterRSE==null) {
224                     masterRSE=new SimpleInspectorSummary(rse);
225                     ss.put(rse.getName(), masterRSE);
226                 } else {
227                     masterRSE.addLocations(rse.getLocations());
228                 }
229             }
230         }
231         
232         it=agregee.getMetrics().entrySet().iterator();
233         while (it.hasNext()) {
234             Map.Entry JavaDoc entry=(Map.Entry JavaDoc) it.next();
235             Metric metric=(Metric) metrics.get(entry.getKey());
236             if (metric==null) {
237                 metric=new SimpleMetric((String JavaDoc) entry.getKey());
238                 metrics.put(entry.getKey(), metric);
239             }
240             metric.add((SimpleMetric) entry.getValue());
241         }
242     }
243     
244     /**
245      * Sets number of reviews.
246      * @param reviews The reviews to set.
247      */

248     public void setReviewsNumber(long reviews) {
249         this.reviews = reviews;
250     }
251
252     public void setCodeBase(long codeBase) {
253         this.codeBase=codeBase;
254     }
255
256     private static class Context {
257         private List JavaDoc annotations=new ArrayList JavaDoc();
258         private WaiverSet waiverSet;
259     }
260     
261     private static ThreadLocal JavaDoc contextMap=new ThreadLocal JavaDoc() {
262         protected Object JavaDoc initialValue() {
263             return new HashMap JavaDoc();
264         }
265     };
266     
267     private static int counter;
268     
269     private Integer JavaDoc id;
270     
271     {
272         synchronized (this.getClass()) {
273             id=new Integer JavaDoc(counter++);
274         }
275     }
276     
277     private Context getContext() {
278         Map JavaDoc map = (Map JavaDoc) contextMap.get();
279         Context ret = (Context) map.get(id);
280         if (ret==null) {
281             ret=new Context();
282             map.put(id, ret);
283         }
284         return ret;
285     }
286     
287     private Date JavaDoc date=new Date JavaDoc();
288     
289     public void addAnnotation(Annotation annotation) {
290         getContext().annotations.add(annotation);
291     }
292
293     public Collection JavaDoc getAnnotations() {
294         return Collections.unmodifiableCollection(getContext().annotations);
295     }
296
297     public Date JavaDoc getDate() {
298         return date=new Date JavaDoc();
299     }
300
301     public int getWaivedViolationsNumber() {
302         return waivedViolationsNumber;
303     }
304
305     public String JavaDoc getDPMO() {
306         if (reviews==0) {
307             return "Not available, no reviews";
308         } else {
309             return String.valueOf((int) (1000000*violationLevel/reviews)) + (warnings.isEmpty() ? "" : " (not accurate because of warnings)");
310         }
311     }
312
313     public String JavaDoc getSigma() {
314         double p=1.0-violationLevel/reviews;
315         if (reviews==0) {
316             return "No results";
317         } else if (p<=0) {
318             return "Full incompliance";
319         } else if (p>=1) {
320             return "Full compliance";
321         } else {
322             return MessageFormat.format("{0,number,#.###}", new Object JavaDoc[] {new Double JavaDoc(normsinv(p)+1.5)}) + (warnings.isEmpty() ? "" : " (not accurate because of warnings)");
323         }
324     }
325     
326     public static double normsinv(double probability) {
327         // Coefficients in rational approximations
328
double[] a = {-3.969683028665376e+01,
329                 2.209460984245205e+02,
330                 -2.759285104469687e+02,
331                 1.383577518672690e+02,
332                 -3.066479806614716e+01,
333                 2.506628277459239e+00};
334
335         double[] b = {-5.447609879822406e+01,
336                 1.615858368580409e+02,
337                 -1.556989798598866e+02,
338                 6.680131188771972e+01,
339                 -1.328068155288572e+01};
340
341         double[] c = {-7.784894002430293e-03,
342                 -3.223964580411365e-01,
343                 -2.400758277161838e+00,
344                 -2.549732539343734e+00,
345                 4.374664141464968e+00,
346                 2.938163982698783e+00};
347
348         double[] d = {7.784695709041462e-03,
349                 3.224671290700398e-01,
350                 2.445134137142996e+00,
351                 3.754408661907416e+00};
352
353         // Define break-points.
354
double plow = 0.02425;
355         double phigh = 1 - plow;
356
357         // Rational approximation for lower region:
358
if ( probability < plow ) {
359                  double q = Math.sqrt(-2*Math.log(probability));
360                  return (((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
361                                                  ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
362         }
363
364         // Rational approximation for upper region:
365
if ( phigh < probability ) {
366                  double q = Math.sqrt(-2*Math.log(1-probability));
367                  return -(((((c[0]*q+c[1])*q+c[2])*q+c[3])*q+c[4])*q+c[5]) /
368                                                         ((((d[0]*q+d[1])*q+d[2])*q+d[3])*q+1);
369         }
370
371         // Rational approximation for central region:
372
double q = probability - 0.5;
373         double r = q*q;
374         return (((((a[0]*r+a[1])*r+a[2])*r+a[3])*r+a[4])*r+a[5])*q /
375                                  (((((b[0]*r+b[1])*r+b[2])*r+b[3])*r+b[4])*r+1);
376 }
377
378     public boolean hasWarnings() {
379         return !warnings.isEmpty();
380     }
381     
382     public WaiverSet getWaiverSet() {
383         return getContext().waiverSet;
384     }
385
386     public void commit() {
387         // Does nothing because Simple results are not persistent.
388
}
389
390     public boolean isNew() {
391         return true;
392     }
393
394     public BasicResults getBaseLine() {
395         return null;
396     }
397 }
398
Popular Tags