Skip to content

Commit 784e24c

Browse files
committed
refactored Patching class
1 parent a7b2208 commit 784e24c

4 files changed

Lines changed: 297 additions & 156 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
339339
gameEngine.add(Show.tree(patchTransformerResult.getSuccess(), "patch transformer result"));
340340
}
341341
isPatchTransformerCorrect = patchTransformerResult.match(tree -> {
342-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
342+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
343343
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
344344
scenario.sourceVariantConfig, scenario.unchangedAfter);
345345
return equiv.first() && equiv.second();
@@ -352,7 +352,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
352352
gameEngine.add(Show.tree(gnuPatchResult.getSuccess(), "gnu patch result"));
353353
}
354354
isGnuPatchCorrect = gnuPatchResult.match(tree -> {
355-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
355+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
356356
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
357357
scenario.sourceVariantConfig, scenario.unchangedAfter);
358358
return equiv.first() && equiv.second();
@@ -368,7 +368,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
368368
gameEngine.add(Show.tree(mpatchResult.getSuccess(), "mpatch result"));
369369
}
370370
isMpatchCorrect = mpatchResult.match(tree -> {
371-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
371+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
372372
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
373373
scenario.sourceVariantConfig, scenario.unchangedAfter);
374374
return equiv.first() && equiv.second();
@@ -386,7 +386,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
386386
gameEngine.add(Show.tree(mpatchResult2.getSuccess(), "mpatch result"));
387387
}
388388
isMpatchCorrect2 = mpatchResult2.match(tree -> {
389-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
389+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
390390
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
391391
scenario.sourceVariantConfig, scenario.unchangedAfter);
392392
return equiv.first() && equiv.second();
@@ -402,7 +402,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
402402
gameEngine.add(Show.tree(gnuPatchResult2.getSuccess(), "gnu patch result"));
403403
}
404404
isGnuPatchCorrect2 = gnuPatchResult2.match(tree -> {
405-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
405+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
406406
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
407407
scenario.sourceVariantConfig, scenario.unchangedAfter);
408408
return equiv.first() && equiv.second();

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import org.variantsync.diffdetective.AnalysisRunner;
1010
import org.variantsync.diffdetective.variation.DiffLinesLabel;
11-
import org.variantsync.diffdetective.variation.diff.patching.Patching;
1211
import org.variantsync.diffdetective.variation.diff.transform.CutNonEditedSubtrees;
1312
import org.variantsync.diffdetective.variation.tree.VariationTree;
1413
import org.variantsync.diffdetective.variation.tree.view.TreeView;
@@ -383,7 +382,7 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
383382

384383
patchTransformerResult.match(tree -> {
385384
if (tree != null) {
386-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
385+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
387386
scenario.sourceVariantAfterRedToCrossVarFeatures, scenario.targetVariantBeforeRedToUnchanged,
388387
scenario.sourceVariantConfig, scenario.unchangedAfter);
389388
if (!equiv.first()) {
@@ -413,7 +412,7 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
413412
gnuPatchResult = Generator.runGnuPatch(scenario.targetVariantBefore, PATCH, CODE, commitHash);
414413
gnuPatchResult.match(tree -> {
415414
if (tree != null) {
416-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
415+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
417416
scenario.sourceVariantAfterRedToCrossVarFeatures,
418417
scenario.targetVariantBeforeRedToUnchanged, scenario.sourceVariantConfig,
419418
scenario.unchangedAfter);
@@ -441,7 +440,7 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
441440
mpatchResult = Generator.runMPatch(scenario.targetVariantBefore, PATCH, CODE, commitHash);
442441
mpatchResult.match(tree -> {
443442
if (tree != null) {
444-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
443+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
445444
scenario.sourceVariantAfterRedToCrossVarFeatures,
446445
scenario.targetVariantBeforeRedToUnchanged, scenario.sourceVariantConfig,
447446
scenario.unchangedAfter);
@@ -471,7 +470,7 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
471470
gnuPatchResultView = Generator.runGnuPatch(scenario.targetVariantBefore, PATCH, CODE, commitHash);
472471
gnuPatchResultView.match(tree -> {
473472
if (tree != null) {
474-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
473+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
475474
scenario.sourceVariantAfterRedToCrossVarFeatures,
476475
scenario.targetVariantBeforeRedToUnchanged, scenario.sourceVariantConfig,
477476
scenario.unchangedAfter);
@@ -499,7 +498,7 @@ public boolean analyzeVariationDiff(Analysis analysis) throws Exception {
499498
mpatchResultView = Generator.runMPatch(scenario.targetVariantBefore, PATCH, CODE, commitHash);
500499
mpatchResultView.match(tree -> {
501500
if (tree != null) {
502-
Pair<Boolean, Boolean> equiv = Patching.arePatchedVariantsEquivalent(tree,
501+
Pair<Boolean, Boolean> equiv = Utils.arePatchedVariantsEquivalent(tree,
503502
scenario.sourceVariantAfterRedToCrossVarFeatures,
504503
scenario.targetVariantBeforeRedToUnchanged, scenario.sourceVariantConfig,
505504
scenario.unchangedAfter);
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package org.variantsync.diffdetective.experiments.thesis_pm;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.util.HashSet;
6+
import java.util.Iterator;
7+
import java.util.Set;
8+
9+
import org.eclipse.jgit.diff.DiffAlgorithm;
10+
import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
11+
import org.variantsync.diffdetective.diff.result.DiffParseException;
12+
import org.variantsync.diffdetective.variation.DiffLinesLabel;
13+
import org.variantsync.diffdetective.variation.Label;
14+
import org.variantsync.diffdetective.variation.diff.DiffNode;
15+
import org.variantsync.diffdetective.variation.diff.VariationDiff;
16+
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
17+
import org.variantsync.diffdetective.variation.tree.VariationTree;
18+
import org.variantsync.diffdetective.variation.tree.view.TreeView;
19+
import org.variantsync.diffdetective.variation.tree.view.relevance.ConfigureWithFullConfig;
20+
import org.variantsync.diffdetective.variation.tree.view.relevance.Unchanged;
21+
import org.variantsync.functjonal.Pair;
22+
23+
public class Utils {
24+
25+
public static boolean comparePatchedVariantWithExpectedResult(VariationTree<DiffLinesLabel> patchedVariant,
26+
VariationTree<DiffLinesLabel> expectedResult) {
27+
return Utils.isSameAs(patchedVariant.toCompletelyUnchangedVariationDiff(),
28+
expectedResult.toCompletelyUnchangedVariationDiff());
29+
}
30+
31+
public static Pair<Boolean, Boolean> arePatchedVariantsEquivalent(
32+
VariationTree<DiffLinesLabel> patchedTargetVariant,
33+
VariationTree<DiffLinesLabel> sourceVariantAfterRedToCrossVarFeatures,
34+
VariationTree<DiffLinesLabel> targetVariantBeforeRedToUnchanged, ConfigureWithFullConfig configSourceVariant,
35+
Unchanged unchangedAfter) {
36+
37+
VariationTree<DiffLinesLabel> targetVariantAfterRedToCrossVarFeatures = TreeView.tree(patchedTargetVariant,
38+
configSourceVariant);
39+
40+
VariationTree<DiffLinesLabel> patchedTargetVariantRedToUnchanged = TreeView.tree(patchedTargetVariant,
41+
unchangedAfter);
42+
43+
// GameEngine.showAndAwaitAll(Show.tree(patchedTargetVariant, "patched target variant"),
44+
// Show.tree(patchedTargetVariantRedToUnchanged, "patched target variant red. to unchanged"),
45+
// Show.tree(sourceVariantAfterRedToCrossVarFeatures,
46+
// "patched source variant red. to cross variant features"),
47+
// Show.tree(targetVariantAfterRedToCrossVarFeatures,
48+
// "patched target variant red. to cross variant features"),
49+
// Show.tree(targetVariantBeforeRedToUnchanged, "target variant before red. to unchanged"));
50+
51+
return new Pair<Boolean, Boolean>(
52+
sourceVariantAfterRedToCrossVarFeatures.unparse().equals(targetVariantAfterRedToCrossVarFeatures.unparse()),
53+
patchedTargetVariantRedToUnchanged.unparse().equals(targetVariantBeforeRedToUnchanged.unparse()));
54+
55+
}
56+
57+
public static VariationDiff<DiffLinesLabel> parseVariationDiffFromFiles(String file1, String file2)
58+
throws IOException, DiffParseException {
59+
Path examplesDir = Path.of("data", "examples");
60+
return VariationDiff.fromFiles(examplesDir.resolve(file1), examplesDir.resolve(file2),
61+
DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default);
62+
}
63+
64+
public static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String file) {
65+
Path examplesDir = Path.of("data", "examples");
66+
Path path = examplesDir.resolve(file);
67+
try {
68+
VariationTree<DiffLinesLabel> tree = VariationTree.fromFile(path, VariationDiffParseOptions.Default);
69+
return tree;
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
} catch (DiffParseException e) {
73+
e.printStackTrace();
74+
}
75+
return null;
76+
}
77+
78+
public static VariationDiff<DiffLinesLabel> parseVariationDiffFromFile(String file)
79+
throws IOException, DiffParseException {
80+
Path examplesDir = Path.of("data", "examples");
81+
return VariationDiff.fromFile(examplesDir.resolve(file), VariationDiffParseOptions.Default);
82+
}
83+
84+
public static <L extends Label> boolean isSameAs(VariationDiff<L> diff1, VariationDiff<L> diff2) {
85+
return Utils.isSameAs(diff1.getRoot(), diff2.getRoot());
86+
}
87+
88+
public static <L extends Label> boolean isSameAs(DiffNode<L> a, DiffNode<L> b) {
89+
return Utils.isSameAs(a, b, new HashSet<>());
90+
}
91+
92+
public static <L extends Label> boolean isSameAs(DiffNode<L> a, DiffNode<L> b, Set<DiffNode<L>> visited) {
93+
if (!visited.add(a)) {
94+
return true;
95+
}
96+
97+
if (!(a.getNodeType().equals(b.getNodeType()) && hasSameLabel(a.getLabel(), b.getLabel())
98+
&& (a.getFormula() == null ? b.getFormula() == null : a.getFormula().equals(b.getFormula())))) {
99+
return false;
100+
}
101+
102+
Iterator<DiffNode<L>> aIt = a.getAllChildren().iterator();
103+
Iterator<DiffNode<L>> bIt = b.getAllChildren().iterator();
104+
while (aIt.hasNext() && bIt.hasNext()) {
105+
if (!isSameAs(aIt.next(), bIt.next(), visited)) {
106+
return false;
107+
}
108+
}
109+
110+
return aIt.hasNext() == bIt.hasNext();
111+
}
112+
113+
public static <L extends Label> boolean hasSameLabel(L a, L b) {
114+
String labelA = a.toString().replaceAll(" ", "");
115+
String labelB = b.toString().replaceAll(" ", "");
116+
return labelA.equals(labelB);
117+
}
118+
119+
}

0 commit comments

Comments
 (0)