-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSimpleRenderer.java
More file actions
174 lines (157 loc) · 8.28 KB
/
SimpleRenderer.java
File metadata and controls
174 lines (157 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package org.variantsync.diffdetective.internal;
import org.tinylog.Logger;
import org.variantsync.diffdetective.datasets.PatchDiffParseOptions;
import org.variantsync.diffdetective.datasets.Repository;
import org.variantsync.diffdetective.diff.git.PatchDiff;
import org.variantsync.diffdetective.diff.result.DiffParseException;
import org.variantsync.diffdetective.feature.cpp.CPPAnnotationParser;
import org.variantsync.diffdetective.mining.RWCompositePatternNodeFormat;
import org.variantsync.diffdetective.mining.RWCompositePatternTreeFormat;
import org.variantsync.diffdetective.mining.VariationDiffMiner;
import org.variantsync.diffdetective.util.Assert;
import org.variantsync.diffdetective.util.FileUtils;
import org.variantsync.diffdetective.variation.DiffLinesLabel;
import org.variantsync.diffdetective.variation.diff.VariationDiff;
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParser;
import org.variantsync.diffdetective.variation.diff.render.RenderOptions;
import org.variantsync.diffdetective.variation.diff.render.VariationDiffRenderer;
import org.variantsync.diffdetective.variation.diff.serialize.nodeformat.MappingsDiffNodeFormat;
import org.variantsync.diffdetective.variation.diff.transform.Transformer;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Function;
/**
* NOT INTENDED FOR API USE.
* Renderer that may be invoked directly on a directory containing diff
* and linegraph files. The renderer will render all valid files in the given
* directory.
* This class is mostly used for debuggin purposes within DiffDetective and
* contains mostly quick-and-dirty hardcoded configuration options.
*
* @author Paul Bittner
*/
public class SimpleRenderer {
private static final VariationDiffRenderer renderer = VariationDiffRenderer.WithinDiffDetective();
private static final RenderOptions<DiffLinesLabel> renderOptions = new RenderOptions.Builder<DiffLinesLabel>()
// .setNodeFormat(new ReleaseMiningDiffNodeFormat()),
.setNodeFormat(new MappingsDiffNodeFormat<>())
.setDpi(RenderOptions.DEFAULT().dpi() / 2)
.setNodesize(3 * RenderOptions.DEFAULT().nodesize())
.setEdgesize(2 * RenderOptions.DEFAULT().edgesize())
.setArrowsize(2 * RenderOptions.DEFAULT().arrowsize())
.setFontsize(8)
// .addExtraArguments("--format", "patternsrelease")
.setCleanUpTemporaryFiles(false)
.build();
private static final RenderOptions<DiffLinesLabel> vulkanRenderOptions = new RenderOptions.Builder<DiffLinesLabel>()
// .setNodeFormat(new ReleaseMiningDiffNodeFormat()),
.setNodeFormat(new MappingsDiffNodeFormat<>())
.setDpi(1500)
.setNodesize(3)
.setEdgesize(0.1)
.setArrowsize(2)
.setFontsize(2)
// .addExtraArguments("--format", "patternsrelease")
.setCleanUpTemporaryFiles(false)
.setWithlabels(false)
.build();
private static final RenderOptions<DiffLinesLabel> renderExampleOptions = new RenderOptions.Builder<DiffLinesLabel>()
.setTreeFormat(new RWCompositePatternTreeFormat())
.setNodesize(3 * RenderOptions.DEFAULT().nodesize())
.setEdgesize(2 * RenderOptions.DEFAULT().edgesize())
.setArrowsize(2 * RenderOptions.DEFAULT().arrowsize())
.setFontsize(8)
.addExtraArguments("--startlineno", "4201")
.build();
private static final RenderOptions<DiffLinesLabel> renderCompositePatterns = new RenderOptions.Builder<DiffLinesLabel>()
.setNodesize(3 * RenderOptions.DEFAULT().nodesize())
.setEdgesize(2 * RenderOptions.DEFAULT().edgesize())
.setArrowsize(2 * RenderOptions.DEFAULT().arrowsize())
.setFontsize(2 * RenderOptions.DEFAULT().fontsize())
.setTreeFormat(new RWCompositePatternTreeFormat())
.setNodeFormat(new RWCompositePatternNodeFormat())
.setCleanUpTemporaryFiles(true)
.addExtraArguments("--format", "patternsdebug")
.build();
private static final RenderOptions<DiffLinesLabel> RENDER_OPTIONS_TO_USE = renderExampleOptions;
private final static boolean collapseMultipleCodeLines = true;
private final static boolean ignoreEmptyLines = true;
private final static List<String> SUPPORTED_FILE_TYPES = List.of(".diff", ".c", ".cpp", ".h", ".hpp");
private final static Function<Path, Path> GetRelativeOutputDir =
// Path::getParent
p -> p.getParent().resolve("render");
private static void render(final Path fileToRender) {
if (FileUtils.isLineGraph(fileToRender)) {
Logger.info("Rendering {}", fileToRender);
renderer.renderFile(fileToRender, RENDER_OPTIONS_TO_USE);
} else if (SUPPORTED_FILE_TYPES.stream().anyMatch(extension -> FileUtils.hasExtension(fileToRender, extension))) {
Logger.info("Rendering {}", fileToRender);
final VariationDiff<DiffLinesLabel> t;
try {
t = VariationDiff.fromFile(fileToRender,
new VariationDiffParseOptions(
new CPPAnnotationParser(), collapseMultipleCodeLines, ignoreEmptyLines
));
} catch (IOException | DiffParseException e) {
Logger.error(e, "Could not read given file '{}'", fileToRender);
return;
}
renderer.renderWithTreeFormat(
t,
fileToRender.getFileName().toString(),
GetRelativeOutputDir.apply(fileToRender),
// renderExampleOptions
RENDER_OPTIONS_TO_USE
);
} else {
Logger.warn("Skipping unsupported file {}", fileToRender);
}
}
/**
* Expects one of the following argument configurations.
* 1.) For rendering files: Exactly one argument pointing to a file or directory to render.
* 2.) For rendering patches: Exactly three arguments.
* The first argument is the path to a local directory from which a patch should be analyzed.
* The second argument is a commit hash.
* The third argument is the file name of the patched file in the given commit.
*
* @param args See above
* @throws IOException when reading a file fails.
*/
public static void main(String[] args) throws IOException {
if (args.length < 1) {
System.err.println("Expected either a path to diff or lg file as argument, or a path to a git repository and a commit hash.");
return;
}
if (args.length == 1) {
final Path fileToRender = Path.of(args[0]);
if (!Files.exists(fileToRender)) {
Logger.error("Path {} does not exist!", fileToRender);
return;
}
if (Files.isDirectory(fileToRender)) {
Logger.info("Rendering directory {}", fileToRender);
FileUtils.listAllFilesRecursively(fileToRender).forEach(SimpleRenderer::render);
} else {
Logger.info("Rendering file {}", fileToRender);
render(fileToRender);
}
} else if (args.length == 3) {
final Path repoPath = Path.of(args[0]);
final String repoName = repoPath.getFileName().toString();
final String commit = args[1];
final String file = args[2];
final Repository repository = Repository.fromDirectory(repoPath, repoName);
repository.setParseOptions(repository.getParseOptions().withDiffStoragePolicy(PatchDiffParseOptions.DiffStoragePolicy.REMEMBER_STRIPPED_DIFF));
final List<Transformer<VariationDiff<DiffLinesLabel>>> transform = VariationDiffMiner.Postprocessing(repository);
final PatchDiff patch = VariationDiffParser.parsePatch(repository, file, commit);
Assert.assertNotNull(patch != null);
Transformer.apply(transform, patch.getVariationDiff());
renderer.render(patch, Path.of("render", repoName), RENDER_OPTIONS_TO_USE);
}
System.out.println("done");
}
}