
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import java.sql.SQLException;
import java.io.IOException;

/** 
	An application that defines models whose schemas can be graphically represented by the SchemaTool.
*/

public class DefineModels implements Constants{

  public static String modelName="erOld";
  public static String entityConstructName="entityOld";
  public static String attributeConstructName="attributeOld";
  public static String relationshipConstructName="relationshipOld";
  public static String generalizationConstructName="generalisationOld";
 
	public static void createERinMDR()
		throws AutoMedException, SQLException, IOException, InconsistentException
	{
		if (Model.exists(modelName)){
			Model.getModel(modelName).retract();
		}

		//create the ER model
		Model erModel = Model.createModel(modelName);

		//////////////////// ER ENTITY//////////////////////////////////
		Construct erEntity = 
			erModel.createConstruct(entityConstructName, CONSTRUCT_CLASS_NODAL, true);
		erEntity.createConstructScheme(SCHEME_TYPE_NAME, null, true, 
											SCHEME_HDM_TYPE_P1, 1, 1);
		
		//////////////////// ER ATTRIBUTE//////////////////////////////////
		Construct erAttribute =
			erModel.createConstruct(attributeConstructName, CONSTRUCT_CLASS_LINK_NODAL, true);
		erAttribute.createConstructScheme(SCHEME_TYPE_CONSTRUCT, erEntity,
					true, SCHEME_HDM_TYPE_P1, 1, 1);
		erAttribute.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_N1, 1, 1);

		//////////////////// ER RELATIONSHIP//////////////////////////////////
		Construct erRelationship =
			erModel.createConstruct(relationshipConstructName, CONSTRUCT_CLASS_LINK, true);
		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_N1, 1, 1);
		erRelationship.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_P1, 1, 1);
		erRelationship.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_P2, 1, 1);
//		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
//					true, SCHEME_HDM_TYPE_C, 1, 1);
//		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
//					true, SCHEME_HDM_TYPE_C, 1, 1);

		//////////////////// ER GENERALIZATION//////////////////////////////////
		Construct erGeneralization =
			erModel.createConstruct(generalizationConstructName, CONSTRUCT_CLASS_CONSTRAINT, true);
		erGeneralization.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 0);
	}

	public static void main(String[] args){
		try{
			if(!Model.exists(DefineModels.modelName)){
				System.out.println("Re-defining the ER model...");
				DefineModels.createERinMDR();
			}
		}
		catch(InconsistentException aie){
			aie.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
	}



}


