[O] Reformat code for java

This commit is contained in:
Azalea (on HyDEV-Daisy)
2022-05-16 01:40:16 -04:00
parent 99c365076a
commit 40a2722a01
23 changed files with 2719 additions and 2370 deletions
@@ -17,7 +17,7 @@ import java.util.Properties;
*/ */
public class Launcher 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 public static void main(String[] args) throws IOException
{ {
@@ -3,42 +3,51 @@ package edu.lu.uni.serval.richedit.ediff;
/** /**
* Created by anilkoyuncu on 18/09/2018. * Created by anilkoyuncu on 18/09/2018.
*/ */
public class BaseMessage { public class BaseMessage
{
private long SECONDS_TO_WAIT; private long SECONDS_TO_WAIT;
private int id; private int id;
private int threadPoolSize; private int threadPoolSize;
public BaseMessage(int id,Long timeout) { public BaseMessage(int id, Long timeout)
{
this.id = id; this.id = id;
this.SECONDS_TO_WAIT = timeout; this.SECONDS_TO_WAIT = timeout;
// this.threadPoolSize = threadPoolSize; // this.threadPoolSize = threadPoolSize;
} }
public int getId() { public int getId()
{
return id; return id;
} }
public void setId(int id) { public void setId(int id)
{
this.id = id; this.id = id;
} }
public long getSECONDS_TO_WAIT()
public long getSECONDS_TO_WAIT() { {
return SECONDS_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; this.SECONDS_TO_WAIT = SECONDS_TO_WAIT;
} }
public int getThreadPoolSize() { public int getThreadPoolSize()
{
return threadPoolSize; return threadPoolSize;
} }
public void setThreadPoolSize(int threadPoolSize) { public void setThreadPoolSize(int threadPoolSize)
{
this.threadPoolSize = threadPoolSize; this.threadPoolSize = threadPoolSize;
} }
@@ -10,28 +10,35 @@ import org.slf4j.LoggerFactory;
import java.util.List; import java.util.List;
public class EDiffActor extends UntypedActor { public class EDiffActor extends UntypedActor
{
private static Logger logger = LoggerFactory.getLogger(EDiffActor.class); private static final Logger logger = LoggerFactory.getLogger(EDiffActor.class);
private final ActorRef mineRouter;
private ActorRef mineRouter;
private final int numberOfWorkers; private final int numberOfWorkers;
private int counter = 0; private int counter = 0;
public EDiffActor(int numberOfWorkers, String project) { public EDiffActor(int numberOfWorkers, String project)
{
mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers) mineRouter = this.getContext().actorOf(new RoundRobinPool(numberOfWorkers)
.props(EDiffWorker.props(project)), "mine-fix-pattern-router"); .props(EDiffWorker.props(project)), "mine-fix-pattern-router");
this.numberOfWorkers = numberOfWorkers; this.numberOfWorkers = numberOfWorkers;
} }
public static Props props(final int numberOfWorkers, final String project) { public static Props props(final int numberOfWorkers, final String project)
{
return Props.create(new Creator<EDiffActor>() { return Props.create(new Creator<EDiffActor>()
{
private static final long serialVersionUID = 9207427376110704705L; private static final long serialVersionUID = 9207427376110704705L;
@Override @Override
public EDiffActor create() throws Exception { public EDiffActor create() throws Exception
{
return new EDiffActor(numberOfWorkers, project); return new EDiffActor(numberOfWorkers, project);
} }
@@ -40,34 +47,45 @@ public class EDiffActor extends UntypedActor {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void onReceive(Object message) throws Exception { public void onReceive(Object message) throws Exception
if (message instanceof EDiffMessage) { {
if (message instanceof EDiffMessage)
{
List<MessageFile> files = ((EDiffMessage) message).getMsgFiles(); List<MessageFile> files = ((EDiffMessage) message).getMsgFiles();
int size = files.size(); int size = files.size();
int average = size / numberOfWorkers; int average = size / numberOfWorkers;
int reminder = size % numberOfWorkers; int reminder = size % numberOfWorkers;
int counter = 0; int counter = 0;
for (int i = 0; i < numberOfWorkers; i ++) { for (int i = 0; i < numberOfWorkers; i++)
{
int fromIndex = i * average + counter; int fromIndex = i * average + counter;
if (counter < reminder) counter ++; if (counter < reminder)
{
counter++;
}
int toIndex = (i + 1) * average + counter; int toIndex = (i + 1) * average + counter;
List<MessageFile> filesOfWorkers = files.subList(fromIndex, toIndex); List<MessageFile> 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()); 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()); mineRouter.tell(workMsg, getSelf());
logger.info("Assign {} task to worker #" + (i + 1) ,filesOfWorkers.size()); logger.info("Assign {} task to worker #" + (i + 1), filesOfWorkers.size());
} }
} else if ("STOP".equals(message.toString())) { }
counter ++; else if ("STOP".equals(message.toString()))
{
counter++;
logger.info(counter + " workers finalized their work..."); logger.info(counter + " workers finalized their work...");
if (counter >= numberOfWorkers) { if (counter >= numberOfWorkers)
{
logger.info("All workers finalized their work..."); logger.info("All workers finalized their work...");
this.getContext().stop(mineRouter); this.getContext().stop(mineRouter);
this.getContext().stop(getSelf()); this.getContext().stop(getSelf());
this.getContext().system().shutdown(); this.getContext().system().shutdown();
} }
} else { }
else
{
unhandled(message); unhandled(message);
} }
} }
@@ -11,19 +11,21 @@ import java.io.File;
import java.util.List; import java.util.List;
/** /**
* Parse fix violations with GumTree in terms of multiple statements. * Parse fix violations with GumTree in terms of multiple statements.
* *
* @author kui.liu * @author kui.liu
*
*/ */
public class EDiffHunkParser extends EDiffParser { public class EDiffHunkParser extends EDiffParser
{
private static final Logger logger = LoggerFactory.getLogger(EDiffHunkParser.class);
private static Logger logger = LoggerFactory.getLogger(EDiffHunkParser.class);
@Override @Override
public void parseFixPatterns(File prevFile, File revFile, File diffentryFile, String project, JedisPool innerPool,String srcMLPath,String hunkLimit,boolean isJava) { public void parseFixPatterns(File prevFile, File revFile, File diffentryFile, String project, JedisPool innerPool, String srcMLPath, String hunkLimit, boolean isJava)
try { {
try
{
String datasetName = project; String datasetName = project;
String[] split1 = diffentryFile.getParent().split(datasetName); String[] split1 = diffentryFile.getParent().split(datasetName);
String root = split1[0]; String root = split1[0];
@@ -32,40 +34,45 @@ public class EDiffHunkParser extends EDiffParser {
List<HierarchicalActionSet> actionSets = parseChangedSourceCodeWithGumTree2(prevFile, revFile, srcMLPath, isJava); List<HierarchicalActionSet> 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)) { if (actionSets.size() > Integer.valueOf(hunkLimit))
{
processActionSet = false; processActionSet = false;
logger.debug("Skipping {} set size {}", diffentryFile.getName(), hunkLimit); logger.debug("Skipping {} set size {}", diffentryFile.getName(), hunkLimit);
} }
int hunkSet = 0; int hunkSet = 0;
if (processActionSet) { if (processActionSet)
{
for (HierarchicalActionSet actionSet : actionSets) { for (HierarchicalActionSet actionSet : actionSets)
// FileOutputStream f = null; {
// FileOutputStream f = null;
// try { // try {
String astNodeType = actionSet.getAstNodeType(); String astNodeType = actionSet.getAstNodeType();
// if (astNodeType.equals(rootType)){ // if (astNodeType.equals(rootType)){
// //
// } // }
actionSet.toString(); actionSet.toString();
int size = actionSet.getActionSize(); 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 targetTree = EDiffHelper.getTargets(actionSet, isJava);
ITree actionTree = EDiffHelper.getActionTrees(actionSet); ITree actionTree = EDiffHelper.getActionTrees(actionSet);
ITree shapeTree = EDiffHelper.getShapeTree(actionSet, isJava); ITree shapeTree = EDiffHelper.getShapeTree(actionSet, isJava);
ITree tokenTree = EDiffHelper.getTokenTree(actionSet,isJava); ITree tokenTree = EDiffHelper.getTokenTree(actionSet, isJava);
String tokens = EDiffHelper.getNames2(tokenTree); String tokens = EDiffHelper.getNames2(tokenTree);
// EDiffHelper.getTokenTree(actionSet, parent, children, tc); // EDiffHelper.getTokenTree(actionSet, parent, children, tc);
try (Jedis inner = innerPool.getResource()) { try (Jedis inner = innerPool.getResource())
{
inner.hset("dump", key, actionSet.toString()); inner.hset("dump", key, actionSet.toString());
inner.hset(key, "actionTree", actionTree.toStaticHashString()); inner.hset(key, "actionTree", actionTree.toStaticHashString());
@@ -73,39 +80,41 @@ public class EDiffHunkParser extends EDiffParser {
inner.hset(key, "shapeTree", shapeTree.toStaticHashString()); inner.hset(key, "shapeTree", shapeTree.toStaticHashString());
inner.hset(key, "tokens", tokens); inner.hset(key, "tokens", tokens);
} }
// File f = new File(root+"dumps/"+astNodeType+"/"+String.valueOf(size)+"/"); // File f = new File(root+"dumps/"+astNodeType+"/"+String.valueOf(size)+"/");
// f.mkdirs(); // f.mkdirs();
// f = new File(root+"dumps/"+key); // f = new File(root+"dumps/"+key);
// //
// FileUtils.writeByteArrayToFile(f,EDiffHelper.kryoSerialize(actionSet)); // FileUtils.writeByteArrayToFile(f,EDiffHelper.kryoSerialize(actionSet));
// FileUtils.writeByteArrayToFile(f,EDiffHelper.commonsSerialize(actionSet)); // FileUtils.writeByteArrayToFile(f,EDiffHelper.commonsSerialize(actionSet));
// FileUtils.writeByteArrayToFile(f,actionSet.toString().getBytes()); // FileUtils.writeByteArrayToFile(f,actionSet.toString().getBytes());
// FileOutputStream fos = new FileOutputStream(f); // FileOutputStream fos = new FileOutputStream(f);
// ObjectOutputStream oos = new ObjectOutputStream(fos); // ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(EDiffHelper.kryoSerialize(actionSet)); // oos.writeObject(EDiffHelper.kryoSerialize(actionSet));
// oos.flush(); // oos.flush();
// oos.close(); // oos.close();
// } catch (Exception e) { // } catch (Exception e) {
// logger.error("error", e); // logger.error("error", e);
//// e.printStackTrace(); //// e.printStackTrace();
// } // }
hunkSet++; hunkSet++;
} }
try (Jedis inner = innerPool.getResource()) { try (Jedis inner = innerPool.getResource())
{
inner.hset("diffEntry", pj + "_" + diffentryFile.getName(), "1"); inner.hset("diffEntry", pj + "_" + diffentryFile.getName(), "1");
} }
} }
} }
} catch (Exception e) { }
catch (Exception e)
{
logger.error("error", e); logger.error("error", e);
// e.printStackTrace(); // e.printStackTrace();
} }
} }
} }
@@ -4,62 +4,69 @@ import redis.clients.jedis.JedisPool;
import java.util.List; import java.util.List;
public class EDiffMessage extends BaseMessage{ public class EDiffMessage extends BaseMessage
{
private List<MessageFile> msgFiles; private final List<MessageFile> msgFiles;
public JedisPool getInnerPool() { public JedisPool getInnerPool()
{
return innerPool; return innerPool;
} }
public void setInnerPool(JedisPool innerPool) { public void setInnerPool(JedisPool innerPool)
{
this.innerPool = innerPool; this.innerPool = innerPool;
} }
private JedisPool innerPool; private JedisPool innerPool;
public String getSrcMLPath() { public String getSrcMLPath()
{
return srcMLPath; return srcMLPath;
} }
private String srcMLPath; private String srcMLPath;
public String getRootType() { public String getRootType()
{
return rootType; return rootType;
} }
private String rootType; private String rootType;
public EDiffMessage(int id, List<MessageFile> msgFiles, String eDiffTimeout, JedisPool pool, String srcMLPath, String rootType)
{
super(id, new Long(eDiffTimeout));
public EDiffMessage(int id, List<MessageFile> msgFiles,String eDiffTimeout,JedisPool pool,String srcMLPath,String rootType) {
super(id,new Long(eDiffTimeout));
this.msgFiles = msgFiles; this.msgFiles = msgFiles;
this.innerPool = pool; this.innerPool = pool;
this.srcMLPath = srcMLPath; this.srcMLPath = srcMLPath;
this.rootType = rootType; this.rootType = rootType;
} }
public EDiffMessage(int id, List<MessageFile> msgFiles,Long eDiffTimeout,JedisPool pool) {
super(id,eDiffTimeout); public EDiffMessage(int id, List<MessageFile> msgFiles, Long eDiffTimeout, JedisPool pool)
{
super(id, eDiffTimeout);
this.msgFiles = msgFiles; this.msgFiles = msgFiles;
this.innerPool = pool; this.innerPool = pool;
} }
public EDiffMessage(int id, List<MessageFile> filesOfWorkers, long seconds_to_wait, JedisPool innerPool, String srcMLPath,String rootType) { public EDiffMessage(int id, List<MessageFile> filesOfWorkers, long seconds_to_wait, JedisPool innerPool, String srcMLPath, String rootType)
{
super(id,seconds_to_wait); super(id, seconds_to_wait);
this.msgFiles = filesOfWorkers; this.msgFiles = filesOfWorkers;
this.innerPool = innerPool; this.innerPool = innerPool;
this.srcMLPath = srcMLPath; this.srcMLPath = srcMLPath;
this.rootType = rootType; this.rootType = rootType;
} }
public List<MessageFile> getMsgFiles() { public List<MessageFile> getMsgFiles()
{
return msgFiles; return msgFiles;
} }
@@ -3,8 +3,6 @@ package edu.lu.uni.serval.richedit.ediff;
import com.github.gumtreediff.actions.model.Action; import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.gen.srcml.GumTreeCComparer; import com.github.gumtreediff.gen.srcml.GumTreeCComparer;
import edu.lu.uni.serval.gumtree.GumTreeComparer; import edu.lu.uni.serval.gumtree.GumTreeComparer;
import edu.lu.uni.serval.utils.ListSorter; import edu.lu.uni.serval.utils.ListSorter;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
@@ -18,9 +16,9 @@ import java.util.List;
* Parse fix patterns with GumTree. * Parse fix patterns with GumTree.
* *
* @author kui.liu * @author kui.liu
*
*/ */
public class EDiffParser extends Parser { public class EDiffParser extends Parser
{
/* /*
* ResultType: * ResultType:
@@ -33,7 +31,6 @@ public class EDiffParser extends Parser {
public int resultType = 0; public int resultType = 0;
/** /**
* Regroup GumTree results without remove the modification of variable names. * Regroup GumTree results without remove the modification of variable names.
* *
@@ -41,37 +38,50 @@ public class EDiffParser extends Parser {
* @param revFile * @param revFile
* @return * @return
*/ */
public List<HierarchicalActionSet> parseChangedSourceCodeWithGumTree2(File prevFile, File revFile,String srcMLPath,boolean isJava) { public List<HierarchicalActionSet> parseChangedSourceCodeWithGumTree2(File prevFile, File revFile, String srcMLPath, boolean isJava)
{
List<HierarchicalActionSet> actionSets = new ArrayList<>(); List<HierarchicalActionSet> actionSets = new ArrayList<>();
// GumTree results // GumTree results
// boolean isJava =false; // boolean isJava =false;
List<Action> gumTreeResults = null; List<Action> gumTreeResults = null;
if (isJava){ 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); // 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) { if (gumTreeResults == null)
{
this.resultType = 1; this.resultType = 1;
return actionSets; return actionSets;
} else if (gumTreeResults.size() == 0){ }
else if (gumTreeResults.size() == 0)
{
this.resultType = 2; this.resultType = 2;
return actionSets; return actionSets;
} else { }
else
{
// Regroup GumTre results. // Regroup GumTre results.
List<HierarchicalActionSet> allActionSets = null; List<HierarchicalActionSet> allActionSets = null;
if (isJava){ if (isJava)
{
allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults); allActionSets = new HierarchicalRegrouper().regroupGumTreeResults(gumTreeResults);
}else{ }
HashSet<Integer> removeType = new HashSet<Integer>(Arrays.asList(131,132,133,134,135,136,137)); else
{
HashSet<Integer> removeType = new HashSet<Integer>(Arrays.asList(131, 132, 133, 134, 135, 136, 137));
boolean b = gumTreeResults.stream().anyMatch(p -> removeType.contains(p.getNode().getType())); boolean b = gumTreeResults.stream().anyMatch(p -> removeType.contains(p.getNode().getType()));
if(b){ if (b)
{
return actionSets; return actionSets;
} }
allActionSets = new HierarchicalRegrouperForC().regroupGumTreeResults(gumTreeResults); allActionSets = new HierarchicalRegrouperForC().regroupGumTreeResults(gumTreeResults);
@@ -81,7 +91,8 @@ public class EDiffParser extends Parser {
ListSorter<HierarchicalActionSet> sorter = new ListSorter<>(allActionSets); ListSorter<HierarchicalActionSet> sorter = new ListSorter<>(allActionSets);
actionSets = sorter.sortAscending(); actionSets = sorter.sortAscending();
if (actionSets.size() == 0) { if (actionSets.size() == 0)
{
this.resultType = 3; this.resultType = 3;
} }
@@ -90,15 +101,15 @@ public class EDiffParser extends Parser {
} }
@Override @Override
public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath,String rootType,boolean isJava) { public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava)
{
} }
// @Override // @Override
// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool) { // public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool) {
// //
// } // }
} }
@@ -11,23 +11,28 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.*; import java.util.concurrent.*;
public class EDiffWorker extends UntypedActor { public class EDiffWorker extends UntypedActor
private static Logger log = LoggerFactory.getLogger(EDiffActor.class); {
private static final Logger log = LoggerFactory.getLogger(EDiffActor.class);
private String project; private final String project;
public EDiffWorker(String project) { public EDiffWorker(String project)
{
this.project = project; this.project = project;
} }
public static Props props(final String project) { public static Props props(final String project)
return Props.create(new Creator<EDiffWorker>() { {
return Props.create(new Creator<EDiffWorker>()
{
private static final long serialVersionUID = -7615153844097275009L; private static final long serialVersionUID = -7615153844097275009L;
@Override @Override
public EDiffWorker create() throws Exception { public EDiffWorker create() throws Exception
{
return new EDiffWorker(project); return new EDiffWorker(project);
} }
@@ -35,8 +40,10 @@ public class EDiffWorker extends UntypedActor {
} }
@Override @Override
public void onReceive(Object message) throws Exception { public void onReceive(Object message) throws Exception
if (message instanceof EDiffMessage) { {
if (message instanceof EDiffMessage)
{
EDiffMessage msg = (EDiffMessage) message; EDiffMessage msg = (EDiffMessage) message;
List<MessageFile> files = msg.getMsgFiles(); List<MessageFile> files = msg.getMsgFiles();
@@ -46,45 +53,57 @@ public class EDiffWorker extends UntypedActor {
String srcMLPath = msg.getSrcMLPath(); String srcMLPath = msg.getSrcMLPath();
String rootType = msg.getRootType(); String rootType = msg.getRootType();
for (MessageFile msgFile : files) { for (MessageFile msgFile : files)
{
File revFile = msgFile.getRevFile(); File revFile = msgFile.getRevFile();
File prevFile = msgFile.getPrevFile(); File prevFile = msgFile.getPrevFile();
File diffentryFile = msgFile.getDiffEntryFile(); File diffentryFile = msgFile.getDiffEntryFile();
EDiffHunkParser parser = new EDiffHunkParser(); EDiffHunkParser parser = new EDiffHunkParser();
final ExecutorService executor = Executors.newSingleThreadExecutor(); final ExecutorService executor = Executors.newSingleThreadExecutor();
// schedule the work // schedule the work
final Future<?> future = executor.submit(new RunnableParser(prevFile, revFile, diffentryFile, parser,project,msg.getInnerPool(),srcMLPath,rootType,false)); final Future<?> future = executor.submit(new RunnableParser(prevFile, revFile, diffentryFile, parser, project, msg.getInnerPool(), srcMLPath, rootType, false));
try { try
{
// wait for task to complete // wait for task to complete
future.get(msg.getSECONDS_TO_WAIT(), TimeUnit.SECONDS); future.get(msg.getSECONDS_TO_WAIT(), TimeUnit.SECONDS);
counter ++; counter++;
if (counter % 1000 == 0) { if (counter % 1000 == 0)
log.info("Worker #" + id +" finalized parsing " + counter + " files... remaing "+ (files.size() - counter)); {
log.info("Worker #" + id + " finalized parsing " + counter + " files... remaing " + (files.size() - counter));
} }
} catch (TimeoutException e) { }
catch (TimeoutException e)
{
future.cancel(true); future.cancel(true);
System.err.println("#Timeout: " + revFile.getName()); System.err.println("#Timeout: " + revFile.getName());
} catch (InterruptedException e) { }
catch (InterruptedException e)
{
System.err.println("#TimeInterrupted: " + revFile.getName()); System.err.println("#TimeInterrupted: " + revFile.getName());
e.printStackTrace(); e.printStackTrace();
} catch (ExecutionException e) { }
catch (ExecutionException e)
{
System.err.println("#TimeAborted: " + revFile.getName()); System.err.println("#TimeAborted: " + revFile.getName());
e.printStackTrace(); e.printStackTrace();
} finally { }
finally
{
executor.shutdownNow(); executor.shutdownNow();
} }
} }
log.info("Worker #" + id + " finalized the work..."); log.info("Worker #" + id + " finalized the work...");
this.getSender().tell("STOP", getSelf()); this.getSender().tell("STOP", getSelf());
} else { }
else
{
unhandled(message); unhandled(message);
} }
} }
@@ -11,82 +11,109 @@ import java.util.List;
* Hierarchical-level results of GumTree results * Hierarchical-level results of GumTree results
* *
* @author kui.liu * @author kui.liu
*
*/ */
public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,Serializable { public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>, Serializable
{
private String astNodeType; private String astNodeType;
private Action action; private Action action;
private Action parentAction; private Action parentAction;
private String actionString; private String actionString;
private Integer startPosition; private Integer startPosition;
private Integer length; private Integer length;
private int bugStartLineNum = 0; private int bugStartLineNum = 0;
private int bugEndLineNum; private int bugEndLineNum;
private int fixStartLineNum; private int fixStartLineNum;
private int fixEndLineNum; private int fixEndLineNum;
private HierarchicalActionSet parent = null; private HierarchicalActionSet parent = null;
private List<HierarchicalActionSet> subActions = new ArrayList<>(); private List<HierarchicalActionSet> subActions = new ArrayList<>();
private ITree node; private ITree node;
// source code tree. // source code tree.
public int getBugEndPosition() { public int getBugEndPosition()
{
return bugEndPosition; return bugEndPosition;
} }
public int getFixEndPosition() { public int getFixEndPosition()
{
return fixEndPosition; return fixEndPosition;
} }
private int bugEndPosition; private int bugEndPosition;
private int fixEndPosition; private int fixEndPosition;
public ITree getNode() { public ITree getNode()
{
return node; return node;
} }
public void setNode(ITree node) { public void setNode(ITree node)
{
this.node = node; this.node = node;
} }
public void setAstNodeType(String astNodeType) { public void setAstNodeType(String astNodeType)
{
this.astNodeType = astNodeType; this.astNodeType = astNodeType;
} }
public String getAstNodeType() { public String getAstNodeType()
{
return astNodeType; return astNodeType;
} }
public Action getAction() { public Action getAction()
{
return action; return action;
} }
public void setAction(Action action) { public void setAction(Action action)
{
this.action = action; this.action = action;
} }
public Action getParentAction() { public Action getParentAction()
{
return parentAction; return parentAction;
} }
public void setParentAction(Action parentAction) { public void setParentAction(Action parentAction)
{
this.parentAction = parentAction; this.parentAction = parentAction;
} }
public String getActionString() { public String getActionString()
{
return actionString; return actionString;
} }
public void setActionString(String actionString) { public void setActionString(String actionString)
{
this.actionString = actionString; this.actionString = actionString;
int atIndex = actionString.indexOf("@AT@") + 4; int atIndex = actionString.indexOf("@AT@") + 4;
int lengthIndex = actionString.indexOf("@LENGTH@"); int lengthIndex = actionString.indexOf("@LENGTH@");
if (lengthIndex == -1) { if (lengthIndex == -1)
{
this.startPosition = Integer.parseInt(actionString.substring(atIndex).trim()); this.startPosition = Integer.parseInt(actionString.substring(atIndex).trim());
this.length = 0; this.length = 0;
} else { }
else
{
this.startPosition = Integer.parseInt(actionString.substring(atIndex, lengthIndex).trim()); this.startPosition = Integer.parseInt(actionString.substring(atIndex, lengthIndex).trim());
this.length = Integer.parseInt(actionString.substring(lengthIndex + 8).trim()); this.length = Integer.parseInt(actionString.substring(lengthIndex + 8).trim());
} }
@@ -96,111 +123,137 @@ public class HierarchicalActionSet implements Comparable<HierarchicalActionSet>,
this.astNodeType = nodeType; this.astNodeType = nodeType;
} }
public int getStartPosition() { public int getStartPosition()
{
return startPosition; return startPosition;
} }
public int getLength() { public int getLength()
{
return length; return length;
} }
public int getBugStartLineNum() { public int getBugStartLineNum()
{
return bugStartLineNum; return bugStartLineNum;
} }
public void setBugStartLineNum(int bugStartLineNum) { public void setBugStartLineNum(int bugStartLineNum)
{
this.bugStartLineNum = bugStartLineNum; this.bugStartLineNum = bugStartLineNum;
} }
public int getBugEndLineNum() { public int getBugEndLineNum()
{
return bugEndLineNum; return bugEndLineNum;
} }
public void setBugEndLineNum(int bugEndLineNum) { public void setBugEndLineNum(int bugEndLineNum)
{
this.bugEndLineNum = bugEndLineNum; this.bugEndLineNum = bugEndLineNum;
} }
public int getFixStartLineNum() { public int getFixStartLineNum()
{
return fixStartLineNum; return fixStartLineNum;
} }
public void setFixStartLineNum(int fixStartLineNum) { public void setFixStartLineNum(int fixStartLineNum)
{
this.fixStartLineNum = fixStartLineNum; this.fixStartLineNum = fixStartLineNum;
} }
public int getFixEndLineNum() { public int getFixEndLineNum()
{
return fixEndLineNum; return fixEndLineNum;
} }
public void setFixEndLineNum(int fixEndLineNum) { public void setFixEndLineNum(int fixEndLineNum)
{
this.fixEndLineNum = fixEndLineNum; this.fixEndLineNum = fixEndLineNum;
} }
public HierarchicalActionSet getParent() { public HierarchicalActionSet getParent()
{
return parent; return parent;
} }
public void setParent(HierarchicalActionSet parent) { public void setParent(HierarchicalActionSet parent)
{
this.parent = parent; this.parent = parent;
} }
public List<HierarchicalActionSet> getSubActions() { public List<HierarchicalActionSet> getSubActions()
{
return subActions; return subActions;
} }
public void setSubActions(List<HierarchicalActionSet> subActions) { public void setSubActions(List<HierarchicalActionSet> subActions)
{
this.subActions = subActions; this.subActions = subActions;
} }
public void setBugEndPosition(int bugEndPosition) { public void setBugEndPosition(int bugEndPosition)
{
this.bugEndPosition = bugEndPosition; this.bugEndPosition = bugEndPosition;
} }
public void setFixEndPosition(int fixEndPosition)
public void setFixEndPosition(int fixEndPosition) { {
this.fixEndPosition = fixEndPosition; this.fixEndPosition = fixEndPosition;
} }
@Override @Override
public int compareTo(HierarchicalActionSet o) { 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 List<String> strList = new ArrayList<>(); private final List<String> strList = new ArrayList<>();
public int getActionSize(){ public int getActionSize()
{
return strList.size(); return strList.size();
} }
@Override @Override
public String toString() { public String toString()
{
String str = actionString; String str = actionString;
if (strList.size() == 0) { if (strList.size() == 0)
{
strList.add(str); strList.add(str);
for (HierarchicalActionSet actionSet : subActions) { for (HierarchicalActionSet actionSet : subActions)
{
actionSet.toString(); actionSet.toString();
List<String> strList1 = actionSet.strList; List<String> strList1 = actionSet.strList;
for (String str1 : strList1) { for (String str1 : strList1)
{
strList.add("---" + str1); strList.add("---" + str1);
} }
} }
} else { }
else
{
strList.clear(); strList.clear();
strList.add(str); strList.add(str);
for (HierarchicalActionSet actionSet : subActions) { for (HierarchicalActionSet actionSet : subActions)
{
actionSet.toString(); actionSet.toString();
List<String> strList1 = actionSet.strList; List<String> strList1 = actionSet.strList;
for (String str1 : strList1) { for (String str1 : strList1)
{
strList.add("---" + str1); strList.add("---" + str1);
} }
} }
} }
str = ""; str = "";
for (String str1 : strList) { for (String str1 : strList)
{
str += str1 + "\n"; str += str1 + "\n";
} }
@@ -5,7 +5,6 @@ import com.github.gumtreediff.tree.ITree;
import edu.lu.uni.serval.utils.ASTNodeMap; import edu.lu.uni.serval.utils.ASTNodeMap;
import edu.lu.uni.serval.utils.ListSorter; import edu.lu.uni.serval.utils.ListSorter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -15,11 +14,12 @@ import java.util.stream.Collectors;
* Regroup GumTree results to a hierarchical construction. * Regroup GumTree results to a hierarchical construction.
* *
* @author kui.liu * @author kui.liu
*
*/ */
public class HierarchicalRegrouper { public class HierarchicalRegrouper
{
public List<HierarchicalActionSet> regroupGumTreeResults(List<Action> actions) { public List<HierarchicalActionSet> regroupGumTreeResults(List<Action> actions)
{
/* /*
* First, sort actions by their positions. * First, sort actions by their positions.
*/ */
@@ -32,17 +32,23 @@ public class HierarchicalRegrouper {
List<HierarchicalActionSet> actionSets = new ArrayList<>(); List<HierarchicalActionSet> actionSets = new ArrayList<>();
HierarchicalActionSet actionSet = null; HierarchicalActionSet actionSet = null;
if(actions == null){ if (actions == null)
{
return actionSets; return actionSets;
} }
for(Action act : actions){ for (Action act : actions)
{
Action parentAct = findParentAction(act, actions); Action parentAct = findParentAction(act, actions);
if (parentAct == null) { if (parentAct == null)
{
actionSet = createActionSet(act, parentAct, null); actionSet = createActionSet(act, parentAct, null);
actionSets.add(actionSet); actionSets.add(actionSet);
} else { }
if (!addToAactionSet(act, parentAct, actionSets)) { else
{
if (!addToAactionSet(act, parentAct, actionSets))
{
// The index of the parent action in the actions' list is larger than the index of this action. // The index of the parent action in the actions' list is larger than the index of this action.
actionSet = createActionSet(act, parentAct, null); actionSet = createActionSet(act, parentAct, null);
actionSets.add(actionSet); actionSets.add(actionSet);
@@ -54,17 +60,22 @@ public class HierarchicalRegrouper {
* Third, add the subActionSet to its parent ActionSet. * Third, add the subActionSet to its parent ActionSet.
*/ */
List<HierarchicalActionSet> reActionSets = new ArrayList<>(); List<HierarchicalActionSet> reActionSets = new ArrayList<>();
for (HierarchicalActionSet actSet : actionSets) { for (HierarchicalActionSet actSet : actionSets)
{
Action parentAct = actSet.getParentAction(); Action parentAct = actSet.getParentAction();
if (parentAct != null) { if (parentAct != null)
{
addToActionSets(actSet, parentAct, actionSets); addToActionSets(actSet, parentAct, actionSets);
} else { }
else
{
// TypeDeclaration, FieldDeclaration, MethodDeclaration, Statement. // TypeDeclaration, FieldDeclaration, MethodDeclaration, Statement.
// CatchClause, ConstructorInvocation, SuperConstructorInvocation, SwitchCase // CatchClause, ConstructorInvocation, SuperConstructorInvocation, SwitchCase
String astNodeType = actSet.getAstNodeType(); String astNodeType = actSet.getAstNodeType();
if (astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") || if (astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") ||
astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") || astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") ||
astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase")) { astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase"))
{
reActionSets.add(actSet); reActionSets.add(actSet);
} }
} }
@@ -74,25 +85,31 @@ public class HierarchicalRegrouper {
ITree movDelNode = null; ITree movDelNode = null;
for(HierarchicalActionSet a:reActionSets){ for (HierarchicalActionSet a : reActionSets)
{
a = removeBlocks(a); a = removeBlocks(a);
HierarchicalActionSet hierarchicalActionSet1 = postOrder(a).stream().collect(Collectors.toList()).get(0); HierarchicalActionSet hierarchicalActionSet1 = postOrder(a).stream().collect(Collectors.toList()).get(0);
Action action = hierarchicalActionSet1.getAction(); Action action = hierarchicalActionSet1.getAction();
if (hierarchicalActionSet1.getSubActions().size() == 0 && action instanceof Update){ if (hierarchicalActionSet1.getSubActions().size() == 0 && action instanceof Update)
{
List<ITree> collect = hierarchicalActionSet1.getNode().getChildren().stream().filter(x -> x.getType() == 14).collect(Collectors.toList()); List<ITree> collect = hierarchicalActionSet1.getNode().getChildren().stream().filter(x -> x.getType() == 14).collect(Collectors.toList());
// if(hierarchicalActionSet1.getNode().getChildren().size() == 1){ // if(hierarchicalActionSet1.getNode().getChildren().size() == 1){
if(collect.size() == 1){ if (collect.size() == 1)
// if(hierarchicalActionSet1.getNode().getChild(0).getType() == 14){ {
if(collect.get(0).getType() == 14){ // if(hierarchicalActionSet1.getNode().getChild(0).getType() == 14){
if (collect.get(0).getType() == 14)
{
continue; continue;
} }
} }
} }
else{ else
Predicate<HierarchicalActionSet> predicate = x->x.getAction() instanceof Move; {
Predicate<HierarchicalActionSet> predicate = x -> x.getAction() instanceof Move;
List<HierarchicalActionSet> collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList()); List<HierarchicalActionSet> collect = postOrder(a).stream().filter(predicate).collect(Collectors.toList());
if(collect.size() == 1){ if (collect.size() == 1)
{
HierarchicalActionSet hierarchicalActionSet = collect.get(0); HierarchicalActionSet hierarchicalActionSet = collect.get(0);
movDelNode = hierarchicalActionSet.getNode().getParent(); movDelNode = hierarchicalActionSet.getNode().getParent();
reActionSets1.add(a); reActionSets1.add(a);
@@ -100,8 +117,10 @@ public class HierarchicalRegrouper {
} }
} }
if( movDelNode != null){ if (movDelNode != null)
if(a.getNode().equals(movDelNode)){ {
if (a.getNode().equals(movDelNode))
{
continue; continue;
} }
} }
@@ -110,42 +129,44 @@ public class HierarchicalRegrouper {
} }
return reActionSets1; return reActionSets1;
// return reActionSets; // return reActionSets;
} }
private boolean isStatement(HierarchicalActionSet actSet){ private boolean isStatement(HierarchicalActionSet actSet)
{
String astNodeType = actSet.getAstNodeType(); String astNodeType = actSet.getAstNodeType();
if (astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") || return astNodeType.endsWith("TypeDeclaration") || astNodeType.endsWith("FieldDeclaration") || astNodeType.endsWith("EnumDeclaration") ||
astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") || astNodeType.endsWith("MethodDeclaration") || astNodeType.endsWith("Statement") ||
astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase")) { astNodeType.endsWith("ConstructorInvocation") || astNodeType.endsWith("CatchClause") || astNodeType.endsWith("SwitchCase");
return true;
} }
return false;
}
Predicate<HierarchicalActionSet> predicate = x-> isStatement(x);
private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet){ Predicate<HierarchicalActionSet> predicate = x -> isStatement(x);
private HierarchicalActionSet removeBlocks(HierarchicalActionSet actionSet)
{
List<HierarchicalActionSet> subActions = actionSet.getSubActions(); List<HierarchicalActionSet> subActions = actionSet.getSubActions();
Action action = actionSet.getAction(); Action action = actionSet.getAction();
if (subActions.size() == 1){ if (subActions.size() == 1)
{
HierarchicalActionSet subaction = subActions.get(0); HierarchicalActionSet subaction = subActions.get(0);
List<HierarchicalActionSet> collect = postOrder(subaction).stream().filter(predicate).collect(Collectors.toList()); List<HierarchicalActionSet> collect = postOrder(subaction).stream().filter(predicate).collect(Collectors.toList());
if(collect.size() == 0){ if (collect.size() == 0)
{
return actionSet; return actionSet;
} }
boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName())); boolean b = collect.stream().anyMatch(p -> p.getAction().getName().equals(subActions.get(0).getAction().getName()));
if(!b){ if (!b)
{
return actionSet; return actionSet;
} }
Action action1 = subaction.getAction(); Action action1 = subaction.getAction();
if(action.getClass().equals(action1.getClass()) && action.getName().equals("UPD")) { if (action.getClass().equals(action1.getClass()) && action.getName().equals("UPD"))
{
subaction.setParent(null); subaction.setParent(null);
return removeBlocks(subaction); return removeBlocks(subaction);
@@ -155,27 +176,34 @@ public class HierarchicalRegrouper {
} }
public List<HierarchicalActionSet> postOrder(HierarchicalActionSet a) { public List<HierarchicalActionSet> postOrder(HierarchicalActionSet a)
{
List<HierarchicalActionSet> trees = new ArrayList<>(); List<HierarchicalActionSet> trees = new ArrayList<>();
getAllSubActions(a, trees); getAllSubActions(a, trees);
return trees; return trees;
} }
private void getAllSubActions(HierarchicalActionSet a,List<HierarchicalActionSet> as) {
private void getAllSubActions(HierarchicalActionSet a, List<HierarchicalActionSet> as)
{
List<HierarchicalActionSet> subActions = a.getSubActions(); List<HierarchicalActionSet> subActions = a.getSubActions();
if (subActions.size() != 0){ if (subActions.size() != 0)
for (HierarchicalActionSet s : subActions) { {
for (HierarchicalActionSet s : subActions)
{
getAllSubActions(s, as); getAllSubActions(s, as);
} }
} }
as.add(a); as.add(a);
// List<HierarchicalActionSet> b = new ArrayList<HierarchicalActionSet>(); // List<HierarchicalActionSet> b = new ArrayList<HierarchicalActionSet>();
// for (HierarchicalActionSet child: this.getSubActions()) // for (HierarchicalActionSet child: this.getSubActions())
// b.add(child); // b.add(child);
// return b; // return b;
} }
private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent) {
private HierarchicalActionSet createActionSet(Action act, Action parentAct, HierarchicalActionSet parent)
{
HierarchicalActionSet actionSet = new HierarchicalActionSet(); HierarchicalActionSet actionSet = new HierarchicalActionSet();
actionSet.setAction(act); actionSet.setAction(act);
actionSet.setActionString(parseAction(act.toString())); actionSet.setActionString(parseAction(act.toString()));
@@ -185,24 +213,32 @@ public class HierarchicalRegrouper {
return actionSet; return actionSet;
} }
private String parseAction(String actStr1) { private String parseAction(String actStr1)
{
// UPD 25@@!a from !a to isTrue(a) at 69 // UPD 25@@!a from !a to isTrue(a) at 69
String[] actStrArrays = actStr1.split("@@"); String[] actStrArrays = actStr1.split("@@");
String actStr = ""; String actStr = "";
int length = actStrArrays.length; int length = actStrArrays.length;
for (int i =0; i < length - 1; i ++) { for (int i = 0; i < length - 1; i++)
{
String actStrFrag = actStrArrays[i]; String actStrFrag = actStrArrays[i];
int index = actStrFrag.lastIndexOf(" ") + 1; int index = actStrFrag.lastIndexOf(" ") + 1;
String nodeType = actStrFrag.substring(index); String nodeType = actStrFrag.substring(index);
if (!"".equals(nodeType)) { if (!"".equals(nodeType))
if (Character.isDigit(nodeType.charAt(0)) || (nodeType.startsWith("-") && Character.isDigit(nodeType.charAt(1)))) { {
try { if (Character.isDigit(nodeType.charAt(0)) || (nodeType.startsWith("-") && Character.isDigit(nodeType.charAt(1))))
{
try
{
int typeInt = Integer.parseInt(nodeType); int typeInt = Integer.parseInt(nodeType);
if (ASTNodeMap.map.containsKey(typeInt)) { if (ASTNodeMap.map.containsKey(typeInt))
{
String type = ASTNodeMap.map.get(Integer.parseInt(nodeType)); String type = ASTNodeMap.map.get(Integer.parseInt(nodeType));
nodeType = type; nodeType = type;
} }
} catch (NumberFormatException e) { }
catch (NumberFormatException e)
{
nodeType = actStrFrag.substring(index); nodeType = actStrFrag.substring(index);
} }
} }
@@ -214,20 +250,32 @@ public class HierarchicalRegrouper {
return actStr; return actStr;
} }
private void addToActionSets(HierarchicalActionSet actionSet, Action parentAct, List<HierarchicalActionSet> actionSets) { private void addToActionSets(HierarchicalActionSet actionSet, Action parentAct, List<HierarchicalActionSet> actionSets)
{
Action act = actionSet.getAction(); Action act = actionSet.getAction();
for (HierarchicalActionSet actSet : actionSets) { for (HierarchicalActionSet actSet : actionSets)
if (actSet.equals(actionSet)) continue; {
if (actSet.equals(actionSet))
{
continue;
}
Action action = actSet.getAction(); Action action = actSet.getAction();
if (!areRelatedActions(action, act)) continue; if (!areRelatedActions(action, act))
if (action.equals(parentAct)) { // actSet is the parent of actionSet. {
continue;
}
if (action.equals(parentAct))
{ // actSet is the parent of actionSet.
actionSet.setParent(actSet); actionSet.setParent(actSet);
actSet.getSubActions().add(actionSet); actSet.getSubActions().add(actionSet);
sortSubActions(actSet); sortSubActions(actSet);
break; break;
} else { }
if (isPossibileSubAction(action, act)) { else
{
if (isPossibileSubAction(action, act))
{
// SubAction range startPosition2 <= startPosition && startPosition + length <= startPosition2 + length2 // SubAction range startPosition2 <= startPosition && startPosition + length <= startPosition2 + length2
addToActionSets(actionSet, parentAct, actSet.getSubActions()); addToActionSets(actionSet, parentAct, actSet.getSubActions());
} }
@@ -235,51 +283,67 @@ public class HierarchicalRegrouper {
} }
} }
private boolean isPossibileSubAction(Action parent, Action child) { private boolean isPossibileSubAction(Action parent, Action child)
// if ((parent instanceof Update && !(child instanceof Addition)) {
// || (parent instanceof Delete && child instanceof Delete) // if ((parent instanceof Update && !(child instanceof Addition))
// || (parent instanceof Insert && (child instanceof Insert))) { // || (parent instanceof Delete && child instanceof Delete)
// int startPosition = child.getPosition(); // || (parent instanceof Insert && (child instanceof Insert))) {
// int length = child.getLength(); // int startPosition = child.getPosition();
// int startPosition2 = parent.getPosition(); // int length = child.getLength();
// int length2 = parent.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. // if (!(startPosition2 <= startPosition && startPosition + length <= startPosition2 + length2)) {
// return false; // // when act is not the sub-set of action.
// } // return false;
// } // }
// }
return true; return true;
} }
private void sortSubActions(HierarchicalActionSet actionSet) { private void sortSubActions(HierarchicalActionSet actionSet)
{
ListSorter<HierarchicalActionSet> sorter = new ListSorter<HierarchicalActionSet>(actionSet.getSubActions()); ListSorter<HierarchicalActionSet> sorter = new ListSorter<HierarchicalActionSet>(actionSet.getSubActions());
List<HierarchicalActionSet> subActions = sorter.sortAscending(); List<HierarchicalActionSet> subActions = sorter.sortAscending();
if (subActions != null) { if (subActions != null)
{
actionSet.setSubActions(subActions); actionSet.setSubActions(subActions);
} }
} }
private boolean addToAactionSet(Action act, Action parentAct, List<HierarchicalActionSet> actionSets) { private boolean addToAactionSet(Action act, Action parentAct, List<HierarchicalActionSet> actionSets)
for(HierarchicalActionSet actionSet : actionSets) { {
for (HierarchicalActionSet actionSet : actionSets)
{
Action action = actionSet.getAction(); Action action = actionSet.getAction();
if (!areRelatedActions(action, act)) continue; if (!areRelatedActions(action, act))
{
continue;
}
if (action.equals(parentAct)) { // actionSet is the parent of actSet. if (action.equals(parentAct))
{ // actionSet is the parent of actSet.
HierarchicalActionSet actSet = createActionSet(act, actionSet.getAction(), actionSet); HierarchicalActionSet actSet = createActionSet(act, actionSet.getAction(), actionSet);
actionSet.getSubActions().add(actSet); actionSet.getSubActions().add(actSet);
sortSubActions(actionSet); sortSubActions(actionSet);
return true; return true;
} else { }
if (isPossibileSubAction(action, act)) { else
{
if (isPossibileSubAction(action, act))
{
// SubAction range startPosition2 <= startPosition && startPosition + length <= startP + length2 // SubAction range startPosition2 <= startPosition && startPosition + length <= startP + length2
List<HierarchicalActionSet> subActionSets = actionSet.getSubActions(); List<HierarchicalActionSet> subActionSets = actionSet.getSubActions();
if (subActionSets.size() > 0) { if (subActionSets.size() > 0)
{
boolean added = addToAactionSet(act, parentAct, subActionSets); boolean added = addToAactionSet(act, parentAct, subActionSets);
if (added) { if (added)
{
return true; return true;
} else { }
else
{
continue; continue;
} }
} }
@@ -289,19 +353,23 @@ public class HierarchicalRegrouper {
return false; return false;
} }
private Action findParentAction(Action action, List<Action> actions) { private Action findParentAction(Action action, List<Action> actions)
{
ITree parent = action.getNode().getParent(); ITree parent = action.getNode().getParent();
if (action instanceof Addition) { if (action instanceof Addition)
{
parent = ((Addition) action).getParent(); // parent in the fixed source code tree parent = ((Addition) action).getParent(); // parent in the fixed source code tree
} }
if (parent.getType() == 55) { if (parent.getType() == 55)
{
int type = action.getNode().getType(); int type = action.getNode().getType();
// Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation // Modifier, NormalAnnotation, MarkerAnnotation, SingleMemberAnnotation
if (type != 83 && type != 77 && type != 78 && type != 79 if (type != 83 && type != 77 && type != 78 && type != 79
&& type != 5 && type != 39 && type != 43 && type != 74 && type != 75 && type != 5 && type != 39 && type != 43 && type != 74 && type != 75
&& type != 76 && type != 84 && type != 87 && type != 88 && type != 42) { && type != 76 && type != 84 && type != 87 && type != 88 && type != 42)
{
// ArrayType, PrimitiveType, SimpleType, ParameterizedType, // ArrayType, PrimitiveType, SimpleType, ParameterizedType,
// QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType, SimpleName // QualifiedType, WildcardType, UnionType, IntersectionType, NameQualifiedType, SimpleName
return null; return null;
@@ -310,9 +378,12 @@ public class HierarchicalRegrouper {
} }
for (Action act : actions) { for (Action act : actions)
if (act.getNode().equals(parent)) { {
if (areRelatedActions(act, action)) { if (act.getNode().equals(parent))
{
if (areRelatedActions(act, action))
{
return act; return act;
} }
} }
@@ -320,16 +391,16 @@ public class HierarchicalRegrouper {
return null; return null;
} }
private boolean areRelatedActions(Action parent, Action child) { 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 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. // }
if (parent instanceof Delete && !(child instanceof Delete))
{// If action is INS, its children must be MOV or INS.
return false; return false;
} }
if (parent instanceof Insert && !(child instanceof Addition)) {// If action is DEL, its children must be DEL. // If action is DEL, its children must be DEL.
return false; return !(parent instanceof Insert) || child instanceof Addition;
}
return true;
} }
} }
File diff suppressed because it is too large Load Diff
@@ -2,18 +2,22 @@ package edu.lu.uni.serval.richedit.ediff;
import java.io.File; import java.io.File;
public class MessageFile { public class MessageFile
{
private final File revFile;
private final File prevFile;
private final File diffEntryFile;
private File revFile;
private File prevFile;
private File diffEntryFile;
private File positionFile; private File positionFile;
private String project; private String project;
public MessageFile(File revFile, File prevFile, File diffEntryFile,String project) { public MessageFile(File revFile, File prevFile, File diffEntryFile, String project)
{
super(); super();
this.revFile = revFile; this.revFile = revFile;
this.prevFile = prevFile; this.prevFile = prevFile;
@@ -21,26 +25,38 @@ public class MessageFile {
this.project = project; this.project = project;
} }
public String getProject() { return project;} public String getProject()
{
return project;
}
public void setProject(String project) { this.project = project;} public void setProject(String project)
public File getRevFile() { {
this.project = project;
}
public File getRevFile()
{
return revFile; return revFile;
} }
public File getPrevFile() { public File getPrevFile()
{
return prevFile; return prevFile;
} }
public File getDiffEntryFile() { public File getDiffEntryFile()
{
return diffEntryFile; return diffEntryFile;
} }
public File getPositionFile() { public File getPositionFile()
{
return positionFile; return positionFile;
} }
public void setPositionFile(File positionFile) { public void setPositionFile(File positionFile)
{
this.positionFile = positionFile; this.positionFile = positionFile;
} }
@@ -8,53 +8,63 @@ import java.io.File;
* Parse fix patterns with GumTree. * Parse fix patterns with GumTree.
* *
* @author kui.liu * @author kui.liu
*
*/ */
public abstract class Parser implements ParserInterface { public abstract class Parser implements ParserInterface
{
protected String astEditScripts = ""; // it will be used for fix patterns mining. protected String astEditScripts = ""; // it will be used for fix patterns mining.
protected String patchesSourceCode = ""; // testing protected String patchesSourceCode = ""; // testing
protected String buggyTrees = ""; // Compute similarity for bug localization. protected String buggyTrees = ""; // Compute similarity for bug localization.
protected String sizes = ""; // fix patterns' selection before mining. protected String sizes = ""; // fix patterns' selection before mining.
protected String tokensOfSourceCode = ""; // Compute similarity for bug localization. protected String tokensOfSourceCode = ""; // Compute similarity for bug localization.
protected String originalTree = ""; // Guide of generating patches. protected String originalTree = ""; // Guide of generating patches.
protected String actionSets = ""; // Guide of generating patches. 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); public abstract void parseFixPatterns(File prevFile, File revFile, File diffEntryFile, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava);
@Override @Override
public String getAstEditScripts() { public String getAstEditScripts()
{
return astEditScripts; return astEditScripts;
} }
@Override @Override
public String getPatchesSourceCode() { public String getPatchesSourceCode()
{
return patchesSourceCode; return patchesSourceCode;
} }
// @Override // @Override
// public String getBuggyTrees() { // public String getBuggyTrees() {
// return buggyTrees; // return buggyTrees;
// } // }
@Override @Override
public String getSizes() { public String getSizes()
{
return sizes; return sizes;
} }
@Override @Override
public String getTokensOfSourceCode() { public String getTokensOfSourceCode()
{
return tokensOfSourceCode; return tokensOfSourceCode;
} }
// @Override // @Override
// public String getOriginalTree() { // public String getOriginalTree() {
// return originalTree; // return originalTree;
// } // }
// //
// @Override // @Override
// public String getActionSets() { // public String getActionSets() {
// return actionSets; // return actionSets;
// } // }
} }
@@ -1,21 +1,22 @@
package edu.lu.uni.serval.richedit.ediff; package edu.lu.uni.serval.richedit.ediff;
public interface ParserInterface { public interface ParserInterface
{
// public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile); // public void parseFixPatterns(File prevFile, File revFile, File diffEntryFile);
public String getAstEditScripts(); String getAstEditScripts();
public String getPatchesSourceCode(); String getPatchesSourceCode();
// public String getBuggyTrees(); // public String getBuggyTrees();
public String getSizes(); String getSizes();
public String getTokensOfSourceCode(); String getTokensOfSourceCode();
// public String getOriginalTree(); // public String getOriginalTree();
// public String getActionSets(); // public String getActionSets();
} }
@@ -4,26 +4,37 @@ import redis.clients.jedis.JedisPool;
import java.io.File; import java.io.File;
public class RunnableParser implements Runnable { public class RunnableParser implements Runnable
{
private final File prevFile;
private final File revFile;
private final File diffEntryFile;
private final Parser parser;
private File prevFile;
private File revFile;
private File diffEntryFile;
private Parser parser;
private String project; private String project;
private JedisPool pool; private JedisPool pool;
private String srcMLPath; private String srcMLPath;
private String rootType; private String rootType;
boolean isJava; boolean isJava;
public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser) { public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser)
{
this.prevFile = prevFile; this.prevFile = prevFile;
this.revFile = revFile; this.revFile = revFile;
this.diffEntryFile = diffEntryFile; this.diffEntryFile = diffEntryFile;
this.parser = parser; this.parser = parser;
} }
public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool) { public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool)
{
this.prevFile = prevFile; this.prevFile = prevFile;
this.revFile = revFile; this.revFile = revFile;
this.diffEntryFile = diffEntryFile; this.diffEntryFile = diffEntryFile;
@@ -32,7 +43,8 @@ public class RunnableParser implements Runnable {
this.pool = innerPool; this.pool = innerPool;
} }
public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool,String srcMLPath,String rootType,boolean isJava) { public RunnableParser(File prevFile, File revFile, File diffEntryFile, Parser parser, String project, JedisPool innerPool, String srcMLPath, String rootType, boolean isJava)
{
this.prevFile = prevFile; this.prevFile = prevFile;
this.revFile = revFile; this.revFile = revFile;
this.diffEntryFile = diffEntryFile; this.diffEntryFile = diffEntryFile;
@@ -45,7 +57,8 @@ public class RunnableParser implements Runnable {
} }
@Override @Override
public void run() { public void run()
parser.parseFixPatterns(prevFile, revFile, diffEntryFile,project,pool,srcMLPath,rootType,isJava); {
parser.parseFixPatterns(prevFile, revFile, diffEntryFile, project, pool, srcMLPath, rootType, isJava);
} }
} }
@@ -13,174 +13,83 @@ import redis.clients.jedis.JedisPool;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
/** /**
* Created by anilkoyuncu on 03/04/2018. * 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
{
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";
String port = portDumps; //"6399"; String port = portDumps; //"6399";
CallShell cs = new CallShell(); CallShell cs = new CallShell();
String cmd = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; String cmd = "bash " + redisPath + "/" + "startServer.sh" + " %s %s %s";
cmd = String.format(cmd, redisPath,dumpsName,Integer.valueOf(port)); cmd = String.format(cmd, redisPath, dumpsName, Integer.valueOf(port));
log.info(cmd); log.info(cmd);
cs.runShell(cmd, port); CallShell.runShell(cmd, port);
// String cmdInner = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; final JedisPool outerPool = new JedisPool(PoolBuilder.getPoolConfig(), "localhost", Integer.valueOf(port), 20000000);
// cmdInner = String.format(cmdInner, redisPath,compareDBName,Integer.valueOf(portInner));
// log.info(cmdInner);
// cs.runShell(cmdInner, portInner);
// 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<String> listOfPairs = AkkaTreeParser.getMessages(innerPool,Integer.valueOf(numOfWorkers));
HashMap<String, String> filenames = getFilenames(outerPool); HashMap<String, String> filenames = getFilenames(outerPool);
String job= getLevel(outerPool); String job = getLevel(outerPool);
// List<String> listOfPairs = AkkaTreeParser.files2compare(outerPool);
// ArrayList<String> samePairs = new ArrayList<>();
ArrayList<String> errorPairs = new ArrayList<>(); ArrayList<String> errorPairs = new ArrayList<>();
// Integer numberOfWorkers = Integer.valueOf(numOfWorkers);
// final ExecutorService executor = Executors.newWorkStealingPool(numberOfWorkers);
Long compare; Long compare;
try (Jedis inner = outerPool.getResource()) { try (Jedis inner = outerPool.getResource())
{
compare = inner.scard("compare"); compare = inner.scard("compare");
} }
IntStream stream = IntStream.range(0, compare.intValue()); IntStream stream = IntStream.range(0, compare.intValue());
String finalJob = job; String finalJob = job;
ProgressBar.wrap(stream. ProgressBar.wrap(stream.parallel(), "Task").forEach(m ->
parallel(),"Task").
forEach(m ->
{ {
newCoreCompare(finalJob, errorPairs, filenames, outerPool); newCoreCompare(finalJob, errorPairs, filenames, outerPool);
} }
); );
//
// ArrayList<Future<?>> results = new ArrayList<Future<?>>();
// for (int i = 0; i <numberOfWorkers ; i++) {
//
//
// // schedule the work
// log.info("Starting job {}",i);
// final Future<?> 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();
log.info("End process"); log.info("End process");
} }
public static boolean newCoreCompare(String treeType, ArrayList<String> errorPairs, HashMap<String, String> filenames, JedisPool outerPool)
// public static class RunnableCompare implements Runnable { {
// String job;
// ArrayList<String> errorPairs;
// HashMap<String, String> filenames;
// JedisPool outerPool;
// Integer threadID;
//
// public RunnableCompare(String treeType,ArrayList<String> errorPairs, HashMap<String, String> 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<String> errorPairs, HashMap<String, String> filenames,JedisPool outerPool ) {
String pairName = null; String pairName = null;
try (Jedis outer = outerPool.getResource()) { try (Jedis outer = outerPool.getResource())
{
pairName = outer.spop("compare"); pairName = outer.spop("compare");
// } // }
if(pairName.equals("0")) if (pairName.equals("0"))
{
return true; return true;
}
String matchKey = null; String matchKey = null;
// try {
String[] split = pairName.split("/"); String[] split = pairName.split("/");
String i = split[1]; String i = split[1];
String j = split[2]; String j = split[2];
String keyName = split[0]; String keyName = split[0];
matchKey = keyName + "/" + (String.valueOf(i)) + "/" + String.valueOf(j); matchKey = keyName + "/" + (i) + "/" + j;
if (matchKey == null){ if (matchKey == null)
{
return false; return false;
} }
Map<String, String> oldTreeString = EDiffHelper.getTreeString(keyName, i, outerPool, filenames); Map<String, String> oldTreeString = EDiffHelper.getTreeString(keyName, i, outerPool, filenames);
Map<String, String> newTreeString = EDiffHelper.getTreeString(keyName, j, outerPool, filenames); Map<String, String> newTreeString = EDiffHelper.getTreeString(keyName, j, outerPool, filenames);
switch (treeType) { switch (treeType)
{
case "single": case "single":
String oldShapeTree = oldTreeString.get("shapeTree");
String oldShapeTree =oldTreeString.get("shapeTree"); String newShapeTree = newTreeString.get("shapeTree");
String newShapeTree =newTreeString.get("shapeTree");
String oldActionTree = oldTreeString.get("actionTree"); String oldActionTree = oldTreeString.get("actionTree");
String newActionTree = newTreeString.get("actionTree"); String newActionTree = newTreeString.get("actionTree");
@@ -189,12 +98,14 @@ public class CompareTrees {
String newTargetTree = newTreeString.get("targetTree"); String newTargetTree = newTreeString.get("targetTree");
if (oldShapeTree.equals(newShapeTree)) { if (oldShapeTree.equals(newShapeTree))
if (oldActionTree.equals(newActionTree)) { {
if (oldTargetTree.equals(newTargetTree)) { if (oldActionTree.equals(newActionTree))
// samePairs.add(matchKey); {
try (Jedis jedis = outerPool.getResource()) { if (oldTargetTree.equals(newTargetTree))
//// jedis.del(matchKey); {
try (Jedis jedis = outerPool.getResource())
{
jedis.select(2); jedis.select(2);
jedis.set(matchKey, "1"); jedis.set(matchKey, "1");
} }
@@ -206,61 +117,54 @@ public class CompareTrees {
String oldTokens = oldTreeString.get("tokens"); String oldTokens = oldTreeString.get("tokens");
String newTokens = newTreeString.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(); JaroWinklerDistance jwd = new JaroWinklerDistance();
//
//
Double overallSimi = Double.valueOf(0); Double overallSimi = Double.valueOf(0);
// if (!(oldTokens.trim().isEmpty() || newTokens.trim().isEmpty()))
if (!(oldTokens.trim().isEmpty() || newTokens.trim().isEmpty())) { {
overallSimi = jwd.apply(oldTokens, newTokens); overallSimi = jwd.apply(oldTokens, newTokens);
} }
int retval = Double.compare(overallSimi, Double.valueOf(1)); int retval = Double.compare(overallSimi, Double.valueOf(1));
if (retval >= 0) { if (retval >= 0)
try (Jedis jedis = outerPool.getResource()) { {
try (Jedis jedis = outerPool.getResource())
{
jedis.select(3); jedis.select(3);
jedis.set(matchKey, "1"); jedis.set(matchKey, "1");
} }
} }
return true; return true;
default: default:
return true; return true;
// break;
} }
}catch (Exception e) { }
catch (Exception e)
{
errorPairs.add(pairName); errorPairs.add(pairName);
if (pairName == null) if (pairName == null) return false;
return false;
log.debug("{} not comparable", pairName); log.debug("{} not comparable", pairName);
} }
return true; return true;
} }
public static String getLevel(JedisPool innerPool)
{
HashMap<String, String> fileMap = new HashMap<String, String>();
public static String getLevel(JedisPool innerPool){ try (Jedis inner = innerPool.getResource())
{
while (!inner.ping().equals("PONG"))
HashMap<String, String> fileMap =new HashMap<String, String>(); {
try (Jedis inner = innerPool.getResource()) {
while (!inner.ping().equals("PONG")){
log.info("wait"); log.info("wait");
} }
inner.select(1); inner.select(1);
String level = inner.get("level"); String level = inner.get("level");
switch (level){ switch (level)
{
case "l1": case "l1":
return "single"; return "single";
case "l2": case "l2":
@@ -268,47 +172,29 @@ public class CompareTrees {
default: default:
return ""; return "";
} }
} }
} }
public static HashMap<String, String> getFilenames(JedisPool innerPool){ public static HashMap<String, String> getFilenames(JedisPool innerPool)
{
HashMap<String, String> fileMap = new HashMap<String, String>();
try (Jedis inner = innerPool.getResource())
HashMap<String, String> fileMap =new HashMap<String, String>(); {
while (!inner.ping().equals("PONG"))
try (Jedis inner = innerPool.getResource()) { {
while (!inner.ping().equals("PONG")){
log.info("wait"); log.info("wait");
} }
inner.select(1); inner.select(1);
Map<String, String> filenames = inner.hgetAll("filenames"); Map<String, String> filenames = inner.hgetAll("filenames");
for (Map.Entry<String, String> stringStringEntry : filenames.entrySet().stream().collect(Collectors.toList()))
for (Map.Entry<String, String> stringStringEntry : filenames.entrySet().stream().collect(Collectors.toList())) { {
fileMap.put(stringStringEntry.getKey(),stringStringEntry.getValue()); fileMap.put(stringStringEntry.getKey(), stringStringEntry.getValue());
}
} }
}
// log.info("Getting results %d",fileMap.s);
return fileMap; return fileMap;
} }
} }
@@ -1,9 +1,9 @@
package edu.lu.uni.serval.richedit.jobs; package edu.lu.uni.serval.richedit.jobs;
import edu.lu.uni.serval.richedit.ediff.EDiffHunkParser; 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.richedit.ediff.MessageFile;
import edu.lu.uni.serval.utils.CallShell; import edu.lu.uni.serval.utils.CallShell;
import edu.lu.uni.serval.utils.FileHelper;
import edu.lu.uni.serval.utils.PoolBuilder; import edu.lu.uni.serval.utils.PoolBuilder;
import me.tongfei.progressbar.ProgressBar; import me.tongfei.progressbar.ProgressBar;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -25,7 +25,7 @@ import java.util.stream.Stream;
public class EnhancedASTDiff 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, 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 String[] projectList, String patchSize, String projectType, String srcPath) throws Exception
@@ -35,19 +35,12 @@ public class EnhancedASTDiff
CallShell cs = new CallShell(); 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 = 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)); CallShell.runShell(cmd, redisPort);
cs.runShell(cmd, redisPort);
JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1", Integer.valueOf(redisPort), 20000000); JedisPool innerPool = new JedisPool(PoolBuilder.getPoolConfig(), "127.0.0.1", Integer.valueOf(redisPort), 20000000);
boolean isJava = false; boolean isJava = projectType.equals("java");
if (projectType.equals("java"))
{
isJava = true;
}
File folder = new File(inputPath); File folder = new File(inputPath);
File[] listOfFiles = folder.listFiles(); File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) if (listOfFiles == null)
@@ -80,22 +73,17 @@ public class EnhancedASTDiff
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
String project = folder.getName(); String project = folder.getName();
List<MessageFile> allMessageFiles = new ArrayList<>(); List<MessageFile> allMessageFiles = new ArrayList<>();
for (File target : folders) for (File target : folders)
{ {
List<MessageFile> msgFiles = getMessageFiles(target.toString() + "/", project, patchSize, isJava); //"/Users/anilkoyuncu/bugStudy/code/python/GumTreeInput/Apache/CAMEL/" List<MessageFile> msgFiles = getMessageFiles(target.toString() + "/", project, patchSize, isJava); //"/Users/anilkoyuncu/bugStudy/code/python/GumTreeInput/Apache/CAMEL/"
// msgFiles = msgFiles.subList(0,3000);
if (msgFiles == null) if (msgFiles == null)
{ {
continue; continue;
} }
allMessageFiles.addAll(msgFiles); allMessageFiles.addAll(msgFiles);
} }
Map<String, String> diffEntry; Map<String, String> diffEntry;
@@ -112,24 +100,12 @@ public class EnhancedASTDiff
log.info("{} files to process ...", allMessageFiles.size()); log.info("{} files to process ...", allMessageFiles.size());
} }
boolean finalIsJava = isJava; boolean finalIsJava = isJava;
ProgressBar.wrap(allMessageFiles.stream(). ProgressBar.wrap(allMessageFiles.stream().parallel(), "Task").forEach(m ->
parallel(), "Task").
forEach(m ->
{ {
EDiffHunkParser parser = new EDiffHunkParser(); EDiffHunkParser parser = new EDiffHunkParser();
parser.parseFixPatterns(m.getPrevFile(), m.getRevFile(), m.getDiffEntryFile(), project, innerPool, srcMLPath, hunkLimit, finalIsJava); 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(); File[] revFiles = revFilesPath.listFiles();
if (revFiles != null) if (revFiles != null)
{ {
// List<File> 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<MessageFile> msgFiles = new ArrayList<>(); List<MessageFile> msgFiles = new ArrayList<>();
for (File revFile : revFiles) for (File revFile : revFiles)
{ {
// for (File revFile : collect) {
String fileName = revFile.getName(); String fileName = revFile.getName();
File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName);// previous file File prevFile = new File(gumTreeInput + "prevFiles/prev_" + fileName); // previous file
// if (isJava){
// fileName = fileName.replace(".java",".txt");
// }else{
// fileName = fileName + ".txt";
// }
fileName = fileName + ".txt"; fileName = fileName + ".txt";
File diffentryFile = new File(gumTreeInput + "DiffEntries/" + fileName); // DiffEntry file File diffentryFile = new File(gumTreeInput + "DiffEntries/" + fileName); // DiffEntry file
String s = FileHelper.readFile(diffentryFile); String s = FileHelper.readFile(diffentryFile);
@@ -165,15 +133,7 @@ public class EnhancedASTDiff
{ {
count++; count++;
} }
if (count >= Integer.valueOf(patchSize)) if (count >= Integer.valueOf(patchSize)) continue;
// if(count>201)
{
continue;
}
// if(FileHelper.readFile(diffentryFile).split("@@\\s\\-\\d+,*\\d*\\s\\+\\d+,*\\d*\\s@@").length > 2)
// continue;
// String datasetName = project;
String[] split1 = diffentryFile.getParent().split(datasetName); String[] split1 = diffentryFile.getParent().split(datasetName);
String root = split1[0]; String root = split1[0];
String pj = split1[1].split("/")[1]; String pj = split1[1].split("/")[1];
@@ -181,16 +141,10 @@ public class EnhancedASTDiff
MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile, pj); MessageFile msgFile = new MessageFile(revFile, prevFile, diffentryFile, pj);
msgFiles.add(msgFile); msgFiles.add(msgFile);
} }
return msgFiles; return msgFiles;
} }
else else return null;
{
return null;
} }
}
} }
@@ -3,12 +3,13 @@ package edu.lu.uni.serval.utils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class ASTNodeMap { public class ASTNodeMap
{
public static Map<Integer, String> map; public static Map<Integer, String> map;
static { static
map = new HashMap<Integer, String>(); {
map = new HashMap<>();
map.put(-3, "Instanceof"); map.put(-3, "Instanceof");
map.put(-2, "New"); map.put(-2, "New");
map.put(-1, "Operator"); map.put(-1, "Operator");
@@ -106,11 +107,10 @@ public class ASTNodeMap {
map.put(91, "SuperMethodReference"); map.put(91, "SuperMethodReference");
map.put(92, "TypeMethodReference"); map.put(92, "TypeMethodReference");
map.put(100, "Insert");
map.put(100,"Insert"); map.put(101, "Update");
map.put(101,"Update"); map.put(102, "Delete");
map.put(102,"Delete"); map.put(103, "Move");
map.put(103,"Move"); map.put(104, "NoChange");
map.put(104,"NoChange");
} }
} }
@@ -1,9 +1,5 @@
package edu.lu.uni.serval.utils; package edu.lu.uni.serval.utils;
/**
* Created by anilkoyuncu on 17/04/2018.
*/
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -12,109 +8,83 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class CallShell { /**
* Created by anilkoyuncu on 17/04/2018.
private static Logger log = LoggerFactory.getLogger(CallShell.class); */
public class CallShell
{
private static final Logger log = LoggerFactory.getLogger(CallShell.class);
public void runShell(String command) throws Exception {
public void runShell(String command) throws Exception
{
Process process = Runtime.getRuntime().exec(command); Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader( BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.getInputStream()));
String s; String s;
while ((s = reader.readLine()) != null) { while ((s = reader.readLine()) != null)
{
System.out.println("Script output: " + s); 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); log.trace(command);
Process process = Runtime.getRuntime().exec(command); Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader( BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.getInputStream()));
String s; String s;
while ((s = reader.readLine()) != null) { while ((s = reader.readLine()) != null)
// System.out.println("Script output: " + s); {
log.trace("Script output: " + s); log.trace("Script output: " + s);
} }
// Thread.sleep(Integer.valueOf(serverWait));
String cmd = "redis-cli -p %s ping"; 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); runPing(cmd1);
} }
public static void runPing(String command) throws Exception { public static void runPing(String command) throws Exception
try{ {
try
{
StringBuffer output = new StringBuffer(); StringBuffer output = new StringBuffer();
Process process = Runtime.getRuntime().exec(command); Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader( BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
process.getInputStream()));
String s; String s;
BufferedReader stdError = new BufferedReader(new BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
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(); s = reader.readLine();
if(s !=null && s.equals("PONG")){ if (s != null && s.equals("PONG"))
// System.out.println(s); {
log.trace(s); log.trace(s);
}
}else{ else
{
String e; String e;
if((e = stdError.readLine()) == null) { if ((e = stdError.readLine()) == null)
{
TimeUnit.MINUTES.sleep(1); TimeUnit.MINUTES.sleep(1);
runPing(command); runPing(command);
}else{ }
else
{
TimeUnit.MINUTES.sleep(1); TimeUnit.MINUTES.sleep(1);
System.out.println(e); System.out.println(e);
} }
} }
// System.out.println(s); while ((s = stdError.readLine()) != null)
// }
// 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); System.out.print(s);
} }
catch (IOException e)
// System.exit(0); {
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: "); System.out.println("exception happened - here's what I know: ");
e.printStackTrace(); e.printStackTrace();
System.exit(-1); 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");
}
} }
@@ -6,44 +6,45 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
public class ClusterToPattern { public class ClusterToPattern
private static Logger log = LoggerFactory.getLogger(ClusterToPattern.class); {
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(); CallShell cs = new CallShell();
String cmd = "bash "+redisPath + "/" + "startServer.sh" +" %s %s %s"; String cmd = "bash " + redisPath + "/" + "startServer.sh" + " %s %s %s";
cmd = String.format(cmd, redisPath,dumpsName,Integer.valueOf(port)); cmd = String.format(cmd, redisPath, dumpsName, Integer.valueOf(port));
log.trace(cmd); log.trace(cmd);
cs.runShell(cmd, port); CallShell.runShell(cmd, port);
String host = "localhost";//args[5]; 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); String export = export(parameter, outerPool);
System.out.println(export); 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()) { try (Jedis outer = outerPool.getResource())
while (!outer.ping().equals("PONG")) { {
while (!outer.ping().equals("PONG"))
{
log.info("wait"); log.info("wait");
} }
// byte[] s = outer.hget("dump".getBytes(), filename.getBytes()); // byte[] s = outer.hget("dump".getBytes(), filename.getBytes());
// HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); // HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s);
// if (actionSet == null){ // if (actionSet == null){
// throw new Error(filename +" not found"); // throw new Error(filename +" not found");
// } // }
String s1 = outer.hget("dump",filename); String s1 = outer.hget("dump", filename);
// outer.close(); // outer.close();
return s1; return s1;
} }
} }
@@ -21,57 +21,67 @@ import java.util.stream.Collectors;
/** /**
* Created by anilkoyuncu on 17/09/2018. * Created by anilkoyuncu on 17/09/2018.
*/ */
public class EDiffHelper { public class EDiffHelper
private static Logger log = LoggerFactory.getLogger(EDiffHelper.class); {
private static final Logger log = LoggerFactory.getLogger(EDiffHelper.class);
/** Read the object from Base64 string. */ /** Read the object from Base64 string. */
public static Object fromString( String s ) throws IOException, public static Object fromString(String s) throws IOException,
ClassNotFoundException { ClassNotFoundException
byte [] data = Base64.getDecoder().decode( s ); {
byte[] data = Base64.getDecoder().decode(s);
ObjectInputStream ois = new ObjectInputStream( ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) ); new ByteArrayInputStream(data));
Object o = ois.readObject(); Object o = ois.readObject();
ois.close(); ois.close();
return o; return o;
} }
/** Write the object to a Base64 string. */ /** 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(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos ); ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject( o ); oos.writeObject(o);
oos.close(); oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray()); return Base64.getEncoder().encodeToString(baos.toByteArray());
} }
public static Object fromByteArray( byte[] data ) throws IOException, public static Object fromByteArray(byte[] data) throws IOException,
ClassNotFoundException { ClassNotFoundException
// byte [] data = Base64.getDecoder().decode( s ); {
// byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream( ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream( data ) ); new ByteArrayInputStream(data));
Object o = ois.readObject(); Object o = ois.readObject();
ois.close(); ois.close();
return o; return o;
} }
/** Write the object to a Base64 string. */ /** 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(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos ); ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject( o ); oos.writeObject(o);
oos.close(); oos.close();
return baos.toByteArray(); return baos.toByteArray();
} }
public static byte[] commonsSerialize(Serializable o){ public static byte[] commonsSerialize(Serializable o)
{
return SerializationUtils.serialize(o); return SerializationUtils.serialize(o);
} }
public static Object commonsDeserialize(byte[] data){ public static Object commonsDeserialize(byte[] data)
{
return SerializationUtils.deserialize(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; int newType = 0;
@@ -80,36 +90,46 @@ public class EDiffHelper {
String label = actionSet.getAction().toString(); String label = actionSet.getAction().toString();
Map<Integer, String> nodeMap; Map<Integer, String> nodeMap;
if(isJava){ if (isJava)
{
nodeMap = ASTNodeMap.map; nodeMap = ASTNodeMap.map;
}else{ }
else
{
nodeMap = NodeMap_new.map; nodeMap = NodeMap_new.map;
} }
// List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType); // List<Integer> keysByValue = getKeysByValue(ASTNodeMap.map, astNodeType);
List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType); List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType);
if(keysByValue.size() != 1){ if (keysByValue.size() != 1)
{
log.error("More than 1"); log.error("More than 1");
} }
newType = keysByValue.get(0); newType = keysByValue.get(0);
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
//root //root
parent = tc.createTree(newType, label, null); parent = tc.createTree(newType, label, null);
tc.setRoot(parent); tc.setRoot(parent);
}else{ }
else
{
children = tc.createTree(newType, label, null); children = tc.createTree(newType, label, null);
children.setParentAndUpdateChildren(parent); children.setParentAndUpdateChildren(parent);
} }
List<HierarchicalActionSet> subActions = actionSet.getSubActions(); List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){ if (subActions.size() != 0)
for (HierarchicalActionSet subAction : subActions) { {
for (HierarchicalActionSet subAction : subActions)
{
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
children = parent; 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; int newType = 0;
String astNodeType = null; String astNodeType = null;
Map<Integer, String> nodeMap; Map<Integer, String> nodeMap;
if(isJava){ if (isJava)
{
nodeMap = ASTNodeMap.map; nodeMap = ASTNodeMap.map;
}else{ }
else
{
nodeMap = NodeMap_new.map; nodeMap = NodeMap_new.map;
} }
Action action = actionSet.getAction(); Action action = actionSet.getAction();
if (action instanceof Update){ if (action instanceof Update)
{
astNodeType = actionSet.getAstNodeType(); astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType); List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType);
if(keysByValue.size() != 1){ if (keysByValue.size() != 1)
{
log.error("More than 1"); log.error("More than 1");
} }
newType = keysByValue.get(0); newType = keysByValue.get(0);
}else if(action instanceof Insert){ }
newType = ((Insert)action).getParent().getType(); else if (action instanceof Insert)
}else if(action instanceof Move){ {
newType = ((Move)action).getParent().getType(); newType = ((Insert) action).getParent().getType();
}else if(action instanceof Delete){ }
else if (action instanceof Move)
{
newType = ((Move) action).getParent().getType();
}
else if (action instanceof Delete)
{
astNodeType = actionSet.getAstNodeType(); astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType); List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType);
if(keysByValue.size() != 1){ if (keysByValue.size() != 1)
{
log.error("More than 1"); log.error("More than 1");
} }
newType = keysByValue.get(0); newType = keysByValue.get(0);
} }
if (actionSet.getParent() == null)
if(actionSet.getParent() == null){ {
//root //root
// parent = new Tree(newType,""); // parent = new Tree(newType,"");
parent = tc.createTree(newType, "", null); parent = tc.createTree(newType, "", null);
tc.setRoot(parent); tc.setRoot(parent);
}else{ }
// children = new Tree(newType,""); else
// parent.addChild(children); {
// children = new Tree(newType,"");
// parent.addChild(children);
children = tc.createTree(newType, "", null); children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent); children.setParentAndUpdateChildren(parent);
} }
List<HierarchicalActionSet> subActions = actionSet.getSubActions(); List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){ if (subActions.size() != 0)
for (HierarchicalActionSet subAction : subActions) { {
for (HierarchicalActionSet subAction : subActions)
{
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
children = parent; children = parent;
} }
getTargetTree(subAction,children,null,tc,isJava); getTargetTree(subAction, children, null, tc, isJava);
} }
@@ -187,43 +224,54 @@ public class EDiffHelper {
return parent; 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; int newType = 0;
Map<Integer, String> nodeMap; Map<Integer, String> nodeMap;
if(isJava){ if (isJava)
{
nodeMap = ASTNodeMap.map; nodeMap = ASTNodeMap.map;
}else{ }
else
{
nodeMap = NodeMap_new.map; nodeMap = NodeMap_new.map;
} }
String astNodeType = actionSet.getAstNodeType(); String astNodeType = actionSet.getAstNodeType();
List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType); List<Integer> keysByValue = getKeysByValue(nodeMap, astNodeType);
if(keysByValue.size() != 1){ if (keysByValue.size() != 1)
{
log.error("More than 1"); log.error("More than 1");
} }
newType = keysByValue.get(0); newType = keysByValue.get(0);
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
//root //root
// parent = new Tree(newType,""); // parent = new Tree(newType,"");
parent = tc.createTree(newType, "", null); parent = tc.createTree(newType, "", null);
tc.setRoot(parent); tc.setRoot(parent);
}else{ }
// children = new Tree(newType,""); else
// parent.addChild(children); {
// children = new Tree(newType,"");
// parent.addChild(children);
children = tc.createTree(newType, "", null); children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent); children.setParentAndUpdateChildren(parent);
} }
List<HierarchicalActionSet> subActions = actionSet.getSubActions(); List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){ if (subActions.size() != 0)
for (HierarchicalActionSet subAction : subActions) { {
for (HierarchicalActionSet subAction : subActions)
{
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
children = parent; children = parent;
} }
getASTTree(subAction,children,null,tc,isJava); getASTTree(subAction, children, null, tc, isJava);
} }
@@ -233,7 +281,8 @@ public class EDiffHelper {
} }
public static <T, E> List<T> getKeysByValue(Map<T, E> map, E value) { public static <T, E> List<T> getKeysByValue(Map<T, E> map, E value)
{
return map.entrySet() return map.entrySet()
.stream() .stream()
.filter(entry -> Objects.equals(entry.getValue(), value)) .filter(entry -> Objects.equals(entry.getValue(), value))
@@ -241,69 +290,70 @@ public class EDiffHelper {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
// public static ITree getSimpliedTree(String prefix, String fn, JedisPool outerPool) { // public static ITree getSimpliedTree(String prefix, String fn, JedisPool outerPool) {
// //
// ITree tree = null; // ITree tree = null;
// Jedis inner = null; // Jedis inner = null;
// try { // try {
// inner = outerPool.getResource(); // inner = outerPool.getResource();
// while (!inner.ping().equals("PONG")){ // while (!inner.ping().equals("PONG")){
// log.info("wait"); // log.info("wait");
// } // }
// inner.select(1); // inner.select(1);
// String dist2load = inner.get(prefix+"-"+fn); // String dist2load = inner.get(prefix+"-"+fn);
// inner.select(0); // inner.select(0);
// String s = inner.get(prefix.replace("-","/") +"/"+dist2load); // String s = inner.get(prefix.replace("-","/") +"/"+dist2load);
// HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s); // HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s);
// //
// ITree parent = null; // ITree parent = null;
// ITree children =null; // ITree children =null;
// TreeContext tc = new TreeContext(); // TreeContext tc = new TreeContext();
// tree = EDiffHelper.getASTTree(actionSet, parent, children,tc); // tree = EDiffHelper.getASTTree(actionSet, parent, children,tc);
// tree.setParent(null); // tree.setParent(null);
// tc.validate(); // tc.validate();
// //
// } catch (IOException e) { // } catch (IOException e) {
// e.printStackTrace(); // e.printStackTrace();
// } catch (ClassNotFoundException e) { // } catch (ClassNotFoundException e) {
// e.printStackTrace(); // e.printStackTrace();
// }finally { // }finally {
// if (inner != null) { // if (inner != null) {
// inner.close(); // inner.close();
// } // }
// } // }
// return tree; // return tree;
// //
// } // }
// public static ITree getShapes(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) { // public static ITree getShapes(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) {
// //
// ITree tree = null; // ITree tree = null;
// //
// try (Jedis outer = outerPool.getResource()) { // try (Jedis outer = outerPool.getResource()) {
// try { // try {
// while (!outer.ping().equals("PONG")) { // while (!outer.ping().equals("PONG")) {
// log.info("wait"); // log.info("wait");
// } // }
// String dist2load = filenames.get(prefix + "-" + fn); // String dist2load = filenames.get(prefix + "-" + fn);
// //
// String key = prefix.replace("-", "/") + "/" + dist2load; // String key = prefix.replace("-", "/") + "/" + dist2load;
// //
// byte[] s = outer.hget("dump".getBytes(), key.getBytes()); // byte[] s = outer.hget("dump".getBytes(), key.getBytes());
// HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); // HierarchicalActionSet actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s);
// //
// tree = getShapeTree(actionSet); // tree = getShapeTree(actionSet);
// //
// } catch (Exception e) { // } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
// } // }
// return tree; // return tree;
// //
// } // }
public static ITree getShapeTree(HierarchicalActionSet actionSet,boolean isJava) { public static ITree getShapeTree(HierarchicalActionSet actionSet, boolean isJava)
{
ITree tree = null; ITree tree = null;
ITree parent = null; ITree parent = null;
ITree children = null; ITree children = null;
@@ -314,40 +364,49 @@ public class EDiffHelper {
return tree; return tree;
} }
public static ITree getTokenTree(HierarchicalActionSet actionSet,boolean isJava) { public static ITree getTokenTree(HierarchicalActionSet actionSet, boolean isJava)
{
ITree tree = null; ITree tree = null;
ITree parent = null; ITree parent = null;
ITree children = null; ITree children = null;
TreeContext tc = new TreeContext(); TreeContext tc = new TreeContext();
tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,isJava); tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc, isJava);
//tree.setParent(null); //tree.setParent(null);
tc.validate(); tc.validate();
return tree; return tree;
} }
public static ITree getTargets(HierarchicalActionSet actionSet,boolean isJava) { public static ITree getTargets(HierarchicalActionSet actionSet, boolean isJava)
{
ITree tree = null; ITree tree = null;
try { try
{
ITree parent = null; ITree parent = null;
ITree children =null; ITree children = null;
TreeContext tc = new TreeContext(); TreeContext tc = new TreeContext();
tree = EDiffHelper.getTargetTree(actionSet, parent, children,tc,isJava); tree = EDiffHelper.getTargetTree(actionSet, parent, children, tc, isJava);
//tree.setParent(null); //tree.setParent(null);
tc.validate(); tc.validate();
} catch (Exception e) { }
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
return tree; return tree;
} }
public static Map<String, String> getTreeString(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) { public static Map<String, String> getTreeString(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames)
try (Jedis outer = outerPool.getResource()) { {
try { try (Jedis outer = outerPool.getResource())
while (!outer.ping().equals("PONG")) { {
try
{
while (!outer.ping().equals("PONG"))
{
log.info("wait"); log.info("wait");
} }
@@ -358,47 +417,50 @@ public class EDiffHelper {
String key = split[0] + "/" + split[1] + "/" + dist2load; String key = split[0] + "/" + split[1] + "/" + dist2load;
Map<String, String> treeMap = outer.hgetAll(key); Map<String, String> treeMap = outer.hgetAll(key);
return treeMap; return treeMap;
}catch (Exception e) { }
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
return null; return null;
} }
// public static Pair<ITree,HierarchicalActionSet> getActions(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) { // public static Pair<ITree,HierarchicalActionSet> getActions(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) {
// //
// //
// ITree tree = null; // ITree tree = null;
// HierarchicalActionSet actionSet = null; // HierarchicalActionSet actionSet = null;
// //
// try (Jedis outer = outerPool.getResource()) { // try (Jedis outer = outerPool.getResource()) {
// try { // try {
// while (!outer.ping().equals("PONG")) { // while (!outer.ping().equals("PONG")) {
// log.info("wait"); // log.info("wait");
// } // }
// //
// //
// String dist2load = filenames.get(prefix + "-" + fn); // String dist2load = filenames.get(prefix + "-" + fn);
// //
// String[] split = prefix.split("-"); // String[] split = prefix.split("-");
// String key = split[0] + "/"+split[1]+"/" + dist2load; // String key = split[0] + "/"+split[1]+"/" + dist2load;
// //
// byte[] s = outer.hget("dump".getBytes(), key.getBytes()); // byte[] s = outer.hget("dump".getBytes(), key.getBytes());
// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); // actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s);
// //
// //
// tree = getActionTrees(actionSet); // tree = getActionTrees(actionSet);
// }catch (Exception e) { // }catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
// } // }
// //
// Pair<ITree, HierarchicalActionSet> pair = new Pair<>(tree,actionSet); // Pair<ITree, HierarchicalActionSet> pair = new Pair<>(tree,actionSet);
// return pair; // return pair;
// //
// } // }
public static ITree getActionTrees(HierarchicalActionSet actionSet) { public static ITree getActionTrees(HierarchicalActionSet actionSet)
{
ITree tree = null; ITree tree = null;
ITree parent = null; ITree parent = null;
ITree children = null; ITree children = null;
@@ -409,95 +471,114 @@ public class EDiffHelper {
return tree; return tree;
} }
public static void getLeaves(ITree tc){ public static void getLeaves(ITree tc)
{
int height = tc.getHeight(); int height = tc.getHeight();
if(height == 0){ if (height == 0)
{
log.info(tc.getLabel()); log.info(tc.getLabel());
}else{ }
else
{
List<ITree> children = tc.getChildren(); List<ITree> children = tc.getChildren();
for (ITree child:children){ for (ITree child : children)
{
getLeaves(child); getLeaves(child);
} }
} }
} }
// public static ITree getTokens(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) { // public static ITree getTokens(String prefix, String fn, JedisPool outerPool, HashMap<String, String> filenames) {
// //
// //
// ITree tree = null; // ITree tree = null;
// //
// HierarchicalActionSet actionSet = null; // HierarchicalActionSet actionSet = null;
// try (Jedis outer = outerPool.getResource()) { // try (Jedis outer = outerPool.getResource()) {
// try { // try {
// while (!outer.ping().equals("PONG")) { // while (!outer.ping().equals("PONG")) {
// log.info("wait"); // log.info("wait");
// } // }
// String dist2load = filenames.get(prefix + "-" + fn); // String dist2load = filenames.get(prefix + "-" + fn);
// //
// String[] split = prefix.split("-"); // String[] split = prefix.split("-");
// String key = split[0] + "/"+split[1]+"/" + dist2load; // String key = split[0] + "/"+split[1]+"/" + dist2load;
// //
// String s = outer.hget("dump", key); // String s = outer.hget("dump", key);
// actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s); // actionSet = (HierarchicalActionSet) EDiffHelper.fromString(s);
// //
//// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s); //// actionSet = (HierarchicalActionSet) EDiffHelper.kryoDeseerialize(s);
// //
// ITree parent = null; // ITree parent = null;
// ITree children = null; // ITree children = null;
// TreeContext tc = new TreeContext(); // TreeContext tc = new TreeContext();
// tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,is); // tree = EDiffHelper.getTokenTree(actionSet, parent, children, tc,is);
// tree.setParent(null); // tree.setParent(null);
// tc.validate(); // tc.validate();
//// getLeaves(tree); //// getLeaves(tree);
// //
// //
// } catch (Exception e) { // } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
// } // }
// //
// return tree; // 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; int newType = 0;
Action action = actionSet.getAction(); Action action = actionSet.getAction();
if (action instanceof Update){ if (action instanceof Update)
{
newType = 101; newType = 101;
}else if(action instanceof Insert){ }
newType =100; else if (action instanceof Insert)
}else if(action instanceof Move){ {
newType = 100;
}
else if (action instanceof Move)
{
newType = 102; newType = 102;
}else if(action instanceof Delete){ }
newType=103; else if (action instanceof Delete)
}else{ {
newType = 103;
}
else
{
new Exception("unknow action"); new Exception("unknow action");
} }
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
//root //root
parent = tc.createTree(newType, "", null); parent = tc.createTree(newType, "", null);
tc.setRoot(parent); tc.setRoot(parent);
}else{ }
else
{
children = tc.createTree(newType, "", null); children = tc.createTree(newType, "", null);
children.setParentAndUpdateChildren(parent); children.setParentAndUpdateChildren(parent);
} }
List<HierarchicalActionSet> subActions = actionSet.getSubActions(); List<HierarchicalActionSet> subActions = actionSet.getSubActions();
if (subActions.size() != 0){ if (subActions.size() != 0)
for (HierarchicalActionSet subAction : subActions) { {
for (HierarchicalActionSet subAction : subActions)
{
if(actionSet.getParent() == null){ if (actionSet.getParent() == null)
{
children = parent; children = parent;
} }
getActionTree(subAction,children,null,tc); getActionTree(subAction, children, null, tc);
} }
@@ -507,40 +588,50 @@ public class EDiffHelper {
} }
public static List<ITree> retainLeaves(List<ITree> trees) { public static List<ITree> retainLeaves(List<ITree> trees)
{
Iterator<ITree> tIt = trees.iterator(); Iterator<ITree> tIt = trees.iterator();
while (tIt.hasNext()) { while (tIt.hasNext())
{
ITree t = tIt.next(); ITree t = tIt.next();
if (!t.isLeaf()) tIt.remove(); if (!t.isLeaf())
{
tIt.remove();
}
} }
return trees; return trees;
} }
public static String getTokenAtNode(ITree leaf,String token){ public static String getTokenAtNode(ITree leaf, String token)
// if (leaf.getParent() == null) { {
// if (leaf.getParent() == null) {
String label = leaf.getLabel(); String label = leaf.getLabel();
String key = label.split(String.valueOf(leaf.getType()))[0]; String key = label.split(String.valueOf(leaf.getType()))[0];
String group=""; String group = "";
Pattern pattern; Pattern pattern;
java.util.regex.Matcher matcher; java.util.regex.Matcher matcher;
List<ITree> parents; List<ITree> parents;
List<String> actionList = new ArrayList<>(); List<String> actionList = new ArrayList<>();
Set<String> uniqueGas = new HashSet<String>(actionList); Set<String> uniqueGas = new HashSet<String>(actionList);
switch (key.trim()) { switch (key.trim())
{
case "DEL": case "DEL":
parents = leaf.getParents(); parents = leaf.getParents();
for (ITree parent : parents) { for (ITree parent : parents)
{
String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; String s = parent.getLabel().split(String.valueOf(parent.getType()))[0];
actionList.add(s); actionList.add(s);
} }
actionList.add(key); actionList.add(key);
if (uniqueGas.size() == 1){ if (uniqueGas.size() == 1)
{
Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst(); Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst();
if(first.isPresent()){ if (first.isPresent())
{
ITree parent = first.get(); ITree parent = first.get();
label = parent.getLabel(); label = parent.getLabel();
} }
@@ -548,7 +639,8 @@ public class EDiffHelper {
pattern = Pattern.compile(delPattern); pattern = Pattern.compile(delPattern);
matcher = pattern.matcher(label); matcher = pattern.matcher(label);
if (matcher.matches()) { if (matcher.matches())
{
group = matcher.group(2); group = matcher.group(2);
} }
@@ -556,24 +648,28 @@ public class EDiffHelper {
case "INS": case "INS":
parents = leaf.getParents(); parents = leaf.getParents();
for (ITree parent : parents) { for (ITree parent : parents)
{
String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; String s = parent.getLabel().split(String.valueOf(parent.getType()))[0];
actionList.add(s); actionList.add(s);
} }
actionList.add(key); actionList.add(key);
if (uniqueGas.size() == 1){ if (uniqueGas.size() == 1)
{
Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst(); Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst();
if(first.isPresent()){ if (first.isPresent())
{
ITree parent = first.get(); ITree parent = first.get();
label = parent.getLabel(); label = parent.getLabel();
} }
} }
// //
pattern = Pattern.compile(insPattern); pattern = Pattern.compile(insPattern);
matcher = pattern.matcher(label); matcher = pattern.matcher(label);
if (matcher.matches()) { if (matcher.matches())
{
group = matcher.group(2);// +" " + matcher.group(3); group = matcher.group(2);// +" " + matcher.group(3);
} }
@@ -581,15 +677,18 @@ public class EDiffHelper {
case "MOV": case "MOV":
parents = leaf.getParents(); parents = leaf.getParents();
for (ITree parent : parents) { for (ITree parent : parents)
{
String s = parent.getLabel().split(String.valueOf(parent.getType()))[0]; String s = parent.getLabel().split(String.valueOf(parent.getType()))[0];
actionList.add(s); actionList.add(s);
} }
actionList.add(key); actionList.add(key);
if (uniqueGas.size() == 1){ if (uniqueGas.size() == 1)
{
Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst(); Optional<ITree> first = parents.stream().filter(p -> p.isRoot()).findFirst();
if(first.isPresent()){ if (first.isPresent())
{
ITree parent = first.get(); ITree parent = first.get();
label = parent.getLabel(); label = parent.getLabel();
} }
@@ -597,7 +696,8 @@ public class EDiffHelper {
pattern = Pattern.compile(movPattern); pattern = Pattern.compile(movPattern);
matcher = pattern.matcher(label); matcher = pattern.matcher(label);
if (matcher.matches()) { if (matcher.matches())
{
group = matcher.group(2); group = matcher.group(2);
String group1 = matcher.group(3); String group1 = matcher.group(3);
@@ -606,8 +706,9 @@ public class EDiffHelper {
case "UPD": case "UPD":
pattern = Pattern.compile(updPattern); pattern = Pattern.compile(updPattern);
matcher = pattern.matcher(label); matcher = pattern.matcher(label);
if (matcher.matches()) { if (matcher.matches())
group = matcher.group(2) +" " + matcher.group(3); {
group = matcher.group(2) + " " + matcher.group(3);
} }
break; break;
@@ -615,40 +716,47 @@ public class EDiffHelper {
} }
token = group; token = group;
return group; return group;
// }else{ // }else{
// getTokenAtNode(leaf.getParent(),token); // getTokenAtNode(leaf.getParent(),token);
// return token; // return token;
// } // }
} }
static String movPattern = "MOV (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*"; static String movPattern = "MOV (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*";
static String delPattern = "DEL (.*)@@(.*)@AT@.*"; static String delPattern = "DEL (.*)@@(.*)@AT@.*";
static String insPattern = "INS (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*"; static String insPattern = "INS (.*)@@(.*)@TO@ (.*)@@(.*)@AT@.*";
static String updPattern = "UPD (.*)@@(.*)@TO@(.*)@AT@.*"; static String updPattern = "UPD (.*)@@(.*)@TO@(.*)@AT@.*";
public static String getNames2(ITree oldTree){ public static String getNames2(ITree oldTree)
{
List<ITree> leaves = retainLeaves(TreeUtils.postOrder(oldTree)); List<ITree> leaves = retainLeaves(TreeUtils.postOrder(oldTree));
List<String> res = new ArrayList<>(); List<String> res = new ArrayList<>();
String resu=""; String resu = "";
for (ITree leaf : leaves) { for (ITree leaf : leaves)
{
String tokenAtNode = getTokenAtNode(leaf, resu); String tokenAtNode = getTokenAtNode(leaf, resu);
res.add(tokenAtNode); 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]+):.*"; final String regex = "MethodName:([a-zA-Z0-9]+):.*MethodName:([a-zA-Z0-9]+):.*";
Pattern compile = Pattern.compile(regex); Pattern compile = Pattern.compile(regex);
Matcher matcher = compile.matcher(res.get(0)); Matcher matcher = compile.matcher(res.get(0));
if(matcher.matches()){ if (matcher.matches())
res.set(0,matcher.group(1) +" " + matcher.group(2)); {
res.set(0, matcher.group(1) + " " + matcher.group(2));
} }
} }
return String.join(" ",res); return String.join(" ", res);
} }
} }
@@ -1,104 +1,138 @@
package edu.lu.uni.serval.utils; package edu.lu.uni.serval.utils;
import java.io.BufferedInputStream;
import java.io.BufferedWriter; import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class FileHelper { public class FileHelper
{
/** /**
* @param filePath * @param filePath
*/ */
public static void createDirectory(String filePath) { public static void createDirectory(String filePath)
{
File file = new File(filePath); File file = new File(filePath);
if (file.exists()) { if (file.exists())
{
deleteDirectory(filePath); deleteDirectory(filePath);
} }
file.mkdirs(); file.mkdirs();
} }
public static void createFile(File file, String content) { public static void createFile(File file, String content)
{
FileWriter writer = null; FileWriter writer = null;
BufferedWriter bw = null; BufferedWriter bw = null;
try { try
if (!file.getParentFile().exists()) { {
if (!file.getParentFile().exists())
{
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
if (!file.exists()) file.createNewFile(); if (!file.exists())
{
file.createNewFile();
}
writer = new FileWriter(file); writer = new FileWriter(file);
bw = new BufferedWriter(writer); bw = new BufferedWriter(writer);
bw.write(content); bw.write(content);
bw.flush(); bw.flush();
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} finally { }
finally
{
close(bw); close(bw);
close(writer); close(writer);
} }
} }
public static void deleteDirectory(String dir) { public static void deleteDirectory(String dir)
{
File file = new File(dir); File file = new File(dir);
if (file.exists()) { if (file.exists())
if (file.isDirectory()) { {
if (file.isDirectory())
{
File[] files = file.listFiles(); File[] files = file.listFiles();
if (files.length > 0) { if (files.length > 0)
for (File f : files) { {
if (f.isFile()) { for (File f : files)
{
if (f.isFile())
{
deleteFile(f.getAbsolutePath()); deleteFile(f.getAbsolutePath());
} else { }
else
{
deleteDirectory(f.getAbsolutePath()); deleteDirectory(f.getAbsolutePath());
} }
} }
} }
file.delete(); file.delete();
} else { }
else
{
deleteFile(dir); deleteFile(dir);
} }
} }
} }
public static void deleteFiles(String dir) { public static void deleteFiles(String dir)
{
File file = new File(dir); File file = new File(dir);
if (file.exists()) { if (file.exists())
if (file.isDirectory()) { {
if (file.isDirectory())
{
File[] files = file.listFiles(); File[] files = file.listFiles();
if (files.length > 0) { if (files.length > 0)
for (File f : files) { {
if (f.isFile()) { for (File f : files)
{
if (f.isFile())
{
deleteFile(f.getAbsolutePath()); deleteFile(f.getAbsolutePath());
} else { }
else
{
deleteFiles(f.getAbsolutePath()); deleteFiles(f.getAbsolutePath());
} }
} }
} }
} else { }
else
{
deleteFile(dir); deleteFile(dir);
} }
} }
} }
public static void deleteFile(String fileName) { public static void deleteFile(String fileName)
{
File file = new File(fileName); File file = new File(fileName);
if (file.exists()) { if (file.exists())
if (file.isFile()) { {
if (file.isFile())
{
file.delete(); file.delete();
} else { }
else
{
deleteDirectory(fileName); deleteDirectory(fileName);
} }
} }
} }
public static List<File> getAllDirectories(String filePath) { public static List<File> getAllDirectories(String filePath)
{
return listAllDirectories(new File(filePath)); return listAllDirectories(new File(filePath));
} }
@@ -109,26 +143,33 @@ public class FileHelper {
* @param type * @param type
* @return * @return
*/ */
public static List<File> getAllFiles(String filePath, String type) { public static List<File> getAllFiles(String filePath, String type)
{
return listAllFiles(new File(filePath), type); return listAllFiles(new File(filePath), type);
} }
public static List<File> getAllFilesInCurrentDiectory(String filePath, String type) { public static List<File> getAllFilesInCurrentDiectory(String filePath, String type)
{
return getAllFilesInCurrentDiectory(new File(filePath), type); return getAllFilesInCurrentDiectory(new File(filePath), type);
} }
public static List<File> getAllFilesInCurrentDiectory(File directory, String type) { public static List<File> getAllFilesInCurrentDiectory(File directory, String type)
{
List<File> fileList = new ArrayList<>(); List<File> fileList = new ArrayList<>();
if (!directory.exists()) { if (!directory.exists())
{
return null; return null;
} }
File[] files = directory.listFiles(); File[] files = directory.listFiles();
for (File file : files) { for (File file : files)
if (file.isFile()) { {
if (file.toString().endsWith(type)) { if (file.isFile())
{
if (file.toString().endsWith(type))
{
fileList.add(file); fileList.add(file);
} }
} }
@@ -137,36 +178,47 @@ public class FileHelper {
return fileList; return fileList;
} }
public static String getFileName(String filePath) { public static String getFileName(String filePath)
{
File file = new File(filePath); File file = new File(filePath);
if (file.exists()) { if (file.exists())
{
return file.getName(); return file.getName();
} else { }
else
{
return null; return null;
} }
} }
public static String getFileNameWithoutExtension(File file) { public static String getFileNameWithoutExtension(File file)
if (file.exists()) { {
if (file.exists())
{
String fileName = file.getName(); String fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf(".")); fileName = fileName.substring(0, fileName.lastIndexOf("."));
return fileName; return fileName;
} else { }
else
{
return null; return null;
} }
} }
public static String getFileExtension(File file) { public static String getFileExtension(File file)
{
String fileName = file.getName(); String fileName = file.getName();
String extension = fileName.substring(fileName.lastIndexOf(".")); String extension = fileName.substring(fileName.lastIndexOf("."));
return extension; return extension;
} }
public static String getFileParentPath(String filePath) { public static String getFileParentPath(String filePath)
{
File file = new File(filePath); File file = new File(filePath);
if (file.exists()) { if (file.exists())
{
return file.getParent() + "/"; return file.getParent() + "/";
} }
return ""; return "";
@@ -176,17 +228,13 @@ public class FileHelper {
* Check whether a file path is valid or not. * Check whether a file path is valid or not.
* *
* @param path, file path. * @param path, file path.
* @return true, the file path is valid. * @return true, the file path is valid. false, the file path is invalid.
* false, the file path is invalid.
*/ */
public static boolean isValidPath(String path) { public static boolean isValidPath(String path)
{
File file = new File(path); File file = new File(path);
if (file.exists()) { return file.exists();
return true;
}
return false;
} }
/** /**
@@ -195,23 +243,31 @@ public class FileHelper {
* @param file * @param file
* @return * @return
*/ */
private static List<File> listAllFiles(File file, String type) { private static List<File> listAllFiles(File file, String type)
{
List<File> fileList = new ArrayList<>(); List<File> fileList = new ArrayList<>();
if (!file.exists()) { if (!file.exists())
{
return null; return null;
} }
File[] files = file.listFiles(); File[] files = file.listFiles();
for (File f : files) { for (File f : files)
if (f.isFile()) { {
if (f.toString().endsWith(type)) { if (f.isFile())
{
if (f.toString().endsWith(type))
{
fileList.add(f); fileList.add(f);
} }
} else { }
else
{
List<File> fl = listAllFiles(f, type); List<File> fl = listAllFiles(f, type);
if (fl != null && fl.size() > 0) { if (fl != null && fl.size() > 0)
{
fileList.addAll(fl); fileList.addAll(fl);
} }
} }
@@ -226,13 +282,16 @@ public class FileHelper {
* @param file * @param file
* @return * @return
*/ */
private static List<File> listAllDirectories(File file) { private static List<File> listAllDirectories(File file)
{
List<File> fileList = new ArrayList<>(); List<File> fileList = new ArrayList<>();
File[] files = file.listFiles(); File[] files = file.listFiles();
for (File f : files) { for (File f : files)
if (f.isDirectory()) { {
if (f.isDirectory())
{
fileList.add(f); fileList.add(f);
fileList.addAll(listAllDirectories(f)); fileList.addAll(listAllDirectories(f));
} }
@@ -241,10 +300,12 @@ public class FileHelper {
return fileList; return fileList;
} }
public static void makeDirectory(String fileName) { public static void makeDirectory(String fileName)
{
deleteFile(fileName); deleteFile(fileName);
File file = new File(fileName).getParentFile(); File file = new File(fileName).getParentFile();
if (!file.exists()) { if (!file.exists())
{
file.mkdirs(); file.mkdirs();
} }
} }
@@ -255,7 +316,8 @@ public class FileHelper {
* @param fileName * @param fileName
* @return String, the content of a file. * @return String, the content of a file.
*/ */
public static String readFile(String fileName) { public static String readFile(String fileName)
{
return readFile(new File(fileName)); return readFile(new File(fileName));
} }
@@ -265,26 +327,35 @@ public class FileHelper {
* @param file * @param file
* @return String, the content of a file. * @return String, the content of a file.
*/ */
public static String readFile(File file) { public static String readFile(File file)
{
byte[] input = null; byte[] input = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
try { try
{
bis = new BufferedInputStream(new FileInputStream(file)); bis = new BufferedInputStream(new FileInputStream(file));
input = new byte[bis.available()]; input = new byte[bis.available()];
bis.read(input); bis.read(input);
} catch (FileNotFoundException e) { }
catch (FileNotFoundException e)
{
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} finally { }
finally
{
close(bis); close(bis);
} }
String sourceCode = null; String sourceCode = null;
if (input != null) { if (input != null)
{
sourceCode = new String(input); sourceCode = new String(input);
} }
@@ -298,82 +369,112 @@ public class FileHelper {
* @param data, output data. * @param data, output data.
* @param append, the output data will be appended previous data in the file or not. * @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); 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); 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); File file = new File(fileName);
outputToFile(file, data, append); 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; FileWriter writer = null;
BufferedWriter bw = null; BufferedWriter bw = null;
try { try
if (!file.getParentFile().exists()) { {
if (!file.getParentFile().exists())
{
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
if (!file.exists()) { if (!file.exists())
{
file.createNewFile(); file.createNewFile();
} }
writer = new FileWriter(file, append); writer = new FileWriter(file, append);
bw = new BufferedWriter(writer); bw = new BufferedWriter(writer);
bw.write(data); bw.write(data);
bw.flush(); bw.flush();
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} finally { }
finally
{
close(bw); close(bw);
close(writer); close(writer);
} }
} }
private static void close(FileWriter writer) { private static void close(FileWriter writer)
try { {
if (writer != null) { try
{
if (writer != null)
{
writer.close(); writer.close();
writer = null; writer = null;
} }
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void close(BufferedWriter bw) { private static void close(BufferedWriter bw)
try { {
if (bw != null) { try
{
if (bw != null)
{
bw.close(); bw.close();
bw = null; bw = null;
} }
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
private static void close(BufferedInputStream bis) { private static void close(BufferedInputStream bis)
try { {
if (bis != null) { try
{
if (bis != null)
{
bis.close(); bis.close();
bis = null; bis = null;
} }
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
public static List<File> getAllSubDirectories(String fileName) { public static List<File> getAllSubDirectories(String fileName)
{
File file = new File(fileName); File file = new File(fileName);
List<File> subDirectories = new ArrayList<>(); List<File> subDirectories = new ArrayList<>();
if (file.exists()) { if (file.exists())
{
File[] files = file.listFiles(); File[] files = file.listFiles();
for (File f : files) { for (File f : files)
if (f.isDirectory()) { {
if (f.isDirectory())
{
subDirectories.add(f); subDirectories.add(f);
} }
} }
@@ -5,39 +5,51 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
public class ListSorter<T extends Comparable<? super T>> { public class ListSorter<T extends Comparable<? super T>>
{
private List<T> list; private final List<T> list;
public ListSorter(List<T> list) { public ListSorter(List<T> list)
{
this.list = new ArrayList<>(); this.list = new ArrayList<>();
this.list.addAll(list); this.list.addAll(list);
} }
public List<T> getList() { public List<T> getList()
{
return this.list; return this.list;
} }
public List<T> sortAscending() { public List<T> sortAscending()
try { {
if (list != null && list.size() > 0) { try
Collections.sort(this.list, new Comparator<T>() { {
if (list != null && list.size() > 0)
{
Collections.sort(this.list, new Comparator<T>()
{
@Override @Override
public int compare(T t1, T t2) { public int compare(T t1, T t2)
{
return t1.compareTo(t2); return t1.compareTo(t2);
} }
}); });
} }
} catch (Exception e) { }
catch (Exception e)
{
return null; return null;
} }
return this.list; return this.list;
} }
public List<T> sortDescending() { public List<T> sortDescending()
if (list != null && list.size() > 0) { {
if (list != null && list.size() > 0)
{
Collections.sort(this.list, Collections.reverseOrder()); Collections.sort(this.list, Collections.reverseOrder());
} }
return this.list; return this.list;
@@ -7,25 +7,25 @@ import java.time.Duration;
/** /**
* Created by anilkoyuncu on 17/09/2018. * Created by anilkoyuncu on 17/09/2018.
*/ */
public class PoolBuilder { public class PoolBuilder
public static JedisPoolConfig getPoolConfig() { {
public static JedisPoolConfig getPoolConfig()
{
return poolConfig; return poolConfig;
} }
static final JedisPoolConfig poolConfig = buildPoolConfig(); static final JedisPoolConfig poolConfig = buildPoolConfig();
private static JedisPoolConfig buildPoolConfig()
{
private static JedisPoolConfig buildPoolConfig() {
final JedisPoolConfig poolConfig = new JedisPoolConfig(); final JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(1024); poolConfig.setMaxTotal(1024);
poolConfig.setMaxIdle(1024); poolConfig.setMaxIdle(1024);
// poolConfig.setMinIdle(16); // poolConfig.setMinIdle(16);
// poolConfig.setTestOnBorrow(true); // poolConfig.setTestOnBorrow(true);
// poolConfig.setTestOnReturn(true); // poolConfig.setTestOnReturn(true);
// poolConfig.setTestWhileIdle(true); // poolConfig.setTestWhileIdle(true);
poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis()); poolConfig.setMinEvictableIdleTimeMillis(Duration.ofMinutes(60).toMillis());
poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis()); poolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofHours(30).toMillis());
poolConfig.setNumTestsPerEvictionRun(3); poolConfig.setNumTestsPerEvictionRun(3);