(*
 * $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 Interconnect physical requirement allocation - EXPRESS ARM
   by LKSoftWare GmbH for implementation purpose within IDA-STEP(TM)
*)

 SCHEMA Interconnect_physical_requirement_allocation_xim;

USE FROM Requirement_view_definition_relationship_xim; -- ISO/TS 10303-1142

USE FROM Interconnect_physical_requirement_allocation_mim;

USE FROM Integral_shield_xim;

REFERENCE FROM support_resource_schema   -- ISO 10303-41
  (bag_to_set);

REFERENCE FROM Requirement_decomposition_xim(get_rvd);  

  TYPE land_physical_template_or_inter_stratum_feature_template = SELECT
       (Land_physical_template,
        Inter_stratum_feature_template);
  END_TYPE;
  
  ENTITY Dependent_electrical_isolation_removal_template_armx
    SUBTYPE OF (Electrical_isolation_removal_template_armx, dependent_electrical_isolation_removal_template);
      associated_item : land_physical_template_or_inter_stratum_feature_template;
    WHERE
      WR1 : SIZEOF(QUERY (dtlipd <* USEDIN(SELF,'PART_TEMPLATE_2D_SHAPE_XIM.TEMPLATE_LOCATION_IN_STRUCTURED_TEMPLATE.TEMPLATE') | 
                  NOT(
                     (SIZEOF(QUERY (tlist <* USEDIN(associated_item,'PART_TEMPLATE_2D_SHAPE_XIM.TEMPLATE_LOCATION_IN_STRUCTURED_TEMPLATE.TEMPLATE') | 
                             NOT(
                                 (tlist\Template_location_in_structured_template.assembly :=: 
                                  dtlipd\Template_location_in_structured_template.assembly) AND
                                 ('PART_TEMPLATE_2D_SHAPE_XIM.DEPENDENT_TEMPLATE_LOCATION_IN_PADSTACK_DEFINITION' IN TYPEOF(dtlipd)) AND                                 
                                 (tlist :=: dtlipd\Dependent_template_location_in_padstack_definition.reference_location)
                                 )
                      )) = 0)
                      )
             )) = 0;      
  END_ENTITY;

  ENTITY Dependent_thermal_isolation_removal_template_armx
    SUBTYPE OF (Thermal_isolation_removal_template_armx, dependent_thermal_isolation_removal_template);
      associated_item : Land_physical_template_armx;
    WHERE
      WR1 : SIZEOF(QUERY (dtlipd <* USEDIN(SELF,'PART_TEMPLATE_2D_SHAPE_XIM.TEMPLATE_LOCATION_IN_STRUCTURED_TEMPLATE.TEMPLATE') | 
                  NOT(
                     (SIZEOF(QUERY (tlist <* USEDIN(associated_item,'PART_TEMPLATE_2D_SHAPE_XIM.TEMPLATE_LOCATION_IN_STRUCTURED_TEMPLATE.TEMPLATE') | 
                             NOT(
                                 (tlist\Template_location_in_structured_template.assembly :=: 
                                  dtlipd\Template_location_in_structured_template.assembly) AND
                                 ('PART_TEMPLATE_2D_SHAPE_XIM.DEPENDENT_TEMPLATE_LOCATION_IN_PADSTACK_DEFINITION' IN TYPEOF(dtlipd)) AND                                 
                                 (tlist :=: dtlipd\Dependent_template_location_in_padstack_definition.reference_location)
                                 )
                      )) = 0)
                      )
             )) = 0;      
  END_ENTITY;
  
  ENTITY Electrical_isolation_removal_template_armx
    (* CONNOTATIONAL *) SUBTYPE OF (Material_removal_feature_template_armx, electrical_isolation_removal_template);
    DERIVE
      electrical_isolation_spacing_requirement : SET[1:?] OF Requirement_view_definition := 
        get_rvd(SELF, 'electrical requirement');
    WHERE
      WR1: SIZEOF(QUERY(eisr <* electrical_isolation_spacing_requirement |
         NOT('INTERCONNECT_PHYSICAL_REQUIREMENT_ALLOCATION_XIM.ELECTRICAL_ISOLATION_REQUIREMENT'
        IN TYPEOF(eisr)) )) =0;
      WR2: SIZEOF(QUERY(s <* SELF\Geometric_template_armx.shapes |
        ('PART_TEMPLATE_EXTENSION_XIM.PLANAR_CLOSED_PATH_SHAPE_MODEL_WITH_PARAMETERS' IN TYPEOF(s)))) = 0;
      WR3: SIZEOF(QUERY(s <* SELF\Geometric_template_armx.shapes |
        NOT ('CONSTRUCTIVE_SOLID_GEOMETRY_2D_XIM.SINGLE_BOUNDARY_CSG_2D_SHAPE_REPRESENTATION' IN TYPEOF(s)))) = 0;                
  END_ENTITY;

  ENTITY Electrical_isolation_requirement
    SUBTYPE OF (Requirement_view_definition);
      effective_voltage_withstand_capacity_requirement : OPTIONAL Value_limit;
    DERIVE
      electrical_isolation_spacing_requirement         : SET[1:1] OF Predefined_requirement_view_definition_armx := 
        get_specific_requirement_type_for_primary_rvd_relationship(SELF, 'electrical isolation spacing requirement', 
              'INTERCONNECT_PLACEMENT_REQUIREMENTS_XIM.LAYOUT_SPACING_REQUIREMENT_OCCURRENCE_ARMX');
    WHERE
      WR1 : NOT EXISTS(effective_voltage_withstand_capacity_requirement) OR
             ((effective_voltage_withstand_capacity_requirement\Qualified_representation_item.qualifiers[1]\Type_qualifier.name in ['minimum'])
              AND
             ('VALUE_WITH_UNIT_EXTENSION_XIM.ELECTRIC_POTENTIAL_MEASURE_WITH_UNIT' IN TYPEOF(effective_voltage_withstand_capacity_requirement)));
      WR2 : 'INTERCONNECT_PLACEMENT_REQUIREMENTS_XIM.LAYOUT_SPACING_REQUIREMENT_OCCURRENCE_ARMX' IN TYPEOF(electrical_isolation_spacing_requirement[1]);
      WR3 : NOT EXISTS(SELF\Product_view_definition.additional_characterization);
  END_ENTITY;

