Skip to content

Commit 692d6c6

Browse files
committed
code refactoring
1 parent 1b54094 commit 692d6c6

1 file changed

Lines changed: 57 additions & 61 deletions

File tree

src/main/java/org/variantsync/diffdetective/experiments/thesis_pm/PatchingExperiment.java

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818
import org.variantsync.diffdetective.variation.diff.Time;
1919
import org.variantsync.diffdetective.variation.diff.VariationDiff;
2020
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
21+
import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource;
2122
import org.variantsync.diffdetective.variation.diff.view.DiffView;
2223
import org.variantsync.diffdetective.variation.tree.VariationTree;
2324
import org.variantsync.diffdetective.variation.tree.view.relevance.Configure;
2425
import org.variantsync.diffdetective.variation.tree.view.relevance.Relevance;
2526

2627
public class PatchingExperiment {
2728

28-
private static Relevance calculateFeatureSetToDeselect(VariationTree<DiffLinesLabel> variant1, VariationTree<DiffLinesLabel> variant2, boolean debug) {
29+
private static Relevance calculateFeatureSetToDeselect(VariationTree<DiffLinesLabel> variant1version1, VariationTree<DiffLinesLabel> variant1version2, VariationTree<DiffLinesLabel> variant2, boolean debug) {
2930
Set<String> featuresTreeV1 = new HashSet<String>();
30-
variant1.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());});
31+
variant1version1.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());});
32+
if (variant1version2 != null) {
33+
variant1version2.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());});
34+
}
3135
Set<String> featuresTreeV2 = new HashSet<String>();
3236
variant2.forAllPreorder(node -> {featuresTreeV2.addAll(node.getFeatureMapping().getUniqueContainedFeatures());});
3337

@@ -69,88 +73,80 @@ private static Set<DiffNode<DiffLinesLabel>> findRootsOfSubtrees(Set<DiffNode<Di
6973
return subtreeRoots;
7074
}
7175

72-
private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVariantVersion1, VariationTree<DiffLinesLabel> sourceVariantVersion2, VariationTree<DiffLinesLabel> targetVariant) {
73-
if (sourceVariantVersion1 == null || sourceVariantVersion2 == null || targetVariant == null) {
74-
System.out.println("Parsing error");
75-
return;
76-
}
76+
private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> targetVariantDiff, Set<DiffNode<DiffLinesLabel>> subtreeRoots, VariationDiffSource source, boolean debug) {
77+
Time time = (type == DiffType.ADD) ? Time.AFTER : Time.BEFORE;
7778

78-
VariationDiff<DiffLinesLabel> diff = VariationDiff.fromTrees(sourceVariantVersion1, sourceVariantVersion2);
79-
Relevance rho = calculateFeatureSetToDeselect(sourceVariantVersion1, targetVariant, false);
80-
VariationDiff<DiffLinesLabel> optimizedDiff = DiffView.optimized(diff, rho);
81-
82-
// add new nodes
83-
Set<DiffNode<DiffLinesLabel>> addedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
84-
optimizedDiff.forAll(node -> {if (node.isAdd()) {addedNodes.add(node);}});
85-
86-
// find roots of subtrees of added nodes
87-
Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, false);
88-
89-
VariationDiff<DiffLinesLabel> targetVariantDiff = VariationDiff.fromTrees(targetVariant, targetVariant);
90-
for (DiffNode<DiffLinesLabel> root : addedSubtreeRoots) {
91-
VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), optimizedDiff.getSource());
92-
GameEngine.showAndAwaitAll(Show.diff(subTree));
79+
for (DiffNode<DiffLinesLabel> root : subtreeRoots) {
80+
if (debug) {
81+
VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), source);
82+
GameEngine.showAndAwaitAll(Show.diff(subTree));
83+
}
84+
9385
List<DiffNode<DiffLinesLabel>> targetNodes = new ArrayList<DiffNode<DiffLinesLabel>>();
9486
if (root.isArtifact()) {
9587
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
96-
.equals(root.getPresenceCondition(Time.AFTER)) && node.isAnnotation());
88+
.equals(root.getPresenceCondition(time)) && node.isAnnotation());
9789
} else if (root.isAnnotation()) {
9890
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
99-
.equals(root.getParent(Time.AFTER).getPresenceCondition(Time.AFTER)) && node.isAnnotation());
91+
.equals(root.getParent(time).getPresenceCondition(time)) && node.isAnnotation());
10092
}
10193

