Skip to content

Commit

Permalink
GumtreeHelper refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
khesoem committed Oct 24, 2019
1 parent b895eff commit 6aa307f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import fr.inria.coming.utils.GumtreeHelper;
import fr.inria.coming.utils.EntityTypesInfoResolver;
import org.apache.log4j.Logger;

import com.github.gumtreediff.actions.model.Action;
Expand Down Expand Up @@ -450,7 +450,7 @@ private List<MatchingEntity> matchElements(Operation affectedOperation, PatternE
int i_levels = 1;
// Scale the parent hierarchy and check types.
while (currentNodeFromAction != null && i_levels <= parentLevel) {
String typeOfNode = GumtreeHelper.getNodeLabelFromCtElement(currentNodeFromAction);
String typeOfNode = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentNodeFromAction);
String valueOfNode = currentNodeFromAction.toString();
String roleInParent = (currentNodeFromAction.getRoleInParent() != null)
? currentNodeFromAction.getRoleInParent().toString().toLowerCase()
Expand All @@ -461,7 +461,7 @@ private List<MatchingEntity> matchElements(Operation affectedOperation, PatternE
("*".equals(parentEntity.getEntityType())
// || (typeOfNode != null && typeOfNode.equals(parentEntity.getEntityType())))
|| (typeOfNode != null &&
GumtreeHelper.getInstance().isAChildOf(typeOfNode, parentEntity.getEntityType())))
EntityTypesInfoResolver.getInstance().isAChildOf(typeOfNode, parentEntity.getEntityType())))
///
&&
// value of element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import fr.inria.coming.core.entities.output.JSonPatternInstanceOutput;
import fr.inria.coming.main.ComingProperties;
import fr.inria.coming.repairability.models.InstanceStats;
import fr.inria.coming.utils.GumtreeHelper;
import gumtree.spoon.diff.Diff;
import gumtree.spoon.diff.operations.Operation;
import org.apache.log4j.Logger;
Expand Down Expand Up @@ -100,7 +99,7 @@ public void getInstancesOfRevision(RevisionResult revisionResult, JsonArray inst
}

if(isRootOperation(op, diff)){
InstanceStats instanceStats = GumtreeHelper.getStats(op);
InstanceStats instanceStats = getOperationStats(op);
opjson.add("stats", getJSONFromInstanceStats(instanceStats));
}
ops.add(opjson);
Expand All @@ -125,4 +124,17 @@ private boolean isRootOperation(Operation op, Diff diff) {
}
return false;
}

