FlowTracker/src/main/java/edu/nju/ics/frontier/parse/ParseMain.java

224 lines
9.7 KiB
Java
Raw Normal View History

2020-10-26 15:41:48 +08:00
package edu.nju.ics.frontier.parse;
import edu.nju.ics.frontier.bean.intellij.FileEvent;
import edu.nju.ics.frontier.bean.intellij.IntelliJEvent;
import edu.nju.ics.frontier.bean.intellij.ProjectEvent;
import edu.nju.ics.frontier.util.Assertion;
import edu.nju.ics.frontier.util.CollectionUtil;
import edu.nju.ics.frontier.util.Constants;
import edu.nju.ics.frontier.util.Executor;
import edu.nju.ics.frontier.util.FileUtil;
import java.io.File;
import java.util.*;
public class ParseMain {
public static void main(String[] args) {
cleanIntelliJEvent();
parseIntelliJEvent();
parseLabel();
}
public static void cleanIntelliJEvent() {
String root = "C:\\Users\\zzw\\Desktop\\Research\\dataset\\flow_tracker_plugin\\fujitsu";
Executor.executeToDateDir(root, root, new IntelliJEventCleanTask());
}
public static void parseIntelliJEvent() {
String srcRoot = "C:\\Users\\zzw\\Desktop\\Research\\dataset\\flow_tracker_plugin\\fujitsu";
String destRoot = FileUtil.combine(Constants.FUJITSU_ROOT, "parse");
Executor.executeToDateDir(srcRoot, destRoot, new IntelliJEventParseTask());
}
public static void parseLabel() {
String srcRoot = "C:\\Users\\zzw\\Desktop\\Research\\dataset\\flow_tracker_plugin\\fujitsu";
String destRoot = FileUtil.combine(Constants.FUJITSU_ROOT, "parse");
Executor.executeToDateDir(srcRoot, destRoot, new LabelParseTask());
}
public static void enumerateProjects() {
String srcRoot = FileUtil.combine(Constants.FUJITSU_ROOT, "parse");
Executor.executeToUserDir(srcRoot, "", new Executor.Task() {
public void run(String srcRoot, String destRoot, File teamDir, File userDir, String dateStr) {
File[] dateDirs = FileUtil.listSubDirectories(userDir);
if (dateDirs == null) {
return;
}
Set<String> projects = new HashSet<String>();
for (File dateDir : dateDirs) {
List<IntelliJEvent> events = ParseUtil.loadWorkdayEvents(dateDir);
if (events == null) {
continue;
}
for (IntelliJEvent event : events) {
if (!IntelliJEvent.isProjectEvent(event)) {
continue;
}
ProjectEvent projectEvent = IntelliJEvent.toProjectEvent(event);
CollectionUtil.updateSet(projects, projectEvent.getNameField());
}
}
System.out.printf("%s/%s/%d\n%s\n", teamDir.getName(), userDir.getName(), projects.size(), projects);
}
});
}
public static void enumerateFiles() {
String srcRoot = FileUtil.combine(Constants.FUJITSU_ROOT, "parse");
Executor.executeToUserDir(srcRoot, "", new Executor.Task() {
public void run(String srcRoot, String destRoot, File teamDir, File userDir, String dateStr) {
File[] dateDirs = FileUtil.listSubDirectories(userDir);
if (dateDirs == null) {
return;
}
Set<String> globalFiles = new HashSet<String>();
Set<String> switchFiles = new HashSet<String>();
Set<String> selectFiles = new HashSet<String>();
List<Set<String>> fileSwitches = new ArrayList<Set<String>>();
for (File dateDir : dateDirs) {
List<IntelliJEvent> events = ParseUtil.loadWorkdayEvents(dateDir);
if (events == null) {
continue;
}
for (IntelliJEvent event : events) {
if (!IntelliJEvent.isFileEvent(event)) {
continue;
}
FileEvent fileEvent = IntelliJEvent.toFileEvent(event);
if (fileEvent.isWhenSelected()) {
String oldPath = fileEvent.getOldPathField();
String newPath = fileEvent.getNewPathField();
CollectionUtil.updateSet(globalFiles, oldPath);
CollectionUtil.updateSet(globalFiles, newPath);
CollectionUtil.updateSet(selectFiles, oldPath);
CollectionUtil.updateSet(selectFiles, newPath);
if (oldPath != null && newPath != null && (!oldPath.equals(newPath))) {
CollectionUtil.updateSet(switchFiles, oldPath);
CollectionUtil.updateSet(switchFiles, newPath);
Set<String> tmp = new HashSet<String>();
tmp.add(oldPath);
tmp.add(newPath);
fileSwitches.add(tmp);
}
} else if (fileEvent.isWhenChanged()) {
String path = fileEvent.getPathField();
CollectionUtil.updateSet(globalFiles, path);
}
}
}
System.out.printf("%s/%s\n", teamDir.getName(), userDir.getName());
// connected components
List<Set<String>> fileConnectedComponents = getConnectedComponents(fileSwitches);
System.out.println("connected components:");
Set<String> usedFiles = new HashSet<String>();
for (Set<String> fileConnectedComponent : fileConnectedComponents) {
for (String item : fileConnectedComponent) {
Assertion.assertTrue(usedFiles.add(item));
}
System.out.println(fileConnectedComponent);
}
// files selected but not switched
System.out.println("files selected but not switched:");
Set<String> selectButNotSwitch = new HashSet<String>();
for (String file : selectFiles) {
if (!usedFiles.contains(file)) {
selectButNotSwitch.add(file);
}
}
System.out.println(selectButNotSwitch);
// files changed but not selected
System.out.println("files changed but not selected:");
Set<String> changedButNotSelect = new HashSet<String>();
for (String file : globalFiles) {
if (!selectFiles.contains(file)) {
changedButNotSelect.add(file);
}
}
System.out.println(changedButNotSelect);
}
});
}
private static List<Set<String>> getConnectedComponents(List<Set<String>> fileSwitches) {
Map<String, Set<Integer>> clusterIdsForEachFile = getClusterIdsForEachFile(fileSwitches);
int[] conflictClusterIds = getConflictClusterIds(clusterIdsForEachFile);
while (conflictClusterIds != null) {
resolveClusterConflict(clusterIdsForEachFile, conflictClusterIds[1], conflictClusterIds[0]);
conflictClusterIds = getConflictClusterIds(clusterIdsForEachFile);
}
Map<Integer, Set<String>> fileClusters = new HashMap<Integer, Set<String>>();
for (Map.Entry<String, Set<Integer>> entry : clusterIdsForEachFile.entrySet()) {
String key = entry.getKey();
Object[] value = entry.getValue().toArray();
Assertion.assertTrue(value.length == 1);
CollectionUtil.updateSetMap(fileClusters, (Integer) value[0], key);
}
List<Set<String>> result = new ArrayList<Set<String>>();
for (Map.Entry<Integer, Set<String>> entry : fileClusters.entrySet()) {
result.add(entry.getValue());
}
return result.isEmpty() ? null : result;
}
private static Map<String, Set<Integer>> getClusterIdsForEachFile(List<Set<String>> fileSwitches) {
if (fileSwitches == null || fileSwitches.isEmpty()) {
return null;
}
Map<String, Set<Integer>> result = new HashMap<String, Set<Integer>>();
for (int i = 0, l = fileSwitches.size(); i < l; i++) {
Set<String> tmp = fileSwitches.get(i);
for (String item : tmp) {
CollectionUtil.updateSetMap(result, item, i);
}
}
return result.isEmpty() ? null : result;
}
private static int[] getConflictClusterIds(Map<String, Set<Integer>> clusterIdsForEachFile) {
if (clusterIdsForEachFile == null) {
return null;
}
for (Map.Entry<String, Set<Integer>> entry : clusterIdsForEachFile.entrySet()) {
Set<Integer> value = entry.getValue();
if (value.size() > 1) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int item : value) {
if (item < min) {
min = item;
}
if (item > max) {
max = item;
}
}
return new int[]{min, max};
}
}
return null;
}
private static void resolveClusterConflict(Map<String, Set<Integer>> clusterIdsForEachFile, int from, int to) {
if (clusterIdsForEachFile == null) {
return;
}
for (Map.Entry<String, Set<Integer>> entry : clusterIdsForEachFile.entrySet()) {
Set<Integer> value = entry.getValue();
if (value.contains(from)) {
value.remove(from);
value.add(to);
}
}
}
}