diff --git a/src/main/java/edu/lu/uni/serval/FixPatternParser/violations/FixedViolationHunkParser.java b/src/main/java/edu/lu/uni/serval/FixPatternParser/violations/FixedViolationHunkParser.java index 74aa543..29bbcde 100644 --- a/src/main/java/edu/lu/uni/serval/FixPatternParser/violations/FixedViolationHunkParser.java +++ b/src/main/java/edu/lu/uni/serval/FixPatternParser/violations/FixedViolationHunkParser.java @@ -2,17 +2,17 @@ package edu.lu.uni.serval.FixPatternParser.violations; import java.io.File; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; 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.SimpleTree; import edu.lu.uni.serval.gumtree.regroup.SimplifyTree; import edu.lu.uni.serval.violation.code.parser.ViolationSourceCodeTree; @@ -145,13 +145,13 @@ public class FixedViolationHunkParser extends FixedViolationParser { // if (noUpdate(editScriptTokens)) { // } + String canonicalVariableNames = getBuggyCodeTree(patchHunk, prevFile, revFile); this.patchesSourceCode += info; - this.sizes += size + "\n"; - this.astEditScripts += astEditScripts + "\n"; - - SimpleTree simpleTree = getBuggyCodeTree(patchHunk, bugEndPosition, prevFile, bugStartLine, bugEndLine); - String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim(); - this.tokensOfSourceCode += tokens + "\n"; + this.patchesSourceCode += "\nRenamed_Variables###:" + canonicalVariableNames; +// this.sizes += size + "\n"; +// this.astEditScripts += astEditScripts + "\n"; +// String tokens = Tokenizer.getTokensDeepFirst(simpleTree).trim(); +// this.tokensOfSourceCode += tokens + "\n"; } } } @@ -164,53 +164,44 @@ public class FixedViolationHunkParser extends FixedViolationParser { return scripts; } - private SimpleTree getBuggyCodeTree(DiffEntryHunk patchHunk, int bugEndPosition, File prevFile, int bugStartLine, int bugEndLine) { - SimpleTree simpleTree = new SimpleTree(); - simpleTree.setLabel("Block"); - simpleTree.setNodeType("Block"); - List children = new ArrayList<>(); + private String getBuggyCodeTree(DiffEntryHunk patchHunk, File prevFile, File revFile) { + int bugStartLine = patchHunk.getBugLineStartNum(); + int bugEndLine = bugStartLine + patchHunk.getBugRange() - 1; - int vStartLine = patchHunk.getBugLineStartNum(); - int vEndLine = vStartLine + patchHunk.getBuggyHunkSize() + 1; - if (bugStartLine < vStartLine && vEndLine < bugEndLine) { - ViolationSourceCodeTree parser = new ViolationSourceCodeTree(prevFile, vStartLine, vEndLine); - parser.extract(); - List matchedTrees = parser.getViolationSourceCodeTrees(); - if (matchedTrees.size() > 0) { - for (ITree matchedTree : matchedTrees) { - SimpleTree simpleT = new SimplifyTree().canonicalizeSourceCodeTree(matchedTree, simpleTree); - children.add(simpleT); - } + ViolationSourceCodeTree parser = new ViolationSourceCodeTree(prevFile, bugStartLine, bugEndLine); + parser.extract(); + List matchedTrees = parser.getViolationSourceCodeTrees(); + Map renamedVariablesMap = new HashMap<>(); + Map canonicalVariables = new HashMap<>(); + if (matchedTrees.size() > 0) { + SimplifyTree st = new SimplifyTree(); + for (ITree matchedTree : matchedTrees) { + st.canonicalizeSourceCodeTree(matchedTree); } + renamedVariablesMap = st.canonicalVariableMap; + canonicalVariables = st.canonicalVariables; } - if (children.size() == 0) { - List hunkActionSets = patchHunk.getActionSets(); - /* - * Convert the ITree of buggy code to a simple tree. - * It will be used to compute the similarity. - */ - for (HierarchicalActionSet hunkActionSet : hunkActionSets) { - // TODO simplify buggy tree with buggy code. - /** - * Select edit scripts for deep learning. - * Edit scripts will be used to mine common fix patterns. - */ -// // 1. First level: AST node type. -// // 2. source code: raw tokens -// // 3. abstract identifiers: -// // 4. semi-source code: - SimplifyTree abstractIdentifier = new SimplifyTree(); - abstractIdentifier.abstractTree(hunkActionSet, bugEndPosition); - SimpleTree simpleT = hunkActionSet.getSimpleTree(); - if (simpleT == null) { // Failed to get the simple tree for INS actions. - continue; - } - children.add(simpleT); + + 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; } - simpleTree.setChildren(children); - simpleTree.setParent(null); - return simpleTree; + + StringBuilder builder = new StringBuilder(); + for (Map.Entry entry : renamedVariablesMap.entrySet()) { + builder.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n"); + } + return builder.toString(); } private void removeOverlapperdUPD(List actionSets) { diff --git a/src/main/java/edu/lu/uni/serval/gumtree/regroup/SimplifyTree.java b/src/main/java/edu/lu/uni/serval/gumtree/regroup/SimplifyTree.java index 3ccf138..3dac976 100644 --- a/src/main/java/edu/lu/uni/serval/gumtree/regroup/SimplifyTree.java +++ b/src/main/java/edu/lu/uni/serval/gumtree/regroup/SimplifyTree.java @@ -254,6 +254,44 @@ public class SimplifyTree { return simpleTree; } + public void canonicalizeSourceCodeTree(ITree tree) { + String label = tree.getLabel(); + String astNode = ASTNodeMap.map.get(tree.getType()); + + List 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:")) { // + } 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: + canonicalVariableName(label, tree); + } + } else {// variableName: + canonicalVariableName(label, tree); + } + } + } + } + public SimpleTree canonicalizeSourceCodeTree(ITree tree, SimpleTree parent, int bugEndPosition) { SimpleTree simpleTree = new SimpleTree(); @@ -430,6 +468,9 @@ public class SimplifyTree { return null; } + public Map canonicalVariableMap = new HashMap<>(); + public Map canonicalVariables = new HashMap<>(); + private String matchVariableDeclarationExpression(ITree variable, String label) { List children = variable.getChildren(); ITree type = null; @@ -442,8 +483,7 @@ public class SimplifyTree { ITree simpleName = child.getChild(0); if (simpleName.getLabel().equals(label)) { String typeStr = canonicalizeTypeStr(type.getLabel()); - label = typeStr.toLowerCase() + "Var"; - return label; + return canonicalizeVariable(label, typeStr); } } } @@ -458,8 +498,7 @@ public class SimplifyTree { if (child.getLabel().equals(label)) { ITree type = children.get(i - 1); String typeStr = canonicalizeTypeStr(type.getLabel()); - label = typeStr.toLowerCase() + "Var"; - return label; + return canonicalizeVariable(label, typeStr); } break; } @@ -467,6 +506,24 @@ public class SimplifyTree { 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("<");