Example #1
0
def _copy_characteristics (topic, target_topic, merge_map):
    """Copies the occurrences and names from `topic` to the `target_topic`.

    :param topic: the topic to take the characteristics from
    :type topic: `Topic`
    :param target_topic: the topic that receives the characteristics
    :type target_topic: `Topic`
    :param merge_map: the map that holds the merge mappings
    :type merge_map: dictionary

    """
    signatures = {}
    for occurrence in target_topic.get_occurrences():
        signature = generate_occurrence_signature(occurrence)
        signatures[signature] = occurrence
    topic_map = target_topic.get_topic_map()
    for occurrence in topic.get_occurrences():
        occ_type = _copy_type(occurrence, topic_map, merge_map)
        scope = _copy_scope(occurrence, topic_map, merge_map)
        target_occurrence = target_topic.create_occurrence(
            occ_type, occurrence.get_value(), scope, occurrence.get_datatype())
        signature = generate_occurrence_signature(target_occurrence)
        existing = signatures.get(signature)
        if existing is not None:
            target_occurrence.remove()
            target_occurrence = existing
        _copy_reifier(occurrence, target_occurrence, merge_map)
        _copy_item_identifiers(occurrence, target_occurrence)
    signatures = {}
    for name in target_topic.get_names():
        signature = generate_name_signature(name)
        signatures[signature] = name
    for name in topic.get_names():
        name_type = _copy_type(name, topic_map, merge_map)
        scope = _copy_scope(name, topic_map, merge_map)
        target_name = target_topic.create_name(name.get_value(), name_type,
                                               scope)
        signature = generate_name_signature(target_name)
        existing = signatures.get(signature)
        if existing is not None:
            target_name.remove()
            target_name = existing
        _copy_reifier(name, target_name, merge_map)
        _copy_item_identifiers(name, target_name)
        _copy_variants(name, target_name, merge_map)
Example #2
0
    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()
Example #3
0
    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()