use of catdata.fql.sql.Union in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String dst, TransExp.Sigma e) {
List<PSM> ret = new LinkedList<>();
Pair<String, String> ht = e.h.type(prog);
Signature sig = prog.insts.get(ht.first).type(prog).toSig(prog);
Mapping F = ((Sigma) prog.insts.get(e.src)).F.toMap(prog);
String next = next();
ret.addAll(PSMGen.makeTables(next, sig, false));
ret.addAll(e.h.accept(next, this));
Signature sig2 = prog.insts.get(e.src).type(prog).toSig(prog);
String xxx = "sigfunc";
ret.addAll(PSMGen.makeTables(xxx, sig2, false));
for (Node n : sig2.nodes) {
List<Flower> l = new LinkedList<>();
for (Node m : F.source.nodes) {
if (F.nm.get(m).equals(n)) {
l.add(new CopyFlower(next + "_" + m.string, "c0", "c1"));
}
}
String yyy = xxx + "_" + n.string;
if (l.isEmpty()) {
ret.add(new InsertSQL(yyy, l.get(0), "c0", "c1"));
} else {
ret.add(new InsertSQL(yyy, new Union(l), "c0", "c1"));
}
ret.add(new InsertSQL(dst + "_" + n.string, PSMGen.compose(e.src + "_" + n.string + "_subst_inv", yyy, e.dst + "_" + n.string + "_subst"), "c0", "c1"));
}
ret.addAll(PSMGen.dropTables(xxx, sig2));
ret.addAll(PSMGen.dropTables(next, sig));
return ret;
}
use of catdata.fql.sql.Union in project fql by CategoricalData.
the class InstOps method visit.
@Override
public Pair<List<PSM>, Object> visit(String dst, Plus e) {
SigExp k = e.type(prog);
Signature s = k.toSig(prog);
List<PSM> ret = new LinkedList<>();
for (Node n : s.nodes) {
List<Flower> l = new LinkedList<>();
l.add(new CopyFlower(e.a + "_" + n.string, "c0", "c1"));
l.add(new CopyFlower(e.b + "_" + n.string, "c0", "c1"));
ret.add(new InsertSQL(dst + "_" + n.string, new Union(l), "c0", "c1"));
}
for (Attribute<Node> n : s.attrs) {
List<Flower> l = new LinkedList<>();
l.add(new CopyFlower(e.a + "_" + n.name, "c0", "c1"));
l.add(new CopyFlower(e.b + "_" + n.name, "c0", "c1"));
ret.add(new InsertSQL(dst + "_" + n.name, new Union(l), "c0", "c1"));
}
for (Edge n : s.edges) {
List<Flower> l = new LinkedList<>();
l.add(new CopyFlower(e.a + "_" + n.name, "c0", "c1"));
l.add(new CopyFlower(e.b + "_" + n.name, "c0", "c1"));
ret.add(new InsertSQL(dst + "_" + n.name, new Union(l), "c0", "c1"));
}
ret.addAll(PSMGen.guidify(dst, s, true));
ret.addAll(PSMGen.makeTables(dst + "_inl", s, false));
ret.addAll(PSMGen.makeTables(dst + "_inr", s, false));
for (Node n : s.nodes) {
SQL f = PSMGen.compose(e.a + "_" + n.string, dst + "_" + n.string + "_subst");
ret.add(new InsertSQL(dst + "_inl_" + n.string, f, "c0", "c1"));
SQL f0 = PSMGen.compose(e.b + "_" + n.string, dst + "_" + n.string + "_subst");
ret.add(new InsertSQL(dst + "_inr_" + n.string, f0, "c0", "c1"));
}
// (f+g) : A+B -> C f : A -> C g : B -> C
Fn<Quad<String, String, String, String>, List<PSM>> fn = x -> {
// e.a -> x.third
String f = x.first;
// e.b -> x.third
String g = x.second;
// String C = x.third;
String dst0 = x.fourth;
// must be a map dst -> x.third
List<PSM> ret1 = new LinkedList<>();
for (Node n : s.nodes) {
Flower sql1 = PSMGen.compose(dst + "_" + n.string + "_subst_inv", f + "_" + n.string);
Flower sql2 = PSMGen.compose(dst + "_" + n.string + "_subst_inv", g + "_" + n.string);
List<Flower> flowers = new LinkedList<>();
flowers.add(sql1);
flowers.add(sql2);
ret1.add(new InsertSQL(dst0 + "_" + n.string, new Union(flowers), "c0", "c1"));
}
return ret1;
};
return new Pair<>(ret, fn);
}
use of catdata.fql.sql.Union in project fql by CategoricalData.
the class InstOps method visit.
@Override
public List<PSM> visit(String env, Coreturn e) {
String xxx = "coreturn_temp_xxx";
List<PSM> ret = new LinkedList<>();
InstExp i1 = prog.insts.get(e.inst);
if (i1 instanceof Sigma) {
String middle = ((Sigma) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((Sigma) i1).F.toMap(prog);
if (i2 instanceof Delta) {
for (Node n : f.target.nodes) {
List<Flower> u = new LinkedList<>();
for (Node m : f.source.nodes) {
if (!f.nm.get(m).equals(n)) {
continue;
}
u.add(new CopyFlower(middle + "_" + m.string + "_subst_inv", "c0", "c1"));
}
ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
ret.add(new InsertSQL(xxx, new Union(u), "c0", "c1"));
ret.add(new InsertSQL(env + "_" + n.string, PSMGen.compose(e.inst + "_" + n.string + "_subst_inv", xxx), "c0", "c1"));
ret.add(new DropTable(xxx));
}
}
} else if (i1 instanceof FullSigma) {
String middle = ((FullSigma) i1).I;
// can't be null
InstExp i2 = prog.insts.get(middle);
Mapping f = ((FullSigma) i1).F.toMap(prog);
if (i2 instanceof Delta) {
ret.add(new FullSigmaCounit(f, ((Delta) i2).I, middle, e.inst, env));
} else {
throw new RuntimeException();
}
} else 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 Pi) {
Pi input0 = ((Pi) i2);
String input = input0.I;
try {
Map<String, Triple<Node, Node, Arr<Node, Path>>[]> colmap = PSMGen.pi(f, input, middle).second;
for (Node m : f.source.nodes) {
Node n = f.nm.get(m);
Triple<Node, Node, Arr<Node, Path>>[] col = colmap.get(n.string);
Triple<Node, Node, Arr<Node, Path>> toFind = new Triple<>(n, m, new Arr<>(new Path(f.target, n), n, n));
int i = 0;
boolean found = false;
for (Triple<Node, Node, Arr<Node, Path>> cand : col) {
if (cand.equals(toFind)) {
found = true;
Map<String, String> from = new HashMap<>();
from.put("lim", middle + "_" + n + "_limit");
LinkedHashMap<String, Pair<String, String>> select = new LinkedHashMap<>();
select.put("c0", new Pair<>("lim", "guid"));
select.put("c1", new Pair<>("lim", "c" + i));
List<Pair<Pair<String, String>, Pair<String, String>>> where = new LinkedList<>();
Flower flower = new Flower(select, from, where);
ret.add(new SimpleCreateTable(xxx, PSM.VARCHAR(), false));
ret.add(new InsertSQL(xxx, flower, "c0", "c1"));
ret.add(new InsertSQL(env + "_" + m, PSMGen.compose(e.inst + "_" + m + "_subst_inv", xxx), "c0", "c1"));
ret.add(new DropTable(xxx));
break;
}
i++;
}
if (!found) {
throw new RuntimeException();
}
}
} catch (FQLException fe) {
fe.printStackTrace();
throw new RuntimeException(fe.getMessage());
}
} else {
throw new RuntimeException();
}
} else {
throw new RuntimeException();
}
return ret;
}
Aggregations