/* * Created on May 23, 2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package fr.lifl.stc.stan.tools; //import java.io.FileNotFoundException; //import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import fr.lifl.stc.stan.link.TableLink; import fr.lifl.stc.stan.samples.flow.FlowPartialSignature; import fr.lifl.stc.stan.samples.flow.FlowPartialSignatureData; import fr.lifl.stc.stan.signature.PartialSignature; import fr.lifl.stc.stan.signature.Signature; import fr.lifl.stc.stan.signature.SignatureDictionary; import gnu.getopt.Getopt; import gnu.getopt.LongOpt; /** * @author dorina * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class Stats { public static final int NO_DECIMALS = 2; String pack; String annotation; /** * */ public Stats(String annotation, String pack) { this.annotation = annotation; this.pack = pack; } private void stats(SignatureDictionary dict) { String index[] = dict.getIndex(); statsBranch(index, dict); /* System.out.println("\n --- Statistics for exact signatures --- \n"); statsBranch(dict.getExactSignatures()); System.out.println("\n --- Statistics for global signatures --- \n"); statsBranch(dict.getGlobalSignatures()); */ } //private void statsBranch(HashMap h) { private void statsBranch(String[] h, SignatureDictionary dict) { System.out.println("no signatures in dictionary = "+h.length); int noSign = 0; int totalSize = 0; int totalNotNull = 0; float notNullPerSign = 0; int totalNoLines = 0; int maxSizeLine = 0; HashMap linksPerLine = new HashMap(); Hashtable methPerLinks = new Hashtable(); for(int kk = 0; kk < h.length; kk++){ String key = h[kk]; if(key.indexOf(pack)<0) continue; noSign++; //Signature sign = (Signature)h.get(key); Signature sign = dict.findSignature(key, true); Collection c = sign.getPartialSignatures(); Iterator it = c.iterator(); while (it.hasNext()) { PartialSignature element = it.next(); if(element.getClassId().indexOf(annotation) != -1) { //element.display(System.out); //PartialSignature FlowPartialSignature ps = (FlowPartialSignature)sign.getPartialSignature("fr.lifl.rd2p.jits.tools.ea.samples.flow.FlowAnnotation"); TableLink data = ((FlowPartialSignatureData)(ps.getComponent())).getData(); if(data.getSizeL() > 15) { System.out.println(key+" has dimension = "+data.getSizeL() ); } totalNoLines += data.getSizeL(); totalSize += data.getSizeL()*data.getSizeC(); totalNotNull += data.getLinksNotNull(); notNullPerSign += ((float)data.getLinksNotNull()*100)/(float)(data.getSizeL()*data.getSizeC()); //add to hash of methods per signature Integer nol= new Integer(data.getLinksNotNull()); if(methPerLinks.containsKey(nol)) { int xx = methPerLinks.get(nol).intValue(); methPerLinks.put(nol, new Integer(xx+1)); } else methPerLinks.put(nol, new Integer(1)); if(maxSizeLine < data.getSizeC()) maxSizeLine = data.getSizeC(); StatsForSize s = null; if(linksPerLine.containsKey(""+data.getSizeC())){ s = linksPerLine.get(""+data.getSizeC()); } else { s = new StatsForSize(data.getSizeC()); linksPerLine.put(""+data.getSizeC(), s); } s.incNoMeth(); for(int i = 0; i < data.getSizeL(); i++) { int x = data.getLinksNotNull(i); s.inc(x, 1); } } } //System.out.println(key); } System.out.println("no of analyzed signatures = "+noSign); System.out.println("Total Size: "+totalSize+" links not null: "+totalNotNull+" ("+floatToString(totalNotNull*100/(float)totalSize,2)+"%)"); System.out.println("Links not null per method = "+totalNotNull/(float)h.length); System.out.println("Links not null per method (%) = "+notNullPerSign/h.length); System.out.println("Number of lines = "+totalNoLines); for(int j = 0; j <= maxSizeLine; j++) { System.out.print("\t"+j); } System.out.println(""); for(int j = 0; j <= maxSizeLine; j++) { System.out.print("\t-----"); } System.out.println(""); for(int j = 0; j <= maxSizeLine; j++) { if(linksPerLine.containsKey(""+j)){ StatsForSize s = linksPerLine.get(""+j); System.out.println(s); } } for(int j = 0; j <= maxSizeLine; j++) { System.out.print("\t-----"); } System.out.println(""); int []l = new int[maxSizeLine+1]; for(int j = 0; j < l.length; j++) { l[j] = 0; for(int i = 4; i <= maxSizeLine; i++) { if(linksPerLine.containsKey(""+i)){ StatsForSize s = linksPerLine.get(""+i); l[j] += s.get(j); } } System.out.print("\t"+l[j]+"("+floatToString(l[j]*100/(float)totalNoLines, NO_DECIMALS)+")"); } System.out.println("\n\n\tNoMeth\tNoLines"); for(int j = 0; j <= maxSizeLine; j++) { if(linksPerLine.containsKey(""+j)){ StatsForSize s = linksPerLine.get(""+j); System.out.println(s.getSize()+"\t"+s.getNoMeth()+"("+floatToString(s.getNoMeth()*100/(float)h.length, NO_DECIMALS)+")\t"+ s.getNoLines()+"("+floatToString(s.getNoLines()*100/(float)totalNoLines , NO_DECIMALS)+")\t" +"\t"); } } System.out.println("\n\n Methods per number of links:\n"); for(Iterator iii = methPerLinks.keySet().iterator(); iii.hasNext(); ) { Integer next = iii.next(); int value = methPerLinks.get(next).intValue(); System.out.println(""+next+"\t"+value); } } private static void printHelp() { System.out.println("-h/--help : print this help"); System.out.println("-d/--dictionary : use as dictionary"); System.out.println("-a/--annotation : stats for annotation"); System.out.println("-p/--package : stats only for classes in package"); } public static void main(String[] args) { int c; // String arg; SignatureDictionary dict = null; String pack = ""; String annot = ""; LongOpt[] longopts = new LongOpt[2]; longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); longopts[1] = new LongOpt("dictionary", LongOpt.REQUIRED_ARGUMENT, null, 'd'); longopts[1] = new LongOpt("annotation", LongOpt.REQUIRED_ARGUMENT, null, 'a'); longopts[1] = new LongOpt("package", LongOpt.NO_ARGUMENT, null, 'p'); /* longopts[2] = new LongOpt("method", LongOpt.REQUIRED_ARGUMENT, null, 'm'); longopts[3] = new LongOpt("annotation", LongOpt.REQUIRED_ARGUMENT, null, 'a'); longopts[4] = new LongOpt("value", LongOpt.REQUIRED_ARGUMENT, null, 'v'); */ Getopt g = new Getopt("Stats", args, "-:hd:a:p:W;", longopts); try { while ((c = g.getopt()) != -1) switch (c) { case 'h': printHelp(); System.exit(0); break; case 'd': String dictName = g.getOptarg(); //System.out.println("dict name = "+dictName); dict = new SignatureDictionary(dictName); break; case 'p': pack = g.getOptarg(); System.out.println("pack name = "+pack); break; case 'a': annot = g.getOptarg(); System.out.println("annot name = "+annot); break; //case 'i': } if (dict == null) { System.err.println("You must provide a dictionary"); System.exit(1); } Stats stats = new Stats(annot, pack); stats.stats(dict); } catch (ClassNotFoundException e) { System.err.println("Error: Dictionary contains unknown classes"); System.exit(1); } /*catch (FileNotFoundException e) { System.err.println("Error: Dictionary file not found"); System.exit(1); } catch (IOException e) { System.err.println("Error: IO error"); System.exit(1); }*/ } public static String floatToString(float f, int noDecimals) { String s = ""+f; int idx = s.indexOf('.'); if(idx >=0 ){ if(idx+noDecimals < s.length()) s = s.substring(0, idx+noDecimals); } return s; } class StatsForSize { int size; int []noLinks; int noMeth; int noLines; //public static StatsForSize st = new StatsForSize(20); public StatsForSize(int s){ size = s; noMeth = 0; noLines = 0; noLinks = new int[s+1]; for(int i = 0 ; i < noLinks.length; i++) noLinks[i] = 0; } int getNoMeth() { return this.noMeth; } int getNoLines() { return this.noLines; } int getSize() { return this.size; } void inc(int index, int count){ if(index