Search in sources :

Example 1 with TransExp

use of catdata.fql.decl.TransExp 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 TransExp

use of catdata.fql.decl.TransExp 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 3 with TransExp

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

the class FQLParser method toTrans.

@SuppressWarnings("rawtypes")
private static TransExp toTrans(Object o) {
    try {
        Tuple4 p = (Tuple4) o;
        String src = p.b.toString();
        String dst = p.c.toString();
        TransExp h = toTrans(p.d);
        String kind = p.a.toString();
        switch(kind) {
            case "delta":
                return new TransExp.Delta(h, src, dst);
            case "pi":
                return new TransExp.Pi(h, src, dst);
            case "sigma":
                return new TransExp.Sigma(h, src, dst);
            case "relationalize":
                return new TransExp.Relationalize(h, src, dst);
            default:
                throw new RuntimeException(o.toString());
        }
    } catch (RuntimeException ex) {
    }
    try {
        Tuple4 p = (Tuple4) o;
        String src = p.b.toString();
        String dst = p.c.toString();
        String name = p.d.toString();
        String kind = p.a.toString();
        switch(kind) {
            case "external":
                return new TransExp.External(src, dst, name);
            case "SIGMA":
                return new TransExp.FullSigma(name, src, dst);
            default:
                throw new RuntimeException(o.toString());
        }
    } catch (RuntimeException ex) {
    }
    try {
        Tuple4 p = (Tuple4) o;
        String obj = p.a.toString();
        String dst = p.d.toString();
        if (p.c.toString().equals("void")) {
            return new TransExp.FF(obj, dst);
        } else if (p.c.toString().equals("unit")) {
            return new TransExp.TT(obj, dst);
        } else if (p.c.toString().equals("curry")) {
            return new TransCurry(obj, dst);
        } else if (p.c.toString().equals("true")) {
            return new Bool(true, dst, obj);
        } else if (p.c.toString().equals("false")) {
            return new Bool(false, dst, obj);
        } else if (p.c.toString().equals("char")) {
            return new Chi(obj, dst);
        }
    } catch (RuntimeException re) {
    }
    try {
        Tuple3 p = (Tuple3) o;
        Object p2 = p.b;
        Object p3 = p.c;
        Object o1 = p.a;
        String p1 = p.a.toString();
        if (p1.equals("iso1")) {
            return new TransIso(true, p2.toString(), p3.toString());
        } else if (p1.equals("iso2")) {
            return new TransIso(false, p2.toString(), p3.toString());
        } else if (p3.toString().equals("fst")) {
            return new TransExp.Fst(p1);
        } else if (p3.toString().equals("not")) {
            return new Not(p1);
        } else if (p3.toString().equals("and")) {
            return new And(p1);
        } else if (p3.toString().equals("or")) {
            return new Or(p1);
        } else if (p3.toString().equals("implies")) {
            return new Implies(p1);
        } else if (p3.toString().equals("eval")) {
            return new TransEval(p1);
        } else if (p3.toString().equals("relationalize")) {
            return new Squash(p1);
        } else if (p3.toString().equals("snd")) {
            return new TransExp.Snd(p1);
        } else if (p3.toString().equals("return")) {
            return new Return(p1);
        } else if (p3.toString().equals("coreturn")) {
            return new Coreturn(p1);
        } else if (p3.toString().equals("inl")) {
            return new TransExp.Inl(p1);
        } else if (p3.toString().equals("kernel")) {
            return new UnChi(p1);
        } else if (p3.toString().equals("inr")) {
            return new TransExp.Inr(p1);
        } else if (p2.toString().equals("then")) {
            return new TransExp.Comp(toTrans(o1), toTrans(p3));
        } else if (p3 instanceof Tuple3) {
            Tuple3 y = (Tuple3) p3;
            String x = y.b.toString();
            switch(x) {
                case "+":
                    return new TransExp.Case(p1, toTrans(y.a), toTrans(y.c));
                case "*":
                    return new TransExp.Prod(p1, toTrans(y.a), toTrans(y.c));
                // return new TransExp.(p1, toTrans(y.a), toTrans(y.c));
                default:
                    throw new RuntimeException("foo");
            }
        }
    } catch (RuntimeException re) {
    }
    try {
        Tuple5 p = (Tuple5) o;
        Object p2 = p.c;
        Object p3 = p.e;
        Object o1 = p.a;
        return toTransConst(o1, p2.toString(), p3.toString());
    } catch (RuntimeException re) {
    }
    try {
        org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) o;
        String p1 = p.a.toString();
        Object p2 = p.b;
        if (p1.equals("id")) {
            return new TransExp.Id(p2.toString());
        }
    } catch (RuntimeException re) {
    }
    if (o instanceof String) {
        return new TransExp.Var(o.toString());
    }
    throw new RuntimeException();
}
Also used : FF(catdata.fql.decl.MapExp.FF) Or(catdata.fql.decl.TransExp.Or) Chi(catdata.fql.decl.TransExp.Chi) UnChi(catdata.fql.decl.TransExp.UnChi) Inl(catdata.fql.decl.MapExp.Inl) Var(catdata.fql.decl.FullQueryExp.Var) TransExp(catdata.fql.decl.TransExp) Sigma(catdata.fql.decl.FullQueryExp.Sigma) FullSigma(catdata.fql.decl.InstExp.FullSigma) UnChi(catdata.fql.decl.TransExp.UnChi) Inr(catdata.fql.decl.MapExp.Inr) Squash(catdata.fql.decl.TransExp.Squash) Case(catdata.fql.decl.MapExp.Case) Prod(catdata.fql.decl.MapExp.Prod) Implies(catdata.fql.decl.TransExp.Implies) Bool(catdata.fql.decl.TransExp.Bool) Pi(catdata.fql.decl.FullQueryExp.Pi) Pair(catdata.Pair) Relationalize(catdata.fql.decl.InstExp.Relationalize) Return(catdata.fql.decl.TransExp.Return) TransEval(catdata.fql.decl.TransExp.TransEval) Fst(catdata.fql.decl.MapExp.Fst) TransCurry(catdata.fql.decl.TransExp.TransCurry) Coreturn(catdata.fql.decl.TransExp.Coreturn) TransIso(catdata.fql.decl.TransExp.TransIso) Tuple4(org.jparsec.functors.Tuple4) Tuple5(org.jparsec.functors.Tuple5) Not(catdata.fql.decl.TransExp.Not) Delta(catdata.fql.decl.FullQueryExp.Delta) And(catdata.fql.decl.TransExp.And) Tuple3(org.jparsec.functors.Tuple3) External(catdata.fql.decl.InstExp.External) FullSigma(catdata.fql.decl.InstExp.FullSigma) Id(catdata.fql.decl.MapExp.Id)

