From 40a2722a01b682ef57d17fcfa5def48e1519fb8b Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Mon, 16 May 2022 01:40:16 -0400 Subject: [PATCH] [O] Reformat code for java --- .../main/java/edu/lu/uni/serval/Launcher.java | 2 +- .../serval/richedit/ediff/BaseMessage.java | 29 +- .../uni/serval/richedit/ediff/EDiffActor.java | 132 +- .../richedit/ediff/EDiffHunkParser.java | 153 ++- .../serval/richedit/ediff/EDiffMessage.java | 93 +- .../serval/richedit/ediff/EDiffParser.java | 147 ++- .../serval/richedit/ediff/EDiffWorker.java | 155 ++- .../richedit/ediff/HierarchicalActionSet.java | 373 +++--- .../richedit/ediff/HierarchicalRegrouper.java | 683 +++++----- .../ediff/HierarchicalRegrouperForC.java | 1160 +++++++++-------- .../serval/richedit/ediff/MessageFile.java | 86 +- .../lu/uni/serval/richedit/ediff/Parser.java | 94 +- .../richedit/ediff/ParserInterface.java | 23 +- .../serval/richedit/ediff/RunnableParser.java | 91 +- .../serval/richedit/jobs/CompareTrees.java | 258 +--- .../serval/richedit/jobs/EnhancedASTDiff.java | 78 +- .../edu/lu/uni/serval/utils/ASTNodeMap.java | 216 +-- .../edu/lu/uni/serval/utils/CallShell.java | 118 +- .../lu/uni/serval/utils/ClusterToPattern.java | 43 +- .../edu/lu/uni/serval/utils/EDiffHelper.java | 780 ++++++----- .../edu/lu/uni/serval/utils/FileHelper.java | 317 +++-- .../edu/lu/uni/serval/utils/ListSorter.java | 38 +- .../edu/lu/uni/serval/utils/PoolBuilder.java | 20 +- 23 files changed, 2719 insertions(+), 2370 deletions(-) diff --git a/richedit/src/main/java/edu/lu/uni/serval/Launcher.java b/richedit/src/main/java/edu/lu/uni/serval/Launcher.java index d4aa8c4..b3658c3 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/Launcher.java +++ b/richedit/src/main/java/edu/lu/uni/serval/Launcher.java @@ -17,7 +17,7 @@ import java.util.Properties; */ public class Launcher { - private static Logger log = LoggerFactory.getLogger(Launcher.class); + private static final Logger log = LoggerFactory.getLogger(Launcher.class); public static void main(String[] args) throws IOException { diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/BaseMessage.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/BaseMessage.java index 34df0aa..70e647d 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/BaseMessage.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/BaseMessage.java @@ -3,42 +3,51 @@ package edu.lu.uni.serval.richedit.ediff; /** * Created by anilkoyuncu on 18/09/2018. */ -public class BaseMessage { +public class BaseMessage +{ private long SECONDS_TO_WAIT; + private int id; + private int threadPoolSize; - public BaseMessage(int id,Long timeout) { + public BaseMessage(int id, Long timeout) + { this.id = id; this.SECONDS_TO_WAIT = timeout; -// this.threadPoolSize = threadPoolSize; + // this.threadPoolSize = threadPoolSize; } - public int getId() { + public int getId() + { return id; } - public void setId(int id) { + public void setId(int id) + { this.id = id; } - - public long getSECONDS_TO_WAIT() { + public long getSECONDS_TO_WAIT() + { return SECONDS_TO_WAIT; } - public void setSECONDS_TO_WAIT(long SECONDS_TO_WAIT) { + public void setSECONDS_TO_WAIT(long SECONDS_TO_WAIT) + { this.SECONDS_TO_WAIT = SECONDS_TO_WAIT; } - public int getThreadPoolSize() { + public int getThreadPoolSize() + { return threadPoolSize; } - public void setThreadPoolSize(int threadPoolSize) { + public void setThreadPoolSize(int threadPoolSize) + { this.threadPoolSize = threadPoolSize; } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffActor.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffActor.java index adece28..45e5bf7 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffActor.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffActor.java @@ -10,66 +10,84 @@ import org.slf4j.LoggerFactory; import java.util.List; -public class EDiffActor extends UntypedActor { - - private static Logger logger = LoggerFactory.getLogger(EDiffActor.class); +public class EDiffActor extends UntypedActor +{ - private ActorRef mineRouter; - private final int numberOfWorkers; - private int counter = 0; - - public EDiffActor(int numberOfWorkers, String project) { - mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers) - .props(EDiffWorker.props(project)), "mine-fix-pattern-router"); - this.numberOfWorkers = numberOfWorkers; - } + private static final Logger logger = LoggerFactory.getLogger(EDiffActor.class); - public static Props props(final int numberOfWorkers, final String project) { - - return Props.create(new Creator() { + private final ActorRef mineRouter; - private static final long serialVersionUID = 9207427376110704705L; + private final int numberOfWorkers; - @Override - public EDiffActor create() throws Exception { - return new EDiffActor(numberOfWorkers, project); - } - - }); - } - - @SuppressWarnings("deprecation") - @Override - public void onReceive(Object message) throws Exception { - if (message instanceof EDiffMessage) { - List files = ((EDiffMessage) message).getMsgFiles(); - int size = files.size(); - int average = size / numberOfWorkers; - int reminder = size % numberOfWorkers; - int counter = 0; - - for (int i = 0; i < numberOfWorkers; i ++) { - int fromIndex = i * average + counter; - if (counter < reminder) counter ++; - int toIndex = (i + 1) * average + counter; - - List filesOfWorkers = files.subList(fromIndex, toIndex); - final EDiffMessage workMsg = new EDiffMessage(i + 1, filesOfWorkers,((EDiffMessage) message).getSECONDS_TO_WAIT(),((EDiffMessage) message).getInnerPool(),((EDiffMessage) message).getSrcMLPath(),((EDiffMessage) message).getRootType()); - mineRouter.tell(workMsg, getSelf()); - logger.info("Assign {} task to worker #" + (i + 1) ,filesOfWorkers.size()); - } - } else if ("STOP".equals(message.toString())) { - counter ++; - logger.info(counter + " workers finalized their work..."); - if (counter >= numberOfWorkers) { - logger.info("All workers finalized their work..."); - this.getContext().stop(mineRouter); - this.getContext().stop(getSelf()); - this.getContext().system().shutdown(); - } - } else { - unhandled(message); - } - } + private int counter = 0; + + public EDiffActor(int numberOfWorkers, String project) + { + mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers) + .props(EDiffWorker.props(project)), "mine-fix-pattern-router"); + this.numberOfWorkers = numberOfWorkers; + } + + public static Props props(final int numberOfWorkers, final String project) + { + + return Props.create(new Creator() + { + + private static final long serialVersionUID = 9207427376110704705L; + + @Override + public EDiffActor create() throws Exception + { + return new EDiffActor(numberOfWorkers, project); + } + + }); + } + + @SuppressWarnings("deprecation") + @Override + public void onReceive(Object message) throws Exception + { + if (message instanceof EDiffMessage) + { + List files = ((EDiffMessage) message).getMsgFiles(); + int size = files.size(); + int average = size / numberOfWorkers; + int reminder = size % numberOfWorkers; + int counter = 0; + + for (int i = 0; i < numberOfWorkers; i++) + { + int fromIndex = i * average + counter; + if (counter < reminder) + { + counter++; + } + int toIndex = (i + 1) * average + counter; + + List filesOfWorkers = files.subList(fromIndex, toIndex); + final EDiffMessage workMsg = new EDiffMessage(i + 1, filesOfWorkers, ((EDiffMessage) message).getSECONDS_TO_WAIT(), ((EDiffMessage) message).getInnerPool(), ((EDiffMessage) message).getSrcMLPath(), ((EDiffMessage) message).getRootType()); + mineRouter.tell(workMsg, getSelf()); + logger.info("Assign {} task to worker #" + (i + 1), filesOfWorkers.size()); + } + } + else if ("STOP".equals(message.toString())) + { + counter++; + logger.info(counter + " workers finalized their work..."); + if (counter >= numberOfWorkers) + { + logger.info("All workers finalized their work..."); + this.getContext().stop(mineRouter); + this.getContext().stop(getSelf()); + this.getContext().system().shutdown(); + } + } + else + { + unhandled(message); + } + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffHunkParser.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffHunkParser.java index 5348144..3292a5b 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffHunkParser.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffHunkParser.java @@ -11,101 +11,110 @@ import java.io.File; import java.util.List; - /** * Parse fix violations with GumTree in terms of multiple statements. * * @author kui.liu - * */ -public class EDiffHunkParser extends EDiffParser { +public class EDiffHunkParser extends EDiffParser +{ - private static Logger logger = LoggerFactory.getLogger(EDiffHunkParser.class); - @Override - public void parseFixPatterns(File prevFile, File revFile, File diffentryFile, String project, JedisPool innerPool,String srcMLPath,String hunkLimit,boolean isJava) { - try { - String datasetName = project; - String[] split1 = diffentryFile.getParent().split(datasetName); - String root = split1[0]; - String pj = split1[1].split("/")[1]; + private static final Logger logger = LoggerFactory.getLogger(EDiffHunkParser.class); + + @Override + public void parseFixPatterns(File prevFile, File revFile, File diffentryFile, String project, JedisPool innerPool, String srcMLPath, String hunkLimit, boolean isJava) + { + try + { + String datasetName = project; + String[] split1 = diffentryFile.getParent().split(datasetName); + String root = split1[0]; + String pj = split1[1].split("/")[1]; - List actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile, srcMLPath, isJava); + List actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile, srcMLPath, isJava); - if (actionSets != null && actionSets.size() != 0) { + if (actionSets != null && actionSets.size() != 0) + { - boolean processActionSet = true; + boolean processActionSet = true; - if (actionSets.size() > Integer.valueOf(hunkLimit)) { - processActionSet = false; - logger.debug("Skipping {} set size {}", diffentryFile.getName(), hunkLimit); - } + if (actionSets.size() > Integer.valueOf(hunkLimit)) + { + processActionSet = false; + logger.debug("Skipping {} set size {}", diffentryFile.getName(), hunkLimit); + } - int hunkSet = 0; - if (processActionSet) { + int hunkSet = 0; + if (processActionSet) + { - for (HierarchicalActionSet actionSet : actionSets) { -// FileOutputStream f = null; + for (HierarchicalActionSet actionSet : actionSets) + { + // FileOutputStream f = null; -// try { + // try { - String astNodeType = actionSet.getAstNodeType(); -// if (astNodeType.equals(rootType)){ -// -// } - actionSet.toString(); - int size = actionSet.getActionSize(); + String astNodeType = actionSet.getAstNodeType(); + // if (astNodeType.equals(rootType)){ + // + // } + actionSet.toString(); + int size = actionSet.getActionSize(); - String key = astNodeType + "/" + String.valueOf(size) + "/" + pj + "_" + diffentryFile.getName() + "_" + String.valueOf(hunkSet); + String key = astNodeType + "/" + size + "/" + pj + "_" + diffentryFile.getName() + "_" + hunkSet; - ITree targetTree = EDiffHelper.getTargets(actionSet, isJava); - ITree actionTree = EDiffHelper.getActionTrees(actionSet); - ITree shapeTree = EDiffHelper.getShapeTree(actionSet, isJava); - ITree tokenTree = EDiffHelper.getTokenTree(actionSet,isJava); - String tokens = EDiffHelper.getNames2(tokenTree); -// EDiffHelper.getTokenTree(actionSet, parent, children, tc); - try (Jedis inner = innerPool.getResource()) { + ITree targetTree = EDiffHelper.getTargets(actionSet, isJava); + ITree actionTree = EDiffHelper.getActionTrees(actionSet); + ITree shapeTree = EDiffHelper.getShapeTree(actionSet, isJava); + ITree tokenTree = EDiffHelper.getTokenTree(actionSet, isJava); + String tokens = EDiffHelper.getNames2(tokenTree); + // EDiffHelper.getTokenTree(actionSet, parent, children, tc); + try (Jedis inner = innerPool.getResource()) + { - inner.hset("dump", key, actionSet.toString()); - inner.hset(key, "actionTree", actionTree.toStaticHashString()); - inner.hset(key, "targetTree", targetTree.toStaticHashString()); - inner.hset(key, "shapeTree", shapeTree.toStaticHashString()); - inner.hset(key, "tokens", tokens); - } -// File f = new File(root+"dumps/"+astNodeType+"/"+String.valueOf(size)+"/"); -// f.mkdirs(); -// f = new File(root+"dumps/"+key); -// -// FileUtils.writeByteArrayToFile(f,EDiffHelper.kryoSerialize(actionSet)); -// FileUtils.writeByteArrayToFile(f,EDiffHelper.commonsSerialize(actionSet)); -// FileUtils.writeByteArrayToFile(f,actionSet.toString().getBytes()); -// FileOutputStream fos = new FileOutputStream(f); -// ObjectOutputStream oos = new ObjectOutputStream(fos); -// oos.writeObject(EDiffHelper.kryoSerialize(actionSet)); -// oos.flush(); -// oos.close(); + inner.hset("dump", key, actionSet.toString()); + inner.hset(key, "actionTree", actionTree.toStaticHashString()); + inner.hset(key, "targetTree", targetTree.toStaticHashString()); + inner.hset(key, "shapeTree", shapeTree.toStaticHashString()); + inner.hset(key, "tokens", tokens); + } + // File f = new File(root+"dumps/"+astNodeType+"/"+String.valueOf(size)+"/"); + // f.mkdirs(); + // f = new File(root+"dumps/"+key); + // + // FileUtils.writeByteArrayToFile(f,EDiffHelper.kryoSerialize(actionSet)); + // FileUtils.writeByteArrayToFile(f,EDiffHelper.commonsSerialize(actionSet)); + // FileUtils.writeByteArrayToFile(f,actionSet.toString().getBytes()); + // FileOutputStream fos = new FileOutputStream(f); + // ObjectOutputStream oos = new ObjectOutputStream(fos); + // oos.writeObject(EDiffHelper.kryoSerialize(actionSet)); + // oos.flush(); + // oos.close(); -// } catch (Exception e) { -// logger.error("error", e); -//// e.printStackTrace(); -// } - hunkSet++; - } - try (Jedis inner = innerPool.getResource()) { - inner.hset("diffEntry", pj + "_" + diffentryFile.getName(), "1"); - } + // } catch (Exception e) { + // logger.error("error", e); + //// e.printStackTrace(); + // } + hunkSet++; + } + try (Jedis inner = innerPool.getResource()) + { + inner.hset("diffEntry", pj + "_" + diffentryFile.getName(), "1"); + } - } - } - } catch (Exception e) { - logger.error("error", e); -// e.printStackTrace(); - } + } + } + } + catch (Exception e) + { + logger.error("error", e); + // e.printStackTrace(); + } - } - + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffMessage.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffMessage.java index 34916bc..46d2520 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffMessage.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffMessage.java @@ -4,63 +4,70 @@ import redis.clients.jedis.JedisPool; import java.util.List; -public class EDiffMessage extends BaseMessage{ +public class EDiffMessage extends BaseMessage +{ - private List msgFiles; + private final List msgFiles; - public JedisPool getInnerPool() { - return innerPool; - } + public JedisPool getInnerPool() + { + return innerPool; + } - public void setInnerPool(JedisPool innerPool) { - this.innerPool = innerPool; - } + public void setInnerPool(JedisPool innerPool) + { + this.innerPool = innerPool; + } - private JedisPool innerPool; + private JedisPool innerPool; - public String getSrcMLPath() { - return srcMLPath; - } + public String getSrcMLPath() + { + return srcMLPath; + } - private String srcMLPath; + private String srcMLPath; - public String getRootType() { - return rootType; - } + public String getRootType() + { + return rootType; + } - private String rootType; + private String rootType; + public EDiffMessage(int id, List msgFiles, String eDiffTimeout, JedisPool pool, String srcMLPath, String rootType) + { + super(id, new Long(eDiffTimeout)); + this.msgFiles = msgFiles; + this.innerPool = pool; + this.srcMLPath = srcMLPath; + this.rootType = rootType; + } + + public EDiffMessage(int id, List msgFiles, Long eDiffTimeout, JedisPool pool) + { + super(id, eDiffTimeout); + this.msgFiles = msgFiles; + this.innerPool = pool; + } - - public EDiffMessage(int id, List msgFiles,String eDiffTimeout,JedisPool pool,String srcMLPath,String rootType) { - super(id,new Long(eDiffTimeout)); - this.msgFiles = msgFiles; - this.innerPool = pool; - this.srcMLPath = srcMLPath; - this.rootType = rootType; - } - public EDiffMessage(int id, List msgFiles,Long eDiffTimeout,JedisPool pool) { - super(id,eDiffTimeout); - this.msgFiles = msgFiles; - this.innerPool = pool; - } + public EDiffMessage(int id, List filesOfWorkers, long seconds_to_wait, JedisPool innerPool, String srcMLPath, String rootType) + { + super(id, seconds_to_wait); + this.msgFiles = filesOfWorkers; + this.innerPool = innerPool; + this.srcMLPath = srcMLPath; + this.rootType = rootType; + } - public EDiffMessage(int id, List filesOfWorkers, long seconds_to_wait, JedisPool innerPool, String srcMLPath,String rootType) { + public List getMsgFiles() + { + return msgFiles; + } - super(id,seconds_to_wait); - this.msgFiles = filesOfWorkers; - this.innerPool = innerPool; - this.srcMLPath = srcMLPath; - this.rootType = rootType; - } - - public List getMsgFiles() { - return msgFiles; - } - -} \ No newline at end of file +} diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffParser.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffParser.java index c85f279..b04da3b 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffParser.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffParser.java @@ -3,8 +3,6 @@ package edu.lu.uni.serval.richedit.ediff; import com.github.gumtreediff.actions.model.Action; import com.github.gumtreediff.gen.srcml.GumTreeCComparer; import edu.lu.uni.serval.gumtree.GumTreeComparer; - - import edu.lu.uni.serval.utils.ListSorter; import redis.clients.jedis.JedisPool; @@ -18,87 +16,100 @@ import java.util.List; * Parse fix patterns with GumTree. * * @author kui.liu - * */ -public class EDiffParser extends Parser { +public class EDiffParser extends Parser +{ - /* - * ResultType: - * 0: normal GumTree results. - * 1: null GumTree result. - * 2: No source code changes. - * 3: No Statement Change. - * 4: useless violations - */ - public int resultType = 0; + /* + * ResultType: + * 0: normal GumTree results. + * 1: null GumTree result. + * 2: No source code changes. + * 3: No Statement Change. + * 4: useless violations + */ + public int resultType = 0; + /** + * Regroup GumTree results without remove the modification of variable names. + * + * @param prevFile + * @param revFile + * @return + */ + public List parseChangedSourceCodeWithGumTree2(File prevFile, File revFile, String srcMLPath, boolean isJava) + { + List actionSets = new ArrayList<>(); + // GumTree results + // boolean isJava =false; + List gumTreeResults = null; + if (isJava) + { + // if (revFile.getName().endsWith(".c") & prevFile.getName().endsWith(".c") || revFile.getName().endsWith(".h") & prevFile.getName().endsWith(".h")){ + // gumTreeResults = new GumTreeComparer().compareCFilesWithGumTree(prevFile, revFile); - /** - * Regroup GumTree results without remove the modification of variable names. - * - * @param prevFile - * @param revFile - * @return - */ - public List parseChangedSourceCodeWithGumTree2(File prevFile, File revFile,String srcMLPath,boolean isJava) { - List actionSets = new ArrayList<>(); - // GumTree results -// boolean isJava =false; - List gumTreeResults = null; - if (isJava){ -// if (revFile.getName().endsWith(".c") & prevFile.getName().endsWith(".c") || revFile.getName().endsWith(".h") & prevFile.getName().endsWith(".h")){ -// gumTreeResults = new GumTreeComparer().compareCFilesWithGumTree(prevFile, revFile); - - gumTreeResults = new GumTreeComparer().compareTwoFilesWithGumTree(prevFile, revFile); + gumTreeResults = new GumTreeComparer().compareTwoFilesWithGumTree(prevFile, revFile); - }else{ - gumTreeResults = new GumTreeCComparer().compareCFilesWithGumTree(prevFile, revFile,srcMLPath); + } + else + { + gumTreeResults = new GumTreeCComparer().compareCFilesWithGumTree(prevFile, revFile, srcMLPath); - } - if (gumTreeResults == null) { - this.resultType = 1; - return actionSets; - } else if (gumTreeResults.size() == 0){ - this.resultType = 2; - return actionSets; - } else { - // Regroup GumTre results. - List allActionSets = null; - if (isJava){ - allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults); - }else{ - HashSet removeType = new HashSet(Arrays.asList(131,132,133,134,135,136,137)); - boolean b = gumTreeResults.stream().anyMatch(p -> removeType.contains(p.getNode().getType())); - if(b){ - return actionSets; - } - allActionSets = new HierarchicalRegrouperForC().regroupGumTreeResults(gumTreeResults); - } + } + if (gumTreeResults == null) + { + this.resultType = 1; + return actionSets; + } + else if (gumTreeResults.size() == 0) + { + this.resultType = 2; + return actionSets; + } + else + { + // Regroup GumTre results. + List allActionSets = null; + if (isJava) + { + allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults); + } + else + { + HashSet removeType = new HashSet(Arrays.asList(131, 132, 133, 134, 135, 136, 137)); + boolean b = gumTreeResults.stream().anyMatch(p -> removeType.contains(p.getNode().getType())); + if (b) + { + return actionSets; + } + allActionSets = new HierarchicalRegrouperForC().regroupGumTreeResults(gumTreeResults); + } - ListSorter sorter = new ListSorter<>(allActionSets); - actionSets = sorter.sortAscending(); + ListSorter sorter = new ListSorter<>(allActionSets); + actionSets = sorter.sortAscending(); - if (actionSets.size() == 0) { - this.resultType = 3; - } + if (actionSets.size() == 0) + { + this.resultType = 3; + } - return actionSets; - } - } + return actionSets; + } + } - @Override - public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath,String rootType,boolean isJava) { + @Override + public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava) + { - } - -// @Override -// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool) { -// -// } + } + // @Override + // public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool) { + // + // } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffWorker.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffWorker.java index cf6ef3e..ee38703 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffWorker.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/EDiffWorker.java @@ -11,82 +11,101 @@ import java.io.File; import java.util.List; import java.util.concurrent.*; -public class EDiffWorker extends UntypedActor { - private static Logger log = LoggerFactory.getLogger(EDiffActor.class); - - private String project; +public class EDiffWorker extends UntypedActor +{ + private static final Logger log = LoggerFactory.getLogger(EDiffActor.class); - - public EDiffWorker(String project) { - this.project = project; - } - - public static Props props(final String project) { - return Props.create(new Creator() { - - private static final long serialVersionUID = -7615153844097275009L; - - @Override - public EDiffWorker create() throws Exception { - return new EDiffWorker(project); - } - - }); - } - - @Override - public void onReceive(Object message) throws Exception { - if (message instanceof EDiffMessage) { - EDiffMessage msg = (EDiffMessage) message; - List files = msg.getMsgFiles(); - - int id = msg.getId(); - int counter = 0; - JedisPool innerPool = msg.getInnerPool(); - String srcMLPath = msg.getSrcMLPath(); - String rootType = msg.getRootType(); - - for (MessageFile msgFile : files) { - File revFile = msgFile.getRevFile(); - File prevFile = msgFile.getPrevFile(); - File diffentryFile = msgFile.getDiffEntryFile(); + private final String project; + public EDiffWorker(String project) + { + this.project = project; + } - EDiffHunkParser parser = new EDiffHunkParser(); + public static Props props(final String project) + { + return Props.create(new Creator() + { - final ExecutorService executor = Executors.newSingleThreadExecutor(); - // schedule the work + private static final long serialVersionUID = -7615153844097275009L; + + @Override + public EDiffWorker create() throws Exception + { + return new EDiffWorker(project); + } + + }); + } + + @Override + public void onReceive(Object message) throws Exception + { + if (message instanceof EDiffMessage) + { + EDiffMessage msg = (EDiffMessage) message; + List files = msg.getMsgFiles(); + + int id = msg.getId(); + int counter = 0; + JedisPool innerPool = msg.getInnerPool(); + String srcMLPath = msg.getSrcMLPath(); + String rootType = msg.getRootType(); + + for (MessageFile msgFile : files) + { + File revFile = msgFile.getRevFile(); + File prevFile = msgFile.getPrevFile(); + File diffentryFile = msgFile.getDiffEntryFile(); - final Future future = executor.submit(new RunnableParser(prevFile, revFile, diffentryFile, parser,project,msg.getInnerPool(),srcMLPath,rootType,false)); - try { - // wait for task to complete - future.get(msg.getSECONDS_TO_WAIT(), TimeUnit.SECONDS); + EDiffHunkParser parser = new EDiffHunkParser(); - counter ++; - if (counter % 1000 == 0) { - log.info("Worker #" + id +" finalized parsing " + counter + " files... remaing "+ (files.size() - counter)); - } - } catch (TimeoutException e) { - future.cancel(true); - System.err.println("#Timeout: " + revFile.getName()); - } catch (InterruptedException e) { - System.err.println("#TimeInterrupted: " + revFile.getName()); - e.printStackTrace(); - } catch (ExecutionException e) { - System.err.println("#TimeAborted: " + revFile.getName()); - e.printStackTrace(); - } finally { - executor.shutdownNow(); - } - } + final ExecutorService executor = Executors.newSingleThreadExecutor(); + // schedule the work - log.info("Worker #" + id + " finalized the work..."); - this.getSender().tell("STOP", getSelf()); - } else { - unhandled(message); - } - } + + final Future future = executor.submit(new RunnableParser(prevFile, revFile, diffentryFile, parser, project, msg.getInnerPool(), srcMLPath, rootType, false)); + try + { + // wait for task to complete + future.get(msg.getSECONDS_TO_WAIT(), TimeUnit.SECONDS); + + counter++; + if (counter % 1000 == 0) + { + log.info("Worker #" + id + " finalized parsing " + counter + " files... remaing " + (files.size() - counter)); + } + } + catch (TimeoutException e) + { + future.cancel(true); + System.err.println("#Timeout: " + revFile.getName()); + } + catch (InterruptedException e) + { + System.err.println("#TimeInterrupted: " + revFile.getName()); + e.printStackTrace(); + } + catch (ExecutionException e) + { + System.err.println("#TimeAborted: " + revFile.getName()); + e.printStackTrace(); + } + finally + { + executor.shutdownNow(); + } + } + + log.info("Worker #" + id + " finalized the work..."); + this.getSender().tell("STOP", getSelf()); + } + else + { + unhandled(message); + } + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalActionSet.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalActionSet.java index 6ce923e..578c859 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalActionSet.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalActionSet.java @@ -9,203 +9,256 @@ import java.util.List; /** * Hierarchical-level results of GumTree results - * - * @author kui.liu * + * @author kui.liu */ -public class HierarchicalActionSet implements Comparable,Serializable { - - private String astNodeType; - private Action action; - private Action parentAction; - private String actionString; - private Integer startPosition; - private Integer length; - private int bugStartLineNum = 0; - private int bugEndLineNum; - private int fixStartLineNum; - private int fixEndLineNum; - private HierarchicalActionSet parent = null; - private List subActions = new ArrayList<>(); - - private ITree node; - // source code tree. +public class HierarchicalActionSet implements Comparable, Serializable +{ - public int getBugEndPosition() { - return bugEndPosition; - } + private String astNodeType; - public int getFixEndPosition() { - return fixEndPosition; - } + private Action action; - private int bugEndPosition; - private int fixEndPosition; + private Action parentAction; - public ITree getNode() { - return node; - } + private String actionString; - public void setNode(ITree node) { - this.node = node; - } + private Integer startPosition; - public void setAstNodeType(String astNodeType) { - this.astNodeType = astNodeType; - } - - public String getAstNodeType() { - return astNodeType; - } + private Integer length; - public Action getAction() { - return action; - } + private int bugStartLineNum = 0; - public void setAction(Action action) { - this.action = action; - } + private int bugEndLineNum; - public Action getParentAction() { - return parentAction; - } + private int fixStartLineNum; - public void setParentAction(Action parentAction) { - this.parentAction = parentAction; - } + private int fixEndLineNum; - public String getActionString() { - return actionString; - } + private HierarchicalActionSet parent = null; - public void setActionString(String actionString) { - this.actionString = actionString; - - int atIndex = actionString.indexOf("@AT@") + 4; - int lengthIndex = actionString.indexOf("@LENGTH@"); - if (lengthIndex == -1) { - this.startPosition = Integer.parseInt(actionString.substring(atIndex).trim()); - this.length = 0; - } else { - this.startPosition = Integer.parseInt(actionString.substring(atIndex, lengthIndex).trim()); - this.length = Integer.parseInt(actionString.substring(lengthIndex + 8).trim()); - } - - String nodeType = actionString.substring(0, actionString.indexOf("@@")); - nodeType = nodeType.substring(nodeType.indexOf(" ") + 1); - this.astNodeType = nodeType; - } + private List subActions = new ArrayList<>(); - public int getStartPosition() { - return startPosition; - } + private ITree node; + // source code tree. - public int getLength() { - return length; - } + public int getBugEndPosition() + { + return bugEndPosition; + } - public int getBugStartLineNum() { - return bugStartLineNum; - } + public int getFixEndPosition() + { + return fixEndPosition; + } - public void setBugStartLineNum(int bugStartLineNum) { - this.bugStartLineNum = bugStartLineNum; - } + private int bugEndPosition; - public int getBugEndLineNum() { - return bugEndLineNum; - } + private int fixEndPosition; - public void setBugEndLineNum(int bugEndLineNum) { - this.bugEndLineNum = bugEndLineNum; - } + public ITree getNode() + { + return node; + } - public int getFixStartLineNum() { - return fixStartLineNum; - } + public void setNode(ITree node) + { + this.node = node; + } - public void setFixStartLineNum(int fixStartLineNum) { - this.fixStartLineNum = fixStartLineNum; - } + public void setAstNodeType(String astNodeType) + { + this.astNodeType = astNodeType; + } - public int getFixEndLineNum() { - return fixEndLineNum; - } + public String getAstNodeType() + { + return astNodeType; + } - public void setFixEndLineNum(int fixEndLineNum) { - this.fixEndLineNum = fixEndLineNum; - } + public Action getAction() + { + return action; + } - public HierarchicalActionSet getParent() { - return parent; - } + public void setAction(Action action) + { + this.action = action; + } - public void setParent(HierarchicalActionSet parent) { - this.parent = parent; - } + public Action getParentAction() + { + return parentAction; + } - public List getSubActions() { - return subActions; - } + public void setParentAction(Action parentAction) + { + this.parentAction = parentAction; + } - public void setSubActions(List subActions) { - this.subActions = subActions; - } + public String getActionString() + { + return actionString; + } + + public void setActionString(String actionString) + { + this.actionString = actionString; + + int atIndex = actionString.indexOf("@AT@") + 4; + int lengthIndex = actionString.indexOf("@LENGTH@"); + if (lengthIndex == -1) + { + this.startPosition = Integer.parseInt(actionString.substring(atIndex).trim()); + this.length = 0; + } + else + { + this.startPosition = Integer.parseInt(actionString.substring(atIndex, lengthIndex).trim()); + this.length = Integer.parseInt(actionString.substring(lengthIndex + 8).trim()); + } + + String nodeType = actionString.substring(0, actionString.indexOf("@@")); + nodeType = nodeType.substring(nodeType.indexOf(" ") + 1); + this.astNodeType = nodeType; + } + + public int getStartPosition() + { + return startPosition; + } + + public int getLength() + { + return length; + } + + 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 HierarchicalActionSet getParent() + { + return parent; + } + + public void setParent(HierarchicalActionSet parent) + { + this.parent = parent; + } + + public List getSubActions() + { + return subActions; + } + + public void setSubActions(List subActions) + { + this.subActions = subActions; + } - public void setBugEndPosition(int bugEndPosition) { - this.bugEndPosition = bugEndPosition; - } + public void setBugEndPosition(int bugEndPosition) + { + this.bugEndPosition = bugEndPosition; + } + public void setFixEndPosition(int fixEndPosition) + { + this.fixEndPosition = fixEndPosition; + } - public void setFixEndPosition(int fixEndPosition) { - this.fixEndPosition = fixEndPosition; - } + @Override + public int compareTo(HierarchicalActionSet o) + { - @Override - public int compareTo(HierarchicalActionSet o) { + return this.startPosition.compareTo(o.startPosition);//this.action.compareTo(o.action); + } - return this.startPosition.compareTo(o.startPosition);//this.action.compareTo(o.action); - } + private final List strList = new ArrayList<>(); - private List strList = new ArrayList<>(); + public int getActionSize() + { + return strList.size(); + } - public int getActionSize(){ - return strList.size(); - } + @Override + public String toString() + { + String str = actionString; + if (strList.size() == 0) + { + strList.add(str); + for (HierarchicalActionSet actionSet : subActions) + { + actionSet.toString(); + List strList1 = actionSet.strList; + for (String str1 : strList1) + { + strList.add("---" + str1); + } + } + } + else + { + strList.clear(); + strList.add(str); + for (HierarchicalActionSet actionSet : subActions) + { + actionSet.toString(); + List strList1 = actionSet.strList; + for (String str1 : strList1) + { + strList.add("---" + str1); + } + } + } + + str = ""; + for (String str1 : strList) + { + str += str1 + "\n"; + } + + return str; + } - @Override - public String toString() { - String str = actionString; - if (strList.size() == 0) { - strList.add(str); - for (HierarchicalActionSet actionSet : subActions) { - actionSet.toString(); - List strList1 = actionSet.strList; - for (String str1 : strList1) { - strList.add("---" + str1); - } - } - } else { - strList.clear(); - strList.add(str); - for (HierarchicalActionSet actionSet : subActions) { - actionSet.toString(); - List strList1 = actionSet.strList; - for (String str1 : strList1) { - strList.add("---" + str1); - } - } - } - - str = ""; - for (String str1 : strList) { - str += str1 + "\n"; - } - - return str; - } - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouper.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouper.java index 9c8bd92..0bc5032 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouper.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouper.java @@ -5,7 +5,6 @@ import com.github.gumtreediff.tree.ITree; import edu.lu.uni.serval.utils.ASTNodeMap; import edu.lu.uni.serval.utils.ListSorter; - import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; @@ -13,323 +12,395 @@ import java.util.stream.Collectors; /** * Regroup GumTree results to a hierarchical construction. - * - * @author kui.liu * + * @author kui.liu */ -public class HierarchicalRegrouper { +public class HierarchicalRegrouper +{ - public List regroupGumTreeResults(List actions) { - /* - * First, sort actions by their positions. - */ - actions = new ListSorter(actions).sortAscending(); - - - /* - * Second, group actions by their positions. - */ - List actionSets = new ArrayList<>(); - HierarchicalActionSet actionSet = null; - - if(actions == null){ - return actionSets; - } - - 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 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); - } - } - } - - List reActionSets1 = new ArrayList<>(); - - ITree movDelNode = null; - - for(HierarchicalActionSet a:reActionSets){ - a = removeBlocks(a); - - HierarchicalActionSet hierarchicalActionSet1 = postOrder(a).stream().collect(Collectors.toList()).get(0); - Action action = hierarchicalActionSet1.getAction(); - if (hierarchicalActionSet1.getSubActions().size() == 0 && action instanceof Update){ - List collect = hierarchicalActionSet1.getNode().getChildren().stream().filter(x -> x.getType() == 14).collect(Collectors.toList()); -// if(hierarchicalActionSet1.getNode().getChildren().size() == 1){ - if(collect.size() == 1){ -// if(hierarchicalActionSet1.getNode().getChild(0).getType() == 14){ - if(collect.get(0).getType() == 14){ - continue; - } - } - } - else{ - Predicate predicate = x->x.getAction() instanceof Move; - List collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList()); - if(collect.size() == 1){ - HierarchicalActionSet hierarchicalActionSet = collect.get(0); - movDelNode = hierarchicalActionSet.getNode().getParent(); - reActionSets1.add(a); - continue; - } - - } - if( movDelNode != null){ - if(a.getNode().equals(movDelNode)){ - continue; - } - } - reActionSets1.add(a); - - } + public List regroupGumTreeResults(List actions) + { + /* + * First, sort actions by their positions. + */ + actions = new ListSorter(actions).sortAscending(); + /* + * Second, group actions by their positions. + */ + List actionSets = new ArrayList<>(); + HierarchicalActionSet actionSet = null; - return reActionSets1; -// return reActionSets; - } + if (actions == null) + { + return actionSets; + } + + 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 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); + } + } + } + + List reActionSets1 = new ArrayList<>(); + + ITree movDelNode = null; + + for (HierarchicalActionSet a : reActionSets) + { + a = removeBlocks(a); + + HierarchicalActionSet hierarchicalActionSet1 = postOrder(a).stream().collect(Collectors.toList()).get(0); + Action action = hierarchicalActionSet1.getAction(); + if (hierarchicalActionSet1.getSubActions().size() == 0 && action instanceof Update) + { + List collect = hierarchicalActionSet1.getNode().getChildren().stream().filter(x -> x.getType() == 14).collect(Collectors.toList()); + // if(hierarchicalActionSet1.getNode().getChildren().size() == 1){ + if (collect.size() == 1) + { + // if(hierarchicalActionSet1.getNode().getChild(0).getType() == 14){ + if (collect.get(0).getType() == 14) + { + continue; + } + } + } + else + { + Predicate predicate = x -> x.getAction() instanceof Move; + List collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList()); + if (collect.size() == 1) + { + HierarchicalActionSet hierarchicalActionSet = collect.get(0); + movDelNode = hierarchicalActionSet.getNode().getParent(); + reActionSets1.add(a); + continue; + } + + } + if (movDelNode != null) + { + if (a.getNode().equals(movDelNode)) + { + continue; + } + } + reActionSets1.add(a); + + } - private boolean isStatement(HierarchicalActionSet actSet){ - 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")) { - return true; - } - return false; - } - Predicate predicate = x-> isStatement(x); - - private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet){ - List subActions = actionSet.getSubActions(); + return reActionSets1; + // return reActionSets; + } + private boolean isStatement(HierarchicalActionSet actSet) + { + String astNodeType = actSet.getAstNodeType(); + return astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") || + astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") || + astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase"); + } - Action action = actionSet.getAction(); - if (subActions.size() == 1){ - HierarchicalActionSet subaction = subActions.get(0); + Predicate predicate = x -> isStatement(x); - List collect = postOrder(subaction).stream().filter(predicate).collect(Collectors.toList()); - if(collect.size() == 0){ - return actionSet; - } - boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); - if(!b){ - return actionSet; - } - Action action1 = subaction.getAction(); - if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { - subaction.setParent(null); - return removeBlocks(subaction); - - } - } - return actionSet; - - } - - public List postOrder(HierarchicalActionSet a) { - List trees = new ArrayList<>(); - getAllSubActions(a, trees); - return trees; - } - private void getAllSubActions(HierarchicalActionSet a,List as) { - - List subActions = a.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet s : subActions) { - getAllSubActions(s, as); - } - - } - as.add(a); -// List b = new ArrayList(); -// for (HierarchicalActionSet child: this.getSubActions()) -// b.add(child); -// return b; - } - private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) { - HierarchicalActionSet actionSet = new HierarchicalActionSet(); - actionSet.setAction(act); - actionSet.setActionString(parseAction(act.toString())); - actionSet.setParentAction(parentAct); - actionSet.setNode(act.getNode()); - actionSet.setParent(parent); - 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 (ASTNodeMap.map.containsKey(typeInt)) { - String type = ASTNodeMap.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 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 && startPosition + length <= startPosition2 + 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 && startPosition + length <= startPosition2 + length2)) { -// // when act is not the sub-set of action. -// return false; -// } -// } - return true; - } - - private void sortSubActions(HierarchicalActionSet actionSet) { - ListSorter sorter = new ListSorter(actionSet.getSubActions()); - List subActions = sorter.sortAscending(); - if (subActions != null) { - actionSet.setSubActions(subActions); - } - } - - private boolean addToAactionSet(Action act, Action parentAct, List 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 subActionSets = actionSet.getSubActions(); - if (subActionSets.size() > 0) { - boolean added = addToAactionSet(act, parentAct, subActionSets); - if (added) { - return true; - } else { - continue; - } - } - } - } - } - return false; - } - - private Action findParentAction(Action action, List actions) { - - ITree parent = action.getNode().getParent(); - if (action instanceof Addition) { - parent = ((Addition) action).getParent(); // parent in the fixed source code tree - } - - if (parent.getType() == 55) { - int type = action.getNode().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 - return null; - } + private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet) + { + List subActions = actionSet.getSubActions(); - } - - for (Action act : actions) { - if (act.getNode().equals(parent)) { - if (areRelatedActions(act, action)) { - return act; - } - } - } - return null; - } - - 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 INS, its children must be MOV or INS. - return false; - } - if (parent instanceof Insert && !(child instanceof Addition)) {// If action is DEL, its children must be DEL. - return false; - } - return true; - } + Action action = actionSet.getAction(); + if (subActions.size() == 1) + { + HierarchicalActionSet subaction = subActions.get(0); + + List collect = postOrder(subaction).stream().filter(predicate).collect(Collectors.toList()); + if (collect.size() == 0) + { + return actionSet; + } + boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); + if (!b) + { + return actionSet; + } + Action action1 = subaction.getAction(); + if (action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) + { + subaction.setParent(null); + return removeBlocks(subaction); + + } + } + return actionSet; + + } + + public List postOrder(HierarchicalActionSet a) + { + List trees = new ArrayList<>(); + getAllSubActions(a, trees); + return trees; + } + + private void getAllSubActions(HierarchicalActionSet a, List as) + { + + List subActions = a.getSubActions(); + if (subActions.size() != 0) + { + for (HierarchicalActionSet s : subActions) + { + getAllSubActions(s, as); + } + + } + as.add(a); + // List b = new ArrayList(); + // for (HierarchicalActionSet child: this.getSubActions()) + // b.add(child); + // return b; + } + + private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) + { + HierarchicalActionSet actionSet = new HierarchicalActionSet(); + actionSet.setAction(act); + actionSet.setActionString(parseAction(act.toString())); + actionSet.setParentAction(parentAct); + actionSet.setNode(act.getNode()); + actionSet.setParent(parent); + 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 (ASTNodeMap.map.containsKey(typeInt)) + { + String type = ASTNodeMap.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 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 && startPosition + length <= startPosition2 + 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 && startPosition + length <= startPosition2 + length2)) { + // // when act is not the sub-set of action. + // return false; + // } + // } + return true; + } + + private void sortSubActions(HierarchicalActionSet actionSet) + { + ListSorter sorter = new ListSorter(actionSet.getSubActions()); + List subActions = sorter.sortAscending(); + if (subActions != null) + { + actionSet.setSubActions(subActions); + } + } + + private boolean addToAactionSet(Action act, Action parentAct, List 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 subActionSets = actionSet.getSubActions(); + if (subActionSets.size() > 0) + { + boolean added = addToAactionSet(act, parentAct, subActionSets); + if (added) + { + return true; + } + else + { + continue; + } + } + } + } + } + return false; + } + + private Action findParentAction(Action action, List actions) + { + + ITree parent = action.getNode().getParent(); + if (action instanceof Addition) + { + parent = ((Addition) action).getParent(); // parent in the fixed source code tree + } + + if (parent.getType() == 55) + { + int type = action.getNode().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 + return null; + } + + + } + + for (Action act : actions) + { + if (act.getNode().equals(parent)) + { + if (areRelatedActions(act, action)) + { + return act; + } + } + } + return null; + } + + 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 INS, its children must be MOV or INS. + return false; + } + // If action is DEL, its children must be DEL. + return !(parent instanceof Insert) || child instanceof Addition; + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouperForC.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouperForC.java index a283b26..a446d37 100644 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouperForC.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/HierarchicalRegrouperForC.java @@ -6,594 +6,674 @@ import com.github.gumtreediff.gen.srcml.NodeMap_new; import com.github.gumtreediff.tree.ITree; import edu.lu.uni.serval.utils.ListSorter; - -import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * Regroup GumTree results to a hierarchical construction. - * - * @author kui.liu * + * @author kui.liu */ -public class HierarchicalRegrouperForC { +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 = com.compareTwoFilesWithGumTreeForCCode(cFile1, cFile2); -// List actionSet = new HierarchicalRegrouperForC().regroupGumTreeResults(action); -// System.out.println(actionSet); -// } - - List actionSets = new ArrayList<>(); - - public List regroupGumTreeResults(List actions) { - /* - * First, sort actions by their positions. - */ -// List actions = new ListSorter(actionsArgu).sortAscending(); -// if (actions == null) { -// actions = actionsArgu; -// } - - /* - * Second, group actions by their positions. - */ - HierarchicalActionSet actionSet = null; - for(Action act : actions){ -// if(act.getNode().getType() == 19 && act.getNode().getLabel().equals("")){ -// continue; -// } - 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); - } - } - } + // 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 = com.compareTwoFilesWithGumTreeForCCode(cFile1, cFile2); + // List actionSet = new HierarchicalRegrouperForC().regroupGumTreeResults(action); + // System.out.println(actionSet); + // } - /* - * Third, add the subActionSet to its parent ActionSet. - */ - List 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")) { - if (isStatement(actSet.getNode()) || actSet.getAstNodeType().equals("block")) { - reActionSets.add(actSet); - } -// } - } - } - List movDelNodes = new ArrayList<>(); - List reActionSets1 = new ArrayList<>(); + List actionSets = new ArrayList<>(); - if (reActionSets.size() == 0 && actionSets.size()==1 && actionSets.get(0).getAstNodeType().equals("block")){ - reActionSets.add(actionSets.get(0)); - } + public List regroupGumTreeResults(List actions) + { + /* + * First, sort actions by their positions. + */ + // List actions = new ListSorter(actionsArgu).sortAscending(); + // if (actions == null) { + // actions = actionsArgu; + // } - for(HierarchicalActionSet a:reActionSets) { - a = purifyActionSet(a); - List hierarchicalActionSets = divideBlocks(a); - if (hierarchicalActionSets != null) { - reActionSets1.addAll(hierarchicalActionSets); - } else { - reActionSets1.add(a); - } - } - if(reActionSets1.size() > 1 && reActionSets1.stream().anyMatch(p->p.getAction() instanceof Delete)) { - List reActionSets2 = new ArrayList<>(); - for (HierarchicalActionSet a : reActionSets1) { - Predicate predicate = x -> x.getAction() instanceof Move; - List collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList()); - if (collect.size() > 0) { - for (HierarchicalActionSet c : collect) { + /* + * Second, group actions by their positions. + */ + HierarchicalActionSet actionSet = null; + for (Action act : actions) + { + // if(act.getNode().getType() == 19 && act.getNode().getLabel().equals("")){ + // continue; + // } + Action parentAct = findParentAction(act, actions); - ITree movDelNode = c.getNode().getParent(); - movDelNodes.add(movDelNode); + 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); + } + } + } - } - reActionSets2.add(a); - continue; - }else if(a.getAction() instanceof Update){ - reActionSets2.add(a); - } + /* + * Third, add the subActionSet to its parent ActionSet. + */ + List 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")) { + if (isStatement(actSet.getNode()) || actSet.getAstNodeType().equals("block")) + { + reActionSets.add(actSet); + } + // } + } + } + List movDelNodes = new ArrayList<>(); + List reActionSets1 = new ArrayList<>(); + + if (reActionSets.size() == 0 && actionSets.size() == 1 && actionSets.get(0).getAstNodeType().equals("block")) + { + reActionSets.add(actionSets.get(0)); + } + + for (HierarchicalActionSet a : reActionSets) + { + a = purifyActionSet(a); + List hierarchicalActionSets = divideBlocks(a); + if (hierarchicalActionSets != null) + { + reActionSets1.addAll(hierarchicalActionSets); + } + else + { + reActionSets1.add(a); + } + } + if (reActionSets1.size() > 1 && reActionSets1.stream().anyMatch(p -> p.getAction() instanceof Delete)) + { + List reActionSets2 = new ArrayList<>(); + for (HierarchicalActionSet a : reActionSets1) + { + Predicate predicate = x -> x.getAction() instanceof Move; + List collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList()); + if (collect.size() > 0) + { + for (HierarchicalActionSet c : collect) + { + + ITree movDelNode = c.getNode().getParent(); + movDelNodes.add(movDelNode); + + } + reActionSets2.add(a); + continue; + } + else if (a.getAction() instanceof Update) + { + reActionSets2.add(a); + } - if (movDelNodes != null) { - if (movDelNodes.contains(a.getNode())) { -// if(a.getNode().equals(movDelNode)){ - continue; - } - } -// reActionSets1.add(a); + if (movDelNodes != null) + { + if (movDelNodes.contains(a.getNode())) + { + // if(a.getNode().equals(movDelNode)){ + continue; + } + } + // reActionSets1.add(a); - } - return reActionSets2; - } + } + return reActionSets2; + } + return reActionSets1; + // return reActionSets; + } - return reActionSets1; -// return reActionSets; - } - private HierarchicalActionSet purifyActionSet(HierarchicalActionSet actionSet){ - HierarchicalActionSet hierarchicalActionSet = removeBlocks(actionSet); -// hierarchicalActionSet = removeIFthenBlocks(hierarchicalActionSet); -// hierarchicalActionSet = removeParentForSingle(hierarchicalActionSet); -// hierarchicalActionSet = removeParentNode(hierarchicalActionSet); - return hierarchicalActionSet; -// return actionSet; - } + private HierarchicalActionSet purifyActionSet(HierarchicalActionSet actionSet) + { + HierarchicalActionSet hierarchicalActionSet = removeBlocks(actionSet); + // hierarchicalActionSet = removeIFthenBlocks(hierarchicalActionSet); + // hierarchicalActionSet = removeParentForSingle(hierarchicalActionSet); + // hierarchicalActionSet = removeParentNode(hierarchicalActionSet); + return hierarchicalActionSet; + // return actionSet; + } - private List divideBlocks(HierarchicalActionSet actionSet){ - if (actionSet.getAstNodeType().equals("block_content") ){ // || actionSet.getAstNodeType().equals("function")){ - List subActions = actionSet.getSubActions(); - if (subActions.size() > 1) { - boolean b = subActions.stream().allMatch(p -> NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, p.getAstNodeType()).size() == 1 && p.getAstNodeType().equals(subActions.get(0).getAstNodeType()) && p.getAction().getName().equals(subActions.get(0).getAction().getName())); - if (b ) { - subActions.stream().forEach(p -> p.setParent(null)); - List collect = subActions.stream().map(p -> purifyActionSet(p)).collect(Collectors.toList()); - return collect; - } + private List divideBlocks(HierarchicalActionSet actionSet) + { + if (actionSet.getAstNodeType().equals("block_content")) + { // || actionSet.getAstNodeType().equals("function")){ + List subActions = actionSet.getSubActions(); + if (subActions.size() > 1) + { + boolean b = subActions.stream().allMatch(p -> NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, p.getAstNodeType()).size() == 1 && p.getAstNodeType().equals(subActions.get(0).getAstNodeType()) && p.getAction().getName().equals(subActions.get(0).getAction().getName())); + if (b) + { + subActions.stream().forEach(p -> p.setParent(null)); + List collect = subActions.stream().map(p -> purifyActionSet(p)).collect(Collectors.toList()); + return collect; + } - } - } - return null; - } + } + } + return null; + } - public List postOrder(HierarchicalActionSet a) { - List trees = new ArrayList<>(); - getAllSubActions(a, trees); - return trees; - } - private void getAllSubActions(HierarchicalActionSet a,List as) { + public List postOrder(HierarchicalActionSet a) + { + List trees = new ArrayList<>(); + getAllSubActions(a, trees); + return trees; + } - List subActions = a.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet s : subActions) { - getAllSubActions(s, as); - } + private void getAllSubActions(HierarchicalActionSet a, List as) + { - } - as.add(a); -// List b = new ArrayList(); -// for (HierarchicalActionSet child: this.getSubActions()) -// b.add(child); -// return b; - } + List subActions = a.getSubActions(); + if (subActions.size() != 0) + { + for (HierarchicalActionSet s : subActions) + { + getAllSubActions(s, as); + } -// Predicate predicate = x-> isStatement(x); - Predicate predicate = x->NodeMap_new.getKeysByValue(NodeMap_new.StatementMap,x.getAstNodeType()).size() == 1 ; - Predicate predicate1 = x->!x.getAstNodeType().equals("block"); - Predicate predicate2 = x->!x.getAstNodeType().equals("block_content"); - private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet){ - List subActions = actionSet.getSubActions(); + } + as.add(a); + // List b = new ArrayList(); + // for (HierarchicalActionSet child: this.getSubActions()) + // b.add(child); + // return b; + } + + // Predicate predicate = x-> isStatement(x); + Predicate predicate = x -> NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, x.getAstNodeType()).size() == 1; + + Predicate predicate1 = x -> !x.getAstNodeType().equals("block"); + + Predicate predicate2 = x -> !x.getAstNodeType().equals("block_content"); + + private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet) + { + List subActions = actionSet.getSubActions(); + Action action = actionSet.getAction(); + if (subActions.size() == 1) + { + HierarchicalActionSet subaction = subActions.get(0); - Action action = actionSet.getAction(); - if (subActions.size() == 1){ - HierarchicalActionSet subaction = subActions.get(0); + List collect = postOrder(subaction).stream().filter(predicate.and(predicate1.and(predicate2))).collect(Collectors.toList()); + if (collect.size() == 0) + { + return actionSet; + } + boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); - List collect = postOrder(subaction).stream().filter(predicate.and(predicate1.and(predicate2))).collect(Collectors.toList()); - if(collect.size() == 0){ - return actionSet; - } - boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); + if (!b) + { + return actionSet; + } + Action action1 = subaction.getAction(); + if (action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) + { + subaction.setParent(null); + return removeBlocks(subaction); - if(!b ){ - return actionSet; - } - Action action1 = subaction.getAction(); - if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { - subaction.setParent(null); - return removeBlocks(subaction); + } + } + return actionSet; - } - } - return actionSet; + } + // Predicate predicate = x->NodeMap_new.getKeysByValue(NodeMap_new.StatementMap,x.getAstNodeType()).size() == 1 ; + // Predicate predicate1 = x->!x.getAstNodeType().equals("block"); + // Predicate predicate2 = x->!x.getAstNodeType().equals("block_content"); + //// Predicate predicate3 = p->p.getAction().getName().equals(subActions.get(0).getAction().getName())); + // private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet){ + // List subActions = actionSet.getSubActions(); + // + // + // + // Action action = actionSet.getAction(); + // if (subActions.size() == 1){ + // HierarchicalActionSet subaction = subActions.get(0); + //// if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ + //// return actionSet; + //// } + // List collect = postOrder(subaction).stream().filter(predicate.and(predicate1.and(predicate2))).collect(Collectors.toList()); + // if(collect.size() == 0){ + // return actionSet; + // } + // boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); + // if(!b){ + // return actionSet; + // } + // Action action1 = subaction.getAction(); + // //else,then,block + // if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { + // + //// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); + //// if (keysByValue != null && keysByValue.size() == 1) { + // + // subaction.setParent(null); + // return removeBlocks(subaction); + // + //// } + //// if(areRelatedActions(action,action1)) { + //// if (subaction.getAstNodeType().equals("block")) {//|| subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")){ + //// List subSubActions = subaction.getSubActions(); + //// if (subSubActions.size() == 1) { + //// + //// HierarchicalActionSet subsubsubAction = subSubActions.get(0); + //// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subsubsubAction.getAstNodeType()); + //// if (keysByValue != null && keysByValue.size() == 1) { + //// + //// subsubsubAction.setParent(null); + //// return removeBlocks(subsubsubAction); + //// + //// } + //// } + //// } + // } + // } + // return actionSet; + // + // } - } -// Predicate predicate = x->NodeMap_new.getKeysByValue(NodeMap_new.StatementMap,x.getAstNodeType()).size() == 1 ; -// Predicate predicate1 = x->!x.getAstNodeType().equals("block"); -// Predicate predicate2 = x->!x.getAstNodeType().equals("block_content"); -//// Predicate predicate3 = p->p.getAction().getName().equals(subActions.get(0).getAction().getName())); -// private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet){ -// List subActions = actionSet.getSubActions(); -// -// -// -// Action action = actionSet.getAction(); -// if (subActions.size() == 1){ -// HierarchicalActionSet subaction = subActions.get(0); -//// if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ -//// return actionSet; -//// } -// List collect = postOrder(subaction).stream().filter(predicate.and(predicate1.and(predicate2))).collect(Collectors.toList()); -// if(collect.size() == 0){ -// return actionSet; -// } -// boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); -// if(!b){ -// return actionSet; -// } -// Action action1 = subaction.getAction(); -// //else,then,block -// if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { -// -//// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); -//// if (keysByValue != null && keysByValue.size() == 1) { -// -// subaction.setParent(null); -// return removeBlocks(subaction); -// -//// } -//// if(areRelatedActions(action,action1)) { -//// if (subaction.getAstNodeType().equals("block")) {//|| subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")){ -//// List subSubActions = subaction.getSubActions(); -//// if (subSubActions.size() == 1) { -//// -//// HierarchicalActionSet subsubsubAction = subSubActions.get(0); -//// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subsubsubAction.getAstNodeType()); -//// if (keysByValue != null && keysByValue.size() == 1) { -//// -//// subsubsubAction.setParent(null); -//// return removeBlocks(subsubsubAction); -//// -//// } -//// } -//// } -// } -// } -// return actionSet; -// -// } + // private HierarchicalActionSet removeParentNode(HierarchicalActionSet actionSet){ + // List subActions = actionSet.getSubActions(); + // Action action = actionSet.getAction(); + // if (subActions.size() == 1) { + // HierarchicalActionSet subaction = subActions.get(0); + // if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ + // return actionSet; + // } + // Action action1 = subaction.getAction(); + // if (!action.getClass().equals(action1.getClass())) { + // List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); + // if (keysByValue != null && keysByValue.size() == 1) { + // subaction.setParent(null); + // return removeParentNode(subaction); + // + // } + // } + // + // } + // return actionSet; + // } + // + // private HierarchicalActionSet removeParentForSingle(HierarchicalActionSet actionSet){ + // List subActions = actionSet.getSubActions(); + // Action action = actionSet.getAction(); + // if (subActions.size() == 1){ + // HierarchicalActionSet subaction = subActions.get(0); + // if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ + // return actionSet; + // } + // //else,then,block + // Action action1 = subaction.getAction(); + // //else,then,block + // if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { + // + //// if(areRelatedActions(action,action1)) { + // if(!(subaction.getAstNodeType().equals("condition") || subaction.getAstNodeType().equals("init"))){ + // List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); + // if(keysByValue != null && keysByValue.size() ==1){ + // subaction.setParent(null); + // return removeParentForSingle(subaction); + // } + // } + // } + // } + // return actionSet; + // + // } + // private HierarchicalActionSet removeIFthenBlocks(HierarchicalActionSet actionSet){ + // List subActions = actionSet.getSubActions(); + // Action action = actionSet.getAction(); + // if (subActions.size() == 1){ + // HierarchicalActionSet subaction = subActions.get(0); + // //else,then,block + // Action action1 = subaction.getAction(); + // //else,then,block + // if(action.getClass().equals(action1.getClass())) { + //// if(areRelatedActions(action,action1)) { + // if (subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")) {//|| subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")){ + // List subSubActions = subaction.getSubActions(); + // if (subSubActions.size() == 1) { + // + // HierarchicalActionSet subsubsubAction = subSubActions.get(0); + // if(!postOrder(subsubsubAction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ + // return actionSet; + // } + // if (subsubsubAction.getAstNodeType().equals("block")) { + // List subActions1 = subsubsubAction.getSubActions(); + // if (subActions1.size() == 1) { + // HierarchicalActionSet hierarchicalActionSet = subActions1.get(0); + // List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, hierarchicalActionSet.getAstNodeType()); + // if (keysByValue != null && keysByValue.size() == 1) { + // hierarchicalActionSet.setParent(null); + // return removeBlocks(hierarchicalActionSet); + // + // } + // } + // } + // + // } + // } + // } + // } + // return actionSet; + // + // } -// private HierarchicalActionSet removeParentNode(HierarchicalActionSet actionSet){ -// List subActions = actionSet.getSubActions(); -// Action action = actionSet.getAction(); -// if (subActions.size() == 1) { -// HierarchicalActionSet subaction = subActions.get(0); -// if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ -// return actionSet; -// } -// Action action1 = subaction.getAction(); -// if (!action.getClass().equals(action1.getClass())) { -// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); -// if (keysByValue != null && keysByValue.size() == 1) { -// subaction.setParent(null); -// return removeParentNode(subaction); -// -// } -// } -// -// } -// return actionSet; -// } -// -// private HierarchicalActionSet removeParentForSingle(HierarchicalActionSet actionSet){ -// List subActions = actionSet.getSubActions(); -// Action action = actionSet.getAction(); -// if (subActions.size() == 1){ -// HierarchicalActionSet subaction = subActions.get(0); -// if(!postOrder(subaction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ -// return actionSet; -// } -// //else,then,block -// Action action1 = subaction.getAction(); -// //else,then,block -// if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { -// -//// if(areRelatedActions(action,action1)) { -// if(!(subaction.getAstNodeType().equals("condition") || subaction.getAstNodeType().equals("init"))){ -// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, subaction.getAstNodeType()); -// if(keysByValue != null && keysByValue.size() ==1){ -// subaction.setParent(null); -// return removeParentForSingle(subaction); -// } -// } -// } -// } -// return actionSet; -// -// } -// private HierarchicalActionSet removeIFthenBlocks(HierarchicalActionSet actionSet){ -// List subActions = actionSet.getSubActions(); -// Action action = actionSet.getAction(); -// if (subActions.size() == 1){ -// HierarchicalActionSet subaction = subActions.get(0); -// //else,then,block -// Action action1 = subaction.getAction(); -// //else,then,block -// if(action.getClass().equals(action1.getClass())) { -//// if(areRelatedActions(action,action1)) { -// if (subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")) {//|| subaction.getAstNodeType().equals("then") || subaction.getAstNodeType().equals("else")){ -// List subSubActions = subaction.getSubActions(); -// if (subSubActions.size() == 1) { -// -// HierarchicalActionSet subsubsubAction = subSubActions.get(0); -// if(!postOrder(subsubsubAction).stream().anyMatch(predicate.and(predicate1.and(predicate2)))){ -// return actionSet; -// } -// if (subsubsubAction.getAstNodeType().equals("block")) { -// List subActions1 = subsubsubAction.getSubActions(); -// if (subActions1.size() == 1) { -// HierarchicalActionSet hierarchicalActionSet = subActions1.get(0); -// List keysByValue = NodeMap_new.getKeysByValue(NodeMap_new.StatementMap, hierarchicalActionSet.getAstNodeType()); -// if (keysByValue != null && keysByValue.size() == 1) { -// hierarchicalActionSet.setParent(null); -// return removeBlocks(hierarchicalActionSet); -// -// } -// } -// } -// -// } -// } -// } -// } -// return actionSet; -// -// } + private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) + { + HierarchicalActionSet actionSet = new HierarchicalActionSet(); + actionSet.setAction(act); + actionSet.setActionString(parseAction(act.toString())); + actionSet.setParentAction(parentAct); + actionSet.setNode(act.getNode()); + actionSet.setParent(parent); + return actionSet; + } - private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) { - HierarchicalActionSet actionSet = new HierarchicalActionSet(); - actionSet.setAction(act); - actionSet.setActionString(parseAction(act.toString())); - actionSet.setParentAction(parentAct); - actionSet.setNode(act.getNode()); - actionSet.setParent(parent); - 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 (NodeMap_new.map.containsKey(typeInt)) + { + String type = NodeMap_new.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 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 (NodeMap_new.map.containsKey(typeInt)) { - String type = NodeMap_new.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 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 && startPosition + length <= startPosition2 + length2 - addToActionSets(actionSet, parentAct, actSet.getSubActions()); - } - } - } - } + private void addToActionSets(HierarchicalActionSet actionSet, Action parentAct, List actionSets) + { + Action act = actionSet.getAction(); + for (HierarchicalActionSet actSet : actionSets) + { + if (actSet.equals(actionSet)) + { + continue; + } + Action action = actSet.getAction(); - 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 && startPosition + length <= startPosition2 + length2)) { -// // when act is not the sub-set of action. -// return false; -// } -// } - return true; - } + 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 && startPosition + length <= startPosition2 + length2 + addToActionSets(actionSet, parentAct, actSet.getSubActions()); + } + } + } + } - private void sortSubActions(HierarchicalActionSet actionSet) { - ListSorter sorter = new ListSorter(actionSet.getSubActions()); - List subActions = sorter.sortAscending(); - if (subActions != null) { - actionSet.setSubActions(subActions); - } - } + 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 && startPosition + length <= startPosition2 + length2)) { + // // when act is not the sub-set of action. + // return false; + // } + // } + return true; + } - private boolean addToAactionSet(Action act, Action parentAct, List 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 subActionSets = actionSet.getSubActions(); - if (subActionSets.size() > 0) { - boolean added = addToAactionSet(act, parentAct, subActionSets); - if (added) { - return true; - } else { - continue; - } - } - } - } - } - return false; - } + private void sortSubActions(HierarchicalActionSet actionSet) + { + ListSorter sorter = new ListSorter(actionSet.getSubActions()); + List subActions = sorter.sortAscending(); + if (subActions != null) + { + actionSet.setSubActions(subActions); + } + } - private Action findParentAction(Action action, List actions) { + private boolean addToAactionSet(Action act, Action parentAct, List actionSets) + { + for (HierarchicalActionSet actionSet : actionSets) + { + Action action = actionSet.getAction(); - ITree parent = action.getNode().getParent(); - if (action instanceof Addition) { - parent = ((Addition) action).getParent(); // parent in the fixed source code tree - } + if (!areRelatedActions(action, act)) + { + continue; + } -// if (parent.getType() == 55) { -// int type = action.getNode().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 -// return null; -// } -// -// -// } + 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 subActionSets = actionSet.getSubActions(); + if (subActionSets.size() > 0) + { + boolean added = addToAactionSet(act, parentAct, subActionSets); + if (added) + { + return true; + } + else + { + continue; + } + } + } + } + } + return false; + } - for (Action act : actions) { - if (act instanceof Move){ - if(act.getNode().equals(action.getNode())){ - return act; - } - } - else if (act.getNode().equals(parent)) { - if (areRelatedActions(act, action)) { - return act; - } - } - } - return null; - } + private Action findParentAction(Action action, List actions) + { -// List newParentActions = new ArrayList<>(); -// //TODO -// private Action findParentAction(Action action, List 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) { + ITree parent = action.getNode().getParent(); + if (action instanceof Addition) + { + parent = ((Addition) action).getParent(); // parent in the fixed source code tree + } + + // if (parent.getType() == 55) { + // int type = action.getNode().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 + // return null; + // } + // + // + // } + + for (Action act : actions) + { + if (act instanceof Move) + { + if (act.getNode().equals(action.getNode())) + { + return act; + } + } + else if (act.getNode().equals(parent)) + { + if (areRelatedActions(act, action)) + { + return act; + } + } + } + return null; + } + + // List newParentActions = new ArrayList<>(); + // //TODO + // private Action findParentAction(Action action, List 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(); -// List collect = tree.getChildren().stream().filter(m -> m.getType() == 6).collect(Collectors.toList()); -// if (collect.size() > 0){ -// return true; -// } - if (NodeMap_new.StatementMap.containsKey(nodeType)){ - return true; - } -// else{ -// if((nodeType ==6) && tree.getParent().getType() == 1){ -// return true; -// } -// } + int nodeType = tree.getType(); + // List collect = tree.getChildren().stream().filter(m -> m.getType() == 6).collect(Collectors.toList()); + // if (collect.size() > 0){ + // return true; + // } + return NodeMap_new.StatementMap.containsKey(nodeType); + // else{ + // if((nodeType ==6) && tree.getParent().getType() == 1){ + // return true; + // } + // } -// if (nodeType == 11 || nodeType == 16 || nodeType == 18 || nodeType == 21 -// || nodeType == 22 || nodeType == 23 || nodeType == 24 || nodeType == 84 -// || 30 == nodeType || nodeType == 31 || nodeType == 32 || nodeType == 33 -// || nodeType == 34 || nodeType == 35 || nodeType == 36 || nodeType == 40 -// || nodeType == 41 || nodeType == 49 || nodeType == 73 || nodeType == 81 || nodeType == 80 || nodeType == 46 || nodeType == 60 -// ||nodeType == 62 || nodeType == 64 || nodeType == 45 || nodeType == 85 || nodeType == 86 || nodeType == 59 || nodeType == 27 || nodeType == 25 -// || nodeType == 26 || nodeType ==93 || nodeType == 37 || nodeType == 38 || nodeType == 39 || nodeType == 89) {// TODO -// return true; -// } - return false; - } + // if (nodeType == 11 || nodeType == 16 || nodeType == 18 || nodeType == 21 + // || nodeType == 22 || nodeType == 23 || nodeType == 24 || nodeType == 84 + // || 30 == nodeType || nodeType == 31 || nodeType == 32 || nodeType == 33 + // || nodeType == 34 || nodeType == 35 || nodeType == 36 || nodeType == 40 + // || nodeType == 41 || nodeType == 49 || nodeType == 73 || nodeType == 81 || nodeType == 80 || nodeType == 46 || nodeType == 60 + // ||nodeType == 62 || nodeType == 64 || nodeType == 45 || nodeType == 85 || nodeType == 86 || nodeType == 59 || nodeType == 27 || nodeType == 25 + // || nodeType == 26 || nodeType ==93 || nodeType == 37 || nodeType == 38 || nodeType == 39 || nodeType == 89) {// TODO + // return true; + // } + } - 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; - } + 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 action is INS, its children must be MOV or INS. + return !(parent instanceof Insert) || child instanceof Addition; + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/MessageFile.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/MessageFile.java index d774fd0..a9cd0e2 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/MessageFile.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/MessageFile.java @@ -2,46 +2,62 @@ package edu.lu.uni.serval.richedit.ediff; import java.io.File; -public class MessageFile { - - private File revFile; - private File prevFile; - private File diffEntryFile; - private File positionFile; +public class MessageFile +{ + + private final File revFile; + + private final File prevFile; + + private final File diffEntryFile; + + private File positionFile; + private String project; - private String project; - - public MessageFile(File revFile, File prevFile, File diffEntryFile,String project) { - super(); - this.revFile = revFile; - this.prevFile = prevFile; - this.diffEntryFile = diffEntryFile; - this.project = project; - } + public MessageFile(File revFile, File prevFile, File diffEntryFile, String project) + { + super(); + this.revFile = revFile; + this.prevFile = prevFile; + this.diffEntryFile = diffEntryFile; + this.project = project; + } - public String getProject() { return project;} + public String getProject() + { + return project; + } - public void setProject(String project) { this.project = project;} - public File getRevFile() { - return revFile; - } - - public File getPrevFile() { - return prevFile; - } - - public File getDiffEntryFile() { - return diffEntryFile; - } + public void setProject(String project) + { + this.project = project; + } - public File getPositionFile() { - return positionFile; - } + public File getRevFile() + { + return revFile; + } + + public File getPrevFile() + { + return prevFile; + } + + public File getDiffEntryFile() + { + return diffEntryFile; + } + + public File getPositionFile() + { + return positionFile; + } + + public void setPositionFile(File positionFile) + { + this.positionFile = positionFile; + } - public void setPositionFile(File positionFile) { - this.positionFile = positionFile; - } - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/Parser.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/Parser.java index 2309305..538574a 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/Parser.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/Parser.java @@ -6,55 +6,65 @@ import java.io.File; /** * Parse fix patterns with GumTree. - * - * @author kui.liu * + * @author kui.liu */ -public abstract class Parser implements ParserInterface { - - protected String astEditScripts = ""; // it will be used for fix patterns mining. - protected String patchesSourceCode = ""; // testing - protected String buggyTrees = ""; // Compute similarity for bug localization. - protected String sizes = ""; // fix patterns' selection before mining. - protected String tokensOfSourceCode = ""; // Compute similarity for bug localization. - protected String originalTree = ""; // Guide of generating patches. - protected String actionSets = ""; // Guide of generating patches. +public abstract class Parser implements ParserInterface +{ - public abstract void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath,String rootType,boolean isJava); - + protected String astEditScripts = ""; // it will be used for fix patterns mining. - @Override - public String getAstEditScripts() { - return astEditScripts; - } + protected String patchesSourceCode = ""; // testing - @Override - public String getPatchesSourceCode() { - return patchesSourceCode; - } + protected String buggyTrees = ""; // Compute similarity for bug localization. -// @Override -// public String getBuggyTrees() { -// return buggyTrees; -// } + protected String sizes = ""; // fix patterns' selection before mining. - @Override - public String getSizes() { - return sizes; - } + protected String tokensOfSourceCode = ""; // Compute similarity for bug localization. - @Override - public String getTokensOfSourceCode() { - return tokensOfSourceCode; - } + protected String originalTree = ""; // Guide of generating patches. -// @Override -// public String getOriginalTree() { -// return originalTree; -// } -// -// @Override -// public String getActionSets() { -// return actionSets; -// } + protected String actionSets = ""; // Guide of generating patches. + + public abstract void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava); + + + @Override + public String getAstEditScripts() + { + return astEditScripts; + } + + @Override + public String getPatchesSourceCode() + { + return patchesSourceCode; + } + + // @Override + // public String getBuggyTrees() { + // return buggyTrees; + // } + + @Override + public String getSizes() + { + return sizes; + } + + @Override + public String getTokensOfSourceCode() + { + return tokensOfSourceCode; + } + + // @Override + // public String getOriginalTree() { + // return originalTree; + // } + // + // @Override + // public String getActionSets() { + // return actionSets; + // } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/ParserInterface.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/ParserInterface.java index 1048eb4..f1f406d 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/ParserInterface.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/ParserInterface.java @@ -1,21 +1,22 @@ package edu.lu.uni.serval.richedit.ediff; -public interface ParserInterface { - +public interface ParserInterface +{ -// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile); - - public String getAstEditScripts(); - public String getPatchesSourceCode(); + // public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile); -// public String getBuggyTrees(); + String getAstEditScripts(); - public String getSizes(); + String getPatchesSourceCode(); - public String getTokensOfSourceCode(); + // public String getBuggyTrees(); -// public String getOriginalTree(); + String getSizes(); -// public String getActionSets(); + String getTokensOfSourceCode(); + + // public String getOriginalTree(); + + // public String getActionSets(); } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/RunnableParser.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/RunnableParser.java index 5bb4e52..3b7a934 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/RunnableParser.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/ediff/RunnableParser.java @@ -4,48 +4,61 @@ import redis.clients.jedis.JedisPool; import java.io.File; -public class RunnableParser implements Runnable { +public class RunnableParser implements Runnable +{ - private File prevFile; - private File revFile; - private File diffEntryFile; - private Parser parser; - private String project; - private JedisPool pool; - private String srcMLPath; - private String rootType; - boolean isJava; + private final File prevFile; - public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser) { - this.prevFile = prevFile; - this.revFile = revFile; - this.diffEntryFile = diffEntryFile; - this.parser = parser; - } + private final File revFile; - public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool) { - this.prevFile = prevFile; - this.revFile = revFile; - this.diffEntryFile = diffEntryFile; - this.parser = parser; - this.project = project; - this.pool = innerPool; - } + private final File diffEntryFile; - public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool,String srcMLPath,String rootType,boolean isJava) { - this.prevFile = prevFile; - this.revFile = revFile; - this.diffEntryFile = diffEntryFile; - this.parser = parser; - this.project = project; - this.pool = innerPool; - this.srcMLPath = srcMLPath; - this.rootType = rootType; - this.isJava = isJava; - } + private final Parser parser; - @Override - public void run() { - parser.parseFixPatterns(prevFile, revFile, diffEntryFile,project,pool,srcMLPath,rootType,isJava); - } + private String project; + + private JedisPool pool; + + private String srcMLPath; + + private String rootType; + + boolean isJava; + + public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser) + { + this.prevFile = prevFile; + this.revFile = revFile; + this.diffEntryFile = diffEntryFile; + this.parser = parser; + } + + public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool) + { + this.prevFile = prevFile; + this.revFile = revFile; + this.diffEntryFile = diffEntryFile; + this.parser = parser; + this.project = project; + this.pool = innerPool; + } + + public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava) + { + this.prevFile = prevFile; + this.revFile = revFile; + this.diffEntryFile = diffEntryFile; + this.parser = parser; + this.project = project; + this.pool = innerPool; + this.srcMLPath = srcMLPath; + this.rootType = rootType; + this.isJava = isJava; + } + + @Override + public void run() + { + parser.parseFixPatterns(prevFile, revFile, diffEntryFile, project, pool, srcMLPath, rootType, isJava); + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/CompareTrees.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/CompareTrees.java index 277f34d..21f6f79 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/CompareTrees.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/CompareTrees.java @@ -13,174 +13,83 @@ import redis.clients.jedis.JedisPool; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; import java.util.stream.Collectors; import java.util.stream.IntStream; - /** * Created by anilkoyuncu on 03/04/2018. */ -public class CompareTrees { +public class CompareTrees +{ + private static final Logger log = LoggerFactory.getLogger(CompareTrees.class); - private static Logger log = LoggerFactory.getLogger(CompareTrees.class); - - - public static void main(String redisPath, String portDumps, String dumpsName, String numOfWorkers) throws Exception { - - // shape /Users/anil.koyuncu/projects/test/richedit-core/python/data/redis ALLdumps-gumInput.rdb clusterl0-gumInputALL.rdb /Users/anil.koyuncu/projects/test/richedit-core/python/data/richEditScript - -// String portInner = "6380"; + public static void main(String redisPath, String portDumps, String dumpsName, String numOfWorkers) throws Exception + { String port = portDumps; //"6399"; CallShell cs = new CallShell(); - String cmd = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; - cmd = String.format(cmd, redisPath,dumpsName,Integer.valueOf(port)); + String cmd = "bash " + redisPath + "/" + "startServer.sh" + " %s %s %s"; + cmd = String.format(cmd, redisPath, dumpsName, Integer.valueOf(port)); log.info(cmd); - cs.runShell(cmd, port); + CallShell.runShell(cmd, port); -// String cmdInner = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; -// cmdInner = String.format(cmdInner, redisPath,compareDBName,Integer.valueOf(portInner)); -// log.info(cmdInner); -// cs.runShell(cmdInner, portInner); + final JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "localhost", Integer.valueOf(port), 20000000); -// String numOfWorkers = "100000000";//args[4]; -// String host = "localhost";//args[5]; -// -Djava.util.concurrent.ForkJoinPool.common.parallelism=256 - -// final JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), host,Integer.valueOf(portInner),20000000); - - final JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "localhost",Integer.valueOf(port),20000000); - -// List listOfPairs = AkkaTreeParser.getMessages(innerPool,Integer.valueOf(numOfWorkers)); HashMap filenames = getFilenames(outerPool); - String job= getLevel(outerPool); -// List listOfPairs = AkkaTreeParser.files2compare(outerPool); + String job = getLevel(outerPool); - -// ArrayList samePairs = new ArrayList<>(); ArrayList errorPairs = new ArrayList<>(); -// Integer numberOfWorkers = Integer.valueOf(numOfWorkers); -// final ExecutorService executor = Executors.newWorkStealingPool(numberOfWorkers); Long compare; - try (Jedis inner = outerPool.getResource()) { + try (Jedis inner = outerPool.getResource()) + { compare = inner.scard("compare"); } IntStream stream = IntStream.range(0, compare.intValue()); String finalJob = job; - ProgressBar.wrap(stream. - parallel(),"Task"). - - forEach(m -> - { - newCoreCompare(finalJob, errorPairs, filenames, outerPool); - } - ); - -// -// ArrayList> results = new ArrayList>(); -// for (int i = 0; i future = executor.submit(new RunnableCompare(job, errorPairs, filenames, outerPool, i)); -// results.add(future); -// } -// for (Future future : ProgressBar.wrap(results, "Comparing")){ -//// for (Future future:results){ -// try { -// // wait for task to complete -// future.get(); -// -// } catch (InterruptedException e) { -// -// e.printStackTrace(); -// } catch (ExecutionException e) { -// -// e.printStackTrace(); -// } -//// finally { -//// executor.shutdownNow(); -//// } -// } -// executor.shutdownNow(); - - - + ProgressBar.wrap(stream.parallel(), "Task").forEach(m -> + { + newCoreCompare(finalJob, errorPairs, filenames, outerPool); + } + ); log.info("End process"); } - -// public static class RunnableCompare implements Runnable { -// String job; -// ArrayList errorPairs; -// HashMap filenames; -// JedisPool outerPool; -// Integer threadID; -// -// public RunnableCompare(String treeType,ArrayList errorPairs, HashMap filenames,JedisPool outerPool,Integer threadID) { -// this.job = treeType; -// this.errorPairs = errorPairs; -// this.filenames = filenames; -// this.outerPool = outerPool; -// this.threadID = threadID; -// } -// -// @Override -// public void run() { -//// int dbsize = 1; -// boolean stop = true; -// while(stop) { -//// try (Jedis outer = outerPool.getResource()) { -//// dbsize = Math.toIntExact(outer.scard("compare")); -//// } -//// if (dbsize != 0){ -// stop = newCoreCompare(job, errorPairs, filenames, outerPool); -//// } -// } -// log.info("Completed worker {}",threadID); -// } -// } - - - public static boolean newCoreCompare( String treeType,ArrayList errorPairs, HashMap filenames,JedisPool outerPool ) { - + public static boolean newCoreCompare(String treeType, ArrayList errorPairs, HashMap filenames, JedisPool outerPool) + { String pairName = null; - try (Jedis outer = outerPool.getResource()) { + try (Jedis outer = outerPool.getResource()) + { pairName = outer.spop("compare"); -// } - if(pairName.equals("0")) - return true; - String matchKey = null; -// try { + // } + if (pairName.equals("0")) + { + return true; + } + String matchKey = null; String[] split = pairName.split("/"); - String i = split[1]; String j = split[2]; String keyName = split[0]; - matchKey = keyName + "/" + (String.valueOf(i)) + "/" + String.valueOf(j); + matchKey = keyName + "/" + (i) + "/" + j; - if (matchKey == null){ + if (matchKey == null) + { return false; } Map oldTreeString = EDiffHelper.getTreeString(keyName, i, outerPool, filenames); Map newTreeString = EDiffHelper.getTreeString(keyName, j, outerPool, filenames); - switch (treeType) { + switch (treeType) + { case "single": - - String oldShapeTree =oldTreeString.get("shapeTree"); - String newShapeTree =newTreeString.get("shapeTree"); + String oldShapeTree = oldTreeString.get("shapeTree"); + String newShapeTree = newTreeString.get("shapeTree"); String oldActionTree = oldTreeString.get("actionTree"); String newActionTree = newTreeString.get("actionTree"); @@ -189,12 +98,14 @@ public class CompareTrees { String newTargetTree = newTreeString.get("targetTree"); - if (oldShapeTree.equals(newShapeTree)) { - if (oldActionTree.equals(newActionTree)) { - if (oldTargetTree.equals(newTargetTree)) { -// samePairs.add(matchKey); - try (Jedis jedis = outerPool.getResource()) { -//// jedis.del(matchKey); + if (oldShapeTree.equals(newShapeTree)) + { + if (oldActionTree.equals(newActionTree)) + { + if (oldTargetTree.equals(newTargetTree)) + { + try (Jedis jedis = outerPool.getResource()) + { jedis.select(2); jedis.set(matchKey, "1"); } @@ -206,61 +117,54 @@ public class CompareTrees { String oldTokens = oldTreeString.get("tokens"); String newTokens = newTreeString.get("tokens"); -// EDiffHelper.getTokens(keyName, i, outerPool, filenames); -// newTree = EDiffHelper.getTokens(keyName, j, outerPool, innerPool); -// String oldTokens = EDiffHelper.getNames2(oldTree); -// String newTokens = EDiffHelper.getNames2(newTree); -// JaroWinklerDistance jwd = new JaroWinklerDistance(); -// -// Double overallSimi = Double.valueOf(0); -// - if (!(oldTokens.trim().isEmpty() || newTokens.trim().isEmpty())) { + if (!(oldTokens.trim().isEmpty() || newTokens.trim().isEmpty())) + { overallSimi = jwd.apply(oldTokens, newTokens); - } int retval = Double.compare(overallSimi, Double.valueOf(1)); - if (retval >= 0) { - try (Jedis jedis = outerPool.getResource()) { + if (retval >= 0) + { + try (Jedis jedis = outerPool.getResource()) + { jedis.select(3); jedis.set(matchKey, "1"); } - } return true; default: return true; -// break; } - }catch (Exception e) { + } + catch (Exception e) + { errorPairs.add(pairName); - if (pairName == null) - return false; + if (pairName == null) return false; log.debug("{} not comparable", pairName); } - return true; } + public static String getLevel(JedisPool innerPool) + { + HashMap fileMap = new HashMap(); - public static String getLevel(JedisPool innerPool){ - - - HashMap fileMap =new HashMap(); - - try (Jedis inner = innerPool.getResource()) { - while (!inner.ping().equals("PONG")){ + try (Jedis inner = innerPool.getResource()) + { + while (!inner.ping().equals("PONG")) + { log.info("wait"); } inner.select(1); String level = inner.get("level"); - switch (level){ + switch (level) + { case "l1": return "single"; case "l2": @@ -268,47 +172,29 @@ public class CompareTrees { default: return ""; } - - - - - - } } - public static HashMap getFilenames(JedisPool innerPool){ + public static HashMap getFilenames(JedisPool innerPool) + { + HashMap fileMap = new HashMap(); - - HashMap fileMap =new HashMap(); - - try (Jedis inner = innerPool.getResource()) { - while (!inner.ping().equals("PONG")){ + try (Jedis inner = innerPool.getResource()) + { + while (!inner.ping().equals("PONG")) + { log.info("wait"); } inner.select(1); Map filenames = inner.hgetAll("filenames"); - - for (Map.Entry stringStringEntry : filenames.entrySet().stream().collect(Collectors.toList())) { - fileMap.put(stringStringEntry.getKey(),stringStringEntry.getValue()); + for (Map.Entry stringStringEntry : filenames.entrySet().stream().collect(Collectors.toList())) + { + fileMap.put(stringStringEntry.getKey(), stringStringEntry.getValue()); } - - - - - } -// log.info("Getting results %d",fileMap.s); - return fileMap; + return fileMap; } - - - - - - - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/EnhancedASTDiff.java b/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/EnhancedASTDiff.java index dde75ba..bd4aa08 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/EnhancedASTDiff.java +++ b/richedit/src/main/java/edu/lu/uni/serval/richedit/jobs/EnhancedASTDiff.java @@ -1,9 +1,9 @@ package edu.lu.uni.serval.richedit.jobs; import edu.lu.uni.serval.richedit.ediff.EDiffHunkParser; -import edu.lu.uni.serval.utils.FileHelper; import edu.lu.uni.serval.richedit.ediff.MessageFile; import edu.lu.uni.serval.utils.CallShell; +import edu.lu.uni.serval.utils.FileHelper; import edu.lu.uni.serval.utils.PoolBuilder; import me.tongfei.progressbar.ProgressBar; import org.slf4j.Logger; @@ -25,7 +25,7 @@ import java.util.stream.Stream; public class EnhancedASTDiff { - private static Logger log = LoggerFactory.getLogger(EnhancedASTDiff.class); + private static final Logger log = LoggerFactory.getLogger(EnhancedASTDiff.class); public static void main(String inputPath, String redisPort, String dbDir, String chunkName, String srcMLPath, String hunkLimit, String[] projectList, String patchSize, String projectType, String srcPath) throws Exception @@ -35,19 +35,12 @@ public class EnhancedASTDiff CallShell cs = new CallShell(); String cmd = String.format("redis-server %s/redis.conf --dir %s --dbfilename redis.rdb --port %s --daemonize yes", srcPath, dbDir, redisPort); - // String cmd = "bash " + dbDir + "/" + "startServer.sh" + " %s %s %s"; - // cmd = String.format(cmd, dbDir, chunkName, Integer.valueOf(redisPort)); - - cs.runShell(cmd, redisPort); + CallShell.runShell(cmd, redisPort); JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1", Integer.valueOf(redisPort), 20000000); - boolean isJava = false; - if (projectType.equals("java")) - { - isJava = true; - } + boolean isJava = projectType.equals("java"); File folder = new File(inputPath); File[] listOfFiles = folder.listFiles(); if (listOfFiles == null) @@ -80,22 +73,17 @@ public class EnhancedASTDiff .collect(Collectors.toList()); } - String project = folder.getName(); List allMessageFiles = new ArrayList<>(); for (File target : folders) { - List msgFiles = getMessageFiles(target.toString() + "/", project, patchSize, isJava); //"/Users/anilkoyuncu/bugStudy/code/python/GumTreeInput/Apache/CAMEL/" - // msgFiles = msgFiles.subList(0,3000); - if (msgFiles == null) - { - continue; - } + if (msgFiles == null) + { + continue; + } allMessageFiles.addAll(msgFiles); - - } Map diffEntry; @@ -112,24 +100,12 @@ public class EnhancedASTDiff log.info("{} files to process ...", allMessageFiles.size()); } boolean finalIsJava = isJava; - ProgressBar.wrap(allMessageFiles.stream(). - parallel(), "Task"). - forEach(m -> + ProgressBar.wrap(allMessageFiles.stream().parallel(), "Task").forEach(m -> { EDiffHunkParser parser = new EDiffHunkParser(); parser.parseFixPatterns(m.getPrevFile(), m.getRevFile(), m.getDiffEntryFile(), project, innerPool, srcMLPath, hunkLimit, finalIsJava); } ); - - - // allMessageFiles.stream(). - // parallel(). - // forEach(m -> - // { - // EDiffHunkParser parser = new EDiffHunkParser(); - // parser.parseFixPatterns(m.getPrevFile(), m.getRevFile(), m.getDiffEntryFile(), project, innerPool, srcMLPath, hunkLimit, finalIsJava); - // } - // ); } @@ -141,19 +117,11 @@ public class EnhancedASTDiff File[] revFiles = revFilesPath.listFiles(); if (revFiles != null) { - // List collect = Arrays.stream(revFiles).filter(x -> x.getName().startsWith("0a2756_7598f8_components#camel-cxf#src#main#java#org#apache#camel#component#cxf#CxfHeaderFilterStrategy")) - // .collect(Collectors.toList());// project folders List msgFiles = new ArrayList<>(); for (File revFile : revFiles) { - // for (File revFile : collect) { String fileName = revFile.getName(); - File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file - // if (isJava){ - // fileName = fileName.replace(".java",".txt"); - // }else{ - // fileName = fileName + ".txt"; - // } + File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName); // previous file fileName = fileName + ".txt"; File diffentryFile = new File(gumTreeInput + "DiffEntries/" + fileName); // DiffEntry file String s = FileHelper.readFile(diffentryFile); @@ -161,19 +129,11 @@ public class EnhancedASTDiff Pattern pattern = Pattern.compile("^[\\+|\\-]\\s*", Pattern.MULTILINE); Matcher matcher = pattern.matcher(s); int count = 0; - while (matcher.find()) - { - count++; - } - if (count >= Integer.valueOf(patchSize)) - // if(count>201) - { - continue; - } - // if(FileHelper.readFile(diffentryFile).split("@@\\s\\-\\d+,*\\d*\\s\\+\\d+,*\\d*\\s@@").length > 2) - // continue; - - // String datasetName = project; + while (matcher.find()) + { + count++; + } + if (count >= Integer.valueOf(patchSize)) continue; String[] split1 = diffentryFile.getParent().split(datasetName); String root = split1[0]; String pj = split1[1].split("/")[1]; @@ -181,16 +141,10 @@ public class EnhancedASTDiff MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile, pj); msgFiles.add(msgFile); - } return msgFiles; } - else - { - return null; - } + else return null; } - - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/ASTNodeMap.java b/richedit/src/main/java/edu/lu/uni/serval/utils/ASTNodeMap.java index c2d5bf8..c1d8534 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/ASTNodeMap.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/ASTNodeMap.java @@ -3,114 +3,114 @@ package edu.lu.uni.serval.utils; import java.util.HashMap; import java.util.Map; -public class ASTNodeMap { - - public static Map map; - - static { - map = new HashMap(); - map.put(-3, "Instanceof"); - map.put(-2, "New"); - map.put(-1, "Operator"); - map.put(0, "ASTNode"); - map.put(1, "AnonymousClassDeclaration"); - map.put(2, "ArrayAccess"); - map.put(3, "ArrayCreation"); - map.put(4, "ArrayInitializer"); - map.put(5, "ArrayType"); - map.put(6, "AssertStatement"); - map.put(7, "Assignment"); - map.put(8, "Block"); - map.put(9, "BooleanLiteral"); - map.put(10, "BreakStatement"); - map.put(11, "CastExpression"); - map.put(12, "CatchClause"); - map.put(13, "CharacterLiteral"); - map.put(14, "ClassInstanceCreation"); - map.put(15, "CompilationUnit"); - map.put(16, "ConditionalExpression"); - map.put(17, "ConstructorInvocation"); - map.put(18, "ContinueStatement"); - map.put(19, "DoStatement"); - map.put(20, "EmptyStatement"); - map.put(21, "ExpressionStatement"); - map.put(22, "FieldAccess"); - map.put(23, "FieldDeclaration"); - map.put(24, "ForStatement"); - map.put(25, "IfStatement"); - map.put(26, "ImportDeclaration"); - map.put(27, "InfixExpression"); - map.put(28, "Initializer"); - map.put(29, "Javadoc"); - map.put(30, "LabeledStatement"); - map.put(31, "MethodDeclaration"); - map.put(32, "MethodInvocation"); - map.put(33, "NullLiteral"); - map.put(34, "NumberLiteral"); - map.put(35, "PackageDeclaration"); - map.put(36, "ParenthesizedExpression"); - map.put(37, "PostfixExpression"); - map.put(38, "PrefixExpression"); - map.put(39, "PrimitiveType"); - map.put(40, "QualifiedName"); - map.put(41, "ReturnStatement"); - map.put(42, "SimpleName"); - map.put(43, "SimpleType"); - map.put(44, "SingleVariableDeclaration"); - map.put(45, "StringLiteral"); - map.put(46, "SuperConstructorInvocation"); - map.put(47, "SuperFieldAccess"); - map.put(48, "SuperMethodInvocation"); - map.put(49, "SwitchCase"); - map.put(50, "SwitchStatement"); - map.put(51, "SynchronizedStatement"); - map.put(52, "ThisExpression"); - map.put(53, "ThrowStatement"); - map.put(54, "TryStatement"); - map.put(55, "TypeDeclaration"); - map.put(56, "TypeDeclarationStatement"); - map.put(57, "TypeLiteral"); - map.put(58, "VariableDeclarationExpression"); - map.put(59, "VariableDeclarationFragment"); - map.put(60, "VariableDeclarationStatement"); - map.put(61, "WhileStatement"); - map.put(62, "InstanceofExpression"); - map.put(63, "LineComment"); - map.put(64, "BlockComment"); - map.put(65, "TagElement"); - map.put(66, "TextElement"); - map.put(67, "MemberRef"); - map.put(68, "MethodRef"); - map.put(69, "MethodRefParameter"); - map.put(70, "EnhancedForStatement"); - map.put(71, "EnumDeclaration"); - map.put(72, "EnumConstantDeclaration"); - map.put(73, "TypeParameter"); - map.put(74, "ParameterizedType"); - map.put(75, "QualifiedType"); - map.put(76, "WildcardType"); - map.put(77, "NormalAnnotation"); - map.put(78, "MarkerAnnotation"); - map.put(79, "SingleMemberAnnotation"); - map.put(80, "MemberValuePair"); - map.put(81, "AnnotationTypeDeclaration"); - map.put(82, "AnnotationTypeMemberDeclaration"); - map.put(83, "Modifier"); - map.put(84, "UnionType"); - map.put(85, "Dimension"); - map.put(86, "LambdaExpression"); - map.put(87, "IntersectionType"); - map.put(88, "NameQualifiedType"); - map.put(89, "CreationReference"); - map.put(90, "ExpressionMethodReference"); - map.put(91, "SuperMethodReference"); - map.put(92, "TypeMethodReference"); +public class ASTNodeMap +{ + public static Map map; + static + { + map = new HashMap<>(); + map.put(-3, "Instanceof"); + map.put(-2, "New"); + map.put(-1, "Operator"); + map.put(0, "ASTNode"); + map.put(1, "AnonymousClassDeclaration"); + map.put(2, "ArrayAccess"); + map.put(3, "ArrayCreation"); + map.put(4, "ArrayInitializer"); + map.put(5, "ArrayType"); + map.put(6, "AssertStatement"); + map.put(7, "Assignment"); + map.put(8, "Block"); + map.put(9, "BooleanLiteral"); + map.put(10, "BreakStatement"); + map.put(11, "CastExpression"); + map.put(12, "CatchClause"); + map.put(13, "CharacterLiteral"); + map.put(14, "ClassInstanceCreation"); + map.put(15, "CompilationUnit"); + map.put(16, "ConditionalExpression"); + map.put(17, "ConstructorInvocation"); + map.put(18, "ContinueStatement"); + map.put(19, "DoStatement"); + map.put(20, "EmptyStatement"); + map.put(21, "ExpressionStatement"); + map.put(22, "FieldAccess"); + map.put(23, "FieldDeclaration"); + map.put(24, "ForStatement"); + map.put(25, "IfStatement"); + map.put(26, "ImportDeclaration"); + map.put(27, "InfixExpression"); + map.put(28, "Initializer"); + map.put(29, "Javadoc"); + map.put(30, "LabeledStatement"); + map.put(31, "MethodDeclaration"); + map.put(32, "MethodInvocation"); + map.put(33, "NullLiteral"); + map.put(34, "NumberLiteral"); + map.put(35, "PackageDeclaration"); + map.put(36, "ParenthesizedExpression"); + map.put(37, "PostfixExpression"); + map.put(38, "PrefixExpression"); + map.put(39, "PrimitiveType"); + map.put(40, "QualifiedName"); + map.put(41, "ReturnStatement"); + map.put(42, "SimpleName"); + map.put(43, "SimpleType"); + map.put(44, "SingleVariableDeclaration"); + map.put(45, "StringLiteral"); + map.put(46, "SuperConstructorInvocation"); + map.put(47, "SuperFieldAccess"); + map.put(48, "SuperMethodInvocation"); + map.put(49, "SwitchCase"); + map.put(50, "SwitchStatement"); + map.put(51, "SynchronizedStatement"); + map.put(52, "ThisExpression"); + map.put(53, "ThrowStatement"); + map.put(54, "TryStatement"); + map.put(55, "TypeDeclaration"); + map.put(56, "TypeDeclarationStatement"); + map.put(57, "TypeLiteral"); + map.put(58, "VariableDeclarationExpression"); + map.put(59, "VariableDeclarationFragment"); + map.put(60, "VariableDeclarationStatement"); + map.put(61, "WhileStatement"); + map.put(62, "InstanceofExpression"); + map.put(63, "LineComment"); + map.put(64, "BlockComment"); + map.put(65, "TagElement"); + map.put(66, "TextElement"); + map.put(67, "MemberRef"); + map.put(68, "MethodRef"); + map.put(69, "MethodRefParameter"); + map.put(70, "EnhancedForStatement"); + map.put(71, "EnumDeclaration"); + map.put(72, "EnumConstantDeclaration"); + map.put(73, "TypeParameter"); + map.put(74, "ParameterizedType"); + map.put(75, "QualifiedType"); + map.put(76, "WildcardType"); + map.put(77, "NormalAnnotation"); + map.put(78, "MarkerAnnotation"); + map.put(79, "SingleMemberAnnotation"); + map.put(80, "MemberValuePair"); + map.put(81, "AnnotationTypeDeclaration"); + map.put(82, "AnnotationTypeMemberDeclaration"); + map.put(83, "Modifier"); + map.put(84, "UnionType"); + map.put(85, "Dimension"); + map.put(86, "LambdaExpression"); + map.put(87, "IntersectionType"); + map.put(88, "NameQualifiedType"); + map.put(89, "CreationReference"); + map.put(90, "ExpressionMethodReference"); + map.put(91, "SuperMethodReference"); + map.put(92, "TypeMethodReference"); - map.put(100,"Insert"); - map.put(101,"Update"); - map.put(102,"Delete"); - map.put(103,"Move"); - map.put(104,"NoChange"); - } + map.put(100, "Insert"); + map.put(101, "Update"); + map.put(102, "Delete"); + map.put(103, "Move"); + map.put(104, "NoChange"); + } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/CallShell.java b/richedit/src/main/java/edu/lu/uni/serval/utils/CallShell.java index 6721ef4..54392cc 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/CallShell.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/CallShell.java @@ -1,9 +1,5 @@ package edu.lu.uni.serval.utils; -/** - * Created by anilkoyuncu on 17/04/2018. - */ - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,109 +8,83 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.concurrent.TimeUnit; -public class CallShell { - - private static Logger log = LoggerFactory.getLogger(CallShell.class); - - - - public void runShell(String command) throws Exception { +/** + * Created by anilkoyuncu on 17/04/2018. + */ +public class CallShell +{ + private static final Logger log = LoggerFactory.getLogger(CallShell.class); + public void runShell(String command) throws Exception + { Process process = Runtime.getRuntime().exec(command); - BufferedReader reader = new BufferedReader(new InputStreamReader( - process.getInputStream())); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String s; - while ((s = reader.readLine()) != null) { + while ((s = reader.readLine()) != null) + { System.out.println("Script output: " + s); } - } - public static void runShell(String command, String port) throws Exception { - + public static void runShell(String command, String port) throws Exception + { log.trace(command); Process process = Runtime.getRuntime().exec(command); - BufferedReader reader = new BufferedReader(new InputStreamReader( - process.getInputStream())); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String s; - while ((s = reader.readLine()) != null) { -// System.out.println("Script output: " + s); + while ((s = reader.readLine()) != null) + { log.trace("Script output: " + s); } -// Thread.sleep(Integer.valueOf(serverWait)); String cmd = "redis-cli -p %s ping"; - String cmd1 = String.format(cmd,Integer.valueOf(port)); + String cmd1 = String.format(cmd, Integer.valueOf(port)); runPing(cmd1); - - } - public static void runPing(String command) throws Exception { -try{ - StringBuffer output = new StringBuffer(); - Process process = Runtime.getRuntime().exec(command); - BufferedReader reader = new BufferedReader(new InputStreamReader( - process.getInputStream())); - String s; + public static void runPing(String command) throws Exception + { + try + { + StringBuffer output = new StringBuffer(); + Process process = Runtime.getRuntime().exec(command); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String s; - BufferedReader stdError = new BufferedReader(new - InputStreamReader(process.getErrorStream())); + BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - - // read the output from the command -// System.out.println("Here is the standard output of the command:\n"); -// while ((s = reader.readLine()) != null && !s.equals("PONG")) { -// System.out.println(s); -// } - -// while(true){ s = reader.readLine(); - if(s !=null && s.equals("PONG")){ -// System.out.println(s); + if (s != null && s.equals("PONG")) + { log.trace(s); - - }else{ + } + else + { String e; - if((e = stdError.readLine()) == null) { + if ((e = stdError.readLine()) == null) + { TimeUnit.MINUTES.sleep(1); runPing(command); - }else{ + } + else + { TimeUnit.MINUTES.sleep(1); System.out.println(e); - } } -// System.out.println(s); -// } - // read any errors from the attempted command -// System.out.println("Here is the standard error of the command (if any):\n"); - while ((s = stdError.readLine()) != null) { - System.out.print(s); + while ((s = stdError.readLine()) != null) + System.out.print(s); + } + catch (IOException e) + { + System.out.println("exception happened - here's what I know: "); + e.printStackTrace(); + System.exit(-1); } - -// System.exit(0); } - catch (IOException e) { - System.out.println("exception happened - here's what I know: "); - e.printStackTrace(); - System.exit(-1); - } - - } - - 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","6380"); - } - - - - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/ClusterToPattern.java b/richedit/src/main/java/edu/lu/uni/serval/utils/ClusterToPattern.java index e2d5fc7..c4c74f9 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/ClusterToPattern.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/ClusterToPattern.java @@ -6,44 +6,45 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -public class ClusterToPattern { - private static Logger log = LoggerFactory.getLogger(ClusterToPattern.class); +public class ClusterToPattern +{ + private static final Logger log = LoggerFactory.getLogger(ClusterToPattern.class); - public static void main(String port,String redisPath, String dumpsName, String parameter) throws Exception { + public static void main(String port, String redisPath, String dumpsName, String parameter) throws Exception + { CallShell cs = new CallShell(); - String cmd = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; - cmd = String.format(cmd, redisPath,dumpsName,Integer.valueOf(port)); + String cmd = "bash " + redisPath + "/" + "startServer.sh" + " %s %s %s"; + cmd = String.format(cmd, redisPath, dumpsName, Integer.valueOf(port)); log.trace(cmd); - cs.runShell(cmd, port); + CallShell.runShell(cmd, port); String host = "localhost";//args[5]; - final JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), host,Integer.valueOf(port),20000000); + final JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), host, Integer.valueOf(port), 20000000); String export = export(parameter, outerPool); System.out.println(export); } - private static String export(String filename,JedisPool outerPool){ + private static String export(String filename, JedisPool outerPool) + { - try (Jedis outer = outerPool.getResource()) { - while (!outer.ping().equals("PONG")) { + try (Jedis outer = outerPool.getResource()) + { + while (!outer.ping().equals("PONG")) + { log.info("wait"); } -// byte[] s = outer.hget("dump".getBytes(), filename.getBytes()); -// HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); -// if (actionSet == null){ -// throw new Error(filename +" not found"); -// } - String s1 = outer.hget("dump",filename); -// outer.close(); + // byte[] s = outer.hget("dump".getBytes(), filename.getBytes()); + // HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); + // if (actionSet == null){ + // throw new Error(filename +" not found"); + // } + String s1 = outer.hget("dump", filename); + // outer.close(); return s1; } - - - - } diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/EDiffHelper.java b/richedit/src/main/java/edu/lu/uni/serval/utils/EDiffHelper.java index 503cdd0..a1417bb 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/EDiffHelper.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/EDiffHelper.java @@ -21,57 +21,67 @@ import java.util.stream.Collectors; /** * Created by anilkoyuncu on 17/09/2018. */ -public class EDiffHelper { - private static Logger log = LoggerFactory.getLogger(EDiffHelper.class); +public class EDiffHelper +{ + private static final 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 ); + 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(); + 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 { + public static String toString(Serializable o) throws IOException + { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream( baos ); - oos.writeObject( o ); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); oos.close(); return Base64.getEncoder().encodeToString(baos.toByteArray()); } - public static Object fromByteArray( byte[] data ) throws IOException, - ClassNotFoundException { -// byte [] data = Base64.getDecoder().decode( s ); + public static Object fromByteArray(byte[] data) throws IOException, + ClassNotFoundException + { + // byte [] data = Base64.getDecoder().decode( s ); ObjectInputStream ois = new ObjectInputStream( - new ByteArrayInputStream( data ) ); - Object o = ois.readObject(); + new ByteArrayInputStream(data)); + Object o = ois.readObject(); ois.close(); return o; } + /** Write the object to a Base64 string. */ - public static byte[] toByteArray(Serializable o ) throws IOException { + public static byte[] toByteArray(Serializable o) throws IOException + { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream( baos ); - oos.writeObject( o ); + ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(o); oos.close(); return baos.toByteArray(); } - public static byte[] commonsSerialize(Serializable o){ + public static byte[] commonsSerialize(Serializable o) + { return SerializationUtils.serialize(o); } - public static Object commonsDeserialize(byte[] data){ + public static Object commonsDeserialize(byte[] data) + { return SerializationUtils.deserialize(data); } - public static ITree getTokenTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc,boolean isJava){ + public static ITree getTokenTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc, boolean isJava) + { int newType = 0; @@ -80,36 +90,46 @@ public class EDiffHelper { String label = actionSet.getAction().toString(); Map nodeMap; - if(isJava){ + if (isJava) + { nodeMap = ASTNodeMap.map; - }else{ + } + else + { nodeMap = NodeMap_new.map; } -// List keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType); + // List keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType); List keysByValue = getKeysByValue(nodeMap, astNodeType); - if(keysByValue.size() != 1){ + if (keysByValue.size() != 1) + { log.error("More than 1"); } newType = keysByValue.get(0); - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { //root parent = tc.createTree(newType, label, null); tc.setRoot(parent); - }else{ + } + else + { children = tc.createTree(newType, label, null); children.setParentAndUpdateChildren(parent); } List subActions = actionSet.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet subAction : subActions) { + if (subActions.size() != 0) + { + for (HierarchicalActionSet subAction : subActions) + { - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { children = parent; } - getTokenTree(subAction,children,null,tc,isJava); + getTokenTree(subAction, children, null, tc, isJava); } @@ -119,66 +139,83 @@ public class EDiffHelper { } - - public static ITree getTargetTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc,boolean isJava){ + public static ITree getTargetTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc, boolean isJava) + { int newType = 0; String astNodeType = null; Map nodeMap; - if(isJava){ - nodeMap = ASTNodeMap.map; - }else{ + if (isJava) + { + nodeMap = ASTNodeMap.map; + } + else + { nodeMap = NodeMap_new.map; } Action action = actionSet.getAction(); - if (action instanceof Update){ + if (action instanceof Update) + { astNodeType = actionSet.getAstNodeType(); List keysByValue = getKeysByValue(nodeMap, astNodeType); - if(keysByValue.size() != 1){ + if (keysByValue.size() != 1) + { log.error("More than 1"); } newType = keysByValue.get(0); - }else if(action instanceof Insert){ - newType = ((Insert)action).getParent().getType(); - }else if(action instanceof Move){ - newType = ((Move)action).getParent().getType(); - }else if(action instanceof Delete){ + } + else if (action instanceof Insert) + { + newType = ((Insert) action).getParent().getType(); + } + else if (action instanceof Move) + { + newType = ((Move) action).getParent().getType(); + } + else if (action instanceof Delete) + { astNodeType = actionSet.getAstNodeType(); List keysByValue = getKeysByValue(nodeMap, astNodeType); - if(keysByValue.size() != 1){ + if (keysByValue.size() != 1) + { log.error("More than 1"); } newType = keysByValue.get(0); } - - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { //root -// parent = new Tree(newType,""); + // parent = new Tree(newType,""); parent = tc.createTree(newType, "", null); tc.setRoot(parent); - }else{ -// children = new Tree(newType,""); -// parent.addChild(children); + } + else + { + // children = new Tree(newType,""); + // parent.addChild(children); children = tc.createTree(newType, "", null); children.setParentAndUpdateChildren(parent); } List subActions = actionSet.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet subAction : subActions) { + if (subActions.size() != 0) + { + for (HierarchicalActionSet subAction : subActions) + { - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { children = parent; } - getTargetTree(subAction,children,null,tc,isJava); + getTargetTree(subAction, children, null, tc, isJava); } @@ -187,43 +224,54 @@ public class EDiffHelper { return parent; } - public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc,boolean isJava){ + public static ITree getASTTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc, boolean isJava) + { int newType = 0; Map nodeMap; - if(isJava){ + if (isJava) + { nodeMap = ASTNodeMap.map; - }else{ + } + else + { nodeMap = NodeMap_new.map; } String astNodeType = actionSet.getAstNodeType(); List keysByValue = getKeysByValue(nodeMap, astNodeType); - if(keysByValue.size() != 1){ + if (keysByValue.size() != 1) + { log.error("More than 1"); } newType = keysByValue.get(0); - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { //root -// parent = new Tree(newType,""); + // parent = new Tree(newType,""); parent = tc.createTree(newType, "", null); tc.setRoot(parent); - }else{ -// children = new Tree(newType,""); -// parent.addChild(children); + } + else + { + // children = new Tree(newType,""); + // parent.addChild(children); children = tc.createTree(newType, "", null); children.setParentAndUpdateChildren(parent); } List subActions = actionSet.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet subAction : subActions) { + if (subActions.size() != 0) + { + for (HierarchicalActionSet subAction : subActions) + { - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { children = parent; } - getASTTree(subAction,children,null,tc,isJava); + getASTTree(subAction, children, null, tc, isJava); } @@ -233,77 +281,79 @@ public class EDiffHelper { } - public static List getKeysByValue(Map map, E value) { + public static List getKeysByValue(Map map, E value) + { return map.entrySet() - .stream() - .filter(entry -> Objects.equals(entry.getValue(), value)) - .map(Map.Entry::getKey) - .collect(Collectors.toList()); + .stream() + .filter(entry -> Objects.equals(entry.getValue(), value)) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); } -// public static ITree getSimpliedTree(String prefix, 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(prefix+"-"+fn); -// inner.select(0); -// String s = inner.get(prefix.replace("-","/") +"/"+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 getSimpliedTree(String prefix, 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(prefix+"-"+fn); + // inner.select(0); + // String s = inner.get(prefix.replace("-","/") +"/"+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 getShapes(String prefix, String fn, JedisPool outerPool, HashMap filenames) { -// -// ITree tree = null; -// -// try (Jedis outer = outerPool.getResource()) { -// try { -// while (!outer.ping().equals("PONG")) { -// log.info("wait"); -// } -// String dist2load = filenames.get(prefix + "-" + fn); -// -// String key = prefix.replace("-", "/") + "/" + dist2load; -// -// byte[] s = outer.hget("dump".getBytes(), key.getBytes()); -// HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); -// -// tree = getShapeTree(actionSet); -// -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -// return tree; -// -// } + // public static ITree getShapes(String prefix, String fn, JedisPool outerPool, HashMap filenames) { + // + // ITree tree = null; + // + // try (Jedis outer = outerPool.getResource()) { + // try { + // while (!outer.ping().equals("PONG")) { + // log.info("wait"); + // } + // String dist2load = filenames.get(prefix + "-" + fn); + // + // String key = prefix.replace("-", "/") + "/" + dist2load; + // + // byte[] s = outer.hget("dump".getBytes(), key.getBytes()); + // HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); + // + // tree = getShapeTree(actionSet); + // + // } catch (Exception e) { + // e.printStackTrace(); + // } + // } + // return tree; + // + // } - public static ITree getShapeTree(HierarchicalActionSet actionSet,boolean isJava) { + public static ITree getShapeTree(HierarchicalActionSet actionSet, boolean isJava) + { ITree tree = null; ITree parent = null; ITree children = null; @@ -314,40 +364,49 @@ public class EDiffHelper { return tree; } - public static ITree getTokenTree(HierarchicalActionSet actionSet,boolean isJava) { + public static ITree getTokenTree(HierarchicalActionSet actionSet, boolean isJava) + { ITree tree = null; ITree parent = null; ITree children = null; TreeContext tc = new TreeContext(); - tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,isJava); + tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc, isJava); //tree.setParent(null); tc.validate(); return tree; } - public static ITree getTargets(HierarchicalActionSet actionSet,boolean isJava) { + public static ITree getTargets(HierarchicalActionSet actionSet, boolean isJava) + { ITree tree = null; - try { + try + { ITree parent = null; - ITree children =null; + ITree children = null; TreeContext tc = new TreeContext(); - tree = EDiffHelper.getTargetTree(actionSet, parent, children,tc,isJava); + tree = EDiffHelper.getTargetTree(actionSet, parent, children, tc, isJava); //tree.setParent(null); tc.validate(); - } catch (Exception e) { + } + catch (Exception e) + { e.printStackTrace(); } return tree; } - public static Map getTreeString(String prefix, String fn, JedisPool outerPool, HashMap filenames) { - try (Jedis outer = outerPool.getResource()) { - try { - while (!outer.ping().equals("PONG")) { + public static Map getTreeString(String prefix, String fn, JedisPool outerPool, HashMap filenames) + { + try (Jedis outer = outerPool.getResource()) + { + try + { + while (!outer.ping().equals("PONG")) + { log.info("wait"); } @@ -358,47 +417,50 @@ public class EDiffHelper { String key = split[0] + "/" + split[1] + "/" + dist2load; Map treeMap = outer.hgetAll(key); return treeMap; - }catch (Exception e) { + } + catch (Exception e) + { e.printStackTrace(); } } return null; } -// public static Pair getActions(String prefix, String fn, JedisPool outerPool, HashMap filenames) { -// -// -// ITree tree = null; -// HierarchicalActionSet actionSet = null; -// -// try (Jedis outer = outerPool.getResource()) { -// try { -// while (!outer.ping().equals("PONG")) { -// log.info("wait"); -// } -// -// -// String dist2load = filenames.get(prefix + "-" + fn); -// -// String[] split = prefix.split("-"); -// String key = split[0] + "/"+split[1]+"/" + dist2load; -// -// byte[] s = outer.hget("dump".getBytes(), key.getBytes()); -// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); -// -// -// tree = getActionTrees(actionSet); -// }catch (Exception e) { -// e.printStackTrace(); -// } -// } -// -// Pair pair = new Pair<>(tree,actionSet); -// return pair; -// -// } + // public static Pair getActions(String prefix, String fn, JedisPool outerPool, HashMap filenames) { + // + // + // ITree tree = null; + // HierarchicalActionSet actionSet = null; + // + // try (Jedis outer = outerPool.getResource()) { + // try { + // while (!outer.ping().equals("PONG")) { + // log.info("wait"); + // } + // + // + // String dist2load = filenames.get(prefix + "-" + fn); + // + // String[] split = prefix.split("-"); + // String key = split[0] + "/"+split[1]+"/" + dist2load; + // + // byte[] s = outer.hget("dump".getBytes(), key.getBytes()); + // actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); + // + // + // tree = getActionTrees(actionSet); + // }catch (Exception e) { + // e.printStackTrace(); + // } + // } + // + // Pair pair = new Pair<>(tree,actionSet); + // return pair; + // + // } - public static ITree getActionTrees(HierarchicalActionSet actionSet) { + public static ITree getActionTrees(HierarchicalActionSet actionSet) + { ITree tree = null; ITree parent = null; ITree children = null; @@ -409,95 +471,114 @@ public class EDiffHelper { return tree; } - public static void getLeaves(ITree tc){ + public static void getLeaves(ITree tc) + { int height = tc.getHeight(); - if(height == 0){ - log.info(tc.getLabel()); - }else{ + if (height == 0) + { + log.info(tc.getLabel()); + } + else + { List children = tc.getChildren(); - for (ITree child:children){ + for (ITree child : children) + { getLeaves(child); } } } -// public static ITree getTokens(String prefix, String fn, JedisPool outerPool, HashMap filenames) { -// -// -// ITree tree = null; -// -// HierarchicalActionSet actionSet = null; -// try (Jedis outer = outerPool.getResource()) { -// try { -// while (!outer.ping().equals("PONG")) { -// log.info("wait"); -// } -// String dist2load = filenames.get(prefix + "-" + fn); -// -// String[] split = prefix.split("-"); -// String key = split[0] + "/"+split[1]+"/" + dist2load; -// -// String s = outer.hget("dump", key); -// actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s); -// -//// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); -// -// ITree parent = null; -// ITree children = null; -// TreeContext tc = new TreeContext(); -// tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,is); -// tree.setParent(null); -// tc.validate(); -//// getLeaves(tree); -// -// -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -// -// return tree; -// } + // public static ITree getTokens(String prefix, String fn, JedisPool outerPool, HashMap filenames) { + // + // + // ITree tree = null; + // + // HierarchicalActionSet actionSet = null; + // try (Jedis outer = outerPool.getResource()) { + // try { + // while (!outer.ping().equals("PONG")) { + // log.info("wait"); + // } + // String dist2load = filenames.get(prefix + "-" + fn); + // + // String[] split = prefix.split("-"); + // String key = split[0] + "/"+split[1]+"/" + dist2load; + // + // String s = outer.hget("dump", key); + // actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s); + // + //// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); + // + // ITree parent = null; + // ITree children = null; + // TreeContext tc = new TreeContext(); + // tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,is); + // tree.setParent(null); + // tc.validate(); + //// getLeaves(tree); + // + // + // } catch (Exception e) { + // e.printStackTrace(); + // } + // } + // + // return tree; + // } - - - public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children,TreeContext tc){ + public static ITree getActionTree(HierarchicalActionSet actionSet, ITree parent, ITree children, TreeContext tc) + { int newType = 0; Action action = actionSet.getAction(); - if (action instanceof Update){ + if (action instanceof Update) + { newType = 101; - }else if(action instanceof Insert){ - newType =100; - }else if(action instanceof Move){ + } + else if (action instanceof Insert) + { + newType = 100; + } + else if (action instanceof Move) + { newType = 102; - }else if(action instanceof Delete){ - newType=103; - }else{ + } + else if (action instanceof Delete) + { + newType = 103; + } + else + { new Exception("unknow action"); } - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { //root parent = tc.createTree(newType, "", null); tc.setRoot(parent); - }else{ + } + else + { children = tc.createTree(newType, "", null); children.setParentAndUpdateChildren(parent); } List subActions = actionSet.getSubActions(); - if (subActions.size() != 0){ - for (HierarchicalActionSet subAction : subActions) { + if (subActions.size() != 0) + { + for (HierarchicalActionSet subAction : subActions) + { - if(actionSet.getParent() == null){ + if (actionSet.getParent() == null) + { children = parent; } - getActionTree(subAction,children,null,tc); + getActionTree(subAction, children, null, tc); } @@ -507,148 +588,175 @@ public class EDiffHelper { } - public static List retainLeaves(List trees) { + public static List retainLeaves(List trees) + { Iterator tIt = trees.iterator(); - while (tIt.hasNext()) { + while (tIt.hasNext()) + { ITree t = tIt.next(); - if (!t.isLeaf()) tIt.remove(); + if (!t.isLeaf()) + { + tIt.remove(); + } } return trees; } - public static String getTokenAtNode(ITree leaf,String token){ -// if (leaf.getParent() == null) { - String label = leaf.getLabel(); - String key = label.split(String.valueOf(leaf.getType()))[0]; - String group=""; - Pattern pattern; - java.util.regex.Matcher matcher; + public static String getTokenAtNode(ITree leaf, String token) + { + // if (leaf.getParent() == null) { + String label = leaf.getLabel(); + String key = label.split(String.valueOf(leaf.getType()))[0]; + String group = ""; + Pattern pattern; + java.util.regex.Matcher matcher; - List parents; - List actionList = new ArrayList<>(); - Set uniqueGas = new HashSet(actionList); - switch (key.trim()) { + List parents; + List actionList = new ArrayList<>(); + Set uniqueGas = new HashSet(actionList); + switch (key.trim()) + { - case "DEL": - parents = leaf.getParents(); + case "DEL": + parents = leaf.getParents(); - for (ITree parent : parents) { - String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; - actionList.add(s); + for (ITree parent : parents) + { + String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; + actionList.add(s); + } + actionList.add(key); + + if (uniqueGas.size() == 1) + { + Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); + if (first.isPresent()) + { + ITree parent = first.get(); + label = parent.getLabel(); } - actionList.add(key); + } - if (uniqueGas.size() == 1){ - Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); - if(first.isPresent()){ - ITree parent = first.get(); - label = parent.getLabel(); - } + pattern = Pattern.compile(delPattern); + matcher = pattern.matcher(label); + if (matcher.matches()) + { + group = matcher.group(2); + + } + break; + case "INS": + parents = leaf.getParents(); + + for (ITree parent : parents) + { + String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; + actionList.add(s); + } + actionList.add(key); + + if (uniqueGas.size() == 1) + { + Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); + if (first.isPresent()) + { + ITree parent = first.get(); + label = parent.getLabel(); } + } - pattern = Pattern.compile(delPattern); - matcher = pattern.matcher(label); - if (matcher.matches()) { - group = matcher.group(2); + // + pattern = Pattern.compile(insPattern); + matcher = pattern.matcher(label); + if (matcher.matches()) + { + group = matcher.group(2);// +" " + matcher.group(3); + } + break; + case "MOV": + parents = leaf.getParents(); + + for (ITree parent : parents) + { + String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; + actionList.add(s); + } + actionList.add(key); + + if (uniqueGas.size() == 1) + { + Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); + if (first.isPresent()) + { + ITree parent = first.get(); + label = parent.getLabel(); } - break; - case "INS": - parents = leaf.getParents(); + } - for (ITree parent : parents) { - String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; - actionList.add(s); - } - actionList.add(key); + pattern = Pattern.compile(movPattern); + matcher = pattern.matcher(label); + if (matcher.matches()) + { + group = matcher.group(2); + String group1 = matcher.group(3); - if (uniqueGas.size() == 1){ - Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); - if(first.isPresent()){ - ITree parent = first.get(); - label = parent.getLabel(); - } - } + } + break; + case "UPD": + pattern = Pattern.compile(updPattern); + matcher = pattern.matcher(label); + if (matcher.matches()) + { + group = matcher.group(2) + " " + matcher.group(3); -// - pattern = Pattern.compile(insPattern); - matcher = pattern.matcher(label); - if (matcher.matches()) { - group = matcher.group(2);// +" " + matcher.group(3); + } + break; - } - break; - case "MOV": - parents = leaf.getParents(); - - for (ITree parent : parents) { - String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; - actionList.add(s); - } - actionList.add(key); - - if (uniqueGas.size() == 1){ - Optional first = parents.stream().filter(p -> p.isRoot()).findFirst(); - if(first.isPresent()){ - ITree parent = first.get(); - label = parent.getLabel(); - } - } - - pattern = Pattern.compile(movPattern); - matcher = pattern.matcher(label); - if (matcher.matches()) { - group = matcher.group(2); - String group1 = matcher.group(3); - - } - break; - case "UPD": - pattern = Pattern.compile(updPattern); - matcher = pattern.matcher(label); - if (matcher.matches()) { - group = matcher.group(2) +" " + matcher.group(3); - - } - break; - - } - token = group; - return group; -// }else{ -// getTokenAtNode(leaf.getParent(),token); -// return token; -// } + } + token = group; + return group; + // }else{ + // getTokenAtNode(leaf.getParent(),token); + // return token; + // } } static String movPattern = "MOV (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*"; + static String delPattern = "DEL (.*)@@(.*)@AT@.*"; + static String insPattern = "INS (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*"; + static String updPattern = "UPD (.*)@@(.*)@TO@(.*)@AT@.*"; - public static String getNames2(ITree oldTree){ + public static String getNames2(ITree oldTree) + { List leaves = retainLeaves(TreeUtils.postOrder(oldTree)); List res = new ArrayList<>(); - String resu=""; - for (ITree leaf : leaves) { + String resu = ""; + for (ITree leaf : leaves) + { String tokenAtNode = getTokenAtNode(leaf, resu); res.add(tokenAtNode); } -// log.info("s"); + // log.info("s"); - if (res.size() == 1){ + if (res.size() == 1) + { final String regex = "MethodName:([a-zA-Z0-9]+):.*MethodName:([a-zA-Z0-9]+):.*"; Pattern compile = Pattern.compile(regex); Matcher matcher = compile.matcher(res.get(0)); - if(matcher.matches()){ - res.set(0,matcher.group(1) +" " + matcher.group(2)); + if (matcher.matches()) + { + res.set(0, matcher.group(1) + " " + matcher.group(2)); } } - return String.join(" ",res); + return String.join(" ", res); } } diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/FileHelper.java b/richedit/src/main/java/edu/lu/uni/serval/utils/FileHelper.java index 5d4bb40..0324fb9 100644 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/FileHelper.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/FileHelper.java @@ -1,104 +1,138 @@ package edu.lu.uni.serval.utils; -import java.io.BufferedInputStream; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileWriter; -import java.io.IOException; + +import java.io.*; import java.util.ArrayList; import java.util.List; -public class FileHelper { +public class FileHelper +{ /** * @param filePath */ - public static void createDirectory(String filePath) { + public static void createDirectory(String filePath) + { File file = new File(filePath); - if (file.exists()) { + if (file.exists()) + { deleteDirectory(filePath); } file.mkdirs(); } - public static void createFile(File file, String content) { + public static void createFile(File file, String content) + { FileWriter writer = null; BufferedWriter bw = null; - try { - if (!file.getParentFile().exists()) { + try + { + if (!file.getParentFile().exists()) + { file.getParentFile().mkdirs(); } - if (!file.exists()) file.createNewFile(); + if (!file.exists()) + { + file.createNewFile(); + } writer = new FileWriter(file); bw = new BufferedWriter(writer); bw.write(content); bw.flush(); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); - } finally { + } + finally + { close(bw); close(writer); } } - public static void deleteDirectory(String dir) { + public static void deleteDirectory(String dir) + { File file = new File(dir); - if (file.exists()) { - if (file.isDirectory()) { + if (file.exists()) + { + if (file.isDirectory()) + { File[] files = file.listFiles(); - if (files.length > 0) { - for (File f : files) { - if (f.isFile()) { + if (files.length > 0) + { + for (File f : files) + { + if (f.isFile()) + { deleteFile(f.getAbsolutePath()); - } else { + } + else + { deleteDirectory(f.getAbsolutePath()); } } } file.delete(); - } else { + } + else + { deleteFile(dir); } } } - public static void deleteFiles(String dir) { + public static void deleteFiles(String dir) + { File file = new File(dir); - if (file.exists()) { - if (file.isDirectory()) { + if (file.exists()) + { + if (file.isDirectory()) + { File[] files = file.listFiles(); - if (files.length > 0) { - for (File f : files) { - if (f.isFile()) { + if (files.length > 0) + { + for (File f : files) + { + if (f.isFile()) + { deleteFile(f.getAbsolutePath()); - } else { + } + else + { deleteFiles(f.getAbsolutePath()); } } } - } else { + } + else + { deleteFile(dir); } } } - public static void deleteFile(String fileName) { + public static void deleteFile(String fileName) + { File file = new File(fileName); - if (file.exists()) { - if (file.isFile()) { + if (file.exists()) + { + if (file.isFile()) + { file.delete(); - } else { + } + else + { deleteDirectory(fileName); } } } - public static List getAllDirectories(String filePath) { + public static List getAllDirectories(String filePath) + { return listAllDirectories(new File(filePath)); } @@ -109,26 +143,33 @@ public class FileHelper { * @param type * @return */ - public static List getAllFiles(String filePath, String type) { + public static List getAllFiles(String filePath, String type) + { return listAllFiles(new File(filePath), type); } - public static List getAllFilesInCurrentDiectory(String filePath, String type) { + public static List getAllFilesInCurrentDiectory(String filePath, String type) + { return getAllFilesInCurrentDiectory(new File(filePath), type); } - public static List getAllFilesInCurrentDiectory(File directory, String type) { + public static List getAllFilesInCurrentDiectory(File directory, String type) + { List fileList = new ArrayList<>(); - if (!directory.exists()) { + if (!directory.exists()) + { return null; } File[] files = directory.listFiles(); - for (File file : files) { - if (file.isFile()) { - if (file.toString().endsWith(type)) { + for (File file : files) + { + if (file.isFile()) + { + if (file.toString().endsWith(type)) + { fileList.add(file); } } @@ -137,36 +178,47 @@ public class FileHelper { return fileList; } - public static String getFileName(String filePath) { + public static String getFileName(String filePath) + { File file = new File(filePath); - if (file.exists()) { + if (file.exists()) + { return file.getName(); - } else { + } + else + { return null; } } - public static String getFileNameWithoutExtension(File file) { - if (file.exists()) { + public static String getFileNameWithoutExtension(File file) + { + if (file.exists()) + { String fileName = file.getName(); fileName = fileName.substring(0, fileName.lastIndexOf(".")); return fileName; - } else { + } + else + { return null; } } - public static String getFileExtension(File file) { + public static String getFileExtension(File file) + { String fileName = file.getName(); String extension = fileName.substring(fileName.lastIndexOf(".")); return extension; } - public static String getFileParentPath(String filePath) { + public static String getFileParentPath(String filePath) + { File file = new File(filePath); - if (file.exists()) { + if (file.exists()) + { return file.getParent() + "/"; } return ""; @@ -176,17 +228,13 @@ public class FileHelper { * Check whether a file path is valid or not. * * @param path, file path. - * @return true, the file path is valid. - * false, the file path is invalid. + * @return true, the file path is valid. false, the file path is invalid. */ - public static boolean isValidPath(String path) { + public static boolean isValidPath(String path) + { File file = new File(path); - if (file.exists()) { - return true; - } - - return false; + return file.exists(); } /** @@ -195,23 +243,31 @@ public class FileHelper { * @param file * @return */ - private static List listAllFiles(File file, String type) { + private static List listAllFiles(File file, String type) + { List fileList = new ArrayList<>(); - if (!file.exists()) { + if (!file.exists()) + { return null; } File[] files = file.listFiles(); - for (File f : files) { - if (f.isFile()) { - if (f.toString().endsWith(type)) { + for (File f : files) + { + if (f.isFile()) + { + if (f.toString().endsWith(type)) + { fileList.add(f); } - } else { + } + else + { List fl = listAllFiles(f, type); - if (fl != null && fl.size() > 0) { + if (fl != null && fl.size() > 0) + { fileList.addAll(fl); } } @@ -226,13 +282,16 @@ public class FileHelper { * @param file * @return */ - private static List listAllDirectories(File file) { + private static List listAllDirectories(File file) + { List fileList = new ArrayList<>(); File[] files = file.listFiles(); - for (File f : files) { - if (f.isDirectory()) { + for (File f : files) + { + if (f.isDirectory()) + { fileList.add(f); fileList.addAll(listAllDirectories(f)); } @@ -241,10 +300,12 @@ public class FileHelper { return fileList; } - public static void makeDirectory(String fileName) { + public static void makeDirectory(String fileName) + { deleteFile(fileName); File file = new File(fileName).getParentFile(); - if (!file.exists()) { + if (!file.exists()) + { file.mkdirs(); } } @@ -255,7 +316,8 @@ public class FileHelper { * @param fileName * @return String, the content of a file. */ - public static String readFile(String fileName) { + public static String readFile(String fileName) + { return readFile(new File(fileName)); } @@ -265,26 +327,35 @@ public class FileHelper { * @param file * @return String, the content of a file. */ - public static String readFile(File file) { + public static String readFile(File file) + { byte[] input = null; BufferedInputStream bis = null; - try { + try + { bis = new BufferedInputStream(new FileInputStream(file)); input = new byte[bis.available()]; bis.read(input); - } catch (FileNotFoundException e) { + } + catch (FileNotFoundException e) + { e.printStackTrace(); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); - } finally { + } + finally + { close(bis); } String sourceCode = null; - if (input != null) { + if (input != null) + { sourceCode = new String(input); } @@ -295,89 +366,119 @@ public class FileHelper { * Output output into a file. * * @param fileName, output file name. - * @param data, output data. - * @param append, the output data will be appended previous data in the file or not. + * @param data, output data. + * @param append, the output data will be appended previous data in the file or not. */ - public static void outputToFile(String fileName, StringBuilder data, boolean append) { + public static void outputToFile(String fileName, StringBuilder data, boolean append) + { outputToFile(fileName, data.toString(), append); } - public static void outputToFile(File file, StringBuilder data, boolean append) { + public static void outputToFile(File file, StringBuilder data, boolean append) + { outputToFile(file, data.toString(), append); } - public static void outputToFile(String fileName, String data, boolean append) { + public static void outputToFile(String fileName, String data, boolean append) + { File file = new File(fileName); outputToFile(file, data, append); } - public static void outputToFile(File file, String data, boolean append) { + public static void outputToFile(File file, String data, boolean append) + { FileWriter writer = null; BufferedWriter bw = null; - try { - if (!file.getParentFile().exists()) { + try + { + if (!file.getParentFile().exists()) + { file.getParentFile().mkdirs(); } - if (!file.exists()) { + if (!file.exists()) + { file.createNewFile(); } writer = new FileWriter(file, append); bw = new BufferedWriter(writer); bw.write(data); bw.flush(); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); - } finally { + } + finally + { close(bw); close(writer); } } - private static void close(FileWriter writer) { - try { - if (writer != null) { + private static void close(FileWriter writer) + { + try + { + if (writer != null) + { writer.close(); writer = null; } - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); } } - private static void close(BufferedWriter bw) { - try { - if (bw != null) { + private static void close(BufferedWriter bw) + { + try + { + if (bw != null) + { bw.close(); bw = null; } - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); } } - private static void close(BufferedInputStream bis) { - try { - if (bis != null) { + private static void close(BufferedInputStream bis) + { + try + { + if (bis != null) + { bis.close(); bis = null; } - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); } } - public static List getAllSubDirectories(String fileName) { + public static List getAllSubDirectories(String fileName) + { File file = new File(fileName); List subDirectories = new ArrayList<>(); - if (file.exists()) { + if (file.exists()) + { File[] files = file.listFiles(); - for (File f : files) { - if (f.isDirectory()) { + for (File f : files) + { + if (f.isDirectory()) + { subDirectories.add(f); } } } return subDirectories; } -} \ No newline at end of file +} diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/ListSorter.java b/richedit/src/main/java/edu/lu/uni/serval/utils/ListSorter.java index ac69440..73ec62d 100644 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/ListSorter.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/ListSorter.java @@ -5,41 +5,53 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; -public class ListSorter> { +public class ListSorter> +{ - private List list; + private final List list; - public ListSorter(List list) { + public ListSorter(List list) + { this.list = new ArrayList<>(); this.list.addAll(list); } - public List getList() { + public List getList() + { return this.list; } - public List sortAscending() { - try { - if (list != null && list.size() > 0) { - Collections.sort(this.list, new Comparator() { + public List sortAscending() + { + try + { + if (list != null && list.size() > 0) + { + Collections.sort(this.list, new Comparator() + { @Override - public int compare(T t1, T t2) { + public int compare(T t1, T t2) + { return t1.compareTo(t2); } }); } - } catch (Exception e) { + } + catch (Exception e) + { return null; } return this.list; } - public List sortDescending() { - if (list != null && list.size() > 0) { + public List sortDescending() + { + if (list != null && list.size() > 0) + { Collections.sort(this.list, Collections.reverseOrder()); } return this.list; } -} \ No newline at end of file +} diff --git a/richedit/src/main/java/edu/lu/uni/serval/utils/PoolBuilder.java b/richedit/src/main/java/edu/lu/uni/serval/utils/PoolBuilder.java index ce78ca5..eb76795 100755 --- a/richedit/src/main/java/edu/lu/uni/serval/utils/PoolBuilder.java +++ b/richedit/src/main/java/edu/lu/uni/serval/utils/PoolBuilder.java @@ -7,25 +7,25 @@ import java.time.Duration; /** * Created by anilkoyuncu on 17/09/2018. */ -public class PoolBuilder { - public static JedisPoolConfig getPoolConfig() { +public class PoolBuilder +{ + public static JedisPoolConfig getPoolConfig() + { return poolConfig; } static final JedisPoolConfig poolConfig = buildPoolConfig(); - - - - private static JedisPoolConfig buildPoolConfig() { + private static JedisPoolConfig buildPoolConfig() + { final JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(1024); poolConfig.setMaxIdle(1024); -// poolConfig.setMinIdle(16); -// poolConfig.setTestOnBorrow(true); -// poolConfig.setTestOnReturn(true); -// poolConfig.setTestWhileIdle(true); + // 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);