1 23 24 package org.hammurapi.results.simple; 25 26 import java.io.Serializable ; 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.Date ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 import java.util.Set ; 37 import java.util.TreeMap ; 38 import java.util.TreeSet ; 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 59 public class SimpleAggregatedResults implements AggregatedResults, Serializable { 60 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 metrics=new HashMap (); 70 private Set warnings=new TreeSet (); 71 72 public SimpleAggregatedResults(WaiverSet waiverSet) { 73 getContext().waiverSet=waiverSet; 74 } 75 76 80 protected Map severitySummary=new TreeMap (); 81 82 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 severity = violation.getDescriptor().getSeverity(); 91 Integer s=severity; 92 Map rsm=(Map ) severitySummary.get(s); 93 if (rsm==null) { 94 rsm=new TreeMap (); 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 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 getSeveritySummary() { 120 return Collections.unmodifiableMap(severitySummary); 121 } 122 123 public Number getMaxSeverity() { 124 Integer ret=null; 125 Iterator it=severitySummary.keySet().iterator(); 126 while (it.hasNext()) { 127 Integer severity=(Integer ) it.next(); 128 if (ret==null || severity.intValue()<ret.intValue()) { 129 ret=severity; 130 } 131 } 132 return ret; 133 } 134 135 public Collection 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 name, double value) { 144 class SourceAwareMetric extends SimpleMetric { 145 SourceAwareMetric(String name) { 146 super(name); 147 } 148 149 SourceAwareMetric(String 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 Map getMetrics() { 195 return Collections.unmodifiableMap(metrics); 196 } 197 198 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 it=agregee.getSeveritySummary().entrySet().iterator(); 211 while (it.hasNext()) { 212 Map.Entry entry=(Map.Entry ) it.next(); 213 Map ss=(Map ) severitySummary.get(entry.getKey()); 214 if (ss==null) { 215 ss=new TreeMap (); 216 severitySummary.put(entry.getKey(), ss); 217 } 218 219 Iterator rsit=((Map ) 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 entry=(Map.Entry ) it.next(); 235 Metric metric=(Metric) metrics.get(entry.getKey()); 236 if (metric==null) { 237 metric=new SimpleMetric((String ) entry.getKey()); 238 metrics.put(entry.getKey(), metric); 239 } 240 metric.add((SimpleMetric) entry.getValue()); 241 } 242 } 243 244 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 annotations=new ArrayList (); 258 private WaiverSet waiverSet; 259 } 260 261 private static ThreadLocal contextMap=new ThreadLocal () { 262 protected Object initialValue() { 263 return new HashMap (); 264 } 265 }; 266 267 private static int counter; 268 269 private Integer id; 270 271 { 272 synchronized (this.getClass()) { 273 id=new Integer (counter++); 274 } 275 } 276 277 private Context getContext() { 278 Map map = (Map ) 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 date=new Date (); 288 289 public void addAnnotation(Annotation annotation) { 290 getContext().annotations.add(annotation); 291 } 292 293 public Collection getAnnotations() { 294 return Collections.unmodifiableCollection(getContext().annotations); 295 } 296 297 public Date getDate() { 298 return date=new Date (); 299 } 300 301 public int getWaivedViolationsNumber() { 302 return waivedViolationsNumber; 303 } 304 305 public String 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 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 [] {new Double (normsinv(p)+1.5)}) + (warnings.isEmpty() ? "" : " (not accurate because of warnings)"); 323 } 324 } 325 326 public static double normsinv(double probability) { 327 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 double plow = 0.02425; 355 double phigh = 1 - plow; 356 357 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 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 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 } 389 390 public boolean isNew() { 391 return true; 392 } 393 394 public BasicResults getBaseLine() { 395 return null; 396 } 397 } 398 | Popular Tags |