use of catdata.Triple 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.Triple in project fql by CategoricalData.
the class FinFunctor method toMapping.
/**
* Converts a functor to a mapping by converting the source and target
* categories
*
* @return the mapping, and a bunch of isomorphisms
* @throws FQLException
*/
public Triple<Mapping, Quad<Signature, Pair<Map<ObjA, String>, Map<String, ObjA>>, Pair<Map<Arr<ObjA, ArrowA>, String>, Map<String, Arr<ObjA, ArrowA>>>, Pair<Map<Attribute<ObjA>, String>, Map<String, Attribute<ObjA>>>>, Quad<Signature, Pair<Map<ObjB, String>, Map<String, ObjB>>, Pair<Map<Arr<ObjB, ArrowB>, String>, Map<String, Arr<ObjB, ArrowB>>>, Pair<Map<Attribute<ObjB>, String>, Map<String, Attribute<ObjB>>>>> toMapping(Map<String, Type> types) throws FQLException {
Quad<Signature, Pair<Map<ObjA, String>, Map<String, ObjA>>, Pair<Map<Arr<ObjA, ArrowA>, String>, Map<String, Arr<ObjA, ArrowA>>>, Pair<Map<Attribute<ObjA>, String>, Map<String, Attribute<ObjA>>>> src = srcCat.toSig(types);
Quad<Signature, Pair<Map<ObjB, String>, Map<String, ObjB>>, Pair<Map<Arr<ObjB, ArrowB>, String>, Map<String, Arr<ObjB, ArrowB>>>, Pair<Map<Attribute<ObjB>, String>, Map<String, Attribute<ObjB>>>> dst = dstCat.toSig(types);
Signature srcSig = src.first;
Signature dstSig = dst.first;
Map<Arr<ObjA, ArrowA>, String> srcM = src.third.first;
Map<ObjA, String> srcM2 = src.second.first;
Map<Attribute<ObjA>, String> srcMA = src.fourth.first;
Map<Arr<ObjB, ArrowB>, String> dstM = dst.third.first;
Map<ObjB, String> dstM2 = dst.second.first;
Map<Attribute<ObjB>, String> dstMA = dst.fourth.first;
List<Pair<String, String>> nm = new LinkedList<>();
List<Pair<String, List<String>>> em = new LinkedList<>();
for (Arr<ObjA, ArrowA> a : srcCat.arrows) {
if (srcCat.isId(a)) {
ObjA o = a.src;
ObjB u = objMapping.get(o);
nm.add(new Pair<>(srcM2.get(o), dstM2.get(u)));
} else {
Arr<ObjB, ArrowB> u = arrowMapping.get(a);
List<String> t = new LinkedList<>();
t.add(dstM2.get(u.src));
if (!dstCat.isId(u)) {
t.add(dstM.get(u));
}
em.add(new Pair<>(srcM.get(a), t));
}
}
List<Pair<String, String>> am0 = new LinkedList<>();
for (Attribute<ObjA> k : am.keySet()) {
am0.add(new Pair<>(srcMA.get(k), dstMA.get(am.get(k))));
}
Mapping m = new Mapping(/*n,*/
srcSig, dstSig, nm, am0, em);
return new Triple<>(m, src, dst);
}
use of catdata.Triple in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Return e) {
// String xxx = "return_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Delta) {
String middle = ((Delta) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Delta) i1).F.toMap(prog);
if (i2 instanceof Sigma) {
Sigma input0 = ((Sigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + f.nm.get(n) + "_subst", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else if (i2 instanceof FullSigma) {
FullSigma input0 = ((FullSigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + n.string + "_e", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else {
throw new RuntimeException();
}
} else if (i1 instanceof Pi) {
String middle = ((Pi) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Pi) i1).F.toMap(prog);
if (i2 instanceof Delta) {
Delta input0 = ((Delta) i2);
String input = input0.I;
for (Node n : f.target.nodes) {
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, middle, e.inst).second;
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
if (col.length == 0) {
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
from.put("middle", input + "_" + n.string);
select.put("c0", new Pair<>("middle", "c0"));
select.put("c1", new Pair<>("lim", "guid"));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
return ret;
}
// LinkedHashMap<String, String> attrs = new LinkedHashMap<>();
// attrs.put("guid", PSM.VARCHAR());
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
int i = 0;
for (Triple<Node, Node, Arr<Node, Path>> col0 : col) {
from.put("c" + i + "_subst_inv", middle + "_" + col0.second.string + "_subst_inv");
where.add(new Pair<>(new Pair<>("lim", "c" + i), new Pair<>("c" + i + "_subst_inv", "c0")));
// attrs.put("c" + i, PSM.VARCHAR());
i++;
}
// if (col.length > 1) {
for (int j = 0; j < col.length; j++) {
if (col[j].third.arr.equals(f.target.toCategory2().second.of(new Path(f.target, n)).arr)) {
where.add(new Pair<>(new Pair<>("c" + 0 + "_subst_inv", "c1"), new Pair<>("c" + j + "_subst_inv", "c1")));
}
}
select.put("c" + 0, new Pair<>("c" + 0 + "_subst_inv", "c1"));
select.put("c1", new Pair<>("lim", "guid"));
// }
// ret.add(new CreateTable(xxx, attrs, false));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
use of catdata.Triple in project fql by CategoricalData.
the class Driver method makeEnv.
public static Triple<FqlEnvironment, String, List<Throwable>> makeEnv(FQLProgram prog, String... toUpdate) {
List<Throwable> exns = new LinkedList<>();
Map<String, Signature> sigs = new HashMap<>();
Map<String, Mapping> maps = new HashMap<>();
Map<String, Instance> insts = new HashMap<>();
Map<String, Query> queries = new HashMap<>();
Map<String, FullQuery> full_queries = new HashMap<>();
for (String k : prog.sigs.keySet()) {
try {
SigExp v = prog.sigs.get(k);
v.typeOf(prog);
sigs.put(k, v.toSig(prog));
toUpdate[0] = "Last Processed: " + k;
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "schema");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
for (String k : prog.maps.keySet()) {
try {
MapExp v = prog.maps.get(k);
v.type(prog);
maps.put(k, v.toMap(prog));
toUpdate[0] = "Last Processed: " + k;
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "mapping");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
for (String k : prog.queries.keySet()) {
try {
QueryExp v = prog.queries.get(k);
v.type(prog);
queries.put(k, Query.toQuery(prog, v));
toUpdate[0] = "Last Processed: " + k;
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "query");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
for (String k : prog.full_queries.keySet()) {
try {
FullQueryExp v = prog.full_queries.get(k);
v.type(prog);
full_queries.put(k, FullQuery.toQuery(prog, v));
toUpdate[0] = "Last Processed: " + k;
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "QUERY");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
for (String k : prog.insts.keySet()) {
try {
InstExp v = prog.insts.get(k);
v.type(prog);
toUpdate[0] = "Last Processed: " + v + " (type-check only)";
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "instance");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
for (String k : prog.transforms.keySet()) {
try {
TransExp v = prog.transforms.get(k);
v.type(prog);
toUpdate[0] = "Last Processed: " + v + " (type-check only)";
} catch (RuntimeException re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "transform");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
prog = rewriteQueries(prog);
toUpdate[0] = "SQL generation complete, executing.";
Triple<Map<String, Set<Map<Object, Object>>>, String, List<Throwable>> res = JDBCBridge.run(prog);
toUpdate[0] = "SQL Execution Complete";
exns.addAll(res.third);
for (String k : prog.insts.keySet()) {
try {
Signature s = prog.insts.get(k).type(prog).toSig(prog);
List<Pair<String, List<Pair<Object, Object>>>> b = PSMGen.gather(k, s, res.first);
insts.put(k, new Instance(s, b));
toUpdate[0] = "Last Processed: " + k;
} catch (Exception re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "instance");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
Map<String, Transform> transforms = new HashMap<>();
for (String k : prog.transforms.keySet()) {
try {
Pair<String, String> val = prog.transforms.get(k).type(prog);
InstExp i = prog.insts.get(val.first);
Signature s = i.type(prog).toSig(prog);
List<Pair<String, List<Pair<Object, Object>>>> b = PSMGen.gather(k, s, res.first);
transforms.put(k, new Transform(insts.get(val.first), insts.get(val.second), b));
toUpdate[0] = "Last Processed: " + k;
} catch (Exception re) {
re.printStackTrace();
LineException exn = new LineException(re.getLocalizedMessage(), k, "transform");
if (DefunctGlobalOptions.debug.fql.continue_on_error) {
exns.add(exn);
} else {
throw exn;
}
}
}
toUpdate[0] = "Load of SQL data into FQL complete.";
// check full sigmas with EDs
if (DefunctGlobalOptions.debug.fql.VALIDATE_WITH_EDS) {
try {
validateWithEds(prog, insts);
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getLocalizedMessage());
}
}
String toRetStr = res.second.trim();
if (containsFullSigma(prog)) {
toRetStr = "Cannot generate SQL for full sigma";
}
return new Triple<>(new FqlEnvironment(sigs, maps, insts, queries, transforms, full_queries), toRetStr, dedup(exns));
}
use of catdata.Triple in project fql by CategoricalData.
the class XPoly method compose.
public static <C, D, E> XPoly<C, E> compose(XPoly<C, D> Q, XPoly<D, E> Q0) {
XCtx<D> Qo = Q.o();
XPoly<D, E> Q0hat = Q0.hat();
XCtx<Pair<Object, Map<Object, Triple<D, D, List<D>>>>> labels = Q0hat.apply(Qo);
for (Pair<Object, Map<Object, Triple<D, D, List<D>>>> label : labels.terms()) {
Object l = label.first;
Object A = labels.type(label).second;
Map<Object, Triple<D, D, List<D>>> valuation = label.second;
XCtx<D> frc = Q0hat.freeze().apply(new Pair(l, Util.singList(A))).src;
Map<D, List<D>> map = new HashMap<>();
for (Object k : valuation.keySet()) {
List<D> list = new LinkedList<>(valuation.get(k).third);
list.add(0, valuation.get(k).first);
map.put((D) k, list);
}
for (Object o : frc.allTerms()) {
if (map.containsKey(o)) {
continue;
}
map.put((D) o, Util.singList((D) o));
}
XMapping<D, D> h = new XMapping<>(frc, Qo, map, "homomorphism");
XCtx<D> intQo = Q.grotho();
Map<D, Pair<D, D>> types = new HashMap<>();
for (D gen : frc.terms()) {
D ty = frc.type(gen).second;
List<D> hgen = h.em.get(gen);
if (hgen.size() != 2) {
throw new RuntimeException(gen + " mapsto " + hgen);
}
types.put(gen, new Pair("_1", new Pair(hgen.get(1), ty)));
}
XCtx<D> inth_pre = new XCtx<>(new HashSet<>(), types, new HashSet<>(), intQo.global, intQo, "instance");
Set<Pair<List<D>, List<D>>> new_eqs = new HashSet<>();
for (Pair<List<D>, List<D>> eq : h.src.eqs) {
try {
// morally should transform !, but can't so hack
inth_pre.type(eq.first);
inth_pre.type(eq.second);
new_eqs.add(eq);
} catch (Exception ex) {
}
}
XCtx<D> inth = new XCtx<>(new HashSet<>(), types, new_eqs, intQo.global, intQo, "instance");
/*XCtx<C> fr = */
Q.tilde().coapply(inth);
}
return (XPoly<C, E>) Q;
}
Aggregations