import uk.ac.bbk.dcs.automed.qproc.*;
import uk.ac.ic.doc.automed.NotFoundException;
import uk.ac.ic.doc.automed.reps.Schema;

public class UniversityQueryExamples {
  public static void main(String[] args) {
    try {
      String[][] query=new String[][]{
	{"distinct [{y} | {x,y}<-<<person,name>>]",
	 "[{'Alex'},{'Dimitri'},{'Mike'},{'Nerissa'},{'Nick'},{'Peter'}]"},
	{"distinct [{x}|{x,y}<-<<person,id>>;{w,z}<-<<female,id>>;(=) {x}{w}]",
	 "[{1 },{4 }]"},
	{"distinct [{n} | {i}<-<<person>>; member <<female>> {i}; "+
	"{j,n}<-<<person,name>>; (=) {i} {j}]",
	 "[{'Alex' },{'Nerissa' }]"},
	{"distinct [{j} | {i,j}<-<<person,name>>; member <<female>> {i}; "+
	 "(=) 'Nerissa' j]",
	 "[{'Nerissa' }]"},
	{"distinct [ j | {i,j}<-<<person,name>>; member <<female>> {i}; "+
	 "(=) 'Nerissa' j]",
	 "['Nerissa' ]"},
	{"distinct [n | {f}<- distinct <<female>>; "+
	 "{f1,n} <- distinct <<person,name>>;"+
	 "(=) f f1; {f2,d} <- distinct <<person,dname>>; "+
	 "(=) f2 f1; (=) d 'CS-BBK']",
	 "['Alex' ]"}};

      Schema src = Schema.getSchema("uni_zb");
      Schema[] tgts = new Schema[]{Schema.getSchema("uni_s1_src"),
				   Schema.getSchema("uni_s2_src"),
				   Schema.getSchema("uni_s3_src")};

      QueryReformulator qr=new QueryReformulator(src,tgts);
      FragmentProcessor fp=new FragmentProcessor();
      FunctionTable ft=new StandardFunctionTable();
      Evaluator e=new Evaluator(ft);
	
      for (int i=0;i<query.length;i++) {
	ASG q = new ASG(query[i][0]);
	
	System.out.println("***************");
	System.out.println("*** Query "+i+" ***");
	System.out.println("***************\n"+q);
	
	qr.reformulate(q);	
	System.out.println("*** Query after query reformulator:\n" + q);
      
	fp.process(q);	
	System.out.println("*** Query after fragment processor:\n" + q);

	e.evaluate(q);
	System.out.println("*** Query after evaluator:\n" + q);

	System.out.println("*** Result Expected:\n"+query[i][1]);
      }
    }
    catch (NotFoundException e) {
      e.printStackTrace();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}
