Пример #1
0
def merge_requirement_assignment(context, relationship_property_definitions,
                                 relationship_interface_definitions,
                                 requirement, our_requirement):
    our_capability = our_requirement.capability
    if our_capability is not None:
        requirement._raw['capability'] = deepcopy_with_locators(our_capability)

    our_node = our_requirement.node
    if our_node is not None:
        requirement._raw['node'] = deepcopy_with_locators(our_node)

    our_node_filter = our_requirement.node_filter
    if our_node_filter is not None:
        requirement._raw['node_filter'] = deepcopy_with_locators(
            our_node_filter._raw)

    our_relationship = our_requirement.relationship  # RelationshipAssignment
    if our_relationship is not None:
        # Make sure we have a dict
        if 'relationship' not in requirement._raw:
            requirement._raw['relationship'] = OrderedDict()
        elif not isinstance(requirement._raw['relationship'], dict):
            # Convert existing short form to long form
            the_type = requirement._raw['relationship']
            requirement._raw['relationship'] = OrderedDict()
            requirement._raw['relationship']['type'] = deepcopy_with_locators(
                the_type)

        merge_requirement_assignment_relationship(
            context, our_relationship, relationship_property_definitions,
            relationship_interface_definitions, requirement, our_relationship)
Пример #2
0
def merge_raw_operation_definition(context, raw_operation, our_operation,
                                   interface_name, presentation, type_name):
    if not isinstance(our_operation._raw, dict):
        # Convert short form to long form
        raw_operation['implementation'] = deepcopy_with_locators(
            our_operation._raw)
        return

    # Add/merge inputs
    our_operation_inputs = our_operation.inputs
    if our_operation_inputs:
        # Make sure we have the dict
        if ('inputs'
                not in raw_operation) or (raw_operation.get('inputs') is None):
            raw_operation['inputs'] = OrderedDict()

        merge_raw_input_definitions(context, raw_operation['inputs'],
                                    our_operation_inputs, interface_name,
                                    our_operation._name, presentation,
                                    type_name)

    # Override the description
    if our_operation._raw.get('description') is not None:
        raw_operation['description'] = deepcopy_with_locators(
            our_operation._raw['description'])

    # Add/merge implementation
    if our_operation._raw.get('implementation') is not None:
        if raw_operation.get('implementation') is not None:
            merge(raw_operation['implementation'],
                  deepcopy_with_locators(our_operation._raw['implementation']))
        else:
            raw_operation['implementation'] = \
                deepcopy_with_locators(our_operation._raw['implementation'])
Пример #3
0
def convert_interface_definition_from_type_to_raw_template(context, presentation):                  # pylint: disable=invalid-name
    raw = OrderedDict()

    # Copy default values for inputs
    interface_inputs = presentation._get_inputs(context)
    if interface_inputs is not None:
        raw['inputs'] = convert_parameter_definitions_to_values(context, interface_inputs)

    # Copy operations
    operations = presentation._get_operations(context)
    if operations:
        for operation_name, operation in operations.iteritems():
            raw[operation_name] = OrderedDict()
            description = operation.description
            if description is not None:
                raw[operation_name]['description'] = deepcopy_with_locators(description._raw)
            implementation = operation.implementation
            if implementation is not None:
                raw[operation_name]['implementation'] = deepcopy_with_locators(implementation._raw)
            inputs = operation.inputs
            if inputs is not None:
                raw[operation_name]['inputs'] = convert_parameter_definitions_to_values(context,
                                                                                        inputs)

    return raw
Пример #4
0
def merge_requirement_assignment(context, relationship_property_definitions,
                                 relationship_interface_definitions,
                                 requirement, our_requirement):
    our_capability = our_requirement.capability
    if our_capability is not None:
        requirement._raw['capability'] = deepcopy_with_locators(our_capability)

    our_node = our_requirement.node
    if our_node is not None:
        requirement._raw['node'] = deepcopy_with_locators(our_node)

    our_node_filter = our_requirement.node_filter
    if our_node_filter is not None:
        requirement._raw['node_filter'] = deepcopy_with_locators(
            our_node_filter._raw)

    our_relationship = our_requirement.relationship  # RelationshipAssignment
    if (our_relationship is not None) and (our_relationship.type is None):
        # Make sure we have a dict
        if 'relationship' not in requirement._raw:
            requirement._raw['relationship'] = OrderedDict()

        merge_requirement_assignment_relationship(
            context, our_relationship, relationship_property_definitions,
            relationship_interface_definitions, requirement, our_relationship)
