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());
}
}
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;
}
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();
}
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());
}
}
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);
}
Aggregations