From 325d66d7a97a5bf0fe49a1c81cc9e0e93b0c398f Mon Sep 17 00:00:00 2001 From: Kui LIU Date: Sat, 12 Aug 2017 19:59:29 +0200 Subject: [PATCH] Remove useless actions. --- .../uni/serval/FixPatternParser/Parser.java | 7 +- .../violations/FixedViolationHunkParser.java | 15 +- .../serval/gumtree/regroup/SimplifyTree.java | 133 ++++++++++++++++++ 3 files changed, 144 insertions(+), 11 deletions(-) diff --git a/src/main/java/edu/lu/uni/serval/FixPatternParser/Parser.java b/src/main/java/edu/lu/uni/serval/FixPatternParser/Parser.java index a27bec8..a09611b 100644 --- a/src/main/java/edu/lu/uni/serval/FixPatternParser/Parser.java +++ b/src/main/java/edu/lu/uni/serval/FixPatternParser/Parser.java @@ -196,14 +196,13 @@ public abstract class Parser implements ParserInterface { while (actionSets.size() != 0) { List subSets = new ArrayList<>(); for (HierarchicalActionSet set : actionSets) { - int bugS = set.getStartPosition(); - int fixS = set.getFixStartLineNum(); + int position = set.getAction().getPosition(); if (set.getActionString().startsWith("INS")) { - if (fixS > fixEndPosition) { + if (position > fixEndPosition) { continue; } - } else if (!set.getActionString().startsWith("MOV") && bugS > bugEndPosition) { + } else if (!set.getActionString().startsWith("MOV") && position > bugEndPosition) { continue; } 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 42313fc..37b61ea 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 @@ -151,9 +151,11 @@ public class FixedViolationHunkParser extends FixedViolationParser { fixEndLine = actionFixEnd; fixEndPosition = hunkActionSet.getFixEndPosition(); } - + } + + for (HierarchicalActionSet hunkActionSet : hunkActionSets) { SimplifyTree abstractIdentifier = new SimplifyTree(); - abstractIdentifier.abstractTree(hunkActionSet); + abstractIdentifier.abstractTree(hunkActionSet, bugEndPosition); SimpleTree simpleT = hunkActionSet.getSimpleTree(); if (simpleT == null) { // Failed to get the simple tree for INS actions. continue; @@ -190,7 +192,7 @@ public class FixedViolationHunkParser extends FixedViolationParser { if (size == 1) continue; counter ++; - String patchPosition = "";//"###:" + counter + "\n" + revFile.getName() + "\nPosition: " + violation.getStartLineNum() + " --> " + violation.getEndLineNum() + "\n@@ -" + bugStartLine + ", " + bugEndLine + " +" + fixStartLine + ", " + fixEndLine + "@@\n"; + 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, bugEndPosition, fixEndPosition) + "\n"; this.sizes += size + "\n"; this.astEditScripts += astEditScripts + "\n"; @@ -225,14 +227,13 @@ public class FixedViolationHunkParser extends FixedViolationParser { editScripts += startStr + actionSet.getActionString() + "\n"; for (HierarchicalActionSet subActionSet : actionSet.getSubActions()) { - int bugS = subActionSet.getStartPosition(); - int fixS = subActionSet.getFixStartLineNum(); + int position = subActionSet.getAction().getPosition(); if (subActionSet.getActionString().startsWith("INS")) { - if (fixS > fixEndPosition) { + if (position > fixEndPosition) { continue; } - } else if (!subActionSet.getActionString().startsWith("MOV") && bugS > bugEndPosition) { + } else if (!subActionSet.getActionString().startsWith("MOV") && position > bugEndPosition) { continue; } editScripts += getActionString(subActionSet, bugEndPosition, fixEndPosition, startStr + "---"); 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 cbe12e4..73e1cf1 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 @@ -89,6 +89,69 @@ public class SimplifyTree { 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 allMoveActions = getAllMoveActions(actionSet); + if (allMoveActions != null) { + List 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 children = tree.getChildren(); +// List 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); + } + } + + actionSet.setAbstractSimpleTree(abstractSimpleTree); + actionSet.setAbstractIdentifierTree(abstractIdentifierTree); + actionSet.setSimpleTree(simpleTree); + actionSet.setOriginalTree(sourceCodeSimpleTree); + } + private SimpleTree canonicalizeSourceCodeTree(List actions, SimpleTree parent) { SimpleTree simpleTree = new SimpleTree(); simpleTree.setLabel("Block"); @@ -171,6 +234,76 @@ public class SimplifyTree { simpleTree.setParent(parent); return simpleTree; } + + 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 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")) && label.startsWith("MethodName:")) { + simpleTree.setNodeType("MethodName"); + label = label.substring(11); + label = label.substring(0, label.indexOf(":[")); + simpleTree.setLabel(label); + } else { + simpleTree.setLabel(astNode); + } + List 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:")) { // + simpleTree.setNodeType("MethodName"); + label = label.substring(11); + label = label.substring(0, label.indexOf(":[")); + 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: + simpleTree.setNodeType("VariableName"); + simpleTree.setLabel(canonicalVariableName(label, tree)); + } + } else {// variableName: + 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); + label = label.substring(0, label.indexOf(":[")); + simpleTree.setLabel(label); + } else { + simpleTree.setLabel(label.replaceAll(" ", "")); + } + } + } + + simpleTree.setParent(parent); + return simpleTree; + } private String canonicalVariableName(String label, ITree tree) { ITree parent = tree.getParent();