def _copy_associations (source, target, merge_map): """Copies the associations from `source` topic map to `target` topic map. :param source: the topic map to take the associations from :type source: `TopicMap` :param target: the topic map that receives the associations :type target: `TopicMap` :param merge_map: the map that holds the merge mappings :type merge_map: dictionary """ signatures = {} for association in target.get_associations(): signature = generate_association_signature(association) signatures[signature] = association for association in source.get_associations(): association_type = _copy_type(association, target, merge_map) scope = _copy_scope(association, target, merge_map) target_association = target.create_association(association_type, scope) for role in association.get_roles(): role_type = _copy_type(role, target, merge_map) source_player = role.get_player() if source_player in merge_map: player = merge_map.get(source_player) else: player = _copy_topic(source_player, target, merge_map) target_role = target_association.create_role(role_type, player) _copy_item_identifiers(role, target_role) _copy_reifier(role, target_role, merge_map) signature = generate_association_signature(target_association) existing = signatures.get(signature) if existing is not None: move_role_characteristics(target_association, existing) target_association.remove() target_association = existing _copy_reifier(association, target_association, merge_map) _copy_item_identifiers(association, target_association)
def merge_in (self, other): """Merges another topic into this topic. Merging a topic into this topic causes this topic to gain all of the characteristics of the other topic and to replace the other topic wherever it is used as type, theme, or reifier. After this method completes, `other` will have been removed from the `TopicMap`. If `self` equals `other` no changes are made to the topic. NOTE: The other topic must belong to the same `TopicMap` instance as this topic. :param other: the topic to be merged into this topic :type other: `Topic` """ if other is None: raise ModelConstraintException( self, 'The topic to merge in may not be None') if other == self: return if self.topic_map != other.topic_map: raise ModelConstraintException( self, 'The topic to merge in is not from the same topic map') other_reified = other.get_reified() if self.get_reified() is not None and other_reified is not None: raise ModelConstraintException( self, 'Both topics are being used as reifiers') if other_reified is not None: other_reified.set_reifier(self) for topic_type in other.get_types(): self.add_type(topic_type) for subject_identifier in other.get_subject_identifiers(): subject_identifier.topic = self subject_identifier.save() for subject_locator in other.get_subject_locators(): subject_locator.topic = self subject_locator.save() for item_identifier in other.get_item_identifiers(): other.item_identifiers.remove(item_identifier) self.item_identifiers.add(item_identifier) signatures = {} for name in self.get_names(): signature = generate_name_signature(name) signatures[signature] = name for name in other.get_names(): signature = generate_name_signature(name) existing = signatures.get(signature) if existing is not None: handle_existing_construct(name, existing) move_variants(name, existing) name.remove() else: name.topic = self name.save() signatures = {} for occurrence in self.get_occurrences(): signature = generate_occurrence_signature(occurrence) signatures[signature] = occurrence for occurrence in other.get_occurrences(): signature = generate_occurrence_signature(occurrence) existing = signatures.get(signature) if existing is not None: handle_existing_construct(occurrence, existing) occurrence.remove() else: occurrence.topic = self occurrence.save() signatures = {} for role in self.get_roles_played(): parent = role.get_parent() signature = generate_association_signature(parent) signatures[signature] = parent for role in other.get_roles_played(): role.set_player(self) parent = role.get_parent() signature = generate_association_signature(parent) existing = signatures.get(signature) if existing is not None: handle_existing_construct(parent, existing) move_role_characteristics(parent, existing) parent.remove() other.remove()
def merge_in(self, other): """Merges another topic into this topic. Merging a topic into this topic causes this topic to gain all of the characteristics of the other topic and to replace the other topic wherever it is used as type, theme, or reifier. After this method completes, `other` will have been removed from the `TopicMap`. If `self` equals `other` no changes are made to the topic. NOTE: The other topic must belong to the same `TopicMap` instance as this topic. :param other: the topic to be merged into this topic :type other: `Topic` """ if other is None: raise ModelConstraintException( self, 'The topic to merge in may not be None') if other == self: return if self.topic_map != other.topic_map: raise ModelConstraintException( self, 'The topic to merge in is not from the same topic map') other_reified = other.get_reified() if self.get_reified() is not None and other_reified is not None: raise ModelConstraintException( self, 'Both topics are being used as reifiers') if other_reified is not None: other_reified.set_reifier(self) for topic_type in other.get_types(): self.add_type(topic_type) for subject_identifier in other.get_subject_identifiers(): subject_identifier.topic = self subject_identifier.save() for subject_locator in other.get_subject_locators(): subject_locator.topic = self subject_locator.save() for item_identifier in other.get_item_identifiers(): other.item_identifiers.remove(item_identifier) self.item_identifiers.add(item_identifier) signatures = {} for name in self.get_names(): signature = generate_name_signature(name) signatures[signature] = name for name in other.get_names(): signature = generate_name_signature(name) existing = signatures.get(signature) if existing is not None: handle_existing_construct(name, existing) move_variants(name, existing) name.remove() else: name.topic = self name.save() signatures = {} for occurrence in self.get_occurrences(): signature = generate_occurrence_signature(occurrence) signatures[signature] = occurrence for occurrence in other.get_occurrences(): signature = generate_occurrence_signature(occurrence) existing = signatures.get(signature) if existing is not None: handle_existing_construct(occurrence, existing) occurrence.remove() else: occurrence.topic = self occurrence.save() signatures = {} for role in self.get_roles_played(): parent = role.get_parent() signature = generate_association_signature(parent) signatures[signature] = parent for role in other.get_roles_played(): role.set_player(self) parent = role.get_parent() signature = generate_association_signature(parent) existing = signatures.get(signature) if existing is not None: handle_existing_construct(parent, existing) move_role_characteristics(parent, existing) parent.remove() other.remove()