Пример #5
0
def merge_capability_definition_from_type(context, presentation, capability_definition):
    """
    Merge ``properties`` and ``valid_source_types`` from the node type's capability definition
    over those taken from the parent node type.
    """
    raw_properties = OrderedDict()

    # Merge properties from parent
    the_type = capability_definition._get_type(context)
    type_property_defintions = the_type._get_properties(context)
    merge_raw_parameter_definitions(context, presentation, raw_properties, type_property_defintions,
                                    'properties')

    # Merge our properties (might override definitions in parent)
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    capability_definition.properties, 'properties')

    if raw_properties:
        capability_definition._raw['properties'] = raw_properties

    # Override valid_source_types
    if capability_definition._raw.get('valid_source_types') is None:
        valid_source_types = the_type._get_valid_source_types(context)
        if valid_source_types is not None:
            capability_definition._raw['valid_source_types'] = \
                deepcopy_with_locators(valid_source_types)
Пример #6
0
def merge_raw_input_definitions(context, raw_inputs, our_inputs,
                                interface_name, operation_name, presentation,
                                type_name):
    for input_name, our_input in our_inputs.iteritems():
        if input_name in raw_inputs:
            merge_raw_input_definition(context, raw_inputs[input_name],
                                       our_input, interface_name,
                                       operation_name, presentation, type_name)
        else:
            raw_inputs[input_name] = deepcopy_with_locators(our_input._raw)
Пример #7
0
def merge_interface(context, presentation, interface_assignment,
                    our_interface_assignment, interface_definition,
                    interface_name):
    # Assign/merge interface inputs
    assign_raw_inputs(context, interface_assignment._raw,
                      our_interface_assignment.inputs,
                      interface_definition._get_inputs(context),
                      interface_name, None, presentation)

    our_operation_templates = our_interface_assignment.operations  # OperationAssignment
    if our_operation_templates is None:
        our_operation_templates = {}

    # OperationDefinition or OperationAssignment:
    operation_definitions = interface_definition._get_operations(context) \
        if hasattr(interface_definition, '_get_operations') else interface_definition.operations
    if operation_definitions is None:
        operation_definitions = {}

    # OperationAssignment:
    for operation_name, our_operation_template in our_operation_templates.iteritems(
    ):
        operation_definition = operation_definitions.get(
            operation_name)  # OperationDefinition

        our_input_assignments = our_operation_template.inputs
        our_implementation = our_operation_template.implementation

        if operation_definition is None:
            context.validation.report(
                u'interface definition "{0}" refers to an unknown operation "{1}" in "{2}"'
                .format(interface_name, operation_name,
                        presentation._fullname),
                locator=our_operation_template._locator,
                level=Issue.BETWEEN_TYPES)

        # Make sure we have the dict
        if (operation_name not in interface_assignment._raw) \
            or (interface_assignment._raw[operation_name] is None):
            interface_assignment._raw[operation_name] = OrderedDict()

        if our_implementation is not None:
            interface_assignment._raw[operation_name]['implementation'] = \
                deepcopy_with_locators(our_implementation._raw)

        # Assign/merge operation inputs
        input_definitions = operation_definition.inputs \
            if operation_definition is not None else None
        assign_raw_inputs(context, interface_assignment._raw[operation_name],
                          our_input_assignments, input_definitions,
                          interface_name, operation_name, presentation)
Пример #8
0
def merge_raw_operation_definitions(context, raw_operations, our_operations,
                                    interface_name, presentation, type_name):
    for operation_name, our_operation in our_operations.iteritems():
        if operation_name in raw_operations:
            raw_operation = raw_operations[operation_name]
            if isinstance(raw_operation, basestring):
                # Convert short form to long form
                raw_operations[operation_name] = OrderedDict(
                    (('implementation', raw_operation), ))
                raw_operation = raw_operations[operation_name]
            merge_raw_operation_definition(context, raw_operation,
                                           our_operation, interface_name,
                                           presentation, type_name)
        else:
            raw_operations[operation_name] = deepcopy_with_locators(
                our_operation._raw)
