Skip to content

Commit a7b2208

Browse files
committed
fixed and clean up generator
1 parent 2878951 commit a7b2208

1 file changed

Lines changed: 14 additions & 78 deletions

File tree

  • src/main/java/org/variantsync/diffdetective/experiments/thesis_pm

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

Lines changed: 14 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ public class Generator {
5656
final private static String targetVariant = "targetVariant";
5757
final private static String code = "code.txt";
5858
final private static String patch = "patch.txt";
59-
// final private static ShellExecutor shellSourceVariantDir = new ShellExecutor(Logger::info, Logger::error,
60-
// Path.of(directory, sourceVariant));
61-
// final private static ShellExecutor shellTargetVariantDir = new ShellExecutor(Logger::info, Logger::error,
62-
// Path.of(directory, targetVariant));
6359

6460
enum Error {
6561
FAILED, ERROR
@@ -157,36 +153,22 @@ public static <L extends Label> PatchScenario<L> generatePatchScenario(Variation
157153
// Since we have no feature model, we create a naive problem space model:
158154
// We just collect all features without constraints.
159155
final Set<Object> featureModel = spl.computeAllFeatureNames();
160-
// Logger.info("Extracted feature names: {}", featureModel);
156+
Logger.info("Extracted feature names: {}", featureModel);
161157

162158
// To sample variants, we just pick a random subset of features to set to true,
163159
// set the rest to false
164160
// We could use more sophisticated algorithms here, for example based on how
165161
// often features occur.
166-
// Maybe should ask Sebastian.
167162
// The configurations we produce are complete.
168-
// Hypothesis: the lower the probability value (i.e., the more deselected
169-
// features), the harder the patching challenge.
170163

171164
final Map<Object, Boolean> config1 = randomPartition(featureModel, 0.6);
172165
final Map<Object, Boolean> config2 = mutateByWeightedCoinFlip(config1, 0.5);
173166

174-
// FIXME: We should probably ensure that config1 != config2.
175-
// Logger.info("Configuration 1: {}", config1);
176-
// Logger.info("Configuration 2: {}", config2);
177-
// TODO: We could also distinguish the two major scenarios from these
178-
// configurations:
179-
// There are features in source that are not in target and vice versa? (assumes
180-
// that nothing is labeled to the negation of a feature)
181-
// TODO: Decide how many variants we want to generate per commit / patch and how
182-
// we want to compare them? Have every generated variant be the source once? I
183-
// think this is what Alex did in his ICSME'22 paper.
184-
185-
// To configure our variation trees and diffs, we need to convert our
186-
// configurations to relevance predicates.
187-
// writeToFile(spl.project(Time.BEFORE).unparse(), Path.of("spl-before.txt"));
188-
// logDiff("spl before",spl.project(Time.BEFORE).unparse());
189-
// logDiff("spl after",spl.project(Time.AFTER).unparse());
167+
Logger.info("Configuration 1: {}", config1);
168+
Logger.info("Configuration 2: {}", config2);
169+
170+
logDiff("spl before",spl.project(Time.BEFORE).unparse());
171+
logDiff("spl after",spl.project(Time.AFTER).unparse());
190172

191173
final ConfigureWithFullConfig configureTo1 = new ConfigureWithFullConfig(config1);
192174
final ConfigureWithFullConfig configureTo2 = new ConfigureWithFullConfig(config2);
@@ -206,7 +188,7 @@ public static <L extends Label> PatchScenario<L> generatePatchScenario(Variation
206188
VariationDiff<L> sourcePatchElimEmptyAlt;
207189
try {
208190
sourcePatchElimEmptyAlt = (VariationDiff<L>) VariationDiff.fromLines(sourceBefore.unparse(),
209-
sourceAfter.unparse(), DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default);
191+
sourceAfter.unparse(), sourceBefore, sourceAfter, DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default);
210192
} catch (DiffParseException e) {
211193
return null;
212194
}
@@ -220,7 +202,7 @@ public static <L extends Label> PatchScenario<L> generatePatchScenario(Variation
220202
VariationDiff<L> targetPatchElimEmptyAlt;
221203
try {
222204
targetPatchElimEmptyAlt = (VariationDiff<L>) VariationDiff.fromLines(before.unparse(), after.unparse(),
223-
DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default);
205+
before, after, DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default);
224206
} catch (DiffParseException e) {
225207
return null;
226208
}
@@ -233,14 +215,6 @@ public static <L extends Label> PatchScenario<L> generatePatchScenario(Variation
233215

234216
VariationDiff<L> targetView = DiffView.optimized(targetPatchModified.deepCopy(), configureTo1);
235217

236-
// GameEngine.showAndAwaitAll(Show.diff(targetPatchModified));
237-
// logDiff("modified target patch (before)", targetPatchModified.project(Time.BEFORE).unparse());
238-
239-
// GameEngine.showAndAwaitAll(Show.diff(sourcePatch, "source patch"),
240-
// Show.diff(targetPatchModified, "target patch elim empty altern and resolved"),
241-
// Show.diff(targetPatchElimEmptyAlt, "target patch elim empty altern"),
242-
// Show.diff(targetView, "V' (from target patch)"));
243-
244218
// revert the changes made in B but not in A
245219
new RevertSomeChanges<DiffLinesLabel>(node -> {
246220
if (targetView.getNodeWithID(node.getID()) == null && !node.isNon()) {
@@ -251,32 +225,16 @@ public static <L extends Label> PatchScenario<L> generatePatchScenario(Variation
251225

252226
targetPatch = targetPatchModified;
253227

254-
// GameEngine.showAndAwaitAll(Show.diff(sourcePatch, "source patch"), Show.diff(targetPatch, "target patch"));
255-
256-
// FIXME: Maybe we want to distinguish cases where one of the patches (or both)
257-
// are empty (i.e., noop / id)?
258-
259228
final VariationTree<L> sourceVariantBefore = sourcePatch.project(Time.BEFORE); // input
260229
final VariationTree<L> sourceVariantAfter = sourcePatch.project(Time.AFTER); // input
261230
final VariationTree<L> targetVariantBefore = targetPatch.project(Time.BEFORE); // input
262-
final VariationTree<L> targetVariantAfter = targetPatch.project(Time.AFTER); // ground truth for how the patched
263-
// target variant should ideally
264-
// look like
265-
266-
// GameEngine.showAndAwaitAll(Show.diff(spl));
267-
// GameEngine.showAndAwaitAll(Show.diff(sourcePatch, "Source Patch " + config1),
268-
// Show.diff(targetPatch, "Target Patch " + config2));
269-
// GameEngine.showAndAwaitAll(Show.tree(sourceVariantBefore, "Source Before " + config1),
270-
// Show.tree(sourceVariantAfter, "Source After " + config1),
271-
// Show.tree(targetVariantBefore, "Target Before " + config2),
272-
// Show.tree(targetVariantAfter, "Target After" + config2));
231+
final VariationTree<L> targetVariantAfter = targetPatch.project(Time.AFTER);
273232

274233
// ## 3. To use command-line patchers such as GNU patch and mpatch, we need to
275234
// write our variants to disk.
276235
deleteFilesAndCreateNewDirectories(commitHash);
277236
Path patchPath = writeVariantsToFileSystem(sourceVariantBefore, sourceVariantAfter, targetVariantBefore, code,
278237
patch, commitHash);
279-
// writeToFile(targetVariantCodeAfter, targetVariantAfterPath);
280238

281239
if (!runGnuDiff(patchPath, commitHash)) {
282240
return null;
@@ -291,7 +249,6 @@ public static <L extends Label> boolean generateViewVariants(VariationDiff<L> so
291249
try {
292250
deleteFilesAndCreateNewDirectories(commitHash);
293251
} catch (Exception e) {
294-
// TODO Auto-generated catch block
295252
e.printStackTrace();
296253
}
297254
final VariationDiff<L> sourceVariantCrossVariantView = DiffView.optimized(sourcePatch, configureTo2);
@@ -304,7 +261,6 @@ public static <L extends Label> boolean generateViewVariants(VariationDiff<L> so
304261
sourceVariantCrossVariantViewAfter, targetVariantBefore, code, patch, commitHash);
305262
return runGnuDiff(patchPath2, commitHash);
306263
} catch (Exception e) {
307-
// TODO Auto-generated catch block
308264
e.printStackTrace();
309265
}
310266
return false;
@@ -315,14 +271,7 @@ private static <L extends Label> Path writeVariantsToFileSystem(final VariationT
315271
final String patch, String commitHash) throws Exception {
316272
final String sourceVariantCodeBefore = sourceVariantBefore.unparse();
317273
final String sourceVariantCodeAfter = sourceVariantAfter.unparse();
318-
final String targetVariantCodeBefore = targetVariantBefore.unparse(); // ground truth for fast comparisons
319-
// (beware
320-
// of differences in line breaks and
321-
// whitespaces!)
322-
// logDiff("Source Before:", sourceVariantCodeBefore);
323-
// logDiff("Source After:", sourceVariantCodeAfter);
324-
// logDiff("Target Before:", targetVariantCodeBefore);
325-
// logDiff("Target After:", targetVariantCodeAfter);
274+
final String targetVariantCodeBefore = targetVariantBefore.unparse();
326275
String newDir = directory + commitHash;
327276
Path sourceVariantBeforePath = Path.of(newDir, sourceVariant, version1, code);
328277
Path sourceVariantAfterPath = Path.of(newDir, sourceVariant, version2, code);
@@ -383,7 +332,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
383332
gameEngine.add(Show.diff(scenario.patchGroundTruth, "targetPatch"));
384333
gameEngine.add(Show.tree(scenario.patchedVariantGroundTruth, "ground truth"));
385334

386-
// TODO: Run Pia's new patcher here and store the result.
335+
// Runs Pia's new patcher here and stores the result.
387336
Result<VariationTree<DiffLinesLabel>, Error> patchTransformerResult = runPatchTransformer(scenario.sourcePatch,
388337
scenario.targetVariantBefore, scenario.sourceVariantConfig, scenario.targetVariantConfig);
389338
if (patchTransformerResult.isSuccess()) {
@@ -409,7 +358,6 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
409358
return equiv.first() && equiv.second();
410359
}, error -> false);
411360
} catch (IOException e) {
412-
// TODO Auto-generated catch block
413361
e.printStackTrace();
414362
}
415363

@@ -426,7 +374,6 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
426374
return equiv.first() && equiv.second();
427375
}, error -> false);
428376
} catch (IOException e) {
429-
// TODO Auto-generated catch block
430377
e.printStackTrace();
431378
}
432379

@@ -445,7 +392,6 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
445392
return equiv.first() && equiv.second();
446393
}, error -> false);
447394
} catch (IOException e) {
448-
// TODO Auto-generated catch block
449395
e.printStackTrace();
450396
}
451397

@@ -462,11 +408,10 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
462408
return equiv.first() && equiv.second();
463409
}, error -> false);
464410
} catch (IOException e) {
465-
// TODO Auto-generated catch block
466411
e.printStackTrace();
467412
}
468413

