Search in sources :

Example 1 with InstExp

use of catdata.fql.decl.InstExp in project fql by CategoricalData.

the class JDBCBridge method run.

public static Triple<Map<String, Set<Map<Object, Object>>>, String, List<Throwable>> run(FQLProgram prog) {
    Map<String, Set<Map<Object, Object>>> ret = new HashMap<>();
    List<Throwable> exns = new LinkedList<>();
    InstOps ops = new InstOps(prog);
    PSMInterp interp = new PSMInterp();
    Connection Conn;
    Statement Stmt = null;
    List<PSM> sqls = new LinkedList<>();
    try {
        switch(DefunctGlobalOptions.debug.fql.sqlKind) {
            case H2:
                Class.forName("org.h2.Driver");
                Conn = DriverManager.getConnection("jdbc:h2:mem:");
                Stmt = Conn.createStatement();
                Stmt.execute("SET @GUID = 0");
                break;
            case JDBC:
                Class.forName(DefunctGlobalOptions.debug.fql.jdbcClass);
                Conn = DriverManager.getConnection(DefunctGlobalOptions.debug.fql.jdbcUrl);
                Stmt = Conn.createStatement();
                String[] prel = DefunctGlobalOptions.debug.fql.prelude.split(";");
                for (String s : prel) {
                    Stmt.execute(s);
                }
                break;
            case NATIVE:
                break;
            default:
                throw new RuntimeException();
        }
        for (String k : prog.order) {
            InstExp v1 = prog.insts.get(k);
            TransExp v2 = prog.transforms.get(k);
            try {
                if (v1 != null) {
                    sqls.addAll(maybeExecInstance(ops, prog, Stmt, k, v1, interp, ret));
                } else if (v2 != null) {
                    sqls.addAll(maybeExecTransform(ops, prog, Stmt, k, v2, interp, ret));
                }
            } catch (Throwable re) {
                String thing = (v1 == null) ? "transform" : "instance";
                re.printStackTrace();
                LineException exn = new LineException(re.getLocalizedMessage(), k, thing);
                if (DefunctGlobalOptions.debug.fql.continue_on_error) {
                    exns.add(exn);
                } else {
                    if (DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.JDBC) {
                        String[] prel0 = DefunctGlobalOptions.debug.fql.afterlude.split(";");
                        for (String s : prel0) {
                            if (!s.trim().isEmpty()) {
                                if (Stmt == null) {
                                    throw new RuntimeException("Anomaly: please report");
                                }
                                Stmt.execute(s);
                            }
                        }
                    }
                    throw exn;
                }
            }
        }
        List<PSM> drops = Driver.computeDrops(prog);
        if (DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.JDBC) {
            for (PSM dr : drops) {
                if (Stmt == null) {
                    throw new RuntimeException("Anomaly: please report");
                }
                Stmt.execute(dr.toPSM());
            }
            String[] prel0 = DefunctGlobalOptions.debug.fql.afterlude.split(";");
            for (String s : prel0) {
                if (!s.trim().isEmpty()) {
                    if (Stmt == null) {
                        throw new RuntimeException("Anomaly: please report");
                    }
                    Stmt.execute(s);
                }
            }
        }
        String str;
        try {
            str = DefunctGlobalOptions.debug.fql.prelude + "\n\n" + PSMGen.prettyPrint(sqls) + "\n\n" + (drops.isEmpty() ? "" : PSMGen.prettyPrint(drops) + "\n\n") + DefunctGlobalOptions.debug.fql.afterlude + "\n\n";
        } catch (RuntimeException re) {
            str = re.getLocalizedMessage();
        }
        return new Triple<>(ret, str, exns);
    } catch (Exception exception) {
        if (exception instanceof LineException) {
            throw ((LineException) exception);
        }
        exception.printStackTrace();
        throw new RuntimeException(exception.getLocalizedMessage());
    }
}
Also used : HashSet(java.util.HashSet) ResultSet(java.sql.ResultSet) Set(java.util.Set) PSMInterp(catdata.fql.sql.PSMInterp) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Statement(java.sql.Statement) TransExp(catdata.fql.decl.TransExp) Connection(java.sql.Connection) InstOps(catdata.fql.decl.InstOps) PSM(catdata.fql.sql.PSM) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) LinkedList(java.util.LinkedList) LineException(catdata.LineException) SQLException(java.sql.SQLException) Triple(catdata.Triple) InstExp(catdata.fql.decl.InstExp) LineException(catdata.LineException)

