gumtree.spoon.diff.ActionClassifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gumtree-spoon-ast-diff Show documentation
Show all versions of gumtree-spoon-ast-diff Show documentation
Computes the AST difference between two Spoon abstract syntax trees using the Gumtree algorithm.
package gumtree.spoon.diff;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.actions.model.Delete;
import com.github.gumtreediff.actions.model.Insert;
import com.github.gumtreediff.actions.model.Move;
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.matchers.Mapping;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.tree.ITree;
import gumtree.spoon.builder.SpoonGumTreeBuilder;
/**
* Action Classifier
*
* @author Matias Martinez, [email protected]
*/
public class ActionClassifier {
// /
// ROOT CLASSIFIER
// /
private Set srcUpdTrees = new HashSet<>();
private Set dstUpdTrees = new HashSet<>();
private Set srcMvTrees = new HashSet<>();
private Set dstMvTrees = new HashSet<>();
private Set srcDelTrees = new HashSet<>();
private Set dstAddTrees = new HashSet<>();
private Map originalActionsSrc = new HashMap<>();
private Map originalActionsDst = new HashMap<>();
public ActionClassifier(Set rawMappings, List actions) {
clean();
MappingStore mappings = new MappingStore(rawMappings);
for (Action action : actions) {
final ITree original = action.getNode();
if (action instanceof Delete) {
srcDelTrees.add(original);
originalActionsSrc.put(original, action);
} else if (action instanceof Insert) {
dstAddTrees.add(original);
originalActionsDst.put(original, action);
} else if (action instanceof Update) {
ITree dest = mappings.getDst(original);
original.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT_DEST,
dest.getMetadata(SpoonGumTreeBuilder.SPOON_OBJECT));
srcUpdTrees.add(original);
dstUpdTrees.add(dest);
originalActionsSrc.put(original, action);
} else if (action instanceof Move) {
ITree dest = mappings.getDst(original);
original.setMetadata(SpoonGumTreeBuilder.SPOON_OBJECT_DEST,
dest.getMetadata(SpoonGumTreeBuilder.SPOON_OBJECT));
srcMvTrees.add(original);
dstMvTrees.add(dest);
originalActionsDst.put(dest, action);
}
}
}
/**
* This method retrieves ONLY the ROOT actions
*/
public List getRootActions() {
final List rootActions = srcUpdTrees.stream().map(t -> originalActionsSrc.get(t))
.collect(Collectors.toList());
rootActions.addAll(srcDelTrees.stream() //
.filter(t -> !srcDelTrees.contains(t.getParent()) && !srcUpdTrees.contains(t.getParent())) //
.map(t -> originalActionsSrc.get(t)) //
.collect(Collectors.toList()));
rootActions.addAll(dstAddTrees.stream() //
.filter(t -> !dstAddTrees.contains(t.getParent()) && !dstUpdTrees.contains(t.getParent())) //
.map(t -> originalActionsDst.get(t)) //
.collect(Collectors.toList()));
rootActions.addAll(dstMvTrees.stream() //
.filter(t -> !dstMvTrees.contains(t.getParent())) //
.map(t -> originalActionsDst.get(t)) //
.collect(Collectors.toList()));
rootActions.removeAll(Collections.singleton(null));
return rootActions;
}
private void clean() {
srcUpdTrees.clear();
dstUpdTrees.clear();
srcMvTrees.clear();
dstMvTrees.clear();
srcDelTrees.clear();
dstAddTrees.clear();
originalActionsSrc.clear();
originalActionsDst.clear();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy