Skip to content

Commit 74d0a17

Browse files
refactor: unite core extraction functionality
1 parent 9044457 commit 74d0a17

3 files changed

Lines changed: 85 additions & 74 deletions

File tree

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,26 @@
11
package org.variantsync.vevos.extraction;
22

33
import org.tinylog.Logger;
4-
import org.variantsync.diffdetective.AnalysisRunner;
54
import org.variantsync.diffdetective.analysis.Analysis;
65
import org.variantsync.diffdetective.datasets.Repository;
76
import org.variantsync.vevos.extraction.analysis.FastVariabilityAnalysis;
87

9-
import java.io.IOException;
108
import java.nio.file.Path;
119
import java.util.List;
1210
import java.util.Properties;
1311
import java.util.function.BiConsumer;
1412
import java.util.function.BiFunction;
1513

1614
import static org.variantsync.vevos.extraction.ConfigProperties.*;
17-
import static org.variantsync.vevos.extraction.ExecutionUtilities.*;
1815

19-
public class FastGroundTruthExtraction {
20-
private final Properties properties;
16+
public class FastGroundTruthExtraction extends GroundTruthExtraction {
2117

2218
public FastGroundTruthExtraction(Properties properties) {
23-
this.properties = properties;
19+
super(properties);
20+
Logger.info("Starting a fast ground truth extraction that only extracts a ground truth for the changed files of each commit.");
2421
}
2522

26-
/**
27-
* Main method to start the extraction.
28-
*
29-
* @param args Command-line options.
30-
* @throws IOException When copying the log file fails.
31-
*/
32-
public static void main(String[] args) throws IOException {
33-
checkOS();
34-
35-
// Load the configuration
36-
Properties properties = getProperties(getPropertiesFile(args));
37-
var extraction = new FastGroundTruthExtraction(properties);
38-
39-
var options = diffdetectiveOptions(properties);
40-
Logger.info("Starting SPL history analysis.");
41-
extraction.run(options);
42-
}
43-
44-
private BiConsumer<Repository, Path> buildRunner() {
23+
protected BiConsumer<Repository, Path> extractionRunner() {
4524
return (repo, repoOutputDir) -> {
4625
Path extractionDir = Path.of(this.properties.getProperty(GT_SAVE_DIR));
4726
Path resultsRoot = extractionDir.resolve(repo.getRepositoryName());
@@ -78,15 +57,4 @@ private BiConsumer<Repository, Path> buildRunner() {
7857
FastVariabilityAnalysis.numProcessed = 0;
7958
};
8059
}
81-
82-
/**
83-
* Starts the extraction.
84-
*
85-
* @param options The options for DiffDetective
86-
* @throws IOException If an IO error occurs in DiffDetective
87-
*/
88-
public void run(AnalysisRunner.Options options) throws IOException {
89-
AnalysisRunner.run(options, buildRunner());
90-
}
91-
9260
}

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

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.eclipse.jgit.api.errors.GitAPIException;
55
import org.eclipse.jgit.revwalk.RevCommit;
66
import org.tinylog.Logger;
7-
import org.variantsync.diffdetective.AnalysisRunner;
87
import org.variantsync.diffdetective.analysis.Analysis;
98
import org.variantsync.diffdetective.datasets.Repository;
109
import org.variantsync.vevos.extraction.analysis.FullVariabilityAnalysis;
@@ -22,40 +21,22 @@
2221
import java.util.function.BiConsumer;
2322
import java.util.function.BiFunction;
2423

25-
import static org.variantsync.vevos.extraction.gt.GroundTruth.*;
26-
import static org.variantsync.vevos.extraction.ExecutionUtilities.*;
2724
import static org.variantsync.vevos.extraction.ConfigProperties.*;
25+
import static org.variantsync.vevos.extraction.gt.GroundTruth.*;
2826

2927

30-
public class FullGroundTruthExtraction {
31-
private final Properties properties;
28+
public class FullGroundTruthExtraction extends GroundTruthExtraction {
3229

3330
public FullGroundTruthExtraction(Properties properties) {
34-
this.properties = properties;
35-
}
36-
37-
/**
38-
* Main method to start the extraction.
39-
*
40-
* @param args Command-line options.
41-
* @throws IOException When copying the log file fails.
42-
*/
43-
public static void main(String[] args) throws IOException {
44-
checkOS();
45-
46-
// Load the configuration
47-
Properties properties = getProperties(getPropertiesFile(args));
48-
var extraction = new FullGroundTruthExtraction(properties);
49-
50-
var options = diffdetectiveOptions(properties);
51-
Logger.info("Starting SPL history analysis.");
52-
extraction.run(options);
31+
super(properties);
32+
Logger.info("Starting full ground truth extraction that extracts a ground truth for all files of each commit.");
5333
}
5434

55-
56-
private BiConsumer<Repository, Path> buildRunner(String diffDetectiveCache) {
35+
protected BiConsumer<Repository, Path> extractionRunner() {
5736
return (repo, repoOutputDir) -> {
58-
FullVariabilityAnalysis analysis = new FullVariabilityAnalysis(Path.of(diffDetectiveCache), Boolean.parseBoolean(properties.getProperty(IGNORE_PC_CHANGES)));
37+
FullVariabilityAnalysis analysis =
38+
new FullVariabilityAnalysis(Path.of(properties.getProperty(DD_OUTPUT_DIR)),
39+
Boolean.parseBoolean(properties.getProperty(IGNORE_PC_CHANGES)));
5940
final BiFunction<Repository, Path, Analysis> AnalysisFactory = (r, out) -> new Analysis(
6041
"PCAnalysis",
6142
List.of(
@@ -92,15 +73,6 @@ private BiConsumer<Repository, Path> buildRunner(String diffDetectiveCache) {
9273
};
9374
}
9475

95-
/**
96-
* Starts the extraction.
97-
*
98-
* @param options The options for DiffDetective
99-
* @throws IOException If an IO error occurs in DiffDetective
100-
*/
101-
public void run(AnalysisRunner.Options options) throws IOException {
102-
AnalysisRunner.run(options, buildRunner(properties.getProperty(DD_OUTPUT_DIR)));
103-
}
10476

10577
/**
10678
* Incrementally combines the ground truths from the first to the last commit. The ground truth for unmodified files

src/main/java/org/variantsync/vevos/extraction/ExecutionUtilities.java renamed to src/main/java/org/variantsync/vevos/extraction/GroundTruthExtraction.java

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,79 @@
33
import org.tinylog.Logger;
44
import org.variantsync.diffdetective.AnalysisRunner;
55
import org.variantsync.diffdetective.datasets.PatchDiffParseOptions;
6+
import org.variantsync.diffdetective.datasets.Repository;
67
import org.variantsync.diffdetective.diff.git.DiffFilter;
78
import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions;
89
import org.variantsync.vevos.extraction.gt.GroundTruth;
910

1011
import java.io.File;
1112
import java.io.FileInputStream;
1213
import java.io.IOException;
14+
import java.lang.reflect.Constructor;
15+
import java.lang.reflect.InvocationTargetException;
1316
import java.nio.file.Path;
1417
import java.util.Properties;
18+
import java.util.function.BiConsumer;
1519

1620
import static org.variantsync.vevos.extraction.ConfigProperties.*;
1721

18-
public class ExecutionUtilities {
22+
public abstract class GroundTruthExtraction {
23+
protected final Properties properties;
24+
25+
protected GroundTruthExtraction(Properties properties) {
26+
this.properties = properties;
27+
}
28+
29+
/**
30+
* Main method to start the extraction.
31+
*
32+
* @param args Command-line options.
33+
* @throws IOException When copying the log file fails.
34+
*/
35+
public static void main(String[] args) throws IOException {
36+
checkOS();
37+
38+
// Load the configuration
39+
Properties properties = getProperties(getPropertiesFile(args));
40+
// TODO: load dynamically
41+
Class<?> extractionClass;
42+
try {
43+
extractionClass = determineExtractionClass(args);
44+
} catch (ClassNotFoundException e) {
45+
Logger.error("The class " + args[1] + " provided as program argument was not found.");
46+
throw new RuntimeException(e);
47+
}
48+
GroundTruthExtraction extraction;
49+
try {
50+
extraction = initializeExtraction(extractionClass, properties);
51+
} catch (NoSuchMethodException e) {
52+
throw new RuntimeException("The required constructor does not exist for the specified class " + args[1]);
53+
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) {
54+
Logger.error("Was not able to instantiate extraction class with the propterties " + args[0]
55+
+ " and the class name " + args[1]);
56+
throw new RuntimeException(e);
57+
}
58+
59+
var options = diffdetectiveOptions(properties);
60+
Logger.info("Starting SPL history analysis.");
61+
extraction.run(options);
62+
}
63+
64+
private static Class<?> determineExtractionClass(String... args) throws ClassNotFoundException {
65+
if (args.length > 1) {
66+
return Class.forName(args[1]);
67+
} else {
68+
Logger.error("The second program argument must specify a valid GroundTruthExtraction class.");
69+
throw new IllegalArgumentException("The second program argument must specify a valid GroundTruthExtraction class.");
70+
}
71+
}
72+
73+
private static GroundTruthExtraction initializeExtraction(Class<?> extractionClass, Properties properties)
74+
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
75+
Constructor<?> constructor = extractionClass.getDeclaredConstructor(Properties.class);
76+
constructor.setAccessible(true); // If the constructor is not public
77+
return (GroundTruthExtraction) constructor.newInstance(properties);
78+
}
1979

2080
/**
2181
* Loads the properties in the given file.
@@ -74,7 +134,6 @@ public static AnalysisRunner.Options diffdetectiveOptions(Properties properties)
74134
public static void checkOS() {
75135
boolean isWindows = System.getProperty("os.name")
76136
.toLowerCase().startsWith("windows");
77-
Logger.info("OS NAME: " + System.getProperty("os.name"));
78137
if (isWindows) {
79138
Logger.error("Running the analysis under Windows is not supported as the Linux/BusyBox sources are not" +
80139
"checked out correctly.");
@@ -124,4 +183,16 @@ public static void print(GroundTruth groundTruth, String commitName) {
124183
System.out.println(groundTruth.get(file));
125184
}
126185
}
186+
187+
/**
188+
* Starts the extraction.
189+
*
190+
* @param options The options for DiffDetective
191+
* @throws IOException If an IO error occurs in DiffDetective
192+
*/
193+
public void run(AnalysisRunner.Options options) throws IOException {
194+
AnalysisRunner.run(options, extractionRunner());
195+
}
196+
197+
protected abstract BiConsumer<Repository, Path> extractionRunner();
127198
}

0 commit comments

Comments
 (0)