Skip to content

Commit 9044457

Browse files
refactor: extract more common code to reduce redundancies
1 parent 1f91d72 commit 9044457

5 files changed

Lines changed: 153 additions & 206 deletions

File tree

src/main/java/org/variantsync/vevos/extraction/Config.java

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.variantsync.vevos.extraction;
2+
3+
public class ConfigProperties {
4+
public static final String PRINT_ENABLED
5+
= "extraction.print-enabled";
6+
public static final String GT_SAVE_DIR
7+
= "extraction.gt-save-dir";
8+
public static final String IGNORE_PC_CHANGES
9+
= "extraction.ignore-pc-changes";
10+
public static final String DATASET_FILE
11+
= "diff-detective.dataset-file";
12+
public static final String DD_OUTPUT_DIR
13+
= "diff-detective.output-dir";
14+
public static final String REPO_SAVE_DIR
15+
= "diff-detective.repo-storage-dir";
16+
public static final String NUM_THREADS
17+
= "diff-detective.num-threads";
18+
public static final String BATCH_SIZE
19+
= "diff-detective.batch-size";
20+
public static final String EXTRACT_CODE_MATCHING
21+
= "extraction.extract-code-matching";
22+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package org.variantsync.vevos.extraction;
2+
3+
import org.tinylog.Logger;
4+
import org.variantsync.diffdetective.AnalysisRunner;
5+
import org.variantsync.diffdetective.datasets.PatchDiffParseOptions;
6+
import org.variantsync.diffdetective.diff.git.DiffFilter;
7+
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
8+
import org.variantsync.vevos.extraction.gt.GroundTruth;
9+
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
import java.nio.file.Path;
14+
import java.util.Properties;
15+
16+
import static org.variantsync.vevos.extraction.ConfigProperties.*;
17+
18+
public class ExecutionUtilities {
19+
20+
/**
21+
* Loads the properties in the given file.
22+
*
23+
* @param propertiesFile The file to load
24+
* @return The loaded properties
25+
*/
26+
public static Properties getProperties(File propertiesFile) {
27+
Properties props = new Properties();
28+
try (FileInputStream input = new FileInputStream(propertiesFile)) {
29+
props.load(input);
30+
} catch (IOException e) {
31+
Logger.error("problem while loading properties");
32+
Logger.error(e);
33+
quitOnError();
34+
}
35+
return props;
36+
}
37+
38+
/**
39+
* Options for the execution of DiffDetective
40+
*
41+
* @param properties The properties loaded by main()
42+
* @return The options instance
43+
*/
44+
public static AnalysisRunner.Options diffdetectiveOptions(Properties properties) {
45+
46+
return new AnalysisRunner.Options(
47+
Path.of(properties.getProperty(REPO_SAVE_DIR)),
48+
Path.of(properties.getProperty(DD_OUTPUT_DIR)),
49+
Path.of(properties.getProperty(DATASET_FILE)),
50+
repo -> {
51+
final PatchDiffParseOptions repoDefault = repo.getParseOptions();
52+
return new PatchDiffParseOptions(
53+
PatchDiffParseOptions.DiffStoragePolicy.DO_NOT_REMEMBER,
54+
new VariationDiffParseOptions(
55+
repoDefault.variationDiffParseOptions().annotationParser(),
56+
false,
57+
false
58+
)
59+
);
60+
},
61+
repo -> new DiffFilter.Builder()
62+
.allowMerge(true)
63+
// TODO: make configurable
64+
.allowedFileExtensions("h", "hpp", "c", "cpp")
65+
.build(),
66+
true,
67+
false
68+
);
69+
}
70+
71+
/**
72+
* Throws an error if the host OS is Windows.
73+
*/
74+
public static void checkOS() {
75+
boolean isWindows = System.getProperty("os.name")
76+
.toLowerCase().startsWith("windows");
77+
Logger.info("OS NAME: " + System.getProperty("os.name"));
78+
if (isWindows) {
79+
Logger.error("Running the analysis under Windows is not supported as the Linux/BusyBox sources are not" +
80+
"checked out correctly.");
81+
quitOnError();
82+
}
83+
}
84+
85+
/**
86+
* Logs an error message and quits the extraction with an exception.
87+
*/
88+
public static void quitOnError() {
89+
Logger.error("An error occurred and the program has to quit.");
90+
throw new IllegalStateException("Not able to continue analysis due to previous error");
91+
}
92+
93+
/**
94+
* Parses the file in which the properties are located from the arguments.
95+
*
96+
* @param args the arguments to parse
97+
* @return the properties file
98+
*/
99+
public static File getPropertiesFile(String[] args) {
100+
File propertiesFile = null;
101+
if (args.length > 0) {
102+
propertiesFile = new File(args[0]);
103+
}
104+
105+
if (propertiesFile == null) {
106+
Logger.error("You must specify a .properties file as first argument");
107+
quitOnError();
108+
}
109+
110+
return propertiesFile;
111+
}
112+
113+
/**
114+
* Prints the given ground truth to console.
115+
*
116+
* @param groundTruth GT to print
117+
* @param commitName The id of the commit for which the GT has been calculated
118+
*/
119+
public static void print(GroundTruth groundTruth, String commitName) {
120+
System.out.println();
121+
System.out.printf("***************** %s ******************", commitName);
122+
System.out.println();
123+
for (String file : groundTruth.fileGTs().keySet()) {
124+
System.out.println(groundTruth.get(file));
125+
}
126+
}
127+
}

src/main/java/org/variantsync/vevos/extraction/FastGroundTruthExtraction.java

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
import org.variantsync.diffdetective.datasets.Repository;
77
import org.variantsync.vevos.extraction.analysis.FastVariabilityAnalysis;
88

9-
import java.io.File;
10-
import java.io.FileInputStream;
119
import java.io.IOException;
1210
import java.nio.file.Path;
1311
import java.util.List;
1412
import java.util.Properties;
1513
import java.util.function.BiConsumer;
1614
import java.util.function.BiFunction;
1715

18-
import static org.variantsync.vevos.extraction.Config.*;
16+
import static org.variantsync.vevos.extraction.ConfigProperties.*;
17+
import static org.variantsync.vevos.extraction.ExecutionUtilities.*;
1918

2019
public class FastGroundTruthExtraction {
2120
private final Properties properties;
@@ -31,7 +30,6 @@ public FastGroundTruthExtraction(Properties properties) {
3130
* @throws IOException When copying the log file fails.
3231
*/
3332
public static void main(String[] args) throws IOException {
34-
3533
checkOS();
3634

3735
// Load the configuration
@@ -43,68 +41,6 @@ public static void main(String[] args) throws IOException {
4341
extraction.run(options);
4442
}
4543

46-
47-
48-
/**
49-
* Parses the file in which the properties are located from the arguments.
50-
*
51-
* @param args the arguments to parse
52-
* @return the properties file
53-
*/
54-
private static File getPropertiesFile(String[] args) {
55-
File propertiesFile = null;
56-
if (args.length > 0) {
57-
propertiesFile = new File(args[0]);
58-
}
59-
60-
if (propertiesFile == null) {
61-
Logger.error("You must specify a .properties file as first argument");
62-
quitOnError();
63-
}
64-
65-
return propertiesFile;
66-
}
67-
68-
/**
69-
* Loads the properties in the given file.
70-
*
71-
* @param propertiesFile The file to load
72-
* @return The loaded properties
73-
*/
74-
private static Properties getProperties(File propertiesFile) {
75-
Properties props = new Properties();
76-
try (FileInputStream input = new FileInputStream(propertiesFile)) {
77-
props.load(input);
78-
} catch (IOException e) {
79-
Logger.error("problem while loading properties");
80-
Logger.error(e);
81-
quitOnError();
82-
}
83-
return props;
84-
}
85-
86-
/**
87-
* Throws an error if the host OS is Windows.
88-
*/
89-
private static void checkOS() {
90-
boolean isWindows = System.getProperty("os.name")
91-
.toLowerCase().startsWith("windows");
92-
Logger.info("OS NAME: " + System.getProperty("os.name"));
93-
if (isWindows) {
94-
Logger.error("Running the analysis under Windows is not supported as the Linux/BusyBox sources are not" +
95-
"checked out correctly.");
96-
quitOnError();
97-
}
98-
}
99-
100-
/**
101-
* Logs an error message and quits the extraction with an exception.
102-
*/
103-
public static void quitOnError() {
104-
Logger.error("An error occurred and the program has to quit.");
105-
throw new IllegalStateException("Not able to continue analysis due to previous error");
106-
}
107-
10844
private BiConsumer<Repository, Path> buildRunner() {
10945
return (repo, repoOutputDir) -> {
11046
Path extractionDir = Path.of(this.properties.getProperty(GT_SAVE_DIR));

0 commit comments

Comments
 (0)