package xml;

import uk.ac.bbk.dcs.automed.qproc.QueryProcessor;
import uk.ac.bbk.dcs.automed.qproc.QueryProcessorConfiguration;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.xml.XMLConstants;
import uk.ac.bbk.dcs.automed.xml.utilities.PrintUtility;
import uk.ac.ic.doc.automed.reps.Schema;

/**
 * Class Task Description
 * 
 * @author <a href="mailto:lucas@dcs.bbk.ac.uk">Lucas Zamboulis </a>
 */
public class XMLBankIntegrationQueries {
	private static char s = '$';

	public static void main(String[] args) {
		try {
			String schemeIQL[] = { "<<account" + s + "1>>" };
			String simpleIQL[] = { "[{x} | {x}<-<<site" + s + "1>>]", "[{x} | {x}<-<<sitecode" + s + "1>>]",
					"[{x} | {x}<-<<cname" + s + "1>>]", "[{x} | {x}<-<<customer" + s + "1>>]",
					"[{x} | {x}<-<<account" + s + "1>>]", "[{x} | {x}<-<<number" + s + "1>>]" };
			String edgeIQL[] = {
			// parent-child in GS is ancestor-descendant in source
					"[{x,y} | {x,y}<-<<root" + s + "1,customer" + s + "1>>]",
					// parent-child in GS is element-attribute in source
					"[{x,y} | {x,y}<-<<account" + s + "1,number" + s + "1>>]",
					// parent-child in GS is child-parent in source
					"[{x,y} | {x,y}<-<<customer" + s + "1,account" + s + "1>>]",
					// element-pcdata in GS is element-attribute in source
					"[{x,y} | {x,y}<-<<number" + s + "1," + XMLConstants.text + ">>]", };

			String complexIQL[] = {
					// ancestor-descendant in GS is sibling elements in source:
					"[{acc,fun}|{acc,site}<-<<account" + s + "1,site" + s + "1>>; {site,fun}<-<<site" + s + "1,function" + s
							+ "1>>]",
					// Get all customer names
					"[{nameData} | {cust,name}<-<<customer" + s + "1,cname" + s + "1>>; " + "{name,nameData}<-<<cname" + s + "1,"
							+ XMLConstants.text + ">>]",
					// Get all distinct customer names
					"distinct [{nameData} | {cust,name}<-<<customer" + s + "1,cname" + s + "1>>; " + "{name,nameData}<-<<cname"
							+ s + "1," + XMLConstants.text + ">>]",
					// Get all account numbers
					"[{numData} | {cust,acc}<-<<customer" + s + "1,account" + s + "1>>; " + "{acc,accNum}<-<<account" + s
							+ "1,number" + s + "1>>; " + "{accNum,numData}<-<<number" + s + "1," + XMLConstants.text + ">>]",
					// Get all distinct account numbers
					"distinct [{numData} | {cust,acc}<-<<customer" + s + "1,account" + s + "1>>; " + "{acc,accNum}<-<<account"
							+ s + "1,number" + s + "1>>; " + "{accNum,numData}<-<<number" + s + "1," + XMLConstants.text + ">>]",
					// Get the names of customers with their account numbers
					"[{nameData,numData} | {cust,name}<-<<customer" + s + "1,cname" + s + "1>>; " + "{name,nameData}<-<<cname"
							+ s + "1," + XMLConstants.text + ">>;" + "{cust,acc}<-<<customer" + s + "1,account" + s + "1>>; "
							+ "{acc,accNum}<-<<account" + s + "1,number" + s + "1>>; " + "{accNum,numData}<-<<number" + s + "1,"
							+ XMLConstants.text + ">>]" };

			Schema src = Schema.getSchema("bank1");
			Schema[] tgts = new Schema[3];
			tgts[0] = Schema.getSchema("bank1");
			tgts[1] = Schema.getSchema("bank2");
			tgts[2] = Schema.getSchema("bank1");

			QueryProcessorConfiguration qpc = new QueryProcessorConfiguration();
			QueryProcessor qp = new QueryProcessor();

			ASG q;
			System.out.println();
			PrintUtility.title("IQL Scheme Queries");
			for (int z = 0; z < schemeIQL.length; z++) {
				q = new ASG(schemeIQL[z]);
				qp.process(q, src, tgts, qpc);
				System.out.println("\nOriginal query: " + q);
				System.out.println("Query after evaluator: " + q);
			}

			System.out.println();
			PrintUtility.title("Simple IQL Queries");
			for (int z = 0; z < simpleIQL.length; z++) {
				q = new ASG(simpleIQL[z]);
				qp.process(q, src, tgts, qpc);
				System.out.println("\nOriginal query: " + q);
				System.out.println("Query after evaluator: " + q);
			}

			System.out.println();
			PrintUtility.title("IQL Edge Queries");
			for (int z = 0; z < edgeIQL.length; z++) {
				q = new ASG(edgeIQL[z]);
				qp.process(q, src, tgts, qpc);
				System.out.println("\nOriginal query: " + q);
				System.out.println("Query after evaluator: " + q);
			}

			System.out.println();
			PrintUtility.title("Complex IQL Queries");
			for (int z = 0; z < complexIQL.length; z++) {
				q = new ASG(complexIQL[z]);
				qp.process(q, src, tgts, qpc);
				System.out.println("\nOriginal query: " + q);
				System.out.println("Query after evaluator: " + q);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}