(*
 * $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 WG12N2595 - ISO/TS 10303-1108 Specification_based_configuration - Express ARM
   by LKSoftWare GmbH for implementation purpose within IDA-STEP(TM)
*)


SCHEMA Specification_based_configuration_xim;

USE FROM Effectivity_application_xim;

USE FROM Product_class_xim;

USE FROM Specification_based_configuration_mim;

TYPE configuration_kind = ENUMERATION OF (usage, design);
END_TYPE;

TYPE configuration_inheritance_types = ENUMERATION OF (exception_case, full_inheritance, no_inheritance);
END_TYPE;

TYPE configured_specification_select = SELECT(Class_condition_association, Class_specification_association);
END_TYPE;

TYPE effectivity_for_configuration = SELECT BASED_ON effectivity_item WITH (Configuration);
END_TYPE;

ENTITY Configuration
	(* CONNOTATIONAL *) SUBTYPE OF (configured_effectivity_assignment);
	(* accessable from supertype configured_effectivity_assignment
  configured_element : configured_effectivity_context_item; *)
  is_solution_for : configured_specification_select;
  configuration_type : configuration_kind;
  inheritance_type : configuration_inheritance_types;
  (* This attribute is overlapping with configuration_type and inheritance_type, so make it derived *)
  DERIVE
  	SELF\effectivity_assignment.role : object_role := ?;
 WHERE
  WR1: consistent_configuration_type(SELF);
END_ENTITY;

RULE restrict_to_dated_effectivity FOR (Effectivity_assignment);
LOCAL
  ndea_set : SET OF Effectivity_assignment := [];
END_LOCAL;
  ndea_set := QUERY(ea <* Effectivity_assignment | 
	NOT ('SPECIFICATION_BASED_CONFIGURATION_ARM.DATED_EFFECTIVITY' IN TYPEOF(ea.assigned_effectivity)));
WHERE
  WR1: SIZEOF(QUERY(ea <* ndea_set | 
		(SIZEOF(['SPECIFICATION_BASED_CONFIGURATION_ARM.CONFIGURATION']*type_of_set(ea.items))>0) )) =0;
END_RULE;

FUNCTION type_of_set(items: SET OF GENERIC_ENTITY): SET OF STRING;
LOCAL
  result: SET OF STRING:= [];
END_LOCAL;

REPEAT i:=1 TO HIINDEX(items);
  result := result + TYPEOF(items[i]);
END_REPEAT;
RETURN(result);
END_FUNCTION;

FUNCTION consistent_configuration_type(config: Configuration): BOOLEAN;
IF ('SPECIFICATION_BASED_CONFIGURATION_ARM.CLASS_CONDITION_ASSOCIATION' IN TYPEOF(config.is_solution_for)) THEN
  IF config.configuration_type = configuration_kind.usage THEN
    RETURN(config.is_solution_for\Class_condition_association.condition_type ='part usage');
  ELSE
    RETURN(config.is_solution_for\Class_condition_association.condition_type ='design case');
  END_IF;
END_IF;

IF ('SPECIFICATION_BASED_CONFIGURATION_ARM.CLASS_SPECIFICATION_ASSOCIATION' IN TYPEOF(config.is_solution_for)) THEN
  IF config.configuration_type = configuration_kind.usage THEN
    RETURN(config.is_solution_for\Class_specification_association.association_type ='part usage');
  ELSE 
    RETURN(config.is_solution_for\Class_specification_association.association_type ='design case');
  END_IF;
END_IF;

RETURN(FALSE);
END_FUNCTION;


END_SCHEMA;