use of catdata.fpql.XExp.Var in project fql by CategoricalData.
the class EnrichViewer method idPoly.
private static XPoly<String, String> idPoly(XSchema isa, String isa0, String merged) {
Map<Object, Pair<String, Block<String, String>>> blocks = new HashMap<>();
for (String node : isa.nodes) {
Map<Object, String> from = new HashMap<>();
from.put("v", node);
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<>();
for (Triple<String, String, String> arrow : isa.arrows) {
if (!arrow.second.equals(node)) {
continue;
}
if (isa.nodes.contains(arrow.third)) {
Map<String, List<String>> map = new HashMap<>();
List<String> l = new LinkedList<>();
l.add("v");
l.add(arrow.first);
map.put("v", l);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Object, Map<Object, List<Object>>> ppp = new Pair("q_" + arrow.third, map);
edges.put(arrow.first, ppp);
} else {
List<Object> l = new LinkedList<>();
l.add("v");
l.add(arrow.first);
attrs.put(arrow.first, l);
}
}
Block<String, String> block = new Block<>(from, where, attrs, edges);
blocks.put("q_" + node, new Pair<>(node, block));
}
return new XPoly<>(new Var(isa0), new Var(merged), blocks);
}
use of catdata.fpql.XExp.Var 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.Var 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.Var in project fql by CategoricalData.
the class XNeo4jToFQL method toInst.
private static XInst toInst(Map<String, Map<String, Object>> properties, Map<String, Set<Pair<String, String>>> edges) {
List<Pair<String, String>> data = new LinkedList<>();
List<Pair<List<String>, List<String>>> eqs = new LinkedList<>();
for (String n : properties.keySet()) {
Map<String, Object> props = properties.get(n);
data.add(new Pair<>(n, (String) props.get("label")));
for (String p : props.keySet()) {
if (p.equals("label")) {
continue;
}
List<String> l = new LinkedList<>();
List<String> r = new LinkedList<>();
l.add(n);
l.add(p);
r.add((String) props.get(p));
eqs.add(new Pair<>(l, r));
}
}
for (String e : edges.keySet()) {
for (Pair<String, String> p : edges.get(e)) {
List<String> l = new LinkedList<>();
List<String> r = new LinkedList<>();
l.add(p.first);
l.add(e);
r.add(p.second);
eqs.add(new Pair<>(l, r));
}
}
XInst ret = new XInst(new Var("S"), data, eqs);
return ret;
}
use of catdata.fpql.XExp.Var in project fql by CategoricalData.
the class XParser method toExp.
private static XExp toExp(Object c) {
if (c instanceof String) {
return new Var((String) c);
}
try {
return fromPoly((Tuple4) c);
} catch (Exception e) {
}
try {
return fromSoed(c);
} catch (Exception e) {
}
try {
return toCatConst(c);
} catch (Exception e) {
}
try {
if (c.toString().contains("variables")) {
return toInstConst(c);
}
} catch (Exception e) {
}
try {
return toMapping(c);
} catch (Exception e) {
}
try {
return toTrans(c);
} catch (Exception e) {
}
if (c instanceof Tuple5) {
Tuple5 p = (Tuple5) c;
if (p.c.toString().equals("+")) {
return new XCoprod(toExp(p.b), toExp(p.d));
}
if (p.c.toString().equals("*")) {
return new XTimes(toExp(p.b), toExp(p.d));
}
if (p.c.toString().equals(";")) {
return new Compose(toExp(p.b), toExp(p.d));
}
if (p.a.toString().equals("return") && p.b.toString().equals("sigma")) {
return new XUnit("sigma", toExp(p.d), toExp(p.e));
}
if (p.a.toString().equals("coreturn") && p.b.toString().equals("sigma")) {
return new XCounit("sigma", toExp(p.d), toExp(p.e));
}
if (p.a.toString().equals("return") && p.b.toString().equals("delta")) {
return new XUnit("pi", toExp(p.d), toExp(p.e));
}
if (p.a.toString().equals("coreturn") && p.b.toString().equals("delta")) {
return new XCounit("pi", toExp(p.d), toExp(p.e));
}
return new XFn((String) p.b, (String) p.d, (String) p.e);
}
if (c instanceof Tuple4) {
Tuple4 p = (Tuple4) c;
return new XEq((List<String>) p.b, (List<String>) p.d);
}
if (c instanceof Tuple3) {
Tuple3 p = (Tuple3) c;
if (p.a.toString().equals("flower")) {
XExp I = toExp(p.c);
Tuple3 q = (Tuple3) p.b;
// list of tuple3 of (path, string)
List s = (List) ((org.jparsec.functors.Pair) q.a).b;
// list of tuple3 of (string, string)
List f = (List) ((org.jparsec.functors.Pair) q.b).b;
// list of tuple3 of (path, path)
List w = (List) ((org.jparsec.functors.Pair) q.c).b;
Map<Object, List<Object>> select = new HashMap<>();
Map<Object, Object> from = new HashMap<>();
List<Pair<List<Object>, List<Object>>> where = new LinkedList<>();
Set<String> seen = new HashSet<>();
for (Object o : w) {
Tuple3 t = (Tuple3) o;
List lhs = (List) t.a;
List rhs = (List) t.c;
where.add(new Pair<>(rhs, lhs));
}
for (Object o : s) {
Tuple3 t = (Tuple3) o;
List lhs = (List) t.a;
String rhs = t.c.toString();
if (seen.contains(rhs)) {
throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
}
seen.add(rhs);
select.put(rhs, lhs);
}
for (Object o : f) {
Tuple3 t = (Tuple3) o;
String lhs = t.a.toString();
String rhs = t.c.toString();
if (seen.contains(rhs)) {
throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
}
seen.add(rhs);
from.put(rhs, lhs);
}
return new Flower(select, from, where, I);
}
if (p.a.toString().equals("FLOWER")) {
XExp I = toExp(p.c);
Tuple3 q = (Tuple3) p.b;
// list of tuple3 of (path, string)
List s = (List) ((org.jparsec.functors.Pair) q.a).b;
// list of tuple3 of (string, string)
List f = (List) ((org.jparsec.functors.Pair) q.b).b;
// list of tuple3 of (path, path)
Object w = ((org.jparsec.functors.Pair) q.c).b;
Map<Object, List<Object>> select = new HashMap<>();
Map<Object, Object> from = new HashMap<>();
// List<Pair<List<String>, List<String>>> where = new LinkedList<>();
Set<String> seen = new HashSet<>();
for (Object o : s) {
Tuple3 t = (Tuple3) o;
List lhs = (List) t.a;
String rhs = t.c.toString();
if (seen.contains(rhs)) {
throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
}
seen.add(rhs);
select.put(rhs, lhs);
}
for (Object o : f) {
Tuple3 t = (Tuple3) o;
String lhs = t.a.toString();
String rhs = t.c.toString();
if (seen.contains(rhs)) {
throw new RuntimeException("Duplicate AS name: " + rhs + " (note: AS names can't be used in the schema either)");
}
seen.add(rhs);
from.put(rhs, lhs);
}
return new FLOWER2(select, from, toWhere(w), I);
}
if (p.a.toString().equals("pushout")) {
return new XPushout(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("hom")) {
return new XToQuery(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("sigma")) {
return new XSigma(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("delta")) {
return new XDelta(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("pi")) {
return new XPi(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("inl")) {
return new XInj(toExp(p.b), toExp(p.c), true);
}
if (p.a.toString().equals("inr")) {
return new XInj(toExp(p.b), toExp(p.c), false);
}
if (p.a.toString().equals("case")) {
return new XMatch(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("fst")) {
return new XProj(toExp(p.b), toExp(p.c), true);
}
if (p.a.toString().equals("snd")) {
return new XProj(toExp(p.b), toExp(p.c), false);
}
if (p.a.toString().equals("pair")) {
return new XPair(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("apply")) {
return new Apply(toExp(p.b), toExp(p.c));
}
if (p.a.toString().equals("coapply")) {
return new XCoApply(toExp(p.b), toExp(p.c));
}
return new XConst((String) p.b, (String) p.c);
}
if (c instanceof org.jparsec.functors.Pair) {
org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) c;
if (p.a.toString().equals("idpoly")) {
return new XIdPoly(toExp(p.b));
}
if (p.a.toString().equals("uberpi")) {
return new XUberPi(toExp(p.b));
}
if (p.a.toString().equals("labels")) {
return new XLabel(toExp(p.b));
}
if (p.a.toString().equals("grothlabels")) {
return new XGrothLabels(toExp(p.b));
}
if (p.a.toString().equals("relationalize")) {
return new XRel(toExp(p.b));
}
if (p.a.toString().equals("void")) {
return new XVoid(toExp(p.b));
}
if (p.a.toString().equals("ff")) {
return new XFF(toExp(p.b));
}
if (p.a.toString().equals("unit")) {
return new XOne(toExp(p.b));
}
if (p.a.toString().equals("tt")) {
return new XTT(toExp(p.b));
}
if (p.a.toString().equals("id")) {
return new Id(false, toExp(p.b));
}
if (p.a.toString().equals("ID")) {
return new Id(true, toExp(p.b));
} else // if (p.a.toString().equals("query")) {
// Tuple3 t = (Tuple3) p.b;
// return new XExp.XQueryExp(toExp(t.a), toExp(t.b), toExp(t.c));
// }
{
try {
return fromSuperSoed(c);
} catch (Exception e) {
e.printStackTrace();
}
return new XTy((String) p.b);
}
}
throw new RuntimeException("x: " + c.getClass() + " " + c);
}
Aggregations