Example 2 with InstExp

use of catdata.fql.decl.InstExp in project fql by CategoricalData.

the class JDBCBridge method maybeExecInstance.

private static List<PSM> maybeExecInstance(InstOps ops, FQLProgram prog, Statement Stmt, String k, InstExp v, PSMInterp interp, Map<String, Set<Map<Object, Object>>> ret) throws SQLException {
    List<PSM> psm = new LinkedList<>();
    psm.addAll(PSMGen.makeTables(k, v.type(prog).toSig(prog), false));
    switch(DefunctGlobalOptions.debug.fql.sqlKind) {
        case NATIVE:
            psm.addAll(v.accept(k, ops).first);
            interp.interpX(psm, ret);
            break;
        case H2:
        case JDBC:
        default:
            if (v instanceof InstExp.FullSigma) {
                List<PSM> xxx = v.accept(k, ops).first;
                if (xxx.size() != 1) {
                    throw new RuntimeException();
                }
                FullSigma yyy = (FullSigma) xxx.get(0);
                int theguid = getGuid(Stmt);
                interp.guid = theguid;
                yyy.exec(interp, ret);
                Stmt.execute("SET @guid = " + interp.guid);
                psm.addAll(makeInserts(k, ret, v.type(prog).toSig(prog), ((InstExp.FullSigma) v).F.toMap(prog).source));
            } else if (v instanceof Exp) {
                List<PSM> xxx = v.accept(k, ops).first;
                if (xxx.size() != 1) {
                    throw new RuntimeException();
                }
                ExpPSM yyy = (ExpPSM) xxx.get(0);
                int theguid = getGuid(Stmt);
                interp.guid = theguid;
                yyy.exec(interp, ret);
                Stmt.execute("SET @guid = " + interp.guid);
                psm.addAll(makeInserts(k, ret, v.type(prog).toSig(prog), null));
            } else if (v instanceof Two) {
                List<PSM> xxx = v.accept(k, ops).first;
                if (xxx.size() != 1) {
                    throw new RuntimeException();
                }
                PropPSM yyy = (PropPSM) xxx.get(0);
                int theguid = getGuid(Stmt);
                interp.guid = theguid;
                yyy.exec(interp, ret);
                Stmt.execute("SET @guid = " + interp.guid);
                psm.addAll(makeInserts(k, ret, v.type(prog).toSig(prog), null));
            } else if (v instanceof Kernel) {
                List<PSM> xxx = v.accept(k, ops).first;
                if (xxx.size() != 1) {
                    throw new RuntimeException();
                }
                PSMUnChi yyy = (PSMUnChi) xxx.get(0);
                int theguid = getGuid(Stmt);
                interp.guid = theguid;
                yyy.exec(interp, ret);
                Stmt.execute("SET @guid = " + interp.guid);
                psm.addAll(makeInserts(k, ret, v.type(prog).toSig(prog), null));
                Signature ooo = v.type(prog).toSig(prog);
                for (Node n : ooo.nodes) {
                    psm.add(new SimpleCreateTable(k + "_trans_" + n.string, PSM.VARCHAR(), false));
                }
                for (Edge n : ooo.edges) {
                    psm.add(new SimpleCreateTable(k + "_trans_" + n.name, PSM.VARCHAR(), false));
                }
                for (Attribute<Node> n : ooo.attrs) {
                    psm.add(new SimpleCreateTable(k + "_trans_" + n.name, n.target.psm(), false));
                }
                psm.addAll(makeInserts(k + "_trans", ret, v.type(prog).toSig(prog), null));
            } else if (v instanceof External && DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.H2) {
            } else {
                psm.addAll(v.accept(k, ops).first);
            }
            for (PSM sql : psm) {
                Stmt.execute(sql.toPSM());
            }
            if (!(v instanceof InstExp.FullSigma) && !(v instanceof Exp) && !(v instanceof Two) && !(v instanceof Kernel)) {
                gatherInstance(prog, ret, Stmt, k, v);
            }
            if (v instanceof Delta) {
                gatherSubstInv(prog, ret, Stmt, k, v);
            } else if (v instanceof Times) {
                gatherTransform(prog, ret, Stmt, k + "_fst", v.type(prog).toConst(prog));
                gatherTransform(prog, ret, Stmt, k + "_snd", v.type(prog).toConst(prog));
            } else if (v instanceof One) {
                gatherSubstInv2(prog, ret, Stmt, k, v);
            }
            break;
    }
    return psm;
}
Also used : ExpPSM(catdata.fql.sql.ExpPSM) Attribute(catdata.fql.decl.Attribute) Node(catdata.fql.decl.Node) One(catdata.fql.decl.InstExp.One) PSMUnChi(catdata.fql.sql.PSMUnChi) PSM(catdata.fql.sql.PSM) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) PropPSM(catdata.fql.sql.PropPSM) Two(catdata.fql.decl.InstExp.Two) LinkedList(java.util.LinkedList) InstExp(catdata.fql.decl.InstExp) SimpleCreateTable(catdata.fql.sql.SimpleCreateTable) Delta(catdata.fql.decl.InstExp.Delta) Signature(catdata.fql.decl.Signature) Times(catdata.fql.decl.InstExp.Times) External(catdata.fql.decl.InstExp.External) FullSigma(catdata.fql.sql.FullSigma) LinkedList(java.util.LinkedList) List(java.util.List) Exp(catdata.fql.decl.InstExp.Exp) InstExp(catdata.fql.decl.InstExp) TransExp(catdata.fql.decl.TransExp) Kernel(catdata.fql.decl.InstExp.Kernel) Edge(catdata.fql.decl.Edge)