private InstanceStats getOperationStats(Operation operation) {
InstanceStats stats = new InstanceStats();
if (operation.getSrcNode() != null) {
stats.setSrcEntityTypes(operation.getSrcNode().getReferencedTypes());
stats.setNumberOfSrcEntities(operation.getSrcNode().getElements(null).size());
}
if (operation.getDstNode() != null) {
stats.setDstEntityTypes(operation.getDstNode().getReferencedTypes());
stats.setNumberOfDstEntities(operation.getDstNode().getElements(null).size());
}
return stats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import fr.inria.coming.changeminer.analyzer.patternspecification.ChangePatternSpecification;
import fr.inria.coming.changeminer.entity.IRevision;
import fr.inria.coming.changeminer.util.PatternXMLParser;
import fr.inria.coming.utils.GumtreeHelper;
import fr.inria.coming.utils.EntityTypesInfoResolver;
import gumtree.spoon.diff.Diff;
import gumtree.spoon.diff.operations.Operation;
import spoon.reflect.code.*;
Expand Down Expand Up @@ -87,8 +87,8 @@ public boolean filter(ChangePatternInstance instance, IRevision revision) {

for (int i = 0; i < allSrcElements.size() - allDstElements.size() + 1; i++) {
CtElement currentSrcElement = allSrcElements.get(i);
String typeOfCurrentSrcElement = GumtreeHelper.getNodeLabelFromCtElement(currentSrcElement);
if (!GumtreeHelper.getInstance().isAChildOf(typeOfCurrentSrcElement, "Expression")) {
String typeOfCurrentSrcElement = EntityTypesInfoResolver.getNodeLabelFromCtElement(currentSrcElement);
if (!EntityTypesInfoResolver.getInstance().isAChildOf(typeOfCurrentSrcElement, "Expression")) {
continue;
}
String srcAsString = currentSrcElement.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package fr.inria.coming.utils;

import fr.inria.coming.repairability.models.InstanceStats;
import gumtree.spoon.diff.operations.Operation;
import org.reflections.Reflections;
import spoon.reflect.declaration.CtElement;

Expand All @@ -13,35 +11,34 @@
/**
* Created by khesoem on 10/4/2019.
*/
public class GumtreeHelper {
private static GumtreeHelper _instance = null;
public class EntityTypesInfoResolver {
private static EntityTypesInfoResolver _instance = null;
private static final String CLASSES_HIERARCHY_PATH = "src/main/resources/gumtree-inheritance-relations.txt";

private Map<String, Set<String>> childrenToParents;
private Map<String, Set<String>> childrenToParentsRelationsBetweenEntityTypes;

public static GumtreeHelper getInstance(){
if(_instance == null)
_instance = new GumtreeHelper();
public static EntityTypesInfoResolver getInstance() {
if (_instance == null)
_instance = new EntityTypesInfoResolver();
return _instance;
}

public GumtreeHelper(){
loadClassToParents();
public EntityTypesInfoResolver() {
loadChildrenToParentsRelationsBetweenEntityTypes();
}

private void loadClassToParents() {
childrenToParents = new HashMap<>();

private void loadChildrenToParentsRelationsBetweenEntityTypes() {
childrenToParentsRelationsBetweenEntityTypes = new HashMap<>();
try {
Scanner sc = new Scanner(new File(CLASSES_HIERARCHY_PATH));

while(sc.hasNextLine()){
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(":");

String child = parts[0];
String[] parents = parts[1].split(" ");
childrenToParents.put(child, new HashSet<>(Arrays.asList(parents)));
childrenToParentsRelationsBetweenEntityTypes.put(child, new HashSet<>(Arrays.asList(parents)));
}

sc.close();
Expand All @@ -50,11 +47,11 @@ private void loadClassToParents() {
}
}

// says whether parent is an ancestor of or equal to child.
public boolean isAChildOf(String child, String parent){
if(!childrenToParents.containsKey(child))
// checks whether parent is an ancestor of or equal to child.
public boolean isAChildOf(String childEntityTypeName, String parentEntityTypeName) {
if (!childrenToParentsRelationsBetweenEntityTypes.containsKey(childEntityTypeName))
return false;
return childrenToParents.get(child).contains(parent);
return childrenToParentsRelationsBetweenEntityTypes.get(childEntityTypeName).contains(parentEntityTypeName);
}

// public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException {
Expand All @@ -74,32 +71,32 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th

// initializing childrenToParents
Set<Class> toBeIgnored = new HashSet<>();
for(Class clazz : allClasses){
if(!clazz.getSimpleName().startsWith("Ct")) {
for (Class clazz : allClasses) {
if (!clazz.getSimpleName().startsWith("Ct")) {
toBeIgnored.add(clazz);
continue;
}
childrenToParents.put(clazz.getSimpleName().substring(2), new HashSet<>());
}
allClasses.removeAll(toBeIgnored);

for(Class clazz : allClasses){
for (Class clazz : allClasses) {
String currentClassName = clazz.getSimpleName().substring(2);
Set<Class<? extends CtElement>> childrenOfCurrentClass = reflections.getSubTypesOf(clazz);
for(Class childOfCurrentClass : childrenOfCurrentClass){
if(!childOfCurrentClass.getSimpleName().startsWith("Ct"))
for (Class childOfCurrentClass : childrenOfCurrentClass) {
if (!childOfCurrentClass.getSimpleName().startsWith("Ct"))
continue;
String currentChildName = childOfCurrentClass.getSimpleName().substring(2);
childrenToParents.get(currentChildName).add(currentClassName);
}
childrenToParents.get(currentClassName).add(currentClassName); // each class is considered as an ancestor of itself
}

for(Map.Entry<String, Set> childToParents : childrenToParents.entrySet()){
for (Map.Entry<String, Set> childToParents : childrenToParents.entrySet()) {
String className = childToParents.getKey();
pw.print(className + ":");
Set<String> parents = childToParents.getValue();
for(String parent : parents){
for (String parent : parents) {
pw.print(" " + parent);
}
pw.print("\n");
Expand All @@ -109,35 +106,6 @@ private static void extractAndSaveCtElementsHierarchyModel(String outputPath) th
pw.close();
}

public static InstanceStats getStats(Operation operation) {
InstanceStats stats = new InstanceStats();
if(operation.getSrcNode() != null) {
stats.setSrcEntityTypes(operation.getSrcNode().getReferencedTypes());
try { // FIXME: exception should not be thrown
stats.setNumberOfSrcEntities(operation.getSrcNode().getElements(null).size());
}catch (Exception e){
// e.printStackTrace();
}
}
if(operation.getDstNode() != null) {
stats.setDstEntityTypes(operation.getDstNode().getReferencedTypes());
try { // FIXME: exception should not be thrown
stats.setNumberOfDstEntities(operation.getDstNode().getElements(null).size());
}catch (Exception e){
// e.printStackTrace();
}
}
return stats;
}

public Map<String, Set<String>> getChildrenToParents() {
return childrenToParents;
}

public void setChildrenToParents(Map<String, Set<String>> childrenToParents) {
this.childrenToParents = childrenToParents;
}

/**
* The label of a CtElement is the simple name of the class without the CT
* prefix.
Expand Down

0 comments on commit 6aa307f

Please sign in to comment.