release cand. 2

This commit is contained in:
fixminer
2018-09-17 18:29:33 +02:00
parent 4a9d87974e
commit 2d12536b93
116 changed files with 650 additions and 118668 deletions
@@ -1,101 +0,0 @@
package edu.lu.uni.serval.FixPattern.utils;
import java.util.HashMap;
import java.util.Map;
public class CNodeMap {
public static Map<Integer, String> map;
static {
map = new HashMap<Integer, String>();
map.put(20100,"Left");
map.put(30100,"ActMisc");
map.put(40000,"FullType");
map.put(50000,"TypeQualifier");
map.put(60100,"BaseType");
map.put(60200,"Pointer");
map.put(60800,"EnumName");
map.put(60900,"StructUnionName");
map.put(61000,"TypeName");
map.put(70002,"SizeType");
map.put(70100,"IntType");
map.put(80001,"CChar");
map.put(80100,"Si");
map.put(90002,"UnSigned");
map.put(100003,"CInt");
map.put(200000,"ParamList");
map.put(210000,"DotsParameter");
map.put(220100,"ParameterType");
map.put(240100,"Ident");
map.put(240200,"Constant");
map.put(240400,"FunCall");
map.put(240500,"CondExpr");
map.put(240600,"Sequence");
map.put(240700,"Assignment");
map.put(240800,"Postfix");
map.put(240900,"Infix");
map.put(241000,"Unary");
map.put(241100,"Binary");
map.put(241200,"ArrayAccess");
map.put(241300,"RecordAccess");
map.put(241400,"RecordPtAccess");
map.put(241500,"SizeOfExpr");
map.put(241600,"SizeOfType");
map.put(241700,"Cast");
map.put(241900,"Constructor");
map.put(242000,"ParenExpr");
map.put(260300,"ExprStatement");
map.put(260800,"Asm");
map.put(270100,"Label");
map.put(270200,"Case");
map.put(270300,"CaseRange");
map.put(270400,"Default");
map.put(280001,"Continue");
map.put(280002,"Break");
map.put(280003,"Return");
map.put(280100,"Goto");
map.put(280200,"ReturnExpr");
map.put(290001,"None");
map.put(290100,"Some");
map.put(300100,"If");
map.put(300200,"Switch");
map.put(310100,"While");
map.put(310200,"DoWhile");
map.put(310300,"For");
map.put(310400,"MacroIteration");
map.put(330000,"Compound");
map.put(340000,"Storage");
map.put(350100,"DeclList");
map.put(350200,"MacroDecl");
map.put(360100,"InitExpr");
map.put(360200,"InitList");
map.put(360300,"InitDesignators");
map.put(370100,"DesignatorField");
map.put(370200,"DesignatorIndex");
map.put(370300,"DesignatorRange");
map.put(380000,"Definition");
map.put(400100,"Define");
map.put(400200,"Include");
map.put(400400,"OtherDirective");
map.put(410001,"DefineVar");
map.put(410002,"Undef");
map.put(410100,"DefineFunc");
map.put(420001,"DefineEmpty");
map.put(420100,"DefineExpr");
map.put(420200,"DefineStmt");
map.put(420400,"DefineDoWhileZero");
map.put(420600,"DefineInit");
map.put(440100,"IfdefDirective");
map.put(450100,"Declaration");
map.put(450200,"Definition");
map.put(450300,"CppTop");
map.put(450400,"IfdefTop");
map.put(450700,"NotParsedCorrectly");
map.put(450800,"FinalDef");
map.put(460000,"Program");
map.put(470000,"GenericList");
map.put(480000,"GenericString");
map.put(490100,"IfToken");
}
}
@@ -0,0 +1,98 @@
package edu.lu.uni.serval.FixPattern.utils;
import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.io.IOException;
import java.util.List;
/**
* Created by anilkoyuncu on 17/09/2018.
*/
public class EDiff {
private static Logger log = LoggerFactory.getLogger(EDiff.class);
public static ITree getSimpliedTree(String fn, JedisPool outerPool) {
ITree tree = null;
Jedis inner = null;
try {
inner = outerPool.getResource();
while (!inner.ping().equals("PONG")){
log.info("wait");
}
inner.select(1);
String dist2load = inner.get(fn);
inner.select(0);
String s = inner.get(dist2load);
HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s);
ITree parent = null;
ITree children =null;
TreeContext tc = new TreeContext();
tree = EDiffHelper.getASTTree(actionSet, parent, children,tc);
tree.setParent(null);
tc.validate();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (inner != null) {
inner.close();
}
}
return tree;
}
public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc){
int newType = 0;
Action action = actionSet.getAction();
if (action instanceof Update){
newType = 101;
}else if(action instanceof Insert){
newType =100;
}else if(action instanceof Move){
newType = 102;
}else if(action instanceof Delete){
newType=103;
}else{
new Exception("unknow action");
}
if(actionSet.getParent() == null){
//root
parent = tc.createTree(newType, "", null);
tc.setRoot(parent);
// parent = new Tree(newType,"");
}else{
children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent);
// children = new Tree(newType,"");
// parent.addChild(children);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getActionTree(subAction,children,null,tc);
}
}
return parent;
}
}
@@ -0,0 +1,88 @@
package edu.lu.uni.serval.FixPattern.utils;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Created by anilkoyuncu on 17/09/2018.
*/
public class EDiffHelper {
private static Logger log = LoggerFactory.getLogger(EDiffHelper.class);
/** Read the object from Base64 string. */
public static Object fromString( String s ) throws IOException,
ClassNotFoundException {
byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return o;
}
/** Write the object to a Base64 string. */
public static String toString( Serializable o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc){
int newType = 0;
String astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType);
if(keysByValue.size() != 1){
log.error("More than 1");
}
newType = keysByValue.get(0);
if(actionSet.getParent() == null){
//root
// parent = new Tree(newType,"");
parent = tc.createTree(newType, "", null);
tc.setRoot(parent);
}else{
// children = new Tree(newType,"");
// parent.addChild(children);
children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getASTTree(subAction,children,null,tc);
}
}
return parent;
}
public static <T, E> List<T> getKeysByValue(Map<T, E> map, E value) {
return map.entrySet()
.stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
}
@@ -0,0 +1,33 @@
package edu.lu.uni.serval.FixPattern.utils;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
/**
* Created by anilkoyuncu on 17/09/2018.
*/
public class PoolBuilder {
public static JedisPoolConfig getPoolConfig() {
return poolConfig;
}
static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true);
return poolConfig;
}
}
@@ -1,15 +1,11 @@
package edu.lu.uni.serval.FixPatternParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import java.io.*;
/**
* Creator of a CompilationUnit.
*
@@ -1,19 +1,6 @@
package edu.lu.uni.serval.FixPatternParser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.gumtree.GumTreeComparer;
import edu.lu.uni.serval.gumtree.regroup.ActionFilter;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalRegrouper;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
/**
* Parse fix patterns with GumTree.
@@ -33,435 +20,7 @@ public abstract class Parser implements ParserInterface {
public abstract void parseFixPatterns(File prevFile, File revFile, File diffEntryFile,String project);
protected List<HierarchicalActionSet> parseChangedSourceCodeWithGumTree(File prevFile, File revFile) {
List<HierarchicalActionSet> actionSets = new ArrayList<>();
// GumTree results
List<Action> gumTreeResults = new GumTreeComparer().compareTwoFilesWithGumTree(prevFile, revFile);
// List<Action> gumTreeResults = new GumTreeComparer().compareTwoFilesWithGumTreeForCCode(prevFile, revFile);
if (gumTreeResults != null && gumTreeResults.size() > 0) {
// Regroup GumTre results.
List<HierarchicalActionSet> allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults);
// Filter out modified actions of changing method names, method parameters, variable names and field names in declaration part.
// TODO: variable effects range, sub-actions are these kinds of modification?
actionSets.addAll(new ActionFilter().filterOutUselessActions(allActionSets));
}
return actionSets;
}
protected List<Move> getFirstAndLastMoveAction(HierarchicalActionSet gumTreeResult) {
List<Move> firstAndLastMoveActions = new ArrayList<>();
List<HierarchicalActionSet> actions = gumTreeResult.getSubActions();
if (actions.size() == 0) {
return null;
}
Move firstMoveAction = null;
Move lastMoveAction = null;
while (actions.size() > 0) {
List<HierarchicalActionSet> subActions = new ArrayList<>();
for (HierarchicalActionSet action : actions) {
subActions.addAll(action.getSubActions());
if (action.toString().startsWith("MOV")) {
if (firstMoveAction == null) {
firstMoveAction = (Move) action.getAction();
lastMoveAction = (Move) action.getAction();
} else {
int startPosition = action.getStartPosition();
int length = action.getLength();
int startPositionFirst = firstMoveAction.getPosition();
int startPositionLast = lastMoveAction.getPosition();
int lengthLast = lastMoveAction.getNode().getLength();
if (startPosition < startPositionFirst || (startPosition == startPositionFirst && length > firstMoveAction.getLength())) {
firstMoveAction = (Move) action.getAction();
}
if ((startPosition + length) > (startPositionLast + lengthLast)) {
lastMoveAction = (Move) action.getAction();
}
}
}
}
actions.clear();
actions.addAll(subActions);
}
if (firstMoveAction == null) {
return null;
}
firstAndLastMoveActions.add(firstMoveAction);
firstAndLastMoveActions.add(lastMoveAction);
return firstAndLastMoveActions;
}
protected String getSemiSourceCodeEditScripts(HierarchicalActionSet actionSet) {
// TODO Auto-generated method stub
return null;
}
protected String getAbstractIdentifiersEditScripts(HierarchicalActionSet actionSet) {
// TODO Auto-generated method stub
return null;
}
protected String getRawTokenEditScripts(HierarchicalActionSet actionSet) {
// TODO Auto-generated method stub
return null;
}
protected int getEndPosition(List<ITree> children) {
int endPosition = 0;
for (ITree child : children) {
if (child.getLabel().endsWith("Body")) {
endPosition = child.getPos() - 1;
break;
}
}
return endPosition;
}
/**
* Get the AST node based edit script of patches in terms of breadth first.
*
* @param actionSet
* @return
*/
protected String getASTEditScriptsBreadthFirst(HierarchicalActionSet actionSet) {
String editScript = "";
List<HierarchicalActionSet> actionSets = new ArrayList<>();
actionSets.add(actionSet);
while (actionSets.size() != 0) {
List<HierarchicalActionSet> subSets = new ArrayList<>();
for (HierarchicalActionSet set : actionSets) {
subSets.addAll(set.getSubActions());
String actionStr = set.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index).replace(" ", "");
singleEdit = handleSimpleNameNode(singleEdit, actionStr, index);
editScript += singleEdit + " ";
}
actionSets.clear();
actionSets.addAll(subSets);
}
return editScript;
}
private String handleSimpleNameNode(String singleEdit, String actionStr, int index) {
// TODO
if (singleEdit.endsWith("SimpleName")) {
actionStr = actionStr.substring(index + 2);
if (actionStr.startsWith("MethodName")) {
singleEdit = singleEdit.replace("SimpleName", "MethodName");
} else if (actionStr.startsWith("ClassName")) {
singleEdit = singleEdit.replace("SimpleName", "ClassName");
} else {
if (actionStr.startsWith("Name")) {
char c = actionStr.charAt(5);
if (Character.isUpperCase(c)) {
singleEdit = singleEdit.replace("SimpleName", "Name");
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
}
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
}
}
}
return singleEdit;
}
/**
* Get the AST node based edit script of patches in terms of deep first.
*
* @param actionSet
* @return
*/
protected String getASTEditScriptsDeepFirst(HierarchicalActionSet actionSet) {
String editScript = "";
String actionStr = actionSet.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index).replace(" ", "");
singleEdit = handleSimpleNameNode(singleEdit, actionStr, index);
editScript = singleEdit + " ";
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();;
for (HierarchicalActionSet subActionSet : subActionSets) {
editScript += getASTEditScriptsDeepFirst(subActionSet);
}
return editScript;
}
/**
*
* @param hunkActionSets
* @param bugStartLine
* @param bugEndLine
* @param fixStartLine
* @param fixEndLine
* @return
*/
protected String getASTEditScriptsBreadthFirst(List<HierarchicalActionSet> hunkActionSets, int bugEndPosition, int fixEndPosition) {
String editScript = "";
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
editScript += getASTEditScriptsBreadthFirst(hunkActionSet, bugEndPosition, fixEndPosition);
}
return editScript;
}
private String getASTEditScriptsBreadthFirst(HierarchicalActionSet hunkActionSet, int bugEndPosition, int fixEndPosition) {
String editScript = "";
List<HierarchicalActionSet> actionSets = new ArrayList<>();
actionSets.add(hunkActionSet);
while (actionSets.size() != 0) {
List<HierarchicalActionSet> subSets = new ArrayList<>();
for (HierarchicalActionSet set : actionSets) {
int position = set.getAction().getPosition();
String actionStr = set.getActionString();
if (isOutofPosition(actionStr, position, bugEndPosition, fixEndPosition)) {
continue;
}
subSets.addAll(set.getSubActions());
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index).replace(" ", "");
singleEdit = handleSimpleNameNode(singleEdit, actionStr, index);
editScript += singleEdit + " ";
}
actionSets.clear();
actionSets.addAll(subSets);
}
return editScript;
}
private boolean isOutofPosition(String actionStr, int actionPosition, int bugEndPosition, int fixEndPosition) {
if (actionStr.startsWith("INS")) {
if (actionPosition > fixEndPosition) {
return true;
}
} else if (!actionStr.startsWith("MOV") && actionPosition > bugEndPosition) {
return true;
}
return false;
}
protected String getASTEditScriptsDeepFirst(List<HierarchicalActionSet> hunkActionSets, int bugEndPosition, int fixEndPosition) {
StringBuilder editScript = new StringBuilder();
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
editScript.append(getASTEditScriptsDeepFirst2(hunkActionSet, bugEndPosition, fixEndPosition));
}
return editScript.toString();
}
private String getASTEditScriptsDeepFirst(HierarchicalActionSet actionSet, int bugEndPosition, int fixEndPosition) {
String editScripts = "";
String actionStr = actionSet.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index).replace(" ", "");
singleEdit = handleSimpleNameNode(singleEdit, actionStr, index);
editScripts += singleEdit + " ";
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
int position = subActionSet.getAction().getPosition();
actionStr = subActionSet.getActionString();
if (isOutofPosition(actionStr, position, bugEndPosition, fixEndPosition)) {
continue;
}
editScripts += getASTEditScriptsDeepFirst(subActionSet, bugEndPosition, fixEndPosition);
}
return editScripts;
}
private String getASTEditScriptsDeepFirst2(HierarchicalActionSet actionSet, int bugEndPosition, int fixEndPosition) {
StringBuilder editScripts = new StringBuilder();
String actionStr = actionSet.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index);
if (singleEdit.endsWith("Statement")) {
singleEdit = singleEdit + " " + singleEdit.substring(4, singleEdit.indexOf("Statement")).toLowerCase(Locale.ENGLISH);
} else {
singleEdit = handleSimpleNameNode2(singleEdit, actionStr, index, actionSet);
}
editScripts.append(singleEdit).append(" ");
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
int position = subActionSet.getAction().getPosition();
actionStr = subActionSet.getActionString();
if (isOutofPosition(actionStr, position, bugEndPosition, fixEndPosition)) {
continue;
}
editScripts.append(getASTEditScriptsDeepFirst2(subActionSet, bugEndPosition, fixEndPosition));
}
return editScripts.toString();
}
private String handleSimpleNameNode2(String singleEdit, String actionStr, int index, HierarchicalActionSet actionSet) {
if (singleEdit.endsWith("SimpleName")) {
actionStr = actionStr.substring(index + 2);
if (actionStr.startsWith("MethodName")) {
singleEdit = singleEdit.replace("SimpleName", "MethodName");
String methodName = actionStr.substring(actionStr.indexOf("MethodName:") + 11);
int index1 = methodName.indexOf(":");
int index2 = methodName.indexOf(" ");
index = (index1 < 0 || index1 > index2) ? index2 : index1;
methodName = methodName.substring(0, index);
singleEdit += " " + methodName;
} else if (actionStr.startsWith("ClassName")) {
singleEdit = singleEdit.replace("SimpleName", "ClassName");
String className = actionStr.substring(actionStr.indexOf("ClassName:") + 10);
int index1 = className.indexOf(" ");
index = index1 < 0 ? className.length() : index1;
className = className.substring(0, index);
singleEdit += " " + className;
} else {
if (actionStr.startsWith("Name")) {
char c = actionStr.charAt(5);
if (Character.isUpperCase(c)) {
singleEdit = singleEdit.replace("SimpleName", "Name");
String name = actionStr.substring(actionStr.indexOf("Name:") + 5);
int index1 = name.indexOf(" ");
index = index1 < 0 ? name.length() : index1;
name = name.substring(0, index);
singleEdit += " " + name;
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
int index1 = actionStr.indexOf(" ");
index = index1 < 0 ? actionStr.length() : index1;
String var = actionStr.substring(0, index);
var = new SimplifyTree().canonicalVariableName(var, actionSet.getAction().getNode());
singleEdit += " " + var.replaceAll(" ", "");
}
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
String var = actionStr.substring(0, (actionStr.indexOf(" ") < 0 ? actionStr.length() : actionStr.indexOf(" ")));
var = new SimplifyTree().canonicalVariableName(var, actionSet.getAction().getNode());
singleEdit += " " + var.replaceAll(" ", "");
}
}
} else {
if (actionSet.getSubActions() != null && actionSet.getSubActions().size() > 0) {
singleEdit += " " + actionSet.getAstNodeType()+ "exp";
} else {
if (singleEdit.endsWith("CharacterLiteral")) {
singleEdit += " charLiteral";
} else if (singleEdit.endsWith("NumberLiteral")) {
singleEdit += " numLiteral";
} else if (singleEdit.endsWith("StringLiteral")) {
singleEdit += " strLiteral";
} else if (singleEdit.endsWith("BooleanLiteral") || singleEdit.endsWith("NullLiteral") || singleEdit.endsWith("Operator") ||
singleEdit.endsWith("ThisExpression") || singleEdit.endsWith("TypeLiteral") || singleEdit.endsWith("Instanceof") ||
singleEdit.endsWith("New") || singleEdit.endsWith("WildcardType") || singleEdit.endsWith("SimpleType") ||
singleEdit.endsWith("QualifiedType") || singleEdit.endsWith("PrimitiveType") || singleEdit.endsWith("NameQualifiedType")) {
singleEdit += " " + actionSet.getNode().getLabel();
} else if (singleEdit.endsWith("SuperConstructorInvocation")) {
singleEdit += " super";
} else if (singleEdit.endsWith("ConstructorInvocation")) {
singleEdit += " this";
} else if (singleEdit.endsWith("SimpleName")) {
actionStr = actionStr.substring(index + 2);
if (actionStr.startsWith("MethodName")) {
singleEdit = singleEdit.replace("SimpleName", "MethodName");
String methodName = actionStr.substring(actionStr.indexOf("MethodName:") + 11);
int index1 = methodName.indexOf(":");
int index2 = methodName.indexOf(" ");
index = (index1 < 0 || index1 > index2) ? index2 : index1;
methodName = methodName.substring(0, index);
singleEdit += " " + methodName;
} else if (actionStr.startsWith("ClassName")) {
singleEdit = singleEdit.replace("SimpleName", "ClassName");
String className = actionStr.substring(actionStr.indexOf("ClassName:") + 10);
int index1 = className.indexOf(" ");
index = index1 < 0 ? className.length() : index1;
className = className.substring(0, index);
singleEdit += " " + className;
} else {
if (actionStr.startsWith("Name")) {
char c = actionStr.charAt(5);
if (Character.isUpperCase(c)) {
singleEdit = singleEdit.replace("SimpleName", "Name");
String name = actionStr.substring(actionStr.indexOf("Name:") + 5);
int index1 = name.indexOf(" ");
index = index1 < 0 ? name.length() : index1;
name = name.substring(0, index);
singleEdit += " " + name;
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
int index1 = actionStr.indexOf(" ");
index = index1 < 0 ? actionStr.length() : index1;
String var = actionStr.substring(0, index);
var = new SimplifyTree().canonicalVariableName(var, actionSet.getAction().getNode());
singleEdit += " " + var.replaceAll(" ", "");
}
} else {
singleEdit = singleEdit.replace("SimpleName", "Variable");
String var = actionStr.substring(0, (actionStr.indexOf(" ") < 0 ? actionStr.length() : actionStr.indexOf(" ")));
var = new SimplifyTree().canonicalVariableName(var, actionSet.getAction().getNode());
singleEdit += " " + var.replaceAll(" ", "");
}
}
} else {
ITree nodeTree = actionSet.getNode();
if (nodeTree.getChildren().size() > 0) {
singleEdit += " " + actionSet.getAstNodeType()+ "exp";
} else {
singleEdit += " " + nodeTree.getLabel().replaceAll(" ", "");
}
}
}
}
return singleEdit;
}
protected String getAstEditScripts(List<HierarchicalActionSet> actionSets, int bugEndLine, int fixEndLine) {
String editScripts = "";
for (HierarchicalActionSet actionSet : actionSets) {
editScripts += getActionString(actionSet, bugEndLine, fixEndLine, "");
}
return editScripts;
}
private String getActionString(HierarchicalActionSet actionSet, int bugEndPosition, int fixEndPosition, String hierarchicalLabel) {
String editScripts = "";
String actionStr = actionSet.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index);
singleEdit = handleSimpleNameNode(singleEdit, actionStr, index);
editScripts += hierarchicalLabel + singleEdit + "\n";
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
int position = subActionSet.getAction().getPosition();
actionStr = subActionSet.getActionString();
if (isOutofPosition(actionStr, position, bugEndPosition, fixEndPosition)) {
continue;
}
editScripts += getActionString(subActionSet, bugEndPosition, fixEndPosition, hierarchicalLabel + "---");
}
return editScripts;
}
protected void clearITree(HierarchicalActionSet actionSet) {
actionSet.getAction().setNode(null);
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
clearITree(subActionSet);
}
}
@Override
public String getAstEditScripts() {
return astEditScripts;
@@ -472,10 +31,10 @@ public abstract class Parser implements ParserInterface {
return patchesSourceCode;
}
@Override
public String getBuggyTrees() {
return buggyTrees;
}
// @Override
// public String getBuggyTrees() {
// return buggyTrees;
// }
@Override
public String getSizes() {
@@ -487,13 +46,13 @@ public abstract class Parser implements ParserInterface {
return tokensOfSourceCode;
}
@Override
public String getOriginalTree() {
return originalTree;
}
@Override
public String getActionSets() {
return actionSets;
}
// @Override
// public String getOriginalTree() {
// return originalTree;
// }
//
// @Override
// public String getActionSets() {
// return actionSets;
// }
}
@@ -1,23 +1,21 @@
package edu.lu.uni.serval.FixPatternParser;
import java.io.File;
public interface ParserInterface {
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile);
// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile);
public String getAstEditScripts();
public String getPatchesSourceCode();
public String getBuggyTrees();
// public String getBuggyTrees();
public String getSizes();
public String getTokensOfSourceCode();
public String getOriginalTree();
// public String getOriginalTree();
public String getActionSets();
// public String getActionSets();
}
@@ -1,75 +0,0 @@
package edu.lu.uni.serval.FixPatternParser;
import java.util.List;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
public class Tokenizer {
public static String getTokensDeepFirst(SimpleTree simpleTree) {
String tokens = "";
List<SimpleTree> children = simpleTree.getChildren();
String astNodeType = simpleTree.getNodeType();
if (children.isEmpty()) { // BreakStatement, ContinueStatement, ReturnStatement, TryStatement
if (astNodeType.endsWith("Statement")) {
String label = astNodeType;
label = label.substring(0, label.lastIndexOf("S")).toLowerCase();
tokens += astNodeType + " " + label + " ";
} else if ("SuperConstructorInvocation".equals(astNodeType)) {
tokens += astNodeType + " super ";
} else if ("ConstructorInvocation".equals(astNodeType)) {
tokens += astNodeType + " this ";
} else if ("StringLiteral".equals(astNodeType)) {
tokens += astNodeType + " stringLiteral ";
} else if ("CharacterLiteral".equals(astNodeType)) {
tokens += astNodeType + " charLiteral ";
} else if ("ArrayInitializer".equals(astNodeType)) {
tokens += astNodeType + " arrayInitializer ";
} else if ("LambdaExpression".equals(astNodeType)) {
tokens += astNodeType + " lambda ";
} else if ("NumberLiteral".equals(astNodeType)) {
tokens += astNodeType + " numberLiteral ";
} else {
tokens += astNodeType + " " + simpleTree.getLabel() + " ";
}
} else {
if ("AssertStatement".equals(astNodeType) || "DoStatement".equals(astNodeType)
|| "ForStatement".equals(astNodeType) || "IfStatement".equals(astNodeType)
|| "ReturnStatement".equals(astNodeType) || "SwitchStatement".equals(astNodeType)
|| "SynchronizedStatement".equals(astNodeType) || "ThrowStatement".equals(astNodeType)
|| "TryStatement".equals(astNodeType) || "WhileStatement".equals(astNodeType)) {
String label = astNodeType;
label = label.substring(0, label.lastIndexOf("S")).toLowerCase();
tokens += astNodeType + " " + label + " ";
} else if ("EnhancedForStatement".equals(astNodeType)) {
tokens += astNodeType + " " + "for ";
} else if ("CatchClause".equals(astNodeType)) {
tokens += astNodeType + " " + "catch ";
} else if ("SwitchCase".equals(astNodeType)) {
tokens += astNodeType + " case ";
} else if ("SuperConstructorInvocation".equals(astNodeType)) {
tokens += astNodeType + " super ";
} else if ("ConstructorInvocation".equals(astNodeType)) {
tokens += astNodeType + " this ";
} else if ("FinallyBody".equals(astNodeType)) {
tokens += astNodeType + " finally ";
} else if ("LabeledStatement".equals(astNodeType)) {
tokens += "LabeledStatement " + simpleTree.getLabel();
} else if ("SuperMethodInvocation".equals(astNodeType)) {
tokens += astNodeType + " super ";
} else if ("MethodName".equals(astNodeType)) {
tokens += "MethodName " + simpleTree.getLabel() + " ";
}
if ("ArrayInitializer".equals(astNodeType)) {
tokens += astNodeType + " arrayInitializer ";
} else {
for (SimpleTree child : children) {
tokens += getTokensDeepFirst(child);
}
}
}
return tokens;
}
}
@@ -1,113 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.patch;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HunkActionFilter;
import edu.lu.uni.serval.gumtree.regroup.HunkFixPattern;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
/**
* Parse fix patterns with GumTree.
*
* Multiple statements bugs.
*
* @author kui.liu
*
*/
public class CommitPatchHunkParser extends CommitPatchParser {
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile) {
// GumTree results
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree(prevFile, revFile);
if (actionSets.size() > 0) {
// DiffEntry size: filter out big hunks.
List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks(diffEntryFile);
//Filter out the modify actions, which are not in the DiffEntry hunks.
HunkActionFilter hunkFilter = new HunkActionFilter();
List<HunkFixPattern> allHunkFixPatterns = hunkFilter.filterActionsByDiffEntryHunk2(diffentryHunks, actionSets, revFile, prevFile);
for (HunkFixPattern hunkFixPattern : allHunkFixPatterns) {
// Range of buggy source code
int startLine = 0;
int endLine = 0;
// Range of fixing source code
int startLine2 = 0;
int endLine2 = 0;
/*
* Convert the ITree of buggy code to a simple tree.
* It will be used to compute the similarity.
*/
List<HierarchicalActionSet> hunkActionSets = hunkFixPattern.getHunkActionSets();
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
String astEditScripts = "";
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(hunkActionSet);
SimpleTree simpleT = hunkActionSet.getSimpleTree();
if (simpleT == null) { // Failed to get the simple tree for INS actions.
continue;
}
children.add(simpleT);
/**
* Select edit scripts for deep learning.
* Edit scripts will be used to mine common fix patterns.
*/
// 1. First level: AST node type.
astEditScripts += getASTEditScriptsBreadthFirst(hunkActionSet);
// 2. source code: raw tokens
// 3. abstract identifiers:
// 4. semi-source code:
if (startLine == 0) {
startLine = hunkActionSet.getBugStartLineNum();
endLine = hunkActionSet.getBugEndLineNum();
startLine2 = hunkActionSet.getFixStartLineNum();
endLine2 = hunkActionSet.getFixEndLineNum();
} else {
if (startLine > hunkActionSet.getBugStartLineNum()) startLine = hunkActionSet.getBugStartLineNum();
if (startLine2 > hunkActionSet.getFixStartLineNum()) startLine2 = hunkActionSet.getFixStartLineNum();
if (endLine < hunkActionSet.getBugEndLineNum()) endLine = hunkActionSet.getBugEndLineNum();
if (endLine2 < hunkActionSet.getFixEndLineNum()) endLine2 = hunkActionSet.getFixEndLineNum();
}
}
if (children.size() == 0) continue;
if (endLine - startLine >= Configuration.HUNK_SIZE - 2 || endLine2 - startLine2 >= Configuration.HUNK_SIZE - 2 ) continue;
simpleTree.setChildren(children);
simpleTree.setParent(null);
// Source Code of patches.
String patchSourceCode = getPatchSourceCode(hunkFixPattern.getHunk(), startLine, endLine, startLine2, endLine2);
if ("".equals(patchSourceCode)) continue;
this.patchesSourceCode += "PATCH###\n" + patchSourceCode + "\n";
int size = astEditScripts.split(" ").length;
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
// this.buggyTrees += Configuration.BUGGY_TREE_TOKEN + "\n" + simpleTree.toString() + "\n";
this.tokensOfSourceCode += Tokenizer.getTokensDeepFirst(simpleTree).trim() + "\n";
// this.actionSets += Configuration.BUGGY_TREE_TOKEN + "\n" + readActionSet(actionSet, "") + "\n";
// this.originalTree += Configuration.BUGGY_TREE_TOKEN + "\n" + actionSet.getOriginalTree().toString() + "\n";
}
}
}
}
@@ -1,105 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.patch;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import edu.lu.uni.serval.FixPatternParser.Parser;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
/**
* Parse fix patterns with GumTree.
*
* @author kui.liu
*
*/
public class CommitPatchParser extends Parser{
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile) {
}
protected DiffEntryHunk matchHunk(int startLine, int endLine, int startLine2, int endLine2, String actionStr, List<DiffEntryHunk> hunks) {
for (DiffEntryHunk hunk : hunks) {
int bugStartLine = hunk.getBugLineStartNum();
int bugRange = hunk.getBugRange();
int fixStartLine = hunk.getFixLineStartNum();
int fixRange = hunk.getFixRange();
if (actionStr.startsWith("INS")) {
if (fixStartLine + fixRange < startLine2) {
continue;
}
if (endLine2 < fixStartLine ) {
return null;
}
return hunk;
} else {
if (bugStartLine + bugRange < startLine) {
continue;
}
if (endLine < bugStartLine ) {
return null;
}
return hunk;
}
}
return null;
}
protected String getPatchSourceCode(DiffEntryHunk hunk, int startLineNum, int endLineNum, int startLineNum2, int endLineNum2) {
String sourceCode = hunk.getHunk();
int bugStartLine = hunk.getBugLineStartNum();
int fixStartLine = hunk.getFixLineStartNum();
String buggyStatements = "";
String fixedStatements = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(sourceCode));
String line = null;
int bugLines = 0;
int fixLines = 0;
int contextLines = 0; // counter of non-buggy code line.
while ((line = reader.readLine()) != null) {
int bugLineIndex = bugLines + contextLines;
int fixLineIndex = fixLines + contextLines;
if (line.startsWith("-")) {
if (bugStartLine + bugLineIndex >= startLineNum && bugStartLine + bugLineIndex <= endLineNum) {
buggyStatements += line + "\n";
}
bugLines ++;
} else if (line.startsWith("+")) {
if (fixStartLine + fixLineIndex >= startLineNum2 && fixStartLine + fixLineIndex <= endLineNum2) {
fixedStatements += line + "\n";
}
fixLines ++;
} else {
contextLines ++;
}
if (bugStartLine + bugLineIndex > endLineNum && fixStartLine + fixLineIndex > endLineNum2) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buggyStatements + fixedStatements;
}
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project) {
}
}
@@ -1,158 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.patch;
import java.io.File;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
/**
* Parse fix patterns with GumTree.
*
* Single Statement bugs.
*
* @author kui.liu
*
*/
public class CommitPatchSingleStatementParser extends CommitPatchParser {
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile) {
// GumTree results
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree(prevFile, revFile);
if (actionSets.size() > 0) {
// DiffEntry Hunks: filter out big hunks.
List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks(diffEntryFile);
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return;
}
for (HierarchicalActionSet actionSet : actionSets) {
// position of buggy statements
int startPosition = 0;
int endPosition = 0;
// position of fixed statements
int startPosition2 = 0;
int endPosition2 = 0;
String actionStr = actionSet.getActionString();
String astNodeType = actionSet.getAstNodeType();
if (actionStr.startsWith("INS")) {
startPosition2 = actionSet.getStartPosition();
endPosition2 = startPosition2 + actionSet.getLength();
List<Move> firstAndLastMov = getFirstAndLastMoveAction(actionSet);
if (firstAndLastMov != null) {
startPosition = firstAndLastMov.get(0).getNode().getPos();
ITree lastTree = firstAndLastMov.get(1).getNode();
endPosition = lastTree.getPos() + lastTree.getLength();
} else { // Ignore the pure insert actions without any move actions.
continue;
}
} else if (actionStr.startsWith("UPD")) {
startPosition = actionSet.getStartPosition();
endPosition = startPosition + actionSet.getLength();
Update update = (Update) actionSet.getAction();
ITree newNode = update.getNewNode();
startPosition2 = newNode.getPos();
endPosition2 = startPosition2 + newNode.getLength();
if (Checker.containsBodyBlock(astNodeType)) {
List<ITree> children = update.getNode().getChildren();
endPosition = getEndPosition(children);
List<ITree> newChildren = newNode.getChildren();
endPosition2 = getEndPosition(newChildren);
if (endPosition == 0) {
endPosition = startPosition + actionSet.getLength();
}
if (endPosition2 == 0) {
endPosition2 = startPosition2 + newNode.getLength();
}
}
} else {// DEL actions and MOV actions: we don't need these actions, as for now.
continue;
}
if (startPosition == 0 || startPosition2 == 0) {
continue;
}
// Get line numbers.
int startLine = prevUnit.getLineNumber(startPosition);
int endLine = prevUnit.getLineNumber(endPosition);
int startLine2 = revUnit.getLineNumber(startPosition2);
int endLine2 = revUnit.getLineNumber(endPosition2);
if (endLine - startLine >= Configuration.HUNK_SIZE - 2 || endLine2 - startLine2 >= Configuration.HUNK_SIZE - 2)
continue;
// Filter out the modify actions, which are not in the DiffEntry hunks.
DiffEntryHunk hunk = matchHunk(startLine, endLine, startLine2, endLine2, actionStr, diffentryHunks);
if (hunk == null) {
continue;
}
/*
* Convert the ITree of buggy code to a simple tree. It will be
* used to compute the similarity.
*/
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(actionSet);
SimpleTree simpleTree = actionSet.getSimpleTree();
if (simpleTree == null) { // Failed to get the simple tree for INS actions.
continue;
}
/**
* Select edit scripts for deep learning. Edit scripts will be
* used to mine common fix patterns.
*/
// 1. First level: AST node type.
String astEditScripts = getASTEditScriptsBreadthFirst(actionSet);
int size = astEditScripts.split(" ").length;
if (size < 2) {
// System.out.println(actionSet);
continue;
}
// Source Code of patches.
String patchSourceCode = getPatchSourceCode(hunk, startLine, endLine, startLine2, endLine2);
if ("".equals(patchSourceCode))
continue;
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + patchSourceCode + "\n";
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
// // 2. source code: raw tokens
// String rawTokenEditScripts = getRawTokenEditScripts(actionSet);
// // 3. abstract identifiers:
// String abstractIdentifiersEditScripts = getAbstractIdentifiersEditScripts(actionSet);
// // 4. semi-source code:
// String semiSourceCodeEditScripts = getSemiSourceCodeEditScripts(actionSet);
// this.buggyTrees += Configuration.BUGGY_TREE_TOKEN + "\n" +
// simpleTree.toString() + "\n";
this.tokensOfSourceCode += Tokenizer.getTokensDeepFirst(simpleTree).trim() + "\n";
// this.actionSets += Configuration.BUGGY_TREE_TOKEN + "\n" +
// readActionSet(actionSet, "") + "\n";
// this.originalTree += Configuration.BUGGY_TREE_TOKEN + "\n" +
// actionSet.getOriginalTree().toString() + "\n";
}
actionSets.clear();
}
}
}
@@ -1,62 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
/**
* Created by anilkoyuncu on 19/03/2018.
*/
public class CSVUtils {
private static final char DEFAULT_SEPARATOR = ',';
public static void writeLine(Writer w, List<String> values) throws IOException {
writeLine(w, values, DEFAULT_SEPARATOR, ' ');
}
public static void writeLine(Writer w, List<String> values, char separators) throws IOException {
writeLine(w, values, separators, ' ');
}
//https://tools.ietf.org/html/rfc4180
private static String followCVSformat(String value) {
String result = value;
if (result.contains("\"")) {
result = result.replace("\"", "\"\"");
}
return result;
}
public static void writeLine(Writer w, List<String> values, char separators, char customQuote) throws IOException {
boolean first = true;
//default customQuote is empty
if (separators == ' ') {
separators = DEFAULT_SEPARATOR;
}
StringBuilder sb = new StringBuilder();
for (String value : values) {
if (!first) {
sb.append(separators);
}
if (customQuote == ' ') {
sb.append(followCVSformat(value));
} else {
sb.append(customQuote).append(followCVSformat(value)).append(customQuote);
}
first = false;
}
sb.append("\n");
w.append(sb.toString());
}
}
@@ -1,226 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import com.github.gumtreediff.actions.ActionGenerator;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;
import com.rabbitmq.client.*;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
/**
* Created by anilkoyuncu on 03/04/2018.
*/
public class Consumer {
private static Logger log = LoggerFactory.getLogger(Consumer.class);
private static String inputPath = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeOutput2";
public static void main(String[] args) {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.240.5.10");
// factory.setVirtualHost("treeCompare");
factory.setPort(5672);
factory.setUsername("anil");
factory.setPassword("changeme2018");
factory.setConnectionTimeout(1000);
Connection connection = null;
Channel channel = null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("tree_queue", false, false, false, null);
boolean autoAck = false;
Channel finalChannel = channel;
channel.basicConsume("tree_queue", autoAck,"myConsumerTag",
new DefaultConsumer(finalChannel) {
@Override
public void handleDelivery(
String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
String[] split = message.split(",");
String key = split[0];
String firstV = split[1];
String secondV = split[2];
try{
calculateSimi(firstV,secondV,key);
}catch (Exception e){
finalChannel.basicNack(envelope.getDeliveryTag(),false,false);
}
finalChannel.basicAck(envelope.getDeliveryTag(),false);
// process the message
}
});
// channel.basicConsume("tree_queue", true, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void calculateSimi(String firstVal,String secondVal, String key){
String[] split = key.split("_");
String i = split[1];
String j = split[2];
String[] firstValueSplit = firstVal.split("GumTreeOutput2");
String[] secondValueSplit = secondVal.split("GumTreeOutput2");
String firstValue;
if (firstValueSplit.length == 1) {
firstValue = inputPath + firstValueSplit[0];
} else {
firstValue = inputPath + firstValueSplit[1];
}
String secondValue;
if (secondValueSplit.length == 1) {
secondValue = inputPath + secondValueSplit[0];
} else {
secondValue = inputPath + secondValueSplit[1];
}
ITree oldTree = getSimpliedTree(firstValue);
ITree newTree = getSimpliedTree(secondValue);
Matcher m = Matchers.getInstance().getMatcher(oldTree, newTree);
m.match();
ActionGenerator ag = new ActionGenerator(oldTree, newTree, m.getMappings());
ag.generate();
List<Action> actions = ag.getActions();
double chawatheSimilarity1 = m.chawatheSimilarity(oldTree, newTree);
String chawatheSimilarity = String.format("%1.2f", chawatheSimilarity1);
double diceSimilarity1 = m.diceSimilarity(oldTree, newTree);
String diceSimilarity = String.format("%1.2f", diceSimilarity1);
double jaccardSimilarity1 = m.jaccardSimilarity(oldTree, newTree);
String jaccardSimilarity = String.format("%1.2f", jaccardSimilarity1);
String editDistance = String.valueOf(actions.size());
String result = firstVal + "," + secondVal + "," + chawatheSimilarity + "," + diceSimilarity + "," + jaccardSimilarity + "," + editDistance;
if (((Double) chawatheSimilarity1).equals(1.0) || ((Double) diceSimilarity1).equals(1.0)
|| ((Double) jaccardSimilarity1).equals(1.0) || actions.size() == 0) {
String matchKey = "match_" + (String.valueOf(i)) + "_" + String.valueOf(j);
log.info(matchKey);
// JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", Integer.valueOf(port), 20000000);
// try (Jedis outer = jedisPool.getResource()) {
// outer.select(1);
// outer.set(matchKey, result);
// }
}
// try (Jedis jedis = pool.getResource()) {
// jedis.del("pair_"+ (String.valueOf(i)) + "_" + String.valueOf(j));
// }
}
public static ITree getSimpliedTree(String fn) {
HierarchicalActionSet actionSet = null;
try {
FileInputStream fi = new FileInputStream(new File(fn));
ObjectInputStream oi = new ObjectInputStream(fi);
actionSet = (HierarchicalActionSet) oi.readObject();
oi.close();
fi.close();
} catch (FileNotFoundException e) {
log.error("File not found");
e.printStackTrace();
} catch (IOException e) {
log.error("Error initializing stream");
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ITree parent = null;
ITree children =null;
ITree tree = getASTTree(actionSet, parent, children);
tree.setParent(null);
return tree;
}
public static <T, E> List<T> getKeysByValue(Map<T, E> map, E value) {
return map.entrySet()
.stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children){
int newType = 0;
String astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType);
if(keysByValue.size() != 1){
log.error("Birden cok astnodemapmapping");
}
newType = keysByValue.get(0);
if(actionSet.getParent() == null){
//root
parent = new Tree(newType,"");
}else{
children = new Tree(newType,"");
parent.addChild(children);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getASTTree(subAction,children,null);
}
}
return parent;
}
}
@@ -1,121 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
public enum FieldViolation {
UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR,
ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD,
LI_LAZY_INIT_STATIC,
URF_UNREAD_FIELD,
MS_SHOULD_BE_FINAL,
URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD,
UWF_UNWRITTEN_FIELD,
MS_PKGPROTECT,
SS_SHOULD_BE_STATIC,
NP_UNWRITTEN_FIELD,
UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD,
SE_BAD_FIELD,
UUF_UNUSED_FIELD,
SE_BAD_FIELD_STORE,
MS_MUTABLE_ARRAY,
NP_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD,
}
enum ClassViolation{
SIC_INNER_SHOULD_BE_STATIC_ANON, //inner class. ClassInstanceCreation by overriding some methods.
SIC_INNER_SHOULD_BE_STATIC,
RI_REDUNDANT_INTERFACES,
SE_COMPARATOR_SHOULD_BE_SERIALIZABLE,
SE_NO_SERIALVERSIONID,
CI_CONFUSED_INHERITANCE,
SE_INNER_CLASS,
NM_CLASS_NOT_EXCEPTION,
}
enum MethodViolation {
NM_METHOD_NAMING_CONVENTION,
UPM_UNCALLED_PRIVATE_METHOD,
DE_MIGHT_IGNORE,
NM_CONFUSING,
CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE,
UC_USELESS_VOID_METHOD,
}
enum StmtViolation {
DLS_DEAD_LOCAL_STORE, //Assignment statement. false positives may be high.
//This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction.
DM_BOXED_PRIMITIVE_FOR_PARSING,
DM_CONVERT_CASE,
DM_DEFAULT_ENCODING,
IS2_INCONSISTENT_SYNC,
NP_GUARANTEED_DEREF,
SIO_SUPERFLUOUS_INSTANCEOF,
NP_BOOLEAN_RETURN_NULL,
SF_SWITCH_FALLTHROUGH,
DP_DO_INSIDE_DO_PRIVILEGED,
DMI_INVOKING_TOSTRING_ON_ARRAY,
NP_NONNULL_PARAM_VIOLATION,
CN_IDIOM_NO_SUPER_CALL,
UL_UNRELEASED_LOCK_EXCEPTION_PATH,
RC_REF_COMPARISON_BAD_PRACTICE,
NP_ALWAYS_NULL,
NP_NULL_PARAM_DEREF,
GC_UNRELATED_TYPES,
DLS_DEAD_LOCAL_STORE_OF_NULL,
RpC_REPEATED_CONDITIONAL_TEST,
ODR_OPEN_DATABASE_RESOURCE_EXCEPTION_PATH,
DB_DUPLICATE_SWITCH_CLAUSES,
RV_RETURN_VALUE_IGNORED,
ES_COMPARING_PARAMETER_STRING_WITH_EQ,
RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE,
ICAST_IDIV_CAST_TO_DOUBLE,
BC_VACUOUS_INSTANCEOF,
EC_UNRELATED_TYPES,
ICAST_INTEGER_MULTIPLY_CAST_TO_LONG,
DM_STRING_TOSTRING,
DMI_HARDCODED_ABSOLUTE_FILENAME,
MS_MUTABLE_COLLECTION_PKGPROTECT,
NP_NULL_ON_SOME_PATH_EXCEPTION,
SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE,
EQ_COMPARETO_USE_OBJECT_EQUALS,
BX_UNBOXING_IMMEDIATELY_REBOXED,
HE_EQUALS_USE_HASHCODE,
DM_EXIT,
FE_FLOATING_POINT_EQUALITY,
RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE,
BC_UNCONFIRMED_CAST,
REC_CATCH_EXCEPTION,
BC_UNCONFIRMED_CAST_OF_RETURN_VALUE,
VA_FORMAT_STRING_USES_NEWLINE,
RV_RETURN_VALUE_IGNORED_BAD_PRACTICE,
EI_EXPOSE_REP2,
WMI_WRONG_MAP_ITERATOR,
EI_EXPOSE_REP,
NP_LOAD_OF_KNOWN_NULL_VALUE,
DM_NUMBER_CTOR,
SBSC_USE_STRINGBUFFER_CONCATENATION,
OS_OPEN_STREAM_EXCEPTION_PATH,
NP_NONNULL_RETURN_VIOLATION,
SF_SWITCH_NO_DEFAULT,
RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT,
ODR_OPEN_DATABASE_RESOURCE,
PZLA_PREFER_ZERO_LENGTH_ARRAYS,
NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE,
UCF_USELESS_CONTROL_FLOW,
UC_USELESS_CONDITION,
NP_NULL_ON_SOME_PATH,
DM_FP_NUMBER_CTOR,
SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING,
OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE,
ES_COMPARING_STRINGS_WITH_EQ,
OS_OPEN_STREAM,
RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE,
NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE,
UC_USELESS_OBJECT,
OBL_UNSATISFIED_OBLIGATION,
}
@@ -1,25 +1,11 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.gumtreediff.actions.model.Delete;
import com.github.gumtreediff.actions.model.Insert;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HunkActionFilter;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
import java.io.*;
import java.util.List;
/**
* Parse fix violations with GumTree in terms of multiple statements.
@@ -97,232 +83,5 @@ public class FixedViolationHunkParser extends FixedViolationParser {
}
// public void parseFixPatterns(File prevFile, File revFile, File diffentryFile) {
// // GumTree results
// List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile);
//
//
// if (this.resultType != 0) {
//// String type = "";
//// if (this.resultType == 1) {
//// type = "#NullGumTreeResult:";
//// } else if (this.resultType == 2) {
//// type = "#NoSourceCodeChange:";
//// } else if (this.resultType == 3) {
//// type = "#NoStatementChange:";
//// }
// } else {
// List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks3(diffentryFile);
//
// //Filter out the modify actions, which are not in the DiffEntry hunks.
// HunkActionFilter hunkFilter = new HunkActionFilter();
// List<DiffEntryHunk> selectedPatchHunks = hunkFilter.filterActionsByModifiedRange2(diffentryHunks, actionSets, revFile, prevFile);
//
// for (DiffEntryHunk patchHunk : selectedPatchHunks) {
// List<HierarchicalActionSet> hunkActionSets = patchHunk.getActionSets();
// // multiple UPD, and some UPD contain other UPD.
// removeOverlapperdUPD(hunkActionSets);
//
// // Range of buggy source code
// int bugStartLine = 0;
// int bugEndLine = 0;
// // Range of fixing source code
// int fixStartLine = 0;
// int fixEndLine = 0;
// int bugEndPosition = 0;
// int fixEndPosition = 0;
// int hunkSet = 0;
// for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
//
//
// FileOutputStream f = null;
// try {
// String pj = diffentryFile.getParent().split("GumTreeInputBug4")[1];
// String root = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeOutput2";
// String hunkTreeFileName = root+pj.replace("DiffEntries","ASTDumps/") + diffentryFile.getName() + "_" + String.valueOf(hunkSet);
// f = new FileOutputStream(new File(hunkTreeFileName));
// ObjectOutputStream o = new ObjectOutputStream(f);
// o.writeObject(hunkActionSet.getNode());
//
// o.close();
// f.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// hunkSet++;
//
//
//
// int actionBugStart = hunkActionSet.getBugStartLineNum();
// int actionBugEnd = hunkActionSet.getBugEndLineNum();
// int actionFixStart = hunkActionSet.getFixStartLineNum();
// int actionFixEnd = hunkActionSet.getFixEndLineNum();
// if (bugStartLine == 0) {
// bugStartLine = actionBugStart;
// } else if (actionBugStart != 0 && actionBugStart < bugStartLine) {
// bugStartLine = actionBugStart;
// }
// if (fixStartLine == 0) {
// fixStartLine = actionFixStart;
// } else if (actionFixStart != 0 && actionFixStart < fixStartLine) {
// fixStartLine = actionFixStart;
// }
// if (bugEndLine < actionBugEnd) {
// bugEndLine = actionBugEnd;
// bugEndPosition = hunkActionSet.getBugEndPosition();
// }
// if (fixEndLine < actionFixEnd) {
// fixEndLine = actionFixEnd;
// fixEndPosition = hunkActionSet.getFixEndPosition();
// }
// }
//
// if (fixStartLine == 0 && bugStartLine == 0) {
// this.unfixedViolations += "#WRONG: " + revFile.getName() + ":" + patchHunk.getBugLineStartNum() + ", " + patchHunk.getBuggyHunkSize() + "\n";
// this.nullMappingGumTreeResult ++;
// continue;
// }
//
// if (fixStartLine == 0 && bugStartLine != 0) {// pure delete actions.
// // get the exact buggy code by violation's position. TODO later
// }
//
//// if (children.size() == 0) continue;
// boolean isPureInsert = false;
// if (bugStartLine == 0 && patchHunk.getBugLineStartNum() > 0) {
// bugStartLine = patchHunk.getBugLineStartNum();
// bugEndLine = bugStartLine + patchHunk.getBuggyHunkSize() - 1;
// isPureInsert = true;
//// continue;
// }
//// if ((bugEndLine - bugStartLine > Configuration.HUNK_SIZE ) || fixEndLine - fixStartLine > Configuration.HUNK_SIZE) {
//// continue; //TODO hunk size
//// }
// if(patchHunk.getBuggyHunkSize() > Configuration.HUNK_SIZE || patchHunk.getFixedHunkSize() > Configuration.HUNK_SIZE){
// continue;
// }
//
// /**
// * Select edit scripts for deep learning.
// * Edit scripts will be used to mine common fix patterns.
// */
// // 1. First level: AST node type.
// String astEditScripts = getASTEditScriptsDeepFirst(hunkActionSets, bugEndPosition, fixEndPosition);
// if (astEditScripts.contains("\n") || astEditScripts.split(" ").length % 3 != 0) {
// System.err.println("===+++===: " + revFile.getName() + ":" +patchHunk.getBugLineStartNum() + ", " + patchHunk.getBuggyHunkSize());
// }
// // 2. source code: raw tokens
// // 3. abstract identifiers:
// // 4. semi-source code:
// String[] editScriptTokens = astEditScripts.split(" ");
// int size = editScriptTokens.length;
// if (size == 1) {
// this.nullMappingGumTreeResult ++;
// this.unfixedViolations += "#NullMatchedGumTreeResult1:" + revFile.getName() + ":" + patchHunk.getBugLineStartNum() + ", " + patchHunk.getBuggyHunkSize() + "\n";
// continue;
// }
//
// String patchPosition = "\n" + revFile.getName() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
//// String info = Configuration.PATCH_SIGNAL + "\n" + patchPosition + patchHunk.getHunk() + "\nAST Diff###:\n" + getAstEditScripts(hunkActionSets, bugEndPosition, fixEndPosition) + "\n";
////TODO uncomment the line below for more detailed gumtree input.
// String info = Configuration.PATCH_SIGNAL + "\n" + patchPosition + patchHunk.getHunk() + "\nAST Diff###:\n" + getAstEditScripts(hunkActionSets) + "\n";
//// if (noUpdate(editScriptTokens)) {
//// }
//
// String canonicalVariableNames = getBuggyCodeTree(patchHunk, prevFile, revFile);
// this.patchesSourceCode += info;
// this.patchesSourceCode += "\nRenamed_Variables###:\n" + canonicalVariableNames;
// this.sizes += size + "\n";
// this.astEditScripts += astEditScripts + "\n";
//// String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim();
//// this.tokensOfSourceCode += tokens + "\n";
// }
// }
// }
private String getAstEditScripts(List<HierarchicalActionSet> hunkActionSets) {
String scripts = "";
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
scripts += hunkActionSet.toString() + "\n";
}
return scripts;
}
private String getBuggyCodeTree(DiffEntryHunk patchHunk, File prevFile, File revFile) {
int bugStartLine = patchHunk.getBugLineStartNum();
int bugEndLine = bugStartLine + patchHunk.getBugRange() - 1;
ViolationSourceCodeTree parser = new ViolationSourceCodeTree(prevFile, bugStartLine, bugEndLine);
parser.extract();
List<ITree> matchedTrees = parser.getViolationSourceCodeTrees();
Map<String, String> renamedVariablesMap = new HashMap<>();
Map<String, Integer> canonicalVariables = new HashMap<>();
if (matchedTrees.size() > 0) {
SimplifyTree st = new SimplifyTree();
for (ITree matchedTree : matchedTrees) {
st.canonicalizeSourceCodeTree(matchedTree);
}
renamedVariablesMap = st.canonicalVariableMap;
canonicalVariables = st.canonicalVariables;
}
int fixStartLine = patchHunk.getFixLineStartNum();
int fixEndLine = fixStartLine + patchHunk.getFixRange() - 1;
ViolationSourceCodeTree fixedParser = new ViolationSourceCodeTree(revFile, fixStartLine, fixEndLine);
fixedParser.extract();
matchedTrees = fixedParser.getViolationSourceCodeTrees();
if (matchedTrees.size() > 0) {
SimplifyTree st = new SimplifyTree();
st.canonicalVariableMap = renamedVariablesMap;
st.canonicalVariables = canonicalVariables;
for (ITree matchedTree : matchedTrees) {
st.canonicalizeSourceCodeTree(matchedTree);
}
renamedVariablesMap = st.canonicalVariableMap;
}
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, String> entry : renamedVariablesMap.entrySet()) {
builder.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
}
return builder.toString();
}
private void removeOverlapperdUPD(List<HierarchicalActionSet> actionSets) {
if (actionSets.size() == 1) {
return;
}
List<HierarchicalActionSet> updates = new ArrayList<>();
for (HierarchicalActionSet actionSet : actionSets) {
if (actionSet.getActionString().startsWith("UPD")) {
updates.add(actionSet);
}
}
List<HierarchicalActionSet> overlappedUpdates = new ArrayList<>();
if (updates.size() > 1) {
for (HierarchicalActionSet update : updates) {
int startLine = update.getBugStartLineNum();
int endLine = update.getBugEndLineNum();
int endPosition = update.getBugEndPosition();
for (HierarchicalActionSet update2 : updates) {
if (update.equals(update2)) continue;
int startLine2 = update2.getBugStartLineNum();
int endLine2 = update2.getBugEndLineNum();
int endPosition2 = update2.getBugEndPosition();
if (startLine <= startLine2
&& endLine2 <= endLine && endPosition2 < endPosition) {
if (!overlappedUpdates.contains(update)) {
overlappedUpdates.add(update);
}
break;
}
}
}
}
actionSets.removeAll(overlappedUpdates);
}
}
@@ -1,21 +1,16 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.actions.model.Action;
import edu.lu.uni.serval.FixPatternParser.Parser;
import edu.lu.uni.serval.gumtree.GumTreeComparer;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalRegrouper;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Parse fix patterns with GumTree.
*
@@ -60,16 +55,7 @@ public class FixedViolationParser extends Parser {
} else {
// Regroup GumTre results.
List<HierarchicalActionSet> allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults);
// for (HierarchicalActionSet actionSet : allActionSets) {
// String astNodeType = actionSet.getAstNodeType();
// if (astNodeType.endsWith("Statement") || "FieldDeclaration".equals(astNodeType)) {
// actionSets.add(actionSet);
// }
// }
// Filter out modified actions of changing method names, method parameters, variable names and field names in declaration part.
// variable effects range, sub-actions are these kinds of modification?
// actionSets.addAll(new ActionFilter().filterOutUselessActions(allActionSets));
ListSorter<HierarchicalActionSet> sorter = new ListSorter<>(allActionSets);
actionSets = sorter.sortAscending();
@@ -82,63 +68,14 @@ public class FixedViolationParser extends Parser {
}
}
/**
* Read patch source code from buggy and fixed files.
* @param prevFile
* @param revFile
* @param bugStartLineNum
* @param bugEndLineNum
* @param fixStartLineNum
* @param fixEndLineNum
* @param isInsert
* @return
*/
protected String getPatchSourceCode(File prevFile, File revFile, int bugStartLineNum, int bugEndLineNum, int fixStartLineNum, int fixEndLineNum, boolean isInsert) {
String buggyStatements = "";
if (isInsert) {
buggyStatements = readSourceCode(prevFile, bugStartLineNum, bugEndLineNum, "");
} else {
buggyStatements = readSourceCode(prevFile, bugStartLineNum, bugEndLineNum, "-");
}
String fixedStatements = readSourceCode(revFile, fixStartLineNum, fixEndLineNum, "+");
return buggyStatements + fixedStatements;
}
private String readSourceCode(File file, int startLineNum, int endLineNum, String type) {
String sourceCode = "";
String fileContent = FileHelper.readFile(file);
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
int lineIndex = 0;
while ((line = reader.readLine()) != null) {
lineIndex ++;
if (lineIndex >= startLineNum && lineIndex <= endLineNum) {
sourceCode += type + line + "\n";
}
if (lineIndex == endLineNum) break;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sourceCode;
}
public String getAlarmTypes() {
return violationTypes;
}
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile) {
}
// @Override
// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile) {
//
// }
// public void setUselessViolations(List<Violation> uselessViolations) {
// this.uselessViolations = uselessViolations;
@@ -1,156 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.File;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
/**
* Parse fixed violations with GumTree in terms of single statement.
*
* @author kui.liu
*
*/
public class FixedViolationSingleStatementParser extends FixedViolationParser {
@Override
public void parseFixPatterns(File prevFile, File revFile, File diffentryFile) {
// GumTree results
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree(prevFile, revFile);
if (actionSets.size() > 0) {
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return;
}
// Read the positions of checked violations
for (HierarchicalActionSet actionSet : actionSets) {
// position of buggy statements
int startPosition = 0;
int endPosition = 0;
// position of fixed statements
int startPosition2 = 0;
int endPosition2 = 0;
String actionStr = actionSet.getActionString();
String astNodeType = actionSet.getAstNodeType();
if (actionStr.startsWith("INS")) {
startPosition2 = actionSet.getStartPosition();
endPosition2 = startPosition2 + actionSet.getLength();
List<Move> firstAndLastMov = getFirstAndLastMoveAction(actionSet);
if (firstAndLastMov != null) {
startPosition = firstAndLastMov.get(0).getNode().getPos();
ITree lastTree = firstAndLastMov.get(1).getNode();
endPosition = lastTree.getPos() + lastTree.getLength();
} else { // Ignore the pure insert actions without any move actions.
continue;
}
} else if (actionStr.startsWith("UPD")) {
startPosition = actionSet.getStartPosition();
endPosition = startPosition + actionSet.getLength();
Update update = (Update) actionSet.getAction();
ITree newNode = update.getNewNode();
startPosition2 = newNode.getPos();
endPosition2 = startPosition2 + newNode.getLength();
if (Checker.containsBodyBlock(astNodeType)) {
List<ITree> children = update.getNode().getChildren();
endPosition = getEndPosition(children);
List<ITree> newChildren = newNode.getChildren();
endPosition2 = getEndPosition(newChildren);
if (endPosition == 0) {
endPosition = startPosition + actionSet.getLength();
}
if (endPosition2 == 0) {
endPosition2 = startPosition2 + newNode.getLength();
}
}
} else {// DEL actions and MOV actions: we don't need these actions, as for now.
continue;
}
if (startPosition == 0 || startPosition2 == 0) {
continue;
}
// Get line numbers.
int startLine = prevUnit.getLineNumber(startPosition);
int endLine = prevUnit.getLineNumber(endPosition);
int startLine2 = revUnit.getLineNumber(startPosition2);
int endLine2 = revUnit.getLineNumber(endPosition2);
Violation violation = null;//findViolation(startLine, endLine, violations);
if (violation == null) continue;
if (endLine - startLine >= Configuration.HUNK_SIZE - 2 || endLine2 - startLine2 >= Configuration.HUNK_SIZE - 2 ) continue;
/*
* Convert the ITree of buggy code to a simple tree.
* It will be used to compute the similarity.
*/
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(actionSet);
SimpleTree simpleTree = actionSet.getSimpleTree();
if (simpleTree == null) { // Failed to get the simple tree for INS actions.
continue;
}
/**
* Select edit scripts for deep learning.
* Edit scripts will be used to mine common fix patterns.
*/
// 1. First level: AST node type.
String astEditScripts = getASTEditScriptsBreadthFirst(actionSet);
int size = astEditScripts.split(" ").length;
if (size == 1) {
continue;
}
// Source Code of patches.
String patchSourceCode = getPatchSourceCode(prevFile, revFile, startLine, endLine, startLine2, endLine2, false);
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + revFile.getName() + "\n" + patchSourceCode + "\n";
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
this.violationTypes += violation.getViolationType() + "\n";
// 2. source code: raw tokens
// String rawTokenEditScripts = getRawTokenEditScripts(actionSet);
// // 3. abstract identifiers:
// String abstractIdentifiersEditScripts = getAbstractIdentifiersEditScripts(actionSet);
// // 4. semi-source code:
// String semiSourceCodeEditScripts = getSemiSourceCodeEditScripts(actionSet);
// this.buggyTrees += Configuration.BUGGY_TREE_TOKEN + "\n" + simpleTree.toString() + "\n";
this.tokensOfSourceCode += Tokenizer.getTokensDeepFirst(simpleTree).trim() + "\n";
// this.actionSets += Configuration.BUGGY_TREE_TOKEN + "\n" + readActionSet(actionSet, "") + "\n";
// this.originalTree += Configuration.BUGGY_TREE_TOKEN + "\n" + actionSet.getOriginalTree().toString() + "\n";
}
actionSets.clear();
} else {
System.out.println(1);
}
}
protected Violation findViolation(int startLine, int endLine, List<Violation> violations) {
for (Violation violation : violations) {
int vStartLine = violation.getStartLineNum();
int vEndLine = violation.getEndLineNum();
if (!(startLine > vEndLine && endLine< vStartLine)) return violation;
}
return null;
}
}
@@ -1,20 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.io.File;
public class Message {
private String name;
private String inputPath;
private String innerPort;
public Message(String name , String inputPath, String innerPort) {
super();
this.name = name;
this.inputPath = inputPath;
this.innerPort = innerPort;
}
}
@@ -1,150 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created by anilkoyuncu on 03/04/2018.
*/
public class Producer {
private static Logger log = LoggerFactory.getLogger(Producer.class);
public static void main(String[] args) {
String inputPath;
String outputPath;
String port;
String portInner;
String pairsCSVPath;
String importScript;
String pairsCompletedPath;
String serverWait;
if (args.length > 0) {
inputPath = args[0];
port = args[1];
portInner = args[2];
serverWait = args[3];
// pairsCSVPath = args[3];
// importScript = args[4];
// pairsCompletedPath = args[3];
} else {
inputPath = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeOutput2";
outputPath = "/Users/anilkoyuncu/bugStudy/dataset/";
port = "6379";
portInner = "6380";
serverWait = "10000";
pairsCSVPath = "/Users/anilkoyuncu/bugStudy/dataset/pairs/test";
importScript = "/Users/anilkoyuncu/bugStudy/dataset/pairs/test2.sh";
pairsCompletedPath = "/Users/anilkoyuncu/bugStudy/dataset/pairs_completed";
}
calculatePairs(inputPath, port);
log.info("Calculate pairs done");
}
public static void calculatePairs(String inputPath,String port) {
File folder = new File(inputPath);
File[] listOfFiles = folder.listFiles();
Stream<File> stream = Arrays.stream(listOfFiles);
List<File> pjs = stream
.filter(x -> !x.getName().startsWith("."))
.collect(Collectors.toList());
List<File> fileToCompare = new ArrayList<>();
for (File pj : pjs) {
File[] files = pj.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("ActionSetDumps");
}
});
Collections.addAll(fileToCompare, files[0].listFiles());
}
System.out.println("a");
// compareAll(fileToCompare);
readMessageFiles(fileToCompare, port);
}
private static void readMessageFiles(List<File> folders, String port) {
List<String> treesFileNames = new ArrayList<>();
for (File target : folders) {
treesFileNames.add(target.toString());
}
log.info("Calculating pairs");
int fileCounter = 0;
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.240.5.10");
// factory.setVirtualHost("treeCompare");
factory.setPort(5672);
factory.setUsername("anil");
factory.setPassword("changeme2018");
factory.setConnectionTimeout(1000);
Connection connection = null;
Channel channel = null;
try {
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare("tree_queue", false, false, false, null);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
for (int i = 0; i < treesFileNames.size(); i++) {
for (int j = i + 1; j < treesFileNames.size(); j++) {
// do operations with jedis resource
String key = "pair_" + String.valueOf(i) + "_" + String.valueOf(j);
// String value = treesFileNames.get(i).split("GumTreeOutput2")[1] +","+treesFileNames.get(j).split("GumTreeOutput2")[1];
// jedis.set(key,value);
String message = key +","+treesFileNames.get(i).split("GumTreeOutput2")[1] + "," + treesFileNames.get(j).split("GumTreeOutput2")[1];
try {
channel.basicPublish("", "tree_queue", null, message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
log.info("Done pairs");
}
}
@@ -1,133 +0,0 @@
package edu.lu.uni.serval.FixPatternParser.violations;
import java.util.ArrayList;
import java.util.List;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
public class Violation implements Comparable<Violation> {
private String fileName = "";
private Integer startLineNum;
private int endLineNum;
private int bugStartLineNum;
private int bugEndLineNum;
private int fixStartLineNum = 0;
private int fixEndLineNum;
private String violationType;
private List<DiffEntryHunk> hunks = new ArrayList<>();
private List<HierarchicalActionSet> actionSets;
private int bugFixStartLineNum = 0; // the heuristic matched fix start line of a violation
private int bugFixEndLineNum = 0; // the heuristic matched fix end line of a violation
public Violation(Integer startLineNum, int endLineNum, String violationType) {
super();
this.startLineNum = startLineNum;
this.endLineNum = endLineNum;
this.violationType = violationType;
this.actionSets = new ArrayList<>();
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Integer getStartLineNum() {
return startLineNum;
}
public int getEndLineNum() {
return endLineNum;
}
public int getBugStartLineNum() {
return bugStartLineNum;
}
public void setBugStartLineNum(int bugStartLineNum) {
this.bugStartLineNum = bugStartLineNum;
}
public int getBugEndLineNum() {
return bugEndLineNum;
}
public void setBugEndLineNum(int bugEndLineNum) {
this.bugEndLineNum = bugEndLineNum;
}
public int getFixStartLineNum() {
return fixStartLineNum;
}
public void setFixStartLineNum(int fixStartLineNum) {
this.fixStartLineNum = fixStartLineNum;
}
public int getFixEndLineNum() {
return fixEndLineNum;
}
public void setFixEndLineNum(int fixEndLineNum) {
this.fixEndLineNum = fixEndLineNum;
}
public List<DiffEntryHunk> getHunks() {
return hunks;
}
public void setHunks(List<DiffEntryHunk> hunks) {
this.hunks = hunks;
}
public String getViolationType() {
return violationType;
}
public List<HierarchicalActionSet> getActionSets() {
return actionSets;
}
public int getBugFixStartLineNum() {
return bugFixStartLineNum;
}
public void setBugFixStartLineNum(int bugFixStartLineNum) {
this.bugFixStartLineNum = bugFixStartLineNum;
}
public int getBugFixEndLineNum() {
return bugFixEndLineNum;
}
public void setBugFixEndLineNum(int bugFixEndLineNum) {
this.bugFixEndLineNum = bugFixEndLineNum;
}
@Override
public int compareTo(Violation v) {
return this.startLineNum.compareTo(v.startLineNum);
}
@Override
public String toString() {
return this.startLineNum + " : " + this.endLineNum + " : " + this.violationType;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Violation) {
Violation v = (Violation) obj;
if (this.fileName.equals(v.fileName) && this.violationType.equals(v.violationType) && this.startLineNum == v.startLineNum && this.endLineNum == v.endLineNum) {
return true;
}
}
return false;
}
}
@@ -1,121 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
/**
* Multi-thread parser of parsing the difference between buggy code file and fixed code file.
*
* @author kui.liu
*
*/
public class AkkaParser {
private static Logger log = LoggerFactory.getLogger(AkkaParser.class);
/**
* Two parameters:
* First one: the root path of input data.
* Second one: the number of threads.
* Third one: the threshold of selecting patch hunks.
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// String inputRootPath = args[0];
int numberOfWorkers = 5; //Integer.parseInt(args[1]);
int hunkThreshold = 10;
// try {
// hunkThreshold = Integer.parseInt(args[2]);
// } catch (NumberFormatException e1) {
// hunkThreshold = 10;
// }
// Configuration.ROOT_PATH = inputRootPath;
Configuration.HUNK_SIZE = hunkThreshold;
List<String> pjList = Arrays.asList("APACHE-CAMEL","APACHE-HBASE","APACHE-HIVE","COMMONS-CODEC","COMMONS-COLLECTIONS",
"COMMONS-COMPRESS","COMMONS-CONFIGURATION","COMMONS-CRYPTO","COMMONS-CSV","COMMONS-IO","COMMONS-LANG",
"COMMONS-MATH","COMMONS-WEAVER","JBOSS-ENTESB","JBOSS-JBMETA","SPRING-AMQP","SPRING-ANDROID","SPRING-BATCH",
"SPRING-BATCHADM","SPRING-DATACMNS","SPRING-DATAGRAPH","SPRING-DATAJPA","SPRING-DATAMONGO","SPRING-DATAREDIS",
"SPRING-DATAREST","SPRING-LDAP","SPRING-MOBILE","SPRING-ROO","SPRING-SEC","SPRING-SECOAUTH","SPRING-SGF","SPRING-SHDP",
"SPRING-SOCIAL","SPRING-SOCIALFB","SPRING-SOCIALLI","SPRING-SOCIALTW","SPRING-SPR","SPRING-SWF","SPRING-SWS","WILDFLY-ELY",
"WILDFLY-SWARM","WILDFLY-WFARQ","WILDFLY-WFCORE","WILDFLY-WFLY","WILDFLY-WFMP");
for (String pj : pjList) {
String[] split = pj.split("-");
String pjPath = split[0];
String pjName = split[1];
// String rootPath = "/Volumes/data/bugStudy/";
// String DATASET_FILE_PATH = rootPath + "/dataset/GumTreeInput/" + pjName;
// String GIT_REPOSITORY_PATH = "/Users/anilkoyuncu/bugLocalizationStudy/irblsensitivity/data/" + pjPath + "/" + pjName + "/gitrepo/.git";
log.info(pjName);
// input data
log.info("Get the input data...");
final List<MessageFile> msgFiles = getMessageFiles(Configuration.GUM_TREE_INPUT +pjName +"gitrepo/");
log.info("MessageFiles: " + msgFiles.size());
// output path
final String editScriptsFilePath = Configuration.EDITSCRIPTS_FILE_PATH + pjName + "/";
final String patchesSourceCodeFilePath = Configuration.PATCH_SOURCECODE_FILE_PATH+ pjName + "/";
final String buggyTokensFilePath = Configuration.BUGGY_CODE_TOKEN_FILE_PATH+ pjName + "/";
final String editScriptSizesFilePath = Configuration.EDITSCRIPT_SIZES_FILE_PATH+ pjName + "/";
FileHelper.deleteDirectory(editScriptsFilePath);
FileHelper.deleteDirectory(patchesSourceCodeFilePath);
FileHelper.deleteDirectory(buggyTokensFilePath);
FileHelper.deleteDirectory(editScriptSizesFilePath);
ActorSystem system = null;
ActorRef parsingActor = null;
final WorkMessage msg = new WorkMessage(0, msgFiles);
try {
log.info("Akka begins...");
system = ActorSystem.create("Mining-FixPattern-System");
// parsingActor = system.actorOf(ParseFixPatternActor.props(numberOfWorkers, editScriptsFilePath,
// patchesSourceCodeFilePath, buggyTokensFilePath, editScriptSizesFilePath), "mine-fix-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
}
/**
* Get violation-related files.
*
* @param gumTreeInput
* @return
*/
public static List<MessageFile> getMessageFiles(String gumTreeInput) {
String inputPath = gumTreeInput; // prevFiles revFiles diffentryFile positionsFile
File revFilesPath = new File(inputPath + "revFiles/");
File[] revFiles = revFilesPath.listFiles(); // project folders
List<MessageFile> msgFiles = new ArrayList<>();
for (File revFile : revFiles) {
if (revFile.getName().endsWith(".java")) {
String fileName = revFile.getName();
File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file
fileName = fileName.replace(".java", ".txt");
File diffentryFile = new File(gumTreeInput + "diffentries/" + fileName); // DiffEntry file
File positionFile = new File(gumTreeInput + "positions/" + fileName); // position file
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile);
msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
}
}
return msgFiles;
}
}
@@ -1,146 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Multi-thread parser of parsing the difference between buggy code file and fixed code file.
*
* @author kui.liu
*
*/
public class AkkaParser2 {
private static Logger log = LoggerFactory.getLogger(AkkaParser2.class);
/**
* Two parameters:
* First one: the root path of input data.
* Second one: the number of threads.
* Third one: the threshold of selecting patch hunks.
* @param args
*/
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String inputRootPath;
String outputRootPath;
int numberOfWorkers;
if(args.length > 0){
inputRootPath = args[0];
outputRootPath = args[1];
numberOfWorkers = Integer.parseInt(args[2]);
}else{
inputRootPath = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeInputBug";
outputRootPath = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeOutputBug/";
numberOfWorkers = 1;
}
// try {
// hunkThreshold = Integer.parseInt(args[2]);
// } catch (NumberFormatException e1) {
// hunkThreshold = 10;
// }
// Configuration.ROOT_PATH = inputRootPath;
// log.info(Configuration.ROOT_PATH);
// Configuration.HUNK_SIZE = hunkThreshold;
// input data
// String GUM_TREE_INPUT = inputRootPath + "GumTreeInput/";
log.info("Get the input data..." + inputRootPath );
log.info("Set the output data..." + outputRootPath );
File folder = new File(inputRootPath);
File[] listOfFiles = folder.listFiles();
Stream<File> stream = Arrays.stream(listOfFiles);
List<File> folders = stream
.filter(x -> !x.getName().startsWith("."))
.collect(Collectors.toList());
for (File target : folders) {
log.info("MessageFiles: " + target.toString());
final List<MessageFile> msgFiles = getMessageFiles(target.toString() + "/");
log.info("MessageFiles: " + msgFiles.size());
// output path
String pjName = target.getName();
// output path
String GUM_TREE_OUTPUT = outputRootPath + pjName + "/";
final String editScriptsFilePath = GUM_TREE_OUTPUT + "editScripts/";
final String patchesSourceCodeFilePath =GUM_TREE_OUTPUT + "patchSourceCode/";
final String buggyTokensFilePath = GUM_TREE_OUTPUT + "tokens/";
final String editScriptSizesFilePath = GUM_TREE_OUTPUT + "editScriptSizes/";
final String alarmTypesFilePath = GUM_TREE_OUTPUT + "alarmTypes/";
FileHelper.deleteDirectory(editScriptsFilePath);
FileHelper.deleteDirectory(patchesSourceCodeFilePath);
FileHelper.deleteDirectory(buggyTokensFilePath);
FileHelper.deleteDirectory(editScriptSizesFilePath);
ActorSystem system = null;
ActorRef parsingActor = null;
final WorkMessage msg = new WorkMessage(0, msgFiles);
try {
log.info("Akka begins...");
system = ActorSystem.create("Mining-FixPattern-System");
// parsingActor = system.actorOf(ParseFixPatternActor.props(numberOfWorkers, editScriptsFilePath,
// patchesSourceCodeFilePath, buggyTokensFilePath, editScriptSizesFilePath), "mine-fix-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
}
/**
* Get violation-related files.
*
* @param gumTreeInput
* @return
*/
public static List<MessageFile> getMessageFiles(String gumTreeInput) {
String inputPath = gumTreeInput; // prevFiles revFiles diffentryFile positionsFile
File revFilesPath = new File(inputPath + "revFiles/");
File[] revFiles = revFilesPath.listFiles(); // project folders
List<MessageFile> msgFiles = new ArrayList<>();
for (File revFile : revFiles) {
// if (revFile.getName().endsWith(".c") || revFile.getName().endsWith(".h")) {
if (revFile.getName().endsWith(".java")) {
String fileName = revFile.getName();
File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file
fileName = fileName.replace(".java", ".txt");
File diffentryFile = new File(gumTreeInput + "DiffEntries/" + fileName); // DiffEntry file
File positionFile = new File(gumTreeInput + "positions/" + fileName); // position file
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile);
msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
}
// }
}
return msgFiles; //.subList(10,20);
}
}
@@ -1,15 +1,14 @@
package edu.lu.uni.serval.MultipleThreadsParser;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import akka.routing.RoundRobinPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
public class ParseFixPatternActor extends UntypedActor {
@@ -1,28 +1,17 @@
package edu.lu.uni.serval.MultipleThreadsParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import edu.lu.uni.serval.FixPatternParser.RunnableParser;
import edu.lu.uni.serval.FixPatternParser.violations.FixedViolationHunkParser;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
import java.util.concurrent.*;
public class ParseFixPatternWorker extends UntypedActor {
private static Logger log = LoggerFactory.getLogger(ParseFixPatternActor.class);
@@ -79,10 +68,6 @@ public class ParseFixPatternWorker extends UntypedActor {
File diffentryFile = msgFile.getDiffEntryFile();
// File positionFile = msgFile.getPositionFile();
/*if (revFile.getName().toLowerCase().contains("test")) {
continue;
}*/
FixedViolationHunkParser parser = new FixedViolationHunkParser();
final ExecutorService executor = Executors.newSingleThreadExecutor();
@@ -103,13 +88,7 @@ public class ParseFixPatternWorker extends UntypedActor {
String editScript = parser.getAstEditScripts();
if ("".equals(editScript)) {
// if (parser.resultType == 1) {
// nullGumTreeResults += countAlarms(positionFile, "");
// } else if (parser.resultType == 2) {
// noSourceCodeChanges += countAlarms(positionFile, "");
// } else if (parser.resultType == 3) {
// noStatementChanges += countAlarms(positionFile, "");
// }
} else {
editScripts.append(editScript);
patchesSourceCode.append(parser.getPatchesSourceCode());
@@ -118,29 +97,21 @@ public class ParseFixPatternWorker extends UntypedActor {
counter ++;
if (counter % 100 == 0) {
// FileHelper.outputToFile(editScriptsFilePath + "edistScripts_" + id + ".list", editScripts, true);
// FileHelper.outputToFile(patchesSourceCodeFilePath + "patches_" + id + ".list", patchesSourceCode, true);
// FileHelper.outputToFile(editScriptSizesFilePath + "sizes_" + id + ".list", sizes, true);
// FileHelper.outputToFile(buggyTokensFilePath + "tokens_" + id + ".list", tokens, true);
editScripts.setLength(0);
patchesSourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
log.info("Worker #" + id +" finialized parsing " + counter + " files...");
// FileHelper.outputToFile("OUTPUT/testingInfo_" + id + ".list", testingInfo, true);
testingInfo.setLength(0);
}
}
} catch (TimeoutException e) {
future.cancel(true);
// timeouts += countAlarms(positionFile, "#Timeout:");
System.err.println("#Timeout: " + revFile.getName());
} catch (InterruptedException e) {
// timeouts += countAlarms(positionFile, "#TimeInterrupted:");
System.err.println("#TimeInterrupted: " + revFile.getName());
e.printStackTrace();
} catch (ExecutionException e) {
// timeouts += countAlarms(positionFile, "#TimeAborted:");
System.err.println("#TimeAborted: " + revFile.getName());
e.printStackTrace();
} finally {
@@ -149,24 +120,13 @@ public class ParseFixPatternWorker extends UntypedActor {
}
if (sizes.length() > 0) {
// FileHelper.outputToFile(editScriptsFilePath + "editScripts_" + id + ".list", editScripts, true);
// FileHelper.outputToFile(patchesSourceCodeFilePath + "patches_" + id + ".list", patchesSourceCode, true);
// FileHelper.outputToFile(editScriptSizesFilePath + "sizes_" + id + ".list", sizes, true);
// FileHelper.outputToFile(buggyTokensFilePath + "tokens_" + id + ".list", tokens, true);
editScripts.setLength(0);
patchesSourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
// FileHelper.outputToFile("OUTPUT/testingInfo_" + id + ".list", testingInfo, true);
testingInfo.setLength(0);
}
// String statistic = "\nNullGumTreeResults: " + nullGumTreeResults + "\nNoSourceCodeChanges: " + noSourceCodeChanges +
// "\nNoStatementChanges: " + noStatementChanges + "\nNullDiffEntry: " + nullDiffEntry + "\nNullMatchedGumTreeResults: " + nullMappingGumTreeResults +
// "\nPureDeletion: " + pureDeletion + "\nLargeHunk: " + largeHunk + "\nNullSourceCode: " + nullSourceCode +
// "\nTestingInfo: " + testInfos + "\nTimeout: " + timeouts;
// FileHelper.outputToFile("OUTPUT/statistic_" + id + ".list", statistic, false);
// FileHelper.outputToFile("OUTPUT/UnfixedV_" + id + ".list", builder, false);
log.info("Worker #" + id +"finialized parsing " + counter + " files...");
log.info("Worker #" + id + " finialized the work...");
@@ -1,190 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import edu.lu.uni.serval.FixPatternParser.RunnableParser;
import edu.lu.uni.serval.FixPatternParser.violations.FixedViolationHunkParser;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class SingleThreadParser {
public static void main(String[] args) {
// output path
final String editScriptsFilePath = Configuration.EDITSCRIPTS_FILE_PATH;
final String patchesSourceCodeFilePath = Configuration.PATCH_SOURCECODE_FILE_PATH;
final String buggyTokensFilePath = Configuration.BUGGY_CODE_TOKEN_FILE_PATH;
final String editScriptSizesFilePath = Configuration.EDITSCRIPT_SIZES_FILE_PATH;
final String alarmTypesFilePath = Configuration.ALARM_TYPES_FILE_PATH;
FileHelper.deleteDirectory(editScriptsFilePath);
FileHelper.deleteDirectory(patchesSourceCodeFilePath);
FileHelper.deleteDirectory(buggyTokensFilePath);
FileHelper.deleteDirectory(editScriptSizesFilePath);
FileHelper.deleteDirectory(alarmTypesFilePath);
final List<MessageFile> files = AkkaParser.getMessageFiles(Configuration.GUM_TREE_INPUT);
StringBuilder editScripts = new StringBuilder();
StringBuilder patchesSourceCode = new StringBuilder();
StringBuilder sizes = new StringBuilder();
StringBuilder tokens = new StringBuilder();
StringBuilder alarmTypes = new StringBuilder();
StringBuilder testingInfo = new StringBuilder();
int counter = 0;
int testViolations = 0;
int nullGumTreeResults = 0;
int noSourceCodeChanges = 0;
int noStatementChanges = 0;
int nullDiffEntry = 0;
int nullMappingGumTreeResults = 0;
int pureDeletion = 0;
int largeHunk = 0;
int nullSourceCode = 0;
int testInfos = 0;
int timeouts = 0;
StringBuilder builder = new StringBuilder();
for (MessageFile msgFile : files) {
File revFile = msgFile.getRevFile();
File prevFile = msgFile.getPrevFile();
File diffentryFile = msgFile.getDiffEntryFile();
File positionFile = msgFile.getPositionFile();
if (revFile.getName().toLowerCase().contains("test#") || revFile.getName().toLowerCase().contains("tests#")) {
testViolations += countAlarms(positionFile, "#TestViolation:");
continue;
}
FixedViolationHunkParser parser = new FixedViolationHunkParser();
// parser.setUselessViolations(uselessViolations);
final ExecutorService executor = Executors.newSingleThreadExecutor();
// schedule the work
final Future<?> future = executor.submit(new RunnableParser(prevFile, revFile, diffentryFile, parser));
try {
// wait for task to complete
future.get(Configuration.SECONDS_TO_WAIT, TimeUnit.SECONDS);
nullDiffEntry += parser.nullMatchedDiffEntry;
nullMappingGumTreeResults += parser.nullMappingGumTreeResult;
pureDeletion += parser.pureDeletions;
largeHunk += parser.largeHunk;
nullSourceCode += parser.nullSourceCode;
testInfos += parser.testInfos;
testingInfo.append(parser.testingInfo);
builder.append(parser.unfixedViolations);
String editScript = parser.getAstEditScripts();
if ("".equals(editScript)) {
if (parser.resultType == 1) {
nullGumTreeResults += countAlarms(positionFile, "");
} else if (parser.resultType == 2) {
noSourceCodeChanges += countAlarms(positionFile, "");
} else if (parser.resultType == 3) {
noStatementChanges += countAlarms(positionFile, "");
// } else if (parser.resultType == 4) {
}
} else {
editScripts.append(editScript);
patchesSourceCode.append(parser.getPatchesSourceCode());
sizes.append(parser.getSizes());
alarmTypes.append(parser.getAlarmTypes());
tokens.append(parser.getTokensOfSourceCode());
counter ++;
if (counter % 5000 == 0) {
FileHelper.outputToFile(editScriptsFilePath + "edistScripts.list", editScripts, true);
FileHelper.outputToFile(patchesSourceCodeFilePath + "patches.list", patchesSourceCode, true);
FileHelper.outputToFile(editScriptSizesFilePath + "sizes.list", sizes, true);
FileHelper.outputToFile(buggyTokensFilePath + "tokens.list", tokens, true);
editScripts.setLength(0);
patchesSourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
FileHelper.outputToFile(alarmTypesFilePath + "alarmTypes.list", alarmTypes, true);
alarmTypes.setLength(0);
FileHelper.outputToFile("OUTPUT/testingInfo.list", testingInfo, true);
testingInfo.setLength(0);
}
}
} catch (TimeoutException e) {
// err.println("task timed out");
future.cancel(true);
timeouts += countAlarms(positionFile, "#Timeout:");
// System.err.println("#Timeout: " + revFile.getName());
} catch (InterruptedException e) {
timeouts += countAlarms(positionFile, "#TimeInterrupted:");
// err.println("task interrupted");
// System.err.println("#TimeInterrupted: " + revFile.getName());
} catch (ExecutionException e) {
timeouts += countAlarms(positionFile, "#TimeAborted:");
// err.println("task aborted");
// System.err.println("#TimeAborted: " + revFile.getName());
} finally {
executor.shutdownNow();
}
}
if (sizes.length() > 0) {
FileHelper.outputToFile(editScriptsFilePath + "edistScripts.list", editScripts, true);
FileHelper.outputToFile(patchesSourceCodeFilePath + "patches.list", patchesSourceCode, true);
FileHelper.outputToFile(editScriptSizesFilePath + "sizes.list", sizes, true);
FileHelper.outputToFile(buggyTokensFilePath + "tokens.list", tokens, true);
editScripts.setLength(0);
patchesSourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
FileHelper.outputToFile(alarmTypesFilePath + "alarmTypes.list", alarmTypes, true);
alarmTypes.setLength(0);
FileHelper.outputToFile("OUTPUT/testingInfo.list", testingInfo, true);
testingInfo.setLength(0);
}
String statistic = "TestViolations: " + testViolations + "\nNullGumTreeResults: " + nullGumTreeResults + "\nNoSourceCodeChanges: " + noSourceCodeChanges +
"\nNoStatementChanges: " + noStatementChanges + "\nNullDiffEntry: " + nullDiffEntry + "\nNullMatchedGumTreeResults: " + nullMappingGumTreeResults +
"\nPureDeletion: " + pureDeletion + "\nLargeHunk: " + largeHunk + "\nNullSourceCode: " + nullSourceCode +
"\nTestingInfo: " + testInfos + "\nTimeout: " + timeouts;
FileHelper.outputToFile("OUTPUT/statistic.list", statistic, false);
FileHelper.outputToFile("OUTPUT/UnfixedV.list", builder, false);
}
private static int countAlarms(File positionFile, String type) {//, List<Violation> uselessViolations) {
int counter = 0;
String content = FileHelper.readFile(positionFile);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] elements = line.split(":");
Violation v = new Violation(Integer.parseInt(elements[1]), Integer.parseInt(elements[2]), elements[0]);
String fileName = positionFile.getName().replace(".txt", ".java");
v.setFileName(fileName);
counter ++;
if (!"".equals(type)) {
System.err.println(type + fileName + ":" + elements[1] + ":" + elements[2] + ":" + elements[0]);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return counter;
}
}
@@ -1,214 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
/**
* Multi-thread parser of parsing the difference between buggy code file and fixed code file.
*
* @author kui.liu
*
*/
public class AkkaMatcher {
private static Logger log = LoggerFactory.getLogger(AkkaMatcher.class);
public static void main(String[] args) {
List<Double[]> extractedFeatures = readStringList(Configuration.ROOT_PATH + "TestData/2_CNNinput.csv");
int size = extractedFeatures.size();
List<Double[]> trainingFeatures = extractedFeatures.subList(0, size - 178);
List<Double[]> bugFeatures = extractedFeatures.subList(size - 178, size);
for (int index = 0; index < 178; index ++) {
// AkkaMatcher computor = new AkkaMatcher();
// computor.matchFixPatterns(bugFeatures.get(index), index, trainingFeatures);
Map<Integer, Double> similarities = new HashMap<>();
for (int i = 0; i < size - 178; i ++) {
Double similarity = Math.abs(computeSimilarity(bugFeatures.get(index), trainingFeatures.get(i)));
similarities.put(i + 1, similarity);
}
MapSorter<Integer, Double> mapSorter = new MapSorter<Integer, Double>();
Map<Integer, Double> sortedSimilarities = mapSorter.sortByValueDescending(similarities);
List<Integer> similarityList = new ArrayList<>();
double similarity = 0;
int num = 0;
for (Map.Entry<Integer, Double> entry : sortedSimilarities.entrySet()) {
if (entry.getValue().equals(Double.NaN)) {
continue;
}
if (entry.getValue() == similarity) {
continue;
}
similarityList.add(entry.getKey());
similarity = entry.getValue();
if (++ num % 100 == 0) {
break;
}
}
outputMatchedPatterns(similarityList, index + 1);
}
}
private static Double computeSimilarity(Double[] feature, Double[] trainingFeature) {
Double similarity = DistanceCalculator.cosineSimilarityDistance(trainingFeature, feature);
return similarity;
}
private static void outputMatchedPatterns(List<Integer> similarityList, int bugId) {
String outputFile = Configuration.ROOT_PATH + "TestData/MatchedFixPatterns/Bug_" + bugId + ".list";
StringBuilder builder = new StringBuilder("BugId: " + bugId + "\n\n\n");
for (int i = 0, size = similarityList.size(); i < size; i ++) {
int patternPosition = similarityList.get(i);
builder.append(readPattern(patternPosition));
}
FileHelper.outputToFile(outputFile, builder, false);
}
private static String readPattern(int patternPosition) {
String patternInfo = "";
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(Configuration.SELECTED_PATCHES_SOURE_CODE_FILE);
scanner = new Scanner(fis);
int index = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("PATCH###")) {
if (patternPosition == index) {
break;
}
index ++;
patternInfo = "";
}
patternInfo += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return patternInfo;
}
@SuppressWarnings("deprecation")
public void matchFixPatterns(Double[] bugFeature, int bugIndex, List<Double[]> trainingFeatures) {
ActorSystem system = null;
ActorRef parsingActor = null;
int numberOfWorkers = 100;
final WorkMessage msg = new WorkMessage(bugIndex, bugFeature, trainingFeatures);
try {
log.info("Akka begins...");
system = ActorSystem.create("Matching-FixPattern-System");
parsingActor = system.actorOf(MatchFixPatternActor.props(numberOfWorkers), "match-fix-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
/**
* Get bug commit-related files.
*
* @return
*/
public static List<MessageFile> getMessageFiles() {
String inputPath = Configuration.GUM_TREE_INPUT; //DiffEntries prevFiles revFiles
File inputFileDirector = new File(inputPath);
File[] files = inputFileDirector.listFiles(); // project folders
log.info("Projects: " + files.length);
List<MessageFile> msgFiles = new ArrayList<>();
for (File file : files) {
if (!file.isDirectory()) continue;
// if (!(file.getName().startsWith("k") || file.getName().startsWith("l"))) continue;
if (file.getName().startsWith("a") || file.getName().startsWith("b")
|| file.getName().startsWith("c") || file.getName().startsWith("d")
|| file.getName().startsWith("e") || file.getName().startsWith("f")
|| file.getName().startsWith("g") || file.getName().startsWith("h")
||file.getName().startsWith("h") || file.getName().startsWith("i")
|| file.getName().startsWith("k") || file.getName().startsWith("l")
|| file.getName().startsWith("j") || file.getName().startsWith("t")) continue;
// if (!file.getName().startsWith("j")) continue;
log.info("Project name: " + file.getName());
String projectFolder = file.getPath();
File revFileFolder = new File(projectFolder + "/revFiles/");// revised file folder
File[] revFiles = revFileFolder.listFiles();
for (File revFile : revFiles) {
if (revFile.getName().endsWith(".java")) {
File prevFile = new File(projectFolder + "/prevFiles/prev_" + revFile.getName());// previous file
File diffentryFile = new File(projectFolder + "/DiffEntries/" + revFile.getName().replace(".java", ".txt")); // DiffEntry file
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile);
msgFiles.add(msgFile);
}
}
}
return msgFiles;
}
public static List<Double[]> readStringList(String inputFile) {
List<Double[]> list = new ArrayList<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(inputFile);
scanner = new Scanner(fis);
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
Double[] features = doubleParseFeature(line);
list.add(features);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
private static Double[] doubleParseFeature(String feature) {
String[] features = feature.split(", ");
int length = features.length;
Double[] doubleFeatures = new Double[length];
for (int i = 0; i < length; i ++) {
doubleFeatures[i] = Double.parseDouble(features[i]);
}
return doubleFeatures;
}
}
@@ -1,68 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
public class DistanceCalculator {
public static double calculateDistance(DistanceFunction distanceType, Double[] targetPoint, Double[] selfPoint) {
double distance = 0;
switch (distanceType) {
case EUCLIDEAN:
distance = DistanceCalculator.euclideanDistance(targetPoint, selfPoint);
break;
case COSINESIMILARITY:
distance = DistanceCalculator.cosineSimilarityDistance(targetPoint, selfPoint);
break;
case MANHATTAN:
distance = DistanceCalculator.manhattanDistance(targetPoint, selfPoint);
break;
default:
distance = DistanceCalculator.euclideanDistance(targetPoint, selfPoint);
break;
}
return distance;
}
public static double euclideanDistance(Double[] targetPoint, Double[] selfPoint) {
double sum = 0.0;
for (int i = 0, length = targetPoint.length; i < length; i++) {
double diff = targetPoint[i] - selfPoint[i];
sum += diff * diff;
}
return Math.sqrt(sum);
}
public static Double cosineSimilarityDistance(Double[] targetPoint, Double[] selfPoint) {
Double sim = 0.0d;
int length = targetPoint.length;
double dot = 0.0d;
double mag1 = 0.0d;
double mag2 = 0.0d;
for (int i = 0; i < length; i ++) {
dot += targetPoint[i] * selfPoint[i];
mag1 += Math.pow(targetPoint[i], 2);
mag2 += Math.pow(selfPoint[i], 2);
}
sim = dot / (Math.sqrt(mag1) * Math.sqrt(mag2));
return sim;
}
public static Double manhattanDistance(Double[] targetPoint, Double[] selfPoint) {
double result = 0.0;
for (int i = 0; i < targetPoint.length; i++) {
result += Math.abs(selfPoint[i] - targetPoint[i]);
}
return result;
}
public static Double minkowskiDistance(Double[] targetPoint, Double[] selfPoint) {
return null;
}
public static Double jaccardSimilarity(Double[] targetPoint, Double[] selfPoint) {
return null;
}
}
@@ -1,5 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
public enum DistanceFunction {
EUCLIDEAN, COSINESIMILARITY, MANHATTAN
}
@@ -1,172 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import akka.routing.RoundRobinPool;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
import edu.lu.uni.serval.utils.MapSorter;
public class MatchFixPatternActor extends UntypedActor {
private static Logger logger = LoggerFactory.getLogger(MatchFixPatternActor.class);
private ActorRef mineRouter;
private final int numberOfWorkers;
private int counter = 0;
public MatchFixPatternActor(int numberOfWorkers) {
mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers)
.props(MatchFixPatternWorker.props()), "match-fix-pattern-router");
this.numberOfWorkers = numberOfWorkers;
}
public static Props props(final int numberOfWorkers) {
return Props.create(new Creator<MatchFixPatternActor>() {
private static final long serialVersionUID = 9207427376110704705L;
@Override
public MatchFixPatternActor create() throws Exception {
return new MatchFixPatternActor(numberOfWorkers);
}
});
}
@SuppressWarnings("deprecation")
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
WorkMessage msg = (WorkMessage) message;
List<Double[]> trainingFeatures = msg.getTrainingFeatures();
int size = trainingFeatures.size();
int average = size / numberOfWorkers;
this.bugId = msg.getId();
Double[] bugFeature = msg.getBugFeature();
for (int i = 0; i < numberOfWorkers; i ++) {
int fromIndex = i * average;
int toIndex = (i + 1) * average;
if (i == numberOfWorkers - 1) {
toIndex = size;
}
List<Double[]> subTrainingFeatures = new ArrayList<>();
subTrainingFeatures.addAll(trainingFeatures.subList(fromIndex, toIndex));
final WorkMessage workMsg = new WorkMessage(bugId, bugFeature, subTrainingFeatures);
workMsg.setNum(i + 1);
mineRouter.tell(workMsg, getSelf());
logger.info("Assign a task to worker #" + (i + 1) + "...");
}
} else if (message instanceof ReturnMessage) {
counter ++;
logger.info(counter + " workers finished their work...");
ReturnMessage rMsg = (ReturnMessage) message;
returnMessages.add(rMsg);
if (counter >= numberOfWorkers) {
ListSorter<ReturnMessage> sorter = new ListSorter<ReturnMessage>(returnMessages);
returnMessages = sorter.sortAscending();
Map<Integer, Double> similarities = new HashMap<>();
int index = 0;
for (int i = 0; i < numberOfWorkers; i ++) {
ReturnMessage returnMessage = returnMessages.get(i);
List<Double> similarity = returnMessage.getSimilarities();
for (int j = 0, size = similarity.size(); j < size; j ++) {
index ++;
similarities.put(index, similarity.get(j));
}
}
MapSorter<Integer, Double> mapSorter = new MapSorter<Integer, Double>();
Map<Integer, Double> sortedSimilarities = mapSorter.sortByValueDescending(similarities);
List<Integer> similarityList = new ArrayList<>();
double similarity = 0;
int num = 0;
for (Map.Entry<Integer, Double> entry : sortedSimilarities.entrySet()) {
if (entry.getValue() == similarity) {
continue;
}
similarityList.add(entry.getKey());
similarity = entry.getValue();
if (++ num % 100 == 0) {
break;
}
}
outputMatchedPatterns(similarityList);
logger.info("All workers finished their work...");
this.getContext().stop(mineRouter);
this.getContext().stop(getSelf());
this.getContext().system().shutdown();
}
} else {
unhandled(message);
}
}
private void outputMatchedPatterns(List<Integer> similarityList) {
String outputFile = Configuration.ROOT_PATH + "TestData/MatchedFixPatterns/Bug_" + bugId + ".list";
StringBuilder builder = new StringBuilder("BugId: " + bugId + "\n\n\n");
for (int i = 0, size = similarityList.size(); i < size; i ++) {
int patternPosition = similarityList.get(i);
builder.append(readPattern(patternPosition));
}
FileHelper.outputToFile(outputFile, builder, false);
}
private String readPattern(int patternPosition) {
String patternInfo = "";
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(Configuration.SELECTED_PATCHES_SOURE_CODE_FILE);
scanner = new Scanner(fis);
int index = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.equals("PATCH###")) {
if (patternPosition == index) {
break;
}
index ++;
patternInfo = "";
}
patternInfo += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return patternInfo;
}
private int bugId;
private List<ReturnMessage> returnMessages = new ArrayList<>();
}
@@ -1,60 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
public class MatchFixPatternWorker extends UntypedActor {
private static Logger log = LoggerFactory.getLogger(MatchFixPatternActor.class);
public MatchFixPatternWorker() {
}
public static Props props() {
return Props.create(new Creator<MatchFixPatternWorker>() {
private static final long serialVersionUID = -7615153844097275009L;
@Override
public MatchFixPatternWorker create() throws Exception {
return new MatchFixPatternWorker();
}
});
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
WorkMessage msg = (WorkMessage) message;
List<Double[]> trainingFeatures = msg.getTrainingFeatures();
Double[] bugFeature = msg.getBugFeature();
int bugID = msg.getId();
int workNum = msg.getNum();
List<Double> similarities = new ArrayList<>();
for (int i = 0, size = trainingFeatures.size(); i < size; i ++) {
Double similarity = Math.abs(computeSimilarity(bugFeature, trainingFeatures.get(i)));
similarities.add(similarity);
}
final ReturnMessage rMsg = new ReturnMessage(bugID, workNum, similarities);
log.info("Worker #" + workNum + " finished the work...");
this.getSender().tell(rMsg, getSelf());
} else {
unhandled(message);
}
}
private Double computeSimilarity(Double[] feature, Double[] trainingFeature) {
Double similarity = DistanceCalculator.cosineSimilarityDistance(trainingFeature, feature);
return similarity;
}
}
@@ -1,35 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.List;
public class ReturnMessage implements Comparable<ReturnMessage>{
private int bugId;
private Integer workerId;
private List<Double> similarities;
public ReturnMessage(int bugId, int workerId, List<Double> similarities) {
super();
this.bugId = bugId;
this.workerId = workerId;
this.similarities = similarities;
}
public int getBugId() {
return bugId;
}
public int getWorkerId() {
return workerId;
}
public List<Double> getSimilarities() {
return similarities;
}
@Override
public int compareTo(ReturnMessage o) {
return this.workerId.compareTo(o.workerId);
}
}
@@ -1,39 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser2;
import java.util.List;
public class WorkMessage {
private int id;
private Double[] bugFeature;
private List<Double[]> trainingFeatures;
private int num;
public WorkMessage(int id, Double[] bugFeature, List<Double[]> trainingFeatures) {
super();
this.id = id;
this.bugFeature = bugFeature;
this.trainingFeatures = trainingFeatures;
}
public int getId() {
return id;
}
public Double[] getBugFeature() {
return bugFeature;
}
public List<Double[]> getTrainingFeatures() {
return trainingFeatures;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
@@ -1,90 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser3;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.MultipleThreadsParser.WorkMessage;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
/**
* Multi-thread parser of parsing source code of unfixed violations.
*
* @author kui.liu
*
*/
public class AkkaParser {
private static Logger log = LoggerFactory.getLogger(AkkaParser.class);
@SuppressWarnings("deprecation")
public static void main(String[] args) {
String violationType = Configuration.GUM_TREE_INPUT + "UnfixedViolations/";
File file = new File(violationType);
File[] violationTypes = file.listFiles();
for (File violationT : violationTypes) {
if (violationT.isDirectory()) {
violationType = violationT.getName();
if (//violationType.equals("SF_SWITCH_NO_DEFAULT") ||
// violationType.equals("SE_NO_SERIALVERSIONID") ||
violationType.equals("DM_DEFAULT_ENCODING")) {
// input data
log.info("Get the input data...");
final List<MessageFile> msgFiles = getMessageFiles(Configuration.GUM_TREE_INPUT, violationType);
log.info("MessageFiles: " + msgFiles.size());
// output path
final String sourceCodeFilesPath = Configuration.ROOT_PATH + "UnfixedViolations_RQ3/" + violationType + "/";
FileHelper.deleteDirectory(sourceCodeFilesPath);
ActorSystem system = null;
ActorRef parsingActor = null;
int numberOfWorkers = 200;
final WorkMessage msg = new WorkMessage(0, msgFiles);
try {
log.info("Akka begins...");
system = ActorSystem.create("Mining-Pattern-System-" + violationType);
parsingActor = system.actorOf(ParseFixPatternActor.props(numberOfWorkers, sourceCodeFilesPath, violationType), "mine-pattern-actor-" + violationType);
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
}
}
}
/**
* Get violation-related files.
*
* @param gumTreeInput
* @return
*/
public static List<MessageFile> getMessageFiles(String inputPath, String violationType) {
File sourceCodeFilesPath = new File(inputPath + "UnfixedViolations/" + violationType + "/");
File[] sourceCodeFiles = sourceCodeFilesPath.listFiles(); // project folders
List<MessageFile> msgFiles = new ArrayList<>();
for (File sourceCodeFile : sourceCodeFiles) {
if (sourceCodeFile.getName().endsWith(".java")) {
String fileName = sourceCodeFile.getName();
fileName = fileName.substring(8).replace(".java", ".txt");
File positionFile = new File(inputPath + "UnFV_positions/" + violationType + "/" + fileName); // position file
MessageFile msgFile = new MessageFile(null, sourceCodeFile, null);
msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
}
}
return msgFiles;
}
}
@@ -1,78 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser3;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.MultipleThreadsParser.WorkMessage;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
/**
* Multi-thread parser of parsing source code of fixed violations.
*
* @author kui.liu
*
*/
public class AkkaParser2 {
private static Logger log = LoggerFactory.getLogger(AkkaParser2.class);
@SuppressWarnings("deprecation")
public static void main(String[] args) {
// input data
log.info("Get the input data...");
final List<MessageFile> msgFiles = getMessageFiles(Configuration.GUM_TREE_INPUT);
log.info("MessageFiles: " + msgFiles.size());
// output path
final String sourceCodeFilesPath = Configuration.ROOT_PATH + "fixedViolations/";
FileHelper.deleteDirectory(sourceCodeFilesPath);
ActorSystem system = null;
ActorRef parsingActor = null;
int numberOfWorkers = 431;
final WorkMessage msg = new WorkMessage(0, msgFiles);
try {
log.info("Akka begins...");
system = ActorSystem.create("Mining-Pattern-System");
parsingActor = system.actorOf(ParseFixPatternActor.props(numberOfWorkers, sourceCodeFilesPath, "Type"), "mine-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
system.shutdown();
e.printStackTrace();
}
}
/**
* Get violation-related files.
*
* @param gumTreeInput
* @return
*/
public static List<MessageFile> getMessageFiles(String inputPath) {
File sourceCodeFilesPath = new File(inputPath + "prevFiles/");
File[] sourceCodeFiles = sourceCodeFilesPath.listFiles();
List<MessageFile> msgFiles = new ArrayList<>();
for (File sourceCodeFile : sourceCodeFiles) {
if (sourceCodeFile.getName().endsWith(".java")) {
String fileName = sourceCodeFile.getName();
fileName = fileName.substring(5).replace(".java", ".txt");
File positionFile = new File(inputPath + "positions/" + fileName); // position file
MessageFile msgFile = new MessageFile(null, sourceCodeFile, null);
msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
}
}
return msgFiles;
}
}
@@ -1,80 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser3;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import akka.routing.RoundRobinPool;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.MultipleThreadsParser.WorkMessage;
public class ParseFixPatternActor extends UntypedActor {
private static Logger logger = LoggerFactory.getLogger(ParseFixPatternActor.class);
private ActorRef mineRouter;
private final int numberOfWorkers;
private int counter = 0;
public ParseFixPatternActor(int numberOfWorkers, String sourceCodeFilesPath, String vType) {
mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers)
.props(ParseFixPatternWorker.props(sourceCodeFilesPath)), "mine-pattern-router-" + vType);
this.numberOfWorkers = numberOfWorkers;
}
public static Props props(final int numberOfWorkers, final String sourceCodeFilesPath, final String vType) {
return Props.create(new Creator<ParseFixPatternActor>() {
private static final long serialVersionUID = 9207427376110704705L;
@Override
public ParseFixPatternActor create() throws Exception {
return new ParseFixPatternActor(numberOfWorkers, sourceCodeFilesPath, vType);
}
});
}
@SuppressWarnings("deprecation")
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
List<MessageFile> files = ((WorkMessage) message).getMsgFiles();
int size = files.size();
int average = size / numberOfWorkers;
for (int i = 0; i < numberOfWorkers; i ++) {
int fromIndex = i * average;
int toIndex = (i + 1) * average;
if (i == numberOfWorkers - 1) {
toIndex = size;
}
List<MessageFile> filesOfWorkers = new ArrayList<>();
filesOfWorkers.addAll(files.subList(fromIndex, toIndex));
final WorkMessage workMsg = new WorkMessage(i + 1, filesOfWorkers);
mineRouter.tell(workMsg, getSelf());
logger.info("Assign a task to worker #" + (i + 1) + "...");
}
} else if ("STOP".equals(message.toString())) {
counter ++;
logger.info(counter + " workers finished their work...");
if (counter >= numberOfWorkers) {
logger.info("All workers finished their work...");
this.getContext().stop(mineRouter);
this.getContext().stop(getSelf());
this.getContext().system().shutdown();
}
} else {
unhandled(message);
}
}
}
@@ -1,122 +0,0 @@
package edu.lu.uni.serval.MultipleThreadsParser3;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.japi.Creator;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.MultipleThreadsParser.WorkMessage;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.violation.code.parser.ViolationCodeParser;
public class ParseFixPatternWorker extends UntypedActor {
private static Logger log = LoggerFactory.getLogger(ParseFixPatternActor.class);
private String sourceCodeFilesPath;
public ParseFixPatternWorker(String sourceCodeFilesPath) {
this.sourceCodeFilesPath = sourceCodeFilesPath;
}
public static Props props(final String sourceCodeFilesPath) {
return Props.create(new Creator<ParseFixPatternWorker>() {
private static final long serialVersionUID = -7615153844097275009L;
@Override
public ParseFixPatternWorker create() throws Exception {
return new ParseFixPatternWorker(sourceCodeFilesPath);
}
});
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof WorkMessage) {
WorkMessage msg = (WorkMessage) message;
List<MessageFile> files = msg.getMsgFiles();
StringBuilder sourceCode = new StringBuilder();
StringBuilder sizes = new StringBuilder();
StringBuilder tokens = new StringBuilder();
List<String> violationTypes = readTypes();
int id = msg.getId();
int counter = 0;
for (MessageFile msgFile : files) {
File prevFile = msgFile.getPrevFile();
File positionFile = msgFile.getPositionFile();
if (prevFile.getName().toLowerCase().contains("test")) {
continue;
}
ViolationCodeParser parser = new ViolationCodeParser();
parser.parse(prevFile, positionFile);
parser.setTypes(violationTypes);
String sourceCodeStr = parser.sourceCode;
if ("".equals(sourceCodeStr)) {
} else {
sourceCode.append(sourceCodeStr);
sizes.append(parser.sizes);
tokens.append(parser.tokens);
counter ++;
if (counter % 100 == 0) {
FileHelper.outputToFile(sourceCodeFilesPath + "SourceCode/worker_" + id + ".list", sourceCode, true);
FileHelper.outputToFile(sourceCodeFilesPath + "Sizes/worker_" + id + ".list", sizes, true);
FileHelper.outputToFile(sourceCodeFilesPath + "Tokens/worker_" + id + ".list", tokens, true);
sourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
}
}
}
if (sizes.length() > 0) {
FileHelper.outputToFile(sourceCodeFilesPath + "SourceCode/worker_" + id + ".list", sourceCode, true);
FileHelper.outputToFile(sourceCodeFilesPath + "Sizes/worker_" + id + ".list", sizes, true);
FileHelper.outputToFile(sourceCodeFilesPath + "Tokens/worker_" + id + ".list", tokens, true);
sourceCode.setLength(0);
sizes.setLength(0);
tokens.setLength(0);
}
log.info("Worker #" + id +"Finish of parsing " + counter + " files...");
log.info("Worker #" + id + " finished the work...");
this.getSender().tell("STOP", getSelf());
} else {
unhandled(message);
}
}
private List<String> readTypes() {
String fileName = Configuration.ROOT_PATH + "fixedViolations/types.list";
String content = FileHelper.readFile(fileName);
List<String> types = new ArrayList<>();
BufferedReader reader = new BufferedReader(new StringReader(content));
try {
String line = null;
while ((line = reader.readLine()) != null) {
types.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
// System.err.println(types.size());
return types;
}
}
@@ -1,47 +0,0 @@
package edu.lu.uni.serval;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
public class TreeToString {
private List<String> strList = new ArrayList<>();
public String toString(ITree tree) {
List<ITree> children = tree.getChildren();
String str = ASTNodeMap.map.get(tree.getType());
if (strList.size() == 0) {
strList.add(str);
for (ITree child : children) {
TreeToString t2s = new TreeToString();
t2s.toString(child);
List<String> strList1 = t2s.strList;
for (String str1 : strList1) {
strList.add("---" + str1);
}
}
} else {
strList.clear();
strList.add(str);
for (ITree child : children) {
TreeToString t2s = new TreeToString();
t2s.toString(child);
List<String> strList1 = t2s.strList;
for (String str1 : strList1) {
strList.add("---" + str1);
}
}
}
str = "";
for (String str1 : strList) {
str += str1 + "\n";
}
return str;
}
}
@@ -4,88 +4,4 @@ public class Configuration {
public static final long SECONDS_TO_WAIT = 900L;
// public static String ROOT_PATH = "/Volumes/data/bugStudy/dataset/"; // The root path of all output data.
public static String ROOT_PATH = "/Users/anilkoyuncu/bugStudy/dataset/"; // The root path of all output data.
public static int HUNK_SIZE = 10; // The limitation of source code lines of each DiffEntry, which will be selected as training data.
public static final String BUGGY_TREE_SIGNAL = "BUGGY_TREE###"; // The starting signal of the tree of buggy source code .
public static final String PATCH_SIGNAL = "PATCH###"; // Th starting signal of each patch.
// input path of GumTree. (i.e., Fix patterns parser)
public static final String GUM_TREE_INPUT = ROOT_PATH + "GumTreeInput/";// Buggy version file VS. Fixing version file, (DiffEntry File)
// the output path of GumTree results.
public static final String GUM_TREE_OUTPUT = ROOT_PATH + "GumTreeResults/";
public static final String EDITSCRIPTS_FILE_PATH = GUM_TREE_OUTPUT + "editScripts/";
public static final String PATCH_SOURCECODE_FILE_PATH = GUM_TREE_OUTPUT + "sourceCode/";
public static final String BUGGYTREE_FILE_PATH = GUM_TREE_OUTPUT + "buggyTrees/";
public static final String BUGGY_CODE_TOKEN_FILE_PATH = GUM_TREE_OUTPUT + "tokens/";
public static final String EDITSCRIPT_SIZES_FILE_PATH = GUM_TREE_OUTPUT + "editScriptSizes/";
public static final String ALARM_TYPES_FILE_PATH = GUM_TREE_OUTPUT + "alarmTypes/";
public static final String EDITSCRIPTS_FILE = GUM_TREE_OUTPUT + "editScripts.list";
public static final String PATCH_SOURCECODE_FILE = GUM_TREE_OUTPUT + "patchSourceCode.list";
public static final String BUGGYTREES_FILE = GUM_TREE_OUTPUT + "buggyTrees.list";
public static final String BUGGY_CODE_TOKENS_FILE = GUM_TREE_OUTPUT + "tokens.list";
public static final String EDITSCRIPT_SIZES_FILE = GUM_TREE_OUTPUT + "editScriptSizes.csv";
public static final String ALARM_TYPES_FILE = GUM_TREE_OUTPUT + "alarmTypes.list";
public static final int VECTOR_SIZE_OF_EMBEDED_TOKEN1 = 100; // tokens of edit scripts.
public static final int VECTOR_SIZE_OF_EMBEDED_TOKEN2 = 200; // tokens of source code
// the input path of fix patterns mining.
private static final String MINING_INPUT = ROOT_PATH + "MiningInput/";
public static final String MAX_TOKEN_VECTORS_SIZE_OF_EDIT_SCRIPTS = MINING_INPUT + "/MaxTokenVectorSizeOfEditScripts.list"; // The max size of edit scripts: upper limitation of max size.
public static final String MAX_TOKEN_VECTORS_SIZE_OF_SOURCE_CODE = MINING_INPUT + "/MaxTokenVectorSizeOfBuggySourceCode.list"; // The max size of all buggy source code token vectors.
// the input path of token embedding.
public static final String EMBEDDING_INPUT = MINING_INPUT + "Embedding/";
public static final String SELECTED_PATCHES_SOURE_CODE_FILE = EMBEDDING_INPUT + "patchSourceCode.list";// Selected patches.
public static final String SELECTED_BUGGY_TREE_FILE = EMBEDDING_INPUT + "buggyTrees.list";
public static final String SELECTED_BUGGY_TOKEN_FILE = EMBEDDING_INPUT + "tokens.list"; // Selected token vectors of buggy source code.
public static final String SELECTED_EDITSCRIPTES_FILE = EMBEDDING_INPUT + "editScripts.list"; // Selected edit script vectors.
public static final String SELECTED_ALARM_TYPES_FILE = EMBEDDING_INPUT + "alarmTypes.list"; // Selected edit script vectors.
// the input path of feature learning.
public static final String FEATURE_LEARNING_INPUT = MINING_INPUT + "FeatureLearning/";
public static final String EMBEDDED_EDIT_SCRIPT_TOKENS = FEATURE_LEARNING_INPUT + "embeddedEditScriptTokens.list"; // All embedded tokens of selected edit scripts.
public static final String VECTORIED_EDIT_SCRIPTS = FEATURE_LEARNING_INPUT + "vectorizedEditScripts.csv"; // Embedded and vectorized edit script vectors.
// the input path of clustering.
public static final String EXTRACTED_FEATURES = MINING_INPUT + "ExtractedFeatures/"; // Extracted features of all edit scripts.
public static final String CLUSTER_INPUT = MINING_INPUT + "ClusteringInput/input.arff";
// the output path of fix patterns mining.
private static final String MINING_OUTPUT = ROOT_PATH + "MiningOutput/";
public static final String CLUSTER_OUTPUT = MINING_OUTPUT + "ClusteringOutput/clusterResults.list";
public static final String CLUSTERED_PATCHES_FILE = MINING_OUTPUT + "ClusteredPatches/";
public static final String CLUSTERED_TOKENSS_FILE = MINING_OUTPUT + "ClusteredTokens/"; // Token vectors of buggy source code.
public static final String COMMON_CLUSTERS_SIZES = MINING_OUTPUT + "ClusteringOutput/CommonClusterSizes.list";
// evaluation data
public static final String TEST_INPUT = ROOT_PATH + "TestProjects/";
public static final String TEST_POSITION_FILE = ROOT_PATH + "TestData/Positions/"; // Positions of all test statements.
public static final String TEST_DATA_FILE = ROOT_PATH + "TestData/TestStatements/"; // Token vectors of all test statements.
public static final String NUMBER_OF_TRAINING_DATA = ROOT_PATH + "TestData/NumberOfTrainingData.list";;
// data of unsupervised learning
public static final String EMBEDDING_DATA_TOKENS1 = ROOT_PATH + "TestData/AllTokenVectorsForEvaluation.list";
public static final String EMBEDDED_ALL_TOKENS1 = ROOT_PATH + "TestData/AllEmbeddedTokens.list";
public static final String VECTORIED_ALL_SOURCE_CODE1 = ROOT_PATH + "TestData/AllVectorizedSourceCode/";
public static final String EXTRACTED_FEATURES_EVALUATION = ROOT_PATH + "TestDataExtractedFeatures/"; // extracted features of all source code (training data and testing data)
// Data of supervised learning
public static final String CLUSTERNUMBER_LABEL_MAP = ROOT_PATH + "TestData/clusterMappingLabel.list";
public static final String EMBEDDING_DATA_TOKENS2 = ROOT_PATH + "TestData/AllTokenVectorsForSupervisedEvaluation.list";
public static final String EMBEDDED_ALL_TOKENS2 = ROOT_PATH + "TestData/AllEmbeddedTokensForSuperVisedEvaluation.list";
public static final String TRAINING_DATA = ROOT_PATH + "TestData/TrainingData.csv"; // Training data of supervised learning
public static final String TESTING_DATA = ROOT_PATH + "TestData/SupervisedLearning/"; // testing data of supervised learning
public static final String FEATURES_OF_TRAINING_DATA = ROOT_PATH + "TestingOutput/TraingFeatures/";
public static final String FEATURES_OF_TESTING_DATA = ROOT_PATH + "TestingOutput/TestingFeatures/";
public static final String POSSIBILITIES_OF_TESTING_DATA = ROOT_PATH + "TestingOutput/Posibilities/";
public static final String PREDICTED_RESULTS_OF_TESTING_DATA = ROOT_PATH + "TestingOutput/Prediction/";
public static final String SUPERVISED_LEARNING_MODEL = ROOT_PATH + "TestingOutput/SupervisedLearningModel.zip";
public static final String FEATURES_OF_COMMON_CLUSTERS = ROOT_PATH + "FeaturesOfCommonClusters/";
}
@@ -1,43 +0,0 @@
package edu.lu.uni.serval.defects4j;
public class Bug {
private String project;
private String fileName;
private int startLine;
private int endLine;
private String type;
public Bug(String project, String fileName, int startLine, int endLine, String type) {
super();
this.project = project;
this.fileName = fileName;
this.startLine = startLine;
this.endLine = endLine;
this.type = type;
}
public String getProject() {
return project;
}
public String getFileName() {
return fileName;
}
public int getStartLine() {
return startLine;
}
public int getEndLine() {
return endLine;
}
public String getType() {
return type;
}
}
@@ -1,206 +0,0 @@
package edu.lu.uni.serval.defects4j;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
public class BugParser {
public static void main(String[] args) throws IOException {
List<Bug> bugs = new ArrayList<>();
String bugsFile = "Dataset/Defects4j/Bugs.txt";
String content = FileHelper.readFile(bugsFile);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
StringBuilder tokensBuilder = new StringBuilder();
StringBuilder buggyCodeBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(" : ");
String type = elements[0];
String project = elements[1];
project = project.substring(0, project.lastIndexOf("."));
String fileName = elements[2];
int startLine = Integer.parseInt(elements[3]);
int endLine = Integer.parseInt(elements[4]);
Bug bug = new Bug(project, fileName, startLine, endLine, type);
bugs.add(bug);
}
int i = 0;
for (Bug bug : bugs) {
String type = bug.getType();
String project = bug.getProject();
String fileName = bug.getFileName();
int startLine = bug.getStartLine();
int endLine = bug.getEndLine();
File buggyFile = null;
String[] elements = project.split("_");
if (elements.length < 2) System.out.println(type + "-" + project + "-" + fileName + "-" + startLine + "-" + endLine);
List<File> javaFiles = FileHelper.getAllFiles(Configuration.ROOT_PATH + "Testing/projects1/" + elements[0] + "/" + elements[1] + "-b", ".java");
for (File javaFile : javaFiles) {
if (javaFile.getPath().endsWith(fileName)) {
buggyFile = javaFile;
break;
}
}
if (i == 31) {
i = i + 1 - 1;
}
SimpleTree simpleTree = null;
if ("EQ_DOESNT_OVERRIDE_EQUALS".equals(type)|| "HE_EQUALS_USE_HASHCODE".equals(type) || "HE_INHERITS_EQUALS_USE_HASHCODE".equals(type)||
"SE_NO_SUITABLE_CONSTRUCTOR".equals(type) || "RI_REDUNDANT_INTERFACES".equals(type)
// ||"CN_IDIOM_NO_SUPER_CALL".equals(type)
||"SE_NO_SERIALVERSIONID".equals(type) || "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(type)) {
ViolationSourceCodeTree parser = new ViolationSourceCodeTree(buggyFile, startLine, endLine);
ITree classNameTree = parser.getClassNameTokens();
simpleTree = new SimplifyTree().canonicalizeSourceCodeTree(classNameTree, null);
startLine = parser.getViolationFinalStartLine();
endLine = parser.getViolationFinalEndLine();
} else {
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(buggyFile, startLine, endLine);
alarmTree.extract();
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
System.err.println("#Null_Violation_Hunk: " + buggyFile.getName() + ":" + startLine + ":" + endLine);
continue;
}
simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, null);
children.add(simpleT);
}
simpleTree.setChildren(children);
startLine = alarmTree.getViolationFinalStartLine();
endLine = alarmTree.getViolationFinalEndLine();
}
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
System.out.println((++ i) + "==" + length);
// sizes += length + "\n";
// this.tokens += tokens + "\n";
String sourceCode = readSourceCode(buggyFile, startLine, endLine, type);
// this.sourceCode += sourceCode + "\n";
// tokensBuilder.append(type).append(":").append(tokens).append("\n");
tokensBuilder.append(tokens).append("\n");
buggyCodeBuilder.append(sourceCode).append("\n");
}
FileHelper.outputToFile("Dataset/Defects4j/buggyTokens.list", tokensBuilder, false);
FileHelper.outputToFile("Dataset/Defects4j/buggySourceCode.list", buggyCodeBuilder, false);
}
private static String readSourceCode(File javaFile, int startLine, int endLine, String violationType) {
StringBuilder sourceCode = new StringBuilder("##Source_Code:\n");
sourceCode.append(violationType).append("\n");
sourceCode.append(javaFile.getName().replaceAll("#", "/")).append("\nPosition: ").append(startLine).append(" : ").append(endLine).append("\n");
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(javaFile);
scanner = new Scanner(fis);
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
counter ++;
if (startLine <= counter && counter <= endLine) {
sourceCode.append(line + "\n");
}
if (counter == endLine) break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sourceCode.toString();
}
}
/*
20
32
20
8
8
16
22
10
10
10
12
16
18
10
12
8
14
44
8
26
20
10
12
12
10
4
10
8
8
12
16
1776
8
10
10
12
8
50
18
8
12
38
4
4
14
16
20
26
8
14
12
14
*/
@@ -1,85 +0,0 @@
package edu.lu.uni.serval.diffentry;
import java.util.ArrayList;
import java.util.List;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
public class DiffEntryHunk {
private int bugLineStartNum;
private int fixLineStartNum;
private int bugRange;
private int fixRange;
private String hunk;
private int buggyHunkSize;
private int fixedHunkSize;
private String file;
private List<HierarchicalActionSet> actionSets = new ArrayList<>();
public DiffEntryHunk(int bugLineStartNum, int fixLineStartNum, int bugRange, int fixRange) {
super();
this.bugLineStartNum = bugLineStartNum;
this.fixLineStartNum = fixLineStartNum;
this.bugRange = bugRange;
this.fixRange = fixRange;
}
public int getBugLineStartNum() {
return bugLineStartNum;
}
public int getFixLineStartNum() {
return fixLineStartNum;
}
public int getBugRange() {
return bugRange;
}
public int getFixRange() {
return fixRange;
}
public String getHunk() {
return hunk;
}
public void setHunk(String hunk) {
this.hunk = hunk;
}
public int getBuggyHunkSize() {
return buggyHunkSize;
}
public void setBuggyHunkSize(int buggyHunkSize) {
this.buggyHunkSize = buggyHunkSize;
}
public int getFixedHunkSize() {
return fixedHunkSize;
}
public void setFixedHunkSize(int fixedHunkSize) {
this.fixedHunkSize = fixedHunkSize;
}
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public List<HierarchicalActionSet> getActionSets() {
return actionSets;
}
@Override
public String toString() {
return "@@ -" + this.bugLineStartNum + ", " + this.bugRange + " +" + this.fixLineStartNum + ", " + this.fixRange + "\n" + this.hunk;
}
}
@@ -1,250 +0,0 @@
package edu.lu.uni.serval.diffentry;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import edu.lu.uni.serval.utils.FileHelper;
public class DiffEntryReader {
public List<DiffEntryHunk> readHunks(File diffentryFile) {
List<DiffEntryHunk> diffentryHunks = new ArrayList<>();
String content = FileHelper.readFile(diffentryFile);
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(content));
String line = null;
int startLine = 0;
int range = 0;
int startLine2 = 0;
int range2 = 0;
StringBuilder hunk = new StringBuilder();
boolean sourceCode = false;
while ((line = reader.readLine()) != null) {
if (RegExp.filterSignal(line.trim())) {
sourceCode = true;
if (hunk.length() > 0) {
if ((range < 7 && range2 < 7) || range == 0 || range2 == 0) { // filter out big hunks
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(startLine, startLine2, range, range2);
diffEntryHunk.setHunk(hunk.toString());
diffentryHunks.add(diffEntryHunk);
}
hunk.setLength(0);
}
int plusIndex = line.indexOf("+");
String lineNum = line.substring(4, plusIndex);
String[] nums = lineNum.split(",");
startLine = Integer.parseInt(nums[0].trim());
if (nums.length == 2) {
range = Integer.parseInt(nums[1].trim());
}
String lineNum2 = line.substring(plusIndex) .trim();
lineNum2 = lineNum2.substring(1, lineNum2.length() - 2);
String[] nums2 = lineNum2.split(",");
startLine2 = Integer.parseInt(nums2[0].trim());
if (nums2.length == 2) {
range2 = Integer.parseInt(nums2[1].trim());
}
continue;
}
if (sourceCode) hunk.append(line + "\n");
}
if ((range < 7 && range2 < 7) || range == 0 || range2 == 0) { // filter out big hunks
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(startLine, startLine2, range, range2);
diffEntryHunk.setHunk(hunk.toString());
diffentryHunks.add(diffEntryHunk);
}
hunk.setLength(0);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return diffentryHunks;
}
/**
* Read all hunks without considering their sizes.
*
* @param diffentryFile
* @return
*/
public List<DiffEntryHunk> readHunks2(File diffentryFile) {
List<DiffEntryHunk> diffentryHunks = new ArrayList<>();
String content = FileHelper.readFile(diffentryFile);
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(content));
String line = null;
int startLine = 0;
int range = 0;
int startLine2 = 0;
int range2 = 0;
StringBuilder hunk = new StringBuilder();
boolean sourceCode = false;
while ((line = reader.readLine()) != null) {
if (RegExp.filterSignal(line.trim())) {
// line = Pattern.compile("^@@\\s\\-\\d+,*\\d*\\s\\+\\d+,*\\d*\\s@@").split(line)[1];
sourceCode = true;
if (hunk.length() > 0) {
if (startLine > 0) {
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(startLine, startLine2, range, range2);
diffEntryHunk.setHunk(hunk.toString());
diffentryHunks.add(diffEntryHunk);
}
hunk.setLength(0);
}
int plusIndex = line.indexOf("+");
String lineNum = line.substring(4, plusIndex);
String[] nums = lineNum.split(",");
startLine = Integer.parseInt(nums[0].trim());
if (nums.length == 2) {
range = Integer.parseInt(nums[1].trim());
}
String lineNum2 = line.substring(plusIndex) .trim();
lineNum2 = lineNum2.substring(1, lineNum2.length() - 2);
String[] nums2 = lineNum2.split(",");
startLine2 = Integer.parseInt(nums2[0].trim());
if (nums2.length == 2) {
range2 = Integer.parseInt(nums2[1].trim());
}
continue;
}
if (sourceCode) hunk.append(line + "\n");
}
if (startLine > 0) {
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(startLine, startLine2, range, range2);
diffEntryHunk.setHunk(hunk.toString());
diffentryHunks.add(diffEntryHunk);
}
hunk.setLength(0);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return diffentryHunks;
}
/**
* Read all hunks with pure deleted lines and added lines.
*
* @param diffentryFile
* @return
*/
public List<DiffEntryHunk> readHunks3(File diffentryFile) {
List<DiffEntryHunk> diffentryHunks = new ArrayList<>();
String content = FileHelper.readFile(diffentryFile);
BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(content));
String line = null;
int buggyStartLine = 0;
int buggyRange = 0;
int fixedStartLine = 0;
int fixedRange = 0;
int buggyHunkSize = 0;
int fixedHunkSize = 0;
StringBuilder hunk = new StringBuilder();
boolean sourceCode = false;
while ((line = reader.readLine()) != null) {
if (RegExp.filterSignal(line.trim())) {
sourceCode = true;
if (hunk.length() > 0) {
if (buggyStartLine > 0) {
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(buggyStartLine, fixedStartLine, buggyRange, fixedRange);
diffEntryHunk.setHunk(hunk.toString());
diffEntryHunk.setBuggyHunkSize(buggyHunkSize);
diffEntryHunk.setFixedHunkSize(fixedHunkSize);
diffEntryHunk.setFile(diffentryFile.getPath());
diffentryHunks.add(diffEntryHunk);
}
hunk.setLength(0);
buggyStartLine = 0;
buggyRange = 0;
fixedStartLine = 0;
fixedRange = 0;
buggyHunkSize = 0;
fixedHunkSize = 0;
}
int plusIndex = line.indexOf("+");
String lineNum = line.substring(4, plusIndex);
String[] nums = lineNum.split(",");
buggyStartLine = Integer.parseInt(nums[0].trim());
if (nums.length == 2) {
buggyRange = Integer.parseInt(nums[1].trim());
}
String lineNum2 = line.substring(plusIndex) .trim();
lineNum2 = lineNum2.substring(1, lineNum2.length() - 2);
String[] nums2 = lineNum2.split(",");
fixedStartLine = Integer.parseInt(nums2[0].trim());
if (nums2.length == 2) {
fixedRange = Integer.parseInt(nums2[1].trim());
}
continue;
} else if (sourceCode) {
if (line.startsWith("-")) {
buggyHunkSize ++;
} else if (line.startsWith("+")) {
fixedHunkSize ++;
}
hunk.append(line + "\n");
}
}
if (buggyStartLine > 0 && hunk.length() > 0) {
DiffEntryHunk diffEntryHunk = new DiffEntryHunk(buggyStartLine, fixedStartLine, buggyRange, fixedRange);
diffEntryHunk.setHunk(hunk.toString());
diffEntryHunk.setBuggyHunkSize(buggyHunkSize);
diffEntryHunk.setFixedHunkSize(fixedHunkSize);
diffEntryHunk.setFile(diffentryFile.getPath());
diffentryHunks.add(diffEntryHunk);
hunk.setLength(0);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return diffentryHunks;
}
}
@@ -1,20 +0,0 @@
package edu.lu.uni.serval.diffentry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegExp {
private static final String REGULAR_EXPRESSION = "^@@\\s\\-\\d+,*\\d*\\s\\+\\d+,*\\d*\\s@@$"; //@@ -21,0 +22,2 @@
private static Pattern pattern = Pattern.compile(REGULAR_EXPRESSION);
public static boolean filterSignal(String string) {
boolean flag = false;
Matcher res = pattern.matcher(string);
if (res.matches()) {
flag = true;
}
return flag;
}
}
@@ -1,4 +1,4 @@
package edu.lu.uni.serval.FixPatternParser.violations;
package edu.lu.uni.serval.fixminer;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
@@ -16,10 +16,10 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestHunkParser {
public class EnhancedASTDiff {
private static Logger log = LoggerFactory.getLogger(EnhancedASTDiff.class);
private static Logger log = LoggerFactory.getLogger(TestHunkParser.class);
// public static void main(String[] args) {
public static void main(String inputPath, String outputPath,String numOfWorkers,String project) {
@@ -44,29 +44,13 @@ public class TestHunkParser {
continue;
String GUM_TREE_OUTPUT = outputPath + "/"+ pjName + "/";
// final String editScriptsFilePath = GUM_TREE_OUTPUT + "editScripts.list";
// final String patchesSourceCodeFilePath =GUM_TREE_OUTPUT + "patchSourceCode.list";
// final String buggyTokensFilePath = GUM_TREE_OUTPUT + "tokens.list";
// final String editScriptSizesFilePath = GUM_TREE_OUTPUT + "editScriptSizes.csv";
// final String alarmTypesFilePath = GUM_TREE_OUTPUT + "alarmTypes.list";
FileHelper.createDirectory(GUM_TREE_OUTPUT + "/UPD");
FileHelper.createDirectory(GUM_TREE_OUTPUT + "/INS");
FileHelper.createDirectory(GUM_TREE_OUTPUT + "/DEL");
FileHelper.createDirectory(GUM_TREE_OUTPUT + "/MOV");
FileHelper.createDirectory(GUM_TREE_OUTPUT + "/ALL");
// FileHelper.deleteDirectory(editScriptsFilePath);
// FileHelper.deleteDirectory(patchesSourceCodeFilePath);
// FileHelper.deleteDirectory(buggyTokensFilePath);
// FileHelper.deleteDirectory(editScriptSizesFilePath);
// FileHelper.deleteDirectory(alarmTypesFilePath);
// StringBuilder astEditScripts = new StringBuilder();
// StringBuilder tokens = new StringBuilder();
// StringBuilder sizes = new StringBuilder();
// StringBuilder patches = new StringBuilder();
// StringBuilder alarmTypes = new StringBuilder();
int a = 0;
@@ -76,7 +60,7 @@ public class TestHunkParser {
try {
log.info("Akka begins...");
system = ActorSystem.create("Mining-FixPattern-System");
System.out.println(system.settings());
parsingActor = system.actorOf(ParseFixPatternActor.props(Integer.valueOf(numOfWorkers), project), "mine-fix-pattern-actor");
parsingActor.tell(msg, ActorRef.noSender());
} catch (Exception e) {
@@ -84,20 +68,6 @@ public class TestHunkParser {
e.printStackTrace();
}
// FileHelper.outputToFile(editScriptsFilePath, astEditScripts, true);
// FileHelper.outputToFile(buggyTokensFilePath, tokens, true);
// FileHelper.outputToFile(editScriptSizesFilePath, sizes, true);
// FileHelper.outputToFile(patchesSourceCodeFilePath, patches, true);
// FileHelper.outputToFile(alarmTypesFilePath, alarmTypes, true);
// astEditScripts.setLength(0);
// tokens.setLength(0);
// sizes.setLength(0);
// patches.setLength(0);
// alarmTypes.setLength(0);
// System.out.println(a);
// classifyByAlarmTypes();
}
}
@@ -109,16 +79,15 @@ public class TestHunkParser {
List<MessageFile> msgFiles = new ArrayList<>();
if (revFiles.length >= 0) {
for (File revFile : revFiles) {
// if (revFile.getName().endsWith(".java")) {
String fileName = revFile.getName();
File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file
fileName = fileName.replace(".java", ".txt");
File diffentryFile = new File(gumTreeInput + "DiffEntries/" + fileName); // DiffEntry file
// File positionFile = new File(gumTreeInput + "positions/" + fileName); // position file
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile);
// msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
// }
}
return msgFiles;
@@ -1,6 +1,5 @@
package edu.lu.uni.serval.fixminer;
import edu.lu.uni.serval.FixPatternParser.violations.TestHunkParser;
import edu.lu.uni.serval.fixminer.cluster.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -27,34 +26,34 @@ public class Launcher {
appProps.load(new FileInputStream(appConfigPath));
String portInner = appProps.getProperty("portInner","6380");
String serverWait = appProps.getProperty("serverWait", "50000");
// String serverWait = appProps.getProperty("serverWait", "50000");
String numOfWorkers = appProps.getProperty("numOfWorkers", "10");
String jobType = appProps.getProperty("jobType","ALL");
String portDumps = appProps.getProperty("portDumps","6399");
String pythonPath = appProps.getProperty("pythonPath","/Users/anilkoyuncu/bugStudy/code/python");
String datasetPath = appProps.getProperty("datasetPath","/Users/anilkoyuncu/bugStudy/dataset");
String pjName = appProps.getProperty("pjName","allDataset");
String dbNo = appProps.getProperty("dbNo","0");
// String dbNo = appProps.getProperty("dbNo","0");
String actionType = appProps.getProperty("actionType","ALL");
String threshold = appProps.getProperty("threshold","1");
String cursor = appProps.getProperty("cursor","10000000");
String chunk = appProps.getProperty("chunk","1.txt");
String parameters = String.format("\nportInner %s " +
"\nserverWait %s \nnumOfWorkers %s " +
"\nnumOfWorkers %s " +
"\njobType %s \nport %s " +
"\npythonPath %s \ndatasetPath %s" +
"\npjName %s \ndbNo %s \nactionType %s \nthreshold %s \ncursor %s"
, portInner, serverWait, numOfWorkers, jobType, portDumps, pythonPath,datasetPath,pjName,dbNo,actionType,threshold,cursor);
"\npjName %s \nactionType %s \nthreshold %s \ncursor %s"
, portInner, numOfWorkers, jobType, portDumps, pythonPath,datasetPath,pjName,actionType,threshold,cursor);
log.info(parameters);
mainLaunch(portInner, serverWait, numOfWorkers, jobType, portDumps, pythonPath,datasetPath,pjName,dbNo,actionType,threshold,cursor,chunk);
mainLaunch(portInner, numOfWorkers, jobType, portDumps, pythonPath,datasetPath,pjName,actionType,threshold,cursor,chunk);
}
public static void mainLaunch(String portInner, String serverWait, String numOfWorkers, String jobType, String portDumps, String pythonPath, String datasetPath, String pjName, String dbNo, String actionType, String threshold, String cursor, String chunk){
public static void mainLaunch(String portInner, String numOfWorkers, String jobType, String portDumps, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String cursor, String chunk){
String dbDir;
@@ -72,43 +71,35 @@ public class Launcher {
try {
switch (jobType) {
case "ENHANCEDASTDIFF":
TestHunkParser.main(gumInput, gumOutput, numOfWorkers, pjName);
EnhancedASTDiff.main(gumInput, gumOutput, numOfWorkers, pjName);
break;
case "CACHE":
StoreFile.main(gumOutput, portDumps, serverWait, dbDir, actionType+dumpsName,actionType);
StoreEDiffInCache.main(gumOutput, portDumps, dbDir, actionType+dumpsName,actionType);
break;
case "COMP":
CalculatePairs.main(serverWait, dbDir, actionType+dumpsName, portDumps, pairsPath+actionType, pjName+actionType);
ImportPairs2DB.main(pairsPath+actionType, portInner, serverWait, dbDir,datasetPath);
case "SI":
CalculatePairs.main(dbDir, actionType+dumpsName, portDumps, pairsPath+actionType, pjName+actionType);
ImportPairs2DB.main(pairsPath+actionType, portInner, dbDir,datasetPath);
break;
case "AKKA":
AkkaTreeLoader.main(portInner, serverWait, dbDir, pjName +actionType+chunk+".rdb" , portDumps, actionType+dumpsName,pairsPath+actionType,numOfWorkers,cursor);
case "SIMI":
AkkaTreeLoader.main(portInner, dbDir, pjName +actionType+chunk+".rdb" , portDumps, actionType+dumpsName,pairsPath+actionType,numOfWorkers,cursor,chunk);
break;
case "LEVEL1":
level1(portInner, serverWait, portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, pairsPath, dumpsName, gumInput);
level1(portInner, portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, pairsPath, dumpsName, gumInput);
break;
//CALC python abstractPatch.py to from cluster folder
case "LEVEL2":
level2(serverWait, portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
level2(portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
break;
//CALC via python
case "LEVEL3":
level3(serverWait, portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
break;
case "ALL":
// TestHunkParser.main(gumInput, gumOutput, numOfWorkers, pjName);
// StoreFile.main(gumOutput, portInner, serverWait, dbDir, actionType+dumpsName,actionType);
// level1(portInner, serverWait, port, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, pairsPath, dumpsName, gumInput);
// level2(serverWait, port, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
// level3(serverWait, port, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
level3(portDumps, pythonPath, datasetPath, pjName, actionType, threshold, dbDir, dumpsName, gumInput);
break;
case "EXTRACTPATTERN":
PatternExtractor.mainLaunch(portInner,serverWait,numOfWorkers,jobType,portDumps,pythonPath,datasetPath,pjName,dbNo,actionType,threshold);
PatternExtractor.mainLaunch(portInner,numOfWorkers,jobType,portDumps,pythonPath,datasetPath,pjName,actionType,threshold);
break;
case "GETPATTERN":
PatternExtractor.mainLaunch(portInner,serverWait,numOfWorkers,jobType,portDumps,pythonPath,datasetPath,pjName,dbNo,actionType,threshold);
PatternExtractor.mainLaunch(portInner,numOfWorkers,jobType,portDumps,pythonPath,datasetPath,pjName,actionType,threshold);
break;
}
} catch (Exception e) {
@@ -120,63 +111,63 @@ public class Launcher {
}
private static void level1(String portInner, String serverWait, String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String pairsPath, String dumpsName, String gumInput) throws Exception {
private static void level1(String portInner, String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String pairsPath, String dumpsName, String gumInput) throws Exception {
TreeLoaderClusterL1.main(portInner, serverWait, port, dbDir, "level1-"+pjName+ actionType+".rdb", dbDir ,pjName + actionType);
TreeLoaderClusterL1.main(portInner, port, dbDir, "level1-"+pjName+ actionType+".rdb", dbDir ,pjName + actionType);
CallShell cs1 =new CallShell();
String db1 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
String db11 = String.format(db1, dbDir,"level1-"+pjName+ actionType+".rdb" ,Integer.valueOf(port));
cs1.runShell(db11,serverWait, port);
cs1.runShell(db11, port);
String runpy = "bash "+datasetPath + "/" + "launchPy.sh" +" %s %s %s %s %s %s";
String formatRunPy = String.format(runpy,pythonPath +"/abstractPatch.py", gumInput, datasetPath + "/cluster"+pjName+ actionType, port, "matches" + pjName + actionType, threshold);
cs1.runShell(formatRunPy);
String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
stopServer = String.format(stopServer,Integer.valueOf(port));
cs1.runShell(stopServer,serverWait, port);
cs1.runShell(stopServer, port);
}
private static void level2(String serverWait, String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String dumpsName, String gumInput) throws Exception {
private static void level2(String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String dumpsName, String gumInput) throws Exception {
String stopServer;
MultiThreadTreeLoaderCluster.calculatePairsOfClusters(datasetPath + "/cluster"+pjName+ actionType, datasetPath,actionType);
MultiThreadTreeLoaderCluster.mainCompare("6300", datasetPath+"/pairs"+actionType, datasetPath + "/redisSingleImport.sh", dbDir, "clusterl1-"+pjName+actionType+".rdb", actionType+dumpsName, "6301",serverWait,actionType);
MultiThreadTreeLoaderCluster.mainCompare("6300", datasetPath+"/pairs"+actionType, datasetPath + "/redisSingleImport.sh", dbDir, "clusterl1-"+pjName+actionType+".rdb", actionType+dumpsName, "6301",actionType);
CallShell cs3 =new CallShell();
String db22 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
String db1b = String.format(db22, dbDir,"clusterl1-"+pjName+actionType+".rdb",Integer.valueOf(port));
cs3.runShell(db1b,serverWait, port);
cs3.runShell(db1b, port);
String runpy2 = "bash "+datasetPath + "/" + "launchPy.sh" +" %s %s %s %s %s %s";
String formatRunPy1a = String.format(runpy2,pythonPath +"/abstractPatchCluster.py", gumInput, datasetPath + "/cluster"+pjName+ actionType, port, datasetPath + "/cluster-2l"+pjName+ actionType,threshold);
cs3.runShell(formatRunPy1a);
String stopServer1a = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
stopServer = String.format(stopServer1a,Integer.valueOf(port));
cs3.runShell(stopServer,serverWait, port);
cs3.runShell(stopServer, port);
}
private static void level3(String serverWait, String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String dumpsName, String gumInput) throws Exception {
private static void level3(String port, String pythonPath, String datasetPath, String pjName, String actionType, String threshold, String dbDir, String dumpsName, String gumInput) throws Exception {
String stopServer;
MultiThreadTreeLoaderCluster3.calculatePairsOfClusters(datasetPath + "/cluster-2l"+pjName+ actionType, datasetPath,actionType);
MultiThreadTreeLoaderCluster3.mainCompare("6300", datasetPath+"/pairs-2l"+actionType, datasetPath + "/redisSingleImport.sh", dbDir, "clusterl2-"+pjName+actionType+".rdb", actionType+dumpsName, "6301",serverWait,actionType);
MultiThreadTreeLoaderCluster3.mainCompare("6300", datasetPath+"/pairs-2l"+actionType, datasetPath + "/redisSingleImport.sh", dbDir, "clusterl2-"+pjName+actionType+".rdb", actionType+dumpsName, "6301",actionType);
CallShell cs5 =new CallShell();
String dba = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
String dbaa = String.format(dba, dbDir,"clusterl2-"+pjName+actionType+".rdb",Integer.valueOf(port));
cs5.runShell(dbaa,serverWait, port);
cs5.runShell(dbaa, port);
String runpya = "bash "+datasetPath + "/" + "launchPy.sh" +" %s %s %s %s %s %s";
String formatRunPya = String.format(runpya,pythonPath +"/abstractPatchClusterLevel3.py", gumInput, datasetPath + "/cluster-3l"+pjName+ actionType, port, datasetPath + "/cluster-2l"+pjName+ actionType,threshold);
cs5.runShell(formatRunPya);
String stopServera = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
stopServer = String.format(stopServera,Integer.valueOf(port));
cs5.runShell(stopServer,serverWait, port);
cs5.runShell(stopServer, port);
return;
}
// System.exit(1);
}
@@ -1,7 +1,5 @@
package edu.lu.uni.serval.fixminer;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.slf4j.Logger;
@@ -21,7 +19,6 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.getASTTree;
/**
* Created by anilkoyuncu on 02/08/2018.
@@ -30,7 +27,7 @@ public class PatternExtractor {
private static Logger log = LoggerFactory.getLogger(PatternExtractor.class);
public static void mainLaunch(String portInner,String serverWait, String numOfWorkers,String jobType,String port, String pythonPath, String datasetPath, String pjName, String dbNo, String actionType,String threshold) {
public static void mainLaunch(String portInner,String numOfWorkers,String jobType,String port, String pythonPath, String datasetPath, String pjName, String actionType,String threshold) {
String dbDir;
@@ -55,7 +52,7 @@ public class PatternExtractor {
switch (jobType) {
case "EXTRACTPATTERN":
loadDB(gumOutput, portInner, serverWait, dbDir, actionType+dumpsName,actionType,fixes);
loadDB(gumOutput, portInner, dbDir, actionType+dumpsName,actionType,fixes);
break;
case "GETPATTERN":
getPattern(fixes,actionType);
@@ -67,7 +64,7 @@ public class PatternExtractor {
}
public static void loadDB(String inputPath,String portInner,String serverWait,String dbDir,String chunkName,String operation,List<String> fixes) throws Exception {
public static void loadDB(String inputPath,String portInner,String dbDir,String chunkName,String operation,List<String> fixes) throws Exception {
// String inputPath;
// String portInner;
// String serverWait;
@@ -246,24 +243,24 @@ public class PatternExtractor {
}
}
public static ITree getSimpliedTree(HierarchicalActionSet actionSet) {
ITree tree = null;
Jedis inner = null;
try {
ITree parent = null;
ITree children = null;
TreeContext tc = new TreeContext();
tree = getASTTree(actionSet, parent, children, tc);
// tree.setParent(null);
tc.validate();
} catch (Exception e) {
e.printStackTrace();
}
return tree;
}
// public static ITree getSimpliedTree(HierarchicalActionSet actionSet) {
//
// ITree tree = null;
// Jedis inner = null;
// try {
//
// ITree parent = null;
// ITree children = null;
// TreeContext tc = new TreeContext();
// tree = getASTTree(actionSet, parent, children, tc);
//// tree.setParent(null);
// tc.validate();
//
// } catch (Exception e) {
// e.printStackTrace();
// }
// return tree;
// }
@@ -1,20 +1,18 @@
package edu.lu.uni.serval.FixPatternParser.violations;
package edu.lu.uni.serval.fixminer;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.MultipleThreadsParser.ParseFixPatternActor;
import edu.lu.uni.serval.MultipleThreadsParser.WorkMessage;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -232,131 +230,10 @@ public class TestHunkParserSingleFile {
// }
}
public static void classifyByAlarmTypes() {
final String alarmTypesFilePath = Configuration.ALARM_TYPES_FILE;
List<String> alarmTypes = readStringList(alarmTypesFilePath);
//edit scripts, sizes of edit scripts, buggy tokens, patches.
classifyByAlarmTypes(alarmTypes, Configuration.EDITSCRIPT_SIZES_FILE);
classifyByAlarmTypes(alarmTypes, Configuration.EDITSCRIPTS_FILE);
classifyByAlarmTypes(alarmTypes, Configuration.BUGGY_CODE_TOKENS_FILE);
classifyByAlarmTypes2(alarmTypes, Configuration.PATCH_SOURCECODE_FILE);
}
private static void classifyByAlarmTypes(List<String> alarmTypes, String file) {
Map<String, StringBuilder> buildersMap = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(file);
scanner = new Scanner(fis);
int counter = 0;
while (scanner.hasNextLine()) {
String alarmType = alarmTypes.get(counter);
StringBuilder builder = getBuilder(buildersMap, alarmType);
builder.append(scanner.nextLine() + "\n");
counter ++;
if (counter % 1000 == 0) {
outputBuilders(buildersMap, file);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
outputBuilders(buildersMap, file);
}
private static void classifyByAlarmTypes2(List<String> alarmTypes, String patchSourcecodeFile) {
Map<String, StringBuilder> buildersMap = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(patchSourcecodeFile);
scanner = new Scanner(fis);
int counter = 0;
String singlePatch = "";
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (Configuration.PATCH_SIGNAL.equals(line)) {
if (!"".equals(singlePatch)) {
String alarmType = alarmTypes.get(counter);
StringBuilder builder = getBuilder(buildersMap, alarmType);
builder.append(scanner.nextLine() + "\n");
counter ++;
if (counter % 2000 == 0) {
outputBuilders(buildersMap, patchSourcecodeFile);
}
}
singlePatch = line + "\n";
}
singlePatch += line + "\n";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
outputBuilders(buildersMap, patchSourcecodeFile);
}
private static void outputBuilders(Map<String, StringBuilder> map, String fileNameStr) {
File file = new File(fileNameStr);
String fileName = file.getName();
String parentPath = file.getParent();
for (Map.Entry<String, StringBuilder> entry : map.entrySet()) {
String alarmType = entry.getKey();
StringBuilder builder = entry.getValue();
FileHelper.outputToFile(parentPath + "/" + alarmType + "/" + fileName, builder, true);
builder.setLength(0);
entry.setValue(builder);
}
}
public static List<String> readStringList(String inputFile) {
List<String> list = new ArrayList<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(inputFile);
scanner = new Scanner(fis);
while(scanner.hasNextLine()) {
list.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
private static StringBuilder getBuilder(Map<String, StringBuilder> buildersMap, String alarmType) {
if (buildersMap.containsKey(alarmType)) {
return buildersMap.get(alarmType);
} else {
StringBuilder builder = new StringBuilder();
buildersMap.put(alarmType, builder);
return builder;
}
}
}
@@ -1,15 +1,16 @@
package edu.lu.uni.serval.FixPatternParser.violations;
package edu.lu.uni.serval.fixminer;
import com.github.gumtreediff.actions.ActionGenerator;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.matchers.Mapping;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -132,98 +133,98 @@ public class TestTreeLoader {
}
public static void compareAll(List<File> folders){
List<ITree> trees = new ArrayList<>();
HashMap<Integer, String> hmap = new HashMap<Integer, String>();
for (File target : folders) {
try {
FileInputStream fi = new FileInputStream(new File(target.toString()));
ObjectInputStream oi = new ObjectInputStream(fi);
ITree pr1 = (ITree) oi.readObject();
oi.close();
fi.close();
trees.add(pr1);
hmap.put(folders.indexOf(target), target.toString());
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (ITree tree : trees) {
// SimplifyTree simplifyTree = new SimplifyTree();
// simplifyTree.canonicalizeSourceCodeTree(tree);
tree.setLabel("");
tree.setParent(null);
List<ITree> descendants = tree.getDescendants();
for (ITree descendant : descendants) {
descendant.setLabel("");
}
}
System.out.println("a");
try {
for (int i = 0; i < trees.size(); i++) {
for (int j = i + 1; j < trees.size(); j++) {
// compare list.get(i) and list.get(j)
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt",true));
ITree oldTree = trees.get(i);
ITree newTree = trees.get(j);
Matcher m = Matchers.getInstance().getMatcher(oldTree, newTree);
m.match();
ActionGenerator ag = new ActionGenerator(oldTree, newTree, m.getMappings());
ag.generate();
List<Action> actions = ag.getActions();
writer.write(String.valueOf(i));
writer.write("\t");
writer.write(String.valueOf(j));
writer.write("\t");
writer.write(String.format("%1.2f", m.chawatheSimilarity(oldTree, newTree)));
writer.write("\t");
writer.write(String.format("%1.2f", m.diceSimilarity(oldTree, newTree)));
writer.write("\t");
writer.write(String.format("%1.2f", m.jaccardSimilarity(oldTree, newTree)));
writer.write("\t");
writer.write(String.valueOf(actions.size()));
writer.write("\t");
writer.write(hmap.get(i));
writer.write("\t");
writer.write(hmap.get(j));
writer.write("\n");
writer.close();
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream");
}
// if (actions.size() > 1) {
// Matcher m1 = Matchers.getInstance().getMatcher(actions.get(0).getNode(), actions.get(0).getNode());
// m1.match();
// Set<Mapping> mappingSet1 = m1.getMappingSet();
// public static void compareAll(List<File> folders){
// List<ITree> trees = new ArrayList<>();
// HashMap<Integer, String> hmap = new HashMap<Integer, String>();
// for (File target : folders) {
//
// try {
// FileInputStream fi = new FileInputStream(new File(target.toString()));
// ObjectInputStream oi = new ObjectInputStream(fi);
// ITree pr1 = (ITree) oi.readObject();
// oi.close();
// fi.close();
// trees.add(pr1);
// hmap.put(folders.indexOf(target), target.toString());
//
// } catch (FileNotFoundException e) {
// System.out.println("File not found");
// } catch (IOException e) {
// System.out.println("Error initializing stream");
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
// for (ITree tree : trees) {
//// SimplifyTree simplifyTree = new SimplifyTree();
//// simplifyTree.canonicalizeSourceCodeTree(tree);
// tree.setLabel("");
// tree.setParent(null);
// List<ITree> descendants = tree.getDescendants();
// for (ITree descendant : descendants) {
// descendant.setLabel("");
// }
//
// }
}
// System.out.println("a");
//
// try {
//
//
//
// for (int i = 0; i < trees.size(); i++) {
// for (int j = i + 1; j < trees.size(); j++) {
// // compare list.get(i) and list.get(j)
// BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt",true));
// ITree oldTree = trees.get(i);
//
// ITree newTree = trees.get(j);
//
// Matcher m = Matchers.getInstance().getMatcher(oldTree, newTree);
// m.match();
//
// ActionGenerator ag = new ActionGenerator(oldTree, newTree, m.getMappings());
// ag.generate();
// List<Action> actions = ag.getActions();
// writer.write(String.valueOf(i));
// writer.write("\t");
// writer.write(String.valueOf(j));
// writer.write("\t");
//
// writer.write(String.format("%1.2f", m.chawatheSimilarity(oldTree, newTree)));
// writer.write("\t");
// writer.write(String.format("%1.2f", m.diceSimilarity(oldTree, newTree)));
// writer.write("\t");
// writer.write(String.format("%1.2f", m.jaccardSimilarity(oldTree, newTree)));
// writer.write("\t");
// writer.write(String.valueOf(actions.size()));
// writer.write("\t");
// writer.write(hmap.get(i));
// writer.write("\t");
// writer.write(hmap.get(j));
// writer.write("\n");
//
// writer.close();
// }
// }
//
//
// } catch (FileNotFoundException e) {
// System.out.println("File not found");
// } catch (IOException e) {
// System.out.println("Error initializing stream");
//
// }
//
//
//// if (actions.size() > 1) {
//// Matcher m1 = Matchers.getInstance().getMatcher(actions.get(0).getNode(), actions.get(0).getNode());
//// m1.match();
//// Set<Mapping> mappingSet1 = m1.getMappingSet();
////
//// }
// }
}
@@ -1,20 +1,17 @@
package edu.lu.uni.serval.fixminer.cluster;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.junit.Assert;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.io.*;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import static edu.lu.uni.serval.fixminer.cluster.akka.AkkaTreeParser.akkaCompare;
@@ -23,58 +20,13 @@ import static edu.lu.uni.serval.fixminer.cluster.akka.AkkaTreeParser.akkaCompare
*/
public class AkkaTreeLoader {
private static class StreamGobbler implements Runnable {
private InputStream inputStream;
private Consumer<String> consumer;
public StreamGobbler(InputStream inputStream, Consumer<String> consumer) {
this.inputStream = inputStream;
this.consumer = consumer;
}
@Override
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines()
.forEach(consumer);
}
}
private static Logger log = LoggerFactory.getLogger(AkkaTreeLoader.class);
public static void loadRedis(String cmd,String serverWait){
Process process;
try {
// String comd = String.format(cmd, f.getAbsoluteFile());
process = Runtime.getRuntime()
.exec(cmd);
public static void main(String portInner, String dbDir, String chunkName, String port, String dumpsName, String pairsPath, String numOfWorkers, String cursor,String chunk) throws Exception {
StreamGobbler streamGobbler =
new StreamGobbler(process.getInputStream(), System.out::println);
Executors.newSingleThreadExecutor().submit(streamGobbler);
int exitCode = process.waitFor();
assert exitCode == 0;
// Thread.sleep(Integer.valueOf(serverWait));
} catch (IOException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Load done");
}
private static Consumer<String> consumer = Assert::assertNotNull;
public static void main(String portInner, String serverWait, String dbDir, String chunkName, String port, String dumpsName, String pairsPath, String numOfWorkers, String cursor) throws Exception {
String parameters = String.format("\nportInner %s \nserverWait %s \nchunkName %s \ndbDir %s \ndumpsName %s",portInner,serverWait,chunkName,dbDir,dumpsName);
String parameters = String.format("\nportInner %s \nchunkName %s \ndbDir %s \ndumpsName %s",portInner,chunkName,dbDir,dumpsName);
log.info(parameters);
@@ -82,243 +34,76 @@ public class AkkaTreeLoader {
String cmd = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
String cmd1 = String.format(cmd, dbDir,dumpsName,Integer.valueOf(port));
//
cs.runShell(cmd1,serverWait,port);
cs.runShell(cmd1,port);
String cmdInner = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
String cmd2 = String.format(cmdInner, dbDir,chunkName,Integer.valueOf(portInner));
log.info(cmd1);
log.info(cmd2);
//
cs.runShell(cmd2,serverWait, portInner);
JedisPool outerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(port),20000000);
JedisPool innerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(portInner),20000000);
// Jedis jedis = new Jedis(new URI("redis://localhost:"+port));
// while (!jedis.ping().equals("PONG")){
// log.info("wait");
// }
//
// jedis = new Jedis(new URI("redis://localhost:"+portInner));
// while (!jedis.ping().equals("PONG")){
// log.info("wait");
// }
// String pairsIndexFile = pairsPath + "/"+ chunkName;
// pairsIndexFile = pairsIndexFile.replace("csv.rdb","index");
//
//
// Pattern pattern = Pattern.compile(",");
// String csvFile = pairsIndexFile;
// try {
// try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
// Map<String,String> namefreq = in
// .lines()
// // .skip(1)
// .map(x -> pattern.split(x))
// // .filter(x -> x[4].equals("CA") && x[3].equals("F"))
// .collect(HashMap::new, (map, x) ->
// map.put(x[0], x[1]),
// Map::putAll);
// // namefreq.forEach((k, v) -> System.out.println(k + " => " + v));
//
// Jedis inner = null;
// try {
// inner = outerPool.getResource();
//
// for (Map.Entry<String, String> entry : namefreq.entrySet()) {
// String key = entry.getKey();
// String value = entry.getValue();
// inner.select(1);
// inner.set(key,value);
// // ...
// }
//
//
// }finally {
// if (inner != null) {
// inner.close();
// }
// }
//
//
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
cs.runShell(cmd2, portInner);
JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(port),20000000);
JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(portInner),20000000);
// comparePairs(innerPool,outerPool);
Thread.sleep(Integer.valueOf(serverWait));
akkaCompare(innerPool,outerPool,numOfWorkers,cursor);
// String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
// String stopServer1 = String.format(stopServer,Integer.valueOf(portInner));
//
// cs.runShell(stopServer1,serverWait);
// stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
// String stopServer2 = String.format(stopServer,Integer.valueOf(port));
//
// cs.runShell(stopServer2,serverWait);
String pairsIndexFile = pairsPath + "/"+ chunkName;
//0.txt.rdb
pairsIndexFile = pairsIndexFile.replace(chunk+".rdb",".index");
}
Pattern pattern = Pattern.compile(",");
String csvFile = pairsIndexFile;
try {
try (BufferedReader in = new BufferedReader(new FileReader(csvFile));){
Map<String,String> namefreq = in
.lines()
.map(x -> pattern.split(x))
.collect(HashMap::new, (map, x) ->
map.put(x[0], x[1]),
Map::putAll);
public static void comparePairs(JedisPool innerPool,JedisPool outerPool){
Jedis inner = null;
try {
inner = outerPool.getResource();
ScanResult<String> scan;
try (Jedis inner = innerPool.getResource()) {
while (!inner.ping().equals("PONG")){
log.info("wait");
for (Map.Entry<String, String> entry : namefreq.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
inner.select(1);
inner.set(key,value);
}
ScanParams sc = new ScanParams();
//150000000
log.info("Scanning ");
sc.count(250000000);
sc.match("pair_[0-9]*");
scan = inner.scan("0", sc);
int size = scan.getResult().size();
log.info("Scanned " + String.valueOf(size));
}finally {
if (inner != null) {
inner.close();
}
}
List<String> result = scan.getResult();
log.info("Getting results");
result
.parallelStream()
.forEach(m ->
{
Compare compare = new Compare();
compare.coreCompare(m, innerPool, outerPool);
;
}
);
// }
}
/** Read the object from Base64 string. */
private static Object fromString( String s ) throws IOException ,
ClassNotFoundException {
byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return o;
}
public static ITree getSimpliedTree(String fn,JedisPool outerPool) {
ITree tree = null;
Jedis inner = null;
try {
inner = outerPool.getResource();
while (!inner.ping().equals("PONG")){
log.info("wait");
}
inner.select(1);
String dist2load = inner.get(fn);
inner.select(0);
String s = inner.get(dist2load);
HierarchicalActionSet actionSet = (HierarchicalActionSet) fromString(s);
ITree parent = null;
ITree children =null;
TreeContext tc = new TreeContext();
tree = getASTTree(actionSet, parent, children,tc);
tree.setParent(null);
tc.validate();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if (inner != null) {
inner.close();
}
}
return tree;
akkaCompare(innerPool,outerPool,numOfWorkers,cursor);
}
public static <T, E> List<T> getKeysByValue(Map<T, E> map, E value) {
return map.entrySet()
.stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc){
int newType = 0;
String astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType);
if(keysByValue.size() != 1){
log.error("More than 1");
}
newType = keysByValue.get(0);
if(actionSet.getParent() == null){
//root
// parent = new Tree(newType,"");
parent = tc.createTree(newType, "", null);
tc.setRoot(parent);
}else{
// children = new Tree(newType,"");
// parent.addChild(children);
children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getASTTree(subAction,children,null,tc);
}
}
return parent;
}
static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(false);
poolConfig.setTestOnReturn(false);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
// poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true);
return poolConfig;
}
@@ -1,5 +1,6 @@
package edu.lu.uni.serval.fixminer.cluster;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import edu.lu.uni.serval.utils.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,7 +14,6 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;
import static edu.lu.uni.serval.fixminer.cluster.TreeLoaderClusterL1.poolConfig;
/**
* Created by anilkoyuncu on 05/04/2018.
@@ -21,21 +21,21 @@ import static edu.lu.uni.serval.fixminer.cluster.TreeLoaderClusterL1.poolConfig;
public class CalculatePairs {
private static Logger log = LoggerFactory.getLogger(CalculatePairs.class);
public static void main(String serverWait,String dbDir,String chunkName,String port,String outputPath,String pjName) throws Exception {
public static void main(String dbDir,String chunkName,String port,String outputPath,String pjName) throws Exception {
String parameters = String.format("\nport %s \nserverWait %s \nchunkName %s \ndbDir %s",port,serverWait,chunkName,dbDir);
String parameters = String.format("\nport %s \nchunkName %s \ndbDir %s",port,chunkName,dbDir);
log.info(parameters);
CallShell cs =new CallShell();
String cmd = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd = String.format(cmd, dbDir,chunkName,Integer.valueOf(port));
cs.runShell(cmd,serverWait, port);
cs.runShell(cmd, port);
FileHelper.createDirectory(outputPath);
JedisPool outerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(port),20000000);
JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(port),20000000);
ScanResult<String> scan;
try (Jedis outer = outerPool.getResource()) {
@@ -26,7 +26,7 @@ public class CallShell {
}
public static void runShell(String command, String serverWait, String port) throws Exception {
public static void runShell(String command, String port) throws Exception {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(
@@ -101,7 +101,7 @@ try{
public static void main(String[] args) throws Exception {
// runPing("redis-cli -p 6380 ping");
runShell("bash /Users/anilkoyuncu/bugStudy/release/code/redis/startServer.sh /Users/anilkoyuncu/bugStudy/release/code/redis Defects4JALL0.txt.rdb 6380","1","6380");
runShell("bash /Users/anilkoyuncu/bugStudy/release/code/redis/startServer.sh /Users/anilkoyuncu/bugStudy/release/code/redis Defects4JALL0.txt.rdb 6380","6380");
}
@@ -5,6 +5,7 @@ import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.EDiff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
@@ -13,7 +14,6 @@ import redis.clients.jedis.JedisPool;
import java.util.List;
import java.util.Map;
import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.getSimpliedTree;
/**
* Created by anilkoyuncu on 03/04/2018.
@@ -44,9 +44,9 @@ public class Compare {
// String firstValue = resultMap.get("0");
// String secondValue = resultMap.get("1");
oldTree = getSimpliedTree(i,outerPool);
oldTree = EDiff.getSimpliedTree(i,outerPool);
newTree = getSimpliedTree(j,outerPool);
newTree = EDiff.getSimpliedTree(j,outerPool);
Matcher m = Matchers.getInstance().getMatcher(oldTree, newTree);
m.match();
@@ -9,9 +9,6 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
//import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis;
//import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedisWait;
/**
* Created by anilkoyuncu on 05/04/2018.
@@ -19,10 +16,10 @@ import java.util.stream.Stream;
public class ImportPairs2DB {
private static Logger log = LoggerFactory.getLogger(ImportPairs2DB.class);
public static void main(String csvInputPath,String portInner,String serverWait,String dbDir,String datasetPath) throws Exception {
public static void main(String csvInputPath,String portInner,String dbDir,String datasetPath) throws Exception {
String parameters = String.format("\nInput path %s \nportInner %s \nserverWait %s \ndbDir %s",csvInputPath,portInner,serverWait,dbDir);
String parameters = String.format("\nInput path %s \nportInner %s \ndbDir %s",csvInputPath,portInner,dbDir);
log.info(parameters);
@@ -40,7 +37,7 @@ public class ImportPairs2DB {
cmd = String.format(cmd, dbDir,pj.getName() +".rdb", portInt);
log.info(cmd);
CallShell cs = new CallShell();
cs.runShell(cmd,serverWait, portInner);
cs.runShell(cmd, portInner);
cmd = "bash "+datasetPath + "/redisSingleImport.sh" +" %s %s";
@@ -50,7 +47,7 @@ public class ImportPairs2DB {
String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
String stopServer2 = String.format(stopServer,portInt);
cs.runShell(stopServer2,serverWait, portInner);
cs.runShell(stopServer2, portInner);
portInt++;
@@ -1,14 +1,13 @@
package edu.lu.uni.serval.fixminer.cluster;
import com.github.gumtreediff.actions.ActionGenerator;
import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.utils.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
@@ -291,17 +290,17 @@ public class MultiThreadTreeLoader {
readMessageFiles(fileToCompare,port);
}
public static void processMessages(String inputPath, String outputPath) {
File folder = new File(outputPath + "pairs_splitted/");
File[] listOfFiles = folder.listFiles();
Stream<File> stream = Arrays.stream(listOfFiles);
List<File> pjs = stream
.filter(x -> !x.getName().startsWith("."))
.collect(Collectors.toList());
FileHelper.createDirectory(outputPath + "comparison_splitted/");
pjs.parallelStream()
.forEach(m -> coreLoop(m, outputPath,inputPath));
}
// public static void processMessages(String inputPath, String outputPath) {
// File folder = new File(outputPath + "pairs_splitted/");
// File[] listOfFiles = folder.listFiles();
// Stream<File> stream = Arrays.stream(listOfFiles);
// List<File> pjs = stream
// .filter(x -> !x.getName().startsWith("."))
// .collect(Collectors.toList());
// FileHelper.createDirectory(outputPath + "comparison_splitted/");
// pjs.parallelStream()
// .forEach(m -> coreLoop(m, outputPath,inputPath));
// }
public static ITree getSimpliedTree(String fn) {
@@ -377,45 +376,45 @@ public class MultiThreadTreeLoader {
return parent;
}
public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children){
int newType = 0;
Action action = actionSet.getAction();
if (action instanceof Update){
newType = 101;
}else if(action instanceof Insert){
newType =100;
}else if(action instanceof Move){
newType = 102;
}else if(action instanceof Delete){
newType=103;
}else{
new Exception("unknow action");
}
if(actionSet.getParent() == null){
//root
parent = new Tree(newType,"");
}else{
children = new Tree(newType,"");
parent.addChild(children);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getActionTree(subAction,children,null);
}
}
return parent;
}
// public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children){
//
// int newType = 0;
//
// Action action = actionSet.getAction();
// if (action instanceof Update){
// newType = 101;
// }else if(action instanceof Insert){
// newType =100;
// }else if(action instanceof Move){
// newType = 102;
// }else if(action instanceof Delete){
// newType=103;
// }else{
// new Exception("unknow action");
// }
// if(actionSet.getParent() == null){
// //root
//
// parent = new Tree(newType,"");
// }else{
// children = new Tree(newType,"");
// parent.addChild(children);
// }
// List<HierarchicalActionSet> subActions = actionSet.getSubActions();
// if (subActions.size() != 0){
// for (HierarchicalActionSet subAction : subActions) {
//
// if(actionSet.getParent() == null){
// children = parent;
// }
// getActionTree(subAction,children,null);
//
// }
//
//
// }
// return parent;
// }
@@ -1,22 +1,29 @@
package edu.lu.uni.serval.fixminer.cluster;
import com.github.gumtreediff.actions.ActionGenerator;
import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.FixPattern.utils.EDiff;
import edu.lu.uni.serval.FixPattern.utils.EDiffHelper;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.utils.FileHelper;
import org.javatuples.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import java.io.*;
import java.time.Duration;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -28,28 +35,27 @@ public class MultiThreadTreeLoaderCluster {
private static Logger log = LoggerFactory.getLogger(MultiThreadTreeLoaderCluster.class);
public static void mainCompare(String port,String pairsCSVPath,String importScript,String dbDir,String chunkName,String dumpName,String portInner,String serverWait,String type) throws Exception {
public static void mainCompare(String port,String pairsCSVPath,String importScript,String dbDir,String chunkName,String dumpName,String portInner,String type) throws Exception {
CallShell cs = new CallShell();
String cmd1 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd1 = String.format(cmd1, dbDir,chunkName,Integer.valueOf(portInner));
// edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis(cmd1,"1000");
cs.runShell(cmd1,serverWait, port);
cs.runShell(cmd1, port);
String cmd2 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd2 = String.format(cmd2, dbDir,dumpName,Integer.valueOf(port));
cs.runShell(cmd2,serverWait, port);
// edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis(cmd2,"10000");
cs.runShell(cmd2, port);
String cmd3;
cmd3 = "bash " + importScript +" %s %s";
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(portInner),20000000);
JedisPool jedisPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(portInner),20000000);
JedisPool outerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(port),20000000);
JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(port),20000000);
File folder = new File(pairsCSVPath);
@@ -104,29 +110,16 @@ public class MultiThreadTreeLoaderCluster {
}
String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
String stopServer1 = String.format(stopServer,Integer.valueOf(portInner));
// loadRedis(stopServer2,serverWait);
cs.runShell(stopServer1,serverWait, port);
cs.runShell(stopServer1, port);
String stopServer2 = String.format(stopServer,Integer.valueOf(port));
// loadRedis(stopServer2,serverWait);
cs.runShell(stopServer2,serverWait, port);
cs.runShell(stopServer2, port);
}
/** Read the object from Base64 string. */
public static Object fromString( String s ) throws IOException ,
ClassNotFoundException {
byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return o;
}
public static Pair<ITree,String> getTree(String firstValue, JedisPool outerPool,String type){
@@ -148,13 +141,13 @@ public class MultiThreadTreeLoaderCluster {
inner = outerPool.getResource();
String filename = project + "/"+type+"/" + pureFileName + ".txt_" + actionSetPosition;
String si= inner.get(filename);
HierarchicalActionSet actionSet = (HierarchicalActionSet) fromString(si);
HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.fromString(si);
ITree parent = null;
ITree children =null;
TreeContext tc = new TreeContext();
tree = getActionTree(actionSet, parent, children,tc);
tree = EDiff.getActionTree(actionSet, parent, children,tc);
tree.setParent(null);
tc.validate();
@@ -179,50 +172,7 @@ public class MultiThreadTreeLoaderCluster {
}
public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc){
int newType = 0;
Action action = actionSet.getAction();
if (action instanceof Update){
newType = 101;
}else if(action instanceof Insert){
newType =100;
}else if(action instanceof Move){
newType = 102;
}else if(action instanceof Delete){
newType=103;
}else{
new Exception("unknow action");
}
if(actionSet.getParent() == null){
//root
parent = tc.createTree(newType, "", null);
tc.setRoot(parent);
// parent = new Tree(newType,"");
}else{
children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent);
// children = new Tree(newType,"");
// parent.addChild(children);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getActionTree(subAction,children,null,tc);
}
}
return parent;
}
private static void coreCompare(String name , JedisPool jedisPool,String clusterName,JedisPool outerPool,String type) {
@@ -383,26 +333,6 @@ public class MultiThreadTreeLoaderCluster {
static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true);
return poolConfig;
}
// return msgFiles;
}
@@ -2,24 +2,26 @@ package edu.lu.uni.serval.fixminer.cluster;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.FixPattern.utils.EDiffHelper;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import edu.lu.uni.serval.utils.FileHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.similarity.JaroWinklerDistance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import java.io.*;
import java.time.Duration;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static edu.lu.uni.serval.fixminer.cluster.MultiThreadTreeLoader.getKeysByValue;
import static edu.lu.uni.serval.fixminer.cluster.MultiThreadTreeLoaderCluster.fromString;
/**
* Created by anilkoyuncu on 19/03/2018.
@@ -30,27 +32,24 @@ public class MultiThreadTreeLoaderCluster3 {
private static Logger log = LoggerFactory.getLogger(MultiThreadTreeLoaderCluster3.class);
// public static void mainCompare(String inputPath,String port,String pairsCSVPath,String importScript) {
public static void mainCompare(String port,String pairsCSVPath,String importScript,String dbDir,String chunkName,String dumpName,String portInner,String serverWait, String type) throws Exception {
public static void mainCompare(String port,String pairsCSVPath,String importScript,String dbDir,String chunkName,String dumpName,String portInner,String type) throws Exception {
CallShell cs = new CallShell();
String cmd1 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd1 = String.format(cmd1, dbDir,chunkName,Integer.valueOf(portInner));
// edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis(cmd1,"1000");
cs.runShell(cmd1,serverWait, port);
cs.runShell(cmd1, port);
String cmd2 = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd2 = String.format(cmd2, dbDir,dumpName,Integer.valueOf(port));
// edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis(cmd2,"10000");
cs.runShell(cmd2,serverWait, port);
cs.runShell(cmd2, port);
String cmd3;
cmd3 = "bash " + importScript +" %s %s";
JedisPool jedisPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(portInner),20000000);
JedisPool jedisPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(portInner),20000000);
JedisPool outerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(port),20000000);
JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(port),20000000);
@@ -79,8 +78,7 @@ public class MultiThreadTreeLoaderCluster3 {
if (size == 0) {
String comd = String.format(cmd3, f.getPath(), portInner);
cs.runShell(comd);
// edu.lu.uni.serval.fixminer.cluster.MultiThreadTreeLoaderCluster.
// loadRedis(comd);
scan = jedis.scan("0", sc);
size = scan.getResult().size();
@@ -103,17 +101,10 @@ public class MultiThreadTreeLoaderCluster3 {
String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
String stopServer1 = String.format(stopServer,Integer.valueOf(portInner));
// loadRedis(stopServer2,serverWait);
cs.runShell(stopServer1,serverWait, port);
cs.runShell(stopServer1, port);
String stopServer2 = String.format(stopServer,Integer.valueOf(port));
// loadRedis(stopServer2,serverWait);
cs.runShell(stopServer2,serverWait, port);
// }
cs.runShell(stopServer2, port);
@@ -151,13 +142,13 @@ public class MultiThreadTreeLoaderCluster3 {
inner = outerPool.getResource();
String fn = project + "/"+type+"/" + s + ".txt_" + actionSetPosition;
String si= inner.get(fn);
HierarchicalActionSet actionSet = (HierarchicalActionSet) fromString(si);
HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.fromString(si);
ITree parent = null;
ITree children =null;
TreeContext tc = new TreeContext();
tree = getASTTree(actionSet, parent, children,tc);
tree = EDiffHelper.getASTTree(actionSet, parent, children,tc);
// tree.setParent(null);
tc.validate();
@@ -176,46 +167,7 @@ public class MultiThreadTreeLoaderCluster3 {
}
public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc){
int newType = 0;
String astNodeType = actionSet.getAstNodeType();
String label = actionSet.getAction().toString();
List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType);
if(keysByValue.size() != 1){
log.error("Birden cok astnodemapmapping");
}
newType = keysByValue.get(0);
if(actionSet.getParent() == null){
//root
// parent = new Tree(newType,"");
parent = tc.createTree(newType, label, null);
tc.setRoot(parent);
}else{
// children = new Tree(newType,"");
// parent.addChild(children);
children = tc.createTree(newType, label, null);
children.setParentAndUpdateChildren(parent);
}
List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){
for (HierarchicalActionSet subAction : subActions) {
if(actionSet.getParent() == null){
children = parent;
}
getASTTree(subAction,children,null,tc);
}
}
return parent;
}
public static List<String> getNames(ITree oldTree, List<String> oldTokens){
@@ -489,31 +441,6 @@ public class MultiThreadTreeLoaderCluster3 {
String firstValue = resultMap.get("0");
String secondValue = resultMap.get("1");
// if(firstValue.equals("7/0/7af9e0_e674f1_spring-social-web#src#main#java#org#springframework#social#connect#web#ConnectController.txt_0_SOCIAL") || secondValue.equals("7/0/7af9e0_e674f1_spring-social-web#src#main#java#org#springframework#social#connect#web#ConnectController.txt_0_SOCIAL")){
// log.info("");
// }
///35/1/22b5f7_84bf27_ui#org.eclipse.pde.runtime#src#org#eclipse#pde#internal#runtime#registry#RegistryBrowserLabelProvider.txt_2_PDE
// firstValue = inputPath + firstValue;
// secondValue = inputPath + secondValue;
// String[] firstValueSplit = firstValue.split("/");
// String[] secondValueSplit = secondValue.split("/");
//
// if (firstValueSplit.length == 1) {
// firstValue = inputPath + firstValueSplit[0];
// } else {
// firstValue = inputPath + firstValueSplit[1];
// }
//
// if (secondValueSplit.length == 1) {
// secondValue = inputPath + secondValueSplit[0];
// } else {
// secondValue = inputPath + secondValueSplit[1];
// }
try {
ITree oldTree = getTree(firstValue,outerPool,type);
@@ -526,30 +453,18 @@ public class MultiThreadTreeLoaderCluster3 {
return;
}
// ITree oldTree = oldPair.getValue0();
// ITree newTree = newPair.getValue0();
//
// String oldProject = oldPair.getValue1();
// String newProject = newPair.getValue1();
List<String> oldTokens = new ArrayList<>();
List<String> newTokens = new ArrayList<>();
// if(secondValue.startsWith("/2/")){
// log.info("newss");
// }
// 41 return statement
oldTokens = getNames(oldTree,oldTokens);
newTokens = getNames(newTree,newTokens);
// if(oldTokens.size() == 0 || newTokens.size() == 0){
// log.error("Cluster {} has no tokens on pair {}",clusterName , name);
// }
// Matcher m = Matchers.getInstance().getMatcher(oldTree, newTree);
// m.match();
CharSequence[] oldSequences = oldTokens.toArray(new CharSequence[oldTokens.size()]);
CharSequence[] newSequences = newTokens.toArray(new CharSequence[newTokens.size()]);
JaroWinklerDistance jwd = new JaroWinklerDistance();
@@ -572,14 +487,7 @@ public class MultiThreadTreeLoaderCluster3 {
int retval = Double.compare(overallSimi, Double.valueOf(0.8));
if(retval >= 0){
// log.info("YES");
// log.info(name);
// log.info(firstValue);
// log.info(secondValue);
// log.info("************");
// if(!overallSimi.equals(1.0)){
// log.info("");
// }
String matchKey = "match-"+clusterName+"_" + (String.valueOf(i)) + "_" + String.valueOf(j);
String result = firstValue + "," + secondValue + ","+String.join(",", oldTokens);
jedis.select(1);
@@ -705,30 +613,6 @@ public class MultiThreadTreeLoaderCluster3 {
static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true);
return poolConfig;
}
// return msgFiles;
}
@@ -1,5 +1,7 @@
package edu.lu.uni.serval.fixminer.cluster;
import edu.lu.uni.serval.FixPattern.utils.EDiffHelper;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import edu.lu.uni.serval.gumtree.regroup.HierarchicalActionSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,50 +11,28 @@ import redis.clients.jedis.JedisPool;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static edu.lu.uni.serval.fixminer.cluster.TreeLoaderClusterL1.poolConfig;
/**
* Created by anilkoyuncu on 03/04/2018.
*/
public class StoreFile {
public class StoreEDiffInCache {
private static Logger log = LoggerFactory.getLogger(StoreFile.class);
private static Logger log = LoggerFactory.getLogger(StoreEDiffInCache.class);
// public static void main(String[] args) {
public static void main(String inputPath,String portInner,String serverWait,String dbDir,String chunkName,String operation) throws Exception {
// String inputPath;
// String portInner;
// String serverWait;
// String dbDir;
// String chunkName;
// String numOfWorkers;
// if (args.length > 0) {
// inputPath = args[0];
// portInner = args[1];
// serverWait = args[2];
// chunkName = args[3];
// numOfWorkers = args[4];
// dbDir = args[5];
// } else {
// inputPath = "/Users/anilkoyuncu/bugStudy/dataset/GumTreeOutput2";
// portInner = "6399";
// serverWait = "10000";
// chunkName ="dumps.rdb";
// dbDir = "/Users/anilkoyuncu/bugStudy/dataset/redis";
// numOfWorkers = "1";
// }
String parameters = String.format("\nInput path %s \nportInner %s \nserverWait %s \nchunkName %s \ndbDir %s \noperation %s",inputPath,portInner,serverWait,chunkName,dbDir,operation);
public static void main(String inputPath,String portInner,String dbDir,String chunkName,String operation) throws Exception {
String parameters = String.format("\nInput path %s \nportInner %s \nchunkName %s \ndbDir %s \noperation %s",inputPath,portInner,chunkName,dbDir,operation);
log.info(parameters);
CallShell cs = new CallShell();
String cmd = "bash "+dbDir + "/" + "startServer.sh" +" %s %s %s";
cmd = String.format(cmd, dbDir,chunkName,Integer.valueOf(portInner));
// loadRedis(cmd,serverWait);
cs.runShell(cmd,serverWait, portInner);
cs.runShell(cmd, portInner);
File folder = new File(inputPath);
File[] subFolders = folder.listFiles();
@@ -98,7 +78,7 @@ public class StoreFile {
}
log.info(String.valueOf(workList.size()));
JedisPool innerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(portInner),20000000);
JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(portInner),20000000);
workList.stream().parallel()
.forEach(m -> storeCore(innerPool, m.split(",")[1],m.split(",")[0]));
@@ -107,10 +87,12 @@ public class StoreFile {
String stopServer = "bash "+dbDir + "/" + "stopServer.sh" +" %s";
String stopServer2 = String.format(stopServer,Integer.valueOf(portInner));
// loadRedis(stopServer2,serverWait);
cs.runShell(stopServer2,serverWait, portInner);
cs.runShell(stopServer2, portInner);
}
public static void storeCore(JedisPool innerPool,String path,String key){
try {
@@ -141,7 +123,7 @@ public class StoreFile {
try (Jedis inner = innerPool.getResource()) {
inner.set(key,toString(actionSet));
inner.set(key, EDiffHelper.toString(actionSet));
}
@@ -155,25 +137,7 @@ public class StoreFile {
}
/** Read the object from Base64 string. */
private static Object fromString( String s ) throws IOException ,
ClassNotFoundException {
byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) );
Object o = ois.readObject();
ois.close();
return o;
}
/** Write the object to a Base64 string. */
private static String toString( Serializable o ) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( o );
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
}
@@ -8,13 +8,10 @@ import akka.routing.RoundRobinPool;
import edu.lu.uni.serval.fixminer.cluster.akka.TreeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.loadRedis;
public class TreeActor extends UntypedActor {
private static Logger logger = LoggerFactory.getLogger(TreeActor.class);
@@ -1,11 +1,14 @@
package edu.lu.uni.serval.fixminer.cluster;
import edu.lu.uni.serval.FixPattern.utils.PoolBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import java.io.File;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@@ -18,19 +21,19 @@ public class TreeLoaderClusterL1 {
private static Logger log = LoggerFactory.getLogger(TreeLoaderClusterL1.class);
public static void main(String portInner,String serverWait,String port,String inputPath,String level1DB,String level1Path,String innerTypePrefix) throws Exception {
public static void main(String portInner,String port,String inputPath,String level1DB,String level1Path,String innerTypePrefix) throws Exception {
String parameters = String.format("\nInput path %s \nportInner %s \nserverWait %s \nport %s",inputPath,portInner,serverWait,port);
String parameters = String.format("\nInput path %s \nportInner %s \nport %s",inputPath,portInner,port);
log.info(parameters);
String cmd = "bash "+inputPath + "/" + "startServer.sh" +" %s %s %s";
cmd = String.format(cmd, inputPath,level1DB,Integer.valueOf(port));
CallShell cs = new CallShell();
cs.runShell(cmd,serverWait, port);
JedisPool outerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(port),20000000);
cs.runShell(cmd, port);
JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(port),20000000);
File chunks = new File(level1Path);
@@ -44,8 +47,8 @@ public class TreeLoaderClusterL1 {
String cmdInner = "bash "+inputPath + "/" + "startServer.sh" +" %s %s %s";
cmdInner = String.format(cmdInner, inputPath,db.getName(),Integer.valueOf(portInner));
cs.runShell(cmdInner,serverWait, port);
JedisPool innerPool = new JedisPool(poolConfig, "127.0.0.1",Integer.valueOf(portInner),20000000);
cs.runShell(cmdInner, port);
JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1",Integer.valueOf(portInner),20000000);
Jedis inner = null;
@@ -97,38 +100,21 @@ public class TreeLoaderClusterL1 {
String stopServer = "bash "+inputPath + "/" + "stopServer.sh" +" %s";
stopServer = String.format(stopServer,Integer.valueOf(portInner));
// loadRedis(stopServer,serverWait);
cs.runShell(stopServer,serverWait, port);
cs.runShell(stopServer, port);
}
String stopServer1 = "bash "+inputPath + "/" + "stopServer.sh" +" %s";
stopServer1 = String.format(stopServer1,Integer.valueOf(port));
// loadRedis(stopServer,serverWait);
cs.runShell(stopServer1,serverWait, port);
cs.runShell(stopServer1, port);
}
static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
poolConfig.setMaxIdle(128);
poolConfig.setMinIdle(16);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3);
poolConfig.setBlockWhenExhausted(true);
return poolConfig;
}
}
@@ -8,14 +8,10 @@ import edu.lu.uni.serval.fixminer.cluster.akka.TreeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.*;
import static edu.lu.uni.serval.fixminer.cluster.AkkaTreeLoader.poolConfig;
public class TreeWorker extends UntypedActor {
private static Logger log = LoggerFactory.getLogger(TreeWorker.class);
@@ -1,254 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.ArrayList;
import java.util.List;
public class ActionFilter {
private List<String> methodNames = new ArrayList<>();
private List<String> variableNames = new ArrayList<>();
/**
* Filter out the modify actions of changing method names, method parameters, variable names and field names in declaration part.
*
* @param actionSets
* @return
*/
public List<HierarchicalActionSet> filterOutUselessActions(List<HierarchicalActionSet> actionSets) {
// Filter out modifications of variable names and method names.
List<HierarchicalActionSet> uselessActions = findoutUselessActions(actionSets);
actionSets.removeAll(uselessActions);
uselessActions.clear();
// Filter out non-UPD modifications, and modifications of variable names and method names.
uselessActions = findoutUselessActionSets(actionSets, true);
actionSets.removeAll(uselessActions);
uselessActions.clear();
return actionSets;
}
private List<HierarchicalActionSet> findoutUselessActionSets(List<HierarchicalActionSet> actionSets, boolean isRoot) {
List<HierarchicalActionSet> uselessActions = new ArrayList<>();
FindActionSet: {
for (HierarchicalActionSet actionSet : actionSets) {
if (!isRoot) {
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("UPD MethodInvocation") || actionStr.startsWith("INS MethodInvocation") || actionStr.startsWith("DEL MethodInvocation")) {
String label = actionSet.getAction().getNode().getLabel();
for (String methodName : methodNames) {
if (actionSet.getActionString().startsWith("UPD MethodInvocation@@" + methodName + "(")
|| actionSet.getActionString().startsWith("INS MethodInvocation@@" + methodName + "(")
|| actionSet.getActionString().startsWith("DEL MethodInvocation@@" + methodName + "(")
|| label.contains("." + methodName + "(")) {
addToUselessActions(actionSet, uselessActions);
break FindActionSet;
}
}
} else if (actionStr.startsWith("UPD SimpleName") || actionStr.startsWith("INS SimpleName") || actionStr.startsWith("DEL SimpleName")) {
String label = actionSet.getAction().getNode().getLabel();
for (String variableName : variableNames) {
if (label.equals(variableName) || label.equals("Name:" + variableName)) {
addToUselessActions(actionSet, uselessActions);
break FindActionSet;
}
}
} else if (actionStr.startsWith("UPD StringLiteral") || actionStr.startsWith("INS StringLiteral")
|| actionStr.startsWith("DEL StringLiteral") || actionStr.startsWith("MOV StringLiteral")
|| actionStr.startsWith("UPD CharacterLiteral") || actionStr.startsWith("INS CharacterLiteral")
|| actionStr.startsWith("DEL CharacterLiteral") || actionStr.startsWith("MOV CharacterLiteral")) {
addToUselessActions(actionSet, uselessActions);
break FindActionSet;
}
List<HierarchicalActionSet> uselessActionSets = findoutUselessActionSets(actionSet.getSubActions(), false);
if (uselessActionSets.size() > 0) {
uselessActions.addAll(uselessActionSets);
break;
}
} else {
if (actionSet.getAstNodeType().endsWith("Statement") || "FieldDeclaration".equals(actionSet.getAstNodeType())) {
uselessActions.addAll(findoutUselessActionSets(actionSet.getSubActions(), false));
} else {
uselessActions.add(actionSet);
}
}
}
}
return uselessActions;
}
private void addToUselessActions(HierarchicalActionSet actionSet, List<HierarchicalActionSet> uselessActions) {
while (actionSet.getParent() != null) {
actionSet = actionSet.getParent();
}
if (!uselessActions.contains(actionSet)) {
uselessActions.add(actionSet);
}
}
/**
* Identify the the modify actions of changing method names, method parameters, variable names and field names in declaration part.
*
* @param actionSets
* @return
*/
private List<HierarchicalActionSet> findoutUselessActions(List<HierarchicalActionSet> actionSets) {
List<HierarchicalActionSet> uselessActions = new ArrayList<>();
for (HierarchicalActionSet actionSet : actionSets) {
String actionType = actionSet.getAstNodeType();
if (actionType.equals("MethodDeclaration")) {
addToUselessActions(actionSet, uselessActions);// INS, DEL: useful?, UPD, except the modifier actions
if (!actionSet.getActionString().startsWith("MOV ")) {
String label = actionSet.getNode().getLabel();
String methodName = label.substring(label.indexOf("MethodName:"));
methodName = methodName.substring(11, methodName.indexOf(","));
methodNames.add(methodName); // "MethodName:***"
// UPD, DEL, INS parameters.
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
for (HierarchicalActionSet subActionSet : subActionSets) {
if (subActionSet.getAstNodeType().equals("SingleVariableDeclaration")) {
List<HierarchicalActionSet> subActionSets2 = subActionSet.getSubActions(); // <Type, identifier>
if (subActionSets2.size() == 0) {
String actSetStr = subActionSet.getActionString();
int index1 = actSetStr.indexOf("@@");
int index2 = 0;
if (actSetStr.startsWith("DEL")) {
index2 = actSetStr.indexOf("@AT@");
} else {
index2 = actSetStr.indexOf("@TO@");;
}
actSetStr = actSetStr.substring(index1, index2).trim();
String variableName = actSetStr.substring(actSetStr.lastIndexOf(" "));
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
} else {
HierarchicalActionSet actSet = subActionSets2.get(subActionSets2.size() - 1);
String actStr = actSet.getActionString();
if (actStr.startsWith("UPD SimpleName") || actStr.startsWith("INS SimpleName") || actStr.startsWith("DEL SimpleName")) {
String variableName = actSet.getNode().getLabel();
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
}
}
}
}
}
} else if (actionType.equals("FieldDeclaration") || actionType.equals("VariableDeclarationStatement")) {
// UPD VariableDeclarationFragment
if (!actionSet.getActionString().startsWith("MOV ")) {
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets.size() > 0) {
for (HierarchicalActionSet subActionSet : subActionSets) { // VariableDeclarationFragments
if (identifyUpdateVDF(subActionSet)) {
addToUselessActions(actionSet, uselessActions);
}
}
}
}
} else if (actionType.equals("TryStatement")) {
if (actionSet.getActionString().startsWith("UPD ")) {
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets.size() > 0) {
for (HierarchicalActionSet subActionSet : subActionSets) {
if (subActionSet.getActionString().startsWith("UPD VariableDeclarationExpression")) {
List<HierarchicalActionSet> subActionSets2 = subActionSet.getSubActions(); // VariableDeclarationFragments
for (HierarchicalActionSet subActionSet2 : subActionSets2) {
if (identifyUpdateVDF(subActionSet2)) {
addToUselessActions(actionSet, uselessActions);
}
}
} else {
break;
}
}
}
}
} else if (actionType.equals("EnhancedForStatement")) { // SingleVariableDeclaration
if (!actionSet.getActionString().startsWith("MOV ")) {
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets.size() > 0) {
HierarchicalActionSet subActionSet = subActionSets.get(0);
if (subActionSet.getActionString().startsWith("UPD SingleVariableDeclaration")) {
List<HierarchicalActionSet> subActionSets2 = subActionSet.getSubActions();
for (HierarchicalActionSet subActionSet2 : subActionSets2) { // Type or Identifier
if (subActionSet2.getActionString().startsWith("UPD SimpleName")) {
String variableName = subActionSet2.getNode().getLabel();
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
addToUselessActions(actionSet, uselessActions);
}
}
}
}
}
} else if (actionType.equals("SingleVariableDeclaration")) {
if (!actionSet.getActionString().startsWith("MOV ")) {
List<HierarchicalActionSet> subActionSets2 = actionSet.getSubActions(); // <Type, identifier>
if (subActionSets2.size() == 0) {
String actSetStr = actionSet.getActionString();
int index1 = actSetStr.indexOf("@@");
int index2 = 0;
if (actSetStr.startsWith("DEL")) {
index2 = actSetStr.indexOf("@AT@");
} else {
index2 = actSetStr.indexOf("@TO@");;
}
actSetStr = actSetStr.substring(index1, index2).trim();
String variableName = actSetStr.substring(actSetStr.lastIndexOf(" "));
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
addToUselessActions(actionSet, uselessActions);
} else {
HierarchicalActionSet actSet = subActionSets2.get(subActionSets2.size() - 1);
String actStr = actSet.getActionString();
if (actStr.startsWith("UPD SimpleName") || actStr.startsWith("INS SimpleName") || actStr.startsWith("DEL SimpleName")) {
String variableName = actSet.getNode().getLabel();
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
addToUselessActions(actionSet, uselessActions);
}
}
}
} else {
if (actionSet.getParent() != null) {
while (actionSet.getParent() != null) {
actionSet = actionSet.getParent();
}
if (uselessActions.contains(actionSet)) {
return uselessActions;
} else {
uselessActions.addAll(findoutUselessActions(actionSet.getSubActions()));
}
}
}
}
return uselessActions;
}
/**
* Identify the AST node of this ActionSet is VariableDeclarationFragment or not.
* And, whether the action is happened on the Variable name or not.
*
* @param actionSet
*/
private boolean identifyUpdateVDF(HierarchicalActionSet actionSet) {
String actStr = actionSet.getActionString();
if (actStr.startsWith("UPD VariableDeclarationFragment")
|| actStr.startsWith("INS VariableDeclarationFragment")
|| actStr.startsWith("DEL VariableDeclarationFragment")) {
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets == null || subActionSets.size() == 0) {
// modification of Dimension
return true;
}
HierarchicalActionSet actSet = subActionSets.get(0);
String actSetStr = actSet.getActionString();
if (actSetStr.startsWith("UPD SimpleName") || actSetStr.startsWith("INS SimpleName") || actSetStr.startsWith("DEL SimpleName")) {
String variableName = actSet.getNode().getLabel();
variableNames.add(variableName); // "SimpleName:" + variableName TODO: effect range
return true;
}
}
return false;
}
}
@@ -1,12 +1,12 @@
package edu.lu.uni.serval.gumtree.regroup;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.tree.ITree;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.tree.ITree;
/**
* Hierarchical-level results of GumTree results
*
@@ -29,11 +29,16 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,
private List<HierarchicalActionSet> subActions = new ArrayList<>();
private ITree node;
private SimpleTree abstractSimpleTree = null; // semi-source code tree. and AST node type tree
private SimpleTree abstractIdentifierTree = null; // abstract identifier tree
private SimpleTree simpleTree = null; // source code tree and AST node type tree
private SimpleTree originalTree = null; // source code tree.
// source code tree.
public int getBugEndPosition() {
return bugEndPosition;
}
public int getFixEndPosition() {
return fixEndPosition;
}
private int bugEndPosition;
private int fixEndPosition;
@@ -147,49 +152,12 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,
this.subActions = subActions;
}
public SimpleTree getAbstractSimpleTree() {
return abstractSimpleTree;
}
public void setAbstractSimpleTree(SimpleTree simpleTree) {
this.abstractSimpleTree = simpleTree;
}
public SimpleTree getAbstractIdentifierTree() {
return abstractIdentifierTree;
}
public void setAbstractIdentifierTree(SimpleTree abstractIdentifierTree) {
this.abstractIdentifierTree = abstractIdentifierTree;
}
public SimpleTree getSimpleTree() {
return simpleTree;
}
public void setSimpleTree(SimpleTree rawTokenTree) {
this.simpleTree = rawTokenTree;
}
public SimpleTree getOriginalTree() {
return originalTree;
}
public void setOriginalTree(SimpleTree originalTree) {
this.originalTree = originalTree;
}
public int getBugEndPosition() {
return bugEndPosition;
}
public void setBugEndPosition(int bugEndPosition) {
this.bugEndPosition = bugEndPosition;
}
public int getFixEndPosition() {
return fixEndPosition;
}
public void setFixEndPosition(int fixEndPosition) {
this.fixEndPosition = fixEndPosition;
@@ -197,10 +165,7 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,
@Override
public int compareTo(HierarchicalActionSet o) {
// int result = this.startPosition.compareTo(o.startPosition);
// if (result == 0) {
// result = this.length >= o.length ? -1 : 1;
// }
return this.startPosition.compareTo(o.startPosition);//this.action.compareTo(o.action);
}
@@ -238,28 +203,5 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,
return str;
}
public String toASTNodeLevelAction() {
if (strList.size() == 0) {
toString();
}
String astNodeStr = "";
for (String str : strList) {
astNodeStr += str.substring(0, str.indexOf("@@")) + "\n";
}
return astNodeStr;
}
public String toRawCodeLevelAction() {
if (strList.size() == 0) {
toString();
}
String astNodeStr = "";
for (String str : strList) {
str = str.substring(0, str.indexOf(" @AT@")) + "\n";
int index1 = str.indexOf(" ") + 1;
int index2 = str.indexOf("@@") + 2;
astNodeStr += str.substring(0, index1) + str.substring(index2);
}
return astNodeStr;
}
}
@@ -1,22 +1,13 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.io.File;
import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.utils.ListSorter;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Addition;
import com.github.gumtreediff.actions.model.Delete;
import com.github.gumtreediff.actions.model.Insert;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.gumtree.GumTreeComparer;
import edu.lu.uni.serval.utils.ListSorter;
/**
* Regroup GumTree results to a hierarchical construction.
*
@@ -209,11 +200,6 @@ public class HierarchicalRegrouper {
}
// } else if (parent.getType() == 31) { // method declaration
// int type = action.getNode().getType();
// if (Checker.isStatement(type)) {// statements
// return null;
// }
}
for (Action act : actions) {
@@ -1,300 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Addition;
import com.github.gumtreediff.actions.model.Delete;
import com.github.gumtreediff.actions.model.Insert;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.CNodeMap;
import edu.lu.uni.serval.utils.ListSorter;
/**
* Regroup GumTree results to a hierarchical construction.
*
* @author kui.liu
*
*/
public class HierarchicalRegrouperForC {
// public static void main(String[] args) {
// GumTreeComparer com = new GumTreeComparer();
// File cFile1 = new File("/Users/anilkoyuncu/bugStudy/dataset/GumTreeInput/linux-stable/prevFiles/prev_0a3d00_b404bc_drivers#pci#iov.c");
// File cFile2 = new File("/Users/anilkoyuncu/bugStudy/dataset/GumTreeInput/linux-stable/revFiles/0a3d00_b404bc_drivers#pci#iov.c");
// List<Action> action = com.compareTwoFilesWithGumTreeForCCode(cFile1, cFile2);
// List<HierarchicalActionSet> actionSet = new HierarchicalRegrouperForC().regroupGumTreeResults(action);
// System.out.println(actionSet);
// }
List<HierarchicalActionSet> actionSets = new ArrayList<>();
public List<HierarchicalActionSet> regroupGumTreeResults(List<Action> actions) {
/*
* First, sort actions by their positions.
*/
// List<Action> actions = new ListSorter<Action>(actionsArgu).sortAscending();
// if (actions == null) {
// actions = actionsArgu;
// }
/*
* Second, group actions by their positions.
*/
HierarchicalActionSet actionSet = null;
for(Action act : actions){
Action parentAct = findParentAction(act, actions);
if (parentAct == null) {
actionSet = createActionSet(act, parentAct, null);
actionSets.add(actionSet);
} else {
if (!addToAactionSet(act, parentAct, actionSets)) {
// The index of the parent action in the actions' list is larger than the index of this action.
actionSet = createActionSet(act, parentAct, null);
actionSets.add(actionSet);
}
}
}
/*
* Third, add the subActionSet to its parent ActionSet.
*/
List<HierarchicalActionSet> reActionSets = new ArrayList<>();
for (HierarchicalActionSet actSet : actionSets) {
Action parentAct = actSet.getParentAction();
if (parentAct != null) {
addToActionSets(actSet, parentAct, actionSets);
} else {
// TypeDeclaration, FieldDeclaration, MethodDeclaration, Statement.
// CatchClause, ConstructorInvocation, SuperConstructorInvocation, SwitchCase
// String astNodeType = actSet.getAstNodeType();
// if (astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") ||
// astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") ||
// astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase")) {
reActionSets.add(actSet);
// }
}
}
return reActionSets;
}
private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) {
HierarchicalActionSet actionSet = new HierarchicalActionSet();
actionSet.setAction(act);
String actStr = parseAction(act.toString());
actionSet.setActionString(actStr);
actionSet.setParentAction(parentAct);
actionSet.setNode(act.getNode());
actionSet.setParent(parent);
int bugStartLineNum = 0;
int bugEndLineNum = 0;
int fixStartLineNum = 0;
int fixEndLineNum = 0;
if (actStr.startsWith("INS")) {
Insert insert = (Insert) act;
// int pos = insert.getPos();//index position of the new node in the children array list of its corresponding old parent node.
ITree newTree = insert.getNode();
fixStartLineNum = newTree.getPos();
fixEndLineNum = newTree.getLength();
} else {
ITree tree = act.getNode();
bugStartLineNum = tree.getPos();
bugEndLineNum = tree.getLength();
if (actStr.startsWith("UPD")) {
Update update = (Update) act;
ITree newTree = update.getNewNode();
fixStartLineNum = newTree.getPos();
fixEndLineNum = newTree.getLength();
} else if (actStr.startsWith("MOV")) {
Move update = (Move) act;
ITree newTree = update.getNewNode();
fixStartLineNum = newTree.getPos();
fixEndLineNum = newTree.getLength();
}
}
actionSet.setBugStartLineNum(bugStartLineNum);
actionSet.setBugEndLineNum(bugEndLineNum);
actionSet.setFixStartLineNum(fixStartLineNum);
actionSet.setFixEndLineNum(fixEndLineNum);
return actionSet;
}
private String parseAction(String actStr1) {
// UPD 25@@!a from !a to isTrue(a) at 69
String[] actStrArrays = actStr1.split("@@");
String actStr = "";
int length = actStrArrays.length;
for (int i =0; i < length - 1; i ++) {
String actStrFrag = actStrArrays[i];
int index = actStrFrag.lastIndexOf(" ") + 1;
String nodeType = actStrFrag.substring(index);
if (!"".equals(nodeType)) {
if (Character.isDigit(nodeType.charAt(0)) || (nodeType.startsWith("-") && Character.isDigit(nodeType.charAt(1)))) {
try {
int typeInt = Integer.parseInt(nodeType);
if (CNodeMap.map.containsKey(typeInt)) {
String type = CNodeMap.map.get(Integer.parseInt(nodeType));
nodeType = type;
}
} catch (NumberFormatException e) {
nodeType = actStrFrag.substring(index);
}
}
}
actStrFrag = actStrFrag.substring(0, index) + nodeType + "@@";
actStr += actStrFrag;
}
actStr += actStrArrays[length - 1];
return actStr;
}
private void addToActionSets(HierarchicalActionSet actionSet, Action parentAct, List<HierarchicalActionSet> actionSets) {
Action act = actionSet.getAction();
for (HierarchicalActionSet actSet : actionSets) {
if (actSet.equals(actionSet)) continue;
Action action = actSet.getAction();
if (!areRelatedActions(action, act)) continue;
if (action.equals(parentAct)) { // actSet is the parent of actionSet.
actionSet.setParent(actSet);
actSet.getSubActions().add(actionSet);
sortSubActions(actSet);
break;
} else {
if (isPossibileSubAction(action, act)) {
// SubAction range startPosition2 <= startPosition && length <= length2
addToActionSets(actionSet, parentAct, actSet.getSubActions());
}
}
}
}
private boolean isPossibileSubAction(Action parent, Action child) {
if ((parent instanceof Update && !(child instanceof Addition))
|| (parent instanceof Delete && child instanceof Delete)
|| (parent instanceof Insert && (child instanceof Insert))) {
int startPosition = child.getPosition();
int length = child.getLength();
int startPosition2 = parent.getPosition();
int length2 = parent.getLength();
if (!(startPosition2 <= startPosition && length <= length2)) {
// when act is not the sub-set of action.
return false;
}
}
return true;
}
private void sortSubActions(HierarchicalActionSet actionSet) {
ListSorter<HierarchicalActionSet> sorter = new ListSorter<HierarchicalActionSet>(actionSet.getSubActions());
List<HierarchicalActionSet> subActions = sorter.sortAscending();
if (subActions != null) {
actionSet.setSubActions(subActions);
}
}
private boolean addToAactionSet(Action act, Action parentAct, List<HierarchicalActionSet> actionSets) {
for(HierarchicalActionSet actionSet : actionSets) {
Action action = actionSet.getAction();
if (!areRelatedActions(action, act)) continue;
if (action.equals(parentAct)) { // actionSet is the parent of actSet.
HierarchicalActionSet actSet = createActionSet(act, actionSet.getAction(), actionSet);
actionSet.getSubActions().add(actSet);
sortSubActions(actionSet);
return true;
} else {
if (isPossibileSubAction(action, act)) {
// SubAction range: startPosition2 <= startPosition && startPosition + length <= startP + length2
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets.size() > 0) {
boolean added = addToAactionSet(act, parentAct, subActionSets);
if (added) {
return true;
} else {
continue;
}
}
}
}
}
return false;
}
List<Action> newParentActions = new ArrayList<>();
private Action findParentAction(Action action, List<Action> actions) {
ITree parent = action.getNode().getParent();
if (parent == null) return null;
if (action instanceof Addition) {
parent = ((Addition) action).getParent(); // parent in the fixed source code tree
}
for (Action act : actions) {
if (act.getNode().equals(parent)) {
if (areRelatedActions(act, action)) {
return act;
}
}
}
for (Action act : newParentActions) {
if (act.getNode().equals(parent)) {
if (areRelatedActions(act, action)) {
return act;
}
}
}
ITree tree = action.getNode();
Action parentAction = null;
if (!isStatement(tree)) {
parentAction = new Update(parent, action.getNode().getParent());
newParentActions.add(parentAction);
Action higherParentAct = findParentAction(parentAction, actions);
HierarchicalActionSet actionSet = null;
if (higherParentAct == null) {
actionSet = createActionSet(parentAction, higherParentAct, null);
actionSets.add(actionSet);
} else {
if (!addToAactionSet(parentAction, higherParentAct, actionSets)) {
// The index of the parent action in the actions' list is larger than the index of this action.
actionSet = createActionSet(parentAction, higherParentAct, null);
actionSets.add(actionSet);
}
}
}
return parentAction;
}
private boolean isStatement(ITree tree) {
int nodeType = tree.getType();
if (nodeType == 280002 || nodeType == 280001 || nodeType == 310200 || nodeType == 280100
|| nodeType == 300100 || nodeType == 280003 || nodeType == 310100 || nodeType == 310300
|| 260300 == nodeType || nodeType == 460000) {// TODO
return true;
}
return false;
}
private boolean areRelatedActions(Action parent, Action child) {
if (parent instanceof Move && !(child instanceof Move)) {// If action is MOV, its children must be MOV.
return false;
}
if (parent instanceof Delete && !(child instanceof Delete)) {// If action is DEL, its children must be DEL.
return false;
}
if (parent instanceof Insert && !(child instanceof Addition)) {// If action is INS, its children must be MOV or INS.
return false;
}
return true;
}
}
@@ -1,625 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
public class HunkActionFilter {
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, without considering the same parent node.
*
* @param hunks
* @param actionSets
* @return
*/
public List<HierarchicalActionSet> filterActionsByDiffEntryHunk(List<DiffEntryHunk> hunks,
List<HierarchicalActionSet> actionSets, File revFile, File prevFile) {
List<HierarchicalActionSet> uselessActions = new ArrayList<>();
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return uselessActions;
}
for (HierarchicalActionSet actionSet : actionSets) {
// position of buggy statements
int startPosition = 0;
int endPosition = 0;
int startLine = 0;
int endLine = 0;
// position of fixed statements
int startPosition2 = 0;
int endPosition2 = 0;
int startLine2 = 0;
int endLine2 = 0;
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
startPosition2 = actionSet.getStartPosition();
endPosition2 = startPosition2 + actionSet.getLength();
List<Move> firstAndLastMov = getFirstAndLastMoveAction(actionSet);
if (firstAndLastMov != null) {
startPosition = firstAndLastMov.get(0).getNode().getPos();
ITree lastTree = firstAndLastMov.get(1).getNode();
endPosition = lastTree.getPos() + lastTree.getLength();
}
} else {
startPosition = actionSet.getStartPosition(); // range of actions
endPosition = startPosition + actionSet.getLength();
if (actionStr.startsWith("UPD")) {
Update update = (Update) actionSet.getAction();
ITree newNode = update.getNewNode();
startPosition2 = newNode.getPos();
endPosition2 = startPosition2 + newNode.getLength();
}
}
startLine = startPosition == 0 ? 0 : prevUnit.getLineNumber(startPosition);
endLine = endPosition == 0 ? 0 : prevUnit.getLineNumber(endPosition);
startLine2 = startPosition2 == 0 ? 0 : revUnit.getLineNumber(startPosition2);
endLine2 = endPosition2 == 0 ? 0 : revUnit.getLineNumber(endPosition2);
for (DiffEntryHunk hunk : hunks) {
int bugStartLine = hunk.getBugLineStartNum();
int bugRange = hunk.getBugRange();
int fixStartLine = hunk.getFixLineStartNum();
int fixRange = hunk.getFixRange();
if (actionStr.startsWith("INS")) {
if (fixStartLine + fixRange < startLine2) {
continue;
}
if (endLine2 < fixStartLine ) {
uselessActions.add(actionSet);
}
} else {
if (bugStartLine + bugRange < startLine) {
continue;
}
if (endLine < bugStartLine ) {
uselessActions.add(actionSet);
}
break;
}
}
actionSet.setBugStartLineNum(startLine);
actionSet.setBugEndLineNum(endLine);
actionSet.setFixStartLineNum(startLine2);
actionSet.setFixEndLineNum(endLine2);
}
actionSets.removeAll(uselessActions);
uselessActions.clear();
return actionSets;
}
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, with considering the same parent node.
*
* @param hunks
* @param actionSets
* @return
*/
public List<HunkFixPattern> filterActionsByDiffEntryHunk2(List<DiffEntryHunk> hunks,
List<HierarchicalActionSet> actionSets, File revFile, File prevFile) {
List<HunkFixPattern> allHunkFixPatterns = new ArrayList<>();
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return allHunkFixPatterns;
}
int i = 0;
int size = actionSets.size();
for (DiffEntryHunk hunk : hunks) {
int hunkBugStartLine = hunk.getBugLineStartNum();
int hunkBugRange = hunk.getBugRange();
int hunkFixStartLine = hunk.getFixLineStartNum();
int hunkFixRange = hunk.getFixRange();
for (; i < size; i ++) {
HierarchicalActionSet actionSet = actionSets.get(i);
int actionBugStartLine = actionSet.getBugStartLineNum();
if (actionBugStartLine == 0) {
actionBugStartLine = setLineNumbers(actionSet, prevUnit, revUnit);
}
int actionBugEndLine = actionSet.getBugEndLineNum();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
String actionStr = actionSet.getActionString();
ITree previousParent = null;
List<HierarchicalActionSet> hunkActionSets = new ArrayList<>();
if (actionStr.startsWith("INS")) {
if (hunkFixStartLine + hunkFixRange < actionFixStartLine) {
addHunkActionSets(hunkActionSets, allHunkFixPatterns, hunk);// save the previous non-null hunkFixPattern.
break;
}
if (actionFixEndLine >= hunkFixStartLine ) {
ITree parent = addToHunkActionSets(actionSet, hunkActionSets, allHunkFixPatterns, previousParent, hunk);
if (parent != null) {
if (parent != previousParent) {
hunkActionSets = new ArrayList<>();
}
hunkActionSets.add(actionSet);
} else if (hunkActionSets.size() > 0) {
hunkActionSets = new ArrayList<>();
}
previousParent = parent;
}
} else { // UPD, DEL, MOV
if (hunkBugStartLine + hunkBugRange < actionBugStartLine) {
addHunkActionSets(hunkActionSets, allHunkFixPatterns, hunk);// save the previous non-null hunkFixPattern.
break;
}
if (actionBugEndLine >= hunkBugStartLine ) {
ITree parent = addToHunkActionSets(actionSet, hunkActionSets, allHunkFixPatterns, previousParent, hunk);
if (parent != null) { // same parent
if (parent != previousParent) {
hunkActionSets = new ArrayList<>();
}
hunkActionSets.add(actionSet);
} else if (hunkActionSets.size() > 0) {
hunkActionSets = new ArrayList<>();
}
previousParent = parent;
}
}
addHunkActionSets(hunkActionSets, allHunkFixPatterns, hunk);
}
}
return allHunkFixPatterns;
}
private int getEndPosition(List<ITree> children) {
int endPosition = 0;
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
if (Checker.isStatement2(type)) {
if ( i > 0) {
child = children.get(i - 1);
endPosition = child.getPos() + child.getLength();
} else {
endPosition = child.getPos() - 1;
}
break;
}
}
return endPosition;
}
private void addHunkActionSets(List<HierarchicalActionSet> hunkActionSets, List<HunkFixPattern> allHunkFixPatterns, DiffEntryHunk hunk) {
if (hunkActionSets.size() > 0) {
HunkFixPattern hunkFixPattern = new HunkFixPattern(hunk, hunkActionSets);
allHunkFixPatterns.add(hunkFixPattern);
}
}
private ITree addToHunkActionSets(HierarchicalActionSet actionSet, List<HierarchicalActionSet> hunkActionSets,
List<HunkFixPattern> allHunkFixPatterns, ITree previousParent, DiffEntryHunk hunk) {
String astNodeType = actionSet.getAstNodeType();
if ("FieldDeclaration".equals(astNodeType)) {
addHunkActionSets(hunkActionSets, allHunkFixPatterns, hunk);
hunkActionSets = new ArrayList<>();
hunkActionSets.add(actionSet);
HunkFixPattern hunkFixPattern = new HunkFixPattern(hunk, hunkActionSets);
allHunkFixPatterns.add(hunkFixPattern);
return null;
} else {
ITree currentParent = actionSet.getNode().getParent();
if (previousParent == null) {
previousParent = currentParent;
} else {
if (!previousParent.equals(currentParent)) {
HunkFixPattern hunkFixPattern = new HunkFixPattern(hunk, hunkActionSets);
allHunkFixPatterns.add(hunkFixPattern);
previousParent = currentParent;
}
}
return previousParent;
}
}
private List<Move> getFirstAndLastMoveAction(HierarchicalActionSet gumTreeResult) {
List<Move> firstAndLastMoveActions = new ArrayList<>();
List<HierarchicalActionSet> actions = new ArrayList<>();
actions.addAll(gumTreeResult.getSubActions());
if (actions.size() == 0) {
return null;
}
Move firstMoveAction = null;
Move lastMoveAction = null;
while (actions.size() > 0) {
List<HierarchicalActionSet> subActions = new ArrayList<>();
for (HierarchicalActionSet action : actions) {
subActions.addAll(action.getSubActions());
if (action.toString().startsWith("MOV")) {
if (firstMoveAction == null) {
firstMoveAction = (Move) action.getAction();
lastMoveAction = (Move) action.getAction();
} else {
int startPosition = action.getStartPosition();
int length = action.getLength();
int startPositionFirst = firstMoveAction.getPosition();
int startPositionLast = lastMoveAction.getPosition();
int lengthLast = lastMoveAction.getNode().getLength();
if (startPosition < startPositionFirst || (startPosition == startPositionFirst && length > firstMoveAction.getLength())) {
firstMoveAction = (Move) action.getAction();
}
if ((startPosition + length) > (startPositionLast + lengthLast)) {
lastMoveAction = (Move) action.getAction();
}
}
}
}
actions.clear();
actions.addAll(subActions);
}
if (firstMoveAction == null) {
return null;
}
firstAndLastMoveActions.add(firstMoveAction);
firstAndLastMoveActions.add(lastMoveAction);
return firstAndLastMoveActions;
}
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, without considering the same parent node.
*
* @param violations
* @param actionSets
* @param revFile
* @param prevFile
* @return
*/
public List<Violation> filterActionsByModifiedRange(List<Violation> violations,
List<HierarchicalActionSet> actionSets, File revFile, File prevFile) {
List<Violation> selectedViolations = new ArrayList<>();
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return selectedViolations;
}
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
int bugStartLine = violation.getBugStartLineNum();
int bugEndLine = violation.getBugEndLineNum();
int fixStartLine = violation.getFixStartLineNum();
int fixEndLine = violation.getFixEndLineNum();
for (HierarchicalActionSet actionSet : actionSets) {
int actionBugStartLine = actionSet.getBugStartLineNum();
if (actionBugStartLine == 0) {
actionBugStartLine = setLineNumbers(actionSet, prevUnit, revUnit);
}
int actionBugEndLine = actionSet.getBugEndLineNum();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
if (fixStartLine <= actionFixStartLine && actionFixEndLine <= fixEndLine) {
if (actionBugStartLine != 0) {
if (startLine <= actionBugEndLine && endLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
} else {
violation.getActionSets().add(actionSet);
}
}
} else {
// if (bugEndLine < actionBugStartLine) {
// break;
// }
if (bugStartLine <= actionBugStartLine && actionBugEndLine <= bugEndLine) {
if (startLine <= actionBugEndLine && endLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
}
}
}
if (violation.getActionSets().size() > 0) {
selectedViolations.add(violation);
}
}
return selectedViolations;
}
/**
* Match hierarchical actions with code change diffs for C code.
*
* @param diffentryHunks
* @param actionSets
* @param revFile
* @param prevFile
* @return
*/
public List<DiffEntryHunk> matchActionsByDiffEntryForC(List<DiffEntryHunk> diffentryHunks, List<HierarchicalActionSet> actionSets) {
List<DiffEntryHunk> selectedViolations = new ArrayList<>();
for (DiffEntryHunk diffentryHunk : diffentryHunks) {
int bugHunkStartLine = diffentryHunk.getBugLineStartNum();
int bugHunkEndLine = bugHunkStartLine + diffentryHunk.getBugRange() - 1;
int fixHunkStartLine = diffentryHunk.getFixLineStartNum();
int fixHunkEndLine = fixHunkStartLine + diffentryHunk.getFixedHunkSize() - 1;
for (HierarchicalActionSet actionSet : actionSets) {
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
if (fixHunkStartLine <= actionFixEndLine && actionFixStartLine <= fixHunkEndLine ) {
diffentryHunk.getActionSets().add(actionSet);
}
} else {
int actionBugStartLine = actionSet.getBugStartLineNum();
int actionBugEndLine = actionSet.getBugEndLineNum();
if (bugHunkStartLine <= actionBugEndLine && actionBugStartLine <= bugHunkEndLine) {
diffentryHunk.getActionSets().add(actionSet);
}
}
}
if (diffentryHunk.getActionSets().size() > 0) {
selectedViolations.add(diffentryHunk);
}
}
return selectedViolations;
}
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, with considering the same parent node.
*
* @param violations
* @param actionSets
* @param revFile
* @param prevFile
* @return
*/
public List<DiffEntryHunk> filterActionsByModifiedRange2(List<DiffEntryHunk> diffentryHunks,
List<HierarchicalActionSet> actionSets, File revFile, File prevFile) {
List<DiffEntryHunk> selectedViolations = new ArrayList<>();
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return selectedViolations;
}
for (DiffEntryHunk diffentryHunk : diffentryHunks) {
// int violationEndLine = violationStartLine + diffentryHunk.getBugRange();
int bugHunkStartLine = diffentryHunk.getBugLineStartNum();
int bugHunkEndLine = bugHunkStartLine + diffentryHunk.getBugRange() - 1;
int fixHunkStartLine = diffentryHunk.getFixLineStartNum();
int fixHunkEndLine = fixHunkStartLine + diffentryHunk.getFixedHunkSize() - 1;
for (HierarchicalActionSet actionSet : actionSets) {
int actionBugStartLine = actionSet.getBugStartLineNum();
if (actionBugStartLine == 0) {
actionBugStartLine = setLineNumbers(actionSet, prevUnit, revUnit);
}
int actionBugEndLine = actionSet.getBugEndLineNum();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
if (fixHunkStartLine <= actionFixEndLine && fixHunkEndLine >= actionFixStartLine ) {
if (actionBugStartLine != 0) {
diffentryHunk.getActionSets().add(actionSet);
}
}
} else {
if (bugHunkStartLine <= actionBugEndLine && bugHunkEndLine >= actionBugStartLine) {
diffentryHunk.getActionSets().add(actionSet);
}
}
}
if (diffentryHunk.getActionSets().size() > 0) {
selectedViolations.add(diffentryHunk);
}
}
return selectedViolations;
}
/**
* Filter out the modify actions, which are not in the DiffEntry hunks, without considering DiffEntry hunks.
*
* @param violations
* @param actionSets
* @param revFile
* @param prevFile
* @return
*/
public List<Violation> filterActionsByModifiedRange3(List<Violation> violations,
List<HierarchicalActionSet> actionSets, File revFile, File prevFile) {
List<Violation> selectedViolations = new ArrayList<>();
CUCreator cuCreator = new CUCreator();
CompilationUnit prevUnit = cuCreator.createCompilationUnit(prevFile);
CompilationUnit revUnit = cuCreator.createCompilationUnit(revFile);
if (prevUnit == null || revUnit == null) {
return selectedViolations;
}
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
// ITree parent = null;
// List<HierarchicalActionSet> actionSetsWithSameParent = new ArrayList<>(); //TODO
for (HierarchicalActionSet actionSet : actionSets) {
int actionBugStartLine = actionSet.getStartPosition();
int actionBugEndLine = actionSet.getLength();
int actionFixStartLine = actionSet.getFixStartLineNum();
int actionFixEndLine = actionSet.getFixEndLineNum();
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) { // FIXME It is impossible to locate the INS action by the buggy line range.
if (startLine <= actionFixStartLine && actionFixEndLine <= endLine) {
if (actionBugStartLine != 0) {
if (startLine <= actionBugEndLine && endLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
} else {
violation.getActionSets().add(actionSet);
}
}
} else {
// if (endLine < actionBugStartLine) {
// break;
// }
if (startLine <= actionBugStartLine && actionBugEndLine <= endLine) {
if (startLine <= actionBugEndLine && endLine >= actionBugStartLine) {
violation.getActionSets().add(actionSet);
}
}
}
}
if (violation.getActionSets().size() > 0) {
selectedViolations.add(violation);
}
}
return selectedViolations;
}
private int setLineNumbers(HierarchicalActionSet actionSet, CompilationUnit prevUnit, CompilationUnit revUnit) {
int actionBugStartLine;
int actionBugEndLine;
int actionFixStartLine;
int actionFixEndLine;
// position of buggy statements
int bugStartPosition = 0;
int bugEndPosition = 0;
// position of fixed statements
int fixStartPosition = 0;
int fixEndPosition = 0;
String actionStr = actionSet.getActionString();
if (actionStr.startsWith("INS")) {
fixStartPosition = actionSet.getStartPosition();
fixEndPosition = fixStartPosition + actionSet.getLength();
List<Move> firstAndLastMov = getFirstAndLastMoveAction(actionSet);
if (firstAndLastMov != null) {
bugStartPosition = firstAndLastMov.get(0).getNode().getPos();
ITree lastTree = firstAndLastMov.get(1).getNode();
bugEndPosition = lastTree.getPos() + lastTree.getLength();
}
} else {
bugStartPosition = actionSet.getStartPosition(); // range of actions
bugEndPosition = bugStartPosition + actionSet.getLength();
if (actionStr.startsWith("UPD")) {
Update update = (Update) actionSet.getAction();
ITree newNode = update.getNewNode();
fixStartPosition = newNode.getPos();
fixEndPosition = fixStartPosition + newNode.getLength();
String astNodeType = actionSet.getAstNodeType();
// if (Checker.withBlockStatement(newNode.getType())) {
//// List<ITree> children = update.getNode().getChildren();
//// bugEndPosition = getEndPosition(children);
//// List<ITree> newChildren = newNode.getChildren();
//// fixEndPosition = getEndPosition(newChildren);
// } else
if ("TypeDeclaration".equals(astNodeType)) {
bugEndPosition = getClassBodyStartPosition(update.getNode());
fixEndPosition = getClassBodyStartPosition(newNode);
} else if ("MethodDeclaration".equals(astNodeType)) {
List<ITree> children = update.getNode().getChildren();
bugEndPosition = getEndPosition(children);
List<ITree> newChildren = newNode.getChildren();
fixEndPosition = getEndPosition(newChildren);
}
if (fixEndPosition == 0) {
fixEndPosition = fixStartPosition + newNode.getLength();
}
} else if (actionStr.startsWith("DEL")) {
ITree buggyTree = actionSet.getNode();
int type = buggyTree.getType();
if (type == 55) { // TypeDeclaration
bugEndPosition = getClassBodyStartPosition(buggyTree);
} else if (type == 31 || Checker.withBlockStatement(type)) {//MethodDeclaration && Block-Statements
List<ITree> children = buggyTree.getChildren();
bugEndPosition = getEndPosition(children);
}
}
if (bugEndPosition == 0) {
bugEndPosition = bugStartPosition + actionSet.getLength();
}
}
actionBugStartLine = bugStartPosition == 0 ? 0 : prevUnit.getLineNumber(bugStartPosition);
actionBugEndLine = bugEndPosition == 0 ? 0 : prevUnit.getLineNumber(bugEndPosition);
actionFixStartLine = fixStartPosition == 0 ? 0 : revUnit.getLineNumber(fixStartPosition);
actionFixEndLine = fixEndPosition == 0 ? 0 : revUnit.getLineNumber(fixEndPosition);
actionSet.setBugStartLineNum(actionBugStartLine);
actionSet.setBugEndLineNum(actionBugEndLine);
actionSet.setFixStartLineNum(actionFixStartLine);
actionSet.setFixEndLineNum(actionFixEndLine);
actionSet.setBugEndPosition(bugEndPosition);
actionSet.setFixEndPosition(fixEndPosition);
return actionBugStartLine;
}
private int getClassBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88 && type != 42) {
// ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType, SimpleName
if (i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength() + 1;
} else {
return child.getPos() - 1;
}
}
}
return 0;
}
}
@@ -1,27 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.List;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
public class HunkFixPattern {
private DiffEntryHunk hunk;
private List<HierarchicalActionSet> hunkActionSets;
public HunkFixPattern(DiffEntryHunk hunk, List<HierarchicalActionSet> hunkActionSets) {
super();
this.hunk = hunk;
this.hunkActionSets = hunkActionSets;
}
public DiffEntryHunk getHunk() {
return hunk;
}
public List<HierarchicalActionSet> getHunkActionSets() {
return hunkActionSets;
}
}
@@ -1,68 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.ArrayList;
import java.util.List;
public class SimpleTree {
private String nodeType;
private String label;
private SimpleTree parent;
private List<SimpleTree> children = new ArrayList<>();
public String getNodeType() {
return nodeType;
}
public void setNodeType(String nodeType) {
this.nodeType = nodeType;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public SimpleTree getParent() {
return parent;
}
public void setParent(SimpleTree parent) {
this.parent = parent;
}
public List<SimpleTree> getChildren() {
return children;
}
public void setChildren(List<SimpleTree> children) {
this.children = children;
}
private List<String> strList = new ArrayList<>();
@Override
public String toString() {
String str = this.nodeType + "@@" + this.label;
if (strList.size() == 0) {
strList.add(str);
for (SimpleTree child : children) {
child.toString();
List<String> strList1 = child.strList;
for (String str1 : strList1) {
strList.add("------" + str1);
}
}
}
str = "";
for (String str1 : strList) {
str += str1 + "\n";
}
return str;
}
}
@@ -1,902 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.utils.ListSorter;
/**
* Simplify the ITree of source code into a simple tree.
*
* @author kui.liu
*
*/
public class SimplifyTree {
private static final String ABSTRACT_TYPE = "T";
private static final String ABSTRACT_NAME = "N";
private static final String ABSTRACT_METHOD = "m";
private static final String ABSTRACT_VARIABLE = "v";
private Map<String, String> abstractTypeIdentifiers = new HashMap<>();
private Map<String, String> abstractMethodIdentifiers = new HashMap<>();
private Map<String, String> abstractNameIdentifiers = new HashMap<>();
private Map<String, String> abstractVariableIdentifiers = new HashMap<>();
/**
* Convert ITree to a source code simple tree, an abstract identifier simple tree, and a semi-source code simple tree.
*
* @param actionSet
*/
public void abstractTree(HierarchicalActionSet actionSet) {
SimpleTree sourceCodeSimpleTree = null; // source code tree and AST node type tree
SimpleTree abstractIdentifierTree = null; // abstract identifier tree
SimpleTree abstractSimpleTree = null; // semi-source code tree. and AST node type tree
SimpleTree simpleTree = null; // source code tree with canonical variable names.
if (actionSet.getActionString().startsWith("INS")) {
List<Action> allMoveActions = getAllMoveActions(actionSet);
if (allMoveActions != null) {
List<Action> actions = new ArrayList<>();
for (Action action : allMoveActions) {
boolean hasParent = false;
ITree parent = action.getNode().getParent();
for (Action act : allMoveActions) {
if (act == action) continue;
ITree actNode = act.getNode();
if (actNode.equals(parent)) {
hasParent = true;
break;
}
}
if (!hasParent) {
actions.add(action);
}
}
// sourceCodeSimpleTree = sourceCodeTree(actions);
simpleTree = canonicalizeSourceCodeTree(actions, null);
}
} else {
ITree tree = actionSet.getNode();
String astNodeType = actionSet.getAstNodeType();
if (Checker.containsBodyBlock(astNodeType)) {
// delete the body block.
List<ITree> children = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (ITree child : children) {
if (!child.getLabel().endsWith("Body")) {
newChildren.add(child);
}
}
tree.setChildren(newChildren);
}
// sourceCodeSimpleTree = originalSourceCodeTree(tree, null);
// abstractIdentifierTree = abstractIdentifierTree(actionSet, tree, null);
// abstractSimpleTree = semiSourceCodeTree(actionSet, tree, null);
simpleTree = canonicalizeSourceCodeTree(tree, null);
}
actionSet.setAbstractSimpleTree(abstractSimpleTree);
actionSet.setAbstractIdentifierTree(abstractIdentifierTree);
actionSet.setSimpleTree(simpleTree);
actionSet.setOriginalTree(sourceCodeSimpleTree);
}
/**
* Convert ITree to a source code simple tree, an abstract identifier simple tree, and a semi-source code simple tree.
*
* @param actionSet
*/
public void abstractTree(HierarchicalActionSet actionSet, int bugEndPosition) {
SimpleTree sourceCodeSimpleTree = null; // source code tree and AST node type tree
SimpleTree abstractIdentifierTree = null; // abstract identifier tree
SimpleTree abstractSimpleTree = null; // semi-source code tree. and AST node type tree
SimpleTree simpleTree = null; // source code tree with canonical variable names.
if (actionSet.getActionString().startsWith("INS")) {
List<Action> allMoveActions = getAllMoveActions(actionSet);
if (allMoveActions != null) {
List<Action> actions = new ArrayList<>();
for (Action action : allMoveActions) {
boolean hasParent = false;
ITree parent = action.getNode().getParent();
for (Action act : allMoveActions) {
if (act == action) continue;
ITree actNode = act.getNode();
if (actNode.equals(parent)) {
hasParent = true;
break;
}
}
if (!hasParent) {
actions.add(action);
}
}
// sourceCodeSimpleTree = sourceCodeTree(actions);
simpleTree = canonicalizeSourceCodeTree(actions, null);
}
} else {
ITree tree = actionSet.getNode();
// String astNodeType = actionSet.getAstNodeType();
// if (Checker.containsBodyBlock(astNodeType)) {
// // delete the body block.
// List<ITree> children = tree.getChildren();
// List<ITree> newChildren = new ArrayList<>();
// for (ITree child : children) {
// if (!child.getLabel().endsWith("Body")) {
// newChildren.add(child);
// }
// }
// tree.setChildren(newChildren);
// }
// sourceCodeSimpleTree = originalSourceCodeTree(tree, null);
// abstractIdentifierTree = abstractIdentifierTree(actionSet, tree, null);
// abstractSimpleTree = semiSourceCodeTree(actionSet, tree, null);
// if (actionSet.getActionString().startsWith("UPD")) {
// simpleTree = canonicalizeSourceCodeTree(tree, null, bugEndPosition);
// } else {
// simpleTree = canonicalizeSourceCodeTree(tree, null);
// }
simpleTree = canonicalizeSourceCodeTree(tree, null, bugEndPosition);
}
actionSet.setAbstractSimpleTree(abstractSimpleTree);
actionSet.setAbstractIdentifierTree(abstractIdentifierTree);
actionSet.setSimpleTree(simpleTree);
actionSet.setOriginalTree(sourceCodeSimpleTree);
}
private SimpleTree canonicalizeSourceCodeTree(List<Action> actions, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
simpleTree.setParent(parent);
List<SimpleTree> children = new ArrayList<>();
for (Action action : actions) {
ITree node = action.getNode();
children.add(canonicalizeSourceCodeTree(node, simpleTree));
}
simpleTree.setChildren(children);
return simpleTree;
}
public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else {
if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation"))) {
if (label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
} else if (label.startsWith("ClassName:")) {
simpleTree.setNodeType("ClassName");
label = label.substring(10);
}
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(astNode);
}
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
subTrees.add(canonicalizeSourceCodeTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
}
} else {
if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <MethodName, name>
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
simpleTree.setLabel(label);
} else if (label.startsWith("ClassName:")) {
simpleTree.setNodeType("ClassName");
label = label.substring(10);
simpleTree.setLabel(label);
} else if (label.startsWith("Name:")) {
label = label.substring(5);
char firstChar = label.charAt(0);
if (Character.isUpperCase(firstChar)) {
simpleTree.setNodeType("Name");
simpleTree.setLabel(label);
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if (astNode.startsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation")) && label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(label.replaceAll(" ", ""));
}
}
}
simpleTree.setParent(parent);
return simpleTree;
}
public void canonicalizeSourceCodeTree(ITree tree) {
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
if (astNode.endsWith("Type")) {
} else {
if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation"))) {
if (label.startsWith("MethodName:")) {
} else if (label.startsWith("ClassName:")) {
}
} else {
}
for (ITree child : children) {
canonicalizeSourceCodeTree(child);
}
}
} else {
if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <MethodName, name>
} else if (label.startsWith("ClassName:")) {
} else if (label.startsWith("Name:")) {
label = label.substring(5);
char firstChar = label.charAt(0);
if (Character.isUpperCase(firstChar)) {
// simpleTree.setNodeType("Name");
} else {// variableName: <VariableName, canonicalName>
canonicalVariableName(label, tree);
}
} else {// variableName: <VariableName, canonicalName>
canonicalVariableName(label, tree);
}
}
}
}
public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent, int bugEndPosition) {
SimpleTree simpleTree = new SimpleTree();
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else {
if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation"))) {
if (label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
} else if (label.startsWith("ClassName:")) {
simpleTree.setNodeType("ClassName");
label = label.substring(10);
}
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(astNode);
}
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
if (child.getPos() > bugEndPosition) continue;
subTrees.add(canonicalizeSourceCodeTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
}
} else {
if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <MethodName, name>
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
simpleTree.setLabel(label);
} else if (label.startsWith("ClassName:")) {
simpleTree.setNodeType("ClassName");
label = label.substring(10);
simpleTree.setLabel(label);
} else if (label.startsWith("Name:")) {
label = label.substring(5);
char firstChar = label.charAt(0);
if (Character.isUpperCase(firstChar)) {
simpleTree.setNodeType("Name");
simpleTree.setLabel(label);
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {// variableName: <VariableName, canonicalName>
simpleTree.setNodeType("VariableName");
simpleTree.setLabel(canonicalVariableName(label, tree));
}
} else {
simpleTree.setNodeType(astNode);
if (astNode.endsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if (astNode.startsWith("Type")) {
simpleTree.setLabel(canonicalizeTypeStr(label).replaceAll(" ", ""));
} else if ((astNode.equals("SimpleName") || astNode.equals("MethodInvocation")) && label.startsWith("MethodName:")) {
simpleTree.setNodeType("MethodName");
label = label.substring(11);
int argusIndex = label.indexOf(":[");
if (argusIndex > 0) {
label = label.substring(0, argusIndex);
}
simpleTree.setLabel(label);
} else {
simpleTree.setLabel(label.replaceAll(" ", ""));
}
}
}
simpleTree.setParent(parent);
return simpleTree;
}
public String canonicalVariableName(String label, ITree tree) {
ITree parent = tree.getParent();
if (parent == null) {
return label;
} else {
String matchStr = null;
int parentType = parent.getType();
if (parentType == 44) { // SingleVariableDeclaration
matchStr = matchSingleVariableDeclaration(parent, label);
} else if (parentType == 23 || parentType == 58 || parentType == 60) {
//FieldDeclaration, VariableDeclarationExpression, VariableDeclarationStatement
matchStr = matchVariableDeclarationExpression(parent, label);
} else if (parentType == 31) { // MethodDeclaration
List<ITree> children = parent.getChildren();
int index = children.indexOf(tree);
for (int i = index - 1; i >=0; i --) {
ITree child = children.get(i);
int childType = child.getType();
if (childType == 60) { // VariableDeclarationStatement
matchStr = matchVariableDeclarationExpression(child, label);
} else if (childType == 44) { // SingleVariableDeclaration
matchStr = matchSingleVariableDeclaration(child, label);
}
if (matchStr != null) break;
}
} else if (parentType ==70 || parentType == 24 ||parentType == 12 || parentType == 54) {
// EnhancedForStatement, ForStatement, CatchClause, TryStatement
matchStr = matchStatements(parentType, parent, label);
} else if (parentType == 55) { // TypeDeclaration: Class Declaration
List<ITree> children = parent.getChildren();
int index = children.indexOf(tree);
for (int i = index - 1; i >=0; i --) {
ITree child = children.get(i);
if (child.getType() == 23) { // FieldDeclaration
matchStr = matchVariableDeclarationExpression(child, label);
if (matchStr != null) break;
}
}
} else if (parentType == 8) { // Block body
List<ITree> children = parent.getChildren();
int index = children.indexOf(tree);
for (int i = index - 1; i >=0; i --) {
ITree child = children.get(i);
if (child.getType() == 60) { // VariableDeclarationStatement
matchStr = matchVariableDeclarationExpression(child, label);
if (matchStr != null) break;
}
}
}
if (matchStr != null) {
return matchStr;
} else {
return canonicalVariableName(label, parent);
}
}
}
private String matchStatements(int typeInt, ITree tree, String label) {
String matchStr = null;
if (typeInt == 70) { // EnhancedForStatement
matchStr = matchSingleVariableDeclaration(tree.getChild(0), label);
} else if (typeInt == 24) { // ForStatement
List<ITree> children = tree.getChildren();
for (ITree child : children) {
if (child.getType() == 58) {
matchStr = matchVariableDeclarationExpression(child, label);
if (matchStr != null) break;
} else {
break;
}
}
} else if (typeInt == 12) { // CatchClause
matchStr = matchSingleVariableDeclaration(tree.getChild(0), label);
} else if (typeInt == 54) { // TryStatement
List<ITree> children = tree.getChildren();
for (ITree child : children) {
if (child.getType() == 58) { //VariableDeclarationExpression
matchStr = matchVariableDeclarationExpression(tree, label);
if (matchStr != null) break;
} else {
break;
}
}
}
return null;
}
public Map<String, String> canonicalVariableMap = new HashMap<>();
public Map<String, Integer> canonicalVariables = new HashMap<>();
private String matchVariableDeclarationExpression(ITree variable, String label) {
List<ITree> children = variable.getChildren();
ITree type = null;
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
if (child.getType() == 59) {// VariableDeclarationFragment
if (type == null) {
type = children.get(i - 1);
}
ITree simpleName = child.getChild(0);
if (simpleName.getLabel().equals(label)) {
String typeStr = canonicalizeTypeStr(type.getLabel());
return canonicalizeVariable(label, typeStr);
}
}
}
return null;
}
private String matchSingleVariableDeclaration(ITree singleVariable, String label) {
List<ITree> children = singleVariable.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
if (child.getType() == 42) { // SimpleName
if (child.getLabel().equals(label)) {
ITree type = children.get(i - 1);
String typeStr = canonicalizeTypeStr(type.getLabel());
return canonicalizeVariable(label, typeStr);
}
break;
}
}
return null;
}
private String canonicalizeVariable(String label, String typeStr) {
String key = label + ":" + typeStr;
if (canonicalVariableMap.containsKey(key)) {
label = canonicalVariableMap.get(key);
} else {
label = typeStr.toLowerCase() + "Var";
Integer num = canonicalVariables.get(label);
if (num == null) {
num = 0;
}
num ++;
canonicalVariables.put(label, num);
label += "" + num;
canonicalVariableMap.put(key, label);
}
return label;
}
private String canonicalizeTypeStr(String label) {
String typeStr = label;
int index1 = typeStr.indexOf("<");
if (index1 != -1) {
typeStr = typeStr.substring(0, index1);
}
index1 = typeStr.lastIndexOf(".");
if (index1 != -1) {
typeStr = typeStr.substring(index1 + 1);
}
return typeStr;
}
// public static String addPrefixByType(Type type) {
// String newName = "";
// if (type instanceof PrimitiveType) {
// // byte,short,char,int,long,float,double,boolean,void
// newName = type.toString().toLowerCase();
// } else if (type instanceof ArrayType) {
// // Type [ ]
// ArrayType at = (ArrayType) type;
// type = at.getElementType();
// if (type instanceof SimpleType || type instanceof PrimitiveType) {
// newName = getNewName(type);
// } else {
// newName = addPrefixByType(type);
// }
// } else if (type instanceof SimpleType) {
// // TypeName
// if (type.toString().equals("Integer")) {
// newName = "int";
// } else {
// newName = getNewName(type);
// }
// } else if (type instanceof QualifiedType) {
// // Type.SimpleName
// newName = ((QualifiedType) type).getName().toString().toLowerCase();
// } else if (type instanceof ParameterizedType) {
// // Type < Type { , Type } > 泛型
// ParameterizedType t = (ParameterizedType) type;
// newName = getNewName(t.getType());
// } else if (type instanceof WildcardType) {
// newName = "object";
// }
// return newName;
// }
//
// private static String getNewName(Type type) {
// String newName = "";
// String typeName = type.toString();
// int dot = typeName.lastIndexOf(".");
// if (dot > 0) {
// newName = typeName.substring(dot + 1).toString().toLowerCase();
// } else {
// newName = typeName.toString().toLowerCase();
// }
// return newName;
// }
/**
* Convert the Move actions of an INS action into a simple tree with AST nodes and leaf labels.
*
* @param actions
* @return
*/
private SimpleTree sourceCodeTree(List<Action> actions) {
if (actions.size() > 0) {
SimpleTree simpleTree = new SimpleTree();
simpleTree.setNodeType("Block");
simpleTree.setLabel("Block");
simpleTree.setParent(null);
List<SimpleTree> subTrees = new ArrayList<>();
for (Action action : actions) {
ITree node = action.getNode();
subTrees.add(sourceCodeTree(node, simpleTree));
}
simpleTree.setChildren(subTrees);
return simpleTree;
}
return null;
}
/**
* Convert a Move action into a simple tree with AST nodes and leaf labels.
*
* @param tree
* @param parent
* @return
*/
private SimpleTree sourceCodeTree(ITree tree, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
String astNode = ASTNodeMap.map.get(tree.getType());
do {
if (astNode.endsWith("Statement") || astNode.equals("FieldDeclaration")) break;
tree = tree.getParent();
astNode = ASTNodeMap.map.get(tree.getType());// FIXME if the ASTNode is a method declaration or class declaration?
} while (!astNode.endsWith("Statement") && !astNode.equals("FieldDeclaration"));
String label = tree.getLabel();
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
subTrees.add(sourceCodeTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
simpleTree.setLabel(astNode);
} else {
simpleTree.setLabel(label);
}
simpleTree.setNodeType(astNode);
simpleTree.setParent(parent);
return simpleTree;
}
/**
* Convert an UPD/DEL/MOV action into a simple tree with AST nodes and leaf labels.
*
* @param tree
* @param parent
* @return
*/
private SimpleTree originalSourceCodeTree(ITree tree, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
simpleTree.setNodeType(astNode);
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
subTrees.add(originalSourceCodeTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
simpleTree.setLabel(astNode);
} else {
simpleTree.setLabel(label);
}
simpleTree.setParent(parent);
return simpleTree;
}
/**
* Convert an UPD/DEL/MOV action into a simple tree with abstract identifiers of AST nodes and abstract identifiers of leaf labels.
*
* @param actionSet
* @param tree
* @param parent
* @return
*/
private SimpleTree abstractIdentifierTree(ITree tree, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
simpleTree.setNodeType(astNode);
List<ITree> children = tree.getChildren();
if (children.size() > 0) {
if (astNode.endsWith("Type")) {
simpleTree.setNodeType("Type");
simpleTree.setLabel(getAbstractLabel(abstractTypeIdentifiers, label, ABSTRACT_TYPE)); // abstract Type identifier
} else {
List<SimpleTree> subTrees = new ArrayList<>();
for (ITree child : children) {
subTrees.add(abstractIdentifierTree(child, simpleTree));
}
simpleTree.setChildren(subTrees);
simpleTree.setLabel(astNode);
}
} else {
if (astNode.endsWith("Type")) {
simpleTree.setNodeType("Type");
if (astNode.equals("WildcardType")) {
simpleTree.setLabel("?");
} else {
simpleTree.setLabel(getAbstractLabel(abstractTypeIdentifiers, label, ABSTRACT_TYPE)); // abstract Type identifier
}
} else if (astNode.endsWith("Name")) {
// variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <Method, name>
label = label.substring(11);
simpleTree.setNodeType("Method");
simpleTree.setLabel(getAbstractLabel(abstractMethodIdentifiers, label, ABSTRACT_METHOD)); // abstract method identifier
} else if (label.startsWith("Name:")) {
label = label.substring(5);
String firstChar = label.substring(0, 1);
if (firstChar.equals(firstChar.toUpperCase())) {
simpleTree.setNodeType("Name");
simpleTree.setLabel(getAbstractLabel(abstractNameIdentifiers, label, ABSTRACT_NAME)); // abstract Name identifier
} else {// variableName: <Variable, var>
simpleTree.setNodeType("Variable");
simpleTree.setLabel(getAbstractLabel(abstractVariableIdentifiers, label, ABSTRACT_VARIABLE));// abstract Variable identifier
}
} else {// variableName: <Variable, var>
simpleTree.setNodeType("Variable");
simpleTree.setLabel(getAbstractLabel(abstractVariableIdentifiers, label, ABSTRACT_VARIABLE));// abstract Variable identifier
}
} else if (astNode.equals("BooleanLiteral") || astNode.equals("CharacterLiteral") || astNode.equals("NullLiteral")
|| astNode.equals("NumberLiteral") || astNode.equals("StringLiteral") || astNode.equals("ThisExpression")
|| astNode.equals("Modifier") || astNode.equals("Operator")) {
simpleTree.setNodeType(astNode);
simpleTree.setLabel(label);
}
}
simpleTree.setParent(parent);
return simpleTree;
}
/**
* Convert an UPD/DEL/MOV action into a semi-source code simple tree by abstracting the non-buggy code.
*
* @param actionSet
* @param tree
* @param parent
* @return
*/
private SimpleTree semiSourceCodeTree(HierarchicalActionSet actionSet, ITree tree, SimpleTree parent) {
SimpleTree simpleTree = new SimpleTree();
simpleTree.setParent(parent);
// deep first
abstractBuggyTreeDeepFirst(actionSet, tree, simpleTree);
return simpleTree;
}
private void abstractBuggyTreeDeepFirst(HierarchicalActionSet actionSet, ITree tree, SimpleTree simpleTree) {
List<ITree> children = tree.getChildren();
HierarchicalActionSet modifyAction = findHierarchicalActionSet(tree.getPos(), tree.getLength(), actionSet);
String label = tree.getLabel();
String astNode = ASTNodeMap.map.get(tree.getType());
if (Checker.isExpressionType(astNode)) {
if (modifyAction == null || !modifyAction.getActionString().contains("@@" + label)) {
simpleTree.setNodeType("Expression");
simpleTree.setLabel("EXP"); // astNode
}
} else {
if (astNode.endsWith("Type")) { // <Type, ?> TODO: sub Type
simpleTree.setNodeType("Type");
// simpleTree.setLabel("?");
if (astNode.equals("WildcardType")) {
simpleTree.setLabel("?");
} else { // ArrayType, PrimitiveType, SimpleType, ParameterizedType, QualifiedType, WildcardType, UnionType,NameQualifiedType, IntersectionType
simpleTree.setLabel(astNode + "@@" + label);
}
} else if (astNode.endsWith("Name")) { // variableName, methodName, QualifiedName
if (label.startsWith("MethodName:")) { // <Method, name>
label = label.substring(11);
simpleTree.setNodeType("Method");
simpleTree.setLabel(label);
} else if (label.startsWith("Name:")) {
label = label.substring(5);
String firstChar = label.substring(0, 1);
if (firstChar.equals(firstChar.toUpperCase())) {
simpleTree.setNodeType("Name");
simpleTree.setLabel(label); // <Name, name>
} else {// variableName: <Variable, var>
simpleTree.setNodeType("Variable");
simpleTree.setLabel(getAbstractLabel(abstractVariableIdentifiers, label, ABSTRACT_VARIABLE));
}
} else {// variableName: <Variable, var>
simpleTree.setNodeType("Variable");
simpleTree.setLabel(getAbstractLabel(abstractVariableIdentifiers, label, ABSTRACT_VARIABLE));
}
} else if (astNode.equals("BooleanLiteral") ||astNode.equals("CharacterLiteral") || astNode.equals("ThisExpression")
|| astNode.equals("NullLiteral") || astNode.equals("NumberLiteral") || astNode.equals("StringLiteral")
|| astNode.equals("Modifier") || astNode.equals("Operator")) {
simpleTree.setNodeType(astNode);
simpleTree.setLabel(label);
} else {
simpleTree.setNodeType(astNode);
simpleTree.setLabel(astNode);
}
}
List<SimpleTree> simpleChildren = new ArrayList<>();
if (children != null && !astNode.endsWith("Type")) {
for (ITree child : children) {
simpleChildren.add(semiSourceCodeTree(actionSet, child, simpleTree));
}
}
simpleTree.setChildren(simpleChildren);
}
private List<Action> getAllMoveActions(HierarchicalActionSet actionSet) {
String astNodeType = actionSet.getAstNodeType();
if (Checker.containsBodyBlock(astNodeType)) {
List<Action> allMoveActions = getAllMoveActions2(actionSet);
if (allMoveActions != null && allMoveActions.size() > 0) {
ListSorter<Action> sorter = new ListSorter<Action>(allMoveActions);
List<Action> moveActions = sorter.sortAscending();
if (moveActions != null) {
allMoveActions = moveActions;
}
return allMoveActions;
} else {// FIXME: pure INS actions.
return null;
}
} else {// FIXME: pure INS actions.
return null;
}
/**
* Variables, non-new and used in the inserted statements, could be selected to localize buggy code
*/
}
private List<Action> getAllMoveActions2(HierarchicalActionSet actionSet) {
List<Action> allMoveActions = new ArrayList<>();
List<HierarchicalActionSet> actions = new ArrayList<>();
actions.addAll(actionSet.getSubActions());
if (actions.size() == 0) {
return null;
}
while (actions.size() > 0) {
List<HierarchicalActionSet> subActions = new ArrayList<>();
for (HierarchicalActionSet action : actions) {
subActions.addAll(action.getSubActions());
if (action.toString().startsWith("MOV")) {
allMoveActions.add(action.getAction());
}
}
actions.clear();
actions.addAll(subActions);
}
return allMoveActions;
}
private String getAbstractLabel(Map<String, String> map, String label, String nameType) {
if (map.containsKey(label)) {
return map.get(label);
} else {
String name = nameType + map.size();
map.put(label, name);
return name;
}
}
private HierarchicalActionSet findHierarchicalActionSet(int position, int length, HierarchicalActionSet actionSet) {
if (actionSet.getStartPosition() == position && actionSet.getLength() == length && !actionSet.getActionString().startsWith("INS")) {
return actionSet;
} else {
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
HierarchicalActionSet actSet = findHierarchicalActionSet(position, length, subActionSet);
if (actSet != null) {
return actSet;
}
}
}
return null;
}
public Map<String, String> getAbstractTypeIdentifiers() {
return abstractTypeIdentifiers;
}
public Map<String, String> getAbstractMethodIdentifiers() {
return abstractMethodIdentifiers;
}
public Map<String, String> getAbstractNameIdentifiers() {
return abstractNameIdentifiers;
}
public Map<String, String> getAbstractVariableIdentifiers() {
return abstractVariableIdentifiers;
}
}
@@ -1,140 +0,0 @@
package edu.lu.uni.serval.gumtree.regroup;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.ASTNodeMap;
/**
* A traveler to travel a tree-constructed object.
*
* @author kui.liu
*
*/
public class Traveler {
public List<List<String>> list = new ArrayList<>();
/**
* Get all action string by traveling HierarchicalActionSet in a deep-first way.
*
* @param actionSet
* @param astNodeTypeActionQueue
*/
public void travelActionSetDeepFirstToASTNodeQueue(HierarchicalActionSet actionSet, List<String> astNodeTypeActionQueue) {
if (actionSet == null) {
System.err.println("Null Action set!");
} else {
if (astNodeTypeActionQueue == null) {
astNodeTypeActionQueue = new ArrayList<>();
}
String actionStr = actionSet.getActionString();
actionStr = actionStr.substring(0, actionStr.indexOf("@@"));
astNodeTypeActionQueue.add(actionStr); // RawToken: TODO
if (actionStr.startsWith("DEL")) {
list.add(astNodeTypeActionQueue); // FIXME BUG: Change AST node type 1 to AST node type 2. Solve method: a list is one pattern.
} else {
List<HierarchicalActionSet> subActionSet = actionSet.getSubActions();
int size = subActionSet.size();
if (size > 0) {
for (HierarchicalActionSet subAction : subActionSet) {
List<String> astNodeTypeActionQueue_ = new ArrayList<>();
astNodeTypeActionQueue_.addAll(astNodeTypeActionQueue);
travelActionSetDeepFirstToASTNodeQueue(subAction, astNodeTypeActionQueue_);
}
} else {
list.add(astNodeTypeActionQueue);
}
}
}
}
/**
* Get all AST node types of a root tree by traveling the root tree in a deep-first way.
*
* @param root
* @return
*/
public static List<String> travelTreeDeepFirstToASTNodeQueue(ITree root) {
if (root == null) {
System.err.println("Null tree!");
return null;
}
List<String> astNodeTypeQueue = new ArrayList<>();
astNodeTypeQueue.add(ASTNodeMap.map.get(root.getType())); // RawToken: root.getLabel();
List<ITree> childrenTreeList = root.getChildren();
if (childrenTreeList != null && childrenTreeList.size() > 0) {
for (ITree childTree : childrenTreeList) {
astNodeTypeQueue.addAll(travelTreeDeepFirstToASTNodeQueue(childTree));
}
}
return astNodeTypeQueue;
}
/**
* Get all AST node types of a root tree by traveling the root tree in a breadth-first way.
*
* @param root
* @return
*/
public static List<String> travelTreeBreadthFirstToASTNodeQueue(ITree root) {
if (root == null) {
System.err.println("Null tree.");
return null;
}
List<String> astNodeTypeQueue = new ArrayList<>();
astNodeTypeQueue.add(ASTNodeMap.map.get(root.getType())); // RawToken: root.getLabel();
List<ITree> treeList = new ArrayList<>();
treeList.add(root);
while (!treeList.isEmpty()) {
List<ITree> childrenTreeList = new ArrayList<>();
for (ITree tree : treeList) {
astNodeTypeQueue.addAll(travelTreeBreadthFirstToASTNodeQueue(tree));
childrenTreeList.addAll(tree.getChildren());
}
treeList.clear();
treeList.addAll(childrenTreeList);
}
return astNodeTypeQueue;
}
/**
* Convert a root ITree into a SimpleTree by traveling the root tree in a deep-first way.
*
* SimpleTree node label is root.toShortString().
*
* @param root
* @param parent
* @return
*/
public static SimpleTree travelITreeDeepFirstToSimpleTree(ITree root, SimpleTree parent) {
if (root == null) {
System.err.println("Null tree!");
return null;
}
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel(root.toShortString());
simpleTree.setParent(parent);
List<SimpleTree> children = new ArrayList<>();
List<ITree> childrenTreeList = root.getChildren();
if (childrenTreeList != null && childrenTreeList.size() > 0) {
for (ITree childTree : childrenTreeList) {
children.add(travelITreeDeepFirstToSimpleTree(childTree, simpleTree));
}
}
simpleTree.setChildren(children);
return simpleTree;
}
}
@@ -1,33 +0,0 @@
package edu.lu.uni.serval.livestudy;
public class Alarm {
private String alarmType;
private String fileName;
private int startLine;
private int endLine;
public String getAlarmType() {
return alarmType;
}
public String getFileName() {
return fileName;
}
public int getStartLine() {
return startLine;
}
public int getEndLine() {
return endLine;
}
public Alarm(String alarmType, String fileName, int startLine, int endLine) {
super();
this.alarmType = alarmType;
this.fileName = fileName;
this.startLine = startLine;
this.endLine = endLine;
}
}
@@ -1,212 +0,0 @@
package edu.lu.uni.serval.livestudy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.utils.FileHelper;
public class ParseViolations {
private static final String OUTPUT_PATH = Configuration.ROOT_PATH + "LiveStudy/BugsInfo/";
private static int i = 0;
private static int j = 0;
public static void main(String[] args) {
// output path
// FileHelper.deleteDirectory(OUTPUT_PATH);
List<String> projects = new ArrayList<>();
File[] files = new File(OUTPUT_PATH).listFiles();
for (File file : files) {
if (file.isDirectory()) {
projects.add(file.getName());
}
}
String projectsPath = Configuration.ROOT_PATH + "LiveStudy/projects/";
String bugsListPath = Configuration.ROOT_PATH + "LiveStudy/BugsList/";
List<File> bugsListFiles = FileHelper.getAllFiles(bugsListPath, ".list");
for (File bugsListFile : bugsListFiles) {
String fileName = FileHelper.getFileNameWithoutExtension(bugsListFile);
if (projects.contains(fileName)) continue;
ParseViolations parser = new ParseViolations();
Map<String, Violation> violations = new HashMap<>(); // <ProjectName, Violations>, Violation: <projectName, List<Alarm>>.
violations = parser.readViolations(bugsListFile);
parser.parseViolationToTokens(violations, projectsPath);
}
System.out.println(i);
System.out.println(j);
}
/**
* Get the source code tokens of all violation instances by visiting each java project.
*
* @param violations
* @param projectsPath
*/
public void parseViolationToTokens(Map<String, Violation> violations, String projectsPath) {
for (Map.Entry<String, Violation> entry : violations.entrySet()) {
String projectName = entry.getKey();
List<Alarm> alarms = entry.getValue().getAlarms();
List<File> javaFiles = FileHelper.getAllFiles(projectsPath + projectName, ".java");
//Each violation: commons-math : DLS_DEAD_LOCAL_STORE : org/apache/commons/math4/dfp/Dfp.java : 2049 : 2049
/**
* @ProjectName
* @ViolationType
* @FileName
* @LineNumber
* @SourceCode
* @Tokens
* @NumberOfTokens
*/
// sizes file, tokens file, and bugs-info file
StringBuilder sizesBuilder = new StringBuilder();
StringBuilder tokensBuilder = new StringBuilder();
StringBuilder bugsInfoBuilder = new StringBuilder();
for (Alarm alarm : alarms) {
String fileName = alarm.getFileName();
// if (fileName.toLowerCase(Locale.ENGLISH).contains("test")) continue;
File sourceCodeFile = locateSourceCodeFile(javaFiles, fileName);
if (sourceCodeFile == null) {
j ++;
continue;
}
Parser parser = new Parser(alarm, sourceCodeFile);
parser.parse();
SimpleTree simpleTree = parser.getSimpleTree();
if (simpleTree != null) {
int finalStartLine = parser.getFinalStartLine();
int finalEndLine = parser.getFinalEndLine();
String violationType = alarm.getAlarmType();
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
StringBuilder sourceCode = readSourceCode(sourceCodeFile, finalStartLine, finalEndLine);
sizesBuilder.append(length).append("\n");
tokensBuilder.append(tokens).append("\n");
bugsInfoBuilder.append("###BugInstance###\n##Info:");
bugsInfoBuilder.append(violationType).append(":");
bugsInfoBuilder.append(projectName).append(":");
bugsInfoBuilder.append(fileName).append(":");
bugsInfoBuilder.append(finalStartLine).append(":");
bugsInfoBuilder.append(finalEndLine).append("\n");
bugsInfoBuilder.append(sourceCode).append("\n");
i ++;
} else {
j ++;
}
}
FileHelper.outputToFile(OUTPUT_PATH + "sizes.list", sizesBuilder, true);
FileHelper.outputToFile(OUTPUT_PATH + "tokens.list", tokensBuilder, true);
FileHelper.outputToFile(OUTPUT_PATH + "bugsInfo.list", bugsInfoBuilder, true);
}
}
private StringBuilder readSourceCode(File javaFile, int startLine, int endLine) {
StringBuilder sourceCode = new StringBuilder("##Source_Code:\n");
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(javaFile);
scanner = new Scanner(fis);
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
counter ++;
if (startLine <= counter && counter <= endLine) {
sourceCode.append(line + "\n");
}
if (counter == endLine) break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sourceCode;
}
private File locateSourceCodeFile(List<File> javaFiles, String fileName) {
for (File javaFile : javaFiles) {
if (javaFile.getPath().endsWith(fileName)) return javaFile;
}
return null;
}
public Map<String, Violation> readViolations(String violationFile) {
return readViolations(new File(violationFile));
}
public Map<String, Violation> readViolations(File violationFile) {
Map<String, Violation> violations = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(violationFile);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
//commons-math : DLS_DEAD_LOCAL_STORE : org/apache/commons/math4/dfp/Dfp.java : 2049 : 2049
String line = scanner.nextLine();
String[] elements = line.split(" : ");
String projectName = elements[0];
String alarmType = elements[1];
String fileName = elements[2];
int startLine = Integer.parseInt(elements[3]);
int endLine = Integer.parseInt(elements[4]);
Alarm alarm = new Alarm(alarmType, fileName, startLine, endLine);
addAlarmToViolations(projectName, alarm, violations);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
private void addAlarmToViolations(String projectName, Alarm alarm, Map<String, Violation> violations) {
Violation violation = null;
if (violations.containsKey(projectName)) {
violation = violations.get(projectName);
} else {
violation = new Violation(projectName);
violations.put(projectName, violation);
}
List<Alarm> alarms = violation.getAlarms();
alarms.add(alarm);
}
}
@@ -1,84 +0,0 @@
package edu.lu.uni.serval.livestudy;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
public class Parser {
// input
private Alarm alarm;
private File sourceCodeFile;
// output
private int finalStartLine = 0;
private int finalEndLine = 0;
private SimpleTree simpleTree = null;
public Parser(Alarm alarm, File sourceCodeFile) {
this.alarm = alarm;
this.sourceCodeFile = sourceCodeFile;
}
public int getFinalStartLine() {
return finalStartLine;
}
public int getFinalEndLine() {
return finalEndLine;
}
public SimpleTree getSimpleTree() {
return simpleTree;
}
public void parse() {
int startLine = this.alarm.getStartLine();
int endLine = this.alarm.getEndLine();
String violationType = this.alarm.getAlarmType();
ViolationSourceCodeTree treeParser = new ViolationSourceCodeTree(this.sourceCodeFile, startLine, endLine);
if ("EQ_DOESNT_OVERRIDE_EQUALS".equals(violationType)|| "HE_EQUALS_USE_HASHCODE".equals(violationType)
|| "HE_INHERITS_EQUALS_USE_HASHCODE".equals(violationType)|| "SE_NO_SUITABLE_CONSTRUCTOR".equals(violationType)
|| "RI_REDUNDANT_INTERFACES".equals(violationType) || "CN_IDIOM".equals(violationType)
|| "SE_NO_SERIALVERSIONID".equals(violationType) || "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(violationType)) {
// Class name level, tokens
// classStartP <= vS <= vE <= classEndP
ITree classNameTree = treeParser.getClassNameTokens();
if (classNameTree != null) {
this.simpleTree = new SimplifyTree().canonicalizeSourceCodeTree(classNameTree, null);
} else {
System.err.println("#Null_Violation_Hunk: " + this.alarm.getAlarmType() + ":" + this.sourceCodeFile.getPath() + ":" + startLine + ":" + endLine);
}
} else {
treeParser.extract();
List<ITree> matchedTrees = treeParser.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
System.err.println("#Null_Violation_Hunk: " + this.alarm.getAlarmType() + ":" + this.sourceCodeFile.getPath() + ":" + startLine + ":" + endLine);
} else {
this.simpleTree = new SimpleTree();
this.simpleTree.setLabel("Block");
this.simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, this.simpleTree);
children.add(simpleT);
}
this.simpleTree.setChildren(children);
}
}
if (this.simpleTree != null) {
this.finalStartLine = treeParser.getViolationFinalStartLine();
this.finalEndLine = treeParser.getViolationFinalEndLine();
}
}
}
@@ -1,6 +0,0 @@
## Parse the violations of Live Study ##
1. Localize bugs with FindBugs.
2. Parse results of FindBugs. (bug-type project.)
3. Parse source code of violations. (ParseViolation.java)
@@ -1,25 +0,0 @@
package edu.lu.uni.serval.livestudy;
import java.util.ArrayList;
import java.util.List;
public class Violation {
private String project;
private List<Alarm> alarms;
public String getProject() {
return project;
}
public List<Alarm> getAlarms() {
return alarms;
}
public Violation(String project) {
super();
this.project = project;
this.alarms = new ArrayList<>();
}
}
@@ -1,145 +0,0 @@
package edu.lu.uni.serval.parameters;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree;
/**
* Prepare data for tuning parameters of deep learning models.
*
* @author kui.liu
*
*/
public class ParseAlarms {
public static void main(String[] args) {
ParseAlarms parser = new ParseAlarms();
String outputPath = Configuration.ROOT_PATH + "TuneParameters/fixedAlarmTokens.list";
String outputPath2 = Configuration.ROOT_PATH + "TuneParameters/EmptyStatement/fixedAlarmTokens.list";
FileHelper.deleteFile(outputPath);
FileHelper.deleteFile(outputPath2);
int subIndex = 5;
String fixedAlarmFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
parser.dataPreparation(fixedAlarmFilesPath, positionsFilePath, subIndex, outputPath, outputPath2);
outputPath = Configuration.ROOT_PATH + "TuneParameters/unfixedAlarmTokens.list";
outputPath2 = Configuration.ROOT_PATH + "TuneParameters/EmptyStatement/unfixedAlarmTokens.list";
FileHelper.deleteFile(outputPath);
FileHelper.deleteFile(outputPath2);
subIndex = 8;
String unfixedAlarmFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
String unfixedPositionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
parser.dataPreparation(unfixedAlarmFilesPath, unfixedPositionsFilePath, subIndex, outputPath, outputPath2);
}
public void dataPreparation(String sourceCodeFilePath, String positionFilePath, int subIndex, String outputPath, String outputPath2) {
StringBuilder tokensBuilder = new StringBuilder();
List<File> javaFiles = FileHelper.getAllFilesInCurrentDiectory(sourceCodeFilePath, ".java");
int counter = 0;
int a = 0;
StringBuilder emptyStatements = new StringBuilder();
StringBuilder sizes = new StringBuilder();
for (File javaFile : javaFiles) {
String fileName = javaFile.getName().replace(".java", ".txt");
fileName = fileName.substring(subIndex);
// if (fileName.endsWith("apache-commons-configuration_8c42aa_8b26e6src#java#org#apache#commons#configuration#plist#XMLPropertyListConfiguration.txt")) {
// System.out.println();
// }
List<Violation> violations = readViolations(positionFilePath + fileName);
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
String alarmType = violation.getViolationType();
if (endLine > startLine + 5) continue;
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract(alarmType);
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
emptyStatements.append(alarmType + "," + fileName + "," + startLine + "," + endLine + "\n");
a ++;
continue;
}
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, null);
children.add(simpleT);
}
simpleTree.setChildren(children);
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
sizes.append(length + "\n");
tokensBuilder.append(alarmType + ":" + fileName + ":" + alarmTree.getViolationFinalStartLine() + ":" + alarmTree.getViolationFinalEndLine() + ":" + tokens + "\n");
counter ++;
if (counter % 10000 == 0) {
FileHelper.outputToFile(outputPath, tokensBuilder, true);
tokensBuilder.setLength(0);
}
}
}
System.out.println("Volidated Instances: " + counter);
System.out.println("Empty Instances: " + a);
FileHelper.outputToFile(outputPath2, emptyStatements, false);
FileHelper.outputToFile(outputPath.replace(".list", "Sizes.csv"), sizes, false);
FileHelper.outputToFile(outputPath, tokensBuilder, true);
}
private List<Violation> readViolations(String file) {
List<Violation> violations = new ArrayList<>();
String fileContent = FileHelper.readFile(file);
BufferedReader reader = null;
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
if (startLine == -1 || endLine == -1) {
continue;
}
String alarmType = positionStr[0];
Violation violation = new Violation(startLine, endLine, alarmType);
violations.add(violation);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
}
@@ -1,75 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class Categories {
public static void main(String[] args) throws IOException {
String content = FileHelper.readFile(Configuration.ROOT_PATH + "RQ1/Quantity-per-Fixed_1.0V-Type.csv");//"RQ1/Quantity-per-V-Type.csv");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = reader.readLine();
int counter = 0;
// int size = 400;
Map<String, Integer> map = new HashMap<>();
map.put("Dodgy code", 0);
map.put("Experimental", 0);
map.put("Internationalization", 0);
map.put("Multithreaded correctness", 0);
map.put("Malicious code vulnerability", 0);
map.put("Performance", 0);
map.put("Correctness", 0);
map.put("Security", 0);
map.put("Bad practice", 0);
map.put("Other", 0);
// List<Integer> list = new ArrayList<>();
Map<String, List<Integer>> map2 = new HashMap<>();
map2.put("Dodgy code", new ArrayList<Integer>());
map2.put("Experimental", new ArrayList<Integer>());
map2.put("Internationalization", new ArrayList<Integer>());
map2.put("Multithreaded correctness", new ArrayList<Integer>());
map2.put("Malicious code vulnerability", new ArrayList<Integer>());
map2.put("Performance", new ArrayList<Integer>());
map2.put("Correctness", new ArrayList<Integer>());
map2.put("Security", new ArrayList<Integer>());
map2.put("Bad practice", new ArrayList<Integer>());
map2.put("Other", new ArrayList<Integer>());
while ((line = reader.readLine()) != null) {
String type = line.substring(line.lastIndexOf(",") + 1);
counter ++;
if (map.containsKey(type)) {
map.put(type, map.get(type) + 1);
} else {
map.put(type, 1);
}
if (counter % 10 == 0) {
for (Map.Entry<String, List<Integer>> entry : map2.entrySet()) {
String type2 = entry.getKey();
Integer integ = map.get(type2);
if (integ == null) {
map2.get(type2).add(0);
} else {
map2.get(type2).add(integ);
}
}
// System.out.println();
}
}
reader.close();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
for (Map.Entry<String, List<Integer>> entry : map2.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
}
}
@@ -1,26 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import edu.lu.uni.serval.utils.FileHelper;
public class FixedProjects {
public static void main(String[] args) throws IOException {
String content = FileHelper.readFile("../FPM_Violations/RQ1/Quantity-per-FixedProj.csv");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = reader.readLine();
StringBuilder builder = new StringBuilder();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
// String projectName = elements[0];
builder.append(elements[0] + "\n");
}
reader.close();
FileHelper.outputToFile("../FPM_Violations/RQ1/fixedProjects.list", builder, false);
}
}
@@ -1,47 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class FixedVilations {
public static void main(String[] args) throws IOException {
// projectName,ViolationType,Number.
FileInputStream fis = new FileInputStream(Configuration.ROOT_PATH + "RQ1/fixed-alarms-v1.0.list");
Scanner scanner = new Scanner(fis);
Map<String, Integer> map = new HashMap<>();
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(":");
// String violationType = elements[0];
// String projectName = elements[1];
String key = elements[1] + "," + elements[0];
if (map.containsKey(key)) {
map.put(key, map.get(key)+ 1);
} else {
map.put(key, 1);
}
counter ++;
}
scanner.close();
fis.close();
System.out.println(counter);
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
builder.append(entry.getKey()).append(",").append(entry.getValue()).append("\n");
counter -= entry.getValue();
}
FileHelper.outputToFile(Configuration.ROOT_PATH + "RQ1/fixedViolations-v-1.0.csv", builder, false);
System.out.println(counter);
}
}
@@ -1,106 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
public class HunkSizeComputer {
public static void main(String[] args) throws IOException {
String violationsFilePath = Configuration.ROOT_PATH + "unFixedInstances/";
sizes(violationsFilePath, ".list");
}
public static void sizes(String filePath, String fileType) throws NumberFormatException, IOException {
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(filePath, fileType);
System.out.println(unfixedAlarmFiles.size());
StringBuilder builder = new StringBuilder();
int counter = 0;
int size = 0;
int i = 0;
List<Integer> sizes = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();// <size, amount>
for (File file : unfixedAlarmFiles) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
int startLine = Integer.parseInt(elements[4]);
int endLine = Integer.parseInt(elements[5]);
i ++;
if (startLine == -1 || endLine == -1) continue;
int range = endLine - startLine + 1;
builder.append(range + "\n");
if (range != 1) {
counter ++;
}
size ++;
sizes.add(range);
if (map.containsKey(range)) {
map.put(range, map.get(range) + 1);
} else {
map.put(range, 1);
}
}
reader.close();
}
System.out.println(i);
String content = FileHelper.readFile("Dataset/fixed-alarms-v1.0.list");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
int arrowIndex = line.indexOf("=>");
String buggyInfo = line.substring(0, arrowIndex);
String[] buggyElements = buggyInfo.split(":");
int startLine = Integer.parseInt(buggyElements[4]);
int endLine = Integer.parseInt(buggyElements[5]);
if (startLine == -1 || endLine == -1) continue;
int range = endLine - startLine + 1;
if (range != 1) {
counter ++;
}
builder.append(range + "\n");
size ++;
sizes.add(range);
if (map.containsKey(range)) {
map.put(range, map.get(range) + 1);
} else {
map.put(range, 1);
}
}
reader.close();
FileHelper.outputToFile("Dataset/sizes.csv", builder, false);
System.out.println(size);
System.out.println(counter);
ListSorter<Integer> sorter = new ListSorter<>(sizes);
sizes = sorter.sortAscending();
System.out.println(sizes.get((int) (sizes.size() * 0.7)));
System.out.println(sizes.get((int) (sizes.size() * 0.8)));
System.out.println(sizes.get((int) (sizes.size() * 0.9)));
System.out.println(sizes.get((int) (sizes.size() * 0.95)));
int sum = 0;
for (i = 1; i <= 100; i ++) {
sum += map.get(i);
System.out.println(i + "," + String.format("%.2f", ((double) sum / size * 100)));
}
}
}
@@ -1,74 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
/**
* Statistics of files and code lines of projects in Live Study.
*
* @author kui.liu
*
*/
public class NumberOfFilesAndCodeLines {
public static void main(String[] args) {
String projectsPath = Configuration.ROOT_PATH + "LiveStudy/BugsInfo/";
File[] projects = new File(projectsPath).listFiles();
for (File project : projects) {
if (project.isDirectory()) {
if (project.getName().equals("poi"))
new NumberOfFilesAndCodeLines().statistic(project.getName());
}
}
}
public void statistic(String project) {
List<File> javaFiles = FileHelper.getAllFiles(Configuration.ROOT_PATH + "LiveStudy/projects/" + project, ".java");
int numberOfFiles = 0;
int LOC = 0;
for (File javaFile : javaFiles) {
if (!javaFile.getPath().toLowerCase(Locale.ENGLISH).contains("test")) {
numberOfFiles ++;
LOC += readLinesOfCode(javaFile);
}
// numberOfFiles ++;
// LOC += readLinesOfCode(javaFile);
}
System.out.println(project + " : Files = " + numberOfFiles + ", LOC = " + LOC);
}
private int readLinesOfCode(File javaFile) {
int LOC = 0;
Scanner scanner = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(javaFile);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
scanner.nextLine();
LOC ++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return LOC;
}
}
@@ -1,104 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import edu.lu.uni.serval.utils.FileHelper;
public class ReadSpearmanResults {
public static void main(String[] args) throws IOException {
String fileName = "../FPM_Violations/RQ1/SpearmanResults.list";
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
String str1 = "1";
String str2 = "2";
String str3 = "3";
String str4 = "4";
String str5 = "5";
String str6 = "6";
String str7 = "7";
String str8 = "8";
String str9 = "9";
String str10 = "10";
int counter = 0;
while ((line = reader.readLine()) != null) {
// String[] elements = line.split("");
counter ++;
if ((counter / 10 == 2 || counter / 10 == 4) && counter % 10 == 1) {
str1 += "@@@@1";
str2 += "@@@@2";
str3 += "@@@@3";
str4 += "@@@@4";
str5 += "@@@@5";
str6 += "@@@@6";
str7 += "@@@@7";
str8 += "@@@@8";
str9 += "@@@@9";
str10 += "@@@@10";
}
int n = counter % 10;
switch (n) {
case 1:
str1 += "@" + line;
break;
case 2:
str2 += "@" + line;
break;
case 3:
str3 += "@" + line;
break;
case 4:
str4 += "@" + line;
break;
case 5:
str5 += "@" + line;
break;
case 6:
str6 += "@" + line;
break;
case 7:
str7 += "@" + line;
break;
case 8:
str8 += "@" + line;
break;
case 9:
str9 += "@" + line;
break;
case 0:
str10 += "@" + line;
break;
}
}
reader.close();
str1 = str1.replace(".", ",");
str2 = str2.replace(".", ",");
str3 = str3.replace(".", ",");
str4 = str4.replace(".", ",");
str5 = str5.replace(".", ",");
str6 = str6.replace(".", ",");
str7 = str7.replace(".", ",");
str8 = str8.replace(".", ",");
str9 = str9.replace(".", ",");
str10 = str10.replace(".", ",");
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
System.out.println(str5);
System.out.println(str6);
System.out.println(str7);
System.out.println(str8);
System.out.println(str9);
System.out.println(str10);
}
}
@@ -1,362 +0,0 @@
a <- read.csv("~/Public/git/FPM_Violations/RQ1/TenFolds-Q/Ten-fold-all-fixed-violation-type1-70.csv", header=T)
d<-cor.test( ~ X0 + X0.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = FALSE, sep = "@")
d<-cor.test( ~ X1 + X1.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2 + X2.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3 + X3.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4 + X4.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5 + X5.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6 + X6.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7 + X7.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8 + X8.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9 + X9.T,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X0.R + X0.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X1.R + X1.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2.R + X2.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3.R + X3.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4.R + X4.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5.R + X5.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6.R + X6.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7.R + X7.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8.R + X8.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9.R + X9.T.R,
data=a, method = "spearman",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X0 + X0.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X1 + X1.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2 + X2.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3 + X3.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4 + X4.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5 + X5.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6 + X6.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7 + X7.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8 + X8.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9 + X9.T,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X0.R + X0.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X1.R + X1.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2.R + X2.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3.R + X3.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4.R + X4.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5.R + X5.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6.R + X6.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7.R + X7.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8.R + X8.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9.R + X9.T.R,
data=a, method = "pearson",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X0 + X0.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X1 + X1.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2 + X2.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3 + X3.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4 + X4.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5 + X5.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6 + X6.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7 + X7.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8 + X8.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9 + X9.T,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X0.R + X0.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X1.R + X1.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X2.R + X2.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X3.R + X3.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X4.R + X4.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X5.R + X5.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X6.R + X6.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X7.R + X7.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X8.R + X8.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
d<-cor.test( ~ X9.R + X9.T.R,
data=a, method = "kendall",
continuity = FALSE,
conf.level = 0.95)
write(c(d$estimate, d$p.value), file = "~/Public/git/FPM_Violations/RQ1/SpearmanResults.list",append = TRUE, sep = "@")
a=1
@@ -1,81 +0,0 @@
package edu.lu.uni.serval.statistics;
public class SpearmanCodeGenerator {
public static void main(String[] args) {
generateSpearmanCode();
}
public static void generateSpearmanCode() {
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + " + X" + i + ".T, \n";
s += " data=a,";
s += " method = \"spearman\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
if (i == 0) {
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = FALSE, sep = \"@\")\n";
} else {
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
}
System.out.println(s);
}
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + ".R + X" + i + ".T.R, \n";
s += " data=a,";
s += " method = \"spearman\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
System.out.println(s);
}
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + " + X" + i + ".T, \n";
s += " data=a,";
s += " method = \"pearson\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
System.out.println(s);
}
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + ".R + X" + i + ".T.R, \n";
s += " data=a,";
s += " method = \"pearson\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
System.out.println(s);
}
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + " + X" + i + ".T, \n";
s += " data=a,";
s += " method = \"kendall\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
System.out.println(s);
}
for (int i = 0; i < 10; i ++) {
String s = "d<-cor.test( ~ X" + i + ".R + X" + i + ".T.R, \n";
s += " data=a,";
s += " method = \"kendall\",\n";
s += " continuity = FALSE,\n";
s += " conf.level = 0.95)\n";
s += "write(c(d$estimate, d$p.value), file = \"~/Public/git/FPM_Violations/RQ1/SpearmanResults.list\"," +
"append = TRUE, sep = \"@\")\n";
System.out.println(s);
}
}
}
@@ -1,947 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
import edu.lu.uni.serval.utils.MapSorter;
public class Statistic {
private static Map<String, Integer> map1 = new HashMap<String, Integer>();
private static Map<String, Integer> map2 = new HashMap<>();
public static void main(String[] args) throws IOException {
/*
* DM_DEFAULT_ENCODING
* NP_NONNULL_RETURN_VIOLATION
* NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
* NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE
* ODR_OPEN_DATABASE_RESOURCE
* PZLA_PREFER_ZERO_LENGTH_ARRAYS
* RI_REDUNDANT_INTERFACES
* RV_RETURN_VALUE_IGNORED_BAD_PRACTICE
* RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT
* SE_NO_SERIALVERSIONID
* SF_SWITCH_NO_DEFAULT
* SIC_INNER_SHOULD_BE_STATIC
* SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING
* UC_USELESS_CONDITION
* UC_USELESS_OBJECT
* UCF_USELESS_CONTROL_FLOW
* WMI_WRONG_MAP_ITERATOR
*/
// /*
// * Quantities' distribution of all violation types.
// */
// quantityOfEachViolationType();
// /*
// * Widespread of each violation type.
// */
// widespreadOfEachViolationType();
// /*
// * Statistics of categories of all violation types.
// */
// statisticWithCategories();
//
// /*
// * Quantity of each violation type in each project.
// */
// reloadData();
//
// /*
// * Quantities' distribution of all fixed violation types.
// * Widespread of each fixed violation type.
// * Statistics of categories of all fixed violation types.
// * Quantity of each fixed violation type in each project.
// */
// statisticOfFixedViolations();
// fixedVSunfixed();
/**
* Do statistics from two files:
*/
statistics("../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv", "", map1, 16918530, 730);
// statistics("../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv", "Fixed");
statistics("../FPM_Violations/RQ1/fixedViolations-v-1.0.csv", "Fixed_1.0", map2, 88927, 548);
// fixedVSunfixed();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Integer> entry : map1.entrySet()) {
String key = entry.getKey();
builder.append(key + "," + entry.getValue() + "," + (map2.containsKey(key) ? map2.get(key) : 0) + "\n");
}
FileHelper.outputToFile(Configuration.ROOT_PATH + "RQ2/quantity-ratios.csv", builder, false);
// statisticsOfFixedViolations();
//rsync -avP gaia-cluster:/work/users/kliu/FixPattern/FPM_Violations/UnfixedViolations/BC_UNCONFIRMED_CAST/Sizes.list Sizes/Sizes1.list
// String s = "rsync -avP gaia-cluster:/work/users/kliu/FixPattern/FPM_Violations/UnfixedViolations_RQ3/";
// File[] files = new File(Configuration.ROOT_PATH + "RQ3_1/UnfixedInstances/").listFiles();
// int i = 0;
// for (File file : files) {
// if (file.getName().endsWith(".list")) {
// i ++;
// System.out.println(s + FileHelper.getFileNameWithoutExtension(file) + "/Sizes.list Sizes/Sizes" + i + ".list");
// }
// }
// tokenSizes();
}
public static void tokenSizes() {
List<Integer> sizes = new ArrayList<>();
List<File> files = FileHelper.getAllFilesInCurrentDiectory(Configuration.ROOT_PATH + "RQ3_1/Sizes/", ".list");
StringBuilder builder = new StringBuilder();
for (File file : files) {
try {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
sizes.add(Integer.parseInt(line));
builder.append("1,").append(line).append("\n");
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(sizes.size());
FileHelper.outputToFile(Configuration.ROOT_PATH + "RQ3_1/Sizes.csv", builder, false);
}
public static void statisticsOfFixedViolations() {
String statistic = "../FPM_Violations/OUTPUT3/";
List<File> files = FileHelper.getAllFiles(statistic, ".list");
int positions = 0;
int numV = 0;
int testAlarms = 0;
int nullGumTreeResults = 0;
int nullMappingGumTreeResults = 0;
int pureDeletion = 0;
int timeout = 0;
int noSourceCodeChagnes = 0;
int largeHunk = 0;
int nullSourceCode = 0;
int noStatementChanges = 0;
int nullDiffentry = 0;
int TestingInfo = 0;
int i = 0;
Map<String, Integer> types1 = new HashMap<>();
for (File file : files) {
if (file.getName().startsWith("statistic")) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
i ++;
try {
while ((line = reader.readLine()) != null) {
if (line.startsWith("Positions")) {
positions += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NumViolations")) {
numV += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else
if (line.startsWith("TestViolations")) {
testAlarms += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullGumTreeResults")) {
nullGumTreeResults += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NoSourceCodeChanges")) {
noSourceCodeChagnes += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NoStatementChanges")) {
noStatementChanges += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullDiffEntry")) {
nullDiffentry += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullMatchedGumTreeResults")) {
nullMappingGumTreeResults += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("PureDeletion")) {
pureDeletion += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("LargeHunk")) {
largeHunk += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("NullSourceCode")) {
nullSourceCode += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("Timeout")) {
timeout += Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
} else if (line.startsWith("TestingInfo")) {
TestingInfo +=Integer.parseInt(line.substring(line.lastIndexOf(":") + 1).trim());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (file.getName().startsWith("UnfixedV")) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
// types1
if (line.startsWith("## OAR [")) break;
String type = line.substring(0, line.indexOf(":"));
if (types1.containsKey(type)) {
types1.put(type, types1.get(type) + 1);
} else {
types1.put(type, 1);
}
if (line.startsWith("#NullSourceCode:")) {
System.out.println(line);
}
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Map<String, Integer> types = new HashMap<>();
// FileInputStream fis = new FileInputStream("../FPM_Violations/OAR.FPM.4222208.stderr");
// Scanner scanner = new Scanner(fis);
// while (scanner.hasNextLine()) {
// String line = scanner.nextLine();
// if (line.startsWith("## OAR [")) break;
// String type = line.substring(0, line.indexOf(":"));
// if (types.containsKey(type)) {
// types.put(type, types.get(type) + 1);
// } else {
// types.put(type, 1);
// }
// }
// scanner.close();
// fis.close();
// int sum = 0;
// int sum2 = 0;
// for (Map.Entry<String, Integer> entry : types.entrySet()) {
// System.out.println(entry.getKey() + ": " + entry.getValue());
// if (!entry.getKey().startsWith("#PureDeletion")) {
// if (!entry.getKey().startsWith("#Timeout") && !entry.getKey().startsWith("#TestViolation"))
// sum += entry.getValue();
// else sum2 += entry.getValue();
// }
// }
int sum3 = 0;
int sum4 = 0;
int sum5 = 0;
for (Map.Entry<String, Integer> entry : types1.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
if (!entry.getKey().startsWith("#PureDeletion")) {
if (!entry.getKey().startsWith("#Timeout") && !entry.getKey().startsWith("#TestViolation"))
sum3 += entry.getValue();
else sum4 += entry.getValue();
sum5+= entry.getValue();
}
}
System.out.println(sum5);
System.out.println(i);
System.out.println("\n\nStatistics:\nPositions: " + positions);
System.out.println("NumViolations: " + numV);
System.out.println("\nTestViolation: " + testAlarms + " :: " + types.get("#TestViolation") + " :: " + types1.get("#TestViolation"));
System.out.println("NullGumTreeResults: " + nullGumTreeResults + " :: " + types.get("#NullGumTreeResults") + " :: " + types1.get("#NullGumTreeResults"));
System.out.println("NoSourceCodeChange: " + noSourceCodeChagnes + " :: " + types.get("#NoSourceCodeChange") + " :: " + types1.get("#NoSourceCodeChange"));
System.out.println("NoStatementChange: " + noStatementChanges + " :: " + types.get("#NoStatementChange") + " :: " + types1.get("#NoStatementChange"));
System.out.println("NullDiffEntry: " + nullDiffentry + " :: " + types.get("#NullDiffEntry") + " :: " + types1.get("#NullDiffEntry"));
System.out.println("NullMatchedGumTreeResult: " + nullMappingGumTreeResults + " :: " + types.get("#NullMatchedGumTreeResult") + " :: " + types1.get("#NullMatchedGumTreeResult"));
System.out.println("PureDeletion: " + pureDeletion + " :: " + types.get("#PureDeletion") + " :: " + types1.get("#PureDeletion"));
System.out.println("LargeHunk: " + largeHunk + " :: " + types.get("#LargeHunk") + " :: " + types1.get("#LargeHunk"));
System.out.println("NullSourceCode: " + nullSourceCode + " :: " + types.get("#NullSourceCode") + " :: " + types1.get("#NullSourceCode"));
System.out.println("Timeout: " + timeout + " :: " + types.get("#Timeout") + " :: " + types1.get("#Timeout"));
System.out.println("TestingInfo: " + TestingInfo + " :: " + types.get("#TestingInfo") + " :: " + types1.get("#TestingInfo"));
// System.out.println("A: " + (positions + testAlarms + timeout));
// System.out.println("B: " + (numV + testAlarms + timeout));
System.out.println(testAlarms + nullGumTreeResults + noSourceCodeChagnes + noStatementChanges +
nullDiffentry + nullMappingGumTreeResults + nullSourceCode + timeout + TestingInfo);
// System.out.println(nullGumTreeResults + noSourceCodeChagnes + noStatementChanges +
// nullDiffentry + nullMappingGumTreeResults + nullSourceCode + TestingInfo);
// System.out.println(testAlarms + timeout);
// System.out.println(sum);
// System.out.println(sum2);
System.out.println(sum3);
System.out.println(sum4);
System.out.println(types1);
/*32690 56237 ,, 88927 31782
* Statistics:
TestViolation: 4682 :: null :: null
NullGumTreeResults: 0 :: null :: null
NoSourceCodeChange: 7010 :: null :: 6984
NoStatementChange: 163 :: null :: 163
NullDiffEntry: 6943 :: null :: 6943
NullMatchedGumTreeResult: 25747 :: null :: 25747
PureDeletion: 10815 :: null :: 10815
LargeHunk: 6318 :: null :: 6318
NullSourceCode: 1025 :: null :: 1025
Timeout: 0 :: null :: null
TestingInfo: 0 :: null :: null
A: 4682
B: 4682
51888
47206
4682
47180
0
*/
}
public static void statistics(String fileName, String type, Map<String, Integer> map, int totalQ, int totalS) throws IOException {
FileInputStream fis = new FileInputStream(fileName);
Scanner scanner = new Scanner(fis);
Map<String, Integer> violationTypesMap = new HashMap<>();
Map<String, Integer> projectsMap = new HashMap<>();
Map<String, Integer> perVperProjMap = new HashMap<>();
Map<String, List<String>> perVProjs = new HashMap<>();
Map<String, List<String>> widespreadViolationsMap = new HashMap<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(",");
String projectName = elements[0];
String violationType = elements[1];
int quantity = Integer.parseInt(elements[2]);
addToMap(projectsMap, projectName, quantity);
addToMap(violationTypesMap, violationType, quantity);
addToMap(perVperProjMap, projectName + "," + violationType, quantity);
if (widespreadViolationsMap.containsKey(violationType)) {
List<String> projectList = widespreadViolationsMap.get(violationType);
if (!projectList.contains(projectName)) {
projectList.add(projectName);
}
} else {
List<String> projectList = new ArrayList<>();
projectList.add(projectName);
widespreadViolationsMap.put(violationType, projectList);
}
if (perVProjs.containsKey(violationType)) {
perVProjs.get(violationType).add(projectName);
} else {
List<String> projs = new ArrayList<>();
projs.add(projectName);
perVProjs.put(violationType, projs);
}
}
scanner.close();
fis.close();
// Category
MapSorter<String, Integer> sorter = new MapSorter<String, Integer>();
violationTypesMap = sorter.sortByValueDescending(violationTypesMap);
projectsMap = sorter.sortByValueDescending(projectsMap);
Map<String, String> categories = new HashMap<>();
Map<String, List<String>> categoryVList = new HashMap<>();
String content = FileHelper.readFile("../FPM_Violations/RQ1/ViolationCategory.list");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split("@@");
categories.put(elements[1], elements[0]); // Violation type, category
}
reader.close();
// Sort Violation types by widespread.
Map<String, Integer> widespreadOfAllViolations = new HashMap<>();
for (Map.Entry<String, List<String>> entry : widespreadViolationsMap.entrySet()) {
widespreadOfAllViolations.put(entry.getKey(), entry.getValue().size());
}
widespreadOfAllViolations = sorter.sortByValueDescending(widespreadOfAllViolations);
StringBuilder wbuilder = new StringBuilder("Type,Identifier,Quantity,Category\n");
int identifier1 = 0;
for (Map.Entry<String, Integer> entry : widespreadOfAllViolations.entrySet()) {
identifier1 ++;
String violationType = entry.getKey();
String category = categories.get(violationType);
if (category == null || category.equals("null")) {
category = "Other";
}
wbuilder.append(violationType + "," + identifier1 + "," + entry.getValue() + "," + category + "\n");
if (categoryVList.containsKey(category)) {
categoryVList.get(category).add(violationType);
} else {
List<String> list = new ArrayList<>();
list.add(violationType);
categoryVList.put(category, list);
}
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Widespread-per-" + type + "V-Type.csv", wbuilder, false);
// output statistics
List<String> sortedViolationTypes = new ArrayList<>();
Map<String, Integer> quantityOfCategory = new HashMap<>();
Map<String, List<String>> violationTypesOfCategory = new HashMap<>();
Map<String, List<String>> projectsOfCategory = new HashMap<>();
StringBuilder violationsBuilder = new StringBuilder("Type,Identifier,Quantity,Ratio,Widespread,Ratio2, Categories\n");
int identifier = 0;
for (Map.Entry<String, Integer> entry : violationTypesMap.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
identifier ++;
String category = categories.get(violationType);
if (category == null || category.equals("null")) {
category = "Other";
}
int spread = widespreadViolationsMap.get(violationType).size();
violationsBuilder.append(violationType + "," + identifier + "," + quantity + "," + ((double) quantity / totalQ * 100) + ","
+ spread + "," + ((double) spread / totalS * 100) + "," + category + "\n");
if (quantityOfCategory.containsKey(category)) {
quantityOfCategory.put(category, quantityOfCategory.get(category) + quantity);
} else {
quantityOfCategory.put(category, quantity);
}
if (violationTypesOfCategory.containsKey(category)) {
List<String> violationTypes = violationTypesOfCategory.get(category);
if (!violationTypes.contains(violationType)) {
violationTypes.add(violationType);
}
} else {
List<String> violationTypes = new ArrayList<>();
violationTypes.add(violationType);
violationTypesOfCategory.put(category, violationTypes);
}
if (projectsOfCategory.containsKey(category)) {
List<String> projs = projectsOfCategory.get(category);
List<String> projs2 = perVProjs.get(violationType);
for (String proj : projs2) {
if (!projs.contains(proj)) {
projs.add(proj);
}
}
} else {
projectsOfCategory.put(category, perVProjs.get(violationType));
}
sortedViolationTypes.add(violationType);
map.put(violationType, quantity);
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-" + type + "V-Type.csv", violationsBuilder, false);
StringBuilder pBuilder = new StringBuilder("Project,Quantity\n");
List<String> projectNames = new ArrayList<>();
for (Map.Entry<String, Integer> entry : projectsMap.entrySet()) {
String project = entry.getKey();
pBuilder.append(project + "," + entry.getValue() + "\n");
projectNames.add(project);
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-" + type + "Proj.csv", pBuilder, false);
StringBuilder categoryBuilder = new StringBuilder("Category,Quantity,Types,Projects\n");
for (Map.Entry<String, Integer> entry : quantityOfCategory.entrySet()) {
String key = entry.getKey();
categoryBuilder.append(key + "," + entry.getValue() + "," + violationTypesOfCategory.get(key).size() + "," + projectsOfCategory.get(key).size() + "\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-" + type + "Category.csv", categoryBuilder, false);
StringBuilder builder = new StringBuilder("Projects");
String a = "";
String b = "";
for (int i = 0; i < sortedViolationTypes.size(); i ++) {
builder.append("," + sortedViolationTypes.get(i));
if (i < 50) {
a += "a$" + sortedViolationTypes.get(i) + ",";
b += "'" + (i + 1) + "',";
}
}
builder.append("\n");
System.out.println(a);
System.out.println(b);
// for (int i = 0; i < sortedViolationTypes.size(); i ++) {
// builder.append("," + i);
// }
// builder.append("\n");
StringBuilder bui = new StringBuilder();
for (int i = 0; i < projectNames.size(); i++) {
String projectName = projectNames.get(i);
builder.append(projectName);
for (int j = 0; j < sortedViolationTypes.size(); j ++) {
String violationType = sortedViolationTypes.get(j);
String key = projectName + "," + violationType;
Integer value = perVperProjMap.get(key);
if (value == null) {
value = 0;
} else {
bui.append(projectName).append(",").append(violationType).append(",").append(value).append(",").append(categories.get(violationType) == null ? "Other" : categories.get(violationType)).append("\n");
}
builder.append("," + value);
}
builder.append("\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-" + type + "type.csv", builder, false);
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-" + type + "type___.csv", bui, false);
StringBuilder ssbuilder = new StringBuilder("Type, Identifier, Quantity, Category\n");
Map<String, Integer> perTypePerProj = new HashMap<>();
Map<String, List<String>> categoryProjects = new HashMap<>();
List<String> others = new ArrayList<>();
for (int j = 0; j < sortedViolationTypes.size(); j ++) {
String violationType = sortedViolationTypes.get(j);
List<Integer> projs = new ArrayList<>();
for (int i = 0; i < projectNames.size(); i++) {
String projectName = projectNames.get(i);
String key = projectName + "," + violationType;
Integer value = perVperProjMap.get(key);
if (value != null) {
String category = categories.get(violationType);
if (category == null || category.equals("null")) {
category = "Other";
if (!others.contains(violationType)) {
others.add(violationType);
}
}
ssbuilder.append(violationType + "," + (j + 1) + "," + value + "," + category + "\n");
projs.add(value);
if (categoryProjects.containsKey(category)) {
List<String> projects = categoryProjects.get(category);
if (!projects.contains(projectName)) {
projects.add(projectName);
}
} else {
List<String> projects = new ArrayList<>();
projects.add(projectName);
categoryProjects.put(category, projects);
}
}
}
ListSorter<Integer> sorter2 = new ListSorter<Integer>(projs);
projs = sorter2.sortAscending();
int index = projs.size() % 2 == 0 ? projs.size() / 2 - 1 : projs.size() / 2;
perTypePerProj.put(violationType, projs.get(index));
}
System.out.println(others);
for (Map.Entry<String, List<String>> entry : categoryVList.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue().size() + ":" + categoryProjects.get(entry.getKey()).size());
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-" + type + "type2.csv", ssbuilder, false);
StringBuilder ssbuilder2 = new StringBuilder("Type, Identifier, Quantity, Category\n");
perTypePerProj = sorter.sortByValueDescending(perTypePerProj);
int j = 0;
for (Map.Entry<String, Integer> entry : perTypePerProj.entrySet()) {
String violationType = entry.getKey();
// System.out.println(entry.getValue());
j ++;
for (int i = 0; i < projectNames.size(); i++) {
String projectName = projectNames.get(i);
String key = projectName + "," + violationType;
Integer value = perVperProjMap.get(key);
if (value != null) {
String category = categories.get(violationType);
if (category == null || category.equals("null")) {
category = "Other";
}
ssbuilder2.append(violationType + "," + j + "," + value + "," + category + "\n");
}
}
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-" + type + "type3.csv", ssbuilder2, false);
// StringBuilder pVpPBuilder = new StringBuilder();
// for (Map.Entry<String, Integer> entry : perVperProjMap.entrySet()) {
// pVpPBuilder.append(entry.getKey() + "," + entry.getValue() + "\n");
// }
// FileHelper.outputToFile("../FPM_Violations/RQ1/Per-project-per-" + type + "type.csv", pVpPBuilder, false);
}
private static void addToMap(Map<String, Integer> map, String key, int value) {
if (map.containsKey(key)) {
map.put(key, map.get(key) + value);
} else {
map.put(key, value);
}
}
public static void quantityOfEachViolationType() {
Map<String, Integer> violationQuantities = readTypeQuantityMap("../FPM_Violations/RQ1/distinct-per-vtype.csv");
Map<String, Integer> violationWidespread = readWidespread("../FPM_Violations/RQ1/distinct-per-project-vtype.csv");
StringBuilder buidler = new StringBuilder("Violation Type, Identifier, Quantity, Quantity of Projects, Ratio\n");
int identifier = 0;
double totality = 15961605d;
int sum = 0;
for (Map.Entry<String, Integer> entry : violationQuantities.entrySet()) {
String key = entry.getKey();
identifier ++;
int quantity = entry.getValue();
sum += quantity;
buidler.append(key + "," + identifier + "," + quantity + "," + violationWidespread.get(key) + "," + (sum / totality) + "\n");
// if (identifier >= 50) break;
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-V-type.csv", buidler, false);
}
public static void widespreadOfEachViolationType() {
Map<String, Integer> violationWidespread = readWidespread("../FPM_Violations/RQ1/distinct-per-project-vtype.csv");
System.out.println("Violation types: " + violationWidespread.size());
StringBuilder buidler = new StringBuilder("Violation Type, Identifier, Quantity of Projects\n");
int identifier = 0;
for (Map.Entry<String, Integer> entry : violationWidespread.entrySet()) {
identifier ++;
buidler.append(entry.getKey() + "," + identifier + "," + entry.getValue() + "\n");
// if (identifier >= 50) break;
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Widespread-per-V-type.csv", buidler, false);
}
public static void statisticWithCategories() throws IOException {
Map<String, String> categories = new HashMap<>();
String content = FileHelper.readFile("../FPM_Violations/RQ1/ViolationCategory.list");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split("@@");
categories.put(elements[1], elements[0]); // Violation type, category
}
reader.close();
Map<String, Integer> quantityOfCategory = new HashMap<>();
String content2 = FileHelper.readFile("../FPM_Violations/RQ1/distinct-per-vtype.csv");
reader = new BufferedReader(new StringReader(content2));
line = reader.readLine();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
String violationType = elements[0];
int quantity = Integer.parseInt(elements[1]);
String category = categories.get(violationType);
if (quantityOfCategory.containsKey(category)) {
quantityOfCategory.put(category, quantityOfCategory.get(category) + quantity);
} else {
quantityOfCategory.put(category, quantity);
}
}
reader.close();
String categoryQuantity = "Category Type, Quantity\n";
for (Map.Entry<String, Integer> entry : quantityOfCategory.entrySet()) {
categoryQuantity += entry.getKey() + "," + entry.getValue() + "\n";
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-Category.csv", categoryQuantity, false);
}
public static void reloadData() {
// ordered projects by quantity of violations
Map<String, Integer> projectQuantities = readTypeQuantityMap("../FPM_Violations/RQ1/distinct-per-project.csv");
List<String> projectNames = new ArrayList<>();
for (Map.Entry<String, Integer> entry : projectQuantities.entrySet()) {
projectNames.add(entry.getKey());
}
// get ordered types by quantity of violations.
Map<String, Integer> violations = readTypeQuantityMap("../FPM_Violations/RQ1/distinct-per-vtype.csv");
List<String> violationTypes = new ArrayList<>();
for (Map.Entry<String, Integer> entry : violations.entrySet()) {
violationTypes.add(entry.getKey());
}
Map<String, String> perVperProjs = readData("../FPM_Violations/RQ1/distinct-per-project-vtype.csv");
StringBuilder builder = new StringBuilder("Projects");
for (int i = 0; i < violationTypes.size(); i ++) {
builder.append("," + violationTypes.get(i));
}
builder.append("\n");
for (int i = 0; i < projectNames.size(); i++) {
String projectName = projectNames.get(i);
builder.append(projectName);
for (int j = 0; j < violationTypes.size(); j ++) {
String violationType = violationTypes.get(j);
String key = projectName + "," + violationType;
String value = perVperProjs.get(key);
if (value == null) {
value = "0";
}
builder.append("," + value);
}
builder.append("\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-type.csv", builder, false);
}
public static void statisticOfFixedViolations() throws IOException {
String fileName = "../FPM_Violations/RQ1/fixed-alarms-v1.0.list";
FileInputStream fis = new FileInputStream(fileName);
Scanner scanner = new Scanner(fis);
Map<String, Integer> violations = new HashMap<>();
Map<String, Integer> projects = new HashMap<>();
Map<String, Integer> perVperPro = new HashMap<>();
Map<String, List<String>> widespreadFixedV = new HashMap<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(":");
String violationType = elements[0];
String projectName = elements[1];
if (violations.containsKey(violationType)) {
violations.put(violationType, violations.get(violationType) + 1);
} else {
violations.put(violationType, 1);
}
if (projects.containsKey(projectName)) {
projects.put(projectName, projects.get(projectName) + 1);
} else {
projects.put(projectName, 1);
}
String perVperProj = projectName + "," + violationType;
if (perVperPro.containsKey(perVperProj)) {
perVperPro.put(perVperProj, perVperPro.get(perVperProj) + 1);
} else {
perVperPro.put(perVperProj, 1);
}
if (widespreadFixedV.containsKey(violationType)) {
List<String> projectList = widespreadFixedV.get(violationType);
if (!projectList.contains(projectName)) {
projectList.add(projectName);
}
} else {
List<String> projectList = new ArrayList<>();
projectList.add(projectName);
widespreadFixedV.put(violationType, projectList);
}
}
scanner.close();
fis.close();
MapSorter<String, Integer> sorter = new MapSorter<String, Integer>();
violations = sorter.sortByValueDescending(violations);
projects = sorter.sortByValueDescending(projects);
Map<String, String> categories = new HashMap<>();
String content = FileHelper.readFile("../FPM_Violations/RQ1/ViolationCategory.list");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split("@@");
categories.put(elements[1], elements[0]); // Violation type, category
}
reader.close();
List<String> violationTypes = new ArrayList<>();
Map<String, Integer> quantityOfCategory = new HashMap<>();
StringBuilder violationsBuilder = new StringBuilder();
for (Map.Entry<String, Integer> entry : violations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
violationsBuilder.append(violationType + "," + quantity + "," + widespreadFixedV.get(violationType).size() + "\n");
String category = categories.get(violationType);
if (quantityOfCategory.containsKey(category)) {
quantityOfCategory.put(category, quantityOfCategory.get(category) + quantity);
} else {
quantityOfCategory.put(category, quantity);
}
violationTypes.add(violationType);
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-FixedV-Type.csv", violationsBuilder, false);
StringBuilder pBuilder = new StringBuilder();
List<String> projectNames = new ArrayList<>();
for (Map.Entry<String, Integer> entry : projects.entrySet()) {
String project = entry.getKey();
pBuilder.append(project + "," + entry.getValue() + "\n");
projectNames.add(project);
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-FixedV-Proj.csv", pBuilder, false);
StringBuilder categoryBuilder = new StringBuilder();
for (Map.Entry<String, Integer> entry : quantityOfCategory.entrySet()) {
categoryBuilder.append(entry.getKey() + "," + entry.getValue() + "\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Quantity-per-Fixed-Category.csv", categoryBuilder, false);
StringBuilder builder = new StringBuilder("Projects");
for (int i = 0; i < violationTypes.size(); i ++) {
builder.append("," + violationTypes.get(i));
if (i < 500) {
}
}
builder.append("\n");
for (int i = 0; i < projectNames.size(); i++) {
String projectName = projectNames.get(i);
builder.append(projectName);
for (int j = 0; j < violationTypes.size(); j ++) {
String violationType = violationTypes.get(j);
String key = projectName + "," + violationType;
Integer value = perVperPro.get(key);
if (value == null) {
value = 0;
}
builder.append("," + value);
}
builder.append("\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-project-per-Fixed-type.csv", builder, false);
StringBuilder pVpPBuilder = new StringBuilder();
for (Map.Entry<String, Integer> entry : perVperPro.entrySet()) {
pVpPBuilder.append(entry.getKey() + "," + entry.getValue() + "\n");
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Per-project-per-Fixed-type.csv", pVpPBuilder, false);
}
public static void fixedVSunfixed() throws IOException {
String fileName = "../FPM_Violations/RQ1/Quantity-per-V-type.csv"; // all violations
Map<String, Integer> allViolations = readTypeQuantityMap(fileName);
String file = "../FPM_Violations/RQ1/Quantity-per-FixedV-Type.csv"; // fixed violations
Map<String, Integer> fixedViolations = readTypeQuantityMap(file);
MapSorter<String, Integer> sorter = new MapSorter<>();
allViolations = sorter.sortByValueDescending(allViolations);
StringBuilder builder = new StringBuilder("Type,fixed,unfixed,all,fixed Ratio,unfixed Ratio\n");
for (Map.Entry<String, Integer> entry : allViolations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
Integer fixedQuantity = fixedViolations.get(violationType);
if (fixedQuantity == null) {
builder.append(violationType + "," + 0 + "," + quantity + "," + quantity + ",0.0,1\n");
} else {
builder.append(violationType + "," + fixedQuantity + "," + (quantity)
+ "," + quantity + "," + ((double)fixedQuantity) / ((double) quantity)
+ "," + ((double)(quantity)) / ((double) quantity) + "\n");
}
}
FileHelper.outputToFile("../FPM_Violations/RQ1/Distribution-per-Fixed-type-VS-per-unFixed-type.csv", builder, false);
}
private static Map<String, String> readData(String fileName) {
Map<String, String> perVperPros = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(fileName);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(",");
String key = elements[0] + "," + elements[1];
String value = elements[2];
perVperPros.put(key, value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return perVperPros;
}
private static Map<String, Integer> readTypeQuantityMap(String fileName) {
Map<String, Integer> map = new HashMap<>();
String fileContent = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(fileContent));
try {
String line= reader.readLine();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
map.put(elements[0], Integer.valueOf(elements[2]));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
MapSorter<String, Integer> sorter = new MapSorter<>();
map = sorter.sortByValueDescending(map);
return map;
}
private static Map<String, Integer> readWidespread(String fileName) {
Map<String, Integer> violationWidespread = new HashMap<String, Integer>();
String fileContent = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(fileContent));
try {
String line = null;
while ((line = reader.readLine()) != null) {
String[] strArray = line.split(",");
String key = strArray[1];
if (violationWidespread.containsKey(key)) {
violationWidespread.put(key, violationWidespread.get(key) + 1);
} else {
violationWidespread.put(key, 1);
}
// String[] elements = line.split(",");
// violationWidespread.put(elements[0], Integer.valueOf(elements[1]));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
MapSorter<String, Integer> sorter = new MapSorter<>();
violationWidespread = sorter.sortByValueDescending(violationWidespread);
return violationWidespread;
}
}
@@ -1,243 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
public class TenFoldPossibilities {
public static void main(String[] args) throws IOException {
rankingInTenFolds("../FPM_Violations/RQ1/TenFolds/", "../FPM_Violations/RQ1/Quantity-per-V-type.csv", "TenFolds"); // ten folds of all projects.
rankingInTenFolds("../FPM_Violations/RQ1/FixedTenFolds-1/", "../FPM_Violations/RQ1/Quantity-per-FixedV-Type.csv", "FixedTenFolds-1");// all fixed violation types are contained.
}
public static void rankingInTenFolds(String tenFoldFiles, String typeFile, String outputPath) throws NumberFormatException, IOException {
List<File> files = FileHelper.getAllFilesInCurrentDiectory(tenFoldFiles, ".list");
Map<Integer, Map<String, Double>> tenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
for (int i = 0; i < 10; i ++) {
List<String> projects = selectPorjects(files, i);
// the distribution of all violation types in these 9 sub-sets.
Map<String, Integer> violations = new HashMap<>();
int quantityOfAllViolations = readViolationsPerProject(violations, projects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
// the distribution of all fixed violation types in these 9 sub-sets.
Map<String, Integer> fixedViolations = new HashMap<>();
int quantityOfAllFixedViolations = readViolationsPerProject(fixedViolations, projects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
// Ratio of each violation type in all violations.
Map<String, Double> ratioOfEachViolationType = new HashMap<>(); // ratio of each violation type
Map<String, Double> ratioOfEachFixedViolationType1 = new HashMap<>(); // quantity of each fixed violation type / quantity of each violation type.
Map<String, Double> ratioOfEachFixedViolationType2 = new HashMap<>(); // quantity of each fixed violation type / quantityOfAllFixedViolations
for (Map.Entry<String, Integer> entry : violations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / quantityOfAllViolations;
ratioOfEachViolationType.put(violationType, ratio);
if (fixedViolations.containsKey(violationType)) {
int quantity1 = fixedViolations.get(violationType);
double ratio1 = (double)quantity1 / quantity;
double ratio2 = (double)quantity1 / quantityOfAllFixedViolations;
ratioOfEachFixedViolationType1.put(violationType, ratio1);
ratioOfEachFixedViolationType2.put(violationType, ratio2);
} else {
ratioOfEachFixedViolationType1.put(violationType, 0d);
}
}
tenFoldsOfAllViolationTypes.put(i, ratioOfEachViolationType);
tenFoldsOfAllFixedViolationTypes1.put(i, ratioOfEachFixedViolationType1);
tenFoldsOfAllFixedViolationTypes2.put(i, ratioOfEachFixedViolationType2);
// Testing Data
List<String> testingProjects = readList(files.get(i));
Map<String, Integer> testingViolations = new HashMap<>();
int testingQuantitifOfAllViolations = readViolationsPerProject(testingViolations, testingProjects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
Map<String, Integer> testingFixedViolations = new HashMap<>();
int testingQuantityOfAllFixedViolations = readViolationsPerProject(testingFixedViolations, testingProjects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
Map<String, Double> testingRatioOfEachViolationType = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType1 = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType2 = new HashMap<>();
for (Map.Entry<String, Integer> entry : testingViolations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / testingQuantitifOfAllViolations;
testingRatioOfEachViolationType.put(violationType, ratio);
if (testingFixedViolations.containsKey(violationType)) {
int quantity1 = testingFixedViolations.get(violationType);
testingRatioOfEachFixedViolationType1.put(violationType, (double)quantity1 / quantity);
testingRatioOfEachFixedViolationType2.put(violationType, (double)quantity1 / testingQuantityOfAllFixedViolations);
} else {
testingRatioOfEachFixedViolationType1.put(violationType, 0d);
}
}
testingTenFoldsOfAllViolationTypes.put(i, testingRatioOfEachViolationType);
testingTenFoldsOfAllFixedViolationTypes1.put(i, testingRatioOfEachFixedViolationType1);
testingTenFoldsOfAllFixedViolationTypes2.put(i, testingRatioOfEachFixedViolationType2);
}
List<String> violationTypes = readTypes(typeFile);
outputTenFolds(tenFoldsOfAllViolationTypes, testingTenFoldsOfAllViolationTypes, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-violation-type.csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes1, testingTenFoldsOfAllFixedViolationTypes1, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type1.csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes2, testingTenFoldsOfAllFixedViolationTypes2, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type2.csv");
}
private static List<String> readTypes(String fileName) throws IOException {
List<String> violationTypes = new ArrayList<>();
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = reader.readLine();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
violationTypes.add(elements[0]);
}
reader.close();
return violationTypes;
}
private static void outputTenFolds(Map<Integer, Map<String, Double>> ratiosMap, Map<Integer, Map<String, Double>> testingMap, List<String> violationTypes, String fileName) {
StringBuilder builder = new StringBuilder("Violatype, 0, 0-R, 0-T, 0-T-R, 1, 1-R, 1-T, 1-T-R, 2, 2-R, 2-T, 2-T-R, 3, 3-R, 3-T, 3-T-R, 4, 4-R, 4-T, 4-T-R, 5, 5-R, 5-T, 5-T-R, 6, 6-R, 6-T, 6-T-R, 7, 7-R, 7-T, 7-T-R, 8, 8-R, 8-T, 8-T-R, 9, 9-R, 9-T, 9-T-R\n");
Map<Integer, Map<String, Integer>> ratiosRankingMap = new HashMap<>();
Map<Integer, Map<String, Integer>> testingDataRankingMap = new HashMap<>();
for (String type : violationTypes) {
builder.append(type);
for (int i = 0; i < 10; i ++) {
Map<String, Double> ratios = ratiosMap.get(i);
Map<String, Double> testingData = testingMap.get(i);
Map<String, Integer> ratiosRanking;
Map<String, Integer> testingDataRanking;
if (!ratiosRankingMap.containsKey(i)) {
ratiosRanking = rankAlarmTypes(ratios);
testingDataRanking = rankAlarmTypes(testingData);
} else {
ratiosRanking = ratiosRankingMap.get(i);
testingDataRanking = testingDataRankingMap.get(i);
}
if (ratios.containsKey(type)) {
builder.append(", " + ratios.get(type));//.toString().replace(".", ",")
} else {
builder.append(", 0.0");
}
if (ratiosRanking.containsKey(type)) {
builder.append(", " + ratiosRanking.get(type));
} else {
builder.append(", " + (ratiosRanking.size() + 1));
}
if (testingData.containsKey(type)) {
builder.append(", " + testingData.get(type));
} else {
builder.append(", 0.0");
}
if (testingDataRanking.containsKey(type)) {
builder.append(", " + testingDataRanking.get(type));
} else {
builder.append(", " + (testingDataRanking.size() + 1));
}
}
builder.append("\n");
}
FileHelper.outputToFile(fileName, builder, false);
}
private static Map<String, Integer> rankAlarmTypes(Map<String, Double> possibilityMap) {
MapSorter<String, Double> sorter = new MapSorter<>();
possibilityMap = sorter.sortByValueDescending(possibilityMap);
Map<String, Integer> ranking = new HashMap<>();
int ranker = 0;
double possibility = 0;
for (Map.Entry<String, Double> entry : possibilityMap.entrySet()) {
String alarmType = entry.getKey();
double value = entry.getValue();
if (possibility != value) {
ranker = ranking.size() + 1;
}
ranking.put(alarmType, ranker);
}
return ranking;
}
private static int readViolationsPerProject(Map<String, Integer> violations, List<String> projects, String fileName) throws NumberFormatException, IOException {
int quantityOfAllViolations = 0;
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
String projectName = elements[0];
if (projects.contains(projectName)) {
String alarmType = elements[1];
int quantity = Integer.parseInt(elements[2]);
quantityOfAllViolations += quantity;
if (violations.containsKey(alarmType)) {
violations.put(alarmType, violations.get(alarmType) + quantity);
} else {
violations.put(alarmType, quantity);
}
}
}
reader.close();
return quantityOfAllViolations;
}
private static List<String> selectPorjects(List<File> files, int i) {
List<String> projects = new ArrayList<>();
for (File file : files) {
if (file.getName().equals("Fold_" + i + ".list")) {
continue;
}
projects.addAll(readList(file));
}
return projects;
}
private static List<String> readList(File file) {
List<String> projects = new ArrayList<>();
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
projects.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return projects;
}
}
@@ -1,256 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
/**
* Fixed violation types are selected by their widespread.
*
* @author kui.liu
*
*/
public class TenFoldPossibilities2 {
public static void main(String[] args) throws IOException {
rankingInTenFolds("../FPM_Violations/RQ1/FixedTenFolds-W/", "../FPM_Violations/RQ1/Widespread-per-FixedV-Type.csv", "FixedTenFolds-W");
}
public static void rankingInTenFolds(String tenFoldFiles, String typeFile, String outputPath) throws NumberFormatException, IOException {
int topNumber = 10;
List<String> violationTypes = readTypes(typeFile, topNumber);
List<File> files = FileHelper.getAllFilesInCurrentDiectory(tenFoldFiles, ".list");
Map<Integer, Map<String, Double>> tenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
for (int i = 0; i < 10; i ++) {
List<String> projects = selectPorjects(files, i);
// all violations in these 9 sub-sets.
Map<String, Integer> violations = new HashMap<>();
int quantityOfAllViolations = readViolationsPerProject(violationTypes, violations, projects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
Map<String, Integer> fixedViolations = new HashMap<>();
int quantityOfAllFixedViolations = readViolationsPerProject(violationTypes, fixedViolations, projects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
// Ratio of each violation type in all violations.
Map<String, Double> ratioOfEachViolationType = new HashMap<>(); // ratio of each violation type
Map<String, Double> ratioOfEachFixedViolationType1 = new HashMap<>(); // quantity of each fixed violation type / quantity of each violation type.
Map<String, Double> ratioOfEachFixedViolationType2 = new HashMap<>(); // quantity of each fixed violation type / quantityOfAllFixedViolations
for (Map.Entry<String, Integer> entry : violations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / quantityOfAllViolations;
ratioOfEachViolationType.put(violationType, ratio);
if (fixedViolations.containsKey(violationType)) {
int quantity1 = fixedViolations.get(violationType);
double ratio1 = (double)quantity1 / quantity;
double ratio2 = (double)quantity1 / quantityOfAllFixedViolations;
ratioOfEachFixedViolationType1.put(violationType, ratio1);
ratioOfEachFixedViolationType2.put(violationType, ratio2);
} else {
ratioOfEachFixedViolationType1.put(violationType, 0d);
}
}
tenFoldsOfAllViolationTypes.put(i, ratioOfEachViolationType);
tenFoldsOfAllFixedViolationTypes1.put(i, ratioOfEachFixedViolationType1);
tenFoldsOfAllFixedViolationTypes2.put(i, ratioOfEachFixedViolationType2);
// Testing Data
List<String> testingProjects = readList(files.get(i));
Map<String, Integer> testingViolations = new HashMap<>();
int testingQuantitifOfAllViolations = readViolationsPerProject(violationTypes, testingViolations, testingProjects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
Map<String, Integer> testingFixedViolations = new HashMap<>();
int testingQuantityOfAllFixedViolations = readViolationsPerProject(violationTypes, testingFixedViolations, testingProjects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
Map<String, Double> testingRatioOfEachViolationType = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType1 = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType2 = new HashMap<>();
for (Map.Entry<String, Integer> entry : testingViolations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / testingQuantitifOfAllViolations;
testingRatioOfEachViolationType.put(violationType, ratio);
if (testingFixedViolations.containsKey(violationType)) {
int quantity1 = testingFixedViolations.get(violationType);
testingRatioOfEachFixedViolationType1.put(violationType, (double)quantity1 / quantity);
testingRatioOfEachFixedViolationType2.put(violationType, (double)quantity1 / testingQuantityOfAllFixedViolations);
} else {
testingRatioOfEachFixedViolationType1.put(violationType, 0d);
}
}
testingTenFoldsOfAllViolationTypes.put(i, testingRatioOfEachViolationType);
testingTenFoldsOfAllFixedViolationTypes1.put(i, testingRatioOfEachFixedViolationType1);
testingTenFoldsOfAllFixedViolationTypes2.put(i, testingRatioOfEachFixedViolationType2);
}
outputTenFolds(tenFoldsOfAllViolationTypes, testingTenFoldsOfAllViolationTypes, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-violation-type-" + topNumber + ".csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes1, testingTenFoldsOfAllFixedViolationTypes1, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type1-" + topNumber + ".csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes2, testingTenFoldsOfAllFixedViolationTypes2, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type2-" + topNumber + ".csv");
}
private static List<String> readTypes(String fileName, int number) throws IOException {
List<String> violationTypes = new ArrayList<>();
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = reader.readLine();
int counter = 0;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
violationTypes.add(elements[0]);
if (++counter == number) break;
}
reader.close();
return violationTypes;
}
private static void outputTenFolds(Map<Integer, Map<String, Double>> ratiosMap, Map<Integer, Map<String, Double>> testingMap, List<String> violationTypes, String fileName) {
StringBuilder builder = new StringBuilder("Violatype, 0, 0-R, 0-T, 0-T-R, 1, 1-R, 1-T, 1-T-R, 2, 2-R, 2-T, 2-T-R, 3, 3-R, 3-T, 3-T-R, 4, 4-R, 4-T, 4-T-R, 5, 5-R, 5-T, 5-T-R, 6, 6-R, 6-T, 6-T-R, 7, 7-R, 7-T, 7-T-R, 8, 8-R, 8-T, 8-T-R, 9, 9-R, 9-T, 9-T-R\n");
Map<Integer, Map<String, Integer>> ratiosRankingMap = new HashMap<>();
Map<Integer, Map<String, Integer>> testingDataRankingMap = new HashMap<>();
for (String type : violationTypes) {
builder.append(type);
for (int i = 0; i < 10; i ++) {
Map<String, Double> ratios = ratiosMap.get(i);
Map<String, Double> testingData = testingMap.get(i);
Map<String, Integer> ratiosRanking;
Map<String, Integer> testingDataRanking;
if (!ratiosRankingMap.containsKey(i)) {
ratiosRanking = rankAlarmTypes(ratios);
testingDataRanking = rankAlarmTypes(testingData);
} else {
ratiosRanking = ratiosRankingMap.get(i);
testingDataRanking = testingDataRankingMap.get(i);
}
if (ratios.containsKey(type)) {
builder.append(", " + ratios.get(type));//.toString().replace(".", ",")
} else {
builder.append(", 0.0");
}
if (ratiosRanking.containsKey(type)) {
builder.append(", " + ratiosRanking.get(type));
} else {
builder.append(", " + (ratiosRanking.size() + 1));
}
if (testingData.containsKey(type)) {
builder.append(", " + testingData.get(type));
} else {
builder.append(", 0.0");
}
if (testingDataRanking.containsKey(type)) {
builder.append(", " + testingDataRanking.get(type));
} else {
builder.append(", " + (testingDataRanking.size() + 1));
}
}
builder.append("\n");
}
FileHelper.outputToFile(fileName, builder, false);
}
private static Map<String, Integer> rankAlarmTypes(Map<String, Double> possibilityMap) {
MapSorter<String, Double> sorter = new MapSorter<>();
possibilityMap = sorter.sortByValueDescending(possibilityMap);
Map<String, Integer> ranking = new HashMap<>();
int ranker = 0;
double possibility = 0;
for (Map.Entry<String, Double> entry : possibilityMap.entrySet()) {
String alarmType = entry.getKey();
double value = entry.getValue();
if (possibility != value) {
ranker = ranking.size() + 1;
}
ranking.put(alarmType, ranker);
}
return ranking;
}
private static int readViolationsPerProject(List<String> violationTypes, Map<String, Integer> violations, List<String> projects, String fileName) throws NumberFormatException, IOException {
int quantityOfAllViolations = 0;
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
String projectName = elements[0];
if (projects.contains(projectName)) {
String alarmType = elements[1];
if (violationTypes.contains(alarmType)) {
int quantity = Integer.parseInt(elements[2]);
quantityOfAllViolations += quantity;
if (violations.containsKey(alarmType)) {
violations.put(alarmType, violations.get(alarmType) + quantity);
} else {
violations.put(alarmType, quantity);
}
}
}
}
reader.close();
return quantityOfAllViolations;
}
private static List<String> selectPorjects(List<File> files, int i) {
List<String> projects = new ArrayList<>();
for (File file : files) {
if (file.getName().equals("Fold_" + i + ".list")) {
continue;
}
projects.addAll(readList(file));
}
return projects;
}
private static List<String> readList(File file) {
List<String> projects = new ArrayList<>();
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
projects.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return projects;
}
}
@@ -1,256 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
/**
* Fixed violation types are selected by their quantities.
*
* @author kui.liu
*
*/
public class TenFoldPossibilities3 {
public static void main(String[] args) throws IOException {
rankingInTenFolds("../FPM_Violations/RQ1/TenFolds/", "../FPM_Violations/RQ1/Quantity-per-V-type.csv", "TenFolds-Q"); // ten folds of all projects.
// rankingInTenFolds("../FPM_Violations/RQ1/FixedTenFolds-Q/", "../FPM_Violations/RQ1/Quantity-per-FixedV-Type.csv", "FixedTenFolds-Q");
}
public static void rankingInTenFolds(String tenFoldFiles, String typeFile, String outputPath) throws NumberFormatException, IOException {
int topNumber = 70;
List<String> violationTypes = readTypes(typeFile, topNumber);
List<File> files = FileHelper.getAllFilesInCurrentDiectory(tenFoldFiles, ".list");
Map<Integer, Map<String, Double>> tenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> tenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllViolationTypes = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes1 = new HashMap<>();
Map<Integer, Map<String, Double>> testingTenFoldsOfAllFixedViolationTypes2 = new HashMap<>();
for (int i = 0; i < 10; i ++) {
List<String> projects = selectPorjects(files, i);
// all violations in these 9 sub-sets.
Map<String, Integer> violations = new HashMap<>();
int quantityOfAllViolations = readViolationsPerProject(violationTypes, violations, projects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
Map<String, Integer> fixedViolations = new HashMap<>();
int quantityOfAllFixedViolations = readViolationsPerProject(violationTypes, fixedViolations, projects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
// Ratio of each violation type in all violations.
Map<String, Double> ratioOfEachViolationType = new HashMap<>(); // ratio of each violation type
Map<String, Double> ratioOfEachFixedViolationType1 = new HashMap<>(); // quantity of each fixed violation type / quantity of each violation type.
Map<String, Double> ratioOfEachFixedViolationType2 = new HashMap<>(); // quantity of each fixed violation type / quantityOfAllFixedViolations
for (Map.Entry<String, Integer> entry : violations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / quantityOfAllViolations;
ratioOfEachViolationType.put(violationType, ratio);
if (fixedViolations.containsKey(violationType)) {
int quantity1 = fixedViolations.get(violationType);
double ratio1 = (double)quantity1 / quantity;
double ratio2 = (double)quantity1 / quantityOfAllFixedViolations;
ratioOfEachFixedViolationType1.put(violationType, ratio1);
ratioOfEachFixedViolationType2.put(violationType, ratio2);
} else {
ratioOfEachFixedViolationType1.put(violationType, 0d);
}
}
tenFoldsOfAllViolationTypes.put(i, ratioOfEachViolationType);
tenFoldsOfAllFixedViolationTypes1.put(i, ratioOfEachFixedViolationType1);
tenFoldsOfAllFixedViolationTypes2.put(i, ratioOfEachFixedViolationType2);
// Testing Data
List<String> testingProjects = readList(files.get(i));
Map<String, Integer> testingViolations = new HashMap<>();
int testingQuantitifOfAllViolations = readViolationsPerProject(violationTypes, testingViolations, testingProjects, "../FPM_Violations/RQ1/all-leafnodes-per-project-vtype.csv");
Map<String, Integer> testingFixedViolations = new HashMap<>();
int testingQuantityOfAllFixedViolations = readViolationsPerProject(violationTypes, testingFixedViolations, testingProjects, "../FPM_Violations/RQ1/distinct-fixed-summary-per-project-vtype.csv");
Map<String, Double> testingRatioOfEachViolationType = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType1 = new HashMap<>();
Map<String, Double> testingRatioOfEachFixedViolationType2 = new HashMap<>();
for (Map.Entry<String, Integer> entry : testingViolations.entrySet()) {
String violationType = entry.getKey();
int quantity = entry.getValue();
double ratio = (double) quantity / testingQuantitifOfAllViolations;
testingRatioOfEachViolationType.put(violationType, ratio);
if (testingFixedViolations.containsKey(violationType)) {
int quantity1 = testingFixedViolations.get(violationType);
testingRatioOfEachFixedViolationType1.put(violationType, (double)quantity1 / quantity);
testingRatioOfEachFixedViolationType2.put(violationType, (double)quantity1 / testingQuantityOfAllFixedViolations);
} else {
testingRatioOfEachFixedViolationType1.put(violationType, 0d);
}
}
testingTenFoldsOfAllViolationTypes.put(i, testingRatioOfEachViolationType);
testingTenFoldsOfAllFixedViolationTypes1.put(i, testingRatioOfEachFixedViolationType1);
testingTenFoldsOfAllFixedViolationTypes2.put(i, testingRatioOfEachFixedViolationType2);
}
outputTenFolds(tenFoldsOfAllViolationTypes, testingTenFoldsOfAllViolationTypes, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-violation-type" + topNumber + ".csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes1, testingTenFoldsOfAllFixedViolationTypes1, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type1-" + topNumber + ".csv");
outputTenFolds(tenFoldsOfAllFixedViolationTypes2, testingTenFoldsOfAllFixedViolationTypes2, violationTypes, "../FPM_Violations/RQ1/" + outputPath + "/Ten-fold-all-fixed-violation-type2-" + topNumber + ".csv");
}
private static List<String> readTypes(String fileName, int number) throws IOException {
List<String> violationTypes = new ArrayList<>();
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = reader.readLine();
int counter = 0;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
violationTypes.add(elements[0]);
if(++ counter == number) break;
}
reader.close();
return violationTypes;
}
private static void outputTenFolds(Map<Integer, Map<String, Double>> ratiosMap, Map<Integer, Map<String, Double>> testingMap, List<String> violationTypes, String fileName) {
StringBuilder builder = new StringBuilder("Violatype, 0, 0-R, 0-T, 0-T-R, 1, 1-R, 1-T, 1-T-R, 2, 2-R, 2-T, 2-T-R, 3, 3-R, 3-T, 3-T-R, 4, 4-R, 4-T, 4-T-R, 5, 5-R, 5-T, 5-T-R, 6, 6-R, 6-T, 6-T-R, 7, 7-R, 7-T, 7-T-R, 8, 8-R, 8-T, 8-T-R, 9, 9-R, 9-T, 9-T-R\n");
Map<Integer, Map<String, Integer>> ratiosRankingMap = new HashMap<>();
Map<Integer, Map<String, Integer>> testingDataRankingMap = new HashMap<>();
for (String type : violationTypes) {
builder.append(type);
for (int i = 0; i < 10; i ++) {
Map<String, Double> ratios = ratiosMap.get(i);
Map<String, Double> testingData = testingMap.get(i);
Map<String, Integer> ratiosRanking;
Map<String, Integer> testingDataRanking;
if (!ratiosRankingMap.containsKey(i)) {
ratiosRanking = rankAlarmTypes(ratios);
testingDataRanking = rankAlarmTypes(testingData);
} else {
ratiosRanking = ratiosRankingMap.get(i);
testingDataRanking = testingDataRankingMap.get(i);
}
if (ratios.containsKey(type)) {
builder.append(", " + ratios.get(type));//.toString().replace(".", ",")
} else {
builder.append(", 0.0");
}
if (ratiosRanking.containsKey(type)) {
builder.append(", " + ratiosRanking.get(type));
} else {
builder.append(", " + (ratiosRanking.size() + 1));
}
if (testingData.containsKey(type)) {
builder.append(", " + testingData.get(type));
} else {
builder.append(", 0.0");
}
if (testingDataRanking.containsKey(type)) {
builder.append(", " + testingDataRanking.get(type));
} else {
builder.append(", " + (testingDataRanking.size() + 1));
}
}
builder.append("\n");
}
FileHelper.outputToFile(fileName, builder, false);
}
private static Map<String, Integer> rankAlarmTypes(Map<String, Double> possibilityMap) {
MapSorter<String, Double> sorter = new MapSorter<>();
possibilityMap = sorter.sortByValueDescending(possibilityMap);
Map<String, Integer> ranking = new HashMap<>();
int ranker = 0;
double possibility = 0;
for (Map.Entry<String, Double> entry : possibilityMap.entrySet()) {
String alarmType = entry.getKey();
double value = entry.getValue();
if (possibility != value) {
ranker = ranking.size() + 1;
}
ranking.put(alarmType, ranker);
}
return ranking;
}
private static int readViolationsPerProject(List<String> violationTypes, Map<String, Integer> violations, List<String> projects, String fileName) throws NumberFormatException, IOException {
int quantityOfAllViolations = 0;
String content = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
String projectName = elements[0];
if (projects.contains(projectName)) {
String alarmType = elements[1];
if (violationTypes.contains(alarmType)) {
int quantity = Integer.parseInt(elements[2]);
quantityOfAllViolations += quantity;
if (violations.containsKey(alarmType)) {
violations.put(alarmType, violations.get(alarmType) + quantity);
} else {
violations.put(alarmType, quantity);
}
}
}
}
reader.close();
return quantityOfAllViolations;
}
private static List<String> selectPorjects(List<File> files, int i) {
List<String> projects = new ArrayList<>();
for (File file : files) {
if (file.getName().equals("Fold_" + i + ".list")) {
continue;
}
projects.addAll(readList(file));
}
return projects;
}
private static List<String> readList(File file) {
List<String> projects = new ArrayList<>();
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
try {
while ((line = reader.readLine()) != null) {
projects.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return projects;
}
}
@@ -1,121 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import edu.lu.uni.serval.utils.FileHelper;
public class TenFoldProjs {
public static void main(String[] args) {
List<String> allProjects = readList("../FPM_Violations/RQ1/Quantity-per-Proj.csv");
randomSeparateProjects(allProjects, "../FPM_Violations/RQ1/TenFolds/");
List<String> allFixedProjects = readList("../FPM_Violations/RQ1/Quantity-per-FixedProj.csv");
randomSeparateProjects(allFixedProjects, "../FPM_Violations/RQ1/FixedTenFolds-1/");
List<String> selectedProjects = selectFixedProjects("../FPM_Violations/RQ1/Quantity-per-FixedProj.csv");
randomSeparateProjects(selectedProjects, "../FPM_Violations/RQ1/FixedTenFolds-2/");
}
private static void randomSeparateProjects(List<String> projectNames, String outputPath) {
List<Integer> selectedIndexes = new ArrayList<>();
int number = projectNames.size();
int number2 = (int) Math.round((double) number / 10);
Map<Integer, List<String>> map = new HashMap<>();
for (int i = 0; i < 9; i ++) {
int counter = 0;
List<String> value = new ArrayList<>();
while (counter < number2) {
Random random = new Random();
int index = random.nextInt(number);
if (!selectedIndexes.contains(index)) {
counter ++;
value.add(projectNames.get(index));
selectedIndexes.add(index);
if (selectedIndexes.size() == number) break;
}
}
map.put(i, value);
}
List<String> value = new ArrayList<>();
for (int i = 0; i < number; i ++) {
if (!selectedIndexes.contains(i)) {
value.add(projectNames.get(i));
}
}
map.put(9, value);
for (Map.Entry<Integer, List<String>> entry : map.entrySet()) {
int key = entry.getKey();
List<String> projects = entry.getValue();
System.out.println(projects.size());
String pojectsStr = "";
for (String project : projects) {
pojectsStr += project + "\n";
}
FileHelper.outputToFile(outputPath + "Fold_" + key + ".list", pojectsStr, false);
}
}
private static List<String> selectFixedProjects(String fileName) {
List<String> list = new ArrayList<>();
String fileContent = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(fileContent));
String line= null;
try {
line = reader.readLine();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(",");
list.add(elements[0]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
private static List<String> readList(String fileName) {
List<String> list = new ArrayList<>();
String fileContent = FileHelper.readFile(fileName);
BufferedReader reader = new BufferedReader(new StringReader(fileContent));
String line= null;
try {
line = reader.readLine();
while ((line = reader.readLine()) != null) {
list.add(line.split(",")[0]);;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}
}
@@ -1,73 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.MapSorter;
public class UnhappenedTypesLiveStudy {
public static void main(String[] args) throws IOException {
List<String> top50Types = readTypes();
String path = Configuration.ROOT_PATH + "RQ3_3/LiveStudy/BugsInfo/";
File[] projects = new File(path).listFiles();
Map<String, Integer> map = new HashMap<>();
int i = 0;
for (File project : projects) {
if (project.isDirectory()) {
File[] types = project.listFiles();
List<String> top50TypesCopy = new ArrayList<>();
top50TypesCopy.addAll(top50Types);
for (File type :types) {
if (type.isDirectory()) {
String typeStr = type.getName();
top50TypesCopy.remove(typeStr);
}
}
System.out.println(project.getName() + "::");
for (String type : top50TypesCopy) {
System.out.println(type);
if (map.containsKey(type)) {
map.put(type, map.get(type) + 1);
} else {
map.put(type, 1);
}
}
i ++;
if (i == 10) break;
}
}
MapSorter<String, Integer> sorter = new MapSorter<>();
map = sorter.sortByValueDescending(map);
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + "==" + entry.getValue());
}
}
private static List<String> readTypes() throws IOException {
List<String> types = new ArrayList<>();
String content = FileHelper.readFile(Configuration.ROOT_PATH + "RQ1/Quantity-per-Fixed_1.0V-Type.csv");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
int i = 0;
while ((line = reader.readLine()) != null) {
String type = line.substring(0, line.indexOf(","));
types.add(type);
i ++;
if (i == 51) break;
}
return types;
}
}
@@ -1,55 +0,0 @@
package edu.lu.uni.serval.statistics;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.utils.FileHelper;
public class UnparsedFiles {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("../FPM_Violations/OAR.FPM.4221129.stderr");
Scanner scanner = new Scanner(fis);
List<Violation> vList = new ArrayList<>();
String test1 = "";
String test2 = "";
String test3 = "";
int i = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] elements = line.split(":");
Violation v = new Violation(Integer.parseInt(elements[2]), Integer.parseInt(elements[3]), elements[4]);
v.setFileName(elements[1]);
if (vList.contains(v)) {
i ++;
// System.out.println(elements[0]);
} else {
vList.add(v);
}
if (line.startsWith("#TestViolation:")) {
// System.out.println(line);
if (elements[1].toLowerCase().contains("/test/")) {
test1 += elements[1] + "\n";
} else if (elements[1].toLowerCase().contains("tests/")) {
test2 += elements[1] + "\n";
} else {
test3 += elements[1] + "\n";
}
}
}
scanner.close();
fis.close();
System.out.println(vList.size());
System.out.println(i);
FileHelper.outputToFile("logs/test1.txt", test1, false);
FileHelper.outputToFile("logs/test2.txt", test2, false);
FileHelper.outputToFile("logs/test3.txt", test3, false);
}
}
@@ -1,82 +0,0 @@
package edu.lu.uni.serval.violation;
import java.util.List;
import java.util.Map;
public class Alarm {
private String buggyCommitId;
private String buggyFileName;
private String fixedCommitId;
private String fixedFileName;
@Deprecated
private Map<Integer, String> alarmTypes;
@Deprecated
private Map<Integer, Integer> positions;
private List<String> alarmTypesAndPositions;
public Alarm(String buggyCommitId, String buggyFileName, String fixedCommitId, String fixedFileName) {
super();
this.buggyCommitId = buggyCommitId;
this.buggyFileName = buggyFileName;
this.fixedCommitId = fixedCommitId;
this.fixedFileName = fixedFileName;
}
public String getBuggyCommitId() {
return buggyCommitId;
}
public String getBuggyFileName() {
return buggyFileName;
}
public String getFixedCommitId() {
return fixedCommitId;
}
public String getFixedFileName() {
return fixedFileName;
}
@Deprecated
public Map<Integer, String> getAlarmTypes() {
return alarmTypes;
}
@Deprecated
public void setAlarmTypes(Map<Integer, String> alarmTypes) {
this.alarmTypes = alarmTypes;
}
@Deprecated
public Map<Integer, Integer> getPositions() {
return positions;
}
@Deprecated
public void setPositions(Map<Integer, Integer> positions) {
this.positions = positions;
}
public List<String> getAlarmTypesAndPositions() {
return alarmTypesAndPositions;
}
public void setAlarmTypesAndPositions(List<String> alarmTypesAndPositions) {
this.alarmTypesAndPositions = alarmTypesAndPositions;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Alarm) {
Alarm alarm = (Alarm) obj;
if (alarm.buggyCommitId.equals(this.buggyCommitId) && alarm.buggyFileName.equals(this.buggyFileName)
&& alarm.fixedCommitId.equals(this.fixedCommitId) && alarm.fixedFileName.equals(this.fixedFileName)) {
return true;
}
}
return false;
}
}
@@ -1,31 +0,0 @@
package edu.lu.uni.serval.violation;
import java.util.ArrayList;
import java.util.List;
/**
* Violation: all alarms identified by FindBugs in one java project.
*
* @author kui.liu
*
*/
public class Violation {
private String project;
private List<Alarm> alarms;
public Violation(String project) {
this.project = project;
this.alarms = new ArrayList<>();
}
public String getProject() {
return project;
}
public List<Alarm> getAlarms() {
return alarms;
}
}
@@ -1,223 +0,0 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.diffentry.DiffEntryHunk;
import edu.lu.uni.serval.diffentry.DiffEntryReader;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.ListSorter;
public class Test {
public static void main(String[] args) throws IOException {
// testV1();
// testV2();
// testV3("../FPM_Violations/OAR.FPM.4225346.stderr", "OUTPUT/unparsedviolations.txt");
// testV4("../FPM_Violations/OAR.FPM.4222208.stderr");
}
public static void testV4(String inputFile) throws IOException {
FileInputStream fis = new FileInputStream(inputFile);
Scanner scanner = new Scanner(fis);
StringBuilder builder = new StringBuilder();
// boolean isDiff = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("## OAR")) break;
// if (line.startsWith("#NullDiffEntry")) {
// isDiff = true;
// continue;
// } else if (line.startsWith("#")) {
// isDiff = false;
// continue;
// }
// if (!isDiff) continue;
String[] elements = line.trim().split(":");
String type = elements[0];
if (type.equals("#NullDiffEntry")) {
List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks2(new File("/Users/kui.liu/Public/git/FPM_Violations/GumTreeInput/diffentries/" + elements[1].replace(".java", ".txt").trim()));
int startLine = Integer.parseInt(elements[2].trim());
int endLine = Integer.parseInt(elements[3].trim());
for (DiffEntryHunk hunk : diffentryHunks) {
int hunkStart = hunk.getBugLineStartNum();
int hunkEnd = hunk.getBugRange() + hunkStart - 1;
if (startLine <= hunkEnd && hunkStart <= endLine) {
builder.append("vi " + elements[1].replace(".java", ".txt") + "\n : " + elements[2] + " : " + elements[3] + " : " + elements[4] + "\n");
}
}
}
// List<DiffEntryHunk> diffentryHunks = new DiffEntryReader().readHunks2(new File("/Users/kui.liu/Public/git/FPM_Violations/GumTreeInput/diffentries/" + elements[0].replace(".java", ".txt").trim()));
// int startLine = Integer.parseInt(elements[1].trim());
// int endLine = Integer.parseInt(elements[2].trim());
// for (DiffEntryHunk hunk : diffentryHunks) {
// int hunkStart = hunk.getBugLineStartNum();
// int hunkEnd = hunk.getBugRange() + hunkStart - 1;
//
// if (startLine <= hunkEnd && hunkStart <= endLine) {
// builder.append("vi " + elements[0].replace(".java", ".txt") + "\n : " + elements[1] + " : " + elements[2] + "\n"); //+ " : " + elements[4]
// }
// }
}
scanner.close();
fis.close();
FileHelper.outputToFile("Dataset/a.txt", builder, false);
}
public static void testV3(String inputFile, String outputFile) throws IOException {
FileInputStream fis = new FileInputStream(inputFile);
Scanner scanner = new Scanner(fis);
Map<String, List<String>> types = new HashMap<>();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("## OAR")) break;
String[] elements = line.split(":");
String type = elements[0];
if (types.containsKey(type)) {
String info = elements[1] + " : " + elements[2] + " : " + elements[3] + " : " + elements[4];
if (!types.get(type).contains(info)) {
types.get(type).add(info);
}
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + " : " + elements[2] + " : " + elements[3] + " : " + elements[4]);
types.put(type, files);
}
}
scanner.close();
fis.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> entry : types.entrySet()) {
System.out.println(entry.getKey());
if (entry.getKey().equals("#NoStatementChange")) {
// --- /dev/null
builder.append(entry.getKey() + "\n");
List<String> files = entry.getValue();
for (String file : files) {
builder.append(" " + file + "\n");
String fileName = Configuration.GUM_TREE_INPUT + "diffentries/" + file.substring(0, file.indexOf(".java")) + ".txt";
String content = FileHelper.readFile(fileName);
if (!content.contains("--- /dev/null")) {
System.out.println(file.substring(0, file.indexOf(".java")) + ".txt");
}
}
}
}
FileHelper.outputToFile(outputFile, builder, false);
}
public static void testV2() throws IOException {
String content = FileHelper.readFile("logs/testV2.txt");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
Map<String, List<String>> types = new HashMap<>();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(" : ");
String type = elements[0];
if (types.containsKey(type)) {
types.get(type).add(elements[1] + " : " + elements[2] + " : " + elements[3]);
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + " : " + elements[2] + " : " + elements[3]);
types.put(type, files);
}
}
reader.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, List<String>> entry : types.entrySet()) {
System.out.println(entry.getKey());
builder.append(entry.getKey() + "\n");
List<String> files = entry.getValue();
for (String file : files) {
builder.append(file + "\n");
}
}
FileHelper.outputToFile("logs/MultiV.txt", builder, false);
}
public static void testV1() throws IOException {
Map<String, Map<String, List<String>>> map = new HashMap<>();
String content = FileHelper.readFile("OUTPUT/list-1.txt");
BufferedReader reader = new BufferedReader(new StringReader(content));
String line = null;
List<String> types1 = new ArrayList<>();
List<String> types2 = new ArrayList<>();
while ((line = reader.readLine()) != null) {
String[] elements = line.split(":");
String type = elements[0];
String startLine = elements[2];
if (!"-1".equals(startLine)) {
startLine = "1";
if (!types1.contains(type)) {
types1.add(type);
}
} else {
if (!types2.contains(type)) {
types2.add(type);
}
}
if (map.containsKey(startLine)) {
Map<String, List<String>> types = map.get(startLine);
if (types.containsKey(type)) {
List<String> files = types.get(type);
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
} else {
List<String> files = new ArrayList<>();
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
types.put(type, files);
}
} else {
Map<String, List<String>> types = new HashMap<>();
List<String> files = new ArrayList<>();
files.add(elements[1] + "\n" + elements[2] + " : " + elements[3]);
types.put(type, files);
map.put(startLine, types);
}
}
reader.close();
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Map<String, List<String>>> entry : map.entrySet()) {
builder.append(entry.getKey() + "\n");
List<String> tList = null;
if (entry.getKey().equals("1")) {
tList = types1;
} else {
tList = types2;
}
ListSorter<String> sorter = new ListSorter<String>(tList);
tList = sorter.sortAscending();
Map<String, List<String>> types = entry.getValue();
for (String type : tList) {
builder.append(" " + type + "\n");
List<String> files = types.get(type);
for (String file : files) {
builder.append(file + "\n");
}
}
}
FileHelper.outputToFile("OUTPUT/NullV.txt", builder, false);
}
}
@@ -1,138 +0,0 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.lu.uni.serval.MultipleThreadsParser.MessageFile;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
/**
* Read violations' positions.
*
* Class name part,
* Field declaration,
* Initializer,
* Method name,
* Method body,
* Inner class,
* Anonymous class.
*
* @author kui.liu
*
*/
public class ViolatePositionReader {
/*
0 : 1280
-1 : 15
1 : 505 AnonymousClassDeclaration
23 : 7108 FieldDeclaration
71 : 310 EnumDeclaration
55 : 640 TypeDeclaration
72 : 87 EnumConstantDeclaration
28 : 1022 Initializer
31 : 77960 MethodDeclaration
*/
public static void main(String[] args) throws NumberFormatException, IOException {
List<MessageFile> msgFiles = getMessageFiles(Configuration.GUM_TREE_INPUT);
Map<Integer, Integer> map1 = new HashMap<>();
Map<String, Map<Integer, Integer>> map2 = new HashMap<>();
StringBuilder zeorBuilder = new StringBuilder();
StringBuilder minusOneBuilder = new StringBuilder();
for (MessageFile file : msgFiles) {
// if (!file.getPrevFile().getName().equals(
//"prev_ovgu-ccd-jchess_c3f4a0_bdca7bjchess#util#PixelCoordinateNotOnBoardException.java")) continue;
String fileContent = FileHelper.readFile(file.getPositionFile());
BufferedReader reader = null;
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
while ((line = reader.readLine()) != null) {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
String violationType = positionStr[0];
int range;
if (startLine != -1) {
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(file.getPrevFile(), startLine, endLine);
alarmTree.locateParentNode();
range = alarmTree.getViolationFinalStartLine();
} else {
range = 0;
}
if (map1.containsKey(range)) {
map1.put(range, map1.get(range) + 1);
} else {
map1.put(range, 1);
}
if (map2.containsKey(violationType)) {
Map<Integer, Integer> map = map2.get(violationType);
if (map.containsKey(range)) {
map.put(range, map.get(range) + 1);
} else {
map.put(range, 1);
}
} else {
Map<Integer, Integer> map = new HashMap<>();
map.put(range, 1);
map2.put(violationType, map);
}
if (range == 0) {
zeorBuilder.append(line + ":" + file.getPrevFile().getName() + "\n");
} else if (range == -1) {
minusOneBuilder.append(line + ":" + file.getPrevFile().getName() + "\n");
}
}
reader.close();
}
for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
FileHelper.outputToFile("Dataset/0.list", zeorBuilder, false);
FileHelper.outputToFile("Dataset/1.list", minusOneBuilder, false);
}
/**
* Get violation-related files.
*
* @param gumTreeInput
* @return
*/
public static List<MessageFile> getMessageFiles(String gumTreeInput) {
String inputPath = gumTreeInput; // prevFiles revFiles diffentryFile positionsFile
File revFilesPath = new File(inputPath + "revFiles/");
File[] revFiles = revFilesPath.listFiles(); // project folders
List<MessageFile> msgFiles = new ArrayList<>();
for (File revFile : revFiles) {
if (revFile.getName().endsWith(".java")) {
String fileName = revFile.getName();
File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file
fileName = fileName.replace(".java", ".txt");
File positionFile = new File(gumTreeInput + "positions/" + fileName); // position file
MessageFile msgFile = new MessageFile(null, prevFile, null);
msgFile.setPositionFile(positionFile);
msgFiles.add(msgFile);
}
}
return msgFiles;
}
}
@@ -1,282 +0,0 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPatternParser.Tokenizer;
import edu.lu.uni.serval.FixPatternParser.violations.Violation;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.gumtree.regroup.SimpleTree;
import edu.lu.uni.serval.gumtree.regroup.SimplifyTree;
import edu.lu.uni.serval.utils.FileHelper;
public class ViolationCodeParser {
private static Logger log = LoggerFactory.getLogger(ViolationCodeParser.class);
public static void main(String[] args) {
/*
* Fixed violations:
* Violation code files.
* Position files.
*/
// String fixedViolationFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
// String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
// int subIndex = 5;
// String outputPath = Configuration.ROOT_PATH + "Alarms_tokens/fixedAlarms.list";
// FileHelper.deleteFile(outputPath);
// new ViolationCodeParser().parse(fixedViolationFilesPath, positionsFilePath, subIndex, outputPath);
/*
* UnFixed violations:
* Violation code files.
* Position files.
*/
String unfixedViolationFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
String un_positionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
int subIndex2 = 8;
String outputPath2 = Configuration.ROOT_PATH + "Alarms_tokens/unfixedAlarms.list";
FileHelper.deleteFile(outputPath2);
new ViolationCodeParser().parse(unfixedViolationFilesPath, un_positionsFilePath, subIndex2, outputPath2);
}
public void parse(String alarmFilesPath, String positionFilesPath, int subIndex, String outputPath) {
StringBuilder tokensBuilder = new StringBuilder();
List<File> javaFiles = FileHelper.getAllFilesInCurrentDiectory(alarmFilesPath, ".java");
int counter = 0;
int a = 0;
int maxLength = 0;
for (File javaFile : javaFiles) {
String fileName = javaFile.getName().replace(".java", ".txt");
fileName = fileName.substring(subIndex);
List<Violation> violations = readViolationInfo(positionFilesPath + fileName);
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
String alarmType = violation.getViolationType();
// if (endLine > startLine + 5) {
// log.warn("#Large_Violation_Hunk: " + fileName.replace("#", "/").replace(".txt", ".java") + ":" + startLine + ":" + endLine + ":" + alarmType);
// continue;
// }
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract();
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
System.out.println(fileName + " == " + startLine + " : " + endLine);
a ++;
log.warn("#Null_Violation_Hunk: " + fileName.replace("#", "/").replace(".txt", ".java") + ":" + startLine + ":" + endLine + ":" + alarmType);
continue;
}
SimpleTree simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, null);
children.add(simpleT);
}
simpleTree.setChildren(children);
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
if (length > maxLength) maxLength = length;
tokensBuilder.append(alarmType + ":" + fileName + ":" + alarmTree.getViolationFinalStartLine() + ":" + alarmTree.getViolationFinalEndLine() + ":" + tokens + "\n");
counter ++;
if (counter % 5000 == 0) {
FileHelper.outputToFile(outputPath, tokensBuilder, true);
tokensBuilder.setLength(0);
}
}
}
System.out.println(counter);
System.out.println(a);
System.out.println("MaxLength: " + maxLength);
FileHelper.outputToFile(outputPath, tokensBuilder, true);
tokensBuilder.setLength(0);
}
public String sourceCode = "";
public String tokens = "";
public String sizes = "";
public void parse(File javaFile, File positionFile) {
List<Violation> violations = readViolationInfo(positionFile);
for (Violation violation : violations) {
int startLine = violation.getStartLineNum();
int endLine = violation.getEndLineNum();
String type = violation.getViolationType();
// if (violationTypes != null && violationTypes.contains(type)) {
//// if (endLine > startLine + 5) {
////// log.warn("#Large_Violation_Hunk: " + javaFile.getName() + ":" + startLine + ":" + endLine + ":" + alarmType);
//// continue;
//// }
// }
SimpleTree simpleTree = null;
if ("EQ_DOESNT_OVERRIDE_EQUALS".equals(type)|| "HE_EQUALS_USE_HASHCODE".equals(type) || "HE_INHERITS_EQUALS_USE_HASHCODE".equals(type)||
"SE_NO_SUITABLE_CONSTRUCTOR".equals(type) || "RI_REDUNDANT_INTERFACES".equals(type)
||"CN_IDIOM".equals(type)||"SE_NO_SERIALVERSIONID".equals(type) || "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(type)) {
ViolationSourceCodeTree parser = new ViolationSourceCodeTree(javaFile, violation.getStartLineNum(), violation.getEndLineNum());
ITree classNameTree = parser.getClassNameTokens();
simpleTree = new SimplifyTree().canonicalizeSourceCodeTree(classNameTree, null);
startLine = parser.getViolationFinalStartLine();
endLine = parser.getViolationFinalEndLine();
} else {
ViolationSourceCodeTree alarmTree = new ViolationSourceCodeTree(javaFile, startLine, endLine);
alarmTree.extract();
List<ITree> matchedTrees = alarmTree.getViolationSourceCodeTrees();
if (matchedTrees.size() == 0) {
System.err.println("#Null_Violation_Hunk: " + javaFile.getName() + ":" + startLine + ":" + endLine);
continue;
}
simpleTree = new SimpleTree();
simpleTree.setLabel("Block");
simpleTree.setNodeType("Block");
List<SimpleTree> children = new ArrayList<>();
for (ITree matchedTree : matchedTrees) {
SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, null);
children.add(simpleT);
}
simpleTree.setChildren(children);
startLine = alarmTree.getViolationFinalStartLine();
endLine = alarmTree.getViolationFinalEndLine();
}
String tokens = Tokenizer.getTokensDeepFirst(simpleTree);
String[] tokensArray = tokens.split(" ");
int length = tokensArray.length;
sizes += length + "\n";
this.tokens += tokens + "\n";
String sourceCode = readSourceCode(javaFile, startLine, endLine, violation.getViolationType());
this.sourceCode += sourceCode + "\n";
}
}
private String readSourceCode(File javaFile, int startLine, int endLine, String violationType) {
StringBuilder sourceCode = new StringBuilder("##Source_Code:\n");
sourceCode.append(violationType).append("\n");
sourceCode.append(javaFile.getName().replaceAll("#", "/")).append("\nPosition: ").append(startLine).append(" : ").append(endLine).append("\n");
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(javaFile);
scanner = new Scanner(fis);
int counter = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
counter ++;
if (startLine <= counter && counter <= endLine) {
sourceCode.append(line + "\n");
}
if (counter == endLine) break;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sourceCode.toString();
}
private List<Violation> readViolationInfo(String file) {
List<Violation> violations = new ArrayList<>();
String fileContent = FileHelper.readFile(file);
BufferedReader reader = null;
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
String alarmType = positionStr[0];
if (startLine == -1 || endLine == -1) {
log.warn("#Illegal_Line_Position: " + FileHelper.getFileName(file).replace("#", "/").replace(".txt", ".java") + ":" + startLine + ":" + endLine + ":" + alarmType);
continue;
}
Violation violation = new Violation(startLine, endLine, alarmType);
violations.add(violation);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
private List<Violation> readViolationInfo(File file) {
List<Violation> violations = new ArrayList<>();
String fileContent = FileHelper.readFile(file);
BufferedReader reader = null;
reader = new BufferedReader(new StringReader(fileContent));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] positionStr = line.split(":");
int startLine = Integer.parseInt(positionStr[1]);
int endLine = Integer.parseInt(positionStr[2]);
String alarmType = positionStr[0];
if (startLine == -1 || endLine == -1) {
continue;
}
Violation violation = new Violation(startLine, endLine, alarmType);
violations.add(violation);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return violations;
}
public void setTypes(List<String> violationTypes) {
this.violationTypes = violationTypes;
}
private List<String> violationTypes = null;
}
@@ -1,785 +0,0 @@
package edu.lu.uni.serval.violation.code.parser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.FixPattern.utils.Checker;
import edu.lu.uni.serval.FixPatternParser.CUCreator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator;
import edu.lu.uni.serval.gumtree.GumTreeGenerator.GumTreeType;
public class ViolationSourceCodeTree {
private File file;
private int violationStartLine;
private int violationEndLine;
private CompilationUnit cUnit;
private int violationFinalStartLine = 0;
private int violationFinalEndLine;
private List<ITree> matchedTrees = new ArrayList<>();
public ViolationSourceCodeTree(File file, int violationStartLine, int violationEndLine) {
super();
this.file = file;
this.violationStartLine = violationStartLine;
this.violationEndLine = violationEndLine;
CUCreator cuCreator = new CUCreator();
this.cUnit = cuCreator.createCompilationUnit(this.file);
}
public ViolationSourceCodeTree(String fileName, int violationStartLine, int violationEndLine) {
this(new File(fileName), violationStartLine, violationEndLine);
}
public List<ITree> getViolationSourceCodeTrees() {
return this.matchedTrees;
}
public int getViolationFinalStartLine() {
return violationFinalStartLine;
}
public int getViolationFinalEndLine() {
return violationFinalEndLine;
}
/**
* extract source code of violations in method body or field body.
*/
public void extract() {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
// FIXME the violation occurred in the Class Name
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.violationFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.violationFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine);
}
}
public void extract(String type) {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
/*
violationT ||
violationType.equals("UC_USELESS_CONDITION")
*/
if (type.equals("SE_NO_SERIALVERSIONID")) {
}
// FIXME the violation occurred in the Class Name
matchTrees(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
this.violationFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.violationFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
} else {
System.err.println(type + " : " + this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine);
}
}
private void matchTrees(List<ITree> trees) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) continue;
if (endLine == violationEndLine) {
if (tree.getType() == 31) { // MethodDeclaration
matchTrees(tree.getChildren());
} else if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) { // 8: Block
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
continue;
}
if (startLine >= violationStartLine) {
if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement(tree);
if (parent == null) {
if (tree.getType() == 8) {
matchTrees(tree.getChildren());
}
continue;
}
addToMatchedTrees(parent);
}
} else {
// if (tree.getType() == 14) {
// ITree parent = getParentStatement(tree);
// if (parent == null) {
// matchTrees(tree.getChildren());
// } else {
// addToMatchedTrees(parent);
// }
// } else {
// matchTrees(tree.getChildren());
// }
matchTrees(tree.getChildren());
}
}
}
private void addToMatchedTrees(ITree tree) {
if (!matchedTrees.contains(tree)) {
/*
* TODO with the same parent, or the sub trees.
* In the same method body.
*/
matchedTrees.add(tree);
}
int type = tree.getType();
if (type == 8 || type == 12 || Checker.withBlockStatement(type)) {
int endLine = cUnit.getLineNumber(tree.getPos() + tree.getLength());
if (endLine > violationEndLine) {
tree = removeBlock(tree);
}
}
}
private ITree removeBlock(ITree tree) {
List<ITree> oldChildren = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (ITree child : oldChildren) {
int startPosition = child.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + child.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine > this.violationEndLine) {
int type = child.getType();
if (type == 8 || type == 12 || Checker.withBlockStatement(type)) { // 8: Block, CatchClause
child = removeBlock(child);
if (child.getChildren().size() == 0) {
continue;
}
}
}
newChildren.add(child);
}
tree.setChildren(newChildren);
return tree;
}
private ITree getParentStatement(ITree tree) {
ITree parent = tree;
int type = parent.getType();
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
type = parent.getType();
if (type == 1 || type == 31 || type == 55 || type == 71) {
// AnonymousClassDeclaration
// MethodDeclaration Initializer (type == 28)
// TypeDeclaration
// EnumDeclaration
return null;
}
} while (!isStatement(parent));
return parent;
}
private boolean isStatement(ITree tree) {
int type = tree.getType(); // 8 Block
if (type == 12) return true; // catchClause
if (type == 23) return true; // FieldDeclaration
if (Checker.isStatement(type)) return true;
return false;
}
public void locateParentNode(String type) {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
if (startLine == this.violationStartLine || endLine == this.violationEndLine) {
this.violationFinalStartLine = startLine;
this.violationFinalEndLine = endLine;
}
locateParentNode(tree.getChildren(), type);
}
}
private void locateParentNode(List<ITree> trees, String type) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) {
continue;
}
if (endLine < violationEndLine) {
if ("NM_SAME_SIMPLE_NAME_AS_INTERFACE".equals(type) || "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS".equals(type) || "NM_CLASS_NAMING_CONVENTION".equals(type)
|| "NM_CLASS_NOT_EXCEPTION".equals(type) || "RI_REDUNDANT_INTERFACES".equals(type)
// inner class
|| "SE_INNER_CLASS".equals(type) || "SE_BAD_FIELD_INNER_CLASS".equals(type) || "SIC_INNER_SHOULD_BE_STATIC_ANON".equals(type)
|| "SIC_INNER_SHOULD_BE_STATIC".equals(type) || "SIC_INNER_SHOULD_BE_STATIC_NEEDS_THIS".equals(type)) {
if (tree.getType() != 55) {
ITree parent = getParentTypeDeclaration(tree);
if (parent == null) {
this.violationFinalStartLine = -1;
break;
}
tree = parent;
}
startPosition = tree.getPos();
this.violationFinalStartLine = cUnit.getLineNumber(startPosition);
endPosition = getClassBodyStartPosition(tree);
if (endPosition == 0) {
endPosition = startPosition + tree.getLength();
}
this.violationFinalEndLine = cUnit.getLineNumber(endPosition);
} else if ("NM_METHOD_NAMING_CONVENTION".equals(type)){
// method name level
if (tree.getType() != 31) {
ITree parent = getParentMethodDeclaration(tree);
if (parent == null) {
this.violationFinalStartLine = -1;
break;
}
tree = parent;
}
startPosition = tree.getPos();
this.violationFinalStartLine = cUnit.getLineNumber(startPosition);
endPosition = getMethodBodyStartPosition(tree);
if (endPosition == 0) {
endPosition = startPosition + tree.getLength();
}
this.violationFinalEndLine = cUnit.getLineNumber(endPosition);
// } else if ("SE_NO_SUITABLE_CONSTRUCTOR".equals(type) || "CN_IDIOM".equals(type)
// || "SE_NO_SERIALVERSIONID".equals(type) || "SE_NO_SUITABLE_CONSTRUCTOR_FOR_EXTERNALIZATION".equals(type)
// || "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE".equals(type)) {
// this.violationFinalStartLine = -1;
} else {
this.violationFinalStartLine = -1;
}
// FileHelper.outputToFile("logs/testV2.txt", type + " : " + this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine + "\n", true);
break;
}
int astNodeType = tree.getType();
// Class Name(super class etc.), Field, initializer, EnumDeclaration, EnumConstantDeclaration, Method Name, method body,
if (astNodeType == 31 || astNodeType == 23 || astNodeType == 28 || astNodeType == 71 || astNodeType == 72
|| astNodeType == 1 || astNodeType == 55) {// inner class
// MethodDeclaration, FieldDeclaration, Initializer, EnumDeclaration, EnumConstantDeclaration, AnonymousClassDeclaration
this.violationFinalStartLine = startLine;
this.violationFinalEndLine = endLine;
break;
} else {
locateParentNode(tree.getChildren(), type);
}
}
}
private int getMethodBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
if (Checker.isStatement2(type)) {
if ( i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength();
} else {
return child.getPos() - 1;
}
}
}
return 0;
}
private ITree getParentMethodDeclaration(ITree tree) {
ITree parent = tree;
int type = 0;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
type = parent.getType();
} while (type != 31);
return parent;
}
private int getClassBodyStartPosition(ITree tree) {
List<ITree> children = tree.getChildren();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88) {
// ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType
if (type == 42 && child.getLabel().startsWith("ClassName:")) {
continue;
}
if (i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength() + 1;
} else {
return child.getPos() - 1;
}
}
}
return 0;
}
private ITree getParentTypeDeclaration(ITree tree) {
ITree parent = tree;
int type = 0;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
type = parent.getType();
} while (type != 55);
return parent;
}
public void locateParentNode() {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
int type = tree.getType();
switch (type) {
case 55:
this.classNamePosition = 1;
break;
case 71:
this.enumPosition = 1;
break;
case 72:
this.enumConsP = 1;
break;
default :
break;
}
locateParentNode(tree.getChildren());
}
// if (this.violationFinalStartLine == 0) {
// FileHelper.outputToFile("logs/testV1.txt", type + " : " + this.file.getName() + " : " + this.violationStartLine + " : " + this.violationEndLine + "\n", true);
// }
}
int methodPosition = 0;
int classNamePosition = 0;
int fieldPosition = 0;
int initializerP = 0;
int anonymousPosition = 0;
int enumPosition = 0;
int enumConsP = 0;
private void locateParentNode(List<ITree> trees) {
boolean isBigRange = false;
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) {
continue;
}
int type = tree.getType();
if (isBigRange) {
if (type == 31 || type == 55 || type == 23 || type == 28 || type == 71 || type == 72
|| type == 81 || type == 82|| type == 1) {
this.violationFinalStartLine = -1;
break;
}
}
if (endLine < violationEndLine) {
if (this.violationFinalStartLine == 0) {
this.violationFinalStartLine = type;
isBigRange = true;
continue;
}
}
// Class Name(super class etc.), Method Name,
if (type == 31) { //MethodDeclaration
this.violationFinalStartLine = type;
this.methodPosition = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 55) {// TypeDeclaration, inner class
this.violationFinalStartLine = type;
this.classNamePosition = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 1) {// AnonymousClassDeclaration
this.violationFinalStartLine = type;
this.anonymousPosition = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 23) {// FieldDeclaration
this.violationFinalStartLine = type;
this.fieldPosition = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 28) {// Initializer
this.violationFinalStartLine = type;
this.initializerP = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 71) {// EnumDeclaration
this.violationFinalStartLine = type;
this.enumPosition = 1;
locateParentNode(tree.getChildren());
break;
} else if (type == 72) {// EnumConstantDeclaration
this.violationFinalStartLine = tree.getType();
this.enumConsP = 1;
locateParentNode(tree.getChildren());
break;
} else {
locateParentNode(tree.getChildren());
}
}
}
/**
* extract source code of violations.
*/
public void extract2() {
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
matchTrees2(tree.getChildren());
}
int size = matchedTrees.size();
if (size > 0) {
if (this.violationFinalStartLine != 0) {
this.violationFinalStartLine = cUnit.getLineNumber(this.matchedTrees.get(0).getPos());
ITree lastTree = matchedTrees.get(size - 1);
this.violationFinalEndLine = cUnit.getLineNumber(lastTree.getPos() + lastTree.getLength());
}
} else {
System.err.println(this.file.getName() + "===" + this.violationStartLine + ":" + this.violationEndLine);
}
}
private void matchTrees2(List<ITree> trees) {
for (ITree tree : trees) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationEndLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition);
if (endLine < violationStartLine) continue;
if (endLine == violationEndLine) {
if (tree.getType() == 31) { // MethodDeclaration
if (startLine == violationStartLine) {
int finalEndPosition = getMethodDeclarationWithoutBody(tree);
addToMatchedTrees(tree);
this.violationFinalStartLine = cUnit.getLineNumber(tree.getPos());
this.violationFinalEndLine = cUnit.getLineNumber(finalEndPosition);
} else {
matchTrees(tree.getChildren());
}
} else if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement2(tree);
if (parent == null) {
if (tree.getType() == 8) { // 8: Block
matchTrees(tree.getChildren());
}
continue;
} else if (parent.getType() == 31) { // method name
if (startLine == violationStartLine || endLine < violationEndLine) {
int finalEndPosition = getMethodDeclarationWithoutBody(parent);
addToMatchedTrees(parent);
this.violationFinalStartLine = cUnit.getLineNumber(parent.getPos());
this.violationFinalEndLine = cUnit.getLineNumber(finalEndPosition);
}
break;
} else if (parent.getType() == 55) { // class name
if (startLine == violationStartLine || endLine < violationEndLine) {
int finalEndPosition = getClassDecalrationWithoutBody(parent);
addToMatchedTrees(parent);
this.violationFinalStartLine = cUnit.getLineNumber(parent.getPos());
this.violationFinalEndLine = cUnit.getLineNumber(finalEndPosition);
}
break;
}
addToMatchedTrees(parent);
}
continue;
}
if (startLine >= violationStartLine) {
if (isStatement(tree)) {
addToMatchedTrees(tree);
} else {
ITree parent = getParentStatement2(tree);
if (parent == null) {
if (tree.getType() == 8) {
matchTrees(tree.getChildren());
}
continue;
} else if (parent.getType() == 31) { // method name
if (startLine == violationStartLine || endLine < violationEndLine) {
int finalEndPosition = getMethodDeclarationWithoutBody(parent);
addToMatchedTrees(parent);
this.violationFinalStartLine = cUnit.getLineNumber(parent.getPos());
this.violationFinalEndLine = cUnit.getLineNumber(finalEndPosition);
}
break;
} else if (parent.getType() == 55) { // class name
if (startLine == violationStartLine || endLine < violationEndLine) {
int finalEndPosition = getClassDecalrationWithoutBody(parent);
addToMatchedTrees(parent);
this.violationFinalStartLine = cUnit.getLineNumber(parent.getPos());
this.violationFinalEndLine = cUnit.getLineNumber(finalEndPosition);
}
break;
}
addToMatchedTrees(parent);
}
} else {
matchTrees(tree.getChildren());
}
}
}
private int getMethodDeclarationWithoutBody(ITree tree) {
List<ITree> children = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
if (Checker.isStatement2(type)) {
if (i > 0) {
child = children.get(i - 1);
return child.getPos() + child.getLength() + 1;
} else {
return child.getPos() - 1;
}
} else {
newChildren.add(child);
}
}
tree.setChildren(newChildren);
return 0;
}
private int getClassDecalrationWithoutBody(ITree tree) {
List<ITree> children = tree.getChildren();
List<ITree> newChildren = new ArrayList<>();
for (int i = 0, size = children.size(); i < size; i ++) {
ITree child = children.get(i);
int type = child.getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88) {
// ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType
if (type == 42 && child.getLabel().startsWith("ClassName:")) {
newChildren.add(child);
continue;
}
if (i > 0) {
child = children.get(i - 1);
tree.setChildren(newChildren);
return child.getPos() + child.getLength() + 1;
} else {
tree.setChildren(newChildren);
return child.getPos() - 1;
}
} else {
newChildren.add(child);
}
}
tree.setChildren(newChildren);
return 0;
}
private ITree getParentStatement2(ITree tree) {
ITree parent = tree;
do {
parent = parent.getParent();
if (parent == null) {
return null;
}
int type = parent.getType();
if (type == 31 || type == 55) {
// MethodDeclaration
// TypeDeclaration
return parent;
} else if (type == 1 || type == 71) {
// AnonymousClassDeclaration
// EnumDeclaration
return null;
}
} while (!isStatement(parent));
return parent;
}
public ITree getClassNameTokens() {
ITree classNameTree = null;
ITree rootTree = new GumTreeGenerator().generateITreeForJavaFile(file, GumTreeType.EXP_JDT);
List<ITree> trees = rootTree.getChildren();
for (ITree tree : trees) {
int type = tree.getType();
if (type == 55 || type == 71) {
classNameTree = tree;
ITree classNameTree2 = getClassNameTokens(tree.getChildren());
if (classNameTree2 != null) {
classNameTree = classNameTree2;
}
}
}
if (classNameTree != null) {
int finalEndPosition = getClassDecalrationWithoutBody(classNameTree);
int finalStartPosition = classNameTree.getPos();
this.violationFinalStartLine = this.cUnit.getLineNumber(finalStartPosition);
this.violationFinalEndLine = this.cUnit.getLineNumber(finalEndPosition);
}
return classNameTree;
}
private ITree getClassNameTokens(List<ITree> children) {
ITree classNameTree = null;
for (ITree tree : children) {
int startPosition = tree.getPos();
int startLine = cUnit.getLineNumber(startPosition);
if (startLine > violationStartLine) {
break;
}
int endPosition = startPosition + tree.getLength();
int endLine = cUnit.getLineNumber(endPosition - 1);
if (endLine < violationStartLine) continue;
int type = tree.getType();
if (type == 55 || type == 71) {
classNameTree = tree;
break;
}
}
return classNameTree;
}
}
@@ -1,197 +0,0 @@
package edu.lu.uni.serval.violation.parse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.lu.uni.serval.violation.Alarm;
import edu.lu.uni.serval.violation.Violation;
public class AlarmsReader {
@SuppressWarnings("unused")
private static Logger log = LoggerFactory.getLogger(AlarmsReader.class);
int counter = 0;
public Map<String, Violation> readAlarmsList(String fileName) {
// StringBuilder builder = new StringBuilder();
Map<String, Violation> violationsMap = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(fileName);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int arrowIndex = line.indexOf("=>");
String buggyInfo = line.substring(0, arrowIndex);
String fixedInfo = line.substring(arrowIndex + 2);
String[] buggyElements = buggyInfo.split(":");
String[] fixedElements = fixedInfo.split(":");
// String alarmType = buggyElements[0];
String projectName = buggyElements[1];
String buggyCommitId = buggyElements[2];
String buggyFile = buggyElements[3];
// int startLine = Integer.parseInt(buggyElements[4]);
// int endLine = Integer.parseInt(buggyElements[5]);
String fixCommitId = fixedElements[1];
String fixedFile = fixedElements[2];
// if (startLine == -1 || endLine == -1 || endLine == 1) {
//// log.error("FIXED ALARM WRONG_POSITION: " + line);
// builder.append(line + "\n");
// continue;
// }
Alarm alarm = new Alarm(buggyCommitId, buggyFile, fixCommitId, fixedFile);
// String alarmTypeAndPosition = buggyElements[0] + ":" + startLine + ":" + endLine;
String alarmTypeAndPosition = buggyElements[0] + ":" + buggyElements[4] + ":" + buggyElements[5];
Violation violation;
if (violationsMap.containsKey(projectName)) {
violation = violationsMap.get(projectName);
} else {
violation = new Violation(projectName);
violationsMap.put(projectName, violation);
}
List<Alarm> alarms = violation.getAlarms();
int index = alarms.indexOf(alarm);
if (index >= 0) {
Alarm tempAlarm = alarms.get(index);
// Map<Integer, Integer> positions = tempAlarm.getPositions();
// if (positions.containsKey(startLine)) {
// int end = positions.get(startLine);
// if (endLine < end) {
// positions.put(startLine, endLine);
// tempAlarm.getAlarmTypes().put(startLine, alarmType);
// }
// } else {
// positions.put(startLine, endLine);
// tempAlarm.getAlarmTypes().put(startLine, alarmType);
counter ++;
// }
tempAlarm.getAlarmTypesAndPositions().add(alarmTypeAndPosition);
} else {
// Map<Integer, String> alarmTypes = new HashMap<>();
// alarmTypes.put(startLine, alarmType);
// alarm.setAlarmTypes(alarmTypes);
// Map<Integer, Integer> positions = new HashMap<>();
// positions.put(startLine, endLine);
// alarm.setPositions(positions);
List<String> alarmTypesAndPositions = new ArrayList<>();
alarmTypesAndPositions.add(alarmTypeAndPosition);
alarm.setAlarmTypesAndPositions(alarmTypesAndPositions);
alarms.add(alarm);
counter ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(counter);
// FileHelper.outputToFile("../FPM_Violations/TuneParameters/WrongPosition/FixedAlarm.list", builder, false);
return violationsMap;
}
public Map<String, Violation> readAlarmsList2(String fileName) {
// StringBuilder builder = new StringBuilder();
Map<String, Violation> violationsMap = new HashMap<>();
FileInputStream fis = null;
Scanner scanner = null;
try {
fis = new FileInputStream(fileName);
scanner = new Scanner(fis);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// int arrowIndex = line.indexOf("=>");
// String alarmInfo = arrowIndex > 0 ? line.substring(0, arrowIndex) : line;
// String[] buggyElements = alarmInfo.split(",");
String[] buggyElements = line.split(",");
String projectName = buggyElements[1];
String buggyCommitId = buggyElements[2];
String buggyFile = buggyElements[3];
// if (startLine == -1 || endLine == -1 || endLine == 1) {
//// log.error("UNFIXED ALARM WRONG_POSITION: " + line);
// builder.append(line + "\n");
// continue;
// }
String alarmTypeAndPosition = buggyElements[0] + ":" + buggyElements[4] + ":" + buggyElements[5]; // Alarm type : start line : end line.
Alarm alarm = new Alarm(buggyCommitId, buggyFile, "", "");
Violation violation;
if (violationsMap.containsKey(projectName)) {
violation = violationsMap.get(projectName);
} else {
violation = new Violation(projectName);
violationsMap.put(projectName, violation);
}
List<Alarm> alarms = violation.getAlarms();
int index = alarms.indexOf(alarm);
if (index >= 0) {
Alarm tempAlarm = alarms.get(index);
// Map<Integer, Integer> positions = tempAlarm.getPositions();
// if (positions.containsKey(startLine)) {
// int end = positions.get(startLine);
// if (endLine < end) {
// positions.put(startLine, endLine);
// tempAlarm.getAlarmTypes().put(startLine, alarmType);
// }
// } else {
// positions.put(startLine, endLine);
// tempAlarm.getAlarmTypes().put(startLine, alarmType);
counter ++;
// }
tempAlarm.getAlarmTypesAndPositions().add(alarmTypeAndPosition);
} else {
// Map<Integer, String> alarmTypes = new HashMap<>();
// alarmTypes.put(startLine, alarmType);
// alarm.setAlarmTypes(alarmTypes);
// Map<Integer, Integer> positions = new HashMap<>();
// positions.put(startLine, endLine);
// alarm.setPositions(positions);
List<String> alarmTypesAndPositions = new ArrayList<>();
alarmTypesAndPositions.add(alarmTypeAndPosition);
alarm.setAlarmTypesAndPositions(alarmTypesAndPositions);
alarms.add(alarm);
counter ++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
scanner.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(counter);
// FileHelper.outputToFile("../FPM_Violations/TuneParameters/WrongPosition/UnFixedAlarm.list", builder, true);
return violationsMap;
}
}
@@ -1,83 +0,0 @@
package edu.lu.uni.serval.violation.parse;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class Test {
public static void main(String[] args) throws IOException {
// String filePath = Configuration.GUM_TREE_INPUT + "un_positions/";
// List<File> files = FileHelper.getAllFiles(filePath, ".txt");
// System.out.println(files.size());
// List<String> projects = new ArrayList<>();
// int unfixedI = 0;
// for (File file : files) {
// String fileName = file.getName();
// fileName = fileName.substring(0, fileName.indexOf("#"));
// fileName = fileName.substring(0, fileName.lastIndexOf("_"));
// fileName = fileName.substring(0, fileName.lastIndexOf("_"));
// if (!projects.contains(fileName)) {
// projects.add(fileName);
// }
// String content = FileHelper.readFile(file);
// BufferedReader reader = new BufferedReader(new StringReader(content));
// while (reader.readLine() != null) {
// unfixedI ++;
// }
// reader.close();
// }
//
// for (String str : projects) {
// System.out.println(str);
// }
// System.out.println("Unfixed: " + unfixedI);
String positionFath = Configuration.GUM_TREE_INPUT + "positions/";
List<File> files = FileHelper.getAllFilesInCurrentDiectory(positionFath, ".txt");
System.out.println("File Path: " + positionFath);
System.out.println(files.size());
int i = 0;
for (File file : files) {
String content = FileHelper.readFile(file);
BufferedReader reader = new BufferedReader(new StringReader(content));
while (reader.readLine() != null) {
i ++;
}
reader.close();
}
System.out.println("Fixed: " + i);
// String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
// List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
// int a = 0;
// for (File file : unfixedAlarmFiles) {
// String content = FileHelper.readFile(file);
// BufferedReader reader = new BufferedReader(new StringReader(content));
// while (reader.readLine() != null) {
// a ++;
// }
// reader.close();
// }
// System.out.println("unfixed: " + a);
String fixedAlarmFile = "Dataset/fixed-alarms-v1.0.list";
int b = 0;
String content = FileHelper.readFile(fixedAlarmFile);
BufferedReader reader = new BufferedReader(new StringReader(content));
while (reader.readLine() != null) {
b ++;
}
reader.close();
System.out.println(b);
}
}
@@ -1,94 +0,0 @@
package edu.lu.uni.serval.violation.parse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import edu.lu.uni.serval.config.Configuration;
import edu.lu.uni.serval.utils.FileHelper;
public class TestViolationParser {
private static final String REPO_PATH = "../../repos/";//"/Volumes/MacBook/repos/";
public static void main(String[] args) throws IOException {
List<File> repositoriesList = new ArrayList<>();
File repositories = new File(REPO_PATH);
File[] subFiles = repositories.listFiles();
for (File subFile : subFiles) {
if (subFile.isDirectory()) {
File[] repos = subFile.listFiles();
for (File repo : repos) {
if (repo.isDirectory()) {
repositoriesList.add(repo);
}
}
}
}
List<String> violationTypes = new ArrayList<>();
/*
* DM_DEFAULT_ENCODING
*/
violationTypes.add("NP_NONNULL_RETURN_VIOLATION");
violationTypes.add("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE");
violationTypes.add("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE");
violationTypes.add("ODR_OPEN_DATABASE_RESOURCE");
violationTypes.add("PZLA_PREFER_ZERO_LENGTH_ARRAYS");
violationTypes.add("RI_REDUNDANT_INTERFACES");
violationTypes.add("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE");
violationTypes.add("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT");
violationTypes.add("SE_NO_SERIALVERSIONID");
violationTypes.add("SF_SWITCH_NO_DEFAULT");
violationTypes.add("SIC_INNER_SHOULD_BE_STATIC");
violationTypes.add("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING");
violationTypes.add("UC_USELESS_CONDITION");
violationTypes.add("UC_USELESS_OBJECT");
violationTypes.add("UCF_USELESS_CONTROL_FLOW");
violationTypes.add("WMI_WRONG_MAP_ITERATOR");
// Violation instances of single violation type
String unfixedViolations = "../FPM_Violations/unFixedInstances/";
String unfixedFilesPath = Configuration.GUM_TREE_INPUT + "UnfixedViolations/";
String unfixedPositionsFilePath = Configuration.GUM_TREE_INPUT + "UnFV_positions/";
List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedViolations, ".list");
for (File file : unfixedAlarmFiles) {
String fileName = FileHelper.getFileNameWithoutExtension(file);
// if (!violationTypes.contains(fileName)) continue;
if (!fileName.equals("DM_DEFAULT_ENCODING")) continue;
FileHelper.createDirectory(unfixedFilesPath + fileName + "/");
FileHelper.createDirectory(unfixedPositionsFilePath + fileName + "/");
ViolationParser parser = new ViolationParser();
parser.parseViolations(file, repositoriesList, unfixedFilesPath + fileName + "/", unfixedPositionsFilePath + fileName + "/");
}
// String unfixedAlarmFile = "Dataset/Unfixed-Alarms/";
// final String unfixedFilesPath = Configuration.GUM_TREE_INPUT + "unfixAlarms/";
// final String unfixedOositionsFilePath = Configuration.GUM_TREE_INPUT + "un_positions/";
// FileHelper.createDirectory(unfixedFilesPath);
// FileHelper.createDirectory(unfixedOositionsFilePath);
//
// List<File> unfixedAlarmFiles = FileHelper.getAllFilesInCurrentDiectory(unfixedAlarmFile, ".csv");
//
// for (File file : unfixedAlarmFiles) {
// ViolationParser parser = new ViolationParser();
// parser.parseViolations(file, repositoriesList, unfixedFilesPath, unfixedOositionsFilePath);
// }
// String fixedAlarmFile = "Dataset/fixed-alarms-v1.0.list";
// final String previousFilesPath = Configuration.GUM_TREE_INPUT + "prevFiles/";
// final String revisedFilesPath = Configuration.GUM_TREE_INPUT + "revFiles/";
// final String positionsFilePath = Configuration.GUM_TREE_INPUT + "positions/";
// final String diffentryFilePath = Configuration.GUM_TREE_INPUT + "diffentries/";
// FileHelper.createDirectory(previousFilesPath);
// FileHelper.createDirectory(revisedFilesPath);
// FileHelper.createDirectory(positionsFilePath);
// FileHelper.createDirectory(diffentryFilePath);
//
// ViolationParser parser = new ViolationParser();
// parser.parseViolations(fixedAlarmFile, repositoriesList, previousFilesPath, revisedFilesPath, positionsFilePath, diffentryFilePath);
}
}

Some files were not shown because too many files have changed in this diff Show More