Example 3 with InstExp

use of catdata.fql.decl.InstExp in project fql by CategoricalData.

the class JDBCBridge method maybeExecTransform.

private static List<PSM> maybeExecTransform(InstOps ops, FQLProgram prog, Statement Stmt, String k, TransExp v, PSMInterp interp, Map<String, Set<Map<Object, Object>>> ret) throws SQLException {
    List<PSM> psm = new LinkedList<>();
    Pair<String, String> val = prog.transforms.get(k).type(prog);
    InstExp i = prog.insts.get(val.first);
    Const ss = i.type(prog).toConst(prog);
    Signature s = ss.toSig(prog);
    psm.addAll(PSMGen.makeTables(k, s, false));
    switch(DefunctGlobalOptions.debug.fql.sqlKind) {
        case NATIVE:
            psm.addAll(v.accept(k, ops));
            interp.interpX(psm, ret);
            break;
        case H2:
        case JDBC:
        default:
            if (v instanceof TransExp.External && DefunctGlobalOptions.debug.fql.sqlKind == SQLKIND.H2) {
            } else {
                psm.addAll(v.accept(k, ops));
            }
            maybeExec(psm, Stmt, ret, interp, s);
            if (v.gather()) {
                gatherTransform(prog, ret, Stmt, k, ss);
            // have non SQL transform output into temps, so can gather them like any other
            }
            break;
    }
    return psm;
}
Also used : InstExp(catdata.fql.decl.InstExp) Const(catdata.fql.decl.SigExp.Const) Signature(catdata.fql.decl.Signature) TransExp(catdata.fql.decl.TransExp) PSM(catdata.fql.sql.PSM) PropPSM(catdata.fql.sql.PropPSM) ExpPSM(catdata.fql.sql.ExpPSM) LinkedList(java.util.LinkedList)

Example 4 with InstExp

use of catdata.fql.decl.InstExp in project fql by CategoricalData.

the class TransChecker method visit.

