DuplicateSuppressionUtils { public static void removeDuplicates(TopicMapIF topicmap) { int batchSize = 50; Iterator<TopicIF> it = topicmap.getTopics().iterator(); while (it.hasNext()) { List<TopicIF> batch = CollectionUtils.nextBatch(it, batchSize); prefetchTopics(topicmap, batch); Iterator<TopicIF> iter = batch.iterator(); while (iter.hasNext()) { removeDuplicates(iter.next()); } } ClassInstanceIndexIF cindex = (ClassInstanceIndexIF)topicmap.getIndex("net.ontopia.topicmaps.core.index.ClassInstanceIndexIF"); Collection<AssociationIF> assocs; Iterator<TopicIF> atypes = new ArrayList<TopicIF>(cindex.getAssociationTypes()).iterator(); while (atypes.hasNext()) { TopicIF atype = atypes.next(); assocs = cindex.getAssociations(atype); if (!assocs.isEmpty()) removeDuplicateAssociations(assocs); } assocs = cindex.getAssociations(null); if (!assocs.isEmpty()) removeDuplicateAssociations(assocs); }  static void removeDuplicates(TopicMapIF topicmap); static void removeDuplicates(TopicIF topic); static void removeDuplicateTopicNames(Collection<TopicNameIF> basenames); static void removeDuplicateOccurrences(Collection<OccurrenceIF> occurs); static void removeDuplicateAssociations(Collection<AssociationIF> assocs); static void removeDuplicates(TopicNameIF basename); static void removeDuplicates(AssociationIF assoc); static Map<AssociationIF, Set<AssociationIF>> removeDuplicateAssociations(TopicIF topic);  }
@Test public void testVariantRemoval() { TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, ""); VariantNameIF vn = builder.makeVariantName(bn, "duplicate"); vn = builder.makeVariantName(bn, "duplicate"); DuplicateSuppressionUtils.removeDuplicates(bn); Assert.assertTrue("duplicate variant names were not removed", bn.getVariants().size() == 1); }
@Test public void testVariantRemovalWithScope() { TopicIF theme1 = builder.makeTopic(); TopicIF theme2 = builder.makeTopic(); TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, ""); VariantNameIF vn = builder.makeVariantName(bn, "duplicate"); vn.addTheme(theme1); vn.addTheme(theme2); vn = builder.makeVariantName(bn, "duplicate"); vn.addTheme(theme1); vn.addTheme(theme2); DuplicateSuppressionUtils.removeDuplicates(bn); Assert.assertTrue("duplicate variant names were not removed", bn.getVariants().size() == 1); }
@Test public void testTopicNameRemovalWithScope() { TopicIF theme1 = builder.makeTopic(); TopicIF theme2 = builder.makeTopic(); TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, "test"); bn.addTheme(theme1); bn.addTheme(theme2); VariantNameIF vn = builder.makeVariantName(bn, "not duplicate"); TopicNameIF bn2 = builder.makeTopicName(topic, "test"); bn2.addTheme(theme1); bn2.addTheme(theme2); vn = builder.makeVariantName(bn, "not duplicate, either"); DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate base names were not removed", topic.getTopicNames().size() == 1); bn = (TopicNameIF) topic.getTopicNames().iterator().next(); Assert.assertTrue("variant names were not merged", bn.getVariants().size() == 2); }
@Test public void testTopicNameAndVariantNameRemovalWithScope() { TopicIF theme1 = builder.makeTopic(); TopicIF theme2 = builder.makeTopic(); TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, "test"); bn.addTheme(theme1); bn.addTheme(theme2); VariantNameIF vn = builder.makeVariantName(bn, "duplicate"); TopicNameIF bn2 = builder.makeTopicName(topic, "test"); bn2.addTheme(theme1); bn2.addTheme(theme2); vn = builder.makeVariantName(bn, "duplicate"); DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate base names were not removed", topic.getTopicNames().size() == 1); bn = (TopicNameIF) topic.getTopicNames().iterator().next(); Assert.assertTrue("duplicate variant names were not removed", bn.getVariants().size() == 1); }
@Test public void testOccurrenceRemoval() { TopicIF type = builder.makeTopic(); TopicIF topic = builder.makeTopic(); OccurrenceIF occ = builder.makeOccurrence(topic, type, "duplicate"); occ = builder.makeOccurrence(topic, type, "duplicate"); DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate occurrence were not removed", topic.getOccurrences().size() == 1); }
@Test public void testAssociationRemoval() { TopicIF type = builder.makeTopic(); TopicIF role1 = builder.makeTopic(); TopicIF role2 = builder.makeTopic(); TopicIF player1 = builder.makeTopic(); TopicIF player2 = builder.makeTopic(); AssociationIF assoc = builder.makeAssociation(type); AssociationRoleIF role = builder.makeAssociationRole(assoc, role1, player1); role = builder.makeAssociationRole(assoc, role2, player2); assoc = builder.makeAssociation(type); role = builder.makeAssociationRole(assoc, role1, player1); role = builder.makeAssociationRole(assoc, role2, player2); DuplicateSuppressionUtils.removeDuplicates(topicmap); Assert.assertTrue("duplicate association was not removed", topicmap.getAssociations().size() == 1); }
@Test public void testAssociationRoleRemoval() { TopicIF type = builder.makeTopic(); TopicIF role1 = builder.makeTopic(); TopicIF role2 = builder.makeTopic(); TopicIF player1 = builder.makeTopic(); TopicIF player2 = builder.makeTopic(); AssociationIF assoc = builder.makeAssociation(type); AssociationRoleIF role = builder.makeAssociationRole(assoc, role1, player1); role = builder.makeAssociationRole(assoc, role2, player2); role = builder.makeAssociationRole(assoc, role2, player2); DuplicateSuppressionUtils.removeDuplicates(topicmap); Assert.assertTrue("duplicate association role was not removed", assoc.getRoles().size() == 2); }
@Test public void testTopicNameItemIds() throws MalformedURLException { TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, "test"); bn.addItemIdentifier(new URILocator("http: TopicNameIF bn2 = builder.makeTopicName(topic, "test"); bn2.addItemIdentifier(new URILocator("http: DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate base names were not removed", topic.getTopicNames().size() == 1); bn = (TopicNameIF) topic.getTopicNames().iterator().next(); Assert.assertTrue("item IDs were not merged", bn.getItemIdentifiers().size() == 2); }
@Test public void testOccurrenceItemIds() throws MalformedURLException { TopicIF type = builder.makeTopic(); TopicIF topic = builder.makeTopic(); OccurrenceIF occ = builder.makeOccurrence(topic, type, "duplicate"); occ.addItemIdentifier(new URILocator("http: occ = builder.makeOccurrence(topic, type, "duplicate"); occ.addItemIdentifier(new URILocator("http: DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate occurrence were not removed", topic.getOccurrences().size() == 1); occ = (OccurrenceIF) topic.getOccurrences().iterator().next(); Assert.assertTrue("item IDs were not merged", occ.getItemIdentifiers().size() == 2); }
@Test public void testTopicNameReifiers() { TopicIF topic = builder.makeTopic(); TopicNameIF bn = builder.makeTopicName(topic, "test"); TopicIF r1 = builder.makeTopic(); bn.setReifier(r1); builder.makeTopicName(r1, "r1"); TopicNameIF bn2 = builder.makeTopicName(topic, "test"); TopicIF r2 = builder.makeTopic(); bn2.setReifier(r2); builder.makeTopicName(r2, "r2"); DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate base names were not removed", topic.getTopicNames().size() == 1); bn = (TopicNameIF) topic.getTopicNames().iterator().next(); r1 = bn.getReifier(); Assert.assertTrue("reifier was lost", r1 != null); Assert.assertTrue("reifiers were not merged", r1.getTopicNames().size() == 2); }
@Test public void testOccurrenceReifiers() { TopicIF type = builder.makeTopic(); TopicIF topic = builder.makeTopic(); OccurrenceIF occ = builder.makeOccurrence(topic, type, "test"); TopicIF r1 = builder.makeTopic(); occ.setReifier(r1); builder.makeTopicName(r1, "r1"); OccurrenceIF occ2 = builder.makeOccurrence(topic, type, "test"); TopicIF r2 = builder.makeTopic(); occ2.setReifier(r2); builder.makeTopicName(r2, "r2"); DuplicateSuppressionUtils.removeDuplicates(topic); Assert.assertTrue("duplicate occurrences were not removed", topic.getOccurrences().size() == 1); occ = (OccurrenceIF) topic.getOccurrences().iterator().next(); r1 = occ.getReifier(); Assert.assertTrue("reifier was lost", r1 != null); Assert.assertTrue("reifiers were not merged", r1.getTopicNames().size() == 2); }
@Test public void testAssociationReifiers() { TopicIF type = builder.makeTopic(); TopicIF role1 = builder.makeTopic(); TopicIF role2 = builder.makeTopic(); TopicIF player1 = builder.makeTopic(); TopicIF player2 = builder.makeTopic(); AssociationIF assoc = builder.makeAssociation(type); AssociationRoleIF role = builder.makeAssociationRole(assoc, role1, player1); role = builder.makeAssociationRole(assoc, role2, player2); TopicIF r1 = builder.makeTopic(); assoc.setReifier(r1); builder.makeTopicName(r1, "r1"); assoc = builder.makeAssociation(type); role = builder.makeAssociationRole(assoc, role1, player1); role = builder.makeAssociationRole(assoc, role2, player2); TopicIF r2 = builder.makeTopic(); assoc.setReifier(r2); builder.makeTopicName(r2, "r2"); DuplicateSuppressionUtils.removeDuplicates(topicmap); Assert.assertTrue("duplicate association was not removed", topicmap.getAssociations().size() == 1); assoc = (AssociationIF) topicmap.getAssociations().iterator().next(); Assert.assertTrue("reifier was lost", assoc.getReifier() != null); Assert.assertTrue("reifiers were not merged", assoc.getReifier().getTopicNames().size() == 2); }