Example 4 with TransExp

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

the class FqlCodeEditor method vedit.

public void vedit() {
    FQLProgram init = tryParse(topArea.getText());
    if (init == null) {
        respArea.setText(toDisplay);
        return;
    }
    if (init.lines.isEmpty()) {
        return;
    }
    String which = null;
    int start = -1;
    int offs = topArea.getCaretPosition();
    int end = -1;
    int i = 0;
    int pos = 0;
    for (String k : init.lines.keySet()) {
        Integer v = init.lines.get(k);
        if (v < offs && v > start) {
            start = v;
            which = k;
            pos = i;
        }
        i++;
    }
    if (which == null) {
        throw new RuntimeException();
    }
    int j = 0;
    for (String k : init.lines.keySet()) {
        if (j == pos + 1) {
            end = init.lines.get(k);
            break;
        }
        j++;
    }
    if (end == -1) {
        end = topArea.getText().length();
    }
    InstExp ie = init.insts.get(which);
    TransExp te = init.transforms.get(which);
    if ((ie == null && te == null) || (ie != null && !(ie instanceof InstExp.Const)) || (te != null && !(te instanceof Const))) {
        respArea.setText("Cannot visually edit " + which + ": only constant instances or transforms are visually editable.");
        return;
    }
    try {
        if (ie != null) {
            InstExp.Const iec = (InstExp.Const) ie;
            InstExp.Const n = new InstanceEditor(which, iec.sig.toSig(init), iec).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "instance " + which + " = " + n + " : " + n.sig + "\n\n";
            topArea.replaceRange(newText, start, end);
        } else {
            Const iec = (Const) te;
            if (iec == null) {
                throw new RuntimeException("Anomaly: please report");
            }
            InstExp.Const s = (InstExp.Const) init.insts.get(iec.src);
            InstExp.Const t = (InstExp.Const) init.insts.get(iec.dst);
            Const n = new TransformEditor(which, init.insts.get(iec.src).type(init).toSig(init), iec, s, t).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "transform " + which + " = " + n + " : " + n.src + " -> " + n.dst + "\n\n";
            topArea.replaceRange(newText, start, end);
        }
    } catch (FQLException fe) {
        fe.printStackTrace();
        respArea.setText(fe.getLocalizedMessage());
    }
}
Also used : InstExp(catdata.fql.decl.InstExp) FQLException(catdata.fql.FQLException) TransExp(catdata.fql.decl.TransExp) Const(catdata.fql.decl.TransExp.Const) InstanceEditor(catdata.fql.decl.InstanceEditor) FQLProgram(catdata.fql.decl.FQLProgram) TransformEditor(catdata.fql.decl.TransformEditor)

