// Path: core/src/main/java/com/dnt/itl/ext/ExtensionFunction.java
// public class ExtensionFunction {
// 
//     public static final boolean getResumeRate(double age, String education, double income) {
// 
//         if (income * 0.75 > age) {
//             return true;
//         }
//         return false;
//     }
// 
// 
// }
// 
// Path: core/src/main/java/com/dnt/itl/parser/ITLExplainService.java
// public class ITLExplainService {
// 
//     Map<String, List<QlContextWithId>> ds2Ctx = new HashMap<>();
// 
//     static Gson gson = new Gson();
// 
//     ITLVisitor visitor;
// 
// 
//     public ITLExplainService() {
//         FunctionMgr functionMgr = new FunctionMgr();
// 
//         visitor = new ITLVisitor();
//         visitor.setFunctionMgr(functionMgr);
// 
//     }
// 
//     public void addFunction(String funcName, Method method, Object on) {
//         visitor.getFunctionMgr().addFunction(funcName, new Function(method, on));
//     }
// 
//     public synchronized void addITL(Object id, String ITL) {
//         ANTLRInputStream inputStream = null;
//         try {
//             inputStream = new ANTLRInputStream(new ByteArrayInputStream(ITL.getBytes()));
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//         ITLLexer lexer = new ITLLexer(inputStream);
//         CommonTokenStream tokenStream = new CommonTokenStream(lexer);
//         ITLParser ITLParse = new ITLParser(tokenStream);
// 
//         ITLParser.QlContext qlContext = ITLParse.ql();
// 
//         String fromSource = qlContext.from().fromSource.getText();
// 
//         List<QlContextWithId> ctxs = ds2Ctx.get(fromSource);
//         if (ctxs == null) {
//             ctxs = new ArrayList<>();
//             ds2Ctx.put(fromSource, ctxs);
//         }
// 
//         QlContextWithId qlContextWithId = new QlContextWithId(id, qlContext);
// 
//         ctxs.add(qlContextWithId);
//     }
// 
//     public synchronized boolean removeITL(Object id) {
//         for (List<QlContextWithId> qlContexts : ds2Ctx.values()) {
//             if (qlContexts.remove(id)) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     public Map<Object, Object> process(String datasource, String json) {
// 
//         Map data = gson.fromJson(json, Map.class);
// 
//         return this.process(datasource, data);
//     }
// 
//     public Map<Object, Object> process(String datasource, Object json) {
// 
//         List<QlContextWithId> qlContexts = ds2Ctx.get(datasource);
// 
//         if (qlContexts == null) {
//             return Collections.EMPTY_MAP;
//         }
// 
// 
//         Map<Object, Object> rets = new HashMap<>();
// 
//         for (QlContextWithId qlContextWithId : qlContexts) {
//             Object id = qlContextWithId.id;
//             ITLParser.QlContext qlContext = qlContextWithId.qlContext;
// 
//             visitor.setRawData(json);
//             Object var = visitor.visit(qlContext);
//             rets.put(id, var);
//         }
// 
//         return rets;
//     }
// 
// }


import com.dnt.itl.ext.ExtensionFunction;
import com.dnt.itl.parser.ITLExplainService;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
package com.dnt.itl;



/**
 * User: Mark Zang
 * Date: 2015/4/16
 * Time: 16:30
 */
public class DemoAppMain {

