Skip to content

Commit 28cd47b

Browse files
committed
consider order when patching (line numbers), fixed bugs
1 parent 1b59508 commit 28cd47b

1 file changed

Lines changed: 66 additions & 18 deletions

File tree

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

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.Iterator;
88
import java.util.List;
99
import java.util.Set;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
1012

1113
import org.variantsync.diffdetective.diff.result.DiffParseException;
1214
import org.variantsync.diffdetective.show.Show;
@@ -93,11 +95,12 @@ private static DiffNode<DiffLinesLabel> getChildFromListIfIndexInRange(List<Diff
9395
}
9496

9597
private static boolean checkNeighbors(DiffNode<DiffLinesLabel> root, DiffNode<DiffLinesLabel> targetNodeInPatch,
96-
DiffNode<DiffLinesLabel> node, Time time) {
98+
DiffNode<DiffLinesLabel> node, Time time, boolean debug) {
9799

98100
List<DiffNode<DiffLinesLabel>> orderedChildrenTarget = targetNodeInPatch.getChildOrder(time);
99101
if (!orderedChildrenTarget.contains(node)) {
100102
// TODO: throw exception
103+
System.out.println("empty");
101104
return false;
102105
}
103106
int indexTarget = orderedChildrenTarget.indexOf(node);
@@ -112,6 +115,23 @@ private static boolean checkNeighbors(DiffNode<DiffLinesLabel> root, DiffNode<Di
112115
indexSource - 1);
113116
DiffNode<DiffLinesLabel> neighborAfterSource = getChildFromListIfIndexInRange(orderedChildrenSource,
114117
indexSource + 1);
118+
119+
if (neighborBeforeSource != null && neighborBeforeSource.diffType == DiffType.REM) {
120+
neighborBeforeSource = null;
121+
}
122+
123+
if (debug) {
124+
if (neighborBeforeSource != null) {
125+
System.out.print("Neighbor before: " + neighborBeforeSource.toString() + "; ");
126+
} else {
127+
System.out.print("No neighbor before; ");
128+
}
129+
if (neighborAfterSource != null) {
130+
System.out.print("Neighbor after: " + neighborAfterSource.toString() + "\n");
131+
} else {
132+
System.out.print("No neighbor after\n");
133+
}
134+
}
115135

116136
if ((neighborBeforeSource != null && neighborBeforeTarget == null)
117137
|| (neighborBeforeSource == null && neighborBeforeTarget != null)) {
@@ -164,15 +184,33 @@ private static int findPositionOfMatchingNeighborInList(DiffNode<DiffLinesLabel>
164184
}
165185

166186
private static int findInsertPosition(DiffNode<DiffLinesLabel> root, DiffNode<DiffLinesLabel> targetNodeInPatch,
167-
Time time) throws Exception {
168-
187+
Time time, boolean debug) throws Exception {
188+
if (debug) System.out.println("Root node to insert: " + root.toString());
169189
List<DiffNode<DiffLinesLabel>> orderedChildrenTarget = targetNodeInPatch.getChildOrder(time);
190+
if (debug) System.out.println("Children of target node: " + orderedChildrenTarget.toString());
170191
List<DiffNode<DiffLinesLabel>> orderedChildrenSource = root.getParent(time).getChildOrder(time);
192+
if (debug) System.out.println("Children of source node: " + orderedChildrenSource.toString());
171193
int indexSource = orderedChildrenSource.indexOf(root);
172194
DiffNode<DiffLinesLabel> neighborBeforeSource = getChildFromListIfIndexInRange(orderedChildrenSource,
173195
indexSource - 1);
174196
DiffNode<DiffLinesLabel> neighborAfterSource = getChildFromListIfIndexInRange(orderedChildrenSource,
175197
indexSource + 1);
198+
if (neighborAfterSource != null && neighborAfterSource.diffType == DiffType.ADD) {
199+
neighborAfterSource = null;
200+
}
201+
if (debug) {
202+
if (neighborBeforeSource != null) {
203+
System.out.print("Neighbor before: " + neighborBeforeSource.toString() + "; ");
204+
} else {
205+
System.out.print("No neighbor before; ");
206+
}
207+
if (neighborAfterSource != null) {
208+
System.out.print("Neighbor after: " + neighborAfterSource.toString() + "\n");
209+
} else {
210+
System.out.print("No neighbor after\n");
211+
}
212+
}
213+
176214
int indexBefore = -2;
177215
int indexAfter = -2;
178216

@@ -191,14 +229,15 @@ private static int findInsertPosition(DiffNode<DiffLinesLabel> root, DiffNode<Di
191229
// TODO: Alignment Problem
192230
return -1;
193231
}
194-
return indexBefore;
232+
return indexAfter;
195233
}
196-
return Math.max(indexBefore, indexAfter);
234+
return Math.max(indexBefore + 1, indexAfter);
197235
}
198236

199237
private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> targetVariantDiffUnchanged,
200-
VariationDiff<DiffLinesLabel> targetVariantDiffPatched, Set<DiffNode<DiffLinesLabel>> subtreeRoots,
238+
VariationDiff<DiffLinesLabel> targetVariantDiffPatched, List<DiffNode<DiffLinesLabel>> subtreeRoots,
201239
VariationDiffSource source, boolean debug) throws Exception {
240+
202241
Time time = (type == DiffType.ADD) ? Time.AFTER : Time.BEFORE;
203242

204243
for (DiffNode<DiffLinesLabel> root : subtreeRoots) {
@@ -213,12 +252,11 @@ private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> ta
213252
node -> node.getPresenceCondition(Time.AFTER).equals(root.getPresenceCondition(time))
214253
&& node.isAnnotation());
215254
} else if (root.isAnnotation()) {
216-
System.out.println(root.getParent(time).getPresenceCondition(time));
217255
targetNodes = targetVariantDiffUnchanged
218256
.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER)
219257
.equals(root.getParent(time).getPresenceCondition(time)) && node.isAnnotation());
220258
}
221-
259+
222260
if (targetNodes.size() != 1) {
223261
System.out.println("too much or too less target nodes found");
224262
} else {
@@ -227,7 +265,7 @@ private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> ta
227265
System.out.println(targetNodeInPatch.toString());
228266
if (type == DiffType.ADD) {
229267

230-
int insertPosition = findInsertPosition(root, targetNodeInPatch, time);
268+
int insertPosition = findInsertPosition(root, targetNodeInPatch, time, true);
231269
if (insertPosition < 0) {
232270
System.out.println("no matching insert position found");
233271
} else {
@@ -247,10 +285,11 @@ private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> ta
247285
System.out.println("Nodes to remove: " + nodesToRem);
248286

249287
List<DiffNode<DiffLinesLabel>> nodesToRemAfterCheckingNeighbors = nodesToRem.stream()
250-
.filter((node) -> checkNeighbors(node, root, targetNodeInPatch, time)).toList();
288+
.filter((node) -> checkNeighbors(root, targetNodeInPatch, node, time, true)).toList();
251289

252290
if (nodesToRemAfterCheckingNeighbors.size() != 1) {
253291
System.out.println("too much or too less target nodes found");
292+
System.out.println(nodesToRemAfterCheckingNeighbors.toString());
254293
} else {
255294
System.out.println("subtree removed");
256295
nodesToRemAfterCheckingNeighbors.get(0).diffType = DiffType.REM;
@@ -260,6 +299,7 @@ private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> ta
260299
System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER));
261300
}
262301
}
302+
GameEngine.showAndAwaitAll(Show.diff(targetVariantDiffPatched));
263303
}
264304
}
265305
}
@@ -288,8 +328,12 @@ private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVari
288328
}
289329
});
290330
Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, DiffType.ADD, false);
291-
applyChanges(DiffType.ADD, targetVariantDiffUnchanged, targetVariantDiffPatched, addedSubtreeRoots, source,
292-
false);
331+
List<DiffNode<DiffLinesLabel>> addedSortedSubtreeRoots = addedSubtreeRoots.stream().sorted((n1, n2) -> Integer
332+
.compare(n1.getLinesAtTime(Time.AFTER).fromInclusive(), n2.getLinesAtTime(Time.AFTER).fromInclusive()))
333+
.collect(Collectors.toList());
334+
applyChanges(DiffType.ADD, targetVariantDiffUnchanged, targetVariantDiffPatched, addedSortedSubtreeRoots,
335+
source, false);
336+
293337
// remove old nodes
294338
Set<DiffNode<DiffLinesLabel>> removedNodes = new HashSet<DiffNode<DiffLinesLabel>>();
295339
optimizedDiff.forAll(node -> {
@@ -298,8 +342,12 @@ private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVari
298342
}
299343
});
300344
Set<DiffNode<DiffLinesLabel>> removedSubtreeRoots = findRootsOfSubtrees(removedNodes, DiffType.REM, false);
301-
applyChanges(DiffType.REM, targetVariantDiffUnchanged, targetVariantDiffPatched, removedSubtreeRoots, source,
302-
false);
345+
List<DiffNode<DiffLinesLabel>> removedSortedSubtreeRoots = removedSubtreeRoots.stream()
346+
.sorted((n1, n2) -> Integer.compare(n1.getLinesAtTime(Time.BEFORE).fromInclusive(),
347+
n2.getLinesAtTime(Time.BEFORE).fromInclusive()))
348+
.collect(Collectors.toList());
349+
applyChanges(DiffType.REM, targetVariantDiffUnchanged, targetVariantDiffPatched, removedSortedSubtreeRoots,
350+
source, false);
303351