Пример #9
0
def merge_raw_parameter_definitions(context, presentation,
                                    raw_property_definitions,
                                    our_property_definitions, field_name):
    if not our_property_definitions:
        return
    for property_name, our_property_definition in our_property_definitions.iteritems(
    ):
        if property_name in raw_property_definitions:
            raw_property_definition = raw_property_definitions[property_name]
            merge_raw_parameter_definition(context, presentation,
                                           raw_property_definition,
                                           our_property_definition, field_name,
                                           property_name)
        else:
            raw_property_definitions[property_name] = \
                deepcopy_with_locators(our_property_definition._raw)
Пример #10
0
def merge_capability_definition(context, presentation, capability_definition,
                                from_capability_definition):
    capability_definition._raw['type'] = from_capability_definition.type

    raw_properties = OrderedDict()
    raw_attributes = OrderedDict()

    # Merge parameters from type
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    capability_definition.properties,
                                    'properties')
    merge_raw_parameter_definitions(context, presentation, raw_attributes,
                                    capability_definition.attributes,
                                    'attributes')

    # Merge our parameters
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    from_capability_definition.properties,
                                    'properties')
    merge_raw_parameter_definitions(context, presentation, raw_attributes,
                                    from_capability_definition.attributes,
                                    'attributes')

    if raw_properties:
        capability_definition._raw['properties'] = raw_properties
        capability_definition._reset_method_cache()
    if raw_attributes:
        capability_definition._raw['attributes'] = raw_attributes
        capability_definition._reset_method_cache()

    # Merge occurrences
    occurrences = from_capability_definition._raw.get('occurrences')
    if (occurrences is not None) and (
            capability_definition._raw.get('occurrences') is None):
        capability_definition._raw['occurrences'] = \
            deepcopy_with_locators(occurrences)
Пример #11
0
def merge_capability_definition_from_type(context, presentation,
                                          capability_definition):
    raw_properties = OrderedDict()

    # Merge properties from type
    the_type = capability_definition._get_type(context)
    type_property_defintions = the_type._get_properties(context)
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    type_property_defintions, 'properties')

    # Merge our properties
    merge_raw_parameter_definitions(context, presentation, raw_properties,
                                    capability_definition.properties,
                                    'properties')

    if raw_properties:
        capability_definition._raw['properties'] = raw_properties

    # Override valid_source_types
    if capability_definition._raw.get('valid_source_types') is None:
        valid_source_types = the_type._get_valid_source_types(context)
        if valid_source_types is not None:
            capability_definition._raw['valid_source_types'] = \
                deepcopy_with_locators(valid_source_types)
Пример #12
0
def merge_requirement_assignment_relationship(context, presentation,
                                              property_definitions,
                                              interface_definitions,
                                              requirement, our_relationship):
    the_type = our_relationship.type
    if the_type is not None:
        # Could be a type or a template:
        requirement._raw['relationship']['type'] = deepcopy_with_locators(
            the_type)

    our_relationship_properties = our_relationship._raw.get('properties')
    if our_relationship_properties:
        # Make sure we have a dict
        if 'properties' not in requirement._raw['relationship']:
            requirement._raw['relationship']['properties'] = OrderedDict()

        # Merge our properties
        for property_name, prop in our_relationship_properties.iteritems():
            if property_name in property_definitions:
                definition = property_definitions[property_name]
                requirement._raw['relationship']['properties'][property_name] = \
                    coerce_property_value(context, presentation, definition, prop)
            else:
                context.validation.report(
                    'relationship property "%s" not declared at definition of requirement "%s"'
                    ' in "%s"' %
                    (property_name, presentation._fullname,
                     presentation._container._container._fullname),
                    locator=our_relationship._get_child_locator(
                        'properties', property_name),
                    level=Issue.BETWEEN_TYPES)

    our_interfaces = our_relationship.interfaces
    if our_interfaces:
        # Make sure we have a dict
        if 'interfaces' not in requirement._raw['relationship']:
            requirement._raw['relationship']['interfaces'] = OrderedDict()

        # Merge interfaces
        for interface_name, our_interface in our_interfaces.iteritems():
            if interface_name not in requirement._raw['relationship'][
                    'interfaces']:
                requirement._raw['relationship']['interfaces'][
                    interface_name] = OrderedDict()

            if (interface_definitions
                    is not None) and (interface_name in interface_definitions):
                interface_definition = interface_definitions[interface_name]
                interface_assignment = requirement.relationship.interfaces[
                    interface_name]
                merge_interface(context, presentation, interface_assignment,
                                our_interface, interface_definition,
                                interface_name)
            else:
                context.validation.report(
                    'interface definition "%s" not declared at definition of requirement "%s"'
                    ' in "%s"' %
                    (interface_name, presentation._fullname,
                     presentation._container._container._fullname),
                    locator=our_relationship._locator,
                    level=Issue.BETWEEN_TYPES)