    static Gson gson = new Gson();

    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        runOrder();
        runResume();
    }

    static void runOrder() throws NoSuchMethodException {
        // prepare data
        Map data = gson.fromJson(new InputStreamReader(DemoAppMain.class.getResourceAsStream("/order.json")), Map.class);

        String otl = "select\n" +
                "     order.orderId as orderId,\n" +
                "     timeStr(order.datetime) as datetime,\n" +
                "     order.ip as ip,\n" +
                "     upper(order.location) as location,\n" +
                "     order.ref as ref\n" +
                "from OrderData as order\n" +
                "where\n" +
                "     (reduce on order.items using sum(amount*(price-cost)) - order.transferCost) > 0";

        // ITLExplainService is used to run ITL
// Path: core/src/main/java/com/dnt/itl/ext/ExtensionFunction.java
// public class ExtensionFunction {
// 
//     public static final boolean getResumeRate(double age, String education, double income) {
// 
//         if (income * 0.75 > age) {
//             return true;
//         }
//         return false;
//     }
// 
// 
// }
// 
// Path: core/src/main/java/com/dnt/itl/parser/ITLExplainService.java
// public class ITLExplainService {
// 
//     Map<String, List<QlContextWithId>> ds2Ctx = new HashMap<>();
// 
//     static Gson gson = new Gson();
// 
//     ITLVisitor visitor;
// 
// 
//     public ITLExplainService() {
//         FunctionMgr functionMgr = new FunctionMgr();
// 
//         visitor = new ITLVisitor();
//         visitor.setFunctionMgr(functionMgr);
// 
//     }
// 
//     public void addFunction(String funcName, Method method, Object on) {
//         visitor.getFunctionMgr().addFunction(funcName, new Function(method, on));
//     }
// 
//     public synchronized void addITL(Object id, String ITL) {
//         ANTLRInputStream inputStream = null;
//         try {
//             inputStream = new ANTLRInputStream(new ByteArrayInputStream(ITL.getBytes()));
//         } catch (IOException e) {
//             e.printStackTrace();
//         }
//         ITLLexer lexer = new ITLLexer(inputStream);
//         CommonTokenStream tokenStream = new CommonTokenStream(lexer);
//         ITLParser ITLParse = new ITLParser(tokenStream);
// 
//         ITLParser.QlContext qlContext = ITLParse.ql();
// 
//         String fromSource = qlContext.from().fromSource.getText();
// 
//         List<QlContextWithId> ctxs = ds2Ctx.get(fromSource);
//         if (ctxs == null) {
//             ctxs = new ArrayList<>();
//             ds2Ctx.put(fromSource, ctxs);
//         }
// 
//         QlContextWithId qlContextWithId = new QlContextWithId(id, qlContext);
// 
//         ctxs.add(qlContextWithId);
//     }
// 
//     public synchronized boolean removeITL(Object id) {
//         for (List<QlContextWithId> qlContexts : ds2Ctx.values()) {
//             if (qlContexts.remove(id)) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
//     public Map<Object, Object> process(String datasource, String json) {
// 
//         Map data = gson.fromJson(json, Map.class);
// 
//         return this.process(datasource, data);
//     }
// 
//     public Map<Object, Object> process(String datasource, Object json) {
// 
//         List<QlContextWithId> qlContexts = ds2Ctx.get(datasource);
// 
//         if (qlContexts == null) {
//             return Collections.EMPTY_MAP;
//         }
// 
// 
//         Map<Object, Object> rets = new HashMap<>();
// 
//         for (QlContextWithId qlContextWithId : qlContexts) {
//             Object id = qlContextWithId.id;
//             ITLParser.QlContext qlContext = qlContextWithId.qlContext;
// 
//             visitor.setRawData(json);
//             Object var = visitor.visit(qlContext);
//             rets.put(id, var);
//         }
// 
//         return rets;
//     }
// 
// }


// Path: core/src/main/java/com/dnt/itl/DemoAppMain.java
import com.dnt.itl.ext.ExtensionFunction;
import com.dnt.itl.parser.ITLExplainService;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

package com.dnt.itl;



/**
 * User: Mark Zang
 * Date: 2015/4/16
 * Time: 16:30
 */
public class DemoAppMain {

    static Gson gson = new Gson();

    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        runOrder();
        runResume();
    }

    static void runOrder() throws NoSuchMethodException {
        // prepare data
        Map data = gson.fromJson(new InputStreamReader(DemoAppMain.class.getResourceAsStream("/order.json")), Map.class);

        String otl = "select\n" +
                "     order.orderId as orderId,\n" +
                "     timeStr(order.datetime) as datetime,\n" +
                "     order.ip as ip,\n" +
                "     upper(order.location) as location,\n" +
                "     order.ref as ref\n" +
                "from OrderData as order\n" +
                "where\n" +
                "     (reduce on order.items using sum(amount*(price-cost)) - order.transferCost) > 0";

        // ITLExplainService is used to run ITL
        ITLExplainService ITLExplainService = new ITLExplainService();