Example 5 with TransExp

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

the class TransChecker method visit.

@Override
public Pair<String, String> visit(FQLProgram env, Chi e) {
    InstExp prop = env.insts.get(e.prop);
    if (prop == null) {
        throw new RuntimeException("Missing instance " + e.prop);
    }
    if (!(prop instanceof Two)) {
        throw new RuntimeException("Not a prop " + e);
    }
    if (seen.contains(e.trans)) {
        throw new RuntimeException("Circular transform " + e);
    }
    seen.add(e.trans);
    TransExp t = env.transforms.get(e.trans);
    if (t == null) {
        throw new RuntimeException("Missing transform " + e.trans);
    }
    Pair<String, String> k = t.accept(env, this);
    return new Pair<>(k.second, e.prop);
}
Also used : InstExp(catdata.fql.decl.InstExp) TransExp(catdata.fql.decl.TransExp) Two(catdata.fql.decl.InstExp.Two) Pair(catdata.Pair)

Aggregations

TransExp (catdata.fql.decl.TransExp)5 InstExp (catdata.fql.decl.InstExp)4 Pair (catdata.Pair)2 ExpPSM (catdata.fql.sql.ExpPSM)2 PSM (catdata.fql.sql.PSM)2 PropPSM (catdata.fql.sql.PropPSM)2 LinkedList (java.util.LinkedList)2 LineException (catdata.LineException)1 Triple (catdata.Triple)1 FQLException (catdata.fql.FQLException)1 FQLProgram (catdata.fql.decl.FQLProgram)1 Delta (catdata.fql.decl.FullQueryExp.Delta)1 Pi (catdata.fql.decl.FullQueryExp.Pi)1 Sigma (catdata.fql.decl.FullQueryExp.Sigma)1 Var (catdata.fql.decl.FullQueryExp.Var)1 External (catdata.fql.decl.InstExp.External)1 FullSigma (catdata.fql.decl.InstExp.FullSigma)1 Relationalize (catdata.fql.decl.InstExp.Relationalize)1 Two (catdata.fql.decl.InstExp.Two)1 InstOps (catdata.fql.decl.InstOps)1