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

public class AttributeGeneralisationEquivalence 
  extends TemplateCompilerSimulator {
  public AttributeGeneralisationEquivalence()
    throws Exception {
    super();
  }

  //Define the abstract methods//
  protected String templateName() {
    return "Attribute to Generalisation equivalence";
  }

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

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

  //Define the template//
  protected void defineTemplate()
    throws Exception {
    Model er=Model.getModel(defaultModel());
    Construct entity=er.getConstruct("entity");
    Construct attribute=er.getConstruct("attribute");
    Construct relationship=er.getConstruct("relationship");
    Construct generalisation=er.getConstruct("generalisation");

    INPUTS();
    OBJECT parentEntity=askForObject("Existing parent entity",entity);
    OBJECT existingAttribute=askForObject("Attribute to be deleted",attribute);
    NAMELIST subEntityNames=askForNameList("Names of the Subentities");
    NAMELIST populationOfEntities=
      askForNameList("Values of the attribute that correspond to subentities",
		     SIZEOF(subEntityNames));
    NAME genName=askForName("Generalisation Name");

    START();
    
    FOREACH(); 
    NAME subName=IN(subEntityNames);
    NAME popName=IN(populationOfEntities);
    OBJECT att=VARIES_WITH(existingAttribute);
    OBJECT parent=VARIES_WITH(parentEntity);
    
    DO();
    ALIAS (popName,"value");
    ALIAS (subName,"subclassName");
    ALIAS (att,"attribute");
    ALIAS (parent,"superclass");
    
    FUNCTION f1=DEFINE_FUNCTION("[{x} | {x,@value} <- @attribute?scheme]");
    
    OBJECT newEntity=ADD(CONSTRUCT.IS(entity),
			 SCHEME.IS(new Object[]{my(subName)}),
			 FUNCTION.IS(f1));
    
    OBJECTLIST createdEntities=(OBJECTLIST) COLLECTS(newEntity);
    
    ENDFOREACH();
    
    ADD(generalisation,
	new Object []{my(genName),"total",parentEntity,createdEntities});
      
    existingAttribute.DELETE();
    END();
  }
  
  public static void main(String [] args) throws Exception {
    new AttributeGeneralisationEquivalence();
  }
}

