|
| 1 | +package org.variantsync.diffdetective.experiments.thesis_pm; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Path; |
| 5 | +import java.util.HashSet; |
| 6 | +import java.util.Iterator; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import org.eclipse.jgit.diff.DiffAlgorithm; |
| 10 | +import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm; |
| 11 | +import org.variantsync.diffdetective.diff.result.DiffParseException; |
| 12 | +import org.variantsync.diffdetective.variation.DiffLinesLabel; |
| 13 | +import org.variantsync.diffdetective.variation.Label; |
| 14 | +import org.variantsync.diffdetective.variation.diff.DiffNode; |
| 15 | +import org.variantsync.diffdetective.variation.diff.VariationDiff; |
| 16 | +import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions; |
| 17 | +import org.variantsync.diffdetective.variation.tree.VariationTree; |
| 18 | +import org.variantsync.diffdetective.variation.tree.view.TreeView; |
| 19 | +import org.variantsync.diffdetective.variation.tree.view.relevance.ConfigureWithFullConfig; |
| 20 | +import org.variantsync.diffdetective.variation.tree.view.relevance.Unchanged; |
| 21 | +import org.variantsync.functjonal.Pair; |
| 22 | + |
| 23 | +public class Utils { |
| 24 | + |
| 25 | + public static boolean comparePatchedVariantWithExpectedResult(VariationTree<DiffLinesLabel> patchedVariant, |
| 26 | + VariationTree<DiffLinesLabel> expectedResult) { |
| 27 | + return Utils.isSameAs(patchedVariant.toCompletelyUnchangedVariationDiff(), |
| 28 | + expectedResult.toCompletelyUnchangedVariationDiff()); |
| 29 | + } |
| 30 | + |
| 31 | + public static Pair<Boolean, Boolean> arePatchedVariantsEquivalent( |
| 32 | + VariationTree<DiffLinesLabel> patchedTargetVariant, |
| 33 | + VariationTree<DiffLinesLabel> sourceVariantAfterRedToCrossVarFeatures, |
| 34 | + VariationTree<DiffLinesLabel> targetVariantBeforeRedToUnchanged, ConfigureWithFullConfig configSourceVariant, |
| 35 | + Unchanged unchangedAfter) { |
| 36 | + |
| 37 | + VariationTree<DiffLinesLabel> targetVariantAfterRedToCrossVarFeatures = TreeView.tree(patchedTargetVariant, |
| 38 | + configSourceVariant); |
| 39 | + |
| 40 | + VariationTree<DiffLinesLabel> patchedTargetVariantRedToUnchanged = TreeView.tree(patchedTargetVariant, |
| 41 | + unchangedAfter); |
| 42 | + |
| 43 | + // GameEngine.showAndAwaitAll(Show.tree(patchedTargetVariant, "patched target variant"), |
| 44 | + // Show.tree(patchedTargetVariantRedToUnchanged, "patched target variant red. to unchanged"), |
| 45 | + // Show.tree(sourceVariantAfterRedToCrossVarFeatures, |
| 46 | + // "patched source variant red. to cross variant features"), |
| 47 | + // Show.tree(targetVariantAfterRedToCrossVarFeatures, |
| 48 | + // "patched target variant red. to cross variant features"), |
| 49 | + // Show.tree(targetVariantBeforeRedToUnchanged, "target variant before red. to unchanged")); |
| 50 | + |
| 51 | + return new Pair<Boolean, Boolean>( |
| 52 | + sourceVariantAfterRedToCrossVarFeatures.unparse().equals(targetVariantAfterRedToCrossVarFeatures.unparse()), |
| 53 | + patchedTargetVariantRedToUnchanged.unparse().equals(targetVariantBeforeRedToUnchanged.unparse())); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + public static VariationDiff<DiffLinesLabel> parseVariationDiffFromFiles(String file1, String file2) |
| 58 | + throws IOException, DiffParseException { |
| 59 | + Path examplesDir = Path.of("data", "examples"); |
| 60 | + return VariationDiff.fromFiles(examplesDir.resolve(file1), examplesDir.resolve(file2), |
| 61 | + DiffAlgorithm.SupportedAlgorithm.MYERS, VariationDiffParseOptions.Default); |
| 62 | + } |
| 63 | + |
| 64 | + public static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String file) { |
| 65 | + Path examplesDir = Path.of("data", "examples"); |
| 66 | + Path path = examplesDir.resolve(file); |
| 67 | + try { |
| 68 | + VariationTree<DiffLinesLabel> tree = VariationTree.fromFile(path, VariationDiffParseOptions.Default); |
| 69 | + return tree; |
| 70 | + } catch (IOException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } catch (DiffParseException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + public static VariationDiff<DiffLinesLabel> parseVariationDiffFromFile(String file) |
| 79 | + throws IOException, DiffParseException { |
| 80 | + Path examplesDir = Path.of("data", "examples"); |
| 81 | + return VariationDiff.fromFile(examplesDir.resolve(file), VariationDiffParseOptions.Default); |
| 82 | + } |
| 83 | + |
| 84 | + public static <L extends Label> boolean isSameAs(VariationDiff<L> diff1, VariationDiff<L> diff2) { |
| 85 | + return Utils.isSameAs(diff1.getRoot(), diff2.getRoot()); |
| 86 | + } |
| 87 | + |
| 88 | + public static <L extends Label> boolean isSameAs(DiffNode<L> a, DiffNode<L> b) { |
| 89 | + return Utils.isSameAs(a, b, new HashSet<>()); |
| 90 | + } |
| 91 | + |
| 92 | + public static <L extends Label> boolean isSameAs(DiffNode<L> a, DiffNode<L> b, Set<DiffNode<L>> visited) { |
| 93 | + if (!visited.add(a)) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + if (!(a.getNodeType().equals(b.getNodeType()) && hasSameLabel(a.getLabel(), b.getLabel()) |
| 98 | + && (a.getFormula() == null ? b.getFormula() == null : a.getFormula().equals(b.getFormula())))) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + Iterator<DiffNode<L>> aIt = a.getAllChildren().iterator(); |
| 103 | + Iterator<DiffNode<L>> bIt = b.getAllChildren().iterator(); |
| 104 | + while (aIt.hasNext() && bIt.hasNext()) { |
| 105 | + if (!isSameAs(aIt.next(), bIt.next(), visited)) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return aIt.hasNext() == bIt.hasNext(); |
| 111 | + } |
| 112 | + |
| 113 | + public static <L extends Label> boolean hasSameLabel(L a, L b) { |
| 114 | + String labelA = a.toString().replaceAll(" ", ""); |
| 115 | + String labelB = b.toString().replaceAll(" ", ""); |
| 116 | + return labelA.equals(labelB); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments