package test;

import java.io.PrintStream;
import java.util.Iterator;
import java.util.Map;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.ic.doc.automed.util.Debugger;
import uk.ac.ic.doc.automed.util.LogOutputHandler;
import uk.ac.ic.doc.automed.util.Logger;
import uk.ac.ic.doc.rodex.qproc.QueryRewriteProcessor;
import uk.ac.ic.doc.rodex.qproc.QueryRewriterResult;

/**
 * TestRangeBasedQueryRewriter.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 TestRangeBasedQueryRewriter {
  
  /**
   * Creates a new instance of TestRangeBasedQueryRewriter
   */
  public TestRangeBasedQueryRewriter() {
  }
  
  public static void main(String[] args) throws Exception {
    
    Logger.setOutputHandler(new LogOutputHandler(LogOutputHandler.CONSOLE +
      LogOutputHandler.FILE));
    
    Debugger.setSectionDebugLevel(Debugger.DEBUG_QUERY, Debugger.DEBUG_LEVEL_DETAILED);
    Debugger.setSectionDebugLevel(Debugger.DEBUG_QUERY_REWRITE, Debugger.DEBUG_LEVEL_ALL);
    
    PrintStream out = System.out;
    
//    ASG g = new ASG("[{r,n,d} | {r,n} <- <<person,name>>; {r,d} <- <<person,dob>>; "
//        + "n > 'a'; d >= 1970 ; n < 'z']");
    ASG[] queries = new ASG[] {
      new ASG("[{r,d} | {r,d} <- <<person,dob>>; "
      + "d >= 1970 ; d < 2000]"),                   // single numeric
      new ASG("[{r,d} | {r,d} <- <<person,dob>>; "
      + "d >= 1970 ; d < 2000; r >= 1; r <= 10]"),  // multi numeric
      
    };
    
    // works with the following ASG-list syntax in which the order of values is 
    // reversed to that of the head vars      
    ASG[] extents = new ASG[] {
      new ASG ("[" +    
      "{1, 1970}, {2, 1972}, {3,1975}, {4,1980}, {5,1985}, {6,1990}, {7,1995}" +
      "]"),
    };
      
    ASG q = queries[0];
    ASG extent = extents[0];
    
//    extent.explore();
//    out.println(extent.toString());
    
    QueryRewriteProcessor qproc = new QueryRewriteProcessor(null, q, QueryRewriteProcessor.TYPE_RANGE_BASED);    
    qproc.initRewriterEngine(extent);
    
    QueryRewriterResult result = qproc.rewrite();
    
    Map entries = result.getEntries();
    Iterator kit = entries.keySet().iterator();
    while (kit.hasNext()) {
      ASG v = (ASG) kit.next();
      ASG r = (ASG) result.getEntry(v);
      out.println("View: "+ v.toString());
      out.println("\t>>>tuple result:" + ((r != null) ? r.toString() : "null"));
    }
  }
}
