|
1 | 1 | package graphql.schema.diffing; |
2 | 2 |
|
| 3 | +import com.google.common.collect.BiMap; |
| 4 | +import com.google.common.collect.HashBiMap; |
| 5 | +import com.google.common.collect.HashMultimap; |
| 6 | +import com.google.common.collect.Multimaps; |
3 | 7 | import graphql.Internal; |
4 | 8 | import graphql.schema.GraphQLSchema; |
5 | 9 | import graphql.schema.diffing.ana.EditOperationAnalysisResult; |
@@ -52,51 +56,135 @@ private DiffImpl.OptimalEdit diffImpl(SchemaGraph sourceGraph, SchemaGraph targe |
52 | 56 | PossibleMappingsCalculator possibleMappingsCalculator = new PossibleMappingsCalculator(sourceGraph, targetGraph, runningCheck); |
53 | 57 | PossibleMappingsCalculator.PossibleMappings possibleMappings = possibleMappingsCalculator.calculate(); |
54 | 58 |
|
55 | | - Mapping fixedMappings = Mapping.newMapping( |
| 59 | + Mapping startMapping = Mapping.newMapping( |
56 | 60 | possibleMappings.fixedOneToOneMappings, |
57 | 61 | possibleMappings.fixedOneToOneSources, |
58 | 62 | possibleMappings.fixedOneToOneTargets); |
59 | 63 |
|
60 | 64 | assertTrue(sourceGraph.size() == targetGraph.size()); |
61 | 65 | if (possibleMappings.fixedOneToOneMappings.size() == sourceGraph.size()) { |
62 | | - return new DiffImpl.OptimalEdit(sourceGraph, targetGraph, fixedMappings, baseEditorialCostForMapping(fixedMappings, sourceGraph, targetGraph)); |
| 66 | + return new DiffImpl.OptimalEdit(sourceGraph, targetGraph, startMapping, baseEditorialCostForMapping(startMapping, sourceGraph, targetGraph)); |
63 | 67 | } |
64 | 68 |
|
65 | | - DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, possibleMappings, runningCheck); |
66 | 69 | List<Vertex> nonMappedSource = new ArrayList<>(sourceGraph.getVertices()); |
67 | 70 | nonMappedSource.removeAll(possibleMappings.fixedOneToOneSources); |
68 | 71 |
|
69 | 72 | List<Vertex> nonMappedTarget = new ArrayList<>(targetGraph.getVertices()); |
70 | 73 | nonMappedTarget.removeAll(possibleMappings.fixedOneToOneTargets); |
71 | 74 |
|
72 | | - |
73 | 75 | runningCheck.check(); |
74 | | - sortSourceVertices(nonMappedSource, possibleMappings); |
75 | | - |
76 | | - // the non mapped vertices go to the end |
77 | | - List<Vertex> sourceVertices = new ArrayList<>(); |
78 | | - sourceVertices.addAll(possibleMappings.fixedOneToOneSources); |
79 | | - sourceVertices.addAll(nonMappedSource); |
80 | 76 |
|
81 | | - List<Vertex> targetGraphVertices = new ArrayList<>(); |
82 | | - targetGraphVertices.addAll(possibleMappings.fixedOneToOneTargets); |
83 | | - targetGraphVertices.addAll(nonMappedTarget); |
84 | 77 |
|
| 78 | +// System.out.println("sort by mapping count: "); |
| 79 | +// for (Vertex v : nonMappedSource) { |
| 80 | +// System.out.println(possibleMappings.possibleMappings.get(v).size() + " for " + v); |
| 81 | +// } |
| 82 | +// System.out.println("=------------"); |
| 83 | +// ArrayList<Vertex> copy = new ArrayList<>(nonMappedSource); |
| 84 | +// sortSourceVerticesEdgeCountDescending(copy, possibleMappings); |
| 85 | +// System.out.println("sort by edge count "); |
| 86 | +// for (Vertex v : copy) { |
| 87 | +// System.out.println(sourceGraph.adjacentEdgesAndInverseCount(v) + " for " + v); |
| 88 | +// } |
| 89 | +// System.out.println("------------"); |
| 90 | +// |
| 91 | + // the non mapped vertices go to the end |
85 | 92 |
|
86 | | - DiffImpl.OptimalEdit optimalEdit = diffImpl.diffImpl(fixedMappings, sourceVertices, targetGraphVertices); |
87 | | - return optimalEdit; |
| 93 | + int isolatedSourceCount = (int) nonMappedSource.stream().filter(Vertex::isIsolated).count(); |
| 94 | + int isolatedTargetCount = (int) nonMappedTarget.stream().filter(Vertex::isIsolated).count(); |
| 95 | + if (isolatedTargetCount > isolatedSourceCount) { |
| 96 | + System.out.println("delete heavy ... invert source and target graph"); |
| 97 | + // we flip source and target |
| 98 | + BiMap<Vertex, Vertex> fixedOneToOneInverted = HashBiMap.create(); |
| 99 | + for (Vertex s : possibleMappings.fixedOneToOneMappings.keySet()) { |
| 100 | + Vertex t = possibleMappings.fixedOneToOneMappings.get(s); |
| 101 | + fixedOneToOneInverted.put(t, s); |
| 102 | + } |
| 103 | + Mapping startMappingInverted = Mapping.newMapping( |
| 104 | + fixedOneToOneInverted, |
| 105 | + possibleMappings.fixedOneToOneTargets, |
| 106 | + possibleMappings.fixedOneToOneSources |
| 107 | + ); |
| 108 | + HashMultimap<Vertex, Vertex> invertedPossibleOnes = HashMultimap.create(); |
| 109 | + Multimaps.invertFrom(possibleMappings.possibleMappings, invertedPossibleOnes); |
| 110 | + possibleMappings.possibleMappings = invertedPossibleOnes; |
| 111 | + |
| 112 | + List<Vertex> sourceVertices = new ArrayList<>(); |
| 113 | + sourceVertices.addAll(possibleMappings.fixedOneToOneSources); |
| 114 | + sourceVertices.addAll(nonMappedSource); |
| 115 | + |
| 116 | + List<Vertex> targetVertices = new ArrayList<>(); |
| 117 | + targetVertices.addAll(possibleMappings.fixedOneToOneTargets); |
| 118 | + targetVertices.addAll(nonMappedTarget); |
| 119 | + |
| 120 | + sortVerticesEdgeCountDescending(nonMappedTarget, targetGraph); |
| 121 | + DiffImpl diffImpl = new DiffImpl(targetGraph, sourceGraph, possibleMappings, runningCheck); |
| 122 | + DiffImpl.OptimalEdit optimalEdit = diffImpl.diffImpl(startMappingInverted, targetVertices, sourceVertices); |
| 123 | + DiffImpl.OptimalEdit invertedBackOptimalEdit = new DiffImpl.OptimalEdit(sourceGraph, targetGraph, optimalEdit.mapping.invert(), optimalEdit.ged); |
| 124 | + return invertedBackOptimalEdit; |
| 125 | + |
| 126 | + } else { |
| 127 | + System.out.println("Insert heavy ... normal way"); |
| 128 | + sortVerticesEdgeCountDescending(nonMappedSource, sourceGraph); |
| 129 | + |
| 130 | + List<Vertex> sourceVertices = new ArrayList<>(); |
| 131 | + sourceVertices.addAll(possibleMappings.fixedOneToOneSources); |
| 132 | + sourceVertices.addAll(nonMappedSource); |
| 133 | + |
| 134 | + List<Vertex> targetVertices = new ArrayList<>(); |
| 135 | + targetVertices.addAll(possibleMappings.fixedOneToOneTargets); |
| 136 | + targetVertices.addAll(nonMappedTarget); |
| 137 | + |
| 138 | + DiffImpl diffImpl = new DiffImpl(sourceGraph, targetGraph, possibleMappings, runningCheck); |
| 139 | + DiffImpl.OptimalEdit optimalEdit = diffImpl.diffImpl(startMapping, sourceVertices, targetVertices); |
| 140 | + return optimalEdit; |
| 141 | + } |
88 | 142 | } |
89 | 143 |
|
90 | 144 | private void sortSourceVertices(List<Vertex> sourceVertices, PossibleMappingsCalculator.PossibleMappings possibleMappings) { |
91 | 145 | Collections.sort(sourceVertices, (v1, v2) -> |
92 | 146 | { |
93 | | -// int v1Count = possibleMappings.possibleMappings.get(v1).size(); |
94 | | -// int v2Count = possibleMappings.possibleMappings.get(v2).size(); |
95 | | -// int result = Integer.compare(v1Count, v2Count); |
96 | | -// if(result == 0) { |
97 | | - return Integer.compare(sourceGraph.adjacentEdgesAndInverseCount(v2), sourceGraph.adjacentEdgesAndInverseCount(v1)); |
| 147 | + int v1Count = possibleMappings.possibleMappings.get(v1).size(); |
| 148 | + int v2Count = possibleMappings.possibleMappings.get(v2).size(); |
| 149 | +// if (v1Count == v2Count) { |
| 150 | +// return Integer.compare(sourceGraph.adjacentEdgesAndInverseCount(v1), sourceGraph.adjacentEdgesAndInverseCount(v2)); |
98 | 151 | // } |
99 | | -// return result; |
| 152 | + int result = Integer.compare(v1Count, v2Count); |
| 153 | + return result; |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + private void sortSourceVerticesPossibleMappingDescending(List<Vertex> sourceVertices, PossibleMappingsCalculator.PossibleMappings possibleMappings) { |
| 158 | + Collections.sort(sourceVertices, (v1, v2) -> |
| 159 | + { |
| 160 | + int v1Count = possibleMappings.possibleMappings.get(v1).size(); |
| 161 | + int v2Count = possibleMappings.possibleMappings.get(v2).size(); |
| 162 | + int result = Integer.compare(v2Count, v1Count); |
| 163 | + return result; |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + private void sortVerticesEdgeCountDescending(List<Vertex> vertices, SchemaGraph schemaGraph) { |
| 168 | + Collections.sort(vertices, (v1, v2) -> |
| 169 | + { |
| 170 | + return Integer.compare(schemaGraph.adjacentEdgesAndInverseCount(v2), schemaGraph.adjacentEdgesAndInverseCount(v1)); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | +// private void sortSourceVerticesDeleteHeavy(List<Vertex> sourceVertices, PossibleMappingsCalculator.PossibleMappings possibleMappings) { |
| 175 | +// Collections.sort(sourceVertices, (v1, v2) -> |
| 176 | +// { |
| 177 | +// int targetIsolated1 = (int) possibleMappings.possibleMappings.get(v1).stream().filter(Vertex::isIsolated).count(); |
| 178 | +// int targetIsolated2 = (int) possibleMappings.possibleMappings.get(v2).stream().filter(Vertex::isIsolated).count(); |
| 179 | +// return Integer.compare(targetIsolated1, targetIsolated2); |
| 180 | +// }); |
| 181 | +// } |
| 182 | + |
| 183 | + |
| 184 | + private void sortSourceVerticesEdgeCountAscending(List<Vertex> sourceVertices, PossibleMappingsCalculator.PossibleMappings possibleMappings) { |
| 185 | + Collections.sort(sourceVertices, (v1, v2) -> |
| 186 | + { |
| 187 | + return Integer.compare(sourceGraph.adjacentEdgesAndInverseCount(v1), sourceGraph.adjacentEdgesAndInverseCount(v2)); |
100 | 188 | }); |
101 | 189 | } |
102 | 190 |
|
|
0 commit comments