(*
 * $Id$
 *
 * JSDAI(TM), a way to implement STEP, ISO 10303
 * Copyright (C) 1997-2008, LKSoftWare GmbH, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License
 * version 3 as published by the Free Software Foundation (AGPL v3).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * JSDAI is a registered trademark of LKSoftWare GmbH, Germany
 * This software is also available under commercial licenses.
 * See also http://www.jsdai.net/
 *)

(*
   Derived from ISO TC184/SC4/WG12 N - ISO/CD-TS 10303-xxxx Physical node requirement to implementing component allocation - EXPRESS ARM
   by LKSoftWare GmbH for implementation purpose within IDA-STEP(TM)
*)

SCHEMA Physical_node_requirement_to_implementing_component_allocation_xim;

USE FROM Physical_connectivity_definition_xim;

USE FROM Assembly_module_with_packaged_connector_component_xim;

USE FROM Assembly_technology_xim;

USE FROM Physical_node_requirement_to_implementing_component_allocation_mim;

  ENTITY Physical_node_requirement_to_implementing_component_allocation_armx
  	(* CONNOTATIONAL *) SUBTYPE OF (physical_node_requirement_to_implementing_component_allocation);
      SELF\shape_aspect_relationship.relating_shape_aspect RENAMED physical_node_requirement	: Physical_connectivity_definition_armx;
      implementation_component           																											: Assembly_component_armx;
      implementation_interface_joint_set 																											: OPTIONAL SET[1:?] OF Interface_mounted_join_armx;
      implementation_joint_set           																											: OPTIONAL SET[1:?] OF Assembly_joint_armx;
    DERIVE
      implementation_connection_definition_1 : SET[0:1] OF part_connected_terminals_definition_armx := get_pctd(implementation_component,
 implementation_joint_set);
      implementation_connection_definition_2 : SET[0:1] OF packaged_connector_terminal_relationship_armx := get_pctr(implementation_component,
 implementation_interface_joint_set);
      SELF\shape_aspect.of_shape : product_definition_shape := ?;
      SELF\shape_aspect_relationship.related_shape_aspect : shape_aspect := ?;
      SELF\shape_aspect_relationship.description : text := ?;
      SELF\shape_aspect.product_definitional : LOGICAL := ?;
      SELF\shape_aspect_relationship.name : label := ?;
    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_XIM.INTERFACE_COMPONENT_ARMX' IN TYPEOF(implementation_component)) AND 
             ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_XIM.PHYSICAL_COMPONENT_INTERFACE_TERMINAL_ARMX' IN TYPEOF(get_cit(implementation_component,implementation_joint_set)))) OR 
            ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_ARM.ROUTED_INTERCONNECT_COMPONENT_ARMX' IN TYPEOF(implementation_component));
      WR3 : SIZEOF(QUERY(pct <* physical_node_requirement.associated_terminals | pct\Component_feature_armx.associated_component(* TEMP-CF *) = implementation_component)) = 0;
  END_ENTITY;

  FUNCTION get_cit(
    ac  : Assembly_component_armx ;
    ajb : SET OF Assembly_joint_armx
  ) : Physical_component_interface_terminal_armx;

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

    REPEAT i := 1 TO 2 BY 1;
      IF ac :=: cf[i].associated_component THEN
        IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_XIM.INTERFACE_COMPONENT_ARMX' 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_armx ;
    ajb : SET OF Assembly_joint_armx
  ) : SET OF Part_connected_terminals_definition_armx;

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

    REPEAT i := 1 TO 2 BY 1;
      IF ac :=: cf[i].associated_component THEN
        IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_XIM.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_armx ;
    imjb : SET OF Interface_mounted_join_armx
  ) : SET OF Packaged_connector_terminal_relationship_armx;

    LOCAL
      cf   : Component_feature_armx;
      pctr : SET OF packaged_connector_terminal_relationship_armx := [];
    END_LOCAL;

    cf := imjb[1].connection_to_interconnect_module_component;
    IF ac :=: cf.associated_component THEN
      IF ('PHYSICAL_NODE_REQUIREMENT_TO_IMPLEMENTING_COMPONENT_ALLOCATION_xim.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;  



