use of catdata.fpql.XExp.XSchema in project fql by CategoricalData.
the class XRaToFpql method trans.
private static Pair<String, XSchema> trans(XSchema src, EFlower fl, String pre, Set<Object> enums) {
// SigExp src0 = new SigExp.Var("S");
// LinkedList<Pair<List<String>, List<String>>> eqs =
List<String> nodes1 = new LinkedList<>();
List<String> nodes2 = new LinkedList<>();
List<String> nodes3 = new LinkedList<>();
// nodes1.add("adom");
// nodes2.add("adom");
nodes2.add("guid");
// nodes3.add("adom");
// List<Triple<String, String, String>> attrs = new LinkedList<>();
// attrs.add(new Triple<>("att", "adom", "adom"));
List<Triple<String, String, String>> edges1 = new LinkedList<>();
List<Triple<String, String, String>> edges2 = new LinkedList<>();
List<Triple<String, String, String>> edges3 = new LinkedList<>();
List<Pair<String, String>> inodes1 = new LinkedList<>();
List<Pair<String, String>> inodes2 = new LinkedList<>();
List<Pair<String, String>> inodes3 = new LinkedList<>();
// inodes1.add(new Pair<>("adom", "adom"));
// inodes2.add(new Pair<>("adom", "adom"));
// inodes3.add(new Pair<>("adom", "adom"));
// List<Pair<String, String>> iattrs = new LinkedList<>();
// iattrs.add(new Pair<>("att", "att"));
List<Pair<String, List<String>>> iedges1 = new LinkedList<>();
List<Pair<String, List<String>>> iedges2 = new LinkedList<>();
List<Pair<String, List<String>>> iedges3 = new LinkedList<>();
for (String k : fl.from.keySet()) {
String v = fl.from.get(k);
inodes1.add(new Pair<>(k, v));
nodes1.add(k);
inodes2.add(new Pair<>(k, "guid"));
for (Triple<String, String, String> arr : src.arrows) {
if (arr.second.equals(v)) {
List<String> l = new LinkedList<>();
l.add(v);
l.add(arr.first);
edges1.add(new Triple<>(k + "_" + arr.first, k, "adom"));
iedges1.add(new Pair<>(k + "_" + arr.first, l));
edges2.add(new Triple<>(k + "_" + arr.first, "guid", "adom"));
List<String> l0 = new LinkedList<>();
l0.add("guid");
l0.add(k + "_" + arr.first);
iedges2.add(new Pair<>(k + "_" + arr.first, l0));
}
}
}
List<List<Triple<String, String, String>>> eqcs = merge(edges2, fl);
// for each p.q = 3, add (eqc_for(p.q).get(0) = 3) to some list
Iterator<Triple<String, String, String>> it = edges2.iterator();
while (it.hasNext()) {
Triple<String, String, String> k = it.next();
for (List<Triple<String, String, String>> v : eqcs) {
if (v.contains(k) && !v.get(0).equals(k)) {
it.remove();
}
}
}
for (Pair<String, List<String>> kk : iedges2) {
Triple<String, String, String> k = new Triple<>(kk.second.get(1), "guid", "adom");
for (List<Triple<String, String, String>> v : eqcs) {
if (v.contains(k) && !v.get(0).equals(k)) {
List<String> xxx = new LinkedList<>();
xxx.add("guid");
xxx.add(v.get(0).first);
kk.second = xxx;
break;
}
}
}
nodes3.add("guid");
inodes3.add(new Pair<>("guid", "guid"));
for (String k : fl.select.keySet()) {
Pair<String, String> v = fl.select.get(k);
edges3.add(new Triple<>(k, "guid", "adom"));
Triple<String, String, String> t = new Triple<>(v.first + "_" + fl.from.get(v.first) + "_" + v.second, "guid", "adom");
if (fl.from.get(v.first) == null) {
throw new RuntimeException(v.first + " is not selectable in " + fl);
}
for (List<Triple<String, String, String>> eqc : eqcs) {
if (eqc.contains(t)) {
List<String> li = new LinkedList<>();
li.add("guid");
li.add(eqc.get(0).first);
iedges3.add(new Pair<>(k, li));
}
}
}
XSchema sig1 = doSchema(nodes1, /* attrs, */
edges1, new LinkedList<>());
XSchema sig2 = doSchema(nodes2, /* attrs, */
edges2, new LinkedList<>());
XSchema sig3 = doSchema(nodes3, /* attrs, */
edges3, new LinkedList<>());
for (Pair<Pair<String, String>, Pair<String, String>> x : fl.where) {
if (x.second.second != null) {
continue;
}
// : add to global consts
String c = x.second.first;
enums.add(c);
Triple<String, String, String> found = null;
Triple<String, String, String> tofind = new Triple<>(x.first.first + "_" + fl.from.get(x.first.first) + "_" + x.first.second, "guid", "adom");
for (List<Triple<String, String, String>> eqc : eqcs) {
if (eqc.contains(tofind)) {
found = eqc.get(0);
break;
}
}
if (found == null) {
throw new RuntimeException("Bad flower: " + fl);
}
List<String> lhs = new LinkedList<>();
lhs.add(found.first);
// lhs.add("att");
List<String> rhs = new LinkedList<>();
rhs.add("\"!_guid\"");
rhs.add(c);
Pair<List<String>, List<String>> eq = new Pair<>(lhs, rhs);
sig2.eqs.add(eq);
}
XMapConst map1 = doMapping(inodes1, /* iattrs, */
iedges1, sig1, new Var("S"));
XMapConst map2 = doMapping(inodes2, /* iattrs, */
iedges2, src, sig2);
XMapConst map3 = doMapping(inodes3, /* iattrs, */
iedges3, sig3, sig2);
String xxx = "";
xxx += "\n\n" + pre + "fromSchema = " + sig1;
xxx += "\n\n" + pre + "fromMapping = " + map1 + " : " + pre + "fromSchema -> S";
xxx += "\n\n" + pre + "fromInstance = delta " + pre + "fromMapping I";
xxx += "\n\n" + pre + "whereSchema = " + sig2;
xxx += "\n\n" + pre + "whereMapping = " + map2 + " : " + pre + "fromSchema -> " + pre + "whereSchema";
xxx += "\n\n" + pre + "whereInstance = pi " + pre + "whereMapping " + pre + "fromInstance";
xxx += "\n\n" + pre + "Schema = " + sig3;
xxx += "\n\n" + pre + "selectMapping = " + map3 + " : " + pre + "Schema -> " + pre + "whereSchema";
if (fl.distinct) {
xxx += "\n\n" + pre + "selectInstance = delta " + pre + "selectMapping " + pre + "whereInstance";
xxx += "\n\n" + pre + " = relationalize " + pre + "selectInstance";
} else {
xxx += "\n\n" + pre + " = delta " + pre + "selectMapping " + pre + "whereInstance";
}
String comment = longSlash + "\n/* " + "Translation of " + pre + " */\n" + longSlash;
return new Pair<>(comment + xxx, sig3);
}
use of catdata.fpql.XExp.XSchema in project fql by CategoricalData.
the class XSqlToFql 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<>();
Set<String> atoms = new HashSet<>();
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;
}
// if (seen.contains(col.first)) {
// throw new RuntimeException("Duplicate name: " + k.name);
// }
// seen.add(col.first);
String ref = lookup(col.first, k.fks);
if (ref == null) {
String col_t = col.second.equals("int") ? "int" : "adom";
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<>("v" + tuple.get(0), "v" + 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<>("v" + tuple.get(0), "v" + maybeQuote(tuple.get(colNum))));
} else {
atoms.add(maybeQuote(tuple.get(colNum)));
xxx.add(new Pair<>("v" + tuple.get(0), maybeQuote(tuple.get(colNum))));
}
}
}
}
}
arrows.addAll(attrs);
XSchema exp = XRaToFpql.doSchema(nodes, arrows, eqs);
// SigExp.Const exp = new SigExp.Const(nodes, attrs, arrows, eqs);
iarrows.addAll(iattrs);
XInst inst = XRaToFpql.doInst(inodes, iarrows, new Var("S"));
// InstExp.Const inst = new InstExp.Const(inodes, iattrs, iarrows,
// new SigExp.Var("S"));
String old = "S = " + exp + "\n\nI = " + inst + " : S";
return "adom : type\n\n" + Util.sep(atoms.stream().map(x -> x + " : adom").collect(Collectors.toList()), "\n") + "\n\n" + old;
}
use of catdata.fpql.XExp.XSchema in project fql by CategoricalData.
the class XParser method toCatConst.
private static XSchema toCatConst(Object y) {
List<String> nodes = new LinkedList<>();
List<Triple<String, String, String>> arrows = new LinkedList<>();
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
Tuple3 s = (Tuple3) y;
Tuple3 nodes0 = (Tuple3) s.a;
Tuple3 arrows0 = (Tuple3) s.b;
Tuple3 eqs0 = (Tuple3) s.c;
List nodes1 = (List) nodes0.b;
List arrows1 = (List) arrows0.b;
List eqs1 = (List) eqs0.b;
for (Object o : nodes1) {
nodes.add((String) o);
}
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;
List<String> l1 = (List<String>) x.a;
List<String> l2 = (List<String>) x.c;
eqs.add(new Pair<>(l1, l2));
}
XSchema c = new XSchema(nodes, arrows, eqs);
return c;
}
use of catdata.fpql.XExp.XSchema 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.XSchema in project fql by CategoricalData.
the class EnrichViewer method translate.
private String translate(String program) {
XProgram init;
try {
init = XParser.program(program);
} catch (ParserException e) {
int col = e.getLocation().column;
int line = e.getLocation().line;
topArea.requestFocusInWindow();
topArea.area.setCaretPosition(topArea.area.getDocument().getDefaultRootElement().getElement(line - 1).getStartOffset() + (col - 1));
// String s = e.getMessage();
// String t = s.substring(s.indexOf(" "));
// t.split("\\s+");
e.printStackTrace();
return "Syntax error: " + e.getLocalizedMessage();
} catch (Throwable e) {
e.printStackTrace();
return "Error: " + e.getLocalizedMessage();
}
if (init == null) {
return "";
}
String isaX = null, matX = null;
XSchema isa = null, mat = null;
for (String line : init.order) {
XExp exp = init.exps.get(line);
if (exp instanceof XSchema) {
if (isaX == null) {
isaX = line;
isa = (XSchema) exp;
continue;
}
if (matX == null) {
matX = line;
mat = (XSchema) exp;
continue;
}
throw new RuntimeException("More than two schemas");
}
}
if (isaX == null || matX == null) {
throw new RuntimeException("Fewer than two schemas");
}
/* if (isa.arrows.size() != 2) {
String temp = isaX;
XExp.XSchema temp2 = isa;
isaX = matX;
isa = mat;
matX = temp;
mat = temp2;
} */
XEnvironment env;
try {
env = XDriver.makeEnv(program, init);
} catch (LineException e) {
String toDisplay = "Error in " + e.kind + " " + e.decl + ": " + e.getLocalizedMessage();
e.printStackTrace();
topArea.requestFocusInWindow();
Integer theLine = init.getLine(e.decl);
topArea.area.setCaretPosition(theLine);
return toDisplay;
} catch (Throwable re) {
return "Error: " + re.getLocalizedMessage();
}
@SuppressWarnings("unchecked") XCtx<String> isa0 = (XCtx<String>) env.objs.get(isaX);
@SuppressWarnings("unchecked") XCtx<String> mat0 = (XCtx<String>) env.objs.get(matX);
return go(isa, mat, isaX, matX, isa0, mat0, name.getText(), kid.getText(), instField.getText(), isaField.getText());
}
Aggregations