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

import java.util.ArrayList;


public class IntegrateMatchedSchemas{

	public static SchemaElementPairRelationship[] getRelationships(Schema s1, Schema s2)
		throws NotFoundException{
		SchemaObject s1_ugObject = s1.getSchemaObject("<<ug>>");
		SchemaElement s1_ug = new SchemaElement(s1_ugObject.hashCode(), s1_ugObject, false);
		SchemaObject s1_ug_ug_loginObject = 
			s1.getSchemaObject("<<ug,ug_login,text>>");
		SchemaElement s1_ug_ug_login = 
			new SchemaElement(s1_ug_ug_loginObject.hashCode(), s1_ug_ug_loginObject, true);

		SchemaObject s1_pgObject = s1.getSchemaObject("<<pg>>");
		SchemaElement s1_pg = new SchemaElement(s1_pgObject.hashCode(), s1_pgObject, false);
		SchemaObject s1_pg_pg_loginObject = 
			s1.getSchemaObject("<<pg,pg_login,text>>");
		SchemaElement s1_pg_pg_login = 
			new SchemaElement(s1_pg_pg_loginObject.hashCode(), s1_pg_pg_loginObject, true);
	
		SchemaObject s1_staffObject = s1.getSchemaObject("<<staff>>");
		SchemaElement s1_staff = new SchemaElement(s1_staffObject.hashCode(), s1_staffObject, false);
		SchemaObject s1_staff_sloginObject = 
			s1.getSchemaObject("<<staff,slogin,text>>");
		SchemaElement s1_staff_slogin = 
			new SchemaElement(s1_staff_sloginObject.hashCode(), s1_staff_sloginObject, true);
		SchemaObject s1_staff_snameObject = 
			s1.getSchemaObject("<<staff,sname,text>>");
		SchemaElement s1_staff_sname = 
			new SchemaElement(s1_staff_snameObject.hashCode(), s1_staff_snameObject, true);
	
		SchemaObject s1_courseObject = s1.getSchemaObject("<<course>>");
		SchemaElement s1_course = new SchemaElement(s1_courseObject.hashCode(), s1_courseObject, false);
		SchemaObject s1_course_course_idObject = 
			s1.getSchemaObject("<<course,course_id,int>>");
		SchemaElement s1_course_course_id = 
			new SchemaElement(s1_course_course_idObject.hashCode(), s1_course_course_idObject, true);
		SchemaObject s1_course_cnameObject = 
			s1.getSchemaObject("<<course,cname,text>>");
		SchemaElement s1_course_cname = 
			new SchemaElement(s1_course_cnameObject.hashCode(), s1_course_cnameObject, true);
	
		SchemaObject s1_studentObject = s1.getSchemaObject("<<student>>");
		SchemaElement s1_student = new SchemaElement(s1_studentObject.hashCode(), s1_studentObject, false);
		SchemaObject s1_student_loginObject = 
			s1.getSchemaObject("<<student,login,text>>");
		SchemaElement s1_student_login = 
			new SchemaElement(s1_student_loginObject.hashCode(), s1_student_loginObject, true);

		SchemaObject s1_registeredObject = s1.getSchemaObject("<<registered>>");
		SchemaElement s1_registered = new SchemaElement(s1_registeredObject.hashCode(), s1_registeredObject, false);
		SchemaObject s1_registered_ug_loginObject = 
			s1.getSchemaObject("<<registered,ug_login,text>>");
		SchemaElement s1_registered_ug_login = 
			new SchemaElement(s1_registered_ug_loginObject.hashCode(), s1_registered_ug_loginObject, true);
		SchemaObject s1_registered_course_idObject = 
			s1.getSchemaObject("<<registered,course_id,int>>");
		SchemaElement s1_registered_course_id = 
			new SchemaElement(s1_registered_course_idObject.hashCode(), s1_registered_course_idObject, true);

		SchemaObject s1_teachesObject = s1.getSchemaObject("<<teaches>>");
		SchemaElement s1_teaches = new SchemaElement(s1_teachesObject.hashCode(), s1_teachesObject, false);
		SchemaObject s1_teaches_sloginObject = 
			s1.getSchemaObject("<<teaches,slogin,text>>");
		SchemaElement s1_teaches_slogin = 
			new SchemaElement(s1_teaches_sloginObject.hashCode(), s1_teaches_sloginObject, true);
		SchemaObject s1_teaches_course_idObject = 
			s1.getSchemaObject("<<teaches,course_id,int>>");
		SchemaElement s1_teaches_course_id = 
			new SchemaElement(s1_teaches_course_idObject.hashCode(), s1_teaches_course_idObject, true);

		SchemaObject s1_tutored_byObject = s1.getSchemaObject("<<tutored_by>>");
		SchemaElement s1_tutored_by = new SchemaElement(s1_tutored_byObject.hashCode(), s1_tutored_byObject, false);
		SchemaObject s1_tutored_by_ug_loginObject = 
			s1.getSchemaObject("<<tutored_by,ug_login,text>>");
		SchemaElement s1_tutored_by_ug_login = 
			new SchemaElement(s1_tutored_by_ug_loginObject.hashCode(), s1_tutored_by_ug_loginObject, true);
		SchemaObject s1_tutored_by_sloginObject = 
			s1.getSchemaObject("<<tutored_by,slogin,text>>");
		SchemaElement s1_tutored_by_slogin = 
			new SchemaElement(s1_tutored_by_sloginObject.hashCode(), s1_tutored_by_sloginObject, true);
		
		SchemaObject s1_supervised_byObject = s1.getSchemaObject("<<supervised_by>>");
		SchemaElement s1_supervised_by = new SchemaElement(s1_supervised_byObject.hashCode(), s1_supervised_byObject, false);
		SchemaObject s1_supervised_by_pg_loginObject = 
			s1.getSchemaObject("<<supervised_by,pg_login,text>>");
		SchemaElement s1_supervised_by_pg_login = 
			new SchemaElement(s1_supervised_by_pg_loginObject.hashCode(), s1_supervised_by_pg_loginObject, true);
		SchemaObject s1_supervised_by_sloginObject = 
			s1.getSchemaObject("<<supervised_by,slogin,text>>");
		SchemaElement s1_supervised_by_slogin = 
			new SchemaElement(s1_supervised_by_sloginObject.hashCode(), s1_supervised_by_sloginObject, true);
		
		SchemaObject s2_postgradObject = s2.getSchemaObject("<<postgrad>>");
		SchemaElement s2_postgrad = new SchemaElement(s2_postgradObject.hashCode(), s2_postgradObject, false);
		SchemaObject s2_postgrad_postgrad_loginObject = 
			s2.getSchemaObject("<<postgrad,postgrad_login,text>>");
		SchemaElement s2_postgrad_postgrad_login = 
			new SchemaElement(s2_postgrad_postgrad_loginObject.hashCode(), s2_postgrad_postgrad_loginObject, true);
		
		SchemaObject s2_staffObject = s2.getSchemaObject("<<staff>>");
		SchemaElement s2_staff = new SchemaElement(s2_staffObject.hashCode(), s2_staffObject, false);
		SchemaObject s2_staff_staff_loginObject = 
			s2.getSchemaObject("<<staff,staff_login,text>>");
		SchemaElement s2_staff_staff_login = 
			new SchemaElement(s2_staff_staff_loginObject.hashCode(), s2_staff_staff_loginObject, true);
		SchemaObject s2_staff_staff_nameObject = 
			s2.getSchemaObject("<<staff,staff_name,text>>");
		SchemaElement s2_staff_staff_name = 
			new SchemaElement(s2_staff_staff_nameObject.hashCode(), s2_staff_staff_nameObject, true);

		SchemaObject s2_courseObject = s2.getSchemaObject("<<course>>");
		SchemaElement s2_course = new SchemaElement(s2_courseObject.hashCode(), s2_courseObject, false);
		SchemaObject s2_course_course_idObject = 
			s2.getSchemaObject("<<course,course_id,int>>");
		SchemaElement s2_course_course_id = 
			new SchemaElement(s2_course_course_idObject.hashCode(), s2_course_course_idObject, true);
		SchemaObject s2_course_cnameObject = 
			s2.getSchemaObject("<<course,cname,text>>");
		SchemaElement s2_course_cname = 
			new SchemaElement(s2_course_cnameObject.hashCode(), s2_course_cnameObject, true);

		SchemaObject s2_assistanceObject = s2.getSchemaObject("<<assistance>>");
		SchemaElement s2_assistance = new SchemaElement(s2_assistanceObject.hashCode(), s2_assistanceObject, false);
		SchemaObject s2_assistance_staff_loginObject =
			s2.getSchemaObject("<<assistance,staff_login,text>>");
		SchemaElement s2_assistance_staff_login = 
			new SchemaElement(s2_assistance_staff_loginObject.hashCode(), s2_assistance_staff_loginObject, true);
		SchemaObject s2_assistance_postgrad_loginObject =
			s2.getSchemaObject("<<assistance,postgrad_login,text>>");
		SchemaElement s2_assistance_postgrad_login = 
			new SchemaElement(s2_assistance_postgrad_loginObject.hashCode(), s2_assistance_postgrad_loginObject, true);
		SchemaObject s2_assistance_course_idObject =
			s2.getSchemaObject("<<assistance,course_id,int>>");
		SchemaElement s2_assistance_course_id = 
			new SchemaElement(s2_assistance_course_idObject.hashCode(), s2_assistance_course_idObject, true);

		ArrayList rels = new ArrayList();
		rels.add(new SchemaElementPairRelationship(s1_ug, s2_postgrad, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_ug_ug_login, s2_postgrad_postgrad_login, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_ug_ug_login, s2_assistance_postgrad_login, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_registered_ug_login, s2_postgrad_postgrad_login, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_registered_ug_login, s2_assistance_postgrad_login, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_tutored_by_ug_login, s2_postgrad_postgrad_login, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_tutored_by_ug_login, s2_assistance_postgrad_login, Disjointness.getRelationship()));

		rels.add(new SchemaElementPairRelationship(s1_pg, s2_postgrad, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_pg_pg_login, s2_postgrad_postgrad_login, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_pg_pg_login, s2_assistance_postgrad_login, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_supervised_by_pg_login, s2_postgrad_postgrad_login, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_supervised_by_pg_login, s2_assistance_postgrad_login, Equivalence.getRelationship()));

		rels.add(new SchemaElementPairRelationship(s1_staff, s2_staff, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_slogin, s2_staff_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_slogin, s2_assistance_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_teaches_slogin, s2_staff_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_teaches_slogin, s2_assistance_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_tutored_by_slogin, s2_staff_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_tutored_by_slogin, s2_assistance_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_supervised_by_slogin, s2_staff_staff_login, Subset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_supervised_by_slogin, s2_assistance_staff_login, Subset.getRelationship()));

		rels.add(new SchemaElementPairRelationship(s1_staff_sname, s2_staff_staff_name, Subset.getRelationship()));
		
		rels.add(new SchemaElementPairRelationship(s1_course, s2_course, Overlapness.getRelationship()));

		rels.add(new SchemaElementPairRelationship(s1_course_course_id, s2_course_course_id, Overlapness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_course_course_id, s2_assistance_course_id, Overlapness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_registered_course_id, s2_course_course_id, Overlapness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_registered_course_id, s2_assistance_course_id, Overlapness.getRelationship()));
		
		rels.add(new SchemaElementPairRelationship(s1_course_cname, s2_course_cname, Overlapness.getRelationship()));
		
		rels.add(new SchemaElementPairRelationship(s1_student, s2_postgrad, Superset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_student_login, s2_postgrad_postgrad_login, Superset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_student_login, s2_assistance_postgrad_login, Superset.getRelationship()));
		
		return (SchemaElementPairRelationship[])rels.toArray(new SchemaElementPairRelationship[0]);
	}
	
	public static void main(String args[]) throws NotFoundException, IntegratorException{
		Schema s1 = Schema.getSchema("s1_tomatch");
		Schema s2 = Schema.getSchema("s2_tomatch");

System.out.println("======================================================");
s1.listSchema();
System.out.println("======================================================");
s2.listSchema();
System.out.println("======================================================");
		
		SchemaElementPairRelationship[] rels = getRelationships(s1, s2);

		SchemaElement.printElements();

		Schema[] schemas = {s1, s2};
		SQLIntegrator integrator = new SQLIntegrator();
		integrator.setSchemas(schemas);	
		integrator.setRelationships(rels);

		integrator.resolveNamingConflicts();
		integrator.mergeSchemas();
		integrator.resolveStructuralConflicts();
		Schema[] newSchemas = integrator.getSchemas();
		newSchemas[0].listSchema();
	}

}