469-
// TODO: Read the results of the patchers. The patchers should produce the
414+
// Read the results of the patchers. The patchers should produce the
470415
// target variants as string if they did not fail.
471416
GameEngine[] gameEngineArray = new GameEngine[gameEngine.size()];
472417
gameEngineArray = gameEngine.toArray(gameEngineArray);
@@ -484,7 +429,7 @@ public static <L extends Label> void runPatchers(PatchScenario<L> scenario, Stri
484429
public static <L extends Label> Result<VariationTree<DiffLinesLabel>, Error> runMPatch(
485430
final VariationTree<L> targetVariantBefore, String patch, String code, String commitHash)
486431
throws IOException {
487-
// TODO: configure mpatch
432+
// configure mpatch
488433
// reset target variant
489434
resetTargetVariantBefore(targetVariantBefore, code, commitHash);
490435
Path mpatchPath = Path.of("..", "..", "..", "mpatch", "target", "release", "mpatch");
@@ -495,12 +440,6 @@ public static <L extends Label> Result<VariationTree<DiffLinesLabel>, Error> run
495440
try {
496441
shell.execute(command);
497442
} catch (ShellException e) {
498-
// FIXME: When a shell exception occurs, we know that patching failed.
499-
// Either there is a bug or the patcher was not successful!
500-
// We should not throw and catch exceptions in these cases.
501-
// It is probably best to write new ShellCommand subclasses for mpatch and GNU
502-
// patch with a proper interpretResult method.
503-
// We have to distinguish patch success from patch failure anyway somewhere.
504443
Logger.error(e);
505444
return Result.Failure(Error.ERROR);
506445
}
@@ -525,7 +464,7 @@ private static <L extends Label> void resetTargetVariantBefore(final VariationTr
525464
public static <L extends Label> Result<VariationTree<DiffLinesLabel>, Error> runGnuPatch(
526465
final VariationTree<L> targetVariantBefore, String patch, String code, String commitHash)
527466
throws IOException {
528-
// TODO: run mpatch and gnu patch. Here is a sketch for this can be done.
467+
// run mpatch and gnu patch. Here is a sketch for this can be done.
529468
// reset target variant
530469
resetTargetVariantBefore(targetVariantBefore, code, commitHash);
531470
Path pathToTargetVariantCode = Path.of("..", targetVariant, code);
@@ -535,7 +474,6 @@ public static <L extends Label> Result<VariationTree<DiffLinesLabel>, Error> run
535474
GnuPatchCommand command = new GnuPatchCommand("patch", pathToTargetVariantCode.toString(),
536475
pathToSourceVariantPatch.toString());
537476
try {
538-
// TODO: configure GNU patch
539477
shell.execute(command);
540478
// reset target variant
541479
} catch (ShellException e) {
@@ -560,13 +498,11 @@ public static <L extends Label> Result<VariationTree<DiffLinesLabel>, Error> run
560498
ConfigureWithFullConfig sourceVariantConfig, ConfigureWithFullConfig targetVariantConfig) {
561499
VariationTree<DiffLinesLabel> patchTransformerResult = null;
562500
try {
563-
// GameEngine.showAndAwaitAll(Show.tree(targetVariantBefore));
564501
VariationDiff<DiffLinesLabel> diff = Patching.patch((VariationDiff<DiffLinesLabel>) sourcePatch,
565502
(VariationTree<DiffLinesLabel>) targetVariantBefore, sourceVariantConfig, targetVariantConfig,
566503
false, true);
567504
patchTransformerResult = diff.project(Time.AFTER);
568505
} catch (Exception e) {
569-
// TODO Auto-generated catch block
570506
e.printStackTrace();
571507
return Result.Failure(Error.FAILED);
572508
}

0 commit comments

Comments
 (0)