304352
GameEngine.showAndAwaitAll(Show.tree(sourceVariantVersion1), Show.tree(sourceVariantVersion2),
305353
Show.tree(targetVariant), Show.diff(optimizedDiff), Show.diff(targetVariantDiffPatched),
@@ -322,10 +370,10 @@ private static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String f
322370

323371
public static void main(String[] args) {
324372
try {
325-
patchVariationTrees(parseVariationTreeFromFile("exampleA1Add.cpp"),
326-
parseVariationTreeFromFile("exampleA2Add.cpp"), parseVariationTreeFromFile("exampleBAdd.cpp"));
327-
// patchVariationTrees(parseVariationTreeFromFile("exampleA1Rem.cpp"),
328-
// parseVariationTreeFromFile("exampleA2Rem.cpp"), parseVariationTreeFromFile("exampleBRem.cpp"));
373+
// patchVariationTrees(parseVariationTreeFromFile("exampleA1Add.cpp"),
374+
// parseVariationTreeFromFile("exampleA2Add.cpp"), parseVariationTreeFromFile("exampleBAdd.cpp"));
375+
patchVariationTrees(parseVariationTreeFromFile("exampleA1Rem.cpp"),
376+
parseVariationTreeFromFile("exampleA2Rem.cpp"), parseVariationTreeFromFile("exampleBRem.cpp"));
329377
} catch (Exception e) {
330378
System.out.println("Rejected");
331379
e.printStackTrace();

0 commit comments

Comments
 (0)