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 uk.ac.ic.doc.automed.editor.MatchingTool;

import java.util.ArrayList;



public class UniversityAutomaticIntegration{

	public static SchemaElementPairRelationship[] getRelationshipsS1S3(Schema s1, Schema s3)
		throws NotFoundException{

		//s1 elements
		SchemaObject s1_deptObject = s1.getSchemaObject("<<dept>>");
		SchemaElement s1_dept = new SchemaElement(s1_deptObject, false);
		SchemaObject s1_dept_dnameObject = s1.getSchemaObject("<<dept,dname>>");
		SchemaElement s1_dept_dname = new SchemaElement(s1_dept_dnameObject, true);

		SchemaObject s1_staffObject = s1.getSchemaObject("<<staff>>");
		SchemaElement s1_staff = new SchemaElement(s1_staffObject, false);
		SchemaObject s1_staff_idObject = s1.getSchemaObject("<<staff,id>>");
		SchemaElement s1_staff_id = new SchemaElement(s1_staff_idObject, true);
	
		SchemaObject s1_staff_dnameObject = s1.getSchemaObject("<<staff,dname>>");
		SchemaElement s1_staff_dname = new SchemaElement(s1_staff_dnameObject, true);
		
		//s3 elements
		SchemaObject s3_deptObject = s3.getSchemaObject("<<dept>>");
		SchemaElement s3_dept = new SchemaElement(s3_deptObject, false);
		SchemaObject s3_dept_dnameObject = s3.getSchemaObject("<<dept,dname>>");
		SchemaElement s3_dept_dname = new SchemaElement(s3_dept_dnameObject, true);

		SchemaObject s3_personObject = s3.getSchemaObject("<<person>>");
		SchemaElement s3_person = new SchemaElement( s3_personObject, false);
		SchemaObject s3_person_idObject = s3.getSchemaObject("<<person,id>>");
		SchemaElement s3_person_id = new SchemaElement(s3_person_idObject, true);
		SchemaObject s3_person_dnameObject = s3.getSchemaObject("<<person,dname>>");
		SchemaElement s3_person_dname = new SchemaElement(s3_person_dnameObject, true);
		
		SchemaObject s3_maleObject = s3.getSchemaObject("<<male>>");
		SchemaElement s3_male = new SchemaElement(s3_maleObject, false);
		SchemaObject s3_male_idObject = s3.getSchemaObject("<<male,id>>");
		SchemaElement s3_male_id = new SchemaElement(s3_male_idObject, true);
	
		SchemaObject s3_femaleObject = s3.getSchemaObject("<<female>>");
		SchemaElement s3_female = new SchemaElement(s3_femaleObject, false);
		SchemaObject s3_female_idObject = s3.getSchemaObject("<<female,id>>");
		SchemaElement s3_female_id = new SchemaElement(s3_female_idObject, true);


		//relationships
		ArrayList rels = new ArrayList();
		rels.add(new SchemaElementPairRelationship(s1_staff, s3_person, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_id, s3_person_id, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_dname, s3_person_dname, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_dept_dname, s3_dept_dname, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff, s3_male, Superset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_id, s3_male_id, Superset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff, s3_female, Superset.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s1_staff_id, s3_female_id, Superset.getRelationship()));

		return (SchemaElementPairRelationship[])rels.toArray(new SchemaElementPairRelationship[0]);
	}



