Skip to content

Commit c35bebc

Browse files
committed
fixed bugs
1 parent 692d6c6 commit c35bebc

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ private static Relevance calculateFeatureSetToDeselect(VariationTree<DiffLinesLa
6161
return rho;
6262
}
6363

64-
private static Set<DiffNode<DiffLinesLabel>> findRootsOfSubtrees(Set<DiffNode<DiffLinesLabel>> nodes, boolean debug) {
64+
private static Set<DiffNode<DiffLinesLabel>> findRootsOfSubtrees(Set<DiffNode<DiffLinesLabel>> nodes, DiffType type, boolean debug) {
65+
Time time = (type == DiffType.ADD) ? Time.AFTER : Time.BEFORE;
6566
Set<DiffNode<DiffLinesLabel>> subtreeRoots = new HashSet<DiffNode<DiffLinesLabel>>();
6667
for (DiffNode<DiffLinesLabel> node : nodes) {
67-
if (!nodes.contains(node.getParent(Time.AFTER))) {
68+
if (!nodes.contains(node.getParent(time))) {
6869
subtreeRoots.add(node);
6970
}
7071
}
@@ -73,7 +74,7 @@ private static Set<DiffNode<DiffLinesLabel>> findRootsOfSubtrees(Set<DiffNode<Di
7374
return subtreeRoots;
7475
}
7576

76-
private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> targetVariantDiff, Set<DiffNode<DiffLinesLabel>> subtreeRoots, VariationDiffSource source, boolean debug) {
77+
private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> targetVariantDiffUnchanged, VariationDiff<DiffLinesLabel> targetVariantDiffPatched, Set<DiffNode<DiffLinesLabel>> subtreeRoots, VariationDiffSource source, boolean debug) {
7778
Time time = (type == DiffType.ADD) ? Time.AFTER : Time.BEFORE;
7879

7980
for (DiffNode<DiffLinesLabel> root : subtreeRoots) {
@@ -84,29 +85,35 @@ private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> ta
8485

8586
List<DiffNode<DiffLinesLabel>> targetNodes = new ArrayList<DiffNode<DiffLinesLabel>>();
8687
if (root.isArtifact()) {
87-
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
88+
targetNodes = targetVariantDiffUnchanged.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
8889
.equals(root.getPresenceCondition(time)) && node.isAnnotation());
8990
} else if (root.isAnnotation()) {
90-
targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
91+
System.out.println(root.getParent(time).getPresenceCondition(time));
92+
targetNodes = targetVariantDiffUnchanged.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
9193
.equals(root.getParent(time).getPresenceCondition(time)) && node.isAnnotation());
9294
}
9395

9496
if (targetNodes.size() != 1) {
9597
System.out.println("too much or too less target nodes found");
9698
} else {
99+
DiffNode<DiffLinesLabel> targetNodeInPatch = targetVariantDiffPatched.getNodeWithID(targetNodes.get(0).getID());
100+
System.out.println(targetNodeInPatch.toString());
97101
if (type == DiffType.ADD) {
98102
System.out.println("subtree added");
99-
targetNodes.get(0).addChild(root.deepCopy(), Time.AFTER);
100-
System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER));
103+
// TODO: check for neighbors and calculate insert position
104+
targetNodeInPatch.addChild(root.deepCopy(), time);
105+
System.out.println(targetNodeInPatch.getChildOrder(time));
101106
} else if (type == DiffType.REM) {
102-
DiffNode<DiffLinesLabel> parent = targetNodes.get(0);
103107
List<DiffNode<DiffLinesLabel>> nodesToRem = new ArrayList<DiffNode<DiffLinesLabel>>();
104-
parent.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, Time.BEFORE)) nodesToRem.add(node);});
108+
System.out.println("Root: " + root.toString());
109+
System.out.println("Children: " + targetNodeInPatch.getAllChildrenSet());
110+
targetNodeInPatch.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, time)) nodesToRem.add(node);});
105111
if (nodesToRem.size() != 1) {
106112
System.out.println("too much or too less target nodes found");
107113
} else {
108114
System.out.println("subtree removed");
109115
nodesToRem.get(0).diffType = DiffType.REM;
116+
// TODO: check for neighbors
110117
nodesToRem.get(0).drop(Time.AFTER);
111118
System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER));
112119
}
@@ -126,26 +133,27 @@ private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVari
126133
Relevance rho = calculateFeatureSetToDeselect(sourceVariantVersion1, sourceVariantVersion2, targetVariant, false);
127134
VariationDiff<DiffLinesLabel> optimizedDiff = DiffView.optimized(diff, rho);
128135
VariationDiffSource source = optimizedDiff.getSource();
129-
VariationDiff<DiffLinesLabel> targetVariantDiff = targetVariant.toCompletelyUnchangedVariationDiff();
136+
VariationDiff<DiffLinesLabel> targetVariantDiffUnchanged = targetVariant.toCompletelyUnchangedVariationDiff();
137+
VariationDiff<DiffLinesLabel> targetVariantDiffPatched = targetVariant.toCompletelyUnchangedVariationDiff();
130138

131139
// add new nodes
132140
Set<DiffNode<DiffLinesLabel>> addedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
133141
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);
142+
Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, DiffType.ADD, false);
143+
applyChanges(DiffType.ADD, targetVariantDiffUnchanged, targetVariantDiffPatched, addedSubtreeRoots, source, false);
136144
// remove old nodes
137145
Set<DiffNode<DiffLinesLabel>> removedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
138146
optimizedDiff.forAll(node -> {if (node.isRem()) {removedNodes.add(node);}});
139-
Set<DiffNode<DiffLinesLabel>> removedSubtreeRoots = findRootsOfSubtrees(removedNodes, false);
140-
applyChanges(DiffType.REM, targetVariantDiff, removedSubtreeRoots, source, false);
147+
Set<DiffNode<DiffLinesLabel>> removedSubtreeRoots = findRootsOfSubtrees(removedNodes, DiffType.REM, false);
148+
applyChanges(DiffType.REM, targetVariantDiffUnchanged, targetVariantDiffPatched, removedSubtreeRoots, source, false);
141149

142150
GameEngine.showAndAwaitAll(
143151
Show.tree(sourceVariantVersion1),
144152
Show.tree(sourceVariantVersion2),
145153
Show.tree(targetVariant),
146154
Show.diff(optimizedDiff),
147-
Show.diff(targetVariantDiff),
148-
Show.tree(targetVariantDiff.project(Time.AFTER))
155+
Show.diff(targetVariantDiffPatched),
156+
Show.tree(targetVariantDiffPatched.project(Time.AFTER))
149157
);
150158
}
151159

@@ -167,7 +175,8 @@ private static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String f
167175
}
168176

169177
public static void main(String[] args) {
170-
patchVariationTrees(parseVariationTreeFromFile("exampleA1.cpp"), parseVariationTreeFromFile("exampleA2.cpp"), parseVariationTreeFromFile("exampleB.cpp"));
178+
// patchVariationTrees(parseVariationTreeFromFile("exampleA1Add.cpp"), parseVariationTreeFromFile("exampleA2Add.cpp"), parseVariationTreeFromFile("exampleBAdd.cpp"));
179+
patchVariationTrees(parseVariationTreeFromFile("exampleA1Rem.cpp"), parseVariationTreeFromFile("exampleA2Rem.cpp"), parseVariationTreeFromFile("exampleBRem.cpp"));
171180
}
172181

173182
}

0 commit comments

Comments
 (0)