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


public class AttributeEntityEquivalence extends TemplateCompilerSimulator

{
	public AttributeEntityEquivalence()
		throws Exception
	{
		super();
	}

	//Define the abstract methods//

	protected String templateName()
	{
		return "Attribute to entity equivalence";
	}

	protected String templateDescription()
	{
		return "Implements an attribute to entity equivalence";
	}

	protected String defaultModel()
	{
		return "er";
	}




	//Define the template//

	protected void defineTemplate()
		throws Exception
	{
		INPUTS();
			OBJECT existingEntity = askForObject("Existing Entity", CONSTRUCT.IS("entity"));
			OBJECT existingAttribute = askForObject("Existing Attribute (to be deleted)", CONSTRUCT.IS("attribute"));
			NAME newEntityName = askForName("New entity Name");
			NAME newAttributeName = askForName("New attribute Name");
			NAME newRelationshipName = askForName("New relationship name");

		START();
			ALIAS(existingEntity, "existEnt");
			ALIAS(existingAttribute, "existAttr");
			FUNCTION func1 = DEFINE_FUNCTION("@existEnt?scheme(X):-@existAttr?scheme(_,X)");

			OBJECT newEntity = ADD(CONSTRUCT.IS("entity"),
			                       SCHEME.IS(new Object [] {my(newEntityName)}),
					       FUNCTION.IS(func1));



			OBJECT newAttribute = ADD(CONSTRUCT.IS("attribute"),
					      	  SCHEME.IS(new Object [] {newEntity, my(newEntityName), "key"})
						 );


			OBJECT newRelationship = ADD(CONSTRUCT.IS("relationship"),
						     SCHEME.IS(new Object [] {my(newRelationshipName),
						     			      existingEntity,
									      newEntity,
									      "1:1",
									      "1:N"})
						    );



			ALIAS (newRelationship, "newRel");

			FUNCTION func2 = DEFINE_FUNCTION("@existAttr?scheme(X,Y) :- @newRel?scheme(X,Y)");
			existingAttribute.DELETE(func2);

		END();


	}

	public static void main(String [] args)
	{
		try
		{
			new AttributeEntityEquivalence();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}


}

