package test;

import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.ic.doc.rodex.qproc.QueryAnalyser;
import uk.ac.ic.doc.rodex.qproc.QueryFunction;
import uk.ac.ic.doc.rodex.qproc.QuerySubGoal;
import uk.ac.ic.doc.rodex.qproc.QueryVariable;

/**
 * TestQueryAnalyser.java
 *
 * @author Duc M Le  <a href="mailto:dmle@doc.ic.ac.uk"><i>dmle@doc.ic.ac.uk</i></a>
 * @version 1.0
 * Department of Computing, Imperial College
 */

public class TestQueryAnalyser {
  
  /**
   * Creates a new instance of TestQueryAnalyser
   */
  public TestQueryAnalyser() {
  }
  
  public static void main(String[] args) throws Exception {
			PrintStream out = System.out;
			ASG q = new ASG("[{r,x,y} | {r,x} <- <<person,name>>; {r,x} <- <<person,dept>>; {r,y} <- <<person,dob>>; "
					+ "x > 'a'; y = 't'; x < 'z']");

      QueryAnalyser qa = new QueryAnalyser(q);
      qa.analyse();

      out.println("Query: "+ q.toString());
      
      List headVars = qa.getHeadVars();
      for (Iterator it = headVars.iterator(); it.hasNext();) {
        QueryVariable qv = (QueryVariable) it.next();
        out.println("Var obj: " + qv.getName());
      }
      
      String var = "y";
      out.println("Head variable index: " + var + " --> " + qa.getHeadVarIndex(var));
      
      // test sub-goals
      QuerySubGoal qsg = (QuerySubGoal) qa.getSubGoals().get(0);
      out.println("Sub-goal: " + qsg.toIQLString());
      List vars = qsg.getVariables();
      for (Iterator it = vars.iterator(); it.hasNext();) {
        QueryVariable qv = (QueryVariable) it.next();
        out.println("subgoal var : " + qv.getName());
      }      
      
      List funcs = qa.getFunctions(qsg);
      for (Iterator it = funcs.iterator(); it.hasNext();) {
        QueryFunction qf = (QueryFunction) it.next();
        out.println("func : " + qf.toIQLString());
      }      
  }
}
