Skip to content

Commit

Permalink
feat: add code to generate not crossed P4J features (SpoonLabs#216)
Browse files Browse the repository at this point in the history
* feat: add code to generate non crossedP4J features

* refactor:add cross key word in the config-coming.properties and rename the not cross to simpleP4J
  • Loading branch information
Sophie H Ye authored May 6, 2020
1 parent 484f8d5 commit 44c1bbd
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/main/java/fr/inria/coming/codefeatures/P4JFeatureAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import fr.inria.coming.core.entities.AnalysisResult;
import fr.inria.coming.core.entities.DiffResult;
import fr.inria.coming.core.entities.RevisionResult;
import fr.inria.coming.main.ComingProperties;
import fr.inria.prophet4j.feature.FeatureCross;
import fr.inria.prophet4j.feature.extended.ExtendedFeatureCross;
import fr.inria.prophet4j.feature.original.OriginalFeatureCross;
Expand Down Expand Up @@ -68,11 +69,18 @@ public AnalysisResult analyze(IRevision revision, RevisionResult previousResults
Option option = new Option();
option.featureOption = FeatureOption.ORIGINAL;
//We set the first parameter of CodeDiffer as False to not allow the code generation at buggy location
CodeDiffer codeDiffer = new CodeDiffer(false, option);
//By default, coming extracts simple P4J features, so the cross sets to false
Boolean cross = ComingProperties.getPropertyBoolean("cross");
CodeDiffer codeDiffer = new CodeDiffer(false, option,cross);
//Get feature matrix
List<FeatureMatrix> featureMatrix = codeDiffer.runByGenerator(src, target);
//Get feature vector
JsonObject jsonfile = genVectorsCSV(option,target,featureMatrix);
JsonObject jsonfile = null;
if(cross) {
jsonfile = genVectorsCSV(option,target,featureMatrix);
} else {
jsonfile = getSimleP4JJSON(option,target,featureMatrix);
}

JsonArray filesArray = new JsonArray();
JsonObject file = new JsonObject();
Expand All @@ -89,6 +97,22 @@ public AnalysisResult analyze(IRevision revision, RevisionResult previousResults

}

private JsonObject getSimleP4JJSON(Option option, File target, List<FeatureMatrix> featureMatrix) {
ParameterVector parameterVector = new ParameterVector(option.featureOption);
JsonObject jsonfile = new JsonObject();

for (FeatureVector featureVector : featureMatrix.get(0).getFeatureVectors()) {
List<FeatureCross> featureCrosses = featureVector.getNonSortedFeatureCrosses();
for (FeatureCross featureCross : featureCrosses) {
OriginalFeatureCross ofc = (OriginalFeatureCross) featureCross;
jsonfile.addProperty(ofc.getCrossType(), ofc.getFeatures().toString());

}
}
return jsonfile;

}

public Map processFilesPair(File pairFolder) {
Map<String, File> pathmap = new HashMap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
public interface FeatureExtractor {
// this is for CodeDiffer.java
FeatureVector extractFeature(Repair repair, CtElement atom);
FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom);
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,10 @@ private void getNearbyStmts(Repair repair, List<CtElement> stmtsF, List<CtElemen
if (!repair.isReplace)
stmtsL.add(srcElem);
}

@Override
public FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,10 @@ private void getNearbyStmts(Repair repair, List<CtElement> stmtsF, List<CtElemen
if (!repair.isReplace)
stmtsL.add(srcElem);
}

@Override
public FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,10 @@ private void getNearbyStmts(Repair repair, List<CtElement> stmtsF, List<CtElemen
if (!repair.isReplace)
stmtsL.add(srcElem);
}

@Override
public FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom) {
// TODO Auto-generated method stub
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ enum CrossType implements OriginalFeature {
POS_AF_RF_CT, // GlobalFeatureNum = 3 * AtomFeatureNum * RepairFeatureNum
POS_AF_AF_CT, // VarCrossFeatureNum = 3 * AtomFeatureNum * AtomFeatureNum
AF_VF_CT, // ValueCrossFeatureNum = AtomFeatureNum * ValueFeatureNum
SRC,
FORMER,
LATER
}

enum AtomicFeature implements OriginalFeature {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class OriginalFeatureCross implements FeatureCross, Serializable {
private Integer id;
private Double degree;
private List<Feature> features;
private CrossType crossType;

public OriginalFeatureCross(Integer id) {
this(id, 1.0);
Expand Down Expand Up @@ -54,6 +55,7 @@ public OriginalFeatureCross(Integer id, Double degree) {
public OriginalFeatureCross(CrossType crossType, List<Feature> features) {
this(crossType, features, 1.0);
}


public OriginalFeatureCross(CrossType crossType, List<Feature> features, Double degree) {
int ordinal0, ordinal1, ordinal2;
Expand Down Expand Up @@ -95,6 +97,7 @@ public OriginalFeatureCross(CrossType crossType, List<Feature> features, Double
}
this.degree = degree;
this.features = features;
this.crossType = crossType;
}

public Integer getId() {
Expand All @@ -105,6 +108,10 @@ public Double getDegree() {
return degree;
}

public String getCrossType() {
return this.crossType.toString();
}

public List<Feature> getFeatures() {
return features;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -301,7 +302,108 @@ public FeatureVector extractFeature(Repair repair, CtElement atom) {
}
return featureVector;
}

/**
* This function returns simple P4J features
*/
public FeatureVector extractSimpleP4JFeature(Repair repair, CtElement atom) {
List<CtElement> stmtsC = getCurrentStmts(repair);
List<CtElement> stmtsF = new ArrayList<>();
List<CtElement> stmtsL = new ArrayList<>();
getNearbyStmts(repair, stmtsF, stmtsL);

Map<String, Set<AtomicFeature>> srcMapC = new HashMap<>();
Map<String, Set<AtomicFeature>> srcMapF = new HashMap<>();
Map<String, Set<AtomicFeature>> srcMapL = new HashMap<>();

for (CtElement stmt : stmtsC) {
Map<String, Set<AtomicFeature>> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt);
map.forEach((k, v) -> srcMapC.merge(k, v, (v1, v2) -> {
v1.addAll(v2);
return v1;
}));
}
for (CtElement stmt : stmtsF) {
Map<String, Set<AtomicFeature>> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt);
map.forEach((k, v) -> srcMapF.merge(k, v, (v1, v2) -> {
v1.addAll(v2);
return v1;
}));
}
for (CtElement stmt : stmtsL) {
Map<String, Set<AtomicFeature>> map = new OriginalFeatureVisitor(valueExprInfo).traverseStmt(stmt);
map.forEach((k, v) -> srcMapL.merge(k, v, (v1, v2) -> {
v1.addAll(v2);
return v1;
}));
}

FeatureVector featureVector = new FeatureVector();

List<Feature> CFeatures = new ArrayList<>();
List<Feature> FFeatures = new ArrayList<>();
List<Feature> LFeatures = new ArrayList<>();

/**
* current position
*/
// repair features
EnumSet<RepairFeature> repairFeatures = getRepairFeatures(repair);
for (Feature repairFeature : repairFeatures) {
CFeatures.add(repairFeature);
}
// atomic and value features
for (String key : srcMapC.keySet()) {
Set<AtomicFeature> atomicFeatures = srcMapC.get(key);
for (AtomicFeature af : atomicFeatures) {
if (!CFeatures.contains(af)) {
CFeatures.add(af);
}
}
if (!key.contains("@")) {
Set<ValueFeature> valueFeatures = getValueFeature(key, repair, valueExprInfo);
for (ValueFeature vf : valueFeatures) {
if (!CFeatures.contains(vf)) {
CFeatures.add(vf);
}
}
}
}

/**
* former position
*/

for (String key : srcMapF.keySet()) {
Set<AtomicFeature> atomicFeatures = srcMapF.get(key);
for (AtomicFeature af : atomicFeatures) {
if (!FFeatures.contains(af)) {
FFeatures.add(af);
}
}
}

/**
* later position
*/

for (String key : srcMapL.keySet()) {
Set<AtomicFeature> atomicFeatures = srcMapL.get(key);
for (AtomicFeature af : atomicFeatures) {
if (!LFeatures.contains(af)) {
LFeatures.add(af);
}
}
}

featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.SRC, CFeatures));
featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.FORMER, FFeatures));
featureVector.addFeatureCross(new OriginalFeatureCross(CrossType.LATER, LFeatures));

return featureVector;
}


private List<CtElement> getCurrentStmts(Repair repair) {
List<CtElement> ret = new ArrayList<>();
CtElement srcElem = repair.srcElem;
Expand Down Expand Up @@ -374,7 +476,7 @@ private void getNearbyStmts(Repair repair, List<CtElement> stmtsF, List<CtElemen
e = idx + LOOKUP_DIS + 1;
boolean above = true;
for (int i = s; i < e; i++) {
if (tmp.get(i).equals(srcElem)) {
if (!tmp.get(i).equals(srcElem)) {
if (above)
stmtsF.add(tmp.get(i));
else
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/fr/inria/prophet4j/utility/CodeDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,21 @@ public class CodeDiffer {
private boolean byGenerator;
private Option option;
private String pathName;
private boolean cross=true;
private static final Logger logger = LogManager.getLogger(CodeDiffer.class.getName());

public CodeDiffer(boolean byGenerator, Option option) {
this.byGenerator = byGenerator;
this.option = option;
this.pathName = "";
}

public CodeDiffer(boolean byGenerator, Option option, boolean cross) {
this.byGenerator = byGenerator;
this.option = option;
this.pathName = "";
this.cross = cross;
}

public void setPathName(String pathName) {
this.pathName = pathName;
Expand Down Expand Up @@ -424,7 +432,12 @@ private List<FeatureMatrix> genFeatureMatrices(Diff diff, String fileKey) {
Repair repair = generator.obtainHumanRepair();
FeatureVector featureVector = new FeatureVector();
for (CtElement atom : repair.getCandidateAtoms()) {
if(cross) {
featureVector.merge(featureExtractor.extractFeature(repair, atom));
} else {
featureVector = featureExtractor.extractSimpleP4JFeature(repair, atom);
break;
}
}
featureVectors.add(featureVector);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fr/inria/prophet4j/utility/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public List<FeatureCross> getFeatureCrosses() {
list.sort(Comparator.comparingInt(FeatureCross::getId));
return list;
}

public List<FeatureCross> getNonSortedFeatureCrosses() {
List<FeatureCross> list = new ArrayList<>(featureCrosses);
return list;
}

public void merge(FeatureVector featureVector) {
this.featureCrosses.addAll(featureVector.getFeatureCrosses());
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config-coming.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ excludetests=false
addchangeinfoonfeatures=true
#By default, code comments are not considered
processcomments=false
#By default, coming extracts simple P4J features (do no consider the cross features)
cross=false
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public void testFeaturesOnS4REvolutionFromFolder1() throws Exception {
@Test
public void testFeaturesOnP4JEvolutionFromFolder1() throws Exception {
ComingMain main = new ComingMain();

CommandSummary cs = new CommandSummary();
cs.append("-input", "files");
cs.append("-location", (new File("src/main/resources/pairsD4j")).getAbsolutePath());
Expand Down

0 comments on commit 44c1bbd

Please sign in to comment.