use of catdata.Triple in project fql by CategoricalData.
the class XParser method toProgHelper.
private static void toProgHelper(String z, String s, List<Triple<String, Integer, XExp>> ret, Tuple3 decl) {
String txt = z;
int idx = s.indexOf(txt);
if (idx < 0) {
throw new RuntimeException();
}
if (decl.a instanceof Tuple3) {
Tuple3 t = (Tuple3) decl.a;
Object ooo = toExp(decl.c);
if (ooo instanceof Flower) {
Flower f = (Flower) toExp(decl.c);
f.ty = t.c.toString();
ret.add(new Triple<>(t.a.toString(), idx, f));
} else if (ooo instanceof FLOWER2) {
FLOWER2 f = (FLOWER2) toExp(decl.c);
f.ty = t.c.toString();
ret.add(new Triple<>(t.a.toString(), idx, f));
} else {
throw new RuntimeException("Can only use (v:T) for flowers");
}
} else {
String name = decl.a.toString();
if (decl.b.toString().equals(":")) {
ret.add(new Triple<>(name, idx, newToExp(decl.c)));
} else {
ret.add(new Triple<>(name, idx, toExp(decl.c)));
}
}
}
use of catdata.Triple in project fql by CategoricalData.
the class XProd method prod.
public static <X> XCtx<Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>>> prod(XCtx<X> I, XCtx<X> J) {
if (I.global == null || J.global == null) {
throw new RuntimeException();
}
if (!I.global.equals(J.global)) {
throw new RuntimeException();
}
if (I.schema == null || J.schema == null) {
throw new RuntimeException();
}
if (!I.schema.equals(J.schema)) {
throw new RuntimeException();
}
Set ids = new HashSet<>();
Set<Pair<List<Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>>>, List<Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>>>>> eqs = new HashSet<>();
Map types = new HashMap<>();
/* for each pair (i,j) of type X and each generating
edge f:X->Y in S (including edges in type, like length or succ),
(i,j);f = (i;f, j;f). */
for (X x : I.schema.allIds()) {
Set<Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>>> s = new HashSet<>();
for (Triple<X, X, List<X>> i : I.cat().hom((X) "_1", x)) {
for (Triple<X, X, List<X>> j : J.cat().hom((X) "_1", x)) {
types.put(new Pair<>(i, j), new Pair<>("_1", x));
s.add(new Pair<>(i, j));
}
}
for (X f : I.schema.allTerms()) {
Pair<X, X> t = I.type(f);
if (!t.first.equals(x)) {
continue;
}
for (Pair<Triple<X, X, List<X>>, Triple<X, X, List<X>>> xy : s) {
List lhs = new LinkedList();
lhs.add(xy);
lhs.add(f);
List<X> l1 = new LinkedList<>();
l1.add(xy.first.first);
l1.addAll(xy.first.third);
l1.add(f);
Triple<X, X, List<X>> tofind1 = new Triple<>((X) "_1", t.second, l1);
Triple<X, X, List<X>> found1 = I.find_fast(tofind1);
if (found1 == null) {
throw new RuntimeException("foudn1");
}
List<X> l2 = new LinkedList<>();
l2.add(xy.second.first);
l2.addAll(xy.second.third);
l2.add(f);
Triple<X, X, List<X>> tofind2 = new Triple<>((X) "_1", t.second, l2);
Triple<X, X, List<X>> found2 = J.find_fast(tofind2);
if (found2 == null) {
throw new RuntimeException("ouns 2");
}
Pair rhs0 = new Pair<>(found1, found2);
List rhs = new LinkedList();
rhs.add(rhs0);
eqs.add(new Pair<>(lhs, rhs));
}
}
}
return new XCtx(ids, types, eqs, I.global, I.schema, "instance");
}
use of catdata.Triple in project fql by CategoricalData.
the class XProd method flower2.
public static <C> XCtx<C> flower2(FLOWER2 flower, XCtx<C> I) {
XCtx c = frozen(flower, I.schema);
Set<Map<Object, Triple<C, C, List<C>>>> ret = new HashSet<>();
ret.add(new HashMap<>());
// : check from vars do not occur in I
if (!Collections.disjoint(flower.from.keySet(), I.allTerms())) {
throw new RuntimeException("FROM variable is also in instance");
}
for (Object var : flower.from.keySet()) {
Object node = flower.from.get(var);
Set<Map<Object, Triple<C, C, List<C>>>> ret2 = new HashSet<>();
for (Map<Object, Triple<C, C, List<C>>> tuple : ret) {
for (Triple<C, C, List<C>> t : I.cat().hom((C) "_1", (C) node)) {
Map<Object, Triple<C, C, List<C>>> merged = new HashMap<>(tuple);
merged.put(var, t);
String result = eval(flower.where, merged, flower.from.keySet(), I);
if (result.equals("false")) {
continue;
}
ret2.add(merged);
}
}
ret = ret2;
}
// instance
Set ids = new HashSet<>();
Map types = new HashMap<>();
Set eqs = new HashSet<>();
// schema
Set ids2 = new HashSet<>();
Map types2 = new HashMap<>();
Set eqs2 = new HashSet<>();
ids2.add("_Q");
types2.put("_Q", new Pair<>("_Q", "_Q"));
for (Map<Object, Triple<C, C, List<C>>> k : ret) {
types.put(k, new Pair("_1", "_Q"));
for (Object edge : flower.select.keySet()) {
Object tgt = c.type(flower.select.get(edge)).second;
if (!I.global.ids.contains(tgt)) {
throw new RuntimeException("Selection path " + edge + " does not target a type");
}
types2.put(edge, new Pair<>("_Q", tgt));
List lhs = new LinkedList();
lhs.add(k);
lhs.add(edge);
// must normalize in I
List<C> rhs0 = subst_new(flower.select.get(edge), k, new HashSet(), new HashSet());
Triple<C, C, List<C>> rhs = I.find_fast(new Triple("_1", tgt, rhs0));
List rhsX = new LinkedList();
if (I.schema.cat().hom((C) "_1", (C) tgt).contains(rhs)) {
if (rhs.third.isEmpty()) {
rhsX.add(rhs.first);
} else {
rhsX.addAll(rhs.third);
}
} else {
rhsX.add(rhs);
}
eqs.add(new Pair(lhs, rhsX));
}
}
for (C t : I.global.ids) {
for (Triple<C, C, List<C>> arr : I.cat().hom((C) "_1", t)) {
if (I.global.cat().hom((C) "_1", t).contains(arr)) {
continue;
}
types.put(arr, new Pair<>("_1", t));
for (Entry<C, Pair<C, C>> e : I.global.types.entrySet()) {
if (!e.getValue().first.equals(t)) {
continue;
}
List lhs = new LinkedList();
lhs.add(arr);
lhs.add(e.getKey());
List<C> rhs0 = new LinkedList<>();
// rhs0.add(arr.second);
rhs0.addAll(arr.third);
rhs0.add(e.getKey());
Triple<C, C, List<C>> rhsX = I.find_fast(new Triple<>((C) "_1", e.getValue().second, rhs0));
List rhs = new LinkedList();
if (I.schema.cat().hom((C) "_1", e.getValue().second).contains(rhsX)) {
if (rhsX.third.isEmpty()) {
rhs.add(rhsX.first);
} else {
rhs.addAll(rhsX.third);
}
} else {
rhs.add(rhsX);
}
eqs.add(new Pair<>(lhs, rhs));
}
}
}
XCtx c2 = new XCtx(ids2, types2, eqs2, I.global, null, "schema");
XCtx<C> J = new XCtx<>(ids, types, eqs, I.global, c2, "instance");
J.saturated = true;
return J;
}
use of catdata.Triple in project fql by CategoricalData.
the class XProd method uberflower.
/*
public static XCtx flower(Flower e, XCtx I) {
Set ids = new HashSet<>(I.schema.ids);
Map types = new HashMap<>(I.schema.types);
Set eqs = new HashSet<>(I.schema.eqs);
ids.add("_Q");
types.put("_Q", new Pair<>("_Q", "_Q"));
// types.put("!__Q", new Pair<>("_Q", "_1")); needed?
for (Entry<String, String> k : e.from.entrySet()) {
types.put(k.getKey(), new Pair<>("_Q", k.getValue()));
}
eqs.addAll(e.where);
XCtx c = new XCtx(ids, types, eqs, I.global, null, "schema");
Map em = new HashMap<>();
for (Object o : I.schema.allTerms()) {
List l = new LinkedList<>();
l.add(o);
em.put(o, l);
}
XMapping m = new XMapping(I.schema, c, em, "mapping");
Set ids2 = new HashSet<>();
Map types2 = new HashMap<>();
Set eqs2 = new HashSet<>();
Map em2 = new HashMap<>();
ids2.add("_Q");
types2.put("_Q", new Pair<>("_Q", "_Q"));
List ll = new LinkedList<>();
ll.add("_Q");
em2.put("_Q", ll);
for (Entry o : e.select.entrySet()) {
Object tgt = c.type((List)o.getValue()).second;
types2.put(o.getKey(), new Pair<>("_Q", tgt));
if (!I.global.allTerms().contains(tgt)) {
ids2.add(tgt);
types2.put(tgt, new Pair<>(tgt, tgt));
}
em2.put(o.getKey(), o.getValue());
}
XCtx c2 = new XCtx(ids2, types2, eqs2, I.global, null, "schema");
for (Object o : c2.allTerms()) {
if (em2.containsKey(o)) {
continue;
}
List l = new LinkedList<>();
l.add(o);
em2.put(o, l);
}
XMapping m2 = new XMapping(c2, c, em2, "mapping");
XCtx J = null;
if (DEBUG.debug.direct_flower) {
J = fast_flower(e, I, c, c2);
} else {
J = m.pi(I);
}
return m2.delta(J);
}
*/
/*
private static Set<Flower> normalize(FLOWER2 e) {
Set<Flower> ret = new HashSet<>();
if (e.where == null) {
Flower f = new Flower(e.select, e.from, new LinkedList<>(), e.src);
f.ty = e.ty;
ret.add(f);
return ret;
}
if (e.where.lhs != null && e.where.rhs != null) {
List<Pair<List<String>, List<String>>> l = new LinkedList<>();
l.add(new Pair<>(e.where.lhs, e.where.rhs));
Flower f = new Flower(e.select, e.from, l, e.src);
f.ty = e.ty;
ret.add(f);
return ret;
}
e.where.normalize();
List<List<Pair<List<String>, List<String>>>> ll = e.where.fromOr();
for (List<Pair<List<String>, List<String>>> l : ll) {
Flower f = new Flower(e.select, e.from, l, e.src);
f.ty = e.ty;
ret.add(f);
}
return ret;
}
*/
/*
public static XCtx FLOWER(FLOWER2 e0, XCtx I) {
Set ids = new HashSet<>(I.schema.ids);
Map types = new HashMap<>(I.schema.types);
Set eqs = new HashSet<>(I.schema.eqs);
Set<Flower> flowers = normalize(e0);
int i = 0;
for (Flower e : flowers) {
String qi = "_Q" + i;
ids.add(qi);
types.put(qi, new Pair<>(qi, qi));
for (Entry<String, String> k : e.from.entrySet()) {
types.put(qi + k.getKey(), new Pair<>(qi, k.getValue()));
}
//!__Q -> !__Q1
for (Pair<List<String>, List<String>> eq : e.where) {
Function<String, String> f = x -> e.from.keySet().contains(x) ? qi + x : x;
List<String> lhs = eq.first.stream().map(f).collect(Collectors.toList());
List<String> rhs = eq.second.stream().map(f).collect(Collectors.toList());
Function<String, String> g = x -> x.equals("!__Q") ? "!_" + qi : x;
lhs = lhs.stream().map(g).collect(Collectors.toList());
rhs = rhs.stream().map(g).collect(Collectors.toList());
eqs.add(new Pair<>(lhs, rhs));
}
i++;
}
XCtx c = new XCtx(ids, types, eqs, I.global, null, "schema");
Map em = new HashMap<>();
for (Object o : I.schema.allTerms()) {
List l = new LinkedList<>();
l.add(o);
em.put(o, l);
}
XMapping m = new XMapping(I.schema, c, em, "mapping");
Set ids2 = new HashSet<>();
Map types2 = new HashMap<>();
Set eqs2 = new HashSet<>();
Map em2 = new HashMap<>();
i = 0;
for (Flower e : flowers) {
String qi = "_Q" + i;
ids2.add(qi);
types2.put(qi, new Pair<>(qi, qi));
List ll = new LinkedList<>();
ll.add(qi);
em2.put(qi, ll);
for (Entry<String, List<String>> o : e.select.entrySet()) {
Function<String, String> f = x -> e.from.keySet().contains(x) ? qi + x : x;
List<String> lll = o.getValue().stream().map(f).collect(Collectors.toList());
Object tgt = c.type(lll).second;
types2.put(qi + o.getKey(), new Pair<>(qi, tgt));
if (!I.global.allTerms().contains(tgt)) {
ids2.add(tgt);
types2.put(tgt, new Pair<>(tgt, tgt));
}
em2.put(qi + o.getKey(), lll);
}
i++;
}
XCtx c2 = new XCtx(ids2, types2, eqs2, I.global, null, "schema");
for (Object o : c2.allTerms()) {
if (em2.containsKey(o)) {
continue;
}
List l = new LinkedList<>();
l.add(o);
em2.put(o, l);
}
XMapping m2 = new XMapping(c2, c, em2, "mapping");
Set ids3 = new HashSet<>();
Map types3 = new HashMap<>();
Set eqs3 = new HashSet<>();
Map em3 = new HashMap<>();
ids3.add("_Q");
types3.put("_Q", new Pair<>("_Q", "_Q"));
i = 0;
for (Flower e : flowers) {
String qi = "_Q" + i;
List ll = new LinkedList<>();
ll.add("_Q");
em3.put(qi, ll);
List jj = new LinkedList<>();
jj.add("!__Q");
em3.put("!_" + qi, jj);
for (Entry<String, List<String>> o : e.select.entrySet()) {
Function<String, String> f = x -> e.from.keySet().contains(x) ? qi + x : x;
List<String> lll = o.getValue().stream().map(f).collect(Collectors.toList());
Object tgt = c.type(lll).second;
types3.put(o.getKey(), new Pair<>("_Q", tgt));
if (!I.global.allTerms().contains(tgt)) {
ids3.add(tgt);
types3.put(tgt, new Pair<>(tgt, tgt));
}
List u = new LinkedList();
u.add(o.getKey());
em3.put(qi + o.getKey(), u);
}
i++;
}
XCtx c3 = new XCtx(ids3, types3, eqs3, I.global, null, "schema");
for (Object o : c2.allTerms()) {
if (em3.containsKey(o)) {
continue;
}
List l = new LinkedList<>();
l.add(o);
em3.put(o, l);
}
XMapping m3 = new XMapping(c2, c3, em3, "mapping");
XCtx J = null;
if (DEBUG.debug.direct_flower) {
J = fast_flower(flowers, I, c, c2);
} else {
J = m.pi(I);
}
XCtx K = m2.delta(J);
XCtx L = m3.apply0(K);
return L.rel();
}
*/
public static <C, D> XMapping<Pair<Object, Map<Object, Triple<C, C, List<C>>>>, Pair<Object, Map<Object, Triple<C, C, List<C>>>>> uberflower(XPoly<C, D> poly, XMapping<C, C> h) {
// poly.initConjs(); works on full ubers
XCtx<Pair<Object, Map<Object, Triple<C, C, List<C>>>>> hsrc = uberflower(poly, h.src);
// XCtx<Pair<Object, Map<Object, Triple<C, C, List<C>>>>> hdst = uberflower(poly, h.dst);
Map em = new HashMap<>();
for (Pair<Object, Map<Object, Triple<C, C, List<C>>>> k : hsrc.ids) {
Map<Object, Triple<C, C, List<C>>> cand = new HashMap<>();
for (Object v : k.second.keySet()) {
Triple<C, C, List<C>> p = k.second.get(v);
cand.put(v, h.dst.find_fast(new Triple<>(p.first, p.second, h.apply(p.third))));
}
em.put(k, new Pair<>(k.first, cand));
}
for (Object k : hsrc.allTerms()) {
if (!em.containsKey(k)) {
em.put(k, Util.singList(k));
}
}
return new XMapping(uberflower(poly, h.src), uberflower(poly, h.dst), em, "homomorphism");
}
use of catdata.Triple 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);
}
Aggregations