ENTITY Interconnect_shield_allocation_armx
    SUBTYPE OF (Requirement_assignment_armx, interconnect_shield_allocation);
      SELF\Requirement_assignment_armx.assigned_to : Integral_shield_armx;
      SELF\Requirement_assignment_armx.assigned_requirement RENAMED assigned_requirement_property  : Predefined_requirement_view_definition_armx;
    WHERE
     WR1 : assigned_requirement_property\Product_view_definition.initial_context\product_definition_context.application_domain = 'electrical';      
  END_ENTITY;
		
  ENTITY Thermal_isolation_removal_template_armx
    (* CONNOTATIONAL *) SUBTYPE OF (Material_removal_structured_template_armx, thermal_isolation_removal_template);
	DERIVE
  		thermal_isolation_spacing_requirement: SET[1:?] OF Requirement_view_definition :=
		  get_rvd(SELF, 'thermal requirement');
	WHERE
	  WR1: SIZEOF(QUERY(tisr <* THERMAL_ISOLATION_SPACING_REQUIREMENT |
       NOT('INTERCONNECT_PHYSICAL_REQUIREMENT_ALLOCATION_XIM.THERMAL_ISOLATION_REQUIREMENT' IN TYPEOF(tisr))
       )) =0;
  END_ENTITY;

  ENTITY Thermal_isolation_requirement
  	SUBTYPE OF (Requirement_view_definition);
    thermal_bar_width                        : OPTIONAL Length_tolerance_characteristic;
    number_of_bars                           : OPTIONAL INTEGER;
    effective_current_capacity_requirement   : OPTIONAL Value_limit;
    angular_orientation_requirement          : OPTIONAL Value_range;
    effective_thermal_resistance_requirement : OPTIONAL Value_limit;
   DERIVE
    thermal_isolation_spacing_requirement    : SET[1:1] OF Predefined_requirement_view_definition_armx := 
            get_specific_requirement_type_for_primary_rvd_relationship(SELF, 'thermal isolation spacing requirement', 'INTERCONNECT_PLACEMENT_REQUIREMENTS_XIM.LAYOUT_SPACING_REQUIREMENT_OCCURRENCE_ARMX');
   WHERE
      WR1 : NOT EXISTS(SELF\Product_view_definition.additional_characterization);
      WR2 : NOT(EXISTS(thermal_bar_width) XOR EXISTS(number_of_bars));
      WR3 : 'INTERCONNECT_PLACEMENT_REQUIREMENTS_XIM.LAYOUT_SPACING_REQUIREMENT_OCCURRENCE_ARMX' IN TYPEOF(thermal_isolation_spacing_requirement[1]);
      WR4 : NOT EXISTS(effective_current_capacity_requirement) OR
              ((effective_current_capacity_requirement\Qualified_representation_item.qualifiers[1]\Type_qualifier.name in ['minimum'])
              AND
              ('VALUE_WITH_UNIT_EXTENSION_XIM.ELECTRIC_CURRENT_MEASURE_WITH_UNIT' IN TYPEOF(effective_current_capacity_requirement)));
      WR5 : NOT EXISTS(angular_orientation_requirement) OR
              ('VALUE_WITH_UNIT_EXTENSION_XIM.ANGLE_MEASURE_WITH_UNIT' IN TYPEOF(angular_orientation_requirement));
      WR6 : NOT EXISTS(effective_thermal_resistance_requirement) OR
              ((effective_thermal_resistance_requirement\Qualified_representation_item.qualifiers[1]\Type_qualifier.name in ['minimum'])
              AND
              ('VALUE_WITH_UNIT_EXTENSION_XIM.THERMAL_RESISTANCE_MEASURE_WITH_UNIT' IN TYPEOF(effective_thermal_resistance_requirement)));
  END_ENTITY;

FUNCTION get_specific_requirement_type_for_primary_rvd_relationship(
	input : Requirement_view_definition;
	id : STRING;
    reqType : STRING
) : SET OF Predefined_requirement_view_definition_armx; 

LOCAL
    rvdr : SET[0:?] OF Requirement_view_definition_relationship := [];
    rdp : SET[0:?] OF Predefined_requirement_view_definition_armx := [];  --this gives us access to the information base
END_LOCAL;
	rvdr := bag_to_set(QUERY(rvdre <* USEDIN (input, 'REQUIREMENT_DECOMPOSITION_XIM.'+ 'REQUIREMENT_VIEW_DEFINITION_RELATIONSHIP.SECONDARY')|
	                           ((rvdre.relation_type = id) AND (reqType IN TYPEOF(rvdre.primary)))
                       ));
	-- iterate over rvdr
	REPEAT i := 1 to SIZEOF(rvdr) by 1;
		rdp := rdp + rvdr[i].primary;
	END_REPEAT;

	RETURN(rdp);
END_FUNCTION;

 END_SCHEMA;