	public static SchemaElementPairRelationship[] getRelationshipsS2S3(Schema s2, Schema s3)
		throws NotFoundException{

		//s2 elements
		SchemaObject s2_personObject = s2.getSchemaObject("<<person>>");
		SchemaElement s2_person = new SchemaElement( s2_personObject, false);
		SchemaObject s2_person_idObject = s2.getSchemaObject("<<person,id>>");
		SchemaElement s2_person_id = new SchemaElement(s2_person_idObject, true);
		
		SchemaObject s2_person_dnameObject = s2.getSchemaObject("<<person,dname>>");
		SchemaElement s2_person_dname = new SchemaElement(s2_person_dnameObject, true);
		
		SchemaObject s2_maleObject = s2.getSchemaObject("<<male>>");
		SchemaElement s2_male = new SchemaElement(s2_maleObject, false);
		SchemaObject s2_male_idObject = s2.getSchemaObject("<<male,id>>");
		SchemaElement s2_male_id = new SchemaElement(s2_male_idObject, true);
	
		SchemaObject s2_femaleObject = s2.getSchemaObject("<<female>>");
		SchemaElement s2_female = new SchemaElement(s2_femaleObject, false);
		SchemaObject s2_female_idObject = s2.getSchemaObject("<<female,id>>");
		SchemaElement s2_female_id = new SchemaElement(s2_female_idObject, true);


		//s3 elements
		SchemaObject s3_personObject = s3.getSchemaObject("<<person>>");
		SchemaElement s3_person = new SchemaElement(s3_personObject, false);
		SchemaObject s3_person_idObject = s3.getSchemaObject("<<person,id>>");
		SchemaElement s3_person_id = new SchemaElement( s3_person_idObject, true);
		
		SchemaObject s3_person_dnameObject = s3.getSchemaObject("<<person,dname>>");
		SchemaElement s3_person_dname = new SchemaElement(s3_person_dnameObject, true);
		
		SchemaObject s3_maleObject = s3.getSchemaObject("<<male>>");
		SchemaElement s3_male = new SchemaElement(s3_maleObject, false);
		SchemaObject s3_male_idObject = s3.getSchemaObject("<<male,id>>");
		SchemaElement s3_male_id = new SchemaElement(s3_male_idObject, true);
	
		SchemaObject s3_femaleObject = s3.getSchemaObject("<<female>>");
		SchemaElement s3_female = new SchemaElement(s3_femaleObject, false);
		SchemaObject s3_female_idObject = s3.getSchemaObject("<<female,id>>");
		SchemaElement s3_female_id = new SchemaElement(s3_female_idObject, true);

		//relationships
		ArrayList rels = new ArrayList();
		rels.add(new SchemaElementPairRelationship(s2_male, s3_female, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_male_id, s3_female_id, Disjointness.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_male, s3_male, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_male_id, s3_male_id, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_female, s3_female, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_female_id, s3_female_id, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_person, s3_person, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_person_id, s3_person_id, Equivalence.getRelationship()));
		rels.add(new SchemaElementPairRelationship(s2_person_dname, s3_person_dname, Equivalence.getRelationship()));

		return (SchemaElementPairRelationship[])rels.toArray(new SchemaElementPairRelationship[0]);
	}


	public static void main(String[] args) throws NotFoundException, IntegratorException{
		Schema s1 = Schema.getSchema("uni_s1");
		Schema s2 = Schema.getSchema("uni_s2");
		Schema s3 = Schema.getSchema("uni_s3");

		System.out.println("******* Discover relationships between "+s1+" and "+s2+" ******************");
		MatchingTool toolS1S2 = new MatchingTool(s1, s2);
		toolS1S2.run();
//		toolS1S2.discoverRelationships();
		System.out.println("***********************************************************");
		System.out.println("***********************************************************");

		while(!toolS1S2.areSchemasIntegrated()){
			if(toolS1S2.stopped())
				System.exit(0);
			try{
				Thread.sleep(100);
			}catch(InterruptedException e){}
		}
		Schema integrated = toolS1S2.getIntegratedSchema();
		System.out.println(s1+ " and "+s2+ " integrated in " +integrated);
		
		System.out.println("******* Discover relationships between "+s3+" and "+integrated+" ******************");
		MatchingTool toolS3S1S2 = new MatchingTool(s3, integrated);
		toolS3S1S2.run();
//		toolS3S1S2.discoverRelationships();
		System.out.println("***********************************************************");
		System.out.println("***********************************************************");

		while(!toolS3S1S2.areSchemasIntegrated()){
			if(toolS3S1S2.stopped())
				System.exit(0);
			try{
				Thread.sleep(100);
			}catch(InterruptedException e){}
		}

		System.out.println("final integrated schema is "+ toolS3S1S2.getIntegratedSchema());
		
	}
}
