Remove useless actions.

This commit is contained in:
Kui LIU
2017-08-12 13:31:24 +02:00
parent 7d49abfb27
commit 9bff704965
4 changed files with 138 additions and 13 deletions
@@ -170,6 +170,68 @@ public abstract class Parser implements ParserInterface {
return editScript;
}
/**
*
* @param hunkActionSets
* @param bugStartLine
* @param bugEndLine
* @param fixStartLine
* @param fixEndLine
* @return
*/
protected String getASTEditScripts(List<HierarchicalActionSet> hunkActionSets, int bugEndPosition, int fixEndPosition) {
String editScript = "";
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
editScript += getASTEditScripts(hunkActionSet, bugEndPosition, fixEndPosition);
}
return editScript;
}
private String getASTEditScripts(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 bugS = set.getStartPosition();
int fixS = set.getFixStartLineNum();
if (bugS > bugEndPosition || fixS > fixEndPosition) continue;
subSets.addAll(set.getSubActions());
String actionStr = set.getActionString();
int index = actionStr.indexOf("@@");
String singleEdit = actionStr.substring(0, index).replace(" ", "");
if (singleEdit.endsWith("SimpleName")) {
actionStr = actionStr.substring(index + 2);
if (actionStr.startsWith("MethodName")) {
singleEdit = singleEdit.replace("SimpleName", "MethodName");
} 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");
}
}
}
editScript += singleEdit + " ";
}
actionSets.clear();
actionSets.addAll(subSets);
}
return editScript;
}
protected void clearITree(HierarchicalActionSet actionSet) {
actionSet.getAction().setNode(null);
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
@@ -107,6 +107,8 @@ public class FixedViolationHunkParser extends FixedViolationParser {
// Range of fixing source code
int fixStartLine = 0;
int fixEndLine = 0;
int bugEndPosition = 0;
int fixEndPosition = 0;
/*
* Convert the ITree of buggy code to a simple tree.
* It will be used to compute the similarity.
@@ -117,15 +119,15 @@ public class FixedViolationHunkParser extends FixedViolationParser {
List<SimpleTree> children = new ArrayList<>();
String astEditScripts = "";
for (HierarchicalActionSet hunkActionSet : hunkActionSets) {
/**
* Select edit scripts for deep learning.
* Edit scripts will be used to mine common fix patterns.
*/
// 1. First level: AST node type.
astEditScripts += getASTEditScripts(hunkActionSet);
// 2. source code: raw tokens
// 3. abstract identifiers:
// 4. semi-source code:
// /**
// * Select edit scripts for deep learning.
// * Edit scripts will be used to mine common fix patterns.
// */
// // 1. First level: AST node type.
// astEditScripts += getASTEditScripts(hunkActionSet);
// // 2. source code: raw tokens
// // 3. abstract identifiers:
// // 4. semi-source code:
int actionBugStart = hunkActionSet.getBugStartLineNum();
int actionBugEnd = hunkActionSet.getBugEndLineNum();
@@ -141,8 +143,14 @@ public class FixedViolationHunkParser extends FixedViolationParser {
} else if (actionFixStart != 0 && actionFixStart < fixStartLine) {
fixStartLine = actionFixStart;
}
if (bugEndLine < actionBugEnd) bugEndLine = actionBugEnd;
if (fixEndLine < actionFixEnd) fixEndLine = actionFixEnd;
if (bugEndLine < actionBugEnd) {
bugEndLine = actionBugEnd;
bugEndPosition = hunkActionSet.getBugEndPosition();
}
if (fixEndLine < actionFixEnd) {
fixEndLine = actionFixEnd;
fixEndPosition = hunkActionSet.getFixEndPosition();
}
SimplifyTree abstractIdentifier = new SimplifyTree();
abstractIdentifier.abstractTree(hunkActionSet);
@@ -168,12 +176,22 @@ public class FixedViolationHunkParser extends FixedViolationParser {
// Source Code of patches.
String patchSourceCode = getPatchSourceCode(prevFile, revFile, bugStartLine, bugEndLine, fixStartLine, fixEndLine, isInsert);
if ("".equals(patchSourceCode)) continue;
/**
* Select edit scripts for deep learning.
* Edit scripts will be used to mine common fix patterns.
*/
// 1. First level: AST node type.
astEditScripts += getASTEditScripts(hunkActionSets, bugEndPosition, fixEndPosition);
// 2. source code: raw tokens
// 3. abstract identifiers:
// 4. semi-source code:
int size = astEditScripts.split(" ").length;
if (size == 1) continue;
counter ++;
String patchPosition = "";//"###:" + counter + "\n" + revFile.getName() + "\nPosition: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n";
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + patchPosition + patchSourceCode + "\nAST diff:\n" + getAstEditScripts(hunkActionSets) + "\n";
this.patchesSourceCode += Configuration.PATCH_SIGNAL + "\n" + patchPosition + patchSourceCode + "\nAST diff:\n" + getAstEditScripts(hunkActionSets, bugEndPosition, fixEndPosition) + "\n";
this.sizes += size + "\n";
this.astEditScripts += astEditScripts + "\n";
this.alarmTypes += violation.getAlarmType() + "\n";
@@ -194,4 +212,30 @@ public class FixedViolationHunkParser extends FixedViolationParser {
return editScripts;
}
private 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 startStr) {
String editScripts = "";
editScripts += startStr + actionSet.getActionString() + "\n";
for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) {
int bugS = subActionSet.getStartPosition();
int fixS = subActionSet.getFixStartLineNum();
if (bugS > bugEndPosition || fixS > fixEndPosition) {
continue;
} else {
editScripts += getActionString(subActionSet, bugEndPosition, fixEndPosition, startStr + "---");
}
}
return editScripts;
}
}
@@ -27,7 +27,7 @@ public class Configuration {
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.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.
@@ -32,6 +32,9 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>
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.
private int bugEndPosition;
private int fixEndPosition;
public ITree getNode() {
return node;
@@ -171,6 +174,22 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>
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;
}
@Override
public int compareTo(HierarchicalActionSet o) {
return this.action.compareTo(o.action);