use of catdata.fpql.XExp.Var in project fql by CategoricalData.
the class XRaToFpql method transSQLSchema.
private static String transSQLSchema(List<Pair<String, EExternal>> in) {
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<>();
// String adom = "adom";
// nodes.add(adom);
// List<Pair<Object, Object>> adomT = new LinkedList<>();
// LinkedList<Pair<Object, Object>> attT = new LinkedList<>();
// inodes.add(new Pair<String, List<Pair<Object, Object>>>(adom, adomT));
// iattrs.add(new Pair<String, List<Pair<Object, Object>>>("att", attT));
// attrs.add(new Triple<>("att", adom, "adom"));
Set<Object> enums = new HashSet<>();
Map<String, Object> dom1 = new HashMap<>();
List<Pair<String, EExternal>> queries = new LinkedList<>();
int count = 0;
Set<String> seen = new HashSet<>();
Map<String, List<String>> cols = new HashMap<>();
for (Pair<String, EExternal> kk0 : in) {
EExternal k0 = kk0.second;
// String key = kk0.first;
if (k0 instanceof ECreateTable) {
ECreateTable k = (ECreateTable) k0;
if (seen.contains(k.name)) {
throw new RuntimeException("Duplicate name: " + k.name);
}
if (k.name.equals("adom") || k.name.equals("att")) {
throw new RuntimeException("The names adom and att cannot be used.");
}
seen.add(k.name);
nodes.add(k.name);
inodes.add(new Pair<>(k.name, new LinkedList<>()));
List<String> lcols = new LinkedList<>();
for (Pair<String, String> col : k.types) {
lcols.add(col.first);
if (seen.contains(col.first)) {
throw new RuntimeException("Duplicate name: " + col.first);
}
seen.add(col.first);
arrows.add(new Triple<>(k.name + "_" + col.first, k.name, "adom"));
iarrows.add(new Pair<>(k.name + "_" + col.first, new LinkedList<>()));
}
cols.put(k.name, lcols);
}
if (k0 instanceof EInsertValues) {
EInsertValues k = (EInsertValues) k0;
List<String> lcols = cols.get(k.target);
if (lcols == null) {
throw new RuntimeException("Missing: " + 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);
}
String id = "v" + count++;
node.add(new Pair<>(id, id));
for (int colNum = 0; colNum < tuple.size(); colNum++) {
Object xxx = dom1.get(tuple.get(colNum));
if (xxx == null) {
// was 2nd=count
dom1.put(tuple.get(colNum), tuple.get(colNum));
enums.add(tuple.get(colNum));
// adomT.add(new Pair<Object, Object>(count, count));
// adomT.add(new Pair<Object, Object>(count, tuple.get(colNum)
// ));
xxx = dom1.get(tuple.get(colNum));
// count++;
}
List<Pair<Object, Object>> yyy = lookup2(k.target + "_" + lcols.get(colNum), iarrows);
if (yyy == null) {
throw new RuntimeException("Anomaly: please report");
}
yyy.add(new Pair<>(id, xxx));
}
}
}
if (k0 instanceof EFlower || k0 instanceof EUnion || k0 instanceof EDiff || k0 instanceof EED) {
queries.add(kk0);
}
}
XSchema exp = doSchema(nodes, /* attrs, */
arrows, eqs);
XInst inst = doInst(inodes, /* iattrs, */
iarrows, new Var("S"));
// int ctx = 0;
String xxx = "\n\n";
Map<String, String> schemas = new HashMap<>();
Map<String, XSchema> schemas0 = new HashMap<>();
// Map<String, Boolean> done = new HashMap<>();
for (Pair<String, EExternal> gh0 : queries) {
String k = gh0.first;
EExternal gh = gh0.second;
if (gh instanceof EFlower) {
EFlower fl = (EFlower) gh;
Pair<String, XSchema> yyy = trans(exp, fl, k, enums);
xxx += yyy.first + "\n\n";
schemas.put(k, k + "Schema");
schemas0.put(k, yyy.second);
} else if (gh instanceof EUnion) {
EUnion g = (EUnion) gh;
String s1 = schemas.get(g.l);
schemas.put(k, s1);
schemas0.put(k, schemas0.get(g.l));
xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
if (g.distinct) {
xxx += "\n\n" + k + "_temp = (" + g.l + " + " + g.r + ")";
xxx += "\n\n" + k + " = relationalize " + k + "_temp";
} else {
xxx += "\n\n" + k + " = (" + g.l + " + " + g.r + ")";
}
xxx += "\n\n";
} else if (gh instanceof EED) {
EED c = (EED) gh;
XInst f = doED(/*cols, */
c.from1, c.where1, exp);
c.from2.putAll(c.from1);
c.where2.addAll(c.where1);
XInst g = doED(/* cols, */
c.from2, c.where2, exp);
List<Pair<Pair<String, String>, List<String>>> vm = new LinkedList<>();
for (Pair<String, String> x : f.nodes) {
List<String> l = new LinkedList<>();
l.add(x.first);
vm.add(new Pair<>(new Pair<>(x.first, null), l));
}
XTransConst i = new XTransConst(f, g, vm);
xxx += longSlash + "\n/* Translation of " + k + " */\n" + longSlash;
xxx += "\n\n" + k + "A = " + f + " : S";
xxx += "\n\n" + k + "E = " + g + " : S";
xxx += "\n\n" + k + "I = " + i + " : " + k + "A -> " + k + "E";
xxx += "\n\n";
} else {
throw new RuntimeException();
}
}
String comment = "//schema S and instance I represent the entire input database.\n\n";
String preS = "adom : type\n";
String senums0 = preS + Util.sep(enums.stream().map(x -> x + " : adom").collect(Collectors.toList()), "\n");
return comment + senums0 + "\n\nS = " + exp + "\n\nI = " + inst + " : S" + xxx;
}
use of catdata.fpql.XExp.Var in project fql by CategoricalData.
the class EnrichViewer method enrich.
private static XPoly<String, String> enrich(XCtx<String> S0, XSchema S, String s, String T, String mat, String name, String a, /*String b,*/
String l, String r, String n) {
Map<Object, Pair<String, Block<String, String>>> blocks = new HashMap<>();
// Map<String, Map<String, Triple<String, String, List<String>>>> vars1 = new HashMap<>();
Map<String, Map<Triple<String, String, List<String>>, String>> vars2 = new HashMap<>();
int i = 0;
for (String X : S.nodes) {
// Map<String, Triple<String, String, List<String>>> m1 = new HashMap<>();
Map<Triple<String, String, List<String>>, String> m2 = new HashMap<>();
// vars1.put(X, m1);
vars2.put(X, m2);
for (Triple<String, String, List<String>> rp : S0.cat().hom(X, mat)) {
// m1.put("v" + i, rp);
m2.put(rp, "v" + i);
i++;
}
}
for (String X : S.nodes) {
Map<Object, String> from = new HashMap<>();
from.put("x", X);
Set<Pair<List<Object>, List<Object>>> where = new HashSet<>();
Map<String, List<Object>> attrs = new HashMap<>();
Map<String, Pair<Object, Map<Object, List<Object>>>> edges = new HashMap<>();
// Map<String, Triple<String, String, List<String>>> m1 = vars1.get(X); //new HashMap<>();
// new HashMap<>();
Map<Triple<String, String, List<String>>, String> m2 = vars2.get(X);
for (Triple<String, String, List<String>> p : S0.cat().hom(X, mat)) {
String rp = m2.get(p);
from.put(rp, a);
List<Object> lhs = new LinkedList<>();
List<Object> rhs = new LinkedList<>();
lhs.add("x");
lhs.addAll(p.third);
lhs.add(name);
rhs.add(rp);
rhs.add(l);
rhs.add(n);
where.add(new Pair<>(lhs, rhs));
}
for (Triple<String, String, String> e : S.arrows) {
Map<Object, List<Object>> map = new HashMap<>();
if (!e.second.equals(X)) {
continue;
}
if (S.nodes.contains(e.third)) {
List<Object> xxx = new LinkedList<>();
xxx.add("x");
xxx.add(e.first);
map.put("x", xxx);
for (Triple<String, String, List<String>> p0 : S0.cat().hom(e.third, mat)) {
Object rep0 = vars2.get(e.third).get(p0);
List<String> list = new LinkedList<>();
list.add(e.first);
list.addAll(p0.third);
Triple<String, String, List<String>> ep0 = S0.find_fast(new Triple<>(X, p0.second, list));
if (ep0 == null) {
throw new RuntimeException("Cannot find " + new Triple<>(X, e.third, list) + " in " + S0);
}
String tgt = m2.get(ep0);
if (tgt == null) {
throw new RuntimeException("Cannot find " + ep0 + " in " + m2.keySet());
}
List<Object> yyh = Collections.singletonList(tgt);
map.put(rep0, yyh);
}
edges.put(e.first, new Pair<>("q" + e.third, map));
// edge
} else {
List<Object> list = new LinkedList<>();
if (e.first.equals(name) && X.equals(mat)) {
Triple<String, String, List<String>> id = S0.find_fast(new Triple<>(mat, mat, new LinkedList<>()));
list.add(m2.get(id));
list.add(r);
list.add(n);
} else {
list.add("x");
list.add(e.first);
}
attrs.put(e.first, list);
// att
}
}
Block<String, String> block = new Block<>(from, where, attrs, edges);
blocks.put("q" + X, new Pair<>(X, block));
}
XPoly<String, String> poly = new XPoly<>(new Var(s), new Var(T), blocks);
return poly;
}
Aggregations