(*
   $Id$
   ISO TC184/SC4/WG12 N4116 - ISO/TS 10303-1723 Physical node requirement to implementing component allocation - EXPRESS ARM
   Supersedes ISO TC184/SC4/WG12 N3540
*)

SCHEMA Physical_node_requirement_to_implementing_component_allocation_arm;

	USE FROM Assembly_module_with_packaged_connector_component_arm;	-- ISO/TS 10303-1645
	USE FROM Assembly_technology_arm;	-- ISO/TS 10303-1649
	USE FROM Physical_connectivity_definition_arm;	-- ISO/TS 10303-1755

  ENTITY Physical_node_requirement_to_implementing_component_allocation;
      physical_node_requirement          : Physical_connectivity_definition;
      implementation_component           : Assembly_component;
      implementation_interface_joint_set : OPTIONAL SET[1:?] OF Interface_mounted_join;
      implementation_joint_set           : OPTIONAL SET[1:?] OF Assembly_joint;
    DERIVE
      implementation_connection_definition_1 : SET[0:1] OF part_connected_terminals_definition := get_pctd(implementation_component,
 implementation_joint_set);
      implementation_connection_definition_2 : SET[0:1] OF packaged_connector_terminal_relationship := get_pctr(implementation_component,
 implementation_interface_joint_set);
    WHERE
      WR1 : EXISTS(implementation_interface_joint_set) XOR EXISTS(implementation_joint_set);
      WR2 : (SIZEOF(implementation_connection_definition_1) + SIZEOF(implementation_connection_definition_2) = 1) OR 
            (('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.INTERFACE_COMPONENT' IN TYPEOF(implementation_component)) AND 
             ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.PHYSICAL_COMPONENT_INTERFACE_TERMINAL' IN TYPEOF(get_cit(implementation_component,implementation_joint_set)))) OR 
            ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.ROUTED_INTERCONNECT_COMPONENT' IN TYPEOF(implementation_component));
      WR3 : SIZEOF(QUERY(pct <* physical_node_requirement.associated_terminals | pct\Component_feature.associated_component = implementation_component)) = 0;
  END_ENTITY;

  FUNCTION get_cit(
    ac  : Assembly_component ;
    ajb : SET OF Assembly_joint
  ) : Physical_component_interface_terminal;

    LOCAL
      cf  : LIST[1:2] OF component_feature := [ajb[1].assembly_feature_1,
                                               ajb[1].assembly_feature_2];
      cit : Physical_component_interface_terminal;
    END_LOCAL;

    REPEAT i := 1 TO 2 BY 1;
      IF ac :=: cf[i].associated_component THEN
        IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.INTERFACE_COMPONENT' IN TYPEOF(cf[i].associated_component)) THEN
          RETURN(cf[i]);
        ELSE
          RETURN(?);
        END_IF;
      ELSE
        RETURN(?);
      END_IF;
    END_REPEAT;
    RETURN(?);
  END_FUNCTION;

  FUNCTION get_pctd(
    ac  : Assembly_component ;
    ajb : SET OF Assembly_joint
  ) : SET OF Part_connected_terminals_definition;

    LOCAL
      cf  : LIST[1:2] OF component_feature := [ajb[1].assembly_feature_1,
                                               ajb[1].assembly_feature_2];
      pctd : SET OF part_connected_terminals_definition := [];
    END_LOCAL;

    REPEAT i := 1 TO 2 BY 1;
      IF ac :=: cf[i].associated_component THEN
        IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.PART_TERMINAL' IN TYPEOF(cf[i].definition)) THEN
          pctd := cf[i].definition.connection_requirement;
          RETURN(pctd);
        ELSE
          RETURN(pctd);
        END_IF;
      ELSE
        RETURN(pctd);
      END_IF;
    END_REPEAT;
    RETURN(?);
  END_FUNCTION;

  FUNCTION get_pctr(
    ac   : Assembly_component ;
    imjb : SET OF Interface_mounted_join
  ) : SET OF Packaged_connector_terminal_relationship;

    LOCAL
      cf   : Component_feature;
      pctr : SET OF packaged_connector_terminal_relationship := [];
    END_LOCAL;

    cf := imjb[1].connection_to_interconnect_module_component;
    IF ac :=: cf.associated_component THEN
      IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.PACKAGED_PART_INTERFACE_TERMINAL' IN TYPEOF(cf.definition)) THEN
        pctr := cf.definition.external_connector_function;
        RETURN(pctr);
      ELSE
        RETURN(pctr);
      END_IF;
    ELSE
      RETURN(pctr);
    END_IF;
  END_FUNCTION;

END_SCHEMA;  