@SuppressWarnings("unused")
@Override
public Pair<String, String> visit(FQLProgram env, Const e) {
    InstExp src = env.insts.get(e.src);
    if (src == null) {
        throw new RuntimeException("Missing instance " + e.src);
    }
    InstExp dst = env.insts.get(e.dst);
    if (dst == null) {
        throw new RuntimeException("Missing instance " + e.src);
    }
    if (!(src instanceof InstExp.Const)) {
        throw new RuntimeException(e.src + " is not a constant.");
    }
    if (!(dst instanceof InstExp.Const)) {
        throw new RuntimeException(e.dst + " is not a constant.");
    }
    InstExp.Const src0 = (InstExp.Const) src;
    InstExp.Const dst0 = (InstExp.Const) dst;
    SigExp srct = src0.type(env);
    SigExp dstt = dst0.type(env);
    if (!srct.equals(dstt)) {
        throw new RuntimeException("Instances not of same type on " + e + " are " + srct + " and " + dstt);
    }
    Signature sig = srct.toSig(env);
    List<Pair<String, List<Pair<Object, Object>>>> bbb = e.objs;
    try {
        new Transform(new Instance(sig, src0.data), new Instance(sig, dst0.data), bbb);
    } catch (FQLException fe) {
        fe.printStackTrace();
        throw new RuntimeException(fe.getLocalizedMessage());
    }
    return new Pair<>(e.src, e.dst);
}
Also used : InstExp(catdata.fql.decl.InstExp) FQLException(catdata.fql.FQLException) SigExp(catdata.fql.decl.SigExp) Instance(catdata.fql.decl.Instance) Const(catdata.fql.decl.TransExp.Const) Signature(catdata.fql.decl.Signature) Transform(catdata.fql.decl.Transform) Pair(catdata.Pair)

Example 5 with InstExp

use of catdata.fql.decl.InstExp in project fql by CategoricalData.

the class TransChecker method visit.

@Override
public Pair<String, String> visit(FQLProgram env, TransEval e) {
    InstExp k = env.insts.get(e.inst);
    if (k == null) {
        throw new RuntimeException("Missing instance: " + e.inst);
    }
    if (!(k instanceof Times)) {
        throw new RuntimeException("Not a product: " + k + " in " + e);
    }
    Times t = (Times) k;
    InstExp v = env.insts.get(t.a);
    if (!(v instanceof Exp)) {
        throw new RuntimeException("Not an exponential: " + v + " in " + e);
    }
    Exp i = (Exp) v;
    if (!(t.b.equals(i.b))) {
        throw new RuntimeException("Exponent and product do not match in " + e);
    }
    return new Pair<>(e.inst, i.a);
}
Also used : InstExp(catdata.fql.decl.InstExp) Times(catdata.fql.decl.InstExp.Times) Exp(catdata.fql.decl.InstExp.Exp) TransExp(catdata.fql.decl.TransExp) SigExp(catdata.fql.decl.SigExp) InstExp(catdata.fql.decl.InstExp) Pair(catdata.Pair)

Aggregations

InstExp (catdata.fql.decl.InstExp)30 Pair (catdata.Pair)24 Times (catdata.fql.decl.InstExp.Times)9 SigExp (catdata.fql.decl.SigExp)8 TransExp (catdata.fql.decl.TransExp)8 Two (catdata.fql.decl.InstExp.Two)6 Exp (catdata.fql.decl.InstExp.Exp)5 One (catdata.fql.decl.InstExp.One)5 Plus (catdata.fql.decl.InstExp.Plus)5 Kernel (catdata.fql.decl.InstExp.Kernel)4 External (catdata.fql.decl.InstExp.External)3 Zero (catdata.fql.decl.InstExp.Zero)3 Signature (catdata.fql.decl.Signature)3 ExpPSM (catdata.fql.sql.ExpPSM)3 PSM (catdata.fql.sql.PSM)3 PropPSM (catdata.fql.sql.PropPSM)3 LinkedList (java.util.LinkedList)3 FQLException (catdata.fql.FQLException)2 FullQueryExp (catdata.fql.decl.FullQueryExp)2 Delta (catdata.fql.decl.InstExp.Delta)2