Skip to content

Commit

Permalink
fix bug in VariableVisitor
Browse files Browse the repository at this point in the history
refactoring im LogWriter
  • Loading branch information
simonAllier committed Nov 20, 2013
1 parent 2f7615c commit bd255b7
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 70 deletions.
20 changes: 17 additions & 3 deletions src/main/java/fr/inria/diversify/DiversifyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ protected void initAndRunBuilder(Builder builder) throws Exception {

//TODO refactor
if (DiversifyProperties.getProperty("nbRun").equals("all")) {
if(DiversifyProperties.getProperty("transformation.directory") != null)
builder.run(1000);
if(DiversifyProperties.getProperty("transformation.directory") != null) {
TransformationParser tf = new TransformationParser(codeFragments);
List<ITransformation> transformations = tf.parseDir(DiversifyProperties.getProperty("transformation.directory"));
Set<ITransformation> set = new HashSet<ITransformation>(transformations);
Log.debug("apply {} transformation", set.size());
builder.run(set);
}
Util util = new Util(codeFragments);
if (DiversifyProperties.getProperty("transformation.type").equals("replace"))
builder.run(util.getAllReplace());
Expand All @@ -132,7 +137,16 @@ protected void initAndRunBuilder(Builder builder) throws Exception {
} else if (DiversifyProperties.getProperty("transformation.type").equals("stupid")) {
int n = Integer.parseInt(DiversifyProperties.getProperty("nbRun"));
Util util = new Util(codeFragments);
builder.run(util.getStupidTransformation(n, (ASTTransformationQuery)query));
int count = 0;
for(int i = 0; i < 100; i++) {
builder.run(util.getStupidTransformation(n, (ASTTransformationQuery)query));
int size = builder.getTransformations().size();
builder.getTransformations().clear();
count = size+ count;
Log.debug("{}*7 = {}",n,size);
}
Log.debug("mean: {}*7 = {}",n,((double)count/100.0));
// builder.run(util.getStupidTransformation(n, (ASTTransformationQuery)query));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public boolean equals(Object obj) {

protected Set<String> inputContextToString() {
//todo set ou list ?????
//axis 2012 icse 2013 gorla
Set<String> set = new HashSet<String>();
for (CtVariableReference<?> var : localVariableReferences)
set.add(var.getType().toString());
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/fr/inria/diversify/codeFragment/VariableVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtVariableAccess;
import spoon.reflect.declaration.ModifierKind;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtLocalVariableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.reference.CtVariableReference;
Expand Down Expand Up @@ -45,12 +46,19 @@ public <T> void visitCtLocalVariableReference(
super.visitCtLocalVariableReference(reference);
}

public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) {
if(!(fieldAccess.getVariable().getSimpleName() == "super"))
if(!(fieldAccess.getVariable().isStatic() && fieldAccess.getVariable().getModifiers().contains(ModifierKind.PUBLIC)))
fieldReferences.add(fieldAccess);
super.visitCtVariableAccess(fieldAccess);
}
// public <T> void visitCtFieldAccess(CtFieldAccess<T> fieldAccess) {
// if(!(fieldAccess.getVariable().getSimpleName() == "super"))
// if(!(fieldAccess.getVariable().isStatic() && fieldAccess.getVariable().getModifiers().contains(ModifierKind.PUBLIC)))
// fieldReferences.add(fieldAccess);
// super.visitCtVariableAccess(fieldAccess);
// }

public <T> void visitCtFieldReference(CtFieldReference<T> reference) {
if(!(reference.getSimpleName() == "super"))
if(!(reference.isStatic() && reference.getModifiers().contains(ModifierKind.PUBLIC)))
localVariableReferences.add(reference);
super.visitCtFieldReference(reference);
}

public <T> void visitCtInvocation(CtInvocation<T> invocation) {
if(invocation.getTarget() == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected String prepare(String dirSource, String dirTarget, String newPomFile)
}

protected Integer runTest(String directory) throws InterruptedException, CompileException {
RunMaven rt = new RunMaven(directory, new String[]{"test"}, timeOut,clojureTest);
RunMaven rt = new RunMaven(directory, new String[]{"compile"}, timeOut,clojureTest);
rt.start();
rt.join(1000*timeOut);

Expand Down Expand Up @@ -192,4 +192,6 @@ public void setNewPomFile(String pom) {
public void setTransformationQuery(ITransformationQuery transQuery) {
this.transQuery = transQuery;
}

public List<ITransformation> getTransformations() {return transformations;}
}
55 changes: 49 additions & 6 deletions src/main/java/fr/inria/diversify/sosie/Logger/LogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ public class LogWriter {
static private Map<Thread, FileWriter> fileWriters;
static private String separator = ":;:";
private static String currentTestSignature;
private static Map<String,String> idMap;


protected synchronized static FileWriter init(Thread thread) throws IOException {
if(fileWriters == null) {
if(dir == null)
initDir();
try {
idMap = loadIdMap(dir+"/id");
} catch (Exception e) {
idMap = new HashMap<String, String>();
}

fileWriters = new HashMap<Thread, FileWriter>();
ShutdownHookLog shutdownHook = new ShutdownHookLog();
Runtime.getRuntime().addShutdownHook(shutdownHook);
Expand Down Expand Up @@ -63,17 +70,17 @@ public static void writeLog(int id,Thread thread, String className, String metho
}
try {
fileWriter.append("$$$\n");
fileWriter.append(id+"");
fileWriter.append(separator);
fileWriter.append(className);
// fileWriter.append(id+"");
// fileWriter.append(separator);
fileWriter.append(idFor(className));
fileWriter.append(separator);
fileWriter.append(methodSignature);
fileWriter.append(idFor(methodSignature));

for (int i = 0; i < var.length/2; i = i + 2) {
fileWriter.append(separator);
fileWriter.append(var[i].toString());
fileWriter.append(idFor(var[i].toString()));
fileWriter.append(separator);
fileWriter.append(var[i+1]+"");
fileWriter.append(var[i+1].toString());
}
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -157,5 +164,41 @@ public static void close() {
} catch (IOException e) {
e.printStackTrace();
}
try {
writeIdFile();
} catch (IOException e) {
e.printStackTrace();
}
}

protected static String idFor(String string) {
if(!idMap.containsKey(string))
idMap.put(string,idMap.size()+"");

return idMap.get(string);
}

protected static void writeIdFile() throws IOException {
FileWriter fw = new FileWriter(dir.getAbsolutePath()+"/id");

for(String s : idMap.keySet())
fw.write(idMap.get(s)+ " " +s+"\n");

fw.close();
}

protected static Map<String,String> loadIdMap(String file) throws IOException {
Map<String,String> map = new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.readLine();
String line = reader.readLine();

while (line != null) {

String[] tmp = line.split(" ");
map.put(tmp[1],tmp[0]);
line = reader.readLine();
}
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public boolean isToBeProcessed(CtStatement candidate) {
|| CtLoop.class.isAssignableFrom(candidate.getClass())
|| CtThrow.class.isAssignableFrom(candidate.getClass())
;

}

public boolean hasStaticParent(CtElement el) {
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/fr/inria/diversify/sosie/compare/CompareLogMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* User: Simon
Expand Down Expand Up @@ -66,6 +68,12 @@ protected void same() throws IOException {
protected void diff() throws Exception {
List<ITransformation> trans = new ArrayList<ITransformation>();
String startPointString = DiversifyProperties.getProperty("startPoint");
int testMax = 0;
int testMin = 100;
int test = 0;
int varMax = 0;
int varMin = 100;
int var = 0;
int count = 0;
int i =0;
for(File f : (new File(dirSosie).listFiles())) {
Expand All @@ -88,9 +96,22 @@ protected void diff() throws Exception {
Log.info(diff.report());

if(!diff.sameVar()) {

trans.add(parser.parseUniqueTransformation(startPoint));
diff.toDot(DiversifyProperties.getProperty("result")+"cp_"+f.getName()+".dot");
count++;

Set<String> set = new HashSet<String>();
for(VariableDiff d : diff.getAllVariableDiff()) {
set.add(d.getVarName());
}
var = var + set.size();
varMax = Math.max(varMax,set.size());
varMin = Math.min(varMin,set.size());
Log.info("nb diff:{}",set.size());
test = test +diff.getCount();
testMax = Math.max(testMax,diff.getCount());
testMin = Math.min(testMin,diff.getCount());
Log.info("i: "+count);
}
}
Expand All @@ -100,7 +121,8 @@ protected void diff() throws Exception {
Log.error("error",e);
e.printStackTrace();
}

Log.info("nb diff:{}, min: {}, max: {}, {} ",count, testMin,testMax, (double)test/(double)count);
Log.info("var min: {}, var max: {}, {} ", varMin,varMax, (double)var/(double)count);
TransformationsWriter write = new TransformationsWriter(trans,DiversifyProperties.getProperty("result") + "/sosie/sosie");
write.writeAllTransformation(null);
}
Expand All @@ -109,6 +131,9 @@ protected void diff() throws Exception {
protected void diffException() throws Exception {
List<ITransformation> trans = new ArrayList<ITransformation>();
String startPointString = DiversifyProperties.getProperty("startPoint");
int testMax = 0;
int testMin = 100;
int test = 0;
int count = 0;
int i =0;
for(File f : (new File(dirSosie).listFiles())) {
Expand All @@ -133,6 +158,9 @@ protected void diffException() throws Exception {
trans.add(parser.parseUniqueTransformation(startPoint));
diff.toDotCatch(DiversifyProperties.getProperty("result")+"exception_"+f.getName() + ".dot");
count++;
test = test +diff.getCount();
testMax = Math.max(testMax,diff.getCount());
testMin = Math.min(testMin,diff.getCount());
Log.info("i: "+count);
}
}
Expand All @@ -143,7 +171,7 @@ protected void diffException() throws Exception {
e.printStackTrace();
}
}

Log.info("nb diff:{}, min: {}, max: {}, {} ",count, testMin,testMax, (double)test/(double)count);
Log.info("max exception: "+ PointSequence.getMaxSizeException());

TransformationsWriter write = new TransformationsWriter(trans,DiversifyProperties.getProperty("result") + "/sosie/sosieE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* User: Simon
Expand Down Expand Up @@ -81,14 +83,15 @@ public Diff findDiffException() throws IOException {
protected List<PointSequence> loadPointSequence(String dir, boolean recursive) {
List<PointSequence> list = new ArrayList<PointSequence>();
File file = new File(dir);

Log.debug("load trace in directory: {}",dir);
for (File f : file.listFiles()) {
if(recursive && f.isDirectory())
list.addAll(loadPointSequence(f.getAbsolutePath(), recursive));
else {
try {
PointSequence ps = new PointSequence();
ps.parseFile(f);
ps.parseFile(f,null);
list.add(ps);
} catch (Exception e) {
// Log.warn("error during parse file {}",e,f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import org.json.JSONException;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* User: Simon
Expand Down Expand Up @@ -142,26 +139,45 @@ protected Set<VariableDiff> loadVarToExclude(String fileExcludeVar) throws IOExc
return varToExclude;
}

protected List<PointSequence> loadPointSequence(String dir, boolean recursive) {
protected List<PointSequence> loadPointSequence(String dir, boolean recursive){
List<PointSequence> list = new ArrayList<PointSequence>();
File file = new File(dir);
Map<String, String> idMap = null;
try {
idMap = loadIdMap(dir + "/id");
} catch (IOException e) {
e.printStackTrace();
}
Log.debug("load trace in directory: {}",dir);
for (File f : file.listFiles()) {
if(recursive && f.isDirectory())
list.addAll(loadPointSequence(f.getAbsolutePath(),recursive));
else {
try {
PointSequence ps = new PointSequence();
ps.parseFile(f);
ps.parseFile(f,idMap);
list.add(ps);
} catch (Exception e) {
// Log.warn("error during parse file {}",e,f);
}
}
}
return list;
}

protected Map<String,String> loadIdMap(String file) throws IOException {
Map<String,String> map = new HashMap<String, String>();
BufferedReader reader = new BufferedReader(new FileReader(file));
reader.readLine();
String line = reader.readLine();

while (line != null) {
line = reader.readLine();
String[] tmp = line.split(" ");
map.put(tmp[0],tmp[1]);
}
return map;
}

public void setSyncroRange(int syncroRange) {
this.syncroRange = syncroRange;
}
Expand Down
Loading

0 comments on commit bd255b7

Please sign in to comment.