From 690253867d0b36146723f87707995b80c6528994 Mon Sep 17 00:00:00 2001 From: Kui LIU Date: Fri, 4 Aug 2017 13:44:19 +0200 Subject: [PATCH] Optimize the algorithm of regrouping GumTree results. --- .../regroup/HierarchicalRegrouper.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/edu/lu/uni/serval/gumtree/regroup/HierarchicalRegrouper.java b/src/main/java/edu/lu/uni/serval/gumtree/regroup/HierarchicalRegrouper.java index bc6358b..3af7ac5 100644 --- a/src/main/java/edu/lu/uni/serval/gumtree/regroup/HierarchicalRegrouper.java +++ b/src/main/java/edu/lu/uni/serval/gumtree/regroup/HierarchicalRegrouper.java @@ -98,15 +98,34 @@ public class HierarchicalRegrouper { } private void addToActionSets(HierarchicalActionSet actionSet, Action parentAct, List actionSets) { + Action act = actionSet.getAction(); for (HierarchicalActionSet actSet : actionSets) { if (actSet.equals(actionSet)) continue; - if (actSet.getAction().equals(parentAct)) { // actSet is the parent of actionSet. + Action action = actSet.getAction(); + if (action instanceof Move && !(act instanceof Move)) continue; // If action is MOV, its children must be MOV. + if (action instanceof Insert && !(act instanceof Addition)) continue;// If action is INS, its children must be MOV or INS. + + if (action.equals(parentAct)) { // actSet is the parent of actionSet. actionSet.setParent(actSet); actSet.getSubActions().add(actionSet); ListSorter sorter = new ListSorter(actSet.getSubActions()); actSet.setSubActions(sorter.sortAscending()); break; } else { + if ((!(action instanceof Insert) && !(act instanceof Insert)) + || (action instanceof Insert && (act instanceof Insert))) { + int startPosition = act.getPosition(); + int length = act.getLength(); + int startP = action.getPosition(); + int leng = action.getLength(); + + if (startP > startPosition + length) { + break; + } else if (startP + leng < startPosition) { + continue; + } + } + // SubAction range: startP <= startPosition && startPosition + length <= startP + leng addToActionSets(actionSet, parentAct, actSet.getSubActions()); } } @@ -117,13 +136,11 @@ public class HierarchicalRegrouper { for(HierarchicalActionSet actionSet : actionSets) { Action action = actionSet.getAction(); + if (action instanceof Move && !(act instanceof Move)) continue; // If action is MOV, its children must be MOV. + if (action instanceof Insert && !(act instanceof Addition)) continue;// If action is INS, its children must be MOV or INS. ITree tree = action.getNode(); if (tree.equals(parentTree)) { // actionSet is the parent of actSet. - if (action instanceof Move && !(act instanceof Move)) { - continue; - } - HierarchicalActionSet actSet = createActionSet(act, actionSet.getAction(), actionSet); actionSet.getSubActions().add(actSet); return true; @@ -134,12 +151,14 @@ public class HierarchicalRegrouper { int length = act.getLength(); int startP = action.getPosition(); int leng = action.getLength(); - if (!(startP <= startPosition) || !(length <= leng)) { - continue; - } else if (startP > startPosition + length) { + + if (startP > startPosition + length) { break; + } else if (startP + leng < startPosition) { + continue; } } + // SubAction range: startP <= startPosition && startPosition + length <= startP + leng List subActionSets = actionSet.getSubActions(); if (subActionSets.size() > 0) { boolean added = addToAactionSet(act, parentAct, subActionSets);