Skip to content

Commit

Permalink
Add pruned TF algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
gavelino committed Apr 11, 2018
1 parent b39d5d7 commit e73dbf4
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 4 deletions.
3 changes: 2 additions & 1 deletion gittruckfactor/config.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
normalizedDOA=0.75
absoluteDOA=3.293
tfCoverage=0.5
tfCoverage=0.5
minPercentage=0.1
6 changes: 4 additions & 2 deletions gittruckfactor/src/aserg/gtf/GitTruckFactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import aserg.gtf.task.extractor.GitLogExtractor;
import aserg.gtf.task.extractor.LinguistExtractor;
import aserg.gtf.truckfactor.GreedyTruckFactor;
import aserg.gtf.truckfactor.PrunedGreedyTruckFactor;
import aserg.gtf.truckfactor.TFInfo;
import aserg.gtf.truckfactor.TruckFactor;
import aserg.gtf.util.ConfigInfo;
Expand Down Expand Up @@ -135,7 +136,7 @@ private static TFInfo getTFInfo(String repositoryPath,
//Persist authors info
//doaCalculator.persist(repository);

TruckFactor truckFactor = new GreedyTruckFactor();
TruckFactor truckFactor = new PrunedGreedyTruckFactor(config.getMinPercentage());
return truckFactor.getTruckFactor(repository);

}
Expand All @@ -147,7 +148,8 @@ public static void loadConfiguration() {
float normalizedDOA = Float.parseFloat((String) properties.get("normalizedDOA"));
float absoluteDOA = Float.parseFloat((String) properties.get("absoluteDOA"));
float tfCoverage = Float.parseFloat((String) properties.get("tfCoverage"));
config = new ConfigInfo(normalizedDOA, absoluteDOA, tfCoverage);
float minPercentage = Float.parseFloat((String) properties.get("minPercentage"));
config = new ConfigInfo(normalizedDOA, absoluteDOA, tfCoverage, minPercentage);
} catch (IOException e1) {
LOGGER.error("Load configuration info aborted!",e1);
}
Expand Down
7 changes: 7 additions & 0 deletions gittruckfactor/src/aserg/gtf/model/authorship/Developer.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,12 @@ public List<File> getAuthorshipFiles() {
}
return authorshipFiles;
}
public int getDevChanges(){
int count = 0;
for (AuthorshipInfo authorshipInfo : authorshipInfos) {
count+=(authorshipInfo.isFirstAuthor()||authorshipInfo.isSecondaryAuthor()?1:0)+authorshipInfo.getnDeliveries();
}
return count;
}

}
167 changes: 167 additions & 0 deletions gittruckfactor/src/aserg/gtf/truckfactor/PrunedGreedyTruckFactor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package aserg.gtf.truckfactor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import aserg.gtf.GitTruckFactor;
import aserg.gtf.model.authorship.AuthorshipInfo;
import aserg.gtf.model.authorship.Developer;
import aserg.gtf.model.authorship.File;
import aserg.gtf.model.authorship.Repository;

