use of catdata.fql.cat.Arr in project fql by CategoricalData.
the class PSMEval method exec.
@Override
public void exec(PSMInterp interp, Map<String, Set<Map<Object, Object>>> state) {
try {
Quad<Instance, Map<Pair<Node, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Triple<Instance, Map<Node, Map<Object, Pair<Arr<Node, Path>, Object>>>, Map<Node, Map<Pair<Arr<Node, Path>, Object>, Object>>>>, Map<Node, Map<Object, Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>>>, Map<Node, Map<Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>, Object>>> xxx = interp.exps2.get(AB);
Instance Jw = xxx.first;
Map<Pair<Node, LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>>, Triple<Instance, Map<Node, Map<Object, Pair<Arr<Node, Path>, Object>>>, Map<Node, Map<Pair<Arr<Node, Path>, Object>, Object>>>> map = xxx.second;
Map<Node, Map<Object, Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>>> map2 = xxx.third;
FinCat<Node, Path> cat = Jw.thesig.toCategory2().first;
Instance abb = new Instance(sig, PSMGen.gather(ABB, sig, state));
// Instance ab = new Instance(sig, PSMGen.gather(AB, sig, state));
// //already have in interp
Instance a = new Instance(sig, PSMGen.gather(A, sig, state));
Instance b = new Instance(sig, PSMGen.gather(B, sig, state));
Transform fst = new Transform(abb, Jw, PSMGen.gather(ABB + "_fst", sig, state));
Transform snd = new Transform(abb, b, PSMGen.gather(ABB + "_snd", sig, state));
List<Pair<String, List<Pair<Object, Object>>>> data = new LinkedList<>();
for (Node n : sig.nodes) {
List<Pair<Object, Object>> d = new LinkedList<>();
Map<Object, Pair<LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object>, Transform>> m2 = map2.get(n);
for (Pair<Object, Object> id : abb.data.get(n.string)) {
Object id_ab = lookup(fst.data.get(n.string), id.first);
Object x = lookup(snd.data.get(n.string), id.first);
Transform t = m2.get(id_ab).second;
LinkedHashMap<Pair<Arr<Node, Path>, Attribute<Node>>, Object> w = m2.get(id_ab).first;
Triple<Instance, Map<Node, Map<Object, Pair<Arr<Node, Path>, Object>>>, Map<Node, Map<Pair<Arr<Node, Path>, Object>, Object>>> m = map.get(new Pair<>(n, w));
Object y = m.third.get(n).get(new Pair<>(cat.id(n), x));
Object f = lookup(t.data.get(n.string), y);
d.add(new Pair<>(id.first, f));
}
data.add(new Pair<>(n.string, d));
}
Transform curry = new Transform(abb, a, data);
PSMGen.shred(pre, curry, state);
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
}
use of catdata.fql.cat.Arr in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Return e) {
// String xxx = "return_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Delta) {
String middle = ((Delta) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Delta) i1).F.toMap(prog);
if (i2 instanceof Sigma) {
Sigma input0 = ((Sigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + f.nm.get(n) + "_subst", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else if (i2 instanceof FullSigma) {
FullSigma input0 = ((FullSigma) i2);
String input = input0.I;
for (Node n : f.source.nodes) {
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(input + "_" + n.string, middle + "_" + n.string + "_e", e.inst + "_" + n.string + "_subst"), "c0", "c1"));
}
} else {
throw new RuntimeException();
}
} else if (i1 instanceof Pi) {
String middle = ((Pi) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Pi) i1).F.toMap(prog);
if (i2 instanceof Delta) {
Delta input0 = ((Delta) i2);
String input = input0.I;
for (Node n : f.target.nodes) {
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, middle, e.inst).second;
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
if (col.length == 0) {
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
from.put("middle", input + "_" + n.string);
select.put("c0", new Pair<>("middle", "c0"));
select.put("c1", new Pair<>("lim", "guid"));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
return ret;
}
// LinkedHashMap<String, String> attrs = new LinkedHashMap<>();
// attrs.put("guid", PSM.VARCHAR());
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
from.put("lim", e.inst + "_" + n.string + "_limit");
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
int i = 0;
for (Triple<Node, Node, Arr<Node, Path>> col0 : col) {
from.put("c" + i + "_subst_inv", middle + "_" + col0.second.string + "_subst_inv");
where.add(new Pair<>(new Pair<>("lim", "c" + i), new Pair<>("c" + i + "_subst_inv", "c0")));
// attrs.put("c" + i, PSM.VARCHAR());
i++;
}
// if (col.length > 1) {
for (int j = 0; j < col.length; j++) {
if (col[j].third.arr.equals(f.target.toCategory2().second.of(new Path(f.target, n)).arr)) {
where.add(new Pair<>(new Pair<>("c" + 0 + "_subst_inv", "c1"), new Pair<>("c" + j + "_subst_inv", "c1")));
}
}
select.put("c" + 0, new Pair<>("c" + 0 + "_subst_inv", "c1"));
select.put("c1", new Pair<>("lim", "guid"));
// }
// ret.add(new CreateTable(xxx, attrs, false));
Flower flower = new Flower(select, from, where);
ret.add(new InsertSQL(env + "_" + n.string, flower, "c0", "c1"));
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
use of catdata.fql.cat.Arr in project fql by CategoricalData.
the class Transform method build.
private Pair<Graph<Quad<Node, Object, String, Boolean>, Pair<Path, Integer>>, HashMap<Quad<Node, Object, String, Boolean>, Map<Attribute<Node>, Object>>> build(String src_n, String dst_n) throws FQLException {
FinCat<Node, Path> c = src.thesig.toCategory2().first;
HashMap<Quad<Node, Object, String, Boolean>, Map<Attribute<Node>, Object>> map = new HashMap<>();
Graph<Quad<Node, Object, String, Boolean>, Pair<Path, Integer>> g2 = new DirectedSparseMultigraph<>();
for (Node n : c.objects) {
for (Pair<Object, Object> o : src.data.get(n.string)) {
Quad<Node, Object, String, Boolean> xx = new Quad<>(n, o.first, src_n, true);
g2.addVertex(xx);
List<Attribute<Node>> attrs = src.thesig.attrsFor(n);
Map<Attribute<Node>, Object> m = new HashMap<>();
for (Attribute<Node> attr : attrs) {
Object a = lookup(src.data.get(attr.name), o.first);
m.put(attr, a);
}
map.put(xx, m);
}
}
int j = 0;
for (Quad<Node, Object, String, Boolean> x : g2.getVertices()) {
for (Quad<Node, Object, String, Boolean> y : g2.getVertices()) {
if (!x.third.equals(y.third)) {
continue;
}
Set<Arr<Node, Path>> h = c.hom(x.first, y.first);
for (Arr<Node, Path> arr : h) {
if (c.isId(arr)) {
continue;
}
if (!DefunctGlobalOptions.debug.fql.ALL_GR_PATHS && arr.arr.path.size() != 1) {
continue;
}
if (doLookup(src, arr.arr, x.second, y.second)) {
g2.addEdge(new Pair<>(arr.arr, j++), x, y);
}
}
}
}
for (Node n : c.objects) {
for (Pair<Object, Object> o : dst.data.get(n.string)) {
Quad<Node, Object, String, Boolean> xx = new Quad<>(n, o.first, dst_n, false);
g2.addVertex(xx);
List<Attribute<Node>> attrs = dst.thesig.attrsFor(n);
Map<Attribute<Node>, Object> m = new HashMap<>();
for (Attribute<Node> attr : attrs) {
Object a = lookup(dst.data.get(attr.name), o.first);
m.put(attr, a);
}
map.put(xx, m);
}
}
for (Quad<Node, Object, String, Boolean> x : g2.getVertices()) {
for (Quad<Node, Object, String, Boolean> y : g2.getVertices()) {
Set<Arr<Node, Path>> h = c.hom(x.first, y.first);
for (Arr<Node, Path> arr : h) {
if (c.isId(arr)) {
continue;
}
if (!DefunctGlobalOptions.debug.fql.ALL_GR_PATHS && arr.arr.path.size() != 1) {
continue;
}
if (doLookup(dst, arr.arr, x.second, y.second)) {
g2.addEdge(new Pair<>(arr.arr, j++), x, y);
}
}
}
}
for (String k : data.keySet()) {
Set<Pair<Object, Object>> v = data.get(k);
for (Pair<Object, Object> i : v) {
Node n = src.thesig.getNode(k);
g2.addEdge(new Pair<>(null, j++), new Quad<>(n, i.first, src_n, true), new Quad<>(n, i.second, dst_n, false));
}
}
return new Pair<>(g2, map);
}
use of catdata.fql.cat.Arr in project fql by CategoricalData.
the class CategoryOfElements method build.
private static Pair<Graph<Pair<Node, Object>, Pair<Path, Integer>>, HashMap<Pair<Node, Object>, Map<Attribute<Node>, Object>>> build(Instance i) throws FQLException {
FinCat<Node, Path> c = i.thesig.toCategory2().first;
HashMap<Pair<Node, Object>, Map<Attribute<Node>, Object>> map = new HashMap<>();
Graph<Pair<Node, Object>, Pair<Path, Integer>> g2 = new DirectedSparseMultigraph<>();
for (Node n : c.objects) {
for (Pair<Object, Object> o : i.data.get(n.string)) {
Pair<Node, Object> xx = new Pair<>(n, o.first);
g2.addVertex(xx);
List<Attribute<Node>> attrs = i.thesig.attrsFor(n);
Map<Attribute<Node>, Object> m = new HashMap<>();
for (Attribute<Node> attr : attrs) {
Object a = lookup(i.data.get(attr.name), o.first);
m.put(attr, a);
}
map.put(xx, m);
}
}
int j = 0;
for (Pair<Node, Object> x : g2.getVertices()) {
for (Pair<Node, Object> y : g2.getVertices()) {
Set<Arr<Node, Path>> h = c.hom(x.first, y.first);
for (Arr<Node, Path> arr : h) {
if (c.isId(arr)) {
continue;
}
if (!DefunctGlobalOptions.debug.fql.ALL_GR_PATHS && arr.arr.path.size() != 1) {
continue;
}
if (doLookup(i, arr.arr, x.second, y.second)) {
g2.addEdge(new Pair<>(arr.arr, j++), x, y);
}
}
}
}
return new Pair<>(g2, map);
}
use of catdata.fql.cat.Arr in project fql by CategoricalData.
the class PSMGen method pi.
public static Pair<List<PSM>, Map<String, Triple<Node, Node, Arr<Node, Path>>[]>> pi(Mapping F0, String src, String dst) throws FQLException {
tempTables = 0;
Signature D0 = F0.target;
Signature C0 = F0.source;
Pair<FinCat<Node, Path>, Fn<Path, Arr<Node, Path>>> kkk = D0.toCategory2();
FinCat<Node, Path> D = kkk.first;
FinCat<Node, Path> C = C0.toCategory2().first;
FinFunctor<Node, Path, Node, Path> F = F0.toFunctor2().first;
List<PSM> ret = new LinkedList<>();
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = new HashMap<>();
Map<String, Attribute<Node>[]> amap = new HashMap<>();
List<Node> doNotDrop = new LinkedList<>();
for (Node d0 : D.objects) {
CommaCat<Node, Path, Node, Path, Node, Path> B = doComma(D, C, F, d0, D0);
Map<Triple<Node, Node, Arr<Node, Path>>, String> xxx1 = new HashMap<>();
Map<Pair<Arr<Node, Path>, Arr<Node, Path>>, String> xxx2 = new HashMap<>();
List<PSM> xxx3 = deltaX(src, xxx1, xxx2, B.projB);
ret.addAll(xxx3);
Triple<Flower, Triple<Node, Node, Arr<Node, Path>>[], Attribute<Node>[]> xxx = lim(src, C0, D, B, xxx1, xxx2);
// comma cat is empty, need unit for product
if (xxx == null) {
doNotDrop.add(d0);
Map<String, String> attrs2 = new HashMap<>();
attrs2.put("guid", PSM.VARCHAR());
ret.add(new CreateTable(dst + "_" + d0.string + "_limit", attrs2, false));
ret.add(new InsertEmptyKeygen(dst + "_" + d0.string + "_limit"));
ret.add(new InsertSQL(dst + "_" + d0.string, new SquishFlower(dst + "_" + d0.string + "_limit"), "c0", "c1"));
@SuppressWarnings("unchecked") Triple<Node, Node, Arr<Node, Path>>[] cols = new Triple[0];
colmap.put(d0.string, cols);
continue;
}
Triple<Node, Node, Arr<Node, Path>>[] cols = xxx.second;
Flower r = xxx.first;
for (Attribute<Node> a : D0.attrsFor(d0)) {
List<Attribute<Node>> ls = new LinkedList<>();
for (Attribute<Node> aa : C0.attrs) {
if (F.am.get(aa).equals(a)) {
ls.add(aa);
}
}
for (int jj = 1; jj < ls.size(); jj++) {
int xxx02 = cnamelkp(xxx.third, ls.get(0));
int xxx04 = cnamelkp(xxx.third, ls.get(jj));
r.where.add(new Pair<>(new Pair<>("t" + (xxx02 + xxx.second.length), "c1"), new Pair<>("t" + (xxx04 + xxx.second.length), "c1")));
}
}
colmap.put(d0.string, cols);
amap.put(d0.string, xxx.third);
Map<String, String> attrs1 = new HashMap<>();
for (int i = 0; i < xxx.second.length; i++) {
attrs1.put("c" + i, PSM.VARCHAR());
}
for (int j = 0; j < xxx.third.length; j++) {
attrs1.put("c" + (xxx.second.length + j), xxx.third[j].target.psm());
}
Map<String, String> attrs2 = new HashMap<>(attrs1);
attrs2.put("guid", PSM.VARCHAR());
List<String> attcs = new LinkedList<>(attrs1.keySet());
ret.add(new CreateTable(dst + "_" + d0.string + "_limnoguid", attrs1, false));
ret.add(new InsertSQL2(dst + "_" + d0.string + "_limnoguid", r, new LinkedList<>(r.select.keySet())));
ret.add(new CreateTable(dst + "_" + d0.string + "_limit", attrs2, false));
ret.add(new InsertKeygen(dst + "_" + d0.string + "_limit", "guid", dst + "_" + d0.string + "_limnoguid", attcs));
// craeted by createTables
// ret.add(new CreateTable(dst + "_" + d0.string, twocol_attrs));
ret.add(new InsertSQL(dst + "_" + d0.string, new SquishFlower(dst + "_" + d0.string + "_limit"), "c0", "c1"));
}
for (Edge s : F0.target.edges) {
Node dA = s.source;
Node dB = s.target;
String q2 = dB.string;
String q1 = dA.string;
Triple<Node, Node, Arr<Node, Path>>[] q2cols = colmap.get(q2);
Triple<Node, Node, Arr<Node, Path>>[] q1cols = colmap.get(q1);
if (q2cols == null) {
throw new RuntimeException("Cannot find " + q2 + " in " + colmap);
}
List<Pair<Pair<String, String>, Pair<String, String>>> where = subset(D, kkk.second.of(new Path(D0, s)), dst, q2cols, q1cols, q2, q1);
Map<String, String> from = new HashMap<>();
from.put(dst + "_" + q1 + "_limit_1", dst + "_" + q1 + "_limit");
from.put(dst + "_" + q2 + "_limit_2", dst + "_" + q2 + "_limit");
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>(dst + "_" + q1 + "_limit_1", "guid"));
select.put("c1", new Pair<>(dst + "_" + q2 + "_limit_2", "guid"));
Flower f = new Flower(select, from, where);
ret.add(new InsertSQL(dst + "_" + s.name, f, "c0", "c1"));
}
for (Attribute<Node> a : F0.target.attrs) {
int i = colmap.get(a.source.string).length;
Attribute<Node>[] y = amap.get(a.source.string);
if (y == null) {
throw new FQLException("Attribute mapping not surjective " + a.source.string);
}
boolean found = false;
int u = 0;
// int j = -1;
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
Map<String, String> from = new HashMap<>();
List<Integer> xxx = new LinkedList<>();
for (Attribute<Node> b : y) {
if (!F0.am.get(b).equals(a)) {
u++;
continue;
}
found = true;
xxx.add(u);
u++;
}
if (!found) {
throw new FQLException("Attribute mapping not found " + a);
}
from.put(dst + "_" + a.source + "_limit", dst + "_" + a.source + "_limit");
select.put("c0", new Pair<>(dst + "_" + a.source + "_limit", "guid"));
for (int jj = 1; jj < xxx.size(); jj++) {
where.add(new Pair<>(new Pair<>(dst + "_" + a.source + "_limit", "c" + (xxx.get(0) + i)), new Pair<>(dst + "_" + a.source + "_limit", "c" + (xxx.get(jj) + i))));
}
select.put("c1", new Pair<>(dst + "_" + a.source + "_limit", "c" + (xxx.get(0) + i)));
Flower f = new Flower(select, from, where);
ret.add(new InsertSQL(dst + "_" + a.name, f, "c0", "c1"));
// project guid and u+i
}
for (Node d0 : D.objects) {
if (doNotDrop.contains(d0)) {
continue;
}
ret.add(new DropTable(dst + "_" + d0.string + "_limnoguid"));
}
for (int ii = 0; ii < tempTables; ii++) {
ret.add(new DropTable("temp" + ii));
}
return new Pair<>(ret, colmap);
}
Aggregations