
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;


/**
	An application that defines the "Entity-Attribute Basic Equivalence" template transformation.
	This transformation should be applied on a schema of the "er" model.
	It creates a new entity and a relationship between an existing entity and the new one. 
 
	The arguments for this transformation are:
	1. initial schema ID
	2. existing entity ID
	3. new entity name
	4. new relationship name
	5. new entity ID (returned value) 
	6. new relationship ID (returned value)
	7. final schema ID (returned value)
*/
public class AttributeToEntityBasic{
  private static String modelName="er";
  private static String entityConstructName="erEntity";
  private static String attributeConstructName="erAttribute";
  private static String relationshipConstructName="erRelationship";
  private static String generalizationConstructName="erGeneralization";

	private static String templateName = "Entity-Attribute Basic Equivalence";

	public static void defineTemplate()
		throws TemplateTransformationRepositoryException, RepositoryAccessException, TemplateTransformationSyntaxRepositoryException, AutoRepException, AutoMedException
	{
		try{
			CompositeTransformation.removeCompositeTransformation(templateName);
		}
		catch(TemplateTransformationRepositoryException exc){
			System.out.println("new transformation "+templateName);
		}

		Model er = Model.getModel(modelName);
		Construct erEntity = er.getConstruct(entityConstructName);
		Construct erRelationship = er.getConstruct(relationshipConstructName);

		TemplateArgumentSequence newEntityArgs = new TemplateArgumentSequence();	
		newEntityArgs.createArgument(CompositeTransformation.ARG_CID_IN_NEWOBJECT, String.valueOf(erEntity.getCID()));
		newEntityArgs.createArgument(CompositeTransformation.ARG_VALUE_IN_NEWOBJECT, 3);
		newEntityArgs.createArgument(CompositeTransformation.ARG_ISTEMP_IN_NEWOBJECT, "0");	
		TemplateArgumentSequence newEntityScheme = new TemplateArgumentSequence();
		newEntityScheme.createArgument(1, newEntityArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		newEntityArgs.createArgument(CompositeTransformation.ARG_SCHEME_IN_NEWOBJECT, String.valueOf(newEntityScheme.getSequenceId()));

		TemplateArgumentSequence addNewEntityArgs = new TemplateArgumentSequence();
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_ACTION_IN_PRIMITIVE, "add");
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_FROMOBJ_IN_PRIMITIVE, "-1");		
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_TOOBJ_IN_PRIMITIVE, newEntityArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_FROMSCHEMA_IN_PRIMITIVE, 1);
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_TOSCHEMA_IN_PRIMITIVE, newEntityArgs, CompositeTransformation.ARG_SID_IN_NEWOBJECT);
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_FUNCTION_IN_PRIMITIVE, "");
		addNewEntityArgs.createArgument(CompositeTransformation.ARG_CONSTRAINTS_IN_PRIMITIVE, "");

		
		TemplateArgumentSequence newRelationshipArgs = new TemplateArgumentSequence();
		newRelationshipArgs.createArgument(CompositeTransformation.ARG_CID_IN_NEWOBJECT, String.valueOf(erRelationship.getCID()));
		newRelationshipArgs.createArgument(CompositeTransformation.ARG_VALUE_IN_NEWOBJECT, 4);
		newRelationshipArgs.createArgument(CompositeTransformation.ARG_ISTEMP_IN_NEWOBJECT, "0");
		TemplateArgumentSequence newRelationshipScheme = new TemplateArgumentSequence();
		newRelationshipScheme.createArgument(1, newRelationshipArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		newRelationshipScheme.createArgument(2, newEntityArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		newRelationshipScheme.createArgument(3, 2); 
		newRelationshipArgs.createArgument(CompositeTransformation.ARG_SCHEME_IN_NEWOBJECT, String.valueOf(newRelationshipScheme.getSequenceId()));


		TemplateArgumentSequence addNewRelationshipArgs = new TemplateArgumentSequence();
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_ACTION_IN_PRIMITIVE, "add");
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_FROMOBJ_IN_PRIMITIVE, "-1");
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_TOOBJ_IN_PRIMITIVE, newRelationshipArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_FROMSCHEMA_IN_PRIMITIVE, newEntityArgs, CompositeTransformation.ARG_SID_IN_NEWOBJECT);
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_TOSCHEMA_IN_PRIMITIVE, newRelationshipArgs, CompositeTransformation.ARG_SID_IN_NEWOBJECT);
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_FUNCTION_IN_PRIMITIVE, "");
		addNewRelationshipArgs.createArgument(CompositeTransformation.ARG_CONSTRAINTS_IN_PRIMITIVE, "");
	

		TemplateArgumentSequence returnedArgs = new TemplateArgumentSequence();
		returnedArgs.createArgument(5, newEntityArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		returnedArgs.createArgument(6, newRelationshipArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		returnedArgs.createArgument(7, addNewRelationshipArgs, CompositeTransformation.ARG_TOSCHEMA_IN_PRIMITIVE);
		

		CompositeTransformation trans = new CompositeTransformation(templateName, "For the existing attribute an entity is created associated with a relationship with the previously existing entity", true);

		trans.createArgumentDefinition(1, "schema", "SID", "initial schema", false);
		trans.createArgumentDefinition(2, "object", "OBID", "existing entity", false);
		trans.createArgumentDefinition(3, "object", "value", "new entity name", false);
		trans.createArgumentDefinition(4, "object", "value", "new relationship name", false);
		trans.createArgumentDefinition(5, "object", "OBID", "new entity ID", true);
		trans.createArgumentDefinition(6, "object", "OBID", "new relationship ID", true);
		trans.createArgumentDefinition(7, "schema", "SID", "final schema ID", true);

		trans.createReturnedArgumentsDefinition(returnedArgs);


		trans.createStatementExecution(1, Statement.getStatement(CompositeTransformation.NEW_OBJECT_STATEMENT), newEntityArgs);
		trans.createStatementExecution(2, Statement.getStatement(CompositeTransformation.PRIMITIVE_TRANSFORMATION_STATEMENT), addNewEntityArgs);
		trans.createStatementExecution(3, Statement.getStatement(CompositeTransformation.NEW_OBJECT_STATEMENT), newRelationshipArgs);
		trans.createStatementExecution(4, Statement.getStatement(CompositeTransformation.PRIMITIVE_TRANSFORMATION_STATEMENT), addNewRelationshipArgs);
	}	

	public static void main(String[] args){
		try{
			System.out.println("Re-creating template transformation Entity-Attribute Basic Equivalence...");	
			defineTemplate();	
		}
		catch(Exception e){
			e.printStackTrace();
		}	 
	} 
}