public class PrunedGreedyTruckFactor extends TruckFactor {
private static final Logger LOGGER = Logger.getLogger(TruckFactor.class);

private TFInfo tfInfo = new TFInfo();
private float minPercentage;

public PrunedGreedyTruckFactor(float minPercentage) {
this.minPercentage = minPercentage;
}

@Override
public TFInfo getTruckFactor(Repository repository) {
Map<Developer, Set<File>> authorsMap = getFilesAuthorMap(repository);
//GREDDY TRUCK FACTOR ALGORITHM
int repFilesSize = repository.getFiles().size();
int factor = 0;
float coverage = 1;
int nFilesTop1Dev = getNumFilesTopDev(authorsMap);
while(authorsMap.size()>0){
coverage = getCoverage(repFilesSize, authorsMap);
if (coverage<0.5)
break;
removeTopAuthor(repFilesSize, authorsMap);
factor++;
}
tfInfo.setCoverage(getCoverage(repFilesSize, authorsMap));
tfInfo.setTf(factor);
tfInfo.setTotalFiles(repFilesSize);

return pruneTF(tfInfo);
}

private TFInfo pruneTF(TFInfo tfInfo) {
Developer topDev = getTopOneDev(tfInfo.getTfDevelopers());
List<Developer> prunedDevs = new ArrayList<Developer>();
for (Developer developer : tfInfo.getTfDevelopers()) {
if ((float)developer.getAuthorshipFiles().size()/topDev.getAuthorshipFiles().size()<minPercentage)
prunedDevs.add(developer);
}
for (Developer developer : prunedDevs) {
tfInfo.getTfDevelopers().remove(developer);
}
tfInfo.setTf(tfInfo.getTf()-prunedDevs.size());
return tfInfo;
}

private Developer getTopOneDev(List<Developer> tfDevelopers) {
Developer topDev = null;
for (Developer developer : tfDevelopers) {
if (topDev ==null)
topDev = developer;
else if (developer.getAuthorshipFiles().size()>topDev.getAuthorshipFiles().size())
topDev = developer;
}
return topDev;
}

private int getNumFilesTopDev(Map<Developer, Set<File>> authorsMap) {
int maxFiles = 0;
for (Set<File> files : authorsMap.values()) {
if (files.size()>maxFiles)
maxFiles = files.size();
}
return maxFiles;
}

private Map<Developer, Set<File>> getFilesAuthorMap(Repository repository){
Map<Developer, Set<File>> map = new HashMap<Developer, Set<File>>();
List<Developer> developers = repository.getDevelopers();
for (Developer developer : developers) {
Set<File> devFiles = new HashSet<File>();
List<AuthorshipInfo> authorships = developer.getAuthorshipInfos();
for (AuthorshipInfo authorshipInfo : authorships) {
if (authorshipInfo.isDOAAuthor())
devFiles.add(authorshipInfo.getFile());

}
if (devFiles.size()>0)
map.put(developer, devFiles);
}
return map;
}

private float getCoverage(int repFilesSize, Map<Developer, Set<File>> authorsMap) {
Set<File> authorsSet = new HashSet<File>();
for (Entry<Developer, Set<File>> entry : authorsMap.entrySet()) {
for (File file : entry.getValue()) {
authorsSet.add(file);
if(authorsSet.size()==repFilesSize)
return 1f;
}
}
return (float)authorsSet.size()/repFilesSize;
}

private void removeTopAuthor(int repFilesSize, Map<Developer, Set<File>> authorsMap) {
int biggerNumber = 0;
Developer biggerDev = null;
for (Entry<Developer, Set<File>> entry : authorsMap.entrySet()) {
if (entry.getValue().size()>biggerNumber){
biggerNumber = entry.getValue().size();
biggerDev = entry.getKey();
}
if (biggerDev!=null && entry.getValue().size()==biggerNumber)
if(entry.getKey().getDevChanges() > biggerDev.getDevChanges())
biggerDev = entry.getKey();


}
tfInfo.addDeveloper(biggerDev);
authorsMap.remove(biggerDev);
}


// //HELP METHODS: Used only for tests propose

// private void printAuthorsFile(Set<File> set) {
// for (File file : set) {
// System.out.println(file.getPath());
// }
//
// }
//
// private void printAuthorsMap(Map<Long, Set<Long>> authorsMap) {
// for (Entry<Long, Set<Long>> entry : authorsMap.entrySet()) {
// System.out.print(entry.getKey() + ": ");
// for (Long fileId : entry.getValue()) {
// System.out.print(fileId + " ");
// }
// System.out.println();
// }
//
// }
//
// private void printTruckMap(String repName, Map<Integer, Float> truckMap) {
// Date date = new Date();
// for (Entry<Integer, Float> entry : truckMap.entrySet()) {
// System.out.format("%s;%d;%f\n", repName, entry.getKey(), entry.getValue());
//
// }
// System.out.println("TF Developers: ");
// for (String tfInfo : tfAuthorInfo) {
// System.out.println(tfInfo);
// }
// }

}
9 changes: 8 additions & 1 deletion gittruckfactor/src/aserg/gtf/util/ConfigInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ public class ConfigInfo {
private float normalizedDOA;
private float absoluteDOA;
private float tfCoverage;
private float minPercentage;

public ConfigInfo(float normalizedDOA, float absoluteDOA, float tfCoverage) {
public ConfigInfo(float normalizedDOA, float absoluteDOA, float tfCoverage, float minPercentage) {
super();
this.normalizedDOA = normalizedDOA;
this.absoluteDOA = absoluteDOA;
this.tfCoverage = tfCoverage;
this.minPercentage = minPercentage;
}

public float getNormalizedDOA() {
Expand All @@ -21,6 +23,11 @@ public float getAbsoluteDOA() {
public float getTfCoverage() {
return tfCoverage;
}

public float getMinPercentage() {
return minPercentage;
}



}

0 comments on commit e73dbf4

Please sign in to comment.