|
18 | 18 | import org.variantsync.diffdetective.variation.diff.Time; |
19 | 19 | import org.variantsync.diffdetective.variation.diff.VariationDiff; |
20 | 20 | import org.variantsync.diffdetective.variation.diff.parse.VariationDiffParseOptions; |
| 21 | +import org.variantsync.diffdetective.variation.diff.source.VariationDiffSource; |
21 | 22 | import org.variantsync.diffdetective.variation.diff.view.DiffView; |
22 | 23 | import org.variantsync.diffdetective.variation.tree.VariationTree; |
23 | 24 | import org.variantsync.diffdetective.variation.tree.view.relevance.Configure; |
24 | 25 | import org.variantsync.diffdetective.variation.tree.view.relevance.Relevance; |
25 | 26 |
|
26 | 27 | public class PatchingExperiment { |
27 | 28 |
|
28 | | - private static Relevance calculateFeatureSetToDeselect(VariationTree<DiffLinesLabel> variant1, VariationTree<DiffLinesLabel> variant2, boolean debug) { |
| 29 | + private static Relevance calculateFeatureSetToDeselect(VariationTree<DiffLinesLabel> variant1version1, VariationTree<DiffLinesLabel> variant1version2, VariationTree<DiffLinesLabel> variant2, boolean debug) { |
29 | 30 | Set<String> featuresTreeV1 = new HashSet<String>(); |
30 | | - variant1.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());}); |
| 31 | + variant1version1.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());}); |
| 32 | + if (variant1version2 != null) { |
| 33 | + variant1version2.forAllPreorder(node -> {featuresTreeV1.addAll(node.getFeatureMapping().getUniqueContainedFeatures());}); |
| 34 | + } |
31 | 35 | Set<String> featuresTreeV2 = new HashSet<String>(); |
32 | 36 | variant2.forAllPreorder(node -> {featuresTreeV2.addAll(node.getFeatureMapping().getUniqueContainedFeatures());}); |
33 | 37 |
|
@@ -69,88 +73,80 @@ private static Set<DiffNode<DiffLinesLabel>> findRootsOfSubtrees(Set<DiffNode<Di |
69 | 73 | return subtreeRoots; |
70 | 74 | } |
71 | 75 |
|
72 | | - private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVariantVersion1, VariationTree<DiffLinesLabel> sourceVariantVersion2, VariationTree<DiffLinesLabel> targetVariant) { |
73 | | - if (sourceVariantVersion1 == null || sourceVariantVersion2 == null || targetVariant == null) { |
74 | | - System.out.println("Parsing error"); |
75 | | - return; |
76 | | - } |
| 76 | + private static void applyChanges(DiffType type, VariationDiff<DiffLinesLabel> targetVariantDiff, Set<DiffNode<DiffLinesLabel>> subtreeRoots, VariationDiffSource source, boolean debug) { |
| 77 | + Time time = (type == DiffType.ADD) ? Time.AFTER : Time.BEFORE; |
77 | 78 |
|
78 | | - VariationDiff<DiffLinesLabel> diff = VariationDiff.fromTrees(sourceVariantVersion1, sourceVariantVersion2); |
79 | | - Relevance rho = calculateFeatureSetToDeselect(sourceVariantVersion1, targetVariant, false); |
80 | | - VariationDiff<DiffLinesLabel> optimizedDiff = DiffView.optimized(diff, rho); |
81 | | - |
82 | | - // add new nodes |
83 | | - Set<DiffNode<DiffLinesLabel>> addedNodes = new HashSet<DiffNode<DiffLinesLabel>>(); |
84 | | - optimizedDiff.forAll(node -> {if (node.isAdd()) {addedNodes.add(node);}}); |
85 | | - |
86 | | - // find roots of subtrees of added nodes |
87 | | - Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, false); |
88 | | - |
89 | | - VariationDiff<DiffLinesLabel> targetVariantDiff = VariationDiff.fromTrees(targetVariant, targetVariant); |
90 | | - for (DiffNode<DiffLinesLabel> root : addedSubtreeRoots) { |
91 | | - VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), optimizedDiff.getSource()); |
92 | | - GameEngine.showAndAwaitAll(Show.diff(subTree)); |
| 79 | + for (DiffNode<DiffLinesLabel> root : subtreeRoots) { |
| 80 | + if (debug) { |
| 81 | + VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), source); |
| 82 | + GameEngine.showAndAwaitAll(Show.diff(subTree)); |
| 83 | + } |
| 84 | + |
93 | 85 | List<DiffNode<DiffLinesLabel>> targetNodes = new ArrayList<DiffNode<DiffLinesLabel>>(); |
94 | 86 | if (root.isArtifact()) { |
95 | 87 | targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER) |
96 | | - .equals(root.getPresenceCondition(Time.AFTER)) && node.isAnnotation()); |
| 88 | + .equals(root.getPresenceCondition(time)) && node.isAnnotation()); |
97 | 89 | } else if (root.isAnnotation()) { |
98 | 90 | targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER) |
99 | | - .equals(root.getParent(Time.AFTER).getPresenceCondition(Time.AFTER)) && node.isAnnotation()); |
| 91 | + .equals(root.getParent(time).getPresenceCondition(time)) && node.isAnnotation()); |
100 | 92 | } |
101 | 93 |
|
102 | 94 | if (targetNodes.size() != 1) { |
103 | 95 | System.out.println("too much or too less target nodes found"); |
104 | 96 | } else { |
105 | | - System.out.println("subtree added"); |
106 | | - targetNodes.get(0).addChild(root.deepCopy(), Time.AFTER); |
| 97 | + if (type == DiffType.ADD) { |
| 98 | + System.out.println("subtree added"); |
| 99 | + targetNodes.get(0).addChild(root.deepCopy(), Time.AFTER); |
| 100 | + System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER)); |
| 101 | + } else if (type == DiffType.REM) { |
| 102 | + DiffNode<DiffLinesLabel> parent = targetNodes.get(0); |
| 103 | + List<DiffNode<DiffLinesLabel>> nodesToRem = new ArrayList<DiffNode<DiffLinesLabel>>(); |
| 104 | + parent.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, Time.BEFORE)) nodesToRem.add(node);}); |
| 105 | + if (nodesToRem.size() != 1) { |
| 106 | + System.out.println("too much or too less target nodes found"); |
| 107 | + } else { |
| 108 | + System.out.println("subtree removed"); |
| 109 | + nodesToRem.get(0).diffType = DiffType.REM; |
| 110 | + nodesToRem.get(0).drop(Time.AFTER); |
| 111 | + System.out.println(targetNodes.get(0).getChildOrder(Time.AFTER)); |
| 112 | + } |
| 113 | + } |
107 | 114 | } |
108 | 115 | } |
109 | | - |
110 | | - // remove old nodes |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + private static void patchVariationTrees(VariationTree<DiffLinesLabel> sourceVariantVersion1, VariationTree<DiffLinesLabel> sourceVariantVersion2, VariationTree<DiffLinesLabel> targetVariant) { |
| 120 | + if (sourceVariantVersion1 == null || sourceVariantVersion2 == null || targetVariant == null) { |
| 121 | + System.out.println("Parsing error"); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + VariationDiff<DiffLinesLabel> diff = VariationDiff.fromTrees(sourceVariantVersion1, sourceVariantVersion2); |
| 126 | + Relevance rho = calculateFeatureSetToDeselect(sourceVariantVersion1, sourceVariantVersion2, targetVariant, false); |
| 127 | + VariationDiff<DiffLinesLabel> optimizedDiff = DiffView.optimized(diff, rho); |
| 128 | + VariationDiffSource source = optimizedDiff.getSource(); |
| 129 | + VariationDiff<DiffLinesLabel> targetVariantDiff = targetVariant.toCompletelyUnchangedVariationDiff(); |
| 130 | + |
| 131 | + // add new nodes |
| 132 | + Set<DiffNode<DiffLinesLabel>> addedNodes = new HashSet<DiffNode<DiffLinesLabel>>(); |
| 133 | + optimizedDiff.forAll(node -> {if (node.isAdd()) {addedNodes.add(node);}}); |
| 134 | + Set<DiffNode<DiffLinesLabel>> addedSubtreeRoots = findRootsOfSubtrees(addedNodes, false); |
| 135 | + applyChanges(DiffType.ADD, targetVariantDiff, addedSubtreeRoots, source, false); |
| 136 | + // remove old nodes |
111 | 137 | Set<DiffNode<DiffLinesLabel>> removedNodes = new HashSet<DiffNode<DiffLinesLabel>>(); |
112 | 138 | optimizedDiff.forAll(node -> {if (node.isRem()) {removedNodes.add(node);}}); |
113 | | - |
114 | 139 | Set<DiffNode<DiffLinesLabel>> removedSubtreeRoots = findRootsOfSubtrees(removedNodes, false); |
115 | | - |
116 | | -// VariationDiff<DiffLinesLabel> targetVariantDiff2 = VariationDiff.fromTrees(targetVariant, targetVariant); |
117 | | - for (DiffNode<DiffLinesLabel> root : removedSubtreeRoots) { |
118 | | - VariationDiff<DiffLinesLabel> subTree = new VariationDiff<DiffLinesLabel>(root.deepCopy(), optimizedDiff.getSource()); |
119 | | - GameEngine.showAndAwaitAll(Show.diff(subTree)); |
120 | | - List<DiffNode<DiffLinesLabel>> targetNodes = new ArrayList<DiffNode<DiffLinesLabel>>(); |
121 | | - if (root.isArtifact()) { |
122 | | - targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER) |
123 | | - .equals(root.getPresenceCondition(Time.BEFORE)) && node.isAnnotation()); |
124 | | - } else if (root.isAnnotation()) { |
125 | | - targetNodes = targetVariantDiff.computeAllNodesThat(node -> node.getPresenceCondition(Time.AFTER) |
126 | | - .equals(root.getParent(Time.BEFORE).getPresenceCondition(Time.BEFORE)) && node.isAnnotation()); |
127 | | - } |
128 | | - if (targetNodes.size() != 1) { |
129 | | - System.out.println("too much or too less target nodes found"); |
130 | | - } else { |
131 | | - |
132 | | -// System.out.println(targetNodes.get(0)); |
133 | | - DiffNode<DiffLinesLabel> parent = targetNodes.get(0); |
134 | | - List<DiffNode<DiffLinesLabel>> nodesToRem = new ArrayList<DiffNode<DiffLinesLabel>>(); |
135 | | - parent.getAllChildrenStream().forEach(node -> { if (node.isSameAs(root, Time.BEFORE)) nodesToRem.add(node);}); |
136 | | - if (nodesToRem.size() != 1) { |
137 | | - System.out.println("too much or too less target nodes found"); |
138 | | - } else { |
139 | | - System.out.println("subtree removed"); |
140 | | - nodesToRem.get(0).diffType = DiffType.REM; |
141 | | - nodesToRem.get(0).drop(Time.AFTER); |
142 | | - } |
143 | | - } |
144 | | - } |
| 140 | + applyChanges(DiffType.REM, targetVariantDiff, removedSubtreeRoots, source, false); |
145 | 141 |
|
146 | 142 | GameEngine.showAndAwaitAll( |
147 | 143 | Show.tree(sourceVariantVersion1), |
148 | 144 | Show.tree(sourceVariantVersion2), |
149 | 145 | Show.tree(targetVariant), |
150 | 146 | Show.diff(optimizedDiff), |
151 | | - Show.diff(targetVariantDiff) |
| 147 | + Show.diff(targetVariantDiff), |
| 148 | + Show.tree(targetVariantDiff.project(Time.AFTER)) |
152 | 149 | ); |
153 | | - |
154 | 150 | } |
155 | 151 |
|
156 | 152 | private static VariationTree<DiffLinesLabel> parseVariationTreeFromFile(String file) { |
|
0 commit comments