Пример #13
0
def convert_requirement_from_definition_to_assignment(
        context,
        requirement_definition,  # pylint: disable=too-many-branches
        our_requirement_assignment,
        container):
    from ..assignments import RequirementAssignment

    raw = OrderedDict()

    # Capability type name:
    raw['capability'] = deepcopy_with_locators(
        requirement_definition.capability)

    node_type = requirement_definition._get_node_type(context)
    if node_type is not None:
        raw['node'] = deepcopy_with_locators(node_type._name)

    relationship_type = None
    relationship_template = None
    relationship_property_definitions = None
    relationship_interface_definitions = None

    # First try to find the relationship if we declared it
    # RelationshipAssignment:
    our_relationship = our_requirement_assignment.relationship \
        if our_requirement_assignment is not None else None
    if our_relationship is not None:
        relationship_type, relationship_type_variant = our_relationship._get_type(
            context)
        if relationship_type_variant == 'relationship_template':
            relationship_template = relationship_type
            relationship_type = relationship_template._get_type(context)

    definition_relationship_type = None
    relationship_definition = requirement_definition.relationship  # RelationshipDefinition
    if relationship_definition is not None:
        definition_relationship_type = relationship_definition._get_type(
            context)

    # If not exists, try at the node type
    if relationship_type is None:
        relationship_type = definition_relationship_type
    else:
        # Make sure the type is derived
        if not definition_relationship_type._is_descendant(
                context, relationship_type):
            context.validation.report(
                'assigned relationship type "%s" is not a descendant of declared relationship type "%s"'
                %
                (relationship_type._name, definition_relationship_type._name),
                locator=container._locator,
                level=Issue.BETWEEN_TYPES)

    if relationship_type is not None:
        raw['relationship'] = OrderedDict()

        type_name = our_relationship.type if our_relationship is not None else None
        if type_name is None:
            type_name = relationship_type._name

        raw['relationship']['type'] = deepcopy_with_locators(type_name)

        # These are our property definitions
        relationship_property_definitions = relationship_type._get_properties(
            context)

        if relationship_template is not None:
            # Property values from template
            raw['properties'] = relationship_template._get_property_values(
                context)
        else:
            if relationship_property_definitions:
                # Convert property definitions to values
                raw['properties'] = \
                    convert_property_definitions_to_values(context,
                                                           relationship_property_definitions)

        # These are our interface definitions
        # InterfaceDefinition:
        relationship_interface_definitions = OrderedDict(
            relationship_type._get_interfaces(context))

        # Convert interface definitions to templates
        convert_requirement_interface_definitions_from_type_to_raw_template(
            context, raw['relationship'], relationship_interface_definitions)

        if relationship_definition:
            # Merge extra interface definitions
            # InterfaceDefinition:
            relationship_interface_definitions = relationship_definition.interfaces
            merge_interface_definitions(context,
                                        relationship_interface_definitions,
                                        relationship_interface_definitions,
                                        requirement_definition, container)

        if relationship_template is not None:
            # Interfaces from template
            interfaces = relationship_template._get_interfaces(context)
            if interfaces:
                raw['relationship']['interfaces'] = OrderedDict()
                for interface_name, interface in interfaces.iteritems():
                    raw['relationship']['interfaces'][
                        interface_name] = interface._raw

    return \
        RequirementAssignment(name=requirement_definition._name, raw=raw, container=container), \
        relationship_property_definitions, \
        relationship_interface_definitions