Skip to content

Commit 5ca6564

Browse files
committed
refactor: improve the source tracing mechanism
The new mechanism is more flexible and provides more introspection capabilities. Furthermore, it provides more documentation about the intent of our source tracking.
1 parent 047df57 commit 5ca6564

41 files changed

Lines changed: 593 additions & 275 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/org/variantsync/diffdetective/diff/git/GitPatch.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package org.variantsync.diffdetective.diff.git;
22

3+
import java.util.List;
4+
35
import org.eclipse.jgit.diff.DiffEntry;
46
import org.variantsync.diffdetective.diff.text.TextBasedDiff;
7+
import org.variantsync.diffdetective.util.Source;
58
import org.variantsync.diffdetective.variation.diff.Time;
69
import org.variantsync.diffdetective.variation.diff.VariationDiff; // For Javadoc
7-
import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource;
810

911
/**
1012
* Interface for patches from a git repository.
1113
* A git patch is a {@link TextBasedDiff} from which {@link VariationDiff}s can be created.
1214
*
1315
*/
14-
public interface GitPatch extends VariationDiffSource, TextBasedDiff {
16+
public interface GitPatch extends Source, TextBasedDiff {
1517
/**
1618
* Minimal default implementation of {@link GitPatch}
1719
* @param getDiff The diff in text form.
@@ -41,6 +43,16 @@ public GitPatch shallowClone() {
4143
public String toString() {
4244
return oldFileName + "@ " + getParentCommitHash + " (parent) to " + newFileName + " @ " + getCommitHash + " (child)";
4345
}
46+
47+
@Override
48+
public String getSourceExplanation() {
49+
return "SimpleGitPatch";
50+
}
51+
52+
@Override
53+
public List<Object> getSourceArguments() {
54+
return List.of(getChangeType(), oldFileName(), newFileName(), getCommitHash(), getParentCommitHash());
55+
}
4456
}
4557

4658
/**

src/main/java/org/variantsync/diffdetective/diff/git/PatchDiff.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,9 @@ public String toString() {
123123
public GitPatch shallowClone() {
124124
return new GitPatch.SimpleGitPatch(getDiff(), getChangeType(), getFileName(Time.BEFORE), getFileName(Time.AFTER), getCommitHash(), getParentCommitHash());
125125
}
126+
127+
@Override
128+
public String getSourceExplanation() {
129+
return "PatchDiff";
130+
}
126131
}

src/main/java/org/variantsync/diffdetective/examplesearch/ExampleFinder.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.variantsync.diffdetective.show.Show;
1212
import org.variantsync.diffdetective.util.Assert;
1313
import org.variantsync.diffdetective.util.IO;
14+
import org.variantsync.diffdetective.util.Source;
1415
import org.variantsync.diffdetective.util.StringUtils;
1516
import org.variantsync.diffdetective.variation.DiffLinesLabel;
1617
import org.variantsync.diffdetective.variation.diff.Time;
@@ -24,7 +25,6 @@
2425
import org.variantsync.diffdetective.variation.diff.serialize.edgeformat.DefaultEdgeLabelFormat;
2526
import org.variantsync.diffdetective.variation.diff.serialize.nodeformat.MappingsDiffNodeFormat;
2627
import org.variantsync.diffdetective.variation.diff.serialize.treeformat.CommitDiffVariationDiffLabelFormat;
27-
import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource;
2828

2929
import java.io.IOException;
3030
import java.nio.file.Path;
@@ -110,8 +110,8 @@ private boolean checkIfExample(Analysis analysis, String localDiff) {
110110
// Not every local diff can be parsed to a VariationDiff because diffs are unaware of the underlying language (i.e., CPP).
111111
// We want only running examples whose diffs describe entire diff trees for easier understanding.
112112
if (isGoodExample.test(localTree)) {
113-
Assert.assertTrue(variationDiff.getSource() instanceof GitPatch);
114-
final GitPatch variationDiffSource = (GitPatch) variationDiff.getSource();
113+
GitPatch variationDiffSource = Source.findFirst(variationDiff, GitPatch.class);
114+
Assert.assertNotNull(variationDiffSource);
115115
localTree.setSource(variationDiffSource.shallowClone());
116116
} else {
117117
return false;
@@ -149,9 +149,9 @@ public boolean analyzeVariationDiff(Analysis analysis) {
149149
}
150150

151151
private void exportExample(final Analysis analysis, final String tdiff, final VariationDiff<DiffLinesLabel> vdiff, Path outputDir) {
152-
Assert.assertTrue(vdiff.getSource() instanceof GitPatch);
153152
final Repository repo = analysis.getRepository();
154-
final GitPatch patch = (GitPatch) vdiff.getSource();
153+
final GitPatch patch = Source.findFirst(vdiff, GitPatch.class);
154+
Assert.assertNotNull(patch);
155155
outputDir = outputDir.resolve(Path.of(repo.getRepositoryName() + "_" + patch.getCommitHash()));
156156
final String filename = patch.getFileName(Time.AFTER);
157157

@@ -185,8 +185,8 @@ private void exportExample(final Analysis analysis, final String tdiff, final Va
185185
}
186186

187187
static String getDiff(final VariationDiff<?> tree) {
188-
final VariationDiffSource source = tree.getSource();
189-
Assert.assertTrue(source instanceof TextBasedDiff);
190-
return ((TextBasedDiff) source).getDiff();
188+
TextBasedDiff textBasedDiff = Source.findFirst(tree, TextBasedDiff.class);
189+
Assert.assertNotNull(textBasedDiff);
190+
return textBasedDiff.getDiff();
191191
}
192192
}

src/main/java/org/variantsync/diffdetective/experiments/thesis_es/UnparseAnalysis.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.variantsync.diffdetective.util.CSV;
1010
import org.variantsync.diffdetective.util.FileUtils;
1111
import org.variantsync.diffdetective.util.IO;
12+
import org.variantsync.diffdetective.util.Source;
1213
import org.variantsync.diffdetective.util.StringUtils;
1314
import org.variantsync.diffdetective.variation.DiffLinesLabel;
1415
import org.variantsync.diffdetective.variation.VariationUnparser;
@@ -17,7 +18,6 @@
1718
import org.variantsync.diffdetective.variation.diff.construction.JGitDiff;
1819
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
1920
import org.variantsync.diffdetective.variation.tree.VariationTree;
20-
import org.variantsync.diffdetective.variation.tree.source.VariationTreeSource;
2121

2222
public class UnparseAnalysis implements Analysis.Hooks {
2323

@@ -215,7 +215,7 @@ public static String removeWhitespace(String string, boolean diff) {
215215
public static String parseUnparseTree(String text, VariationDiffParseOptions option) {
216216
String temp = "b";
217217
try {
218-
VariationTree<DiffLinesLabel> tree = VariationTree.fromText(text, VariationTreeSource.Unknown, option);
218+
VariationTree<DiffLinesLabel> tree = VariationTree.fromText(text, Source.Unknown, option);
219219
temp = VariationUnparser.unparseTree(tree);
220220
} catch (Exception e) {
221221
e.printStackTrace();

src/main/java/org/variantsync/diffdetective/experiments/views/result/ViewEvaluation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ public String toCSV(String delimiter) {
8585
// repo.getRepositoryName(),
8686
commit,
8787
file,
88-
relevance.getFunctionName(),
89-
// getQueryArguments(),
88+
relevance.getSourceExplanation(),
89+
// relevance.getSourceArguments(),
9090
msNaive,
9191
msOptimized,
9292
diffStatistics.nodeCount,
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package org.variantsync.diffdetective.mining;
22

3-
import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource;
3+
import java.util.List;
4+
5+
import org.variantsync.diffdetective.util.FileSource;
6+
import org.variantsync.diffdetective.util.Source;
47
import org.variantsync.diffdetective.variation.diff.serialize.treeformat.VariationDiffLabelFormat;
5-
import org.variantsync.diffdetective.variation.diff.source.PatchFile;
68

79
public class RWCompositePatternTreeFormat implements VariationDiffLabelFormat {
810
@Override
9-
public VariationDiffSource fromLabel(String label) {
11+
public Source fromLabel(String label) {
1012
throw new UnsupportedOperationException("Cannot read");
1113
}
1214

1315
@Override
14-
public String toLabel(VariationDiffSource variationDiffSource) {
15-
if (variationDiffSource instanceof PatchFile p) {
16-
final String fileName = p.path().getFileName().toString();
17-
return fileName.substring(0, fileName.indexOf('.')).replaceAll("_", " ");
16+
public String toLabel(Source variationDiffSource) {
17+
List<FileSource> pathSources = Source.findAll(variationDiffSource, FileSource.class);
18+
if (pathSources.size() != 1) {
19+
throw new IllegalArgumentException("Expected a single path source but got:\n" + Source.fullExplanation(variationDiffSource));
1820
}
1921

20-
throw new IllegalArgumentException("Expected a PatchFile but got " + variationDiffSource);
22+
final String fileName = pathSources.get(0).getPath().getFileName().toString();
23+
return fileName.substring(0, fileName.indexOf('.')).replaceAll("_", " ");
2124
}
2225
}

src/main/java/org/variantsync/diffdetective/show/Show.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.variantsync.diffdetective.show.engine.GameEngine;
77
import org.variantsync.diffdetective.show.engine.geom.Vec2;
88
import org.variantsync.diffdetective.show.variation.VariationDiffApp;
9+
import org.variantsync.diffdetective.util.Source;
910
import org.variantsync.diffdetective.variation.Label;
1011
import org.variantsync.diffdetective.variation.diff.DiffNode;
1112
import org.variantsync.diffdetective.variation.diff.VariationDiff;
@@ -30,7 +31,7 @@ public static GameEngine diff(final VariationDiff<?> d, final String title) {
3031
}
3132

3233
public static GameEngine diff(final VariationDiff<?> d) {
33-
return diff(d, d.getSource().toString());
34+
return diff(d, Source.shortExplanation(d));
3435
}
3536

3637
public static <L extends Label> GameEngine tree(final VariationTree<L> t, final String title, List<DiffNodeLabelFormat<L>> availableFormats) {
@@ -47,7 +48,7 @@ public static GameEngine tree(final VariationTree<?> t, final String title) {
4748
}
4849

4950
public static GameEngine tree(final VariationTree<?> t) {
50-
return tree(t, t.source().toString());
51+
return tree(t, Source.shortExplanation(t));
5152
}
5253

5354
public static <L extends Label> GameEngine baddiff(final BadVDiff<L> badVDiff, final String title, List<DiffNodeLabelFormat<L>> availableFormats) {
@@ -80,6 +81,6 @@ public static GameEngine baddiff(final BadVDiff<?> badVDiff, final String title)
8081
}
8182

8283
public static GameEngine baddiff(final BadVDiff<?> badVDiff) {
83-
return baddiff(badVDiff, badVDiff.diff().source().toString());
84+
return baddiff(badVDiff, Source.shortExplanation(badVDiff.diff()));
8485
}
8586
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.variantsync.diffdetective.util;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* Represents a {@link Source} without arguments.
8+
*/
9+
public class CompositeSource implements Source {
10+
private final String sourceExplanation;
11+
private final List<Source> sources;
12+
13+
/**
14+
* @param sourceExplanation is returned verbatim by {@link getSourceExplanation}
15+
* @param sources is returned as immutable list by {@link getSources}
16+
*/
17+
public CompositeSource(String sourceExplanation, Source... sources) {
18+
this.sourceExplanation = sourceExplanation;
19+
this.sources = Arrays.asList(sources);
20+
}
21+
22+
@Override
23+
public String getSourceExplanation() {
24+
return sourceExplanation;
25+
}
26+
27+
@Override
28+
public List<Source> getSources() {
29+
return sources;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return Source.shallowExplanation(this);
35+
}
36+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.variantsync.diffdetective.util;
2+
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
6+
/**
7+
* Represents a file input in a {@link Source} hierarchy.
8+
*/
9+
public record FileSource(Path getPath) implements Source {
10+
@Override
11+
public String getSourceExplanation() {
12+
return "File";
13+
}
14+
15+
@Override
16+
public List<Object> getSourceArguments() {
17+
return List.of(getPath());
18+
}
19+
}

0 commit comments

Comments
 (0)