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

SCHEMA Specification_document_xim;

USE FROM Tagged_text_representation_xim;

USE FROM Configuration_item_xim;

USE FROM Characteristic_xim;

USE FROM Specification_document_mim;

USE FROM Document_assignment_mim;

USE FROM management_resources_schema(document_reference);

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

(* removed from arm
 	TYPE sd_external_identification_item = EXTENSIBLE GENERIC_ENTITY SELECT BASED_ON external_identification_item WITH  
		(Structured_text);
	END_TYPE;
*)

	TYPE sd_documented_element_select = EXTENSIBLE GENERIC_ENTITY SELECT BASED_ON documented_element_select WITH (
		configuration_item,
     	Product_definition,
     	product_definition_formation,
     	Product);
	END_TYPE;

  ENTITY Design_specification
    SUPERTYPE OF (Interface_specification_document_definition)
    (* CONNOTATIONAL *) SUBTYPE OF (Specification_definition_armx);
  END_ENTITY;

  ENTITY Fabrication_technology_specification
    (* CONNOTATIONAL *) SUBTYPE OF (Process_specification);
  END_ENTITY;

  ENTITY Interface_specification_document_definition
    (* CONNOTATIONAL *) SUBTYPE OF (Design_specification);
  END_ENTITY;

  ENTITY Language_reference_manual
    (* CONNOTATIONAL *) SUBTYPE OF (Specification_definition_armx);
  END_ENTITY;

  ENTITY Material_specification
    (* CONNOTATIONAL *) SUBTYPE OF (Specification_definition_armx);
  END_ENTITY;

  ENTITY Partial_document_with_structured_text_representation_assignment_armx
    SUBTYPE OF(Partial_document_assignment, partial_document_with_structured_text_representation_assignment);
      structured_portion : string_representation_item_select;
    DERIVE
      SELF\characterized_object.name : label := ?;
      SELF\characterized_object.description : text := ?;
    UNIQUE
      UR1: SELF\Partial_document_assignment.document_portion,
      SELF\Document_assignment.assigned_document;
  END_ENTITY;

  ENTITY Process_specification
    (* CONNOTATIONAL *) SUBTYPE OF (Specification_definition_armx);
  END_ENTITY;

  ENTITY Specification_allocation
  	SUBTYPE OF (Document_assignment);
      SELF\Document_assignment.assigned_document_x RENAMED allocated_specification : Specification_definition;
  END_ENTITY;

  ENTITY Specification_definition_armx
  	SUPERTYPE OF(ONEOF (Design_specification,
	 Process_specification,
	 Material_specification,
	 Test_specification,
	 Language_reference_manual))
    (* CONNOTATIONAL *) SUBTYPE OF (Document_definition, specification_definition);
	DERIVE
	(* GL - in the ARM 1 DERIVED is splitted into 2 in XIMs, because ARM entity is split into select of 2 *)
  		source_organization: SET[0:?] OF Applied_organization_assignment :=
         bag_to_set(QUERY(temp <* USEDIN(SELF,'PERSON_ORGANIZATION_ASSIGNMENT_XIM.'+
         'APPLIED_ORGANIZATION_ASSIGNMENT.ITEMS') |
                         (temp.role.name = 'document source') ));
  		source_person_and_organization: SET[0:?] OF Applied_person_and_organization_assignment :=
         bag_to_set(QUERY(temp <* USEDIN(SELF,'PERSON_ORGANIZATION_ASSIGNMENT_XIM.'+
         'APPLIED_PERSON_AND_ORGANIZATION_ASSIGNMENT.ITEMS') |
                         (temp.role.name = 'document source') ));
	WHERE
   		WR1 : ((EXISTS (source_organization)) OR (EXISTS (source_person_and_organization))) AND 
   		(SIZEOF(source_organization)+SIZEOF(source_person_and_organization) = 1);
  END_ENTITY;

  ENTITY Surface_finish_specification
    (* CONNOTATIONAL *) SUBTYPE OF (Material_specification);
  END_ENTITY;

  ENTITY Test_specification
    (* CONNOTATIONAL *) SUBTYPE OF (Specification_definition_armx);
  END_ENTITY;

FUNCTION get_document_definition(
	input : documented_element_select;
	role : STRING;
	type_name : STRING
) : SET OF Document_definition; 

LOCAL
	dd : SET[0:?] OF Document_definition := [];  --this gives us access to the information base
	da : SET[0:?] OF Document_assignment :=[];
END_LOCAL;
	da := (QUERY(dr <* USEDIN (input, 'DOCUMENT_ASSIGNMENT_ARM.'+
	'DOCUMENT_ASSIGNMENT.IS_ASSIGNED_TO') | (dr.role = role)));

	-- iterate over da
	REPEAT i := 1 to SIZEOF(da) by 1;
		IF(type_name IN TYPEOF (da[i].assigned_document))THEN
			dd := dd + da[i].assigned_document;
		END_IF;
	END_REPEAT;

	RETURN(dd);
END_FUNCTION;

END_SCHEMA;

