IndDeduplicator { public static Set<InclusionDependency> deduplicateColumnIdentifier(InclusionDependency ind) { Set<InclusionDependency> dedupedInds = new HashSet<>(); Set<Integer> dependantIndices = getDuplicateIndices(ind.getDependant().getColumnIdentifiers()); Set<Integer> referencedIndices = getDuplicateIndices(ind.getReferenced().getColumnIdentifiers()); if (dependantIndices.isEmpty() && referencedIndices.isEmpty()) { dedupedInds.add(ind); } else { dedupedInds.addAll(deduplicateColumnIdentifier(ind, dependantIndices)); dedupedInds.addAll(deduplicateColumnIdentifier(ind, referencedIndices)); } if (dedupedInds.size() == 1) { return dedupedInds; } Set<InclusionDependency> nextDedupedInds = new HashSet<>(dedupedInds); do { dedupedInds = new HashSet<>(nextDedupedInds); nextDedupedInds = new HashSet<>(); for (InclusionDependency dedupedInd : dedupedInds) { nextDedupedInds.addAll(deduplicateColumnIdentifier(dedupedInd)); } } while (!nextDedupedInds.equals(dedupedInds)); return dedupedInds; }  static Set<InclusionDependency> deduplicateColumnIdentifier(InclusionDependency ind);  }
@Test void testDeduplication() { ColumnIdentifier a1 = new ColumnIdentifier("R", "A1"); ColumnIdentifier a2 = new ColumnIdentifier("R", "A2"); ColumnIdentifier a3 = new ColumnIdentifier("R", "A3"); ColumnIdentifier b1 = new ColumnIdentifier("S", "B1"); ColumnIdentifier b3 = new ColumnIdentifier("S", "B3"); InclusionDependency ind = new InclusionDependency( new ColumnPermutation(a1, a2, a3), new ColumnPermutation(b1, b1, b3)); Set<InclusionDependency> expectedInds = ImmutableSet.of( new InclusionDependency(new ColumnPermutation(a1, a3), new ColumnPermutation(b1, b3)), new InclusionDependency(new ColumnPermutation(a2, a3), new ColumnPermutation(b1, b3))); Set<InclusionDependency> dedupedInds = IndDeduplicator.deduplicateColumnIdentifier(ind); assertThat(dedupedInds).isEqualTo(expectedInds); }
@Test void test4ary() { ColumnIdentifier c1 = new ColumnIdentifier("c", "1"); ColumnIdentifier c4 = new ColumnIdentifier("c", "4"); ColumnIdentifier c5 = new ColumnIdentifier("c", "5"); ColumnIdentifier c12 = new ColumnIdentifier("c", "12"); ColumnIdentifier d1 = new ColumnIdentifier("d", "1"); ColumnIdentifier d3 = new ColumnIdentifier("d", "3"); ColumnIdentifier d4 = new ColumnIdentifier("d", "4"); InclusionDependency ind = new InclusionDependency(new ColumnPermutation(c4, c12, c1, c5), new ColumnPermutation(d3, d1, d4, d1)); Set<InclusionDependency> expectedInds = ImmutableSet.of( new InclusionDependency(new ColumnPermutation(c4, c1, c5), new ColumnPermutation(d3, d4, d1)), new InclusionDependency(new ColumnPermutation(c4, c12, c1), new ColumnPermutation(d3, d1, d4))); Set<InclusionDependency> dedupedInds = IndDeduplicator.deduplicateColumnIdentifier(ind); assertThat(dedupedInds).isEqualTo(expectedInds); }
@Test void testMultipleDuplications() { ColumnIdentifier a1 = new ColumnIdentifier("R", "A1"); ColumnIdentifier a2 = new ColumnIdentifier("R", "A2"); ColumnIdentifier b1 = new ColumnIdentifier("S", "B1"); ColumnIdentifier b2 = new ColumnIdentifier("S", "B2"); ColumnIdentifier b3 = new ColumnIdentifier("S", "B3"); ColumnIdentifier b4 = new ColumnIdentifier("S", "B4"); InclusionDependency ind = new InclusionDependency( new ColumnPermutation(a1, a1, a2, a2), new ColumnPermutation(b1, b2, b3, b4)); Set<InclusionDependency> expectedInds = ImmutableSet.of( new InclusionDependency(new ColumnPermutation(a1, a2), new ColumnPermutation(b1, b3)), new InclusionDependency(new ColumnPermutation(a1, a2), new ColumnPermutation(b1, b4)), new InclusionDependency(new ColumnPermutation(a1, a2), new ColumnPermutation(b2, b3)), new InclusionDependency(new ColumnPermutation(a1, a2), new ColumnPermutation(b2, b4))); Set<InclusionDependency> dedupedInds = IndDeduplicator.deduplicateColumnIdentifier(ind); assertThat(dedupedInds).isEqualTo(expectedInds); }