Esempio n. 1
0
        def add_applier(apl: 'AttributeResolutionApplier') -> bool:
            if not self.applier_caps:
                self.applier_caps = AttributeResolutionApplierCapabilities()

            # Collect the code that will perform the right action. Associate with the resolved trait and get the priority.
            if apl.will_attribute_modify and apl.do_attribute_modify:
                self.actions_modify.append(apl)
                self.applier_caps.can_attribute_modify = True

            if apl.will_attribute_add and apl.do_attribute_add:
                self.actions_attribute_add.append(apl)
                self.applier_caps.can_attribute_add = True

            if apl.will_group_add and apl.do_group_add:
                self.actions_group_add.append(apl)
                self.applier_caps.can_group_add = True

            if apl.will_round_add and apl.do_round_add:
                self.actions_round_add.append(apl)
                self.applier_caps.can_round_add = True

            if apl.will_alter_directives and apl.do_alter_directives:
                self.applier_caps.can_alter_directives = True
                apl.do_alter_directives(self.res_opt, res_guide)

            if apl.will_create_context and apl.do_create_context:
                self.applier_caps.can_create_context = True

            if apl.will_remove:
                self.actions_remove.append(apl)
                self.applier_caps.can_remove = True

            return True
Esempio n. 2
0
    def __init__(self, res_opt: 'ResolveOptions', res_guide: 'CdmAttributeResolutionGuidanceDefinition', traits: 'ResolvedTraitSet') -> None:
        # Collect a set of appliers for all traits.
        self.res_opt = res_opt  # type: ResolveOptions
        self.res_guide = res_guide  # type: CdmAttributeResolutionGuidanceDefinition
        self.traits_to_apply = traits  # type: ResolvedTraitSet

        self.actions_modify = []  # type: List[AttributeResolutionApplier]
        self.actions_group_add = []  # type: List[AttributeResolutionApplier]
        self.actions_round_add = []  # type: List[AttributeResolutionApplier]
        self.actions_attribute_add = []  # type: List[AttributeResolutionApplier]
        self.actions_remove = []  # type: List[AttributeResolutionApplier]
        self.applier_caps = None  # type: Optional[AttributeResolutionApplierCapabilities]

        from cdm.objectmodel import CdmObject
        self.res_opt = CdmObject._copy_resolve_options(res_opt)

        if not res_guide:
            return

        if not self.applier_caps:
            self.applier_caps = AttributeResolutionApplierCapabilities()

        def add_applier(apl: 'AttributeResolutionApplier') -> bool:
            # Collect the code that will perform the right action.
            # Associate with the resolved trait and get the priority.

            if apl._will_attribute_modify and apl._do_attribute_modify:
                self.actions_modify.append(apl)
                self.applier_caps.can_attribute_modify = True

            if apl._will_attribute_add and apl._do_attribute_add:
                self.actions_attribute_add.append(apl)
                self.applier_caps.can_attribute_add = True

            if apl._will_group_add and apl._do_group_add:
                self.actions_group_add.append(apl)
                self.applier_caps.can_group_add = True

            if apl._will_round_add and apl._do_round_add:
                self.actions_round_add.append(apl)
                self.applier_caps.can_round_add = True

            if apl._will_alter_directives and apl._do_alter_directives:
                self.applier_caps.can_alter_directives = True
                apl._do_alter_directives(self.res_opt, res_guide)

            if apl._will_create_context and apl._do_create_context:
                self.applier_caps.can_create_context = True

            if apl._will_remove:
                self.actions_remove.append(apl)
                self.applier_caps.can_remove = True

            return True

        if res_guide.remove_attribute:
            add_applier(primitive_appliers._is_removed)
        if res_guide.imposed_directives:
            add_applier(primitive_appliers._does_impose_directives)
        if res_guide.removed_directives:
            add_applier(primitive_appliers._does_remove_directives)
        if res_guide.add_supporting_attribute:
            add_applier(primitive_appliers._does_add_supporting_attribute)
        if res_guide.rename_format:
            add_applier(primitive_appliers._does_disambiguate_names)
        if res_guide.cardinality == 'many':
            add_applier(primitive_appliers._does_explain_array)
        if res_guide.entity_by_reference:
            add_applier(primitive_appliers._does_reference_entity_via)
        if res_guide.selects_sub_attribute and res_guide.selects_sub_attribute.selects == 'one':
            add_applier(primitive_appliers._does_select_attributes)

        # Sorted by priority.
        self.actions_modify.sort(key=lambda ara: ara._priority)
        self.actions_group_add.sort(key=lambda ara: ara._priority)
        self.actions_round_add.sort(key=lambda ara: ara._priority)
        self.actions_attribute_add.sort(key=lambda ara: ara._priority)