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


public class GeneralisationToAttributeEquivalence extends TemplateCompilerSimulator {
		
	private static String name = "Generalisation To Attribute equivalence";
	private static Model defaultModel;
	
	static{
		try{	
			defaultModel = Model.getModel("templatesER");
		}
		catch(Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
	} 
	
	public GeneralisationToAttributeEquivalence()
		throws Exception
	{
		super();
	}

	public static String getName(){
		return name;
	}		

	//Define the abstract methods//

	protected String templateName()
	{
		return name;
	}

	protected String templateDescription()
	{
		return "Implements a generalisation to attribute equivalence. Attribute name is the same as generalisation name";
	}

	protected String defaultModel()
	{
		return defaultModel.getName();
	}




	//Define the template//


	protected void defineTemplate()
		throws Exception
	{


		INPUTS();
			OBJECT generalisation = askForObject("Existing generalisation", CONSTRUCT.IS("generalisation"));
		
		START();
			LIST genScheme = generalisation.SCHEME_LIST();
			OBJECTLIST parentList = (OBJECTLIST)genScheme.ELEMENT_AT(2, TypeObjectList);
			OBJECT parent = parentList.ELEMENT_AT(0, CONSTRUCT.IS("entity"));
			
			OBJECTLIST subEntities = (OBJECTLIST)genScheme.ELEMENT_AT(3, TypeObjectList);
			
			ALIAS(generalisation, "generalisation");
			NAME attName = DEFINE_NAME("@generalisation?name");
			generalisation.DELETE();
			
			FOREACH(); OBJECT subEntity =IN(subEntities);							
				DO();
					subEntity.DELETE();
					
				ENDFOREACH();
				
			ADD(CONSTRUCT.IS("attribute"),
		      	  SCHEME.IS(new Object [] {parent, my(attName), "key"})
			 );
			

		END();
	}




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


}