10294
if (targetNodes.size() != 1) {
10395
System.out.println("too much or too less target nodes found");
10496
} else {
105-
System.out.println("subtree added");
106-
targetNodes.get(0).addChild(root.deepCopy(), Time.AFTER);
97+
if (type == DiffType.ADD) {
98+
System.out.println("subtree added");
99+
targetNodes.get(0).addChild(root.deepCopy(), Time.AFTER);
100+
System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER));
101+
} else if (type == DiffType.REM) {
102+
DiffNode<DiffLinesLabel> parent = targetNodes.get(0);
103+
List<DiffNode<DiffLinesLabel>> nodesToRem = new ArrayList<DiffNode<DiffLinesLabel>>();
104+
parent.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, Time.BEFORE)) nodesToRem.add(node);});
105+
if (nodesToRem.size() != 1) {
106+
System.out.println("too much or too less target nodes found");
107+
} else {
108+
System.out.println("subtree removed");
109+
nodesToRem.get(0).diffType = DiffType.REM;
110+
nodesToRem.get(0).drop(Time.AFTER);
111+
System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER));
112+
}
113+
}
107114
}
108115
}
109-
110-
// remove old nodes
116+
}
117+
118+
119+
private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVariantVersion1, VariationTree<DiffLinesLabel> sourceVariantVersion2, VariationTree<DiffLinesLabel> targetVariant) {
120+
if (sourceVariantVersion1 == null || sourceVariantVersion2 == null || targetVariant == null) {
121+
System.out.println("Parsing error");
122+
return;
123+
}
124+
125+
VariationDiff<DiffLinesLabel> diff = VariationDiff.fromTrees(sourceVariantVersion1, sourceVariantVersion2);
126+
Relevance rho = calculateFeatureSetToDeselect(sourceVariantVersion1, sourceVariantVersion2, targetVariant, false);
127+
VariationDiff<DiffLinesLabel> optimizedDiff = DiffView.optimized(diff, rho);
128+
VariationDiffSource source = optimizedDiff.getSource();
129+
VariationDiff<DiffLinesLabel> targetVariantDiff = targetVariant.toCompletelyUnchangedVariationDiff();
130+
131+
// add new nodes
132+
Set<DiffNode<DiffLinesLabel>> addedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
133+
optimizedDiff.forAll(node -> {if (node.isAdd()) {addedNodes.add(node);}});
134+
Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, false);
135+
applyChanges(DiffType.ADD, targetVariantDiff, addedSubtreeRoots, source, false);
136+
// remove old nodes
111137
Set<DiffNode<DiffLinesLabel>> removedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
112138
optimizedDiff.forAll(node -> {if (node.isRem()) {removedNodes.add(node);}});
113-
114139
Set<DiffNode<DiffLinesLabel>> removedSubtreeRoots = findRootsOfSubtrees(removedNodes, false);
115-
116-
// VariationDiff<DiffLinesLabel> targetVariantDiff2 = VariationDiff.fromTrees(targetVariant, targetVariant);
117-
for (DiffNode<DiffLinesLabel> root : removedSubtreeRoots) {
118-
VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), optimizedDiff.getSource());
119-
GameEngine.showAndAwaitAll(Show.diff(subTree));
120-
List<DiffNode<DiffLinesLabel>> targetNodes = new ArrayList<DiffNode<DiffLinesLabel>>();
121-
if (root.isArtifact()) {
122-
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
123-
.equals(root.getPresenceCondition(Time.BEFORE)) && node.isAnnotation());
124-
} else if (root.isAnnotation()) {
125-
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
126-
.equals(root.getParent(Time.BEFORE).getPresenceCondition(Time.BEFORE)) && node.isAnnotation());
127-
}
128-
if (targetNodes.size() != 1) {
129-
System.out.println("too much or too less target nodes found");
130-
} else {
131-
132-
// System.out.println(targetNodes.get(0));
133-
DiffNode<DiffLinesLabel> parent = targetNodes.get(0);
134-
List<DiffNode<DiffLinesLabel>> nodesToRem = new ArrayList<DiffNode<DiffLinesLabel>>();
135-
parent.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, Time.BEFORE)) nodesToRem.add(node);});
136-
if (nodesToRem.size() != 1) {
137-
System.out.println("too much or too less target nodes found");
138-
} else {
139-
System.out.println("subtree removed");
140-
nodesToRem.get(0).diffType = DiffType.REM;
141-
nodesToRem.get(0).drop(Time.AFTER);
142-
}
143-
}
144-
}
140+
applyChanges(DiffType.REM, targetVariantDiff, removedSubtreeRoots, source, false);
145141

146142
GameEngine.showAndAwaitAll(
147143
Show.tree(sourceVariantVersion1),
148144
Show.tree(sourceVariantVersion2),
149145
Show.tree(targetVariant),
150146
Show.diff(optimizedDiff),
151-
Show.diff(targetVariantDiff)
147+
Show.diff(targetVariantDiff),
148+
Show.tree(targetVariantDiff.project(Time.AFTER))
152149
);
153-
154150
}
155151

156152
private static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String file) {

0 commit comments

Comments
 (0)