use of catdata.Triple in project fql by CategoricalData.
the class SqlToFql method transSQLSchema.
private static String transSQLSchema(List<EExternal> in, int depth) {
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
List<Triple<String, String, String>> arrows = new LinkedList<>();
List<Triple<String, String, String>> attrs = new LinkedList<>();
List<String> nodes = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> inodes = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> iattrs = new LinkedList<>();
List<Pair<String, List<Pair<Object, Object>>>> iarrows = new LinkedList<>();
Set<String> seen = new HashSet<>();
Map<String, List<String>> cols = new HashMap<>();
for (EExternal k0 : in) {
if (k0 instanceof ECreateTable) {
ECreateTable k = (ECreateTable) k0;
if (seen.contains(k.name)) {
throw new RuntimeException("Duplicate name: " + k.name);
}
seen.add(k.name);
nodes.add(k.name);
inodes.add(new Pair<>(k.name, new LinkedList<>()));
boolean found = false;
List<String> lcols = new LinkedList<>();
for (Pair<String, String> col : k.types) {
lcols.add(col.first);
if (col.first.equals("id")) {
found = true;
continue;
}
String ref = lookup(col.first, k.fks);
if (ref == null) {
String col_t = col.second.equals("int") ? "int" : "string";
attrs.add(new Triple<>(k.name + "_" + col.first, k.name, col_t));
iattrs.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
} else {
if (!nodes.contains(ref)) {
throw new RuntimeException("Missing table " + ref + " in " + k + " (or cyclic schema with loop length > 1)");
}
arrows.add(new Triple<>(k.name + "_" + col.first, k.name, ref));
iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
if (ref.equals(k.name)) {
List<String> lhs = deep(depth - 1, k.name + "_" + col.first);
lhs.add(0, k.name);
List<String> rhs = deep(depth, k.name + "_" + col.first);
rhs.add(0, k.name);
eqs.add(new Pair<>(lhs, rhs));
}
}
}
if (!found) {
throw new RuntimeException("No id column in " + k);
}
for (Pair<String, String> fk : k.fks) {
if (fk.first.equals("id")) {
throw new RuntimeException("Primary keys cannot be foreign keys.");
}
if (lookup(fk.first, k.types) == null) {
throw new RuntimeException("Missing column " + fk.first + " in " + fk);
}
}
cols.put(k.name, lcols);
}
// add inst_ prefix below
if (k0 instanceof EInsertValues) {
EInsertValues k = (EInsertValues) k0;
List<String> lcols = cols.get(k.target);
for (List<String> tuple : k.values) {
if (lcols.size() != tuple.size()) {
throw new RuntimeException("Column size mismatch " + tuple + " in " + k.target);
}
List<Pair<Object, Object>> node = lookup2(k.target, inodes);
if (node == null) {
throw new RuntimeException("Missing table " + k.target);
}
node.add(new Pair<>(tuple.get(0), tuple.get(0)));
for (int colNum = 1; colNum < tuple.size(); colNum++) {
List<Pair<Object, Object>> xxx = lookup2(k.target + "_" + lcols.get(colNum), iattrs);
if (xxx == null) {
xxx = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
}
if (xxx == null) {
throw new RuntimeException("Anomaly: please report");
}
xxx.add(new Pair<>(tuple.get(0), maybeQuote(tuple.get(colNum))));
}
}
}
}
SigExp.Const exp = new SigExp.Const(nodes, attrs, arrows, eqs);
Const inst = new Const(inodes, iattrs, iarrows, new Var("S"));
return "schema S = " + exp + "\n\ninstance I = " + inst + " : S";
}
use of catdata.Triple in project fql by CategoricalData.
the class SigOps method one.
private static Pair<SigExp.Const, Fn<SigExp.Const, Const>> one(Set<String> at) {
List<String> nodes = new LinkedList<>();
nodes.add("node0");
List<Triple<String, String, String>> attrs = new LinkedList<>();
for (String k : at) {
attrs.add(new Triple<>(k + "_attr", "node0", k));
}
List<Triple<String, String, String>> arrows = new LinkedList<>();
SigExp.Const sig = new SigExp.Const(nodes, attrs, arrows, new LinkedList<>());
Fn<SigExp.Const, Const> fn = (SigExp.Const src) -> {
List<Pair<String, String>> nm = new LinkedList<>();
for (String k : src.nodes) {
nm.add(new Pair<>(k, "node0"));
}
List<Pair<String, String>> am = new LinkedList<>();
for (Triple<String, String, String> k : src.attrs) {
if (!at.contains(k.third)) {
throw new RuntimeException("Enum/type not found: " + k.third);
}
am.add(new Pair<>(k.first, k.third + "_attr"));
}
List<Pair<String, List<String>>> em = new LinkedList<>();
for (Triple<String, String, String> k : src.arrows) {
List<String> l = new LinkedList<>();
l.add("node0");
em.add(new Pair<>(k.first, l));
}
return new Const(nm, am, em, src, sig);
};
return new Pair<>(sig, fn);
}
use of catdata.Triple in project fql by CategoricalData.
the class SigOps method prod.
private static Pair<Quad<SigExp.Const, Const, Const, Fn<Triple<SigExp.Const, Const, Const>, Const>>, Quad<Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>, Map<Pair<String, String>, String>>> prod(SigExp.Const a, SigExp.Const b) {
int node_count = 0;
Map<Pair<String, String>, String> node_map = new LinkedHashMap<>();
// fst
List<Pair<String, String>> a_objs = new LinkedList<>();
// snd
List<Pair<String, String>> b_objs = new LinkedList<>();
for (String n : a.nodes) {
for (String m : b.nodes) {
node_map.put(new Pair<>(n, m), "node" + node_count);
a_objs.add(new Pair<>("node" + node_count, n));
b_objs.add(new Pair<>("node" + node_count, m));
node_count++;
}
}
List<String> nodes = new LinkedList<>();
nodes.addAll(node_map.values());
int attr_count = 0;
Map<Pair<String, String>, String> attr_map = new LinkedHashMap<>();
// fst
List<Pair<String, String>> a_attrs = new LinkedList<>();
// snd
List<Pair<String, String>> b_attrs = new LinkedList<>();
List<Triple<String, String, String>> attrs = new LinkedList<>();
for (Triple<String, String, String> n : a.attrs) {
for (Triple<String, String, String> m : b.attrs) {
if (!n.third.equals(m.third)) {
continue;
}
String k = node_map.get(new Pair<>(n.second, m.second));
attrs.add(new Triple<>("attr" + attr_count, k, n.third));
attr_map.put(new Pair<>(n.first, m.first), "attr" + attr_count);
a_attrs.add(new Pair<>("attr" + attr_count, n.first));
b_attrs.add(new Pair<>("attr" + attr_count, m.first));
attr_count++;
}
}
int edge_count = 0;
Map<Pair<String, String>, String> edge_map_1 = new LinkedHashMap<>();
Map<Pair<String, String>, String> edge_map_2 = new LinkedHashMap<>();
// fst
List<Pair<String, List<String>>> a_edges = new LinkedList<>();
// snd
List<Pair<String, List<String>>> b_edges = new LinkedList<>();
List<Triple<String, String, String>> edges = new LinkedList<>();
for (Triple<String, String, String> n : a.arrows) {
for (String m : b.nodes) {
String k1 = node_map.get(new Pair<>(n.second, m));
String k2 = node_map.get(new Pair<>(n.third, m));
edges.add(new Triple<>("edge" + edge_count, k1, k2));
edge_map_1.put(new Pair<>(n.first, m), "edge" + edge_count);
List<String> al = new LinkedList<>();
al.add(n.second);
al.add(n.first);
a_edges.add(new Pair<>("edge" + edge_count, al));
List<String> bl = new LinkedList<>();
bl.add(m);
b_edges.add(new Pair<>("edge" + edge_count, bl));
edge_count++;
}
}
for (Triple<String, String, String> n : b.arrows) {
for (String m : a.nodes) {
String k1 = node_map.get(new Pair<>(m, n.second));
String k2 = node_map.get(new Pair<>(m, n.third));
edges.add(new Triple<>("edge" + edge_count, k1, k2));
edge_map_2.put(new Pair<>(n.first, m), "edge" + edge_count);
List<String> al = new LinkedList<>();
al.add(n.second);
al.add(n.first);
b_edges.add(new Pair<>("edge" + edge_count, al));
List<String> bl = new LinkedList<>();
bl.add(m);
a_edges.add(new Pair<>("edge" + edge_count, bl));
edge_count++;
}
}
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
for (Triple<String, String, String> n : a.arrows) {
for (Triple<String, String, String> m : b.arrows) {
List<String> lhs = new LinkedList<>();
List<String> rhs = new LinkedList<>();
String src = node_map.get(new Pair<>(n.second, m.second));
String lhs1 = edge_map_1.get(new Pair<>(n.first, m.second));
String lhs2 = edge_map_2.get(new Pair<>(m.first, n.third));
lhs.add(src);
lhs.add(lhs1);
lhs.add(lhs2);
String rhs1 = edge_map_2.get(new Pair<>(m.first, n.second));
String rhs2 = edge_map_1.get(new Pair<>(n.first, m.third));
rhs.add(src);
rhs.add(rhs1);
rhs.add(rhs2);
eqs.add(new Pair<>(lhs, rhs));
}
}
for (Pair<List<String>, List<String>> eqA : a.eqs) {
for (String srcB : b.nodes) {
List<String> lhsA = new LinkedList<>(eqA.first);
List<String> rhsA = new LinkedList<>(eqA.second);
String srcA = lhsA.remove(0);
rhsA.remove(0);
String src = node_map.get(new Pair<>(srcA, srcB));
List<String> lhs = new LinkedList<>();
List<String> rhs = new LinkedList<>();
lhs.add(src);
rhs.add(src);
for (String k : lhsA) {
lhs.add(edge_map_1.get(new Pair<>(k, srcB)));
}
for (String k : rhsA) {
rhs.add(edge_map_1.get(new Pair<>(k, srcB)));
}
eqs.add(new Pair<>(lhs, rhs));
}
}
for (Pair<List<String>, List<String>> eqA : b.eqs) {
for (String srcB : a.nodes) {
List<String> lhsA = new LinkedList<>(eqA.first);
List<String> rhsA = new LinkedList<>(eqA.second);
String srcA = lhsA.remove(0);
rhsA.remove(0);
String src = node_map.get(new Pair<>(srcB, srcA));
List<String> lhs = new LinkedList<>();
List<String> rhs = new LinkedList<>();
lhs.add(src);
rhs.add(src);
for (String k : lhsA) {
lhs.add(edge_map_2.get(new Pair<>(k, srcB)));
}
for (String k : rhsA) {
rhs.add(edge_map_2.get(new Pair<>(k, srcB)));
}
eqs.add(new Pair<>(lhs, rhs));
}
}
SigExp.Const sig = new SigExp.Const(nodes, attrs, edges, eqs);
Const fst = new Const(a_objs, a_attrs, a_edges, sig, a);
Const snd = new Const(b_objs, b_attrs, b_edges, sig, b);
Fn<Triple<SigExp.Const, Const, Const>, Const> pair = x -> {
SigExp.Const c = x.first;
Const f = x.second;
Const g = x.third;
if (!f.src.equals(g.src)) {
throw new RuntimeException("Sources don't agree: " + f.src + " and " + g.src);
}
if (!f.dst.equals(a)) {
throw new RuntimeException("Target of " + f + " is not " + a);
}
if (!g.dst.equals(b)) {
throw new RuntimeException("Target of " + g + "is not " + b);
}
List<Pair<String, String>> objs = new LinkedList<>();
for (String obj_c : c.nodes) {
objs.add(new Pair<>(obj_c, node_map.get(new Pair<>(lookup(obj_c, f.objs), lookup(obj_c, g.objs)))));
}
List<Pair<String, String>> attrs1 = new LinkedList<>();
for (Triple<String, String, String> attr_c : c.attrs) {
attrs1.add(new Pair<>(attr_c.first, attr_map.get(new Pair<>(lookup(attr_c.first, f.attrs), lookup(attr_c.first, g.attrs)))));
}
List<Pair<String, List<String>>> arrows = new LinkedList<>();
for (Triple<String, String, String> edge_c : c.arrows) {
List<String> fc = lookup(edge_c.first, f.arrows);
List<String> gc = lookup(edge_c.first, g.arrows);
List<String> ret = new LinkedList<>();
String fcN = fc.get(0);
String gcN = gc.get(0);
String node_start = node_map.get(new Pair<>(fcN, gcN));
ret.add(node_start);
for (int i = 1; i < fc.size(); i++) {
String fcE = fc.get(i);
Pair<String, String> p = new Pair<>(fcE, gcN);
String v = edge_map_1.get(p);
ret.add(v);
}
node_start = lookup(edge_c.third, f.objs);
for (int i = 1; i < gc.size(); i++) {
String gcE = gc.get(i);
Pair<String, String> p = new Pair<>(gcE, node_start);
String v = edge_map_2.get(p);
ret.add(v);
}
arrows.add(new Pair<>(edge_c.first, ret));
}
Const ret = new Const(objs, attrs1, arrows, c, sig);
return ret;
};
return new Pair<>(new Quad<>(sig, fst, snd, pair), new Quad<>(node_map, attr_map, edge_map_1, edge_map_2));
}
use of catdata.Triple in project fql by CategoricalData.
the class ToFullQueryExp method visit.
@Override
public FullQueryExp visit(FQLProgram env, Match e) {
try {
Const s = e.src.typeOf(env).toConst(env);
Const t = e.dst.typeOf(env).toConst(env);
Pair<Map<Set<Pair<String, String>>, String>, Map<Set<Pair<String, String>>, String>> xxx = computeEqCs(s, t, e.rel);
Map<Set<Pair<String, String>>, String> node_map = xxx.first;
Map<Set<Pair<String, String>>, String> attr_map = xxx.second;
Set<Pair<List<String>, List<String>>> eqs = new HashSet<>();
Set<Triple<String, String, String>> arrows = new HashSet<>();
Set<String> nodes = new HashSet<>();
Set<Triple<String, String, String>> attrs = new HashSet<>();
List<Pair<String, String>> inj1Node = new LinkedList<>();
List<Pair<String, String>> inj1Attrs = new LinkedList<>();
List<Pair<String, String>> inj2Node = new LinkedList<>();
List<Pair<String, String>> inj2Attrs = new LinkedList<>();
List<Pair<String, List<String>>> inj2Arrows = new LinkedList<>();
List<Pair<String, List<String>>> inj1Arrows = new LinkedList<>();
for (Triple<String, String, String> att : s.attrs) {
String eqc = lookupAttr("left", att, attr_map);
attrs.add(new Triple<>(eqc, lookupNode("left", att.second, node_map), att.third));
inj1Attrs.add(new Pair<>(att.first, eqc));
}
for (Triple<String, String, String> att : t.attrs) {
String eqc = lookupAttr("right", att, attr_map);
attrs.add(new Triple<>(eqc, lookupNode("right", att.second, node_map), att.third));
inj2Attrs.add(new Pair<>(att.first, eqc));
}
for (String n : s.nodes) {
String eqc = lookupNode("left", n, node_map);
nodes.add(eqc);
inj1Node.add(new Pair<>(n, eqc));
}
for (String n : t.nodes) {
String eqc = lookupNode("right", n, node_map);
nodes.add(eqc);
inj2Node.add(new Pair<>(n, eqc));
}
for (Triple<String, String, String> n : s.arrows) {
String eqc1 = lookupNode("left", n.second, node_map);
String eqc2 = lookupNode("left", n.third, node_map);
arrows.add(new Triple<>("left_" + n.first, eqc1, eqc2));
List<String> l = new LinkedList<>();
l.add(eqc1);
l.add("left_" + n.first);
inj1Arrows.add(new Pair<>(n.first, l));
}
for (Triple<String, String, String> n : t.arrows) {
String eqc1 = lookupNode("right", n.second, node_map);
String eqc2 = lookupNode("right", n.third, node_map);
arrows.add(new Triple<>("right_" + n.first, eqc1, eqc2));
List<String> l = new LinkedList<>();
l.add(eqc1);
l.add("right_" + n.first);
inj2Arrows.add(new Pair<>(n.first, l));
}
for (Pair<List<String>, List<String>> eq : s.eqs) {
List<String> lhs = new LinkedList<>();
lhs.add(lookupNode("left", eq.first.get(0), node_map));
for (int i = 1; i < eq.first.size(); i++) {
lhs.add("left_" + eq.first.get(i));
}
List<String> rhs = new LinkedList<>();
rhs.add(lookupNode("left", eq.second.get(0), node_map));
for (int i = 1; i < eq.second.size(); i++) {
rhs.add("left_" + eq.second.get(i));
}
eqs.add(new Pair<>(lhs, rhs));
}
for (Pair<List<String>, List<String>> eq : t.eqs) {
List<String> lhs = new LinkedList<>();
lhs.add(lookupNode("right", eq.first.get(0), node_map));
for (int i = 1; i < eq.first.size(); i++) {
lhs.add("right_" + eq.first.get(i));
}
List<String> rhs = new LinkedList<>();
rhs.add(lookupNode("right", eq.second.get(0), node_map));
for (int i = 1; i < eq.second.size(); i++) {
rhs.add("right_" + eq.second.get(i));
}
eqs.add(new Pair<>(lhs, rhs));
}
Const x = new Const(new LinkedList<>(nodes), new LinkedList<>(attrs), new LinkedList<>(arrows), new LinkedList<>(eqs));
MapExp.Const inj1 = new MapExp.Const(inj1Node, inj1Attrs, inj1Arrows, s, x);
MapExp.Const inj2 = new MapExp.Const(inj2Node, inj2Attrs, inj2Arrows, t, x);
switch(e.kind) {
case "delta sigma forward":
{
FullQueryExp q = new Comp(new Sigma(inj2), new Delta(inj1));
return q;
}
case "delta pi forward":
{
FullQueryExp q = new Comp(new Pi(inj2), new Delta(inj1));
return q;
}
case "delta sigma backward":
{
FullQueryExp q = new Comp(new Sigma(inj1), new Delta(inj2));
return q;
}
case "delta pi backward":
FullQueryExp q = new Comp(new Pi(inj1), new Delta(inj2));
return q;
default:
break;
}
throw new RuntimeException("Unknown kind: " + e.kind);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getLocalizedMessage());
}
}
use of catdata.Triple in project fql by CategoricalData.
the class FQLParser method toSchemaConst.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static SigExp toSchemaConst(Object y) {
List<String> nodes = new LinkedList<>();
List<Triple<String, String, String>> attrs = new LinkedList<>();
List<Triple<String, String, String>> arrows = new LinkedList<>();
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
Tuple4 s = (Tuple4) y;
Tuple3 nodes0 = (Tuple3) s.a;
Tuple3 attrs0 = (Tuple3) s.b;
Tuple3 arrows0 = (Tuple3) s.c;
Tuple3 eqs0 = (Tuple3) s.d;
List nodes1 = (List) nodes0.b;
List arrows1 = (List) arrows0.b;
List eqs1 = (List) eqs0.b;
for (Object o : nodes1) {
nodes.add((String) o);
}
if (attrs0.b.toString().equals("ASWRITTEN")) {
for (String k : nodes) {
attrs.add(new Triple<>(k + "_att", k, "string"));
}
} else {
List attrs1 = (List) attrs0.b;
for (Object o : attrs1) {
Tuple5 x = (Tuple5) o;
attrs.add(new Triple<>((String) x.a, (String) x.c, (String) x.e));
}
}
for (Object o : arrows1) {
Tuple5 x = (Tuple5) o;
arrows.add(new Triple<>((String) x.a, (String) x.c, (String) x.e));
}
for (Object o : eqs1) {
Tuple3 x = (Tuple3) o;
eqs.add(new Pair<>((List<String>) x.a, (List<String>) x.c));
}
Const c = new Const(nodes, attrs, arrows, eqs);
return c;
}
Aggregations