Commit 01bb5832 authored by ISWB Prasetya's avatar ISWB Prasetya
Browse files

start adding data analytics to G2

parent 85ebce29
......@@ -31,6 +31,8 @@ import Sequenic.T3.CONSTANTS;
import Sequenic.T3.ImplementationMap;
import Sequenic.T3.Pool;
import Sequenic.T3.SimpleClassLoader;
import Sequenic.T3.Info.Analytics.Analytics;
import Sequenic.T3.Info.Analytics.DataTable;
import Sequenic.T3.Sequence.Datatype.*;
/**
......@@ -229,16 +231,41 @@ public class G2 {
t3log.warning("Prefix generation for " + g2sg.scope.CUT.getName() + " reaches stagnation. NO further prefixes will be generated.");
}
}
if (g2sg.currentPrefixes != null) {
// generating some analytics data
String savedir = "/Users/iswbprasetya/tmp/t3" ;
DataTable dt_prefixcov = new DataTable(1,DataTable.DOUBLE) ;
dt_prefixcov.addHeaderRow("cov") ;
dt_prefixcov.addRow(g2sg.currentPrefixes.coverage) ;
String dt_prefixcov_fname = savedir + "/" + g2sg.scope.CUT.getName()
+ "_prefixcov_"
+ prefixRefinementCount + ".txt" ;
try { dt_prefixcov.save(dt_prefixcov_fname);}
catch(Exception e) { }
t3log.info("=== Generating/refining prefixes of "
+ g2sg.scope.CUT.getName()
+ ". Generation: " + prefixRefinementCount
+ ", adding: " + numOfAddedPrefixes
+ ", tot-size: " + g2sg.getPrefixes().suite.size()
+ ", cov: " + g2sg.currentPrefixes.coverage
+ ", #tobjs: " + g2sg.currentPrefixes.tobjs.size()
);
DataTable dt_tobj = new DataTable(2,DataTable.DOUBLE) ;
dt_tobj.addHeaderRow("depth","diversitySum");
for(ObjStructure o : g2sg.currentPrefixes.tobjs) {
if (o == null) dt_tobj.addRow(0d,0d) ;
else dt_tobj.addRow((double) o.depth,o.diversitySum);
}
String dt_tobj_fname = savedir + "/" + g2sg.scope.CUT.getName()
+ "_tobj_diversity_"
+ prefixRefinementCount + ".txt" ;
try { dt_tobj.save(dt_tobj_fname);}
catch(Exception e) { }
t3log.info("=== Generating/refining prefixes of "
+ g2sg.scope.CUT.getName()
+ ". Generation: " + prefixRefinementCount
+ ", adding: " + numOfAddedPrefixes
+ ", tot-size: " + g2sg.getPrefixes().suite.size()
+ ", cov: " + g2sg.currentPrefixes.coverage
+ ", #tobjs: " + g2sg.currentPrefixes.tobjs.size()
);
}
}
SingleTarget target = worklist.getNext(remaining/budget) ;
target.refine() ;
......
......@@ -21,13 +21,14 @@ import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import Sequenic.T3.Reflection.Reflection;
import Sequenic.T3.Sequence.Datatype.*;
import Sequenic.T3.utils.Show;
import Sequenic.T3.Pool;
import Sequenic.T3.Examples.SimpleIntSortedList;
import Sequenic.T3.Examples.Friends.*;
......@@ -41,6 +42,9 @@ import Sequenic.T3.Examples.Friends.*;
public class ObjStructure {
Node root = new Node() ;
// for statistics:
public int depth ;
public double diversitySum ;
static public class Node {
List<Arrow> next ;
......@@ -77,6 +81,16 @@ public class ObjStructure {
for (Arrow a : next) a.target.normalize(seenValues) ;
}
int getDepth() {
if (next.isEmpty()) return 1 ;
return 1 + next.stream().map(arrow -> arrow.target.getDepth()).max(Comparator.naturalOrder()).get() ;
}
double diversityMetric() {
if (next.isEmpty()) return 0d ;
return next.stream().collect(Collectors.summingDouble(arrow -> arrow.target.diversityMetric())) ;
}
void show(StringBuilder s, int indent) {
s.append(" -->") ;
int k = 0 ;
......@@ -110,6 +124,9 @@ public class ObjStructure {
return 63 - x.numberOfLeadingZeros(x) ;
}
int getDepth() { return 0 ; }
double diversityMetric() { return strDiversity(value) ; }
void normalize(List<String> seenValues) {
if (value == null) return ;
if (value.equals("") || value.equals("0") || value.equals("1")
......@@ -167,6 +184,8 @@ public class ObjStructure {
super() ;
List visited = new LinkedList() ;
constructWorker(maxdepth,o,root,"root",visited) ;
depth = root.getDepth() ;
diversitySum = root.diversityMetric() ;
}
private static void constructWorker(int depth, Object o, Node parent, String fname, List visited) {
......@@ -230,8 +249,6 @@ public class ObjStructure {
return ;
}
public boolean equals(Object o) {
if (o==null) return false ;
if (!(o instanceof ObjStructure)) return false ;
......@@ -305,6 +322,33 @@ public class ObjStructure {
catch(Exception e) { return null ; }
}
/**
* Get a metric of a string. It is 1 + the sum of the diversity of its elements.
*/
static Double strDiversity(String s) {
if (s==null) return 0d ;
char[] s_ = s.toCharArray() ;
int N = s_.length ;
if(N == 0) return 0d ;
int minchar = (int) s_[0] ;
int maxchar = (int) s_[0] ;
for (int k = 1 ; k<N ; k++) {
int c = (int) s_[k] ;
if (c<minchar) minchar = c ;
if (c>maxchar) maxchar = c ;
}
int delta = maxchar - minchar ;
if (delta==0) return 1d ;
int r = 0 ;
for (int k = 0 ; k<N ; k++) {
int c = (int) s_[k] ;
try { r += c - minchar ; }
catch(Throwable e) { }
}
return 1d + ((double) r) / ((double) delta) ;
}
static public void main(String[] args) {
ObjStructure t ;
ObjStructure u ;
......@@ -323,6 +367,16 @@ public class ObjStructure {
System.out.println(t.toString());
t.normalize();
System.out.println(t.toString());
SimpleIntSortedList z = new SimpleIntSortedList() ;
z.insert(1);
z.insert(100);
z.insert(2);
ObjStructure zo = new ObjStructure(z,4) ;
System.out.println(zo.depth) ;
System.out.println(zo.diversitySum) ;
System.out.println(zo.toString());
}
......
package Sequenic.T3.DerivativeSuiteGens.Gen2;
public class Test_G2_SimpleSortedList {
static G2Config config() {
G2Config config = new G2Config() ;
config.regressionMode = true ;
config.CUTrootDir = "/Users/iswbprasetya/eclipseWorkspace/t3Workspace/t3/bin" ;
config.dirToSaveSuites = "/Users/iswbprasetya/tmp/t3" ;
config.dirOfStaticInfo = "/Users/iswbprasetya/tmp/t3" ;
config.generateJunitForEachSuite = false ;
config.maxPrefixLength = 6 ;
config.maxSuffixLength = 1 ;
config.numberOfPrefixes = 10 ;
// config.refinementHeuristic="evo" ;
return config ;
}
static void genWithG2() throws Exception {
G2.generateSuites("Sequenic.T3.Examples.SimpleIntSortedList",config(),120000) ;
}
public static void main(String[] args) throws Exception {
genWithG2() ;
}
}
package Sequenic.T3.Info.Analytics;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
public class Analytics {
static Map<String,DataTable> mydata = new HashMap<String,DataTable>() ;
public static void registerDataTable(String name, boolean isInteger, String... columnNames) {
DataTable dt = new DataTable(columnNames.length,isInteger) ;
dt.addHeaderRow(columnNames) ;
mydata.put(name,dt) ;
}
public static void removeDataTable(String name) {
mydata.remove(name) ;
}
public static DataTable getDataTable(String name) {
return mydata.get(name) ;
}
public static void addRow(String tableName, Integer... values) {
DataTable dt = mydata.get(tableName) ;
if (dt != null) dt.addRow(values) ;
}
public static void addRow(String tableName, Double... values) {
DataTable dt = mydata.get(tableName) ;
if (dt != null) dt.addRow(values) ;
}
public static void clear() {
mydata.clear();
}
public static void save(String dirpath, String prefix, String suffix) throws IOException {
for (Entry<String,DataTable> E : mydata.entrySet()) {
String fname = dirpath
+ "/" + prefix + "_"
+ E.getKey() + "_"
+ suffix + ".txt" ;
E.getValue().save(fname);
}
}
public static void main(String[] args) throws IOException {
Analytics.clear();
Analytics.registerDataTable("temperature",DataTable.DOUBLE,"temp","day");
Analytics.registerDataTable("fun",DataTable.DOUBLE,"happy","day");
Analytics.addRow("temperature",30d,1d) ;
Analytics.addRow("temperature",31d,2d) ;
Analytics.addRow("fun",99d,1d) ;
Analytics.addRow("fun",0d,2d) ;
Analytics.save("/Users/iswbprasetya/tmp/t3","mydaya","xxx") ;
}
}
package Sequenic.T3.Info.Analytics;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.* ;
/**
*
* @author iswbprasetya
*
*/
public class DataTable {
public static final boolean INTEGER = true ;
public static final boolean DOUBLE = false ;
int dimension ;
public String separator = ":" ;
boolean isInteger = false ;
String[] headerrow = null ;
List<Number[]> rows = new LinkedList<Number[]>() ; // should contain either doubles or integers
public DataTable(int dimension, boolean isInteger) {
this.dimension = dimension ;
this.isInteger = isInteger ;
}
public DataTable addHeaderRow(String... columnNames) {
if (columnNames.length != dimension) throw new IllegalArgumentException() ;
headerrow = columnNames ;
return this ;
}
public DataTable addRow(Integer... values) {
if (!isInteger) throw new IllegalArgumentException() ;
if (values.length != dimension) throw new IllegalArgumentException() ;
rows.add(values) ;
return this ;
}
public DataTable addRow(Double... values) {
if (isInteger) throw new IllegalArgumentException() ;
if (values.length != dimension) throw new IllegalArgumentException() ;
rows.add(values) ;
return this ;
}
public String toString() {
StringBuffer s = new StringBuffer() ;
if(headerrow != null) {
for(int k=0; k<dimension; k++) {
if (k>0) s.append(separator) ;
s.append(headerrow[k]) ;
}
s.append("\n") ;
}
for (Number[] R : rows) {
for(int k=0; k<dimension; k++) {
if (k>0) s.append(separator) ;
s.append(R[k].toString()) ;
}
s.append("\n") ;
}
return s.toString() ;
}
/**
* Save the data to a text file.
*/
public void save(String filename) throws IOException {
Path path = Paths.get(filename);
Files.write(path, toString().getBytes());
}
public static void main(String[] args) throws IOException {
DataTable dt = new DataTable(2,DataTable.DOUBLE) ;
dt.addHeaderRow("x","y") ;
dt.addRow(23d,40d) ;
dt.addRow(0.01d,9.000000001d) ;
System.out.println(dt.toString()) ;
dt.save("/Users/iswbprasetya/tmp/t3/somedata